finish the "read" of charset.c
[apps/madmutt.git] / lib-crypt / 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 <lib-lib/mem.h>
37 #include <lib-lib/str.h>
38 #include <lib-lib/date.h>
39
40 #include <lib-mime/mime.h>
41
42 #include "mutt.h"
43 #include "pgp.h"
44 #include "charset.h"
45
46
47 /****************
48  * Read the GNUPG keys.  For now we read the complete keyring by
49  * calling gnupg in a special mode.
50  *
51  * The output format of gpgm is colon delimited with these fields:
52  *   - record type ("pub","uid","sig","rev" etc.)
53  *   - trust info
54  *   - key length
55  *   - pubkey algo
56  *   - 16 hex digits with the long keyid.
57  *   - timestamp (1998-02-28)
58  *   - Local id
59  *   - ownertrust
60  *   - name
61  *   - signature class
62  */
63
64 /* decode the backslash-escaped user ids. */
65
66 static char *_chs = 0;
67
68 static void fix_uid (char *uid)
69 {
70   char *s, *d;
71   iconv_t cd;
72
73   for (s = d = uid; *s;) {
74     if (*s == '\\' && *(s + 1) == 'x' && isxdigit ((unsigned char) *(s + 2))
75         && isxdigit ((unsigned char) *(s + 3))) {
76       *d++ = hexval (*(s + 2)) << 4 | hexval (*(s + 3));
77       s += 4;
78     }
79     else
80       *d++ = *s++;
81   }
82   *d = '\0';
83
84   if (_chs && (cd = mutt_iconv_open (_chs, "utf-8", 0)) != MUTT_ICONV_ERROR) {
85     int n = s - uid + 1;        /* chars available in original buffer */
86     char *buf;
87     const char *ib;
88     char *ob;
89     ssize_t ibl, obl;
90
91     buf = p_new(char, n + 1);
92     ib = uid, ibl = d - uid + 1, ob = buf, obl = n;
93     my_iconv(cd, &ib, &ibl, &ob, &obl);
94     if (!ibl) {
95       if (ob - buf < n) {
96         memcpy (uid, buf, ob - buf);
97         uid[ob - buf] = '\0';
98       }
99       else if (ob - buf == n && (buf[n] = 0, m_strlen(buf) < n))
100         memcpy (uid, buf, n);
101     }
102     p_delete(&buf);
103     iconv_close (cd);
104   }
105 }
106
107 static pgp_key_t parse_pub_line (char *buf, int *is_subkey, pgp_key_t k)
108 {
109   pgp_uid_t *uid = NULL;
110   int field = 0, is_uid = 0;
111   char *pend, *p;
112   int trust = 0;
113   int flags = 0;
114
115   *is_subkey = 0;
116   if (!*buf)
117     return NULL;
118
119   for (p = buf; p; p = pend) {
120     if ((pend = strchr (p, ':')))
121       *pend++ = 0;
122     field++;
123     if (field > 1 && !*p)
124       continue;
125
126     switch (field) {
127     case 1:                    /* record type */
128       {
129         if (!m_strcmp(p, "pub"));
130         else if (!m_strcmp(p, "sub"))
131           *is_subkey = 1;
132         else if (!m_strcmp(p, "sec"));
133         else if (!m_strcmp(p, "ssb"))
134           *is_subkey = 1;
135         else if (!m_strcmp(p, "uid"))
136           is_uid = 1;
137         else
138           return NULL;
139
140         if (!(is_uid || (*is_subkey && option (OPTPGPIGNORESUB))))
141           k = pgp_new_keyinfo();
142
143         break;
144       }
145     case 2:                    /* trust info */
146       {
147         switch (*p) {           /* look only at the first letter */
148         case 'e':
149           flags |= KEYFLAG_EXPIRED;
150           break;
151         case 'r':
152           flags |= KEYFLAG_REVOKED;
153           break;
154         case 'd':
155           flags |= KEYFLAG_DISABLED;
156           break;
157         case 'n':
158           trust = 1;
159           break;
160         case 'm':
161           trust = 2;
162           break;
163         case 'f':
164           trust = 3;
165           break;
166         case 'u':
167           trust = 3;
168           break;
169         }
170
171         if (!is_uid && !(*is_subkey && option (OPTPGPIGNORESUB)))
172           k->flags |= flags;
173
174         break;
175       }
176     case 3:                    /* key length  */
177       {
178         if (!(*is_subkey && option (OPTPGPIGNORESUB)))
179           k->keylen = atoi (p); /* fixme: add validation checks */
180         break;
181       }
182     case 4:                    /* pubkey algo */
183       {
184         if (!(*is_subkey && option (OPTPGPIGNORESUB))) {
185           k->numalg = atoi (p);
186           k->algorithm = pgp_pkalgbytype (atoi (p));
187         }
188         break;
189       }
190     case 5:                    /* 16 hex digits with the long keyid. */
191       {
192         if (!(*is_subkey && option (OPTPGPIGNORESUB)))
193           m_strreplace(&k->keyid, p);
194         break;
195
196       }
197     case 6:                    /* timestamp (1998-02-28) */
198       {
199         char tstr[11];
200         struct tm st_time;
201
202         if (!p)
203           break;
204         st_time.tm_sec = 0;
205         st_time.tm_min = 0;
206         st_time.tm_hour = 12;
207         m_strcpy(tstr, sizeof(tstr), p);
208         tstr[4] = '\0';
209         st_time.tm_year = atoi (tstr) - 1900;
210         tstr[7] = '\0';
211         st_time.tm_mon = (atoi (tstr + 5)) - 1;
212         st_time.tm_mday = atoi (tstr + 8);
213         k->gen_time = mutt_mktime (&st_time, 0);
214         break;
215       }
216     case 7:                    /* valid for n days */
217       break;
218     case 8:                    /* Local id         */
219       break;
220     case 9:                    /* ownertrust       */
221       break;
222     case 10:                   /* name             */
223       {
224         if (!pend || !*p)
225           break;                /* empty field or no trailing colon */
226
227         /* ignore user IDs on subkeys */
228         if (!is_uid && (*is_subkey && option (OPTPGPIGNORESUB)))
229           break;
230
231         uid = p_new(pgp_uid_t, 1);
232         fix_uid (p);
233         uid->addr = m_strdup(p);
234         uid->trust = trust;
235         uid->flags |= flags;
236         uid->parent = k;
237         uid->next = k->address;
238         k->address = uid;
239
240         if (strstr (p, "ENCR"))
241           k->flags |= KEYFLAG_PREFER_ENCRYPTION;
242         if (strstr (p, "SIGN"))
243           k->flags |= KEYFLAG_PREFER_SIGNING;
244
245         break;
246       }
247     case 11:                   /* signature class  */
248       break;
249     case 12:                   /* key capabilities */
250       while (*p) {
251         switch (*p++) {
252         case 'D':
253           flags |= KEYFLAG_DISABLED;
254           break;
255
256         case 'e':
257           flags |= KEYFLAG_CANENCRYPT;
258           break;
259
260         case 's':
261           flags |= KEYFLAG_CANSIGN;
262           break;
263         }
264       }
265
266       if (!is_uid && (!*is_subkey || !option (OPTPGPIGNORESUB)
267                       || !((flags & KEYFLAG_DISABLED)
268                            || (flags & KEYFLAG_REVOKED)
269                            || (flags & KEYFLAG_EXPIRED))))
270         k->flags |= flags;
271
272       break;
273
274     default:
275       break;
276     }
277   }
278   return k;
279 }
280
281 pgp_key_t pgp_get_candidates (pgp_ring_t keyring, string_list_t * hints)
282 {
283   FILE *fp;
284   pid_t thepid;
285   char buf[LONG_STRING];
286   pgp_key_t db = NULL, *kend, k = NULL, kk, mainkey = NULL;
287   int is_sub;
288   int devnull;
289
290   if ((devnull = open ("/dev/null", O_RDWR)) == -1)
291     return NULL;
292
293   m_strreplace(&_chs, Charset);
294
295   thepid = pgp_invoke_list_keys (NULL, &fp, NULL, -1, -1, devnull,
296                                  keyring, hints);
297   if (thepid == -1) {
298     close (devnull);
299     return NULL;
300   }
301
302   kend = &db;
303   k = NULL;
304   while (fgets (buf, sizeof (buf) - 1, fp)) {
305     if (!(kk = parse_pub_line (buf, &is_sub, k)))
306       continue;
307
308     /* Only append kk to the list if it's new. */
309     if (kk != k) {
310       if (k)
311         kend = &k->next;
312       *kend = k = kk;
313
314       if (is_sub) {
315         pgp_uid_t **l;
316
317         k->flags |= KEYFLAG_SUBKEY;
318         k->parent = mainkey;
319         for (l = &k->address; *l; l = &(*l)->next);
320         *l = pgp_copy_uids (mainkey->address, k);
321       }
322       else
323         mainkey = k;
324     }
325   }
326
327   if (ferror (fp))
328     mutt_perror ("fgets");
329
330   fclose (fp);
331   mutt_wait_filter (thepid);
332
333   close (devnull);
334
335   return db;
336 }