Epxerimenta support for GDBM keyings.
[gnupg.git] / g10 / keylist.c
1 /* keylist.c
2  *      Copyright (C) 1998 Free Software Foundation, Inc.
3  *
4  * This file is part of GNUPG.
5  *
6  * GNUPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * GNUPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <assert.h>
27
28 #include "options.h"
29 #include "packet.h"
30 #include "errors.h"
31 #include "keydb.h"
32 #include "memory.h"
33 #include "util.h"
34 #include "trustdb.h"
35 #include "main.h"
36 #include "i18n.h"
37
38 static void list_all(int);
39 static void list_one(const char *name, int secret);
40 static void list_keyblock( KBNODE keyblock, int secret );
41 static void fingerprint( PKT_public_key *pk, PKT_secret_key *sk );
42
43
44 /****************
45  * List the keys
46  * If NNAMES is 0; all available keys are listed
47  */
48 void
49 public_key_list( int nnames, char **names )
50 {
51     if( !nnames )
52         list_all(0);
53     else { /* List by user id */
54         for( ; nnames ; nnames--, names++ )
55             list_one( *names, 0 );
56     }
57 }
58
59 void
60 secret_key_list( int nnames, char **names )
61 {
62     if( !nnames )
63         list_all(1);
64     else { /* List by user id */
65         for( ; nnames ; nnames--, names++ )
66             list_one( *names, 1 );
67     }
68 }
69
70
71 static void
72 list_all( int secret )
73 {
74     KBPOS kbpos;
75     KBNODE keyblock = NULL;
76     int rc=0;
77     int lastresno;
78
79     rc = enum_keyblocks( secret? 5:0, &kbpos, &keyblock );
80     if( rc ) {
81         if( rc != -1 )
82             log_error("enum_keyblocks(open) failed: %s\n", g10_errstr(rc) );
83         goto leave;
84     }
85
86     lastresno = -1;
87     while( !(rc = enum_keyblocks( 1, &kbpos, &keyblock )) ) {
88         if( lastresno != kbpos.resno ) {
89             const char *s = keyblock_resource_name( &kbpos );
90             int i;
91
92             lastresno = kbpos.resno;
93             printf("%s\n", s );
94             for(i=strlen(s); i; i-- )
95                 putchar('-');
96             putchar('\n');
97         }
98         merge_keys_and_selfsig( keyblock );
99         list_keyblock( keyblock, secret );
100         release_kbnode( keyblock ); keyblock = NULL;
101     }
102
103     if( rc && rc != -1 )
104         log_error("enum_keyblocks(read) failed: %s\n", g10_errstr(rc));
105
106   leave:
107     enum_keyblocks( 2, &kbpos, &keyblock ); /* close */
108     release_kbnode( keyblock );
109 }
110
111
112
113 static void
114 list_one( const char *name, int secret )
115 {
116     int rc = 0;
117     KBNODE keyblock = NULL;
118     KBPOS kbpos;
119
120     rc = secret? find_secret_keyblock_byname( &kbpos, name )
121                : find_keyblock_byname( &kbpos, name );
122     if( rc ) {
123         log_error("%s: user not found\n", name );
124         return;
125     }
126
127     rc = read_keyblock( &kbpos, &keyblock );
128     if( rc ) {
129         log_error("%s: keyblock read problem: %s\n", name, g10_errstr(rc) );
130         return;
131     }
132     merge_keys_and_selfsig( keyblock );
133     list_keyblock( keyblock, secret );
134     release_kbnode( keyblock );
135 }
136
137
138 static void
139 list_keyblock( KBNODE keyblock, int secret )
140 {
141     int rc = 0;
142     KBNODE kbctx;
143     KBNODE node;
144     PKT_public_key *pk;
145     PKT_secret_key *sk;
146     u32 keyid[2];
147     int any=0;
148     int trustletter = 0;
149
150     /* get the keyid from the keyblock */
151     node = find_kbnode( keyblock, secret? PKT_SECRET_KEY : PKT_PUBLIC_KEY );
152     if( !node ) {
153         log_error("Oops; key lost!\n");
154         return;
155     }
156
157     if( secret ) {
158         pk = NULL;
159         sk = node->pkt->pkt.secret_key;
160         keyid_from_sk( sk, keyid );
161         if( opt.with_colons )
162             printf("sec::%u:%d:%08lX%08lX:%s:%s:::",
163                     nbits_from_sk( sk ),
164                     sk->pubkey_algo,
165                     (ulong)keyid[0],(ulong)keyid[1],
166                     datestr_from_sk( sk ),
167                     sk->expiredate? strtimestamp(sk->expiredate):""
168                     /* fixme: add LID here */ );
169         else
170             printf("sec  %4u%c/%08lX %s ", nbits_from_sk( sk ),
171                                        pubkey_letter( sk->pubkey_algo ),
172                                        (ulong)keyid[1],
173                                        datestr_from_sk( sk ) );
174     }
175     else {
176         pk = node->pkt->pkt.public_key;
177         sk = NULL;
178         keyid_from_pk( pk, keyid );
179         if( opt.with_colons ) {
180             trustletter = query_trust_info( pk );
181             printf("pub:%c:%u:%d:%08lX%08lX:%s:%s:",
182                     trustletter,
183                     nbits_from_pk( pk ),
184                     pk->pubkey_algo,
185                     (ulong)keyid[0],(ulong)keyid[1],
186                     datestr_from_pk( pk ),
187                     pk->expiredate? strtimestamp(pk->expiredate):""
188                                              );
189             if( pk->local_id )
190                 printf("%lu", pk->local_id );
191             putchar(':');
192             /* fixme: add ownertrust here */
193             putchar(':');
194         }
195         else
196             printf("pub  %4u%c/%08lX %s ", nbits_from_pk( pk ),
197                                        pubkey_letter( pk->pubkey_algo ),
198                                        (ulong)keyid[1],
199                                        datestr_from_pk( pk ) );
200     }
201
202     for( kbctx=NULL; (node=walk_kbnode( keyblock, &kbctx, 0)) ; ) {
203         if( node->pkt->pkttype == PKT_USER_ID ) {
204             if( any ) {
205                 if( opt.with_colons )
206                     printf("uid:::::::::");
207                 else
208                     printf("uid%*s", 28, "");
209             }
210             print_string( stdout,  node->pkt->pkt.user_id->name,
211                           node->pkt->pkt.user_id->len, opt.with_colons );
212             if( opt.with_colons )
213                 putchar(':');
214             putchar('\n');
215             if( !any ) {
216                 if( opt.fingerprint )
217                     fingerprint( pk, sk );
218                 any = 1;
219             }
220         }
221         else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
222             u32 keyid2[2];
223             PKT_public_key *pk2 = node->pkt->pkt.public_key;
224
225             if( !any ) {
226                 putchar('\n');
227                 if( opt.fingerprint )
228                     fingerprint( pk, sk ); /* of the main key */
229                 any = 1;
230             }
231
232
233
234             keyid_from_pk( pk2, keyid2 );
235             if( opt.with_colons ) {
236                 printf("sub:%c:%u:%d:%08lX%08lX:%s:%s:",
237                         trustletter,
238                         nbits_from_pk( pk2 ),
239                         pk2->pubkey_algo,
240                         (ulong)keyid2[0],(ulong)keyid2[1],
241                         datestr_from_pk( pk2 ),
242                         pk2->expiredate? strtimestamp(pk2->expiredate):""
243                         /* fixme: add LID and ownertrust here */
244                                                 );
245                 if( pk->local_id ) /* use the local_id of the main key??? */
246                     printf("%lu", pk->local_id );
247                 putchar(':');
248                 putchar(':');
249                 putchar('\n');
250             }
251             else
252                 printf("sub  %4u%c/%08lX %s\n", nbits_from_pk( pk2 ),
253                                            pubkey_letter( pk2->pubkey_algo ),
254                                            (ulong)keyid2[1],
255                                            datestr_from_pk( pk2 ) );
256             if( opt.fingerprint > 1 )
257                 fingerprint( pk2, NULL );
258         }
259         else if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
260             u32 keyid2[2];
261             PKT_secret_key *sk2 = node->pkt->pkt.secret_key;
262
263             if( !any ) {
264                 putchar('\n');
265                 if( opt.fingerprint )
266                     fingerprint( pk, sk ); /* of the main key */
267                 any = 1;
268             }
269
270             keyid_from_sk( sk2, keyid2 );
271             if( opt.with_colons )
272                 printf("ssb::%u:%d:%08lX%08lX:%s:%s:::\n",
273                         nbits_from_sk( sk2 ),
274                         sk2->pubkey_algo,
275                         (ulong)keyid2[0],(ulong)keyid2[1],
276                         datestr_from_sk( sk2 ),
277                         sk2->expiredate? strtimestamp(sk2->expiredate):""
278                         /* fixme: add LID */
279                                                 );
280             else
281                 printf("ssb  %4u%c/%08lX %s\n", nbits_from_sk( sk2 ),
282                                            pubkey_letter( sk2->pubkey_algo ),
283                                            (ulong)keyid2[1],
284                                            datestr_from_sk( sk2 ) );
285             if( opt.fingerprint > 1 )
286                 fingerprint( NULL, sk2 );
287
288         }
289         else if( opt.list_sigs && node->pkt->pkttype == PKT_SIGNATURE ) {
290             PKT_signature *sig = node->pkt->pkt.signature;
291             int sigrc;
292
293             if( !any ) { /* no user id, (maybe a revocation follows)*/
294                 if( sig->sig_class == 0x20 )
295                     puts("[revoked]");
296                 else if( sig->sig_class == 0x18 )
297                     puts("[key binding]");
298                 else
299                     putchar('\n');
300                 if( opt.fingerprint )
301                     fingerprint( pk, sk );
302                 any=1;
303             }
304
305             if( sig->sig_class == 0x20 || sig->sig_class == 0x30 )
306                 fputs("rev", stdout);
307             else if( (sig->sig_class&~3) == 0x10 )
308                 fputs("sig", stdout);
309             else if( sig->sig_class == 0x18 )
310                 fputs("sig", stdout);
311             else {
312                 if( opt.with_colons )
313                     printf("sig::::::::::%02x:\n",sig->sig_class );
314                 else
315                     printf("sig                             "
316                        "[unexpected signature class 0x%02x]\n",sig->sig_class );
317                 continue;
318             }
319             if( opt.check_sigs ) {
320                 fflush(stdout);
321                 rc = check_key_signature( keyblock, node, NULL );
322                 switch( rc ) {
323                   case 0:                  sigrc = '!'; break;
324                   case G10ERR_BAD_SIGN:    sigrc = '-'; break;
325                   case G10ERR_NO_PUBKEY:   sigrc = '?'; break;
326                   default:                 sigrc = '%'; break;
327                 }
328             }
329             else {
330                 rc = 0;
331                 sigrc = ' ';
332             }
333             if( opt.with_colons ) {
334                 putchar(':');
335                 if( sigrc != ' ' )
336                     putchar(sigrc);
337                 printf(":::%08lX%08lX:%s::::", (ulong)sig->keyid[0],
338                            (ulong)sig->keyid[1], datestr_from_sig(sig));
339             }
340             else
341                 printf("%c       %08lX %s  ",
342                     sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig));
343             if( sigrc == '%' )
344                 printf("[%s] ", g10_errstr(rc) );
345             else if( sigrc == '?' )
346                 ;
347             else {
348                 size_t n;
349                 char *p = get_user_id( sig->keyid, &n );
350                 print_string( stdout, p, n, opt.with_colons );
351                 m_free(p);
352             }
353             if( opt.with_colons )
354                 printf(":%02x:", sig->sig_class );
355             putchar('\n');
356             /* FIXME: check or list other sigs here (subpkt PRIV_ADD_SIG)*/
357         }
358     }
359     if( !any ) {/* oops, no user id */
360         if( opt.with_colons )
361             putchar(':');
362         putchar('\n');
363     }
364 }
365
366
367
368 static void
369 fingerprint( PKT_public_key *pk, PKT_secret_key *sk )
370 {
371     byte *array, *p;
372     size_t i, n;
373
374     p = array = pk? fingerprint_from_pk( pk, NULL, &n )
375                    : fingerprint_from_sk( sk, NULL, &n );
376     if( opt.with_colons ) {
377         printf("fpr:::::::::");
378         for(i=0; i < n ; i++, p++ )
379             printf("%02X", *p );
380         putchar(':');
381     }
382     else {
383         printf("     Key fingerprint =");
384         if( n == 20 ) {
385             for(i=0; i < n ; i++, i++, p += 2 ) {
386                 if( i == 10 )
387                     putchar(' ');
388                 printf(" %02X%02X", *p, p[1] );
389             }
390         }
391         else {
392             for(i=0; i < n ; i++, p++ ) {
393                 if( i && !(i%8) )
394                     putchar(' ');
395                 printf(" %02X", *p );
396             }
397         }
398     }
399     putchar('\n');
400     m_free(array);
401 }
402