ENGINE-557: initial commit. Test now works, but I need to be sure the right things are going into the DB. sync
authorKrista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 15 May 2019 12:53:34 +0200
branchsync
changeset 3681120b329e558a
parent 3676 6902ea2166cf
child 3682 df9823cb0d96
ENGINE-557: initial commit. Test now works, but I need to be sure the right things are going into the DB.
src/pgp_sequoia.c
     1.1 --- a/src/pgp_sequoia.c	Tue May 14 18:11:18 2019 +0200
     1.2 +++ b/src/pgp_sequoia.c	Wed May 15 12:53:34 2019 +0200
     1.3 @@ -9,6 +9,7 @@
     1.4  #include "pEp_internal.h"
     1.5  #include "pgp_gpg.h"
     1.6  
     1.7 +#include <ctype.h>
     1.8  #include <limits.h>
     1.9  #include <sys/stat.h>
    1.10  #include <sys/types.h>
    1.11 @@ -739,6 +740,48 @@
    1.12          pgp_packet_t userid = pgp_user_id_new (user_id_value);
    1.13          pgp_user_id_name(NULL, userid, &name);
    1.14          pgp_user_id_address(NULL, userid, &email);
    1.15 +        
    1.16 +        if (!email || email[0] == '\0') {
    1.17 +            size_t uid_value_len;
    1.18 +            const char* uid_value = (const char*)pgp_user_id_value(userid, &uid_value_len);
    1.19 +            if (!uid_value) {
    1.20 +                // We need some kind of an error here, maybe?
    1.21 +                
    1.22 +            }
    1.23 +            else {
    1.24 +                const char* split = strstr(uid_value, "<");
    1.25 +                if (split != uid_value) {                    
    1.26 +                    while (split) {
    1.27 +                        if (isspace(*(split - 1)))
    1.28 +                            break;
    1.29 +                        split = strstr(split + 1, "<");
    1.30 +                    }
    1.31 +                }
    1.32 +                if (split) {
    1.33 +                    char* stopchr = strrchr(split, '>');
    1.34 +                    if (stopchr) {
    1.35 +                        int email_len = stopchr - split - 1;
    1.36 +                        email = calloc(email_len + 1, 1);                                
    1.37 +                        strlcpy(email, split + 1, email_len + 1);
    1.38 +                        const char* last = NULL;
    1.39 +                        if (split != uid_value) {
    1.40 +                            for (last = split - 1; last > uid_value; last--) {
    1.41 +                                if (!isspace(*last))
    1.42 +                                    break;
    1.43 +                            }
    1.44 +                            int name_len = (last - uid_value) + 1;
    1.45 +                            name = calloc(name_len + 1, 1);
    1.46 +                            strlcpy(name, uid_value, name_len + 1);
    1.47 +                        }
    1.48 +                    }
    1.49 +                    else 
    1.50 +                        split = NULL;
    1.51 +                }
    1.52 +                if (split == NULL) {
    1.53 +                    email = strdup(uid_value);
    1.54 +                }
    1.55 +            }
    1.56 +        }
    1.57          pgp_packet_free(userid);
    1.58          free(user_id_value);
    1.59  
    1.60 @@ -2004,36 +2047,49 @@
    1.61                      PEP_STATUS status = list_keys(session, undotted, private_only,
    1.62                                                    keyinfo_list, keylist);
    1.63                      free(undotted);
    1.64 -                    return status;
    1.65 +                    if (*keylist)
    1.66 +                        return status;
    1.67                  }
    1.68              }
    1.69          }
    1.70 -    } else if (// Only hex characters and spaces
    1.71 -               pattern[strspn(pattern, "0123456789aAbBcCdDeEfF ")] == 0
    1.72 -               // And a fair amount of them.
    1.73 -               && strlen(pattern) >= 16) {
    1.74 -        // Fingerprint.  Note: the pep engine never looks keys up by
    1.75 -        // keyid, so we don't handle them.
    1.76 -        fpr = pgp_fingerprint_from_hex(pattern);
    1.77 -        status = tpk_find_by_fpr(session, fpr, false, &tpk, NULL);
    1.78 -        ERROR_OUT(NULL, status, "Looking up key");
    1.79 -        add_key(session, _keyinfo_list, _keylist, tpk, fpr);
    1.80 -    } else if (pattern[0] == 0) {
    1.81 -        // Empty string.
    1.82 +    } 
    1.83 +    if (!_keylist && !_keyinfo_list) {
    1.84 +        if (// Only hex characters and spaces
    1.85 +                   pattern[strspn(pattern, "0123456789aAbBcCdDeEfF ")] == 0
    1.86 +                   // And a fair amount of them.
    1.87 +                   && strlen(pattern) >= 16) {
    1.88 +            // Fingerprint.  Note: the pep engine never looks keys up by
    1.89 +            // keyid, so we don't handle them.
    1.90 +            fpr = pgp_fingerprint_from_hex(pattern);
    1.91 +            status = tpk_find_by_fpr(session, fpr, false, &tpk, NULL);
    1.92 +            ERROR_OUT(NULL, status, "Looking up key");
    1.93 +            add_key(session, _keyinfo_list, _keylist, tpk, fpr);
    1.94 +        } else if (pattern[0] == 0) {
    1.95 +            // Empty string.
    1.96  
    1.97 -        pgp_tpk_t *tpks = NULL;
    1.98 -        int count = 0;
    1.99 -        status = tpk_all(session, private_only, &tpks, &count);
   1.100 -        ERROR_OUT(NULL, status, "Looking up '%s'", pattern);
   1.101 -        for (int i = 0; i < count; i ++) {
   1.102 -            add_key(session, _keyinfo_list, _keylist, tpks[i], NULL);
   1.103 -            pgp_tpk_free(tpks[i]);
   1.104 +            pgp_tpk_t *tpks = NULL;
   1.105 +            int count = 0;
   1.106 +            status = tpk_all(session, private_only, &tpks, &count);
   1.107 +            ERROR_OUT(NULL, status, "Looking up '%s'", pattern);
   1.108 +            for (int i = 0; i < count; i ++) {
   1.109 +                add_key(session, _keyinfo_list, _keylist, tpks[i], NULL);
   1.110 +                pgp_tpk_free(tpks[i]);
   1.111 +            }
   1.112 +            free(tpks);
   1.113 +        } else {
   1.114 +    //        T("unsupported pattern '%s'", pattern);
   1.115 +            // We look up whatever arbitrary string we have
   1.116 +            pgp_tpk_t *tpks = NULL;
   1.117 +            int count = 0;
   1.118 +            status = tpk_find_by_email(session, pattern, private_only, &tpks, &count);
   1.119 +            ERROR_OUT(NULL, status, "Looking up '%s'", pattern);
   1.120 +            for (int i = 0; i < count; i ++) {
   1.121 +                add_key(session, _keyinfo_list, _keylist, tpks[i], NULL);
   1.122 +                pgp_tpk_free(tpks[i]);
   1.123 +            }
   1.124 +            free(tpks);
   1.125          }
   1.126 -        free(tpks);
   1.127 -    } else {
   1.128 -        T("unsupported pattern '%s'", pattern);
   1.129      }
   1.130 -
   1.131   out:
   1.132      if (tpk)
   1.133          pgp_tpk_free(tpk);