I'm creating a Linux software that will have two types of databases:
- Public: stores information that anyone on
my_program_group
has access (without root privileges); - Private: only users with sudo access can open (common users on
my_program_group
can't). It also means that the database file is owned by root.
The first kind of the database is working fine.
The problem is: my program doesn't need to be run with sudo; so, if at some point a common user needs to open a private database, he will need to access root privileges (raise privileges). I don't want to ask restarting the execution with sudo, instead, just request for password.
#include <sqlite3.h>
#include <stdio.h>
// Private database path
#define PATH = "/tmp/test/database.db"
void private_db(sqlite3 **db);
int main() {
sqlite3 *db;
// When the user needs to open the private database, this function will be called:
private_db(&db);
// Continue...
return 0;
}
void private_db(sqlite3 **db){
int rc;
// NEED TO RUN THIS AS ROOT:
rc = sqlite3_open(PATH, db);
if(rc != SQLITE_OK) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg( &(**db) ));
exit (1);
} else {
printf("Database successfully openend\n");
}
}
(As expected) If I run the code above, sqlite won't be able to open the database because of the system permissions.