netpgp functionality, first try ENGINE-73
authorKrista Grothoff <krista@pep-project.org>
Mon, 15 Aug 2016 20:18:43 +0200
branchENGINE-73
changeset 1018e3c6cf28309c
parent 1017 555d74646dbf
child 1019 e38a6ec264bd
netpgp functionality, first try
src/pgp_netpgp.c
     1.1 --- a/src/pgp_netpgp.c	Mon Aug 15 18:06:31 2016 +0200
     1.2 +++ b/src/pgp_netpgp.c	Mon Aug 15 20:18:43 2016 +0200
     1.3 @@ -1645,6 +1645,67 @@
     1.4      return PEP_STATUS_OK;
     1.5  }
     1.6  
     1.7 +
     1.8 +// Presumption, if this contains an email address at all, is that the last
     1.9 +// '@' is the email address's at.
    1.10 +// 
    1.11 +// Our best guess is that this is structured as "REALNAME <blah@blah.blah>"
    1.12 +//
    1.13 +static void parse_netpgp_uid_str(char* src, char** name, char** email) {
    1.14 +    *name = NULL;
    1.15 +    *email = NULL;
    1.16 +        
    1.17 +    if (!src)
    1.18 +        return;
    1.19 + 
    1.20 +    size_t source_len = strlen(src);
    1.21 +    char* last_char = src + source_len;
    1.22 +    
    1.23 +    char* at = NULL;
    1.24 +
    1.25 +    char* begin = src;
    1.26 +    char* end = last_char; // one past the end;
    1.27 +    size_t copy_len = 0;
    1.28 +    
    1.29 +    // Primitive email extraction
    1.30 +    at = strrchr('@',src);
    1.31 +        
    1.32 +    if (at) {
    1.33 +        // Go back until we hit a space, a '<', or the start of the string
    1.34 +        for (begin = at; begin >= src && *begin != ' ' && *begin != '<'; begin--) {
    1.35 +            continue;
    1.36 +        }
    1.37 +        if (begin != at)
    1.38 +            begin++; // Ugly
    1.39 +        else {
    1.40 +            for (end = at; end <= last_char && *end != ' ' && *end != '>'; end++) {
    1.41 +                continue;
    1.42 +            }
    1.43 +            // Points one char past.
    1.44 +        }
    1.45 +        if (begin < at && end > at) {
    1.46 +            // yay, it's an email address!
    1.47 +            copy_len = end - begin;
    1.48 +            *email = (char*)malloc(sizeof(char) * (copy_len + 1));
    1.49 +            strncpy(email, begin, copy_len);
    1.50 +            *(email + copy_len) = '\0';
    1.51 +            end = (*begin == '<' ? begin - 1 : begin); // if this precedes src, it is checked below
    1.52 +            begin = src;
    1.53 +        }
    1.54 +        else {
    1.55 +            // bail
    1.56 +            begin = src;
    1.57 +            end = last_char;
    1.58 +        }
    1.59 +    }
    1.60 +    if (begin < end) {
    1.61 +        copy_len = end - begin;
    1.62 +        *name = (char*)malloc(sizeof(char) * (copy_len + 1));
    1.63 +        strncpy(name, begin, copy_len);
    1.64 +        *(name + copy_len) = '\0';
    1.65 +    }
    1.66 +}
    1.67 +
    1.68  PEP_STATUS list_keys(
    1.69          PEP_SESSION session, 
    1.70          identity_list_t** id_list)
    1.71 @@ -1666,53 +1727,32 @@
    1.72      if (keyring_end < 1)
    1.73          return result;
    1.74      
    1.75 +    identity_list _retval = new_identity_list(NULL);
    1.76 +    
    1.77      for (key = keyring->keys; n < keyring_end; ++n, ++key) {
    1.78          assert(key)
    1.79          if (!key)
    1.80              continue;
    1.81          char* primary_userid = (char*)pgp_key_get_primary_userid(key);
    1.82 +        // FIXME: For now, we just presume it's name + email address. Let's see what it really is.
    1.83 +        char* username = NULL;
    1.84 +        char* usermail = NULL;
    1.85 +        parse_netpgp_uid_str(primary_userid, &username, &usermail);
    1.86 +     
    1.87 +        char* id_fpr = NULL;
    1.88          
    1.89 -            
    1.90 +        fpr_to_str(&id_fpr, key->pubkeyfpr.fingerprint,
    1.91 +                   key->pubkeyfpr.length);
    1.92 +
    1.93 +        identity_list_add(_retval, new_identity(usermail, id_fpr, primary_user_id,
    1.94 +                                                username));
    1.95 +        free(username);
    1.96 +        free(usermail);
    1.97 +        free(id_fpr);
    1.98 +        status = PEP_STATUS_OK;
    1.99      }
   1.100 -        
   1.101 -    
   1.102 -    
   1.103 -    
   1.104 -    // Try find a fingerprint in pattern
   1.105 -    if (str_to_fpr(pattern, fpr, &length)) {
   1.106 -        unsigned from = 0;
   1.107 -
   1.108 -
   1.109 -        // Only one fingerprint can match
   1.110 -        if ((key = (pgp_key_t *)pgp_getkeybyfpr(
   1.111 -                        netpgp.io,
   1.112 -                        (pgp_keyring_t *)netpgp.pubring, 
   1.113 -                        (const uint8_t *)fpr, length,
   1.114 -                        &from,
   1.115 -                        NULL, 0, 0)) == NULL) {
   1.116 -
   1.117 -            return PEP_KEY_NOT_FOUND;
   1.118 -        }
   1.119 -
   1.120 -        result = cb(cb_arg, key);
   1.121 -
   1.122 -    } else {
   1.123 -        // Search by name for pattern. Can match many.
   1.124 -        unsigned from = 0;
   1.125 -        result = PEP_KEY_NOT_FOUND;
   1.126 -        while((key = (pgp_key_t *)pgp_getnextkeybyname(
   1.127 -                        netpgp.io,
   1.128 -                        (pgp_keyring_t *)netpgp.pubring, 
   1.129 -			            (const char *)pattern,
   1.130 -                        &from)) != NULL) {
   1.131 -
   1.132 -            result = cb(cb_arg, key);
   1.133 -            if (result != PEP_STATUS_OK)
   1.134 -                break;
   1.135 -
   1.136 -            from++;
   1.137 -        }
   1.138 -    }
   1.139 -
   1.140 +    *id_list = _retval;
   1.141      return result;
   1.142  }
   1.143 +
   1.144 +