Module Name: src Committed By: christos Date: Wed Apr 13 01:37:50 UTC 2016
Modified Files: src/usr.sbin/makemandb: apropos-utils.c apropos-utils.h Log Message: PR/51038: Abhinav Upadhyay: check for access permissions to the sqlite database To generate a diff of this commit: cvs rdiff -u -r1.22 -r1.23 src/usr.sbin/makemandb/apropos-utils.c cvs rdiff -u -r1.9 -r1.10 src/usr.sbin/makemandb/apropos-utils.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.sbin/makemandb/apropos-utils.c diff -u src/usr.sbin/makemandb/apropos-utils.c:1.22 src/usr.sbin/makemandb/apropos-utils.c:1.23 --- src/usr.sbin/makemandb/apropos-utils.c:1.22 Thu Mar 31 16:16:58 2016 +++ src/usr.sbin/makemandb/apropos-utils.c Tue Apr 12 21:37:50 2016 @@ -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.upadh...@gmail.com> * 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 @@ get_dbpath(const char *manconf) * 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 @@ init_db(int db_flag, const char *manconf 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 @@ init_db(int db_flag, const char *manconf 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 @@ init_db(int db_flag, const char *manconf return db; error: - sqlite3_close(db); - sqlite3_shutdown(); + close_db(db); return NULL; } Index: src/usr.sbin/makemandb/apropos-utils.h diff -u src/usr.sbin/makemandb/apropos-utils.h:1.9 src/usr.sbin/makemandb/apropos-utils.h:1.10 --- src/usr.sbin/makemandb/apropos-utils.h:1.9 Tue Apr 2 13:16:50 2013 +++ src/usr.sbin/makemandb/apropos-utils.h Tue Apr 12 21:37:50 2016 @@ -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.upadh...@gmail.com> * 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 @@ typedef enum query_format { 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 *);