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

413 {
414  FILE *fp = NULL;
415  pid_t pid;
416  char buf[1024];
417  struct PgpKeyInfo *db = NULL, **kend = NULL, *k = NULL, *kk = NULL, *mainkey = NULL;
418  bool is_sub = false;
419 
420  int fd_null = open("/dev/null", O_RDWR);
421  if (fd_null == -1)
422  return NULL;
423 
425 
426  pid = pgp_invoke_list_keys(NULL, &fp, NULL, -1, -1, fd_null, keyring, hints);
427  if (pid == -1)
428  {
429  close(fd_null);
430  return NULL;
431  }
432 
433  kend = &db;
434  k = NULL;
435  while (fgets(buf, sizeof(buf) - 1, fp))
436  {
437  kk = parse_pub_line(buf, &is_sub, k);
438  if (!kk)
439  continue;
440 
441  /* Only append kk to the list if it's new. */
442  if (kk != k)
443  {
444  if (k)
445  kend = &k->next;
446  *kend = kk;
447  k = kk;
448 
449  if (is_sub)
450  {
451  struct PgpUid **l = NULL;
452 
453  k->flags |= KEYFLAG_SUBKEY;
454  k->parent = mainkey;
455  for (l = &k->address; *l; l = &(*l)->next)
456  ; // do nothing
457 
458  *l = pgp_copy_uids(mainkey->address, k);
459  }
460  else
461  mainkey = k;
462  }
463  }
464 
465  if (ferror(fp))
466  mutt_perror("fgets");
467 
468  mutt_file_fclose(&fp);
469  filter_wait(pid);
470 
471  close(fd_null);
472 
473  return db;
474 }
#define mutt_perror(...)
Definition: logging.h:85
struct PgpUid * next
Definition: pgplib.h:40
#define KEYFLAG_SUBKEY
Key is a subkey.
Definition: lib.h:141
PGP User ID.
Definition: pgplib.h:34
Information about a PGP key.
Definition: pgplib.h:46
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:153
static char * chs
Definition: gnupgparse.c:73
struct PgpUid * pgp_copy_uids(struct PgpUid *up, struct PgpKeyInfo *parent)
Copy a list of PGP UIDs.
Definition: pgplib.c:126
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:520
static struct PgpKeyInfo * parse_pub_line(char *buf, bool *is_subkey, struct PgpKeyInfo *k)
Parse the &#39;pub&#39; line from the pgp output.
Definition: gnupgparse.c:130
char * mutt_str_replace(char **p, const char *s)
Replace one string with another.
Definition: string.c:451
char * C_Charset
Config: Default character set for displaying text on screen.
Definition: charset.c:53
int filter_wait(pid_t pid)
Wait for the exit of a process and return its status.
Definition: filter.c:217
int flags
Definition: pgplib.h:38
+ 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.