netpgp : send_keys without curl POST. Still need to fix import to avoid duplicates
authorEdouard Tisserant
Wed, 13 May 2015 00:08:20 +0200
changeset 2775c8838004648
parent 276 73d4c99cf054
child 278 4c1a784f523b
netpgp : send_keys without curl POST. Still need to fix import to avoid duplicates
src/pgp_netpgp.c
     1.1 --- a/src/pgp_netpgp.c	Tue May 12 17:06:44 2015 +0200
     1.2 +++ b/src/pgp_netpgp.c	Wed May 13 00:08:20 2015 +0200
     1.3 @@ -1029,7 +1029,6 @@
     1.4          goto free_request;
     1.5      }
     1.6  
     1.7 -    printf("request :\n %s\n\nanswer :\n%s\n", request, answer);
     1.8      result = pgp_import_keydata(session, 
     1.9                                  answer.memory, 
    1.10                                  answer.size);
    1.11 @@ -1046,8 +1045,56 @@
    1.12      return result;
    1.13  }
    1.14  
    1.15 -PEP_STATUS add_key_fpr_to_stringlist(stringlist_t **keylist, pgp_key_t *key)
    1.16 +typedef PEP_STATUS (*find_key_cb_t)(void*, pgp_key_t *);
    1.17 +
    1.18 +static PEP_STATUS find_keys_do(
    1.19 +        const char *pattern, find_key_cb_t cb, void* cb_arg)
    1.20  {
    1.21 +    uint8_t fpr[PGP_FINGERPRINT_SIZE];
    1.22 +    size_t length;
    1.23 +    pgp_key_t *key;
    1.24 +
    1.25 +    PEP_STATUS result;
    1.26 +
    1.27 +    // Try find a fingerprint in pattern
    1.28 +    if (str_to_fpr(pattern, fpr, &length)) {
    1.29 +
    1.30 +        // Only one fingerprint can match
    1.31 +        if ((key = (pgp_key_t *)pgp_getkeybyfpr(
    1.32 +                        netpgp.io,
    1.33 +                        (pgp_keyring_t *)netpgp.pubring, 
    1.34 +                        (const uint8_t *)fpr, length,
    1.35 +                        NULL)) == NULL) {
    1.36 +
    1.37 +            return PEP_KEY_NOT_FOUND;
    1.38 +        }
    1.39 +
    1.40 +        result = cb(cb_arg, key);
    1.41 +
    1.42 +    } else {
    1.43 +        // Search by name for pattern. Can match many.
    1.44 +        unsigned from = 0;
    1.45 +        result = PEP_KEY_NOT_FOUND;
    1.46 +        while((key = (pgp_key_t *)pgp_getnextkeybyname(
    1.47 +                        netpgp.io,
    1.48 +                        (pgp_keyring_t *)netpgp.pubring, 
    1.49 +			            (const char *)pattern,
    1.50 +                        &from)) != NULL) {
    1.51 +
    1.52 +            result = cb(cb_arg, key);
    1.53 +            if (result != PEP_STATUS_OK)
    1.54 +                break;
    1.55 +
    1.56 +            from++;
    1.57 +        }
    1.58 +    }
    1.59 +
    1.60 +    return result;
    1.61 +}
    1.62 +
    1.63 +static PEP_STATUS add_key_fpr_to_stringlist(void *arg, pgp_key_t *key)
    1.64 +{
    1.65 +    stringlist_t **keylist = arg;
    1.66      char *newfprstr = NULL;
    1.67  
    1.68      fpr_to_str(&newfprstr,
    1.69 @@ -1072,10 +1119,6 @@
    1.70      )
    1.71  {
    1.72      stringlist_t *_keylist, *_k;
    1.73 -    uint8_t fpr[PGP_FINGERPRINT_SIZE];
    1.74 -    size_t length;
    1.75 -    pgp_key_t *key;
    1.76 -    char *newfprstr = NULL;
    1.77  
    1.78      PEP_STATUS result;
    1.79  
    1.80 @@ -1098,39 +1141,7 @@
    1.81      }
    1.82      _k = _keylist;
    1.83  
    1.84 -    // Try find a fingerprint in pattern
    1.85 -    if (str_to_fpr(pattern, fpr, &length)) {
    1.86 -
    1.87 -        // Only one fingerprint can match
    1.88 -        if ((key = (pgp_key_t *)pgp_getkeybyfpr(
    1.89 -                        netpgp.io,
    1.90 -                        (pgp_keyring_t *)netpgp.pubring, 
    1.91 -                        (const uint8_t *)fpr, length,
    1.92 -                        NULL)) == NULL) {
    1.93 -
    1.94 -            result = PEP_KEY_NOT_FOUND;
    1.95 -            goto unlock_netpgp;
    1.96 -        }
    1.97 -
    1.98 -        result = add_key_fpr_to_stringlist(&_k, key);
    1.99 -
   1.100 -    } else {
   1.101 -        // Search by name for pattern. Can match many.
   1.102 -        unsigned from = 0;
   1.103 -        result = PEP_KEY_NOT_FOUND;
   1.104 -        while((key = (pgp_key_t *)pgp_getnextkeybyname(
   1.105 -                        netpgp.io,
   1.106 -                        (pgp_keyring_t *)netpgp.pubring, 
   1.107 -			            (const char *)pattern,
   1.108 -                        &from)) != NULL) {
   1.109 -
   1.110 -            result = add_key_fpr_to_stringlist(&_k, key);
   1.111 -            if (result != PEP_STATUS_OK)
   1.112 -                goto free_keylist;
   1.113 -
   1.114 -            from++;
   1.115 -        }
   1.116 -    }
   1.117 +    result = find_keys_do(pattern, &add_key_fpr_to_stringlist, &_k);
   1.118  
   1.119      if (result == PEP_STATUS_OK) {
   1.120          *keylist = _keylist;
   1.121 @@ -1147,15 +1158,47 @@
   1.122      return result;
   1.123  }
   1.124  
   1.125 +static PEP_STATUS send_key_cb(void *arg, pgp_key_t *key)
   1.126 +{
   1.127 +    char *newfprstr = NULL;
   1.128 +
   1.129 +    fpr_to_str(&newfprstr,
   1.130 +               key->sigfingerprint.fingerprint,
   1.131 +               key->sigfingerprint.length);
   1.132 +
   1.133 +    if (newfprstr == NULL) {
   1.134 +        return PEP_OUT_OF_MEMORY;
   1.135 +    } else { 
   1.136 +
   1.137 +        printf("would send:\n%s\n", newfprstr);
   1.138 +        pgp_print_keydata(netpgp.io, netpgp.pubring, key, "to send", &key->key.pubkey, 0);
   1.139 +        free(newfprstr);
   1.140 +    }
   1.141 +    return PEP_STATUS_OK;
   1.142 +}
   1.143 +
   1.144  PEP_STATUS pgp_send_key(PEP_SESSION session, const char *pattern)
   1.145  {
   1.146 +
   1.147 +    PEP_STATUS result;
   1.148 +
   1.149      assert(session);
   1.150      assert(pattern);
   1.151  
   1.152 -    /* TODO send key */
   1.153 +    if (!session || !pattern )
   1.154 +        return PEP_UNKNOWN_ERROR;
   1.155 +
   1.156 +    if(pthread_mutex_lock(&netpgp_mutex)){
   1.157 +        return PEP_UNKNOWN_ERROR;
   1.158 +    }
   1.159  
   1.160 -        return PEP_CANNOT_SEND_KEY;
   1.161 -        return PEP_STATUS_OK;
   1.162 +    result = find_keys_do(pattern, &send_key_cb, NULL);
   1.163 +
   1.164 +    result = PEP_CANNOT_SEND_KEY;
   1.165 +unlock_netpgp:
   1.166 +    pthread_mutex_unlock(&netpgp_mutex);
   1.167 +
   1.168 +    return result;
   1.169  }
   1.170  
   1.171