textual changes
[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 fingerprint( PKT_public_key *pk, PKT_secret_key *sk );
41
42
43 /****************
44  * List the keys
45  * If NNAMES is 0; all available keys are listed
46  */
47 void
48 public_key_list( int nnames, char **names )
49 {
50     if( !nnames )
51         list_all(0);
52     else { /* List by user id */
53         for( ; nnames ; nnames--, names++ )
54             list_one( *names, 0 );
55     }
56 }
57
58 void
59 secret_key_list( int nnames, char **names )
60 {
61     if( !nnames )
62         list_all(1);
63     else { /* List by user id */
64         for( ; nnames ; nnames--, names++ )
65             list_one( *names, 1 );
66     }
67 }
68
69
70 static void
71 list_all( int secret )
72 {
73     int i, seq=0;
74     const char *s;
75     IOBUF a;
76
77     while( (s=secret? get_secret_keyring(seq++):get_keyring(seq++)) ) {
78         if( !(a = iobuf_open(s)) ) {
79             log_error(_("can't open %s: %s\n"), s, strerror(errno));
80             continue;
81         }
82         if( seq > 1 )
83             putchar('\n');
84         printf("%s\n", s );
85         for(i=strlen(s); i; i-- )
86             putchar('-');
87         putchar('\n');
88
89         proc_packets( a );
90         iobuf_close(a);
91     }
92 }
93
94 static void
95 list_one( const char *name, int secret )
96 {
97     int rc = 0;
98     KBNODE keyblock = NULL;
99     KBNODE kbctx;
100     KBNODE node;
101     KBPOS kbpos;
102     PKT_public_key *pk;
103     PKT_secret_key *sk;
104     u32 keyid[2];
105     int any=0;
106     int trustletter = 0;
107
108     /* search the userid */
109     rc = secret? find_secret_keyblock_byname( &kbpos, name )
110                : find_keyblock_byname( &kbpos, name );
111     if( rc ) {
112         log_error("%s: user not found\n", name );
113         goto leave;
114     }
115
116     /* read the keyblock */
117     rc = read_keyblock( &kbpos, &keyblock );
118     if( rc ) {
119         log_error("%s: keyblock read problem: %s\n", name, g10_errstr(rc) );
120         goto leave;
121     }
122
123
124     /* get the keyid from the keyblock */
125     node = find_kbnode( keyblock, secret? PKT_SECRET_KEY : PKT_PUBLIC_KEY );
126     if( !node ) {
127         log_error("Oops; key lost!\n");
128         goto leave;
129     }
130
131     if( secret ) {
132         pk = NULL;
133         sk = node->pkt->pkt.secret_key;
134         keyid_from_sk( sk, keyid );
135         if( opt.with_colons )
136             printf("sec::%u:%d:%08lX%08lX:%s:%u:::",
137                     nbits_from_sk( sk ),
138                     sk->pubkey_algo,
139                     (ulong)keyid[0],(ulong)keyid[1],
140                     datestr_from_sk( sk ),
141                     (unsigned)sk->valid_days
142                     /* fixme: add LID here */ );
143         else
144             printf("sec  %4u%c/%08lX %s ", nbits_from_sk( sk ),
145                                        pubkey_letter( sk->pubkey_algo ),
146                                        (ulong)keyid[1],
147                                        datestr_from_sk( sk ) );
148     }
149     else {
150         pk = node->pkt->pkt.public_key;
151         sk = NULL;
152         keyid_from_pk( pk, keyid );
153         if( opt.with_colons ) {
154             trustletter = query_trust_info( pk );
155             printf("pub:%c:%u:%d:%08lX%08lX:%s:%u:",
156                     trustletter,
157                     nbits_from_pk( pk ),
158                     pk->pubkey_algo,
159                     (ulong)keyid[0],(ulong)keyid[1],
160                     datestr_from_pk( pk ),
161                     (unsigned)pk->valid_days );
162             if( pk->local_id )
163                 printf("%lu", pk->local_id );
164             putchar(':');
165             /* fixme: add ownertrust here */
166             putchar(':');
167         }
168         else
169             printf("pub  %4u%c/%08lX %s ", nbits_from_pk( pk ),
170                                        pubkey_letter( pk->pubkey_algo ),
171                                        (ulong)keyid[1],
172                                        datestr_from_pk( pk ) );
173     }
174
175     for( kbctx=NULL; (node=walk_kbnode( keyblock, &kbctx, 0)) ; ) {
176         if( node->pkt->pkttype == PKT_USER_ID ) {
177             if( any ) {
178                 if( opt.with_colons )
179                     printf("uid:::::::::");
180                 else
181                     printf("uid%*s", 28, "");
182             }
183             print_string( stdout,  node->pkt->pkt.user_id->name,
184                           node->pkt->pkt.user_id->len, opt.with_colons );
185             if( opt.with_colons )
186                 putchar(':');
187             putchar('\n');
188             if( !any ) {
189                 if( opt.fingerprint )
190                     fingerprint( pk, sk );
191                 any = 1;
192             }
193         }
194         else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
195             u32 keyid2[2];
196             PKT_public_key *pk2 = node->pkt->pkt.public_key;
197
198             if( !any ) {
199                 putchar('\n');
200                 if( opt.fingerprint )
201                     fingerprint( pk, sk ); /* of the main key */
202                 any = 1;
203             }
204
205             keyid_from_pk( pk2, keyid2 );
206             if( opt.with_colons ) {
207                 printf("sub:%c:%u:%d:%08lX%08lX:%s:%u:",
208                         trustletter,
209                         nbits_from_pk( pk2 ),
210                         pk2->pubkey_algo,
211                         (ulong)keyid2[0],(ulong)keyid2[1],
212                         datestr_from_pk( pk2 ),
213                         (unsigned)pk2->valid_days
214                         /* fixme: add LID and ownertrust here */
215                                                 );
216                 if( pk->local_id ) /* use the local_id of the main key??? */
217                     printf("%lu", pk->local_id );
218                 putchar(':');
219                 putchar(':');
220                 putchar('\n');
221             }
222             else
223                 printf("sub  %4u%c/%08lX %s\n", nbits_from_pk( pk2 ),
224                                            pubkey_letter( pk2->pubkey_algo ),
225                                            (ulong)keyid2[1],
226                                            datestr_from_pk( pk2 ) );
227         }
228         else if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
229             u32 keyid2[2];
230             PKT_secret_key *sk2 = node->pkt->pkt.secret_key;
231
232             if( !any ) {
233                 putchar('\n');
234                 if( opt.fingerprint )
235                     fingerprint( pk, sk ); /* of the main key */
236                 any = 1;
237             }
238
239             keyid_from_sk( sk2, keyid2 );
240             if( opt.with_colons )
241                 printf("ssb::%u:%d:%08lX%08lX:%s:%u:::\n",
242                         nbits_from_sk( sk2 ),
243                         sk2->pubkey_algo,
244                         (ulong)keyid2[0],(ulong)keyid2[1],
245                         datestr_from_sk( sk2 ),
246                         (unsigned)sk2->valid_days
247                         /* fixme: add LID */
248                                                 );
249             else
250                 printf("ssb  %4u%c/%08lX %s\n", nbits_from_sk( sk2 ),
251                                            pubkey_letter( sk2->pubkey_algo ),
252                                            (ulong)keyid2[1],
253                                            datestr_from_sk( sk2 ) );
254         }
255         else if( opt.list_sigs && node->pkt->pkttype == PKT_SIGNATURE ) {
256             PKT_signature *sig = node->pkt->pkt.signature;
257             int sigrc;
258
259             if( !any ) { /* no user id, (maybe a revocation follows)*/
260                 if( sig->sig_class == 0x20 )
261                     puts("[revoked]");
262                 else if( sig->sig_class == 0x18 )
263                     puts("[key binding]");
264                 else
265                     putchar('\n');
266                 if( opt.fingerprint )
267                     fingerprint( pk, sk );
268                 any=1;
269             }
270
271             if( sig->sig_class == 0x20 || sig->sig_class == 0x30 )
272                 fputs("rev", stdout);
273             else if( (sig->sig_class&~3) == 0x10 )
274                 fputs("sig", stdout);
275             else if( sig->sig_class == 0x18 )
276                 fputs("sig", stdout);
277             else {
278                 if( opt.with_colons )
279                     printf("sig::::::::::%02x:\n",sig->sig_class );
280                 else
281                     printf("sig                             "
282                        "[unexpected signature class 0x%02x]\n",sig->sig_class );
283                 continue;
284             }
285             if( opt.check_sigs ) {
286                 fflush(stdout);
287                 rc = check_key_signature( keyblock, node, NULL );
288                 switch( rc ) {
289                   case 0:                  sigrc = '!'; break;
290                   case G10ERR_BAD_SIGN:    sigrc = '-'; break;
291                   case G10ERR_NO_PUBKEY:   sigrc = '?'; break;
292                   default:                 sigrc = '%'; break;
293                 }
294             }
295             else {
296                 rc = 0;
297                 sigrc = ' ';
298             }
299             if( opt.with_colons ) {
300                 putchar(':');
301                 if( sigrc != ' ' )
302                     putchar(sigrc);
303                 printf(":::%08lX%08lX:%s::::", (ulong)sig->keyid[0],
304                            (ulong)sig->keyid[1], datestr_from_sig(sig));
305             }
306             else
307                 printf("%c       %08lX %s  ",
308                     sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig));
309             if( sigrc == '%' )
310                 printf("[%s] ", g10_errstr(rc) );
311             else if( sigrc == '?' )
312                 ;
313             else {
314                 size_t n;
315                 char *p = get_user_id( sig->keyid, &n );
316                 print_string( stdout, p, n, opt.with_colons );
317                 m_free(p);
318             }
319             if( opt.with_colons )
320                 printf(":%02x:", sig->sig_class );
321             putchar('\n');
322             /* FIXME: check or list other sigs here (subpkt PRIV_ADD_SIG)*/
323         }
324     }
325     if( !any ) {/* oops, no user id */
326         if( opt.with_colons )
327             putchar(':');
328         putchar('\n');
329     }
330
331
332   leave:
333     release_kbnode( keyblock );
334 }
335
336 static void
337 fingerprint( PKT_public_key *pk, PKT_secret_key *sk )
338 {
339     byte *array, *p;
340     size_t i, n;
341
342     p = array = pk? fingerprint_from_pk( pk, &n )
343                    : fingerprint_from_sk( sk, &n );
344     if( opt.with_colons ) {
345         printf("fpr:::::::::");
346         for(i=0; i < n ; i++, p++ )
347             printf("%02X", *p );
348         putchar(':');
349     }
350     else {
351         printf("     Key fingerprint =");
352         if( n == 20 ) {
353             for(i=0; i < n ; i++, i++, p += 2 ) {
354                 if( i == 10 )
355                     putchar(' ');
356                 printf(" %02X%02X", *p, p[1] );
357             }
358         }
359         else {
360             for(i=0; i < n ; i++, p++ ) {
361                 if( i && !(i%8) )
362                     putchar(' ');
363                 printf(" %02X", *p );
364             }
365         }
366     }
367     putchar('\n');
368     m_free(array);
369 }
370