more than one config value
authorvb
Thu, 26 Feb 2015 16:13:23 +0100
changeset 78c50876ee5dcd
parent 77 2c60cebca522
child 79 2363a3f35fd2
more than one config value
src/pgp_gpg.c
     1.1 --- a/src/pgp_gpg.c	Thu Feb 26 14:31:03 2015 +0100
     1.2 +++ b/src/pgp_gpg.c	Thu Feb 26 16:13:23 2015 +0100
     1.3 @@ -1,6 +1,8 @@
     1.4  #include "pgp_gpg.h"
     1.5  #include "pEp_internal.h"
     1.6  
     1.7 +#include <limits.h>
     1.8 +
     1.9  #include "wrappers.h"
    1.10  
    1.11  #define _GPGERR(X) ((X) & 0xffffL)
    1.12 @@ -8,12 +10,16 @@
    1.13  static void *gpgme;
    1.14  static struct gpg_s gpg;
    1.15  
    1.16 -static bool ensure_keyserver()
    1.17 +static bool ensure_config_values(stringlist_t *keys, stringlist_t *values)
    1.18  {
    1.19      static char buf[MAX_LINELENGTH];
    1.20      int n;
    1.21 +    unsigned int i;
    1.22 +    int r;
    1.23      FILE *f;
    1.24 -    int r;
    1.25 +    stringlist_t *_k;
    1.26 +    stringlist_t *_v;
    1.27 +    unsigned int found = 0;
    1.28  
    1.29      f = Fopen(gpg_conf(), "r");
    1.30      assert(f);
    1.31 @@ -21,20 +27,34 @@
    1.32          return false;
    1.33  
    1.34      if (f != NULL) {
    1.35 +        int length = stringlist_length(keys);
    1.36 +
    1.37 +        assert(length <= sizeof(unsigned int) * CHAR_BIT);
    1.38 +        assert(length == stringlist_length(values));
    1.39 +
    1.40 +        unsigned int n = (1 << length) - 1;
    1.41 +
    1.42          do {
    1.43              char * s;
    1.44  
    1.45              s = Fgets(buf, MAX_LINELENGTH, f);
    1.46 -            assert(s);
    1.47 -            if (s == NULL)
    1.48 -                return false;
    1.49 +            if (!feof(f)) {
    1.50 +                assert(s);
    1.51 +                if (s == NULL)
    1.52 +                    return false;
    1.53  
    1.54 -            if (s && !feof(f)) {
    1.55 -                char * t = strtok(s, " ");
    1.56 -                if (t && strcmp(t, "keyserver") == 0) {
    1.57 -                    r = Fclose(f);
    1.58 -                    assert(r == 0);
    1.59 -                    return true;
    1.60 +                if (s && !feof(f)) {
    1.61 +                    char * t = strtok(s, " ");
    1.62 +                    for (i = 1, _k = keys, _v = values; _k != NULL;
    1.63 +                            _k = _k->next, _v = _v->next, i <<= 1) {
    1.64 +                        if (t && strcmp(t, _k->value) == 0)
    1.65 +                            found |= i;
    1.66 +
    1.67 +                        if (i == n) {
    1.68 +                            r = Fclose(f);
    1.69 +                            return true;
    1.70 +                        }
    1.71 +                    }
    1.72                  }
    1.73              }
    1.74          } while (!feof(f));
    1.75 @@ -48,8 +68,13 @@
    1.76      if (f == NULL)
    1.77          return false;
    1.78  
    1.79 -    n = Fprintf(f, "keyserver %s\n", DEFAULT_KEYSERVER);
    1.80 -    assert(n >= 0);
    1.81 +    for (i = 1, _k = keys, _v = values; _k != NULL; _k = _k->next,
    1.82 +            _v = _v->next, i <<= 1) {
    1.83 +        if ((found & i) == 0) {
    1.84 +            n = Fprintf(f, "%s %s\n", _k->value, _v->value);
    1.85 +            assert(n >= 0);
    1.86 +        }
    1.87 +    }
    1.88  
    1.89      r = Fclose(f);
    1.90      assert(r == 0);
    1.91 @@ -64,9 +89,17 @@
    1.92      bool bResult;
    1.93      
    1.94      if (in_first) {
    1.95 -        bResult = ensure_keyserver();
    1.96 +        stringlist_t *conf_keys   = new_stringlist("keyserver");
    1.97 +        stringlist_t *conf_values = new_stringlist("hkp://keys.gnupg.net");
    1.98 +        stringlist_add(conf_keys, "cert-digest-algo");
    1.99 +        stringlist_add(conf_values, "SHA256");
   1.100 +
   1.101 +        bResult = ensure_config_values(conf_keys, conf_values);
   1.102          assert(bResult);
   1.103  
   1.104 +        free_stringlist(conf_keys);
   1.105 +        free_stringlist(conf_values);
   1.106 +
   1.107          gpgme = dlopen(LIBGPGME, RTLD_LAZY);
   1.108          if (gpgme == NULL) {
   1.109              status = PEP_INIT_CANNOT_LOAD_GPGME;