GNUPGHOME
authorEdouard Tisserant
Sun, 29 Mar 2015 17:37:56 +0200
changeset 158f24ca355b574
parent 157 63eb5372f2ef
child 161 e3f9a450578f
GNUPGHOME
src/platform_unix.c
     1.1 --- a/src/platform_unix.c	Sun Mar 29 13:54:38 2015 +0200
     1.2 +++ b/src/platform_unix.c	Sun Mar 29 17:37:56 2015 +0200
     1.3 @@ -3,8 +3,10 @@
     1.4  #include <string.h>
     1.5  #include <stdlib.h>
     1.6  #include <assert.h>
     1.7 +#include <sys/stat.h>
     1.8  #include <sys/types.h>
     1.9 -#include <sys/stat.h>
    1.10 +#include <fcntl.h>
    1.11 +
    1.12  #include "platform_unix.h"
    1.13  
    1.14  #define MAX_PATH 1024
    1.15 @@ -39,35 +41,65 @@
    1.16  
    1.17  static const char *gpg_conf_path = ".gnupg";
    1.18  static const char *gpg_conf_name = "gpg.conf";
    1.19 +static const char *gpg_conf_empty = "# Created by pEpEngine\n";
    1.20  
    1.21  const char *gpg_conf(void)
    1.22  {
    1.23      static char buffer[MAX_PATH];
    1.24 +    static char dirname[MAX_PATH];
    1.25      static bool done = false;
    1.26  
    1.27      if (!done) {
    1.28 -        char *p = stpncpy(buffer, getenv("HOME"), MAX_PATH);
    1.29 -        size_t len = MAX_PATH - (p - buffer) - 3;
    1.30 +        char *gpg_home = getenv("GNUPGHOME");
    1.31 +        if(gpg_home){
    1.32 +
    1.33 +            char *p = stpncpy(buffer, gpg_home, MAX_PATH);
    1.34 +            size_t len = MAX_PATH - (p - buffer) - 2;
    1.35 +            if (len < strlen(gpg_conf_name))
    1.36 +            {
    1.37 +                assert(0);
    1.38 +                return NULL;
    1.39 +            }
    1.40 +
    1.41 +            strncpy(dirname, buffer, MAX_PATH);
    1.42 +            *p++ = '/';
    1.43 +            strncpy(p, gpg_conf_name, len);
    1.44 +
    1.45 +        }else{
    1.46  
    1.47 -        if (len < strlen(gpg_conf_path) + strlen(gpg_conf_name))
    1.48 -        {
    1.49 -            assert(0);
    1.50 -            return NULL;
    1.51 +            char *p = stpncpy(buffer, getenv("HOME"), MAX_PATH);
    1.52 +            size_t len = MAX_PATH - (p - buffer) - 3;
    1.53 +
    1.54 +            if (len < strlen(gpg_conf_path) + strlen(gpg_conf_name))
    1.55 +            {
    1.56 +                assert(0);
    1.57 +                return NULL;
    1.58 +            }
    1.59 +
    1.60 +            *p++ = '/';
    1.61 +            strncpy(p, gpg_conf_path, len);
    1.62 +            strncpy(dirname, buffer, MAX_PATH);
    1.63 +            p += strlen(gpg_conf_path);
    1.64 +            len -= strlen(gpg_conf_path) - 1;
    1.65 +            *p++ = '/';
    1.66 +            strncpy(p, gpg_conf_name, len);
    1.67          }
    1.68  
    1.69 -        *p++ = '/';
    1.70 -        strncpy(p, gpg_conf_path, len);
    1.71 +        if(access(buffer, F_OK)){ 
    1.72 +            int fd;
    1.73 +            if(access(dirname, F_OK )) { 
    1.74 +                mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
    1.75 +            }
    1.76  
    1.77 -        mkdir(p, 0700);
    1.78 -        // we ignore the return value intentionally
    1.79 +            fd = open(buffer, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    1.80  
    1.81 -        p += strlen(gpg_conf_path);
    1.82 -        len -= strlen(gpg_conf_path) - 1;
    1.83 -        *p++ = '/';
    1.84 -        strncpy(p, gpg_conf_name, len);
    1.85 +            if(fd>0) {
    1.86 +                write(fd, gpg_conf_empty, strlen(gpg_conf_empty));
    1.87 +                close(fd);
    1.88 +            }
    1.89 +        }
    1.90  
    1.91          done = true;
    1.92      }
    1.93 -
    1.94      return buffer;
    1.95  }