Rocco Rutte
[apps/madmutt.git] / gnupgparse.c
1 /*
2  * Copyright notice from original mutt:
3  * Copyright (C) 1998-2000 Werner Koch <werner.koch@guug.de>
4  * Copyright (C) 1999-2000 Thomas Roessler <roessler@does-not-exist.org>
5  *
6  * This file is part of mutt-ng, see http://www.muttng.org/.
7  * It's licensed under the GNU General Public License,
8  * please see the file GPL in the top level source directory.
9  */
10
11 /*
12  * NOTE
13  * 
14  * This code used to be the parser for GnuPG's output.
15  * 
16  * Nowadays, we are using an external pubring lister with PGP which mimics 
17  * gpg's output format.
18  * 
19  */
20
21 #if HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <time.h>
34 #include <ctype.h>
35
36 #include "mutt.h"
37 #include "pgp.h"
38 #include "charset.h"
39
40 /* for hexval */
41 #include "mime.h"
42
43 #include "lib/mem.h"
44 #include "lib/str.h"
45
46 /****************
47  * Read the GNUPG keys.  For now we read the complete keyring by
48  * calling gnupg in a special mode.
49  *
50  * The output format of gpgm is colon delimited with these fields:
51  *   - record type ("pub","uid","sig","rev" etc.)
52  *   - trust info
53  *   - key length
54  *   - pubkey algo
55  *   - 16 hex digits with the long keyid.
56  *   - timestamp (1998-02-28)
57  *   - Local id
58  *   - ownertrust
59  *   - name
60  *   - signature class
61  */
62
63 /* decode the backslash-escaped user ids. */
64
65 static char *_chs = 0;
66
67 static void fix_uid (char *uid)
68 {
69   char *s, *d;
70   iconv_t cd;
71
72   for (s = d = uid; *s;) {
73     if (*s == '\\' && *(s + 1) == 'x' && isxdigit ((unsigned char) *(s + 2))
74         && isxdigit ((unsigned char) *(s + 3))) {
75       *d++ = hexval (*(s + 2)) << 4 | hexval (*(s + 3));
76       s += 4;
77     }
78     else
79       *d++ = *s++;
80   }
81   *d = '\0';
82
83   if (_chs && (cd = mutt_iconv_open (_chs, "utf-8", 0)) != (iconv_t) - 1) {
84     int n = s - uid + 1;        /* chars available in original buffer */
85     char *buf;
86     ICONV_CONST char *ib;
87     char *ob;
88     size_t ibl, obl;
89
90     buf = safe_malloc (n + 1);
91     ib = uid, ibl = d - uid + 1, ob = buf, obl = n;
92     iconv (cd, &ib, &ibl, &ob, &obl);
93     if (!ibl) {
94       if (ob - buf < n) {
95         memcpy (uid, buf, ob - buf);
96         uid[ob - buf] = '\0';
97       }
98       else if (ob - buf == n && (buf[n] = 0, safe_strlen (buf) < n))
99         memcpy (uid, buf, n);
100     }
101     FREE (&buf);
102     iconv_close (cd);
103   }
104 }
105
106 static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k)
107 {
108   pgp_uid_t *uid = NULL;
109   int field = 0, is_uid = 0;
110   char *pend, *p;
111   int trust = 0;
112   int flags = 0;
113
114   *is_subkey = 0;
115   if (!*buf)
116     return NULL;
117
118   dprint (2, (debugfile, "parse_pub_line: buf = `%s'\n", buf));
119
120   for (p = buf; p; p = pend) {
121     if ((pend = strchr (p, ':')))
122       *pend++ = 0;
123     field++;
124     if (field > 1 && !*p)
125       continue;
126
127     switch (field) {
128     case 1:                    /* record type */
129       {
130         dprint (2, (debugfile, "record type: %s\n", p));
131
132         if (!safe_strcmp (p, "pub"));
133         else if (!safe_strcmp (p, "sub"))
134           *is_subkey = 1;
135         else if (!safe_strcmp (p, "sec"));
136         else if (!safe_strcmp (p, "ssb"))
137           *is_subkey = 1;
138         else if (!safe_strcmp (p, "uid"))
139           is_uid = 1;
140         else
141           return NULL;
142
143         if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
144           k = safe_calloc (sizeof *k, 1);
145
146         break;
147       }
148     case 2:                    /* trust info */
149       {
150         dprint (2, (debugfile, "trust info: %s\n", p));
151
152         switch (*p) {           /* look only at the first letter */
153         case 'e':
154           flags |= KEYFLAG_EXPIRED;
155           break;
156         case 'r':
157           flags |= KEYFLAG_REVOKED;
158           break;
159         case 'd':
160           flags |= KEYFLAG_DISABLED;
161           break;
162         case 'n':
163           trust = 1;
164           break;
165         case 'm':
166           trust = 2;
167           break;
168         case 'f':
169           trust = 3;
170           break;
171         case 'u':
172           trust = 3;
173           break;
174         }
175
176         if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
177           k->flags |= flags;
178
179         break;
180       }
181     case 3:                    /* key length  */
182       {
183
184         dprint (2, (debugfile, "key len: %s\n", p));
185
186         if (!(*is_subkey && option (OPTPGPIGNORESUB)))
187           k->keylen = atoi (p); /* fixme: add validation checks */
188         break;
189       }
190     case 4:                    /* pubkey algo */
191       {
192
193         dprint (2, (debugfile, "pubkey algorithm: %s\n", p));
194
195         if (!(*is_subkey && option (OPTPGPIGNORESUB))) {
196           k->numalg = atoi (p);
197           k->algorithm = pgp_pkalgbytype (atoi (p));
198         }
199         break;
200       }
201     case 5:                    /* 16 hex digits with the long keyid. */
202       {
203         dprint (2, (debugfile, "key id: %s\n", p));
204
205         if (!(*is_subkey && option (OPTPGPIGNORESUB)))
206           str_replace (&k->keyid, p);
207         break;
208
209       }
210     case 6:                    /* timestamp (1998-02-28) */
211       {
212         char tstr[11];
213         struct tm time;
214
215         dprint (2, (debugfile, "time stamp: %s\n", p));
216
217         if (!p)
218           break;
219         time.tm_sec = 0;
220         time.tm_min = 0;
221         time.tm_hour = 12;
222         strncpy (tstr, p, 11);
223         tstr[4] = '\0';
224         time.tm_year = atoi (tstr) - 1900;
225         tstr[7] = '\0';
226         time.tm_mon = (atoi (tstr + 5)) - 1;
227         time.tm_mday = atoi (tstr + 8);
228         k->gen_time = mutt_mktime (&time, 0);
229         break;
230       }
231     case 7:                    /* valid for n days */
232       break;
233     case 8:                    /* Local id         */
234       break;
235     case 9:                    /* ownertrust       */
236       break;
237     case 10:                   /* name             */
238       {
239         if (!pend || !*p)
240           break;                /* empty field or no trailing colon */
241
242         /* ignore user IDs on subkeys */
243         if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
244           break;
245
246         dprint (2, (debugfile, "user ID: %s\n", p));
247
248         uid = safe_calloc (sizeof (pgp_uid_t), 1);
249         fix_uid (p);
250         uid->addr = safe_strdup (p);
251         uid->trust = trust;
252         uid->flags |= flags;
253         uid->parent = k;
254         uid->next = k->address;
255         k->address = uid;
256
257         if (strstr (p, "ENCR"))
258           k->flags |= KEYFLAG_PREFER_ENCRYPTION;
259         if (strstr (p, "SIGN"))
260           k->flags |= KEYFLAG_PREFER_SIGNING;
261
262         break;
263       }
264     case 11:                   /* signature class  */
265       break;
266     case 12:                   /* key capabilities */
267       dprint (2, (debugfile, "capabilities info: %s\n", p));
268
269       while (*p) {
270         switch (*p++) {
271         case 'D':
272           flags |= KEYFLAG_DISABLED;
273           break;
274
275         case 'e':
276           flags |= KEYFLAG_CANENCRYPT;
277           break;
278
279         case 's':
280           flags |= KEYFLAG_CANSIGN;
281           break;
282         }
283       }
284
285       if (!is_uid && (!*is_subkey || !option (OPTPGPIGNORESUB)
286                       || !((flags & KEYFLAG_DISABLED)
287                            || (flags & KEYFLAG_REVOKED)
288                            || (flags & KEYFLAG_EXPIRED))))
289         k->flags |= flags;
290
291       break;
292
293     default:
294       break;
295     }
296   }
297   return k;
298 }
299
300 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, LIST * hints)
301 {
302   FILE *fp;
303   pid_t thepid;
304   char buf[LONG_STRING];
305   pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
306   int is_sub;
307   int devnull;
308
309   if ((devnull = open ("/dev/null", O_RDWR)) == -1)
310     return NULL;
311
312   str_replace (&_chs, Charset);
313
314   thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
315                                  keyring, hints);
316   if (thepid == -1) {
317     close (devnull);
318     return NULL;
319   }
320
321   kend = &db;
322   k = NULL;
323   while (fgets (buf, sizeof (buf) - 1, fp)) {
324     if (!(kk = parse_pub_line (buf, &is_sub, k)))
325       continue;
326
327     /* Only append kk to the list if it's new. */
328     if (kk != k) {
329       if (k)
330         kend = &k->next;
331       *kend = k = kk;
332
333       if (is_sub) {
334         pgp_uid_t **l;
335
336         k->flags |= KEYFLAG_SUBKEY;
337         k->parent = mainkey;
338         for (l = &k->address; *l; l = &(*l)->next);
339         *l = pgp_copy_uids (mainkey->address, k);
340       }
341       else
342         mainkey = k;
343     }
344   }
345
346   if (ferror (fp))
347     mutt_perror ("fgets");
348
349   fclose (fp);
350   mutt_wait_filter (thepid);
351
352   close (devnull);
353
354   return db;
355 }