WIP: ENGINE-524 unix_local_db_file() ENGINE-524
authorClaudio Luck <claudio.luck@pep.foundation>
Thu, 16 May 2019 14:15:12 +0200
branchENGINE-524
changeset 373473fbd2821d46
parent 3673 04873e5d64f4
child 3735 25b31318dcdc
WIP: ENGINE-524 unix_local_db_file()
src/pgp_sequoia.c
src/platform_unix.c
     1.1 --- a/src/pgp_sequoia.c	Mon May 13 19:31:43 2019 +0200
     1.2 +++ b/src/pgp_sequoia.c	Thu May 16 14:15:12 2019 +0200
     1.3 @@ -123,11 +123,11 @@
     1.4  
     1.5  PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
     1.6  {
     1.7 -     PEP_STATUS status = PEP_STATUS_OK;
     1.8 +    PEP_STATUS status = PEP_STATUS_OK;
     1.9  
    1.10      char path[MAX_PATH];
    1.11 -    if (!unix_local_db_file(path, SQ_KEYS_DB))
    1.12 -        ERROR_OUT(NULL, PEP_INIT_GPGME_INIT_FAILED,
    1.13 +    if ((status = unix_local_db_file(path, SQ_KEYS_DB)) != PEP_STATUS_OK)
    1.14 +        ERROR_OUT(NULL, status,
    1.15                    "could not determine path to keys DB");
    1.16  
    1.17      int sqlite_result;
     2.1 --- a/src/platform_unix.c	Mon May 13 19:31:43 2019 +0200
     2.2 +++ b/src/platform_unix.c	Thu May 16 14:15:12 2019 +0200
     2.3 @@ -15,18 +15,18 @@
     2.4  #include <assert.h>
     2.5  #include <sys/stat.h>
     2.6  #include <sys/types.h>
     2.7 +#include <errno.h>
     2.8  #include <fcntl.h>
     2.9  #include <regex.h>
    2.10 -// #include <stdio.h>
    2.11  
    2.12  #include "platform_unix.h"
    2.13  
    2.14  #define MAX_PATH 1024
    2.15  #ifndef LOCAL_DB_FILENAME
    2.16 -#define LOCAL_DB_FILENAME "pEp_management.db"  /* dot (hidden file) now added in *_local_db() */
    2.17 +#define LOCAL_DB_FILENAME "management.db"      /* ".pEp_" prefix now added in *_local_db() */
    2.18  #endif
    2.19  #ifndef LOCAL_KEYS_DB_FILENAME
    2.20 -#define LOCAL_KEYS_DB_FILENAME "pEp_keys.db"
    2.21 +#define LOCAL_KEYS_DB_FILENAME "keys.db"
    2.22  #endif
    2.23  #define SYSTEM_DB_FILENAME "system.db"
    2.24  #ifndef SYSTEM_DB_PREFIX
    2.25 @@ -193,15 +193,15 @@
    2.26      {
    2.27          const char *home_env;
    2.28          const char *subdir;
    2.29 -        /* TODO: ugly data layout, maybe switch to nested struct */
    2.30 -        const char * const confvars[] = { "TRUSTWORDS", "PEP_HOME", NULL,             "HOME",  NULL };
    2.31 -        const char * const confvals[] = { NULL,         NULL,       SYSTEM_DB_PREFIX, NULL,    NULL };
    2.32 -        const char * const confsdir[] = { "",           "",         "",               "/.pEp", NULL };
    2.33 -        const bool confisimportant[] =  { true,         false,      false,            false,   false };
    2.34 +        const char * const confvars[] = { "TRUSTWORDS", "PEP_HOME", "PEPHOME", NULL,             "HOME",   NULL };
    2.35 +        const char * const confvals[] = { NULL,         NULL,       NULL,      SYSTEM_DB_PREFIX, NULL,     NULL };
    2.36 +        const char * const confsdir[] = { "/",          "/",        "/",       "/",              "/.pEp/", NULL };
    2.37 +        const bool automkdir[]        = { false,        true,       true,      false,            true,     false }; // use this on dirs only!
    2.38 +        const bool enforceifset[]     = { true,         false,      false,     false,            false,    false };
    2.39          int cf_i;
    2.40 +
    2.41          for (cf_i = 0; confvars[cf_i] || confvals[cf_i]; cf_i++) {
    2.42              if (((home_env = confvals[cf_i]) || (home_env = getenv (confvars[cf_i]))) && (subdir = confsdir[cf_i])) {
    2.43 -                // printf("unix_system_db (%s) [%s] %s\n", SYSTEM_DB_FILENAME, confvars[cf_i], home_env);
    2.44                  char *p = stpncpy (buffer, home_env, MAX_PATH);
    2.45                  ssize_t len = MAX_PATH - (p - buffer) - 2;
    2.46  
    2.47 @@ -211,17 +211,21 @@
    2.48                  }
    2.49  
    2.50                  p = stpncpy(p, confsdir[cf_i], len);
    2.51 -                *p++ = '/';
    2.52 +                if (automkdir[cf_i]) {
    2.53 +                    if (mkdir(buffer, S_IRUSR | S_IWUSR | S_IXUSR) != 0 && errno != EEXIST) {
    2.54 +                        perror(SYSTEM_DB_FILENAME);
    2.55 +                        return false;
    2.56 +                    }
    2.57 +                }
    2.58 +
    2.59                  strncpy(p, SYSTEM_DB_FILENAME, len);
    2.60 -                // printf("unix_system_db (%s) [%s] -> %s\n", SYSTEM_DB_FILENAME, confvars[cf_i], buffer);
    2.61                  if (access (buffer, R_OK) == 0) {
    2.62                      done = true;
    2.63                      return buffer;
    2.64                  }
    2.65 -                else if (confisimportant[cf_i])
    2.66 +                else if (enforceifset[cf_i])
    2.67                      return NULL;
    2.68              }
    2.69 -            // printf("unix_system_db (%s) %s failed\n", SYSTEM_DB_FILENAME, confvars[cf_i]);
    2.70          }
    2.71          return NULL;
    2.72      }
    2.73 @@ -268,42 +272,56 @@
    2.74  #endif
    2.75  
    2.76  #ifdef NDEBUG
    2.77 -int unix_local_db_file(char *buffer, const char *fname)
    2.78 +PEP_STATUS unix_local_db_file(const char *buffer, const char *fname)
    2.79  #else
    2.80 -int unix_local_db_file(char *buffer, const char *fname, int reset)
    2.81 +PEP_STATUS unix_local_db_file(const char *buffer, const char *fname, int reset)
    2.82  #endif
    2.83  {
    2.84 +    /*
    2.85 +     * TODO: move this to libpEpAdapter and create an have an export symbol here only.
    2.86 +     */
    2.87      const char *home_env;
    2.88      const char *subdir;
    2.89 -    /* TODO: ugly data layout, maybe switch to nested struct */
    2.90 -    /* Note: in HOME, a dot is prepended to the file (~/.pEp_management.db, vs ~/.pEp/pEp_management.db) */
    2.91 -    const char * const confvars[] = { "PEP_HOME", "HOME",  "HOME",   NULL };
    2.92 -    const char * const confvals[] = { NULL,       NULL,    NULL,     NULL };
    2.93 -    const char * const confsdir[] = { "/",        "/.",    "/.pEp/", NULL };
    2.94 -    const bool confisimportant[] =  { true,       false,   true,     false };
    2.95 +    /* Note: in HOME, a dot and pEp_ is prepended to the file (~/.pEp_management.db, vs ~/.pEp/management.db) */
    2.96 +    const char * const confvars[] = { "PEP_HOME", "PEPHOME", "HOME",   "HOME",   NULL };
    2.97 +    const char * const confvals[] = { NULL,       NULL,      NULL,     NULL,     NULL };
    2.98 +    const char * const confsdir[] = { "/",        "/",       "/.pEp_", "/.pEp/", NULL };
    2.99 +    const bool automkdir[]        = { true,       true,      false,    true,     false }; // use this on dirs only!
   2.100 +    const bool debugonly[]        = { true,       true,      false,    false,    false };
   2.101 +    const bool enforceifset[]     = { true,       true,      false,    true,     false };
   2.102      int cf_i;
   2.103  
   2.104      for (cf_i = 0; confvars[cf_i] || confvals[cf_i]; cf_i++) {
   2.105          if (((home_env = confvals[cf_i]) || (home_env = getenv (confvars[cf_i]))) && (subdir = confsdir[cf_i])) {
   2.106 -            // printf("unix_local_db_file(%s) [%s] %s\n", fname, confvars[cf_i], home_env);
   2.107              char *p = stpncpy (buffer, home_env, MAX_PATH);
   2.108              ssize_t len = MAX_PATH - (p - buffer) - 1;
   2.109  
   2.110              if (len < strlen (fname) + strlen (confsdir[cf_i])) {
   2.111                  assert(0);
   2.112 -                return false;
   2.113 +                return PEP_OUT_OF_MEMORY;
   2.114              }
   2.115  
   2.116 +#ifdef NDEBUG
   2.117 +            if (debugonly[cf_i] && enforceifset[cf_i]) {
   2.118 +                printf("WARNING: Variable '%s' MUST NOT be used in production!\n", confvars[cf_i]);
   2.119 +                // return PEP_INIT_CANNOT_OPEN_DB;
   2.120 +            }
   2.121 +#endif
   2.122 +
   2.123              p = stpncpy(p, confsdir[cf_i], len);
   2.124 +            if (automkdir[cf_i]) {
   2.125 +                if (mkdir(buffer, S_IRUSR | S_IWUSR | S_IXUSR) != 0 && errno != EEXIST) {
   2.126 +                    return PEP_INIT_CANNOT_OPEN_DB;
   2.127 +                }
   2.128 +            }
   2.129 +
   2.130              strncpy(p, fname, len);
   2.131 -            // printf("unix_local_db_file(%s) [%s] -> %s\n", fname, confvars[cf_i], buffer);
   2.132 -            if (confisimportant[cf_i] || (access (buffer, R_OK) == 0)) {
   2.133 -                return true;
   2.134 +            if (enforceifset[cf_i] || (access (buffer, R_OK) == 0)) {
   2.135 +                return PEP_STATUS_OK;
   2.136              }
   2.137          }
   2.138 -        // printf("unix_local_db_file(%s) %s failed\n", fname, confvars[cf_i]);
   2.139      }
   2.140 -    return false;
   2.141 +    return PEP_UNKNOWN_DB_ERROR;
   2.142  }
   2.143  
   2.144  #ifdef NDEBUG