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