reinstert unix_local_db_file ENGINE-524
authorClaudio Luck <claudio.luck@pep.foundation>
Mon, 13 May 2019 19:31:43 +0200
branchENGINE-524
changeset 367304873e5d64f4
parent 3672 80b56b8c68dd
child 3734 73fbd2821d46
reinstert unix_local_db_file
src/pgp_sequoia.c
src/platform_unix.c
     1.1 --- a/src/pgp_sequoia.c	Mon May 13 19:30:36 2019 +0200
     1.2 +++ b/src/pgp_sequoia.c	Mon May 13 19:31:43 2019 +0200
     1.3 @@ -5,7 +5,9 @@
     1.4  
     1.5  #define _GNU_SOURCE 1
     1.6  
     1.7 -#include "platform.h"
     1.8 +#define MAX_PATH 1024
     1.9 +#define SQ_KEYS_DB "keys.db"
    1.10 +
    1.11  #include "pEp_internal.h"
    1.12  #include "pgp_gpg.h"
    1.13  
    1.14 @@ -121,31 +123,12 @@
    1.15  
    1.16  PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
    1.17  {
    1.18 -    #define PATH "/.pEp_keys.db"
    1.19 - 
    1.20 -    PEP_STATUS status = PEP_STATUS_OK;
    1.21 +     PEP_STATUS status = PEP_STATUS_OK;
    1.22  
    1.23 -    // Create the home directory.
    1.24 -    char *home_env = NULL;
    1.25 -#ifndef NDEBUG
    1.26 -    home_env = getenv("PEP_HOME");
    1.27 -#endif
    1.28 -    if (!home_env)
    1.29 -        home_env = getenv("HOME");
    1.30 -    if (!home_env)
    1.31 -        ERROR_OUT(NULL, PEP_INIT_GPGME_INIT_FAILED, "HOME unset");
    1.32 -
    1.33 -    // Create the DB and initialize it.
    1.34 -    size_t path_size = strlen(home_env) + sizeof(PATH);
    1.35 -    char *path = (char *) calloc(1, path_size);
    1.36 -    assert(path);
    1.37 -    if (!path)
    1.38 -        ERROR_OUT(NULL, PEP_OUT_OF_MEMORY, "out of memory");
    1.39 -
    1.40 -    int r = snprintf(path, path_size, "%s/.pEp_keys.db", home_env);
    1.41 -    assert(r >= 0 && r < path_size);
    1.42 -    if (r < 0)
    1.43 -        ERROR_OUT(NULL, PEP_UNKNOWN_ERROR, "snprintf");
    1.44 +    char path[MAX_PATH];
    1.45 +    if (!unix_local_db_file(path, SQ_KEYS_DB))
    1.46 +        ERROR_OUT(NULL, PEP_INIT_GPGME_INIT_FAILED,
    1.47 +                  "could not determine path to keys DB");
    1.48  
    1.49      int sqlite_result;
    1.50      sqlite_result = sqlite3_open_v2(path,
    1.51 @@ -155,7 +138,7 @@
    1.52                                      | SQLITE_OPEN_FULLMUTEX
    1.53                                      | SQLITE_OPEN_PRIVATECACHE,
    1.54                                      NULL);
    1.55 -    free(path);
    1.56 +
    1.57      if (sqlite_result != SQLITE_OK)
    1.58          ERROR_OUT(NULL, PEP_INIT_CANNOT_OPEN_DB,
    1.59                    "opening keys DB: %s", sqlite3_errmsg(session->key_db));
     2.1 --- a/src/platform_unix.c	Mon May 13 19:30:36 2019 +0200
     2.2 +++ b/src/platform_unix.c	Mon May 13 19:31:43 2019 +0200
     2.3 @@ -17,14 +17,21 @@
     2.4  #include <sys/types.h>
     2.5  #include <fcntl.h>
     2.6  #include <regex.h>
     2.7 +// #include <stdio.h>
     2.8  
     2.9  #include "platform_unix.h"
    2.10  
    2.11  #define MAX_PATH 1024
    2.12  #ifndef LOCAL_DB_FILENAME
    2.13 -#define LOCAL_DB_FILENAME ".pEp_management.db"
    2.14 +#define LOCAL_DB_FILENAME "pEp_management.db"  /* dot (hidden file) now added in *_local_db() */
    2.15 +#endif
    2.16 +#ifndef LOCAL_KEYS_DB_FILENAME
    2.17 +#define LOCAL_KEYS_DB_FILENAME "pEp_keys.db"
    2.18  #endif
    2.19  #define SYSTEM_DB_FILENAME "system.db"
    2.20 +#ifndef SYSTEM_DB_PREFIX
    2.21 +#define SYSTEM_DB_PREFIX "/usr/local/share/pEp"
    2.22 +#endif
    2.23  
    2.24  #ifndef bool
    2.25  #define bool int
    2.26 @@ -169,6 +176,58 @@
    2.27  
    2.28  #endif
    2.29  
    2.30 +#ifdef NDEBUG
    2.31 +const char *unix_system_db(void)
    2.32 +#else
    2.33 +const char *unix_system_db(int reset)
    2.34 +#endif
    2.35 +{
    2.36 +    static char buffer[MAX_PATH];
    2.37 +    static bool done = false;
    2.38 +
    2.39 +    #ifdef NDEBUG
    2.40 +    if (!done)
    2.41 +    #else
    2.42 +    if ((!done) || reset)
    2.43 +    #endif
    2.44 +    {
    2.45 +        const char *home_env;
    2.46 +        const char *subdir;
    2.47 +        /* TODO: ugly data layout, maybe switch to nested struct */
    2.48 +        const char * const confvars[] = { "TRUSTWORDS", "PEP_HOME", NULL,             "HOME",  NULL };
    2.49 +        const char * const confvals[] = { NULL,         NULL,       SYSTEM_DB_PREFIX, NULL,    NULL };
    2.50 +        const char * const confsdir[] = { "",           "",         "",               "/.pEp", NULL };
    2.51 +        const bool confisimportant[] =  { true,         false,      false,            false,   false };
    2.52 +        int cf_i;
    2.53 +        for (cf_i = 0; confvars[cf_i] || confvals[cf_i]; cf_i++) {
    2.54 +            if (((home_env = confvals[cf_i]) || (home_env = getenv (confvars[cf_i]))) && (subdir = confsdir[cf_i])) {
    2.55 +                // printf("unix_system_db (%s) [%s] %s\n", SYSTEM_DB_FILENAME, confvars[cf_i], home_env);
    2.56 +                char *p = stpncpy (buffer, home_env, MAX_PATH);
    2.57 +                ssize_t len = MAX_PATH - (p - buffer) - 2;
    2.58 +
    2.59 +                if (len < strlen (SYSTEM_DB_FILENAME) + strlen (confsdir[cf_i])) {
    2.60 +                    assert(0);
    2.61 +                    return NULL;
    2.62 +                }
    2.63 +
    2.64 +                p = stpncpy(p, confsdir[cf_i], len);
    2.65 +                *p++ = '/';
    2.66 +                strncpy(p, SYSTEM_DB_FILENAME, len);
    2.67 +                // printf("unix_system_db (%s) [%s] -> %s\n", SYSTEM_DB_FILENAME, confvars[cf_i], buffer);
    2.68 +                if (access (buffer, R_OK) == 0) {
    2.69 +                    done = true;
    2.70 +                    return buffer;
    2.71 +                }
    2.72 +                else if (confisimportant[cf_i])
    2.73 +                    return NULL;
    2.74 +            }
    2.75 +            // printf("unix_system_db (%s) %s failed\n", SYSTEM_DB_FILENAME, confvars[cf_i]);
    2.76 +        }
    2.77 +        return NULL;
    2.78 +    }
    2.79 +    return buffer;
    2.80 +}
    2.81 +
    2.82  #if !defined(BSD) && !defined(__APPLE__)
    2.83  
    2.84  size_t strlcpy(char* dst, const	char* src, size_t size) {
    2.85 @@ -209,6 +268,45 @@
    2.86  #endif
    2.87  
    2.88  #ifdef NDEBUG
    2.89 +int unix_local_db_file(char *buffer, const char *fname)
    2.90 +#else
    2.91 +int unix_local_db_file(char *buffer, const char *fname, int reset)
    2.92 +#endif
    2.93 +{
    2.94 +    const char *home_env;
    2.95 +    const char *subdir;
    2.96 +    /* TODO: ugly data layout, maybe switch to nested struct */
    2.97 +    /* Note: in HOME, a dot is prepended to the file (~/.pEp_management.db, vs ~/.pEp/pEp_management.db) */
    2.98 +    const char * const confvars[] = { "PEP_HOME", "HOME",  "HOME",   NULL };
    2.99 +    const char * const confvals[] = { NULL,       NULL,    NULL,     NULL };
   2.100 +    const char * const confsdir[] = { "/",        "/.",    "/.pEp/", NULL };
   2.101 +    const bool confisimportant[] =  { 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 +            }
   2.114 +
   2.115 +            p = stpncpy(p, confsdir[cf_i], len);
   2.116 +            strncpy(p, fname, len);
   2.117 +            // printf("unix_local_db_file(%s) [%s] -> %s\n", fname, confvars[cf_i], buffer);
   2.118 +            if (confisimportant[cf_i] || (access (buffer, R_OK) == 0)) {
   2.119 +                return true;
   2.120 +            }
   2.121 +        }
   2.122 +        // printf("unix_local_db_file(%s) %s failed\n", fname, confvars[cf_i]);
   2.123 +    }
   2.124 +    return false;
   2.125 +}
   2.126 +
   2.127 +#ifdef NDEBUG
   2.128  const char *unix_local_db(void)
   2.129  #else
   2.130  const char *unix_local_db(int reset)
   2.131 @@ -219,31 +317,41 @@
   2.132  
   2.133      #ifdef NDEBUG
   2.134      if (!done)
   2.135 +        done = unix_local_db_file(buffer, LOCAL_DB_FILENAME);
   2.136      #else
   2.137      if ((!done) || reset)
   2.138 +        done = unix_local_db_file(buffer, LOCAL_DB_FILENAME, reset);
   2.139      #endif
   2.140 -    {
   2.141 -        char *home_env;
   2.142 -        if((home_env = getenv("HOME"))){
   2.143 -            char *p = stpncpy(buffer, home_env, MAX_PATH);
   2.144 -            ssize_t len = MAX_PATH - (p - buffer) - 2;
   2.145  
   2.146 -            if (len < strlen(LOCAL_DB_FILENAME)) {
   2.147 -                assert(0);
   2.148 -                return NULL;
   2.149 -            }
   2.150 +    if (done)
   2.151 +        return buffer;
   2.152 +    return NULL;
   2.153 +}
   2.154  
   2.155 -            *p++ = '/';
   2.156 -            strncpy(p, LOCAL_DB_FILENAME, len);
   2.157 -            done = true;
   2.158 -        }else{
   2.159 -            return NULL;
   2.160 -        }
   2.161 +#ifdef NDEBUG
   2.162 +const char *unix_local_keys_db(void)
   2.163 +#else
   2.164 +const char *unix_local_keys_db(int reset)
   2.165 +#endif
   2.166 +{
   2.167 +    static char buffer[MAX_PATH];
   2.168 +    static bool done = false;
   2.169  
   2.170 +    #ifdef NDEBUG
   2.171 +    if (!done)
   2.172 +        done = unix_local_db_file(buffer, LOCAL_KEYS_DB_FILENAME);
   2.173 +    #else
   2.174 +    if ((!done) || reset)
   2.175 +        done = unix_local_db_file(buffer, LOCAL_KEYS_DB_FILENAME, reset);
   2.176 +    #endif
   2.177 +
   2.178 +    if (done) {
   2.179 +        return buffer;
   2.180      }
   2.181 -    return buffer;
   2.182 +    return NULL;
   2.183  }
   2.184  
   2.185 +
   2.186  static const char *gpg_conf_path = ".gnupg";
   2.187  static const char *gpg_conf_name = "gpg.conf";
   2.188  static const char *gpg_agent_conf_name = "gpg-agent.conf";