NeoMutt  2022-04-29-215-gc12b98
Teaching an old dog new tricks
DOXYGEN
gnupgparse.c File Reference

Parse the output of CLI PGP program. More...

#include "config.h"
#include <ctype.h>
#include <fcntl.h>
#include <iconv.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gnupgparse.h"
#include "lib.h"
#include "pgpinvoke.h"
#include "pgpkey.h"
#include "pgplib.h"
+ Include dependency graph for gnupgparse.c:

Go to the source code of this file.

Functions

static void fix_uid (char *uid)
 Decode backslash-escaped user ids (in place) More...
 
static struct PgpKeyInfoparse_pub_line (char *buf, bool *is_subkey, struct PgpKeyInfo *k)
 Parse the 'pub' line from the pgp output. More...
 
struct PgpKeyInfopgp_get_candidates (enum PgpRing keyring, struct ListHead *hints)
 Find PGP keys matching a list of hints. More...
 

Variables

static char * chs = NULL
 

Detailed Description

Parse the output of CLI PGP program.

Authors
  • Werner Koch
  • Thomas Roessler

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file gnupgparse.c.

Function Documentation

◆ fix_uid()

static void fix_uid ( char *  uid)
static

Decode backslash-escaped user ids (in place)

Parameters
uidString to decode

Definition at line 79 of file gnupgparse.c.

80{
81 char *s = NULL, *d = NULL;
82 iconv_t cd;
83
84 for (s = uid, d = uid; *s;)
85 {
86 if ((s[0] == '\\') && (s[1] == 'x') && isxdigit((unsigned char) s[2]) &&
87 isxdigit((unsigned char) s[3]))
88 {
89 *d++ = (hexval(s[2]) << 4) | hexval(s[3]);
90 s += 4;
91 }
92 else
93 *d++ = *s++;
94 }
95 *d = '\0';
96
97 if (chs && ((cd = mutt_ch_iconv_open(chs, "utf-8", MUTT_ICONV_NO_FLAGS)) != (iconv_t) -1))
98 {
99 int n = s - uid + 1; /* chars available in original buffer */
100
101 char *buf = mutt_mem_malloc(n + 1);
102 const char *ib = uid;
103 size_t ibl = d - uid + 1;
104 char *ob = buf;
105 size_t obl = n;
106 iconv(cd, (ICONV_CONST char **) &ib, &ibl, &ob, &obl);
107 if (ibl == 0)
108 {
109 if (ob - buf < n)
110 {
111 memcpy(uid, buf, ob - buf);
112 uid[ob - buf] = '\0';
113 }
114 else if ((n >= 0) && ((ob - buf) == n) && (buf[n] = 0, (strlen(buf) < (size_t) n)))
115 memcpy(uid, buf, n);
116 }
117 FREE(&buf);
118 iconv_close(cd);
119 }
120}
static char * chs
Definition: gnupgparse.c:73
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
#define FREE(x)
Definition: memory.h:43
#define hexval(ch)
Definition: mime.h:80
iconv_t mutt_ch_iconv_open(const char *tocode, const char *fromcode, uint8_t flags)
Set up iconv for conversions.
Definition: charset.c:564
#define MUTT_ICONV_NO_FLAGS
No flags are set.
Definition: charset.h:71
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_pub_line()

static struct PgpKeyInfo * parse_pub_line ( char *  buf,
bool *  is_subkey,
struct PgpKeyInfo k 
)
static

Parse the 'pub' line from the pgp output.

Parameters
[in]bufBuffer containing string to parse
[out]is_subkeyIs this a subkey of another key?
[in]kKey to from which to merge info (optional)
Return values
ptrPgpKeyInfo containing the (merged) results
NULLError

Definition at line 130 of file gnupgparse.c.

131{
132 struct PgpUid *uid = NULL;
133 int field = 0;
134 bool is_uid = false;
135 bool is_pub = false;
136 bool is_fpr = false;
137 char *pend = NULL, *p = NULL;
138 int trust = 0;
140 struct PgpKeyInfo tmp;
141 char tstr[11] = { 0 };
142
143 *is_subkey = false;
144 if (*buf == '\0')
145 return NULL;
146
147 /* if we're given a key, merge our parsing results, else
148 * start with a fresh one to work with so that we don't
149 * mess up the real key in case we find parsing errors. */
150 if (k)
151 memcpy(&tmp, k, sizeof(tmp));
152 else
153 memset(&tmp, 0, sizeof(tmp));
154
155 mutt_debug(LL_DEBUG2, "buf = '%s'\n", buf);
156
157 const bool c_pgp_ignore_subkeys = cs_subset_bool(NeoMutt->sub, "pgp_ignore_subkeys");
158 for (p = buf; p; p = pend)
159 {
160 pend = strchr(p, ':');
161 if (pend)
162 *pend++ = 0;
163 field++;
164 if ((*p == '\0') && (field != 1) && (field != 10))
165 continue;
166
167 if (is_fpr && (field != 10))
168 continue;
169
170 switch (field)
171 {
172 case 1: /* record type */
173 {
174 mutt_debug(LL_DEBUG2, "record type: %s\n", p);
175
176 if (mutt_str_equal(p, "pub"))
177 is_pub = true;
178 else if (mutt_str_equal(p, "sub"))
179 *is_subkey = true;
180 else if (mutt_str_equal(p, "sec"))
181 ; // do nothing
182 else if (mutt_str_equal(p, "ssb"))
183 *is_subkey = true;
184 else if (mutt_str_equal(p, "uid"))
185 is_uid = true;
186 else if (mutt_str_equal(p, "fpr"))
187 is_fpr = true;
188 else
189 return NULL;
190
191 if (!(is_uid || is_fpr || (*is_subkey && c_pgp_ignore_subkeys)))
192 memset(&tmp, 0, sizeof(tmp));
193
194 break;
195 }
196 case 2: /* trust info */
197 {
198 mutt_debug(LL_DEBUG2, "trust info: %s\n", p);
199
200 switch (*p)
201 { /* look only at the first letter */
202 case 'd':
204 break;
205 case 'e':
207 break;
208 case 'f':
209 trust = 3;
210 break;
211 case 'm':
212 trust = 2;
213 break;
214 case 'n':
215 trust = 1;
216 break;
217 case 'r':
219 break;
220 case 'u':
221 trust = 3;
222 break;
223 }
224
225 if (!is_uid && !(*is_subkey && c_pgp_ignore_subkeys))
226 tmp.flags |= flags;
227
228 break;
229 }
230 case 3: /* key length */
231 {
232 mutt_debug(LL_DEBUG2, "key len: %s\n", p);
233
234 if (!(*is_subkey && c_pgp_ignore_subkeys) && !mutt_str_atos_full(p, &tmp.keylen))
235 {
236 goto bail;
237 }
238 break;
239 }
240 case 4: /* pubkey algo */
241 {
242 mutt_debug(LL_DEBUG2, "pubkey algorithm: %s\n", p);
243
244 if (!(*is_subkey && c_pgp_ignore_subkeys))
245 {
246 int x = 0;
247 if (!mutt_str_atoi_full(p, &x))
248 goto bail;
249 tmp.numalg = x;
250 tmp.algorithm = pgp_pkalgbytype(x);
251 }
252 break;
253 }
254 case 5: /* 16 hex digits with the long keyid. */
255 {
256 mutt_debug(LL_DEBUG2, "key id: %s\n", p);
257
258 if (!(*is_subkey && c_pgp_ignore_subkeys))
259 mutt_str_replace(&tmp.keyid, p);
260 break;
261 }
262 case 6: /* timestamp (1998-02-28) */
263 {
264 mutt_debug(LL_DEBUG2, "time stamp: %s\n", p);
265
266 if (strchr(p, '-')) /* gpg pre-2.0.10 used format (yyyy-mm-dd) */
267 {
268 struct tm time;
269
270 time.tm_sec = 0;
271 time.tm_min = 0;
272 time.tm_hour = 12;
273 strncpy(tstr, p, 11);
274 tstr[4] = '\0';
275 tstr[7] = '\0';
276 if (!mutt_str_atoi_full(tstr, &time.tm_year))
277 {
278 p = tstr;
279 goto bail;
280 }
281 time.tm_year -= 1900;
282 if (!mutt_str_atoi_full(tstr + 5, &time.tm_mon))
283 {
284 p = tstr + 5;
285 goto bail;
286 }
287 time.tm_mon -= 1;
288 if (!mutt_str_atoi_full(tstr + 8, &time.tm_mday))
289 {
290 p = tstr + 8;
291 goto bail;
292 }
293 tmp.gen_time = mutt_date_make_time(&time, false);
294 }
295 else /* gpg 2.0.10+ uses seconds since 1970-01-01 */
296 {
297 unsigned long long secs;
298
299 if (!mutt_str_atoull(p, &secs))
300 goto bail;
301 tmp.gen_time = (time_t) secs;
302 }
303 break;
304 }
305 case 7: /* valid for n days */
306 break;
307 case 8: /* Local id */
308 break;
309 case 9: /* ownertrust */
310 break;
311 case 10: /* name */
312 {
313 /* Empty field or no trailing colon.
314 * We allow an empty field for a pub record type because it is
315 * possible for a primary uid record to have an empty User-ID
316 * field. Without any address records, it is not possible to
317 * use the key in neomutt. */
318 if (!(pend && (*p || is_pub)))
319 break;
320
321 if (is_fpr)
322 {
323 /* don't let a subkey fpr overwrite an existing primary key fpr */
324 if (!tmp.fingerprint)
325 tmp.fingerprint = mutt_str_dup(p);
326 break;
327 }
328
329 /* ignore user IDs on subkeys */
330 if (!is_uid && (*is_subkey && c_pgp_ignore_subkeys))
331 break;
332
333 mutt_debug(LL_DEBUG2, "user ID: %s\n", NONULL(p));
334
335 uid = mutt_mem_calloc(1, sizeof(struct PgpUid));
336 fix_uid(p);
337 uid->addr = mutt_str_dup(p);
338 uid->trust = trust;
339 uid->flags |= flags;
340 uid->next = tmp.address;
341 tmp.address = uid;
342
343 if (strstr(p, "ENCR"))
345 if (strstr(p, "SIGN"))
346 tmp.flags |= KEYFLAG_PREFER_SIGNING;
347
348 break;
349 }
350 case 11: /* signature class */
351 break;
352 case 12: /* key capabilities */
353 mutt_debug(LL_DEBUG2, "capabilities info: %s\n", p);
354
355 while (*p)
356 {
357 switch (*p++)
358 {
359 case 'D':
360 flags |= KEYFLAG_DISABLED;
361 break;
362
363 case 'e':
364 flags |= KEYFLAG_CANENCRYPT;
365 break;
366
367 case 's':
368 flags |= KEYFLAG_CANSIGN;
369 break;
370 }
371 }
372
373 if (!is_uid && (!*is_subkey || !c_pgp_ignore_subkeys ||
374 !((flags & KEYFLAG_DISABLED) || (flags & KEYFLAG_REVOKED) ||
375 (flags & KEYFLAG_EXPIRED))))
376 {
377 tmp.flags |= flags;
378 }
379
380 break;
381
382 default:
383 break;
384 }
385 }
386
387 /* merge temp key back into real key */
388 if (!(is_uid || is_fpr || (*is_subkey && c_pgp_ignore_subkeys)))
389 k = mutt_mem_malloc(sizeof(*k));
390 if (!k)
391 return NULL;
392 memcpy(k, &tmp, sizeof(*k));
393 /* fixup parentship of uids after merging the temp key into
394 * the real key */
395 if (tmp.address)
396 {
397 for (uid = k->address; uid; uid = uid->next)
398 uid->parent = k;
399 }
400
401 return k;
402
403bail:
404 mutt_debug(LL_DEBUG1, "invalid number: '%s'\n", p);
405 return NULL;
406}
const char * mutt_str_atoull(const char *str, unsigned long long *dst)
Convert ASCII string to an unsigned long long.
Definition: atoi.c:274
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
time_t mutt_date_make_time(struct tm *t, bool local)
Convert struct tm to time_t
Definition: date.c:229
static void fix_uid(char *uid)
Decode backslash-escaped user ids (in place)
Definition: gnupgparse.c:79
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:807
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:326
#define KEYFLAG_EXPIRED
Key is expired.
Definition: lib.h:131
uint16_t KeyFlags
Flags describing PGP/SMIME keys, e.g. KEYFLAG_CANSIGN.
Definition: lib.h:125
#define KEYFLAG_CANENCRYPT
Key is suitable for encryption.
Definition: lib.h:128
#define KEYFLAG_NO_FLAGS
No flags are set.
Definition: lib.h:126
#define KEYFLAG_PREFER_SIGNING
Key's owner prefers signing.
Definition: lib.h:137
#define KEYFLAG_DISABLED
Key is marked disabled.
Definition: lib.h:133
#define KEYFLAG_REVOKED
Key is revoked.
Definition: lib.h:132
#define KEYFLAG_PREFER_ENCRYPTION
Key's owner prefers encryption.
Definition: lib.h:136
#define KEYFLAG_CANSIGN
Key is suitable for signing.
Definition: lib.h:127
const char * pgp_pkalgbytype(unsigned char type)
Get the name of the algorithm from its ID.
Definition: pgplib.c:41
#define NONULL(x)
Definition: string2.h:37
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
Information about a PGP key.
Definition: pgplib.h:47
KeyFlags flags
Definition: pgplib.h:51
struct PgpUid * address
Definition: pgplib.h:50
PGP User ID.
Definition: pgplib.h:35
short trust
Definition: pgplib.h:37
struct PgpKeyInfo * parent
Parent key.
Definition: pgplib.h:39
int flags
Definition: pgplib.h:38
char * addr
Definition: pgplib.h:36
struct PgpUid * next
Linked list.
Definition: pgplib.h:40
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ pgp_get_candidates()

struct PgpKeyInfo * pgp_get_candidates ( enum PgpRing  keyring,
struct ListHead *  hints 
)

Find PGP keys matching a list of hints.

Parameters
keyringPGP Keyring
hintsList of strings to match
Return values
ptrKey list
NULLError

Definition at line 415 of file gnupgparse.c.

416{
417 FILE *fp = NULL;
418 pid_t pid;
419 char buf[1024] = { 0 };
420 struct PgpKeyInfo *db = NULL, **kend = NULL, *k = NULL, *kk = NULL, *mainkey = NULL;
421 bool is_sub = false;
422
423 int fd_null = open("/dev/null", O_RDWR);
424 if (fd_null == -1)
425 return NULL;
426
427 const char *const c_charset = cs_subset_string(NeoMutt->sub, "charset");
428 mutt_str_replace(&chs, c_charset);
429
430 pid = pgp_invoke_list_keys(NULL, &fp, NULL, -1, -1, fd_null, keyring, hints);
431 if (pid == -1)
432 {
433 close(fd_null);
434 return NULL;
435 }
436
437 kend = &db;
438 k = NULL;
439 while (fgets(buf, sizeof(buf) - 1, fp))
440 {
441 kk = parse_pub_line(buf, &is_sub, k);
442 if (!kk)
443 continue;
444
445 /* Only append kk to the list if it's new. */
446 if (kk != k)
447 {
448 if (k)
449 kend = &k->next;
450 *kend = kk;
451 k = kk;
452
453 if (is_sub)
454 {
455 struct PgpUid **l = NULL;
456
457 k->flags |= KEYFLAG_SUBKEY;
458 k->parent = mainkey;
459 for (l = &k->address; *l; l = &(*l)->next)
460 ; // do nothing
461
462 *l = pgp_copy_uids(mainkey->address, k);
463 }
464 else
465 mainkey = k;
466 }
467 }
468
469 if (ferror(fp))
470 mutt_perror("fgets");
471
472 mutt_file_fclose(&fp);
473 filter_wait(pid);
474
475 close(fd_null);
476
477 return db;
478}
const char * cs_subset_string(const struct ConfigSubset *sub, const char *name)
Get a string config item by name.
Definition: helpers.c:317
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:217
static struct PgpKeyInfo * parse_pub_line(char *buf, bool *is_subkey, struct PgpKeyInfo *k)
Parse the 'pub' line from the pgp output.
Definition: gnupgparse.c:130
#define mutt_perror(...)
Definition: logging.h:88
#define KEYFLAG_SUBKEY
Key is a subkey.
Definition: lib.h:134
pid_t pgp_invoke_list_keys(FILE **fp_pgp_in, FILE **fp_pgp_out, FILE **fp_pgp_err, int fd_pgp_in, int fd_pgp_out, int fd_pgp_err, enum PgpRing keyring, struct ListHead *hints)
Find matching PGP Keys.
Definition: pgpinvoke.c:542
struct PgpUid * pgp_copy_uids(struct PgpUid *up, struct PgpKeyInfo *parent)
Copy a list of PGP UIDs.
Definition: pgplib.c:126
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ chs

char* chs = NULL
static

Definition at line 73 of file gnupgparse.c.