Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.sbin/makemandb PR/51038: Abhinav Upadhyay: check for acc...



details:   https://anonhg.NetBSD.org/src/rev/6a252ca86617
branches:  trunk
changeset: 814867:6a252ca86617
user:      christos <christos%NetBSD.org@localhost>
date:      Wed Apr 13 01:37:50 2016 +0000

description:
PR/51038: Abhinav Upadhyay: check for access permissions to the sqlite database

diffstat:

 usr.sbin/makemandb/apropos-utils.c |  38 +++++++++++++++++++++++++++-----------
 usr.sbin/makemandb/apropos-utils.h |  13 ++++++++-----
 2 files changed, 35 insertions(+), 16 deletions(-)

diffs (122 lines):

diff -r 88d9c431cc24 -r 6a252ca86617 usr.sbin/makemandb/apropos-utils.c
--- a/usr.sbin/makemandb/apropos-utils.c        Wed Apr 13 01:32:00 2016 +0000
+++ b/usr.sbin/makemandb/apropos-utils.c        Wed Apr 13 01:37:50 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: apropos-utils.c,v 1.22 2016/03/31 20:16:58 christos Exp $      */
+/*     $NetBSD: apropos-utils.c,v 1.23 2016/04/13 01:37:50 christos Exp $      */
 /*-
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay%gmail.com@localhost>
  * All rights reserved.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos-utils.c,v 1.22 2016/03/31 20:16:58 christos Exp $");
+__RCSID("$NetBSD: apropos-utils.c,v 1.23 2016/04/13 01:37:50 christos Exp $");
 
 #include <sys/queue.h>
 #include <sys/stat.h>
@@ -300,7 +300,7 @@
  *     In normal cases the function should return a handle to the db.
  */
 sqlite3 *
-init_db(int db_flag, const char *manconf)
+init_db(mandb_access_mode db_flag, const char *manconf)
 {
        sqlite3 *db = NULL;
        sqlite3_stmt *stmt;
@@ -311,10 +311,10 @@
        char *dbpath = get_dbpath(manconf);
        if (dbpath == NULL)
                errx(EXIT_FAILURE, "_mandb entry not found in man.conf");
-       /* Check if the database exists or not */
+
        if (!(stat(dbpath, &sb) == 0 && S_ISREG(sb.st_mode))) {
-               /* Database does not exist, check if DB_CREATE was specified, and set
-                * flag to create the database schema
+               /* Database does not exist, check if DB_CREATE was specified,
+                * and set flag to create the database schema
                 */
                if (db_flag != (MANDB_CREATE)) {
                        warnx("Missing apropos database. "
@@ -322,16 +322,33 @@
                        return NULL;
                }
                create_db_flag = 1;
+       } else {
+               /*
+                * Database exists. Check if we have the permissions
+                * to read/write the files
+                */
+               int access_mode = R_OK;
+               switch (access_mode) {
+               case MANDB_CREATE:
+               case MANDB_WRITE:
+                       access_mode |= W_OK;
+                       break;
+               default:
+                       break;
+               }
+               if ((access(dbpath, access_mode)) != 0) {
+                       warnx("Unable to access the database, please check"
+                           " permissions for `%s'", dbpath);
+                       return NULL;
+               }
        }
 
-       /* Now initialize the database connection */
        sqlite3_initialize();
        rc = sqlite3_open_v2(dbpath, &db, db_flag, NULL);
 
        if (rc != SQLITE_OK) {
                warnx("%s", sqlite3_errmsg(db));
-               sqlite3_shutdown();
-               return NULL;
+               goto error;
        }
 
        if (create_db_flag && create_db(db) < 0) {
@@ -379,8 +396,7 @@
        return db;
 
 error:
-       sqlite3_close(db);
-       sqlite3_shutdown();
+       close_db(db);
        return NULL;
 }
 
diff -r 88d9c431cc24 -r 6a252ca86617 usr.sbin/makemandb/apropos-utils.h
--- a/usr.sbin/makemandb/apropos-utils.h        Wed Apr 13 01:32:00 2016 +0000
+++ b/usr.sbin/makemandb/apropos-utils.h        Wed Apr 13 01:37:50 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: apropos-utils.h,v 1.9 2013/04/02 17:16:50 christos Exp $       */
+/*     $NetBSD: apropos-utils.h,v 1.10 2016/04/13 01:37:50 christos Exp $      */
 /*-
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay%gmail.com@localhost>
  * All rights reserved.
@@ -39,9 +39,12 @@
 #define SECMAX 9
 
 /* Flags for opening the database */
-#define MANDB_READONLY SQLITE_OPEN_READONLY
-#define MANDB_WRITE SQLITE_OPEN_READWRITE
-#define MANDB_CREATE SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
+typedef enum mandb_access_mode {
+       MANDB_READONLY = SQLITE_OPEN_READONLY,
+       MANDB_WRITE = SQLITE_OPEN_READWRITE,
+       MANDB_CREATE = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
+} mandb_access_mode;
+
 
 #define APROPOS_SCHEMA_VERSION 20120507
 
@@ -92,7 +95,7 @@
 char *lower(char *);
 void concat(char **, const char *);
 void concat2(char **, const char *, size_t);
-sqlite3 *init_db(int, const char *);
+sqlite3 *init_db(mandb_access_mode, const char *);
 void close_db(sqlite3 *);
 char *get_dbpath(const char *);
 int run_query(sqlite3 *, query_format, query_args *);



Home | Main Index | Thread Index | Old Index