Added gpa-agent.conf defaults for passphrase timeouts.
authorKrista Grothoff <krista@pep-project.org>
Fri, 24 Jun 2016 13:11:18 +0200
changeset 7639c0e286f0a23
parent 762 3836d04a9af7
child 764 547131705bf6
Added gpa-agent.conf defaults for passphrase timeouts.
src/pgp_gpg.c
src/platform_unix.c
src/platform_unix.h
src/platform_windows.cpp
     1.1 --- a/src/pgp_gpg.c	Thu Jun 23 16:18:49 2016 +0200
     1.2 +++ b/src/pgp_gpg.c	Fri Jun 24 13:11:18 2016 +0200
     1.3 @@ -11,7 +11,7 @@
     1.4  static void *gpgme;
     1.5  static struct gpg_s gpg;
     1.6  
     1.7 -static bool ensure_config_values(stringlist_t *keys, stringlist_t *values)
     1.8 +static bool ensure_config_values(stringlist_t *keys, stringlist_t *values, const char* config_file_path)
     1.9  {
    1.10      static char buf[MAX_LINELENGTH];
    1.11      int r;
    1.12 @@ -21,7 +21,7 @@
    1.13      unsigned int i;
    1.14      unsigned int found = 0;
    1.15  
    1.16 -    f = Fopen(gpg_conf(), "r");
    1.17 +    f = Fopen(config_file_path, "r");
    1.18      if (f == NULL && errno == ENOMEM)
    1.19          return false;
    1.20  
    1.21 @@ -57,10 +57,10 @@
    1.22                  }
    1.23              }
    1.24          } while (!feof(f));
    1.25 -        f = Freopen(gpg_conf(), "a", f);
    1.26 +        f = Freopen(config_file_path, "a", f);
    1.27      }
    1.28      else {
    1.29 -        f = Fopen(gpg_conf(), "w");
    1.30 +        f = Fopen(config_file_path, "w");
    1.31      }
    1.32  
    1.33      assert(f);
    1.34 @@ -81,6 +81,7 @@
    1.35      return true;
    1.36  }
    1.37  
    1.38 +
    1.39  PEP_STATUS pgp_init(PEP_SESSION session, bool in_first)
    1.40  {
    1.41      PEP_STATUS status = PEP_STATUS_OK;
    1.42 @@ -106,8 +107,8 @@
    1.43  
    1.44          stringlist_add(conf_keys, "personal-digest-preferences");
    1.45          stringlist_add(conf_values, "SHA256 SHA512 SHA384 SHA224");
    1.46 -        
    1.47 -        bResult = ensure_config_values(conf_keys, conf_values);
    1.48 +
    1.49 +        bResult = ensure_config_values(conf_keys, conf_values, gpg_conf());
    1.50  
    1.51          free_stringlist(conf_keys);
    1.52          free_stringlist(conf_values);
    1.53 @@ -118,6 +119,22 @@
    1.54              goto pep_error;
    1.55          }
    1.56  
    1.57 +        conf_keys = new_stringlist("default-cache-ttl");
    1.58 +        conf_values = new_stringlist("300");
    1.59 +
    1.60 +        stringlist_add(conf_keys, "max-cache-ttl");
    1.61 +        stringlist_add(conf_values, "1200");
    1.62 +
    1.63 +        bResult = ensure_config_values(conf_keys, conf_values, gpg_agent_conf());
    1.64 +
    1.65 +        free_stringlist(conf_keys);
    1.66 +        free_stringlist(conf_values);
    1.67 +
    1.68 +        assert(bResult);
    1.69 +        if(!bResult){
    1.70 +            status = PEP_INIT_NO_GPG_HOME; /* FIXME: Wrong error here? */
    1.71 +            goto pep_error;
    1.72 +        }
    1.73  
    1.74          gpgme = dlopen(LIBGPGME, RTLD_LAZY);
    1.75          if (gpgme == NULL) {
     2.1 --- a/src/platform_unix.c	Thu Jun 23 16:18:49 2016 +0200
     2.2 +++ b/src/platform_unix.c	Fri Jun 24 13:11:18 2016 +0200
     2.3 @@ -113,6 +113,7 @@
     2.4  
     2.5  static const char *gpg_conf_path = ".gnupg";
     2.6  static const char *gpg_conf_name = "gpg.conf";
     2.7 +static const char *gpg_agent_conf_name = "gpg-agent.conf";
     2.8  static const char *gpg_conf_empty = "# Created by pEpEngine\n";
     2.9  
    2.10  static bool ensure_gpg_home(const char **conf, const char **home){
    2.11 @@ -186,6 +187,51 @@
    2.12      return true;
    2.13  }
    2.14  
    2.15 +static bool ensure_gpg_agent_conf(const char **agent_conf){
    2.16 +    static char agent_path[MAX_PATH];
    2.17 +    static bool done = false;
    2.18 +
    2.19 +    if (!done) {
    2.20 +        const char *dirname;
    2.21 +
    2.22 +        if (!ensure_gpg_home(NULL, &dirname)) /* Then dirname won't be set. */
    2.23 +            return false;
    2.24 +
    2.25 +        char *p;
    2.26 +        p = stpncpy(agent_path, dirname, MAX_PATH);
    2.27 +        
    2.28 +        size_t len = MAX_PATH - (p - agent_path) - 2;
    2.29 +
    2.30 +        if (len < strlen(gpg_agent_conf_name))
    2.31 +        {
    2.32 +            assert(0);
    2.33 +            return false;
    2.34 +        }
    2.35 +
    2.36 +        *p++ = '/';
    2.37 +     
    2.38 +        strncpy(p, gpg_agent_conf_name, len);
    2.39 +
    2.40 +        if(access(agent_path, F_OK)){ 
    2.41 +            int fd;
    2.42 +            if(access(dirname, F_OK )) { 
    2.43 +                mkdir(dirname, S_IRUSR | S_IWUSR | S_IXUSR);
    2.44 +            }
    2.45 +
    2.46 +            fd = open(agent_path, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
    2.47 +
    2.48 +            if(fd>0) {
    2.49 +                write(fd, gpg_conf_empty, strlen(gpg_conf_empty));
    2.50 +                close(fd);
    2.51 +            }
    2.52 +        }
    2.53 +        done = true;
    2.54 +    }
    2.55 +    if(agent_conf) *agent_conf=agent_path;
    2.56 +
    2.57 +    return true;
    2.58 +}
    2.59 +
    2.60  const char *gpg_conf(void)
    2.61  {
    2.62      const char *conf;
    2.63 @@ -201,3 +247,11 @@
    2.64          return home;
    2.65      return NULL;
    2.66  }
    2.67 +
    2.68 +const char *gpg_agent_conf(void)
    2.69 +{
    2.70 +    const char *agent_conf;
    2.71 +    if(ensure_gpg_agent_conf(&agent_conf))
    2.72 +        return agent_conf;
    2.73 +    return NULL;
    2.74 +}
     3.1 --- a/src/platform_unix.h	Thu Jun 23 16:18:49 2016 +0200
     3.2 +++ b/src/platform_unix.h	Fri Jun 24 13:11:18 2016 +0200
     3.3 @@ -11,6 +11,7 @@
     3.4  const char *unix_local_db(void);
     3.5  
     3.6  const char *gpg_conf(void);
     3.7 +const char *gpg_agent_conf(void);
     3.8  const char *gpg_home(void);
     3.9  
    3.10  #ifdef ANDROID
     4.1 --- a/src/platform_windows.cpp	Thu Jun 23 16:18:49 2016 +0200
     4.2 +++ b/src/platform_windows.cpp	Fri Jun 24 13:11:18 2016 +0200
     4.3 @@ -190,6 +190,15 @@
     4.4      return path.c_str();
     4.5  }
     4.6  
     4.7 +const char *gpg_agent_conf(void)
     4.8 +{
     4.9 +    static string agent_path;
    4.10 +    if (agent_path.length() == 0)
    4.11 +        agent_path = managementPath("%APPDATA%\\gnupg", "gpg-agent.conf");
    4.12 +    return agent_path.c_str();
    4.13 +}
    4.14 +
    4.15 +
    4.16  long random(void)
    4.17  {
    4.18      unsigned int r;