Converted all m_free to xfree etc.
[gnupg.git] / g10 / keyedit.c
1 /* keyedit.c - keyedit stuff
2  * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3  *               2005 Free Software Foundation, Inc.
4  *
5  * This file is part of GnuPG.
6  *
7  * GnuPG is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * GnuPG is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20  * USA.
21  */
22
23 #include <config.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #ifdef HAVE_LIBREADLINE
31 #include <stdio.h>
32 #include <readline/readline.h>
33 #endif
34 #include "options.h"
35 #include "packet.h"
36 #include "errors.h"
37 #include "iobuf.h"
38 #include "keydb.h"
39 #include "memory.h"
40 #include "photoid.h"
41 #include "util.h"
42 #include "main.h"
43 #include "trustdb.h"
44 #include "filter.h"
45 #include "ttyio.h"
46 #include "status.h"
47 #include "i18n.h"
48 #include "keyserver-internal.h"
49
50 static void show_prefs( PKT_user_id *uid, PKT_signature *selfsig, int verbose);
51 static void show_key_with_all_names( KBNODE keyblock, int only_marked,
52             int with_revoker, int with_fpr, int with_subkeys, int with_prefs );
53 static void show_key_and_fingerprint( KBNODE keyblock );
54 static int menu_adduid( KBNODE keyblock, KBNODE sec_keyblock, int photo );
55 static void menu_deluid( KBNODE pub_keyblock, KBNODE sec_keyblock );
56 static int menu_delsig( KBNODE pub_keyblock );
57 static int menu_clean_sigs_from_uids(KBNODE keyblock);
58 static int menu_clean_uids_from_key(KBNODE keyblock);
59 static void menu_delkey( KBNODE pub_keyblock, KBNODE sec_keyblock );
60 static int menu_addrevoker( KBNODE pub_keyblock,
61                             KBNODE sec_keyblock, int sensitive );
62 static int menu_expire( KBNODE pub_keyblock, KBNODE sec_keyblock );
63 static int menu_set_primary_uid( KBNODE pub_keyblock, KBNODE sec_keyblock );
64 static int menu_set_preferences( KBNODE pub_keyblock, KBNODE sec_keyblock );
65 static int menu_set_keyserver_url (const char *url,
66                                    KBNODE pub_keyblock, KBNODE sec_keyblock );
67 static int menu_select_uid( KBNODE keyblock, int idx );
68 static int menu_select_uid_namehash( KBNODE keyblock, const char *namehash );
69 static int menu_select_key( KBNODE keyblock, int idx );
70 static int count_uids( KBNODE keyblock );
71 static int count_uids_with_flag( KBNODE keyblock, unsigned flag );
72 static int count_keys_with_flag( KBNODE keyblock, unsigned flag );
73 static int count_selected_uids( KBNODE keyblock );
74 static int real_uids_left( KBNODE keyblock );
75 static int count_selected_keys( KBNODE keyblock );
76 static int menu_revsig( KBNODE keyblock );
77 static int menu_revuid( KBNODE keyblock, KBNODE sec_keyblock );
78 static int menu_revkey( KBNODE pub_keyblock, KBNODE sec_keyblock );
79 static int menu_revsubkey( KBNODE pub_keyblock, KBNODE sec_keyblock );
80 static int enable_disable_key( KBNODE keyblock, int disable );
81 static void menu_showphoto( KBNODE keyblock );
82
83 static int update_trust=0;
84
85 #define CONTROL_D ('D' - 'A' + 1)
86
87 #define NODFLG_BADSIG (1<<0)  /* bad signature */
88 #define NODFLG_NOKEY  (1<<1)  /* no public key */
89 #define NODFLG_SIGERR (1<<2)  /* other sig error */
90
91 #define NODFLG_MARK_A (1<<4)  /* temporary mark */
92 #define NODFLG_DELSIG (1<<5)  /* to be deleted */
93
94 #define NODFLG_SELUID (1<<8)  /* indicate the selected userid */
95 #define NODFLG_SELKEY (1<<9)  /* indicate the selected key */
96 #define NODFLG_SELSIG (1<<10) /* indicate a selected signature */
97
98 struct sign_attrib {
99     int non_exportable,non_revocable;
100     struct revocation_reason_info *reason;
101     byte trust_depth,trust_value;
102     char *trust_regexp;
103 };
104
105
106 #ifdef ENABLE_CARD_SUPPORT
107 /* Given a node SEC_NODE with a secret key or subkey, locate the
108    corresponding public key from pub_keyblock. */
109 static PKT_public_key *
110 find_pk_from_sknode (KBNODE pub_keyblock, KBNODE sec_node)
111 {
112   KBNODE node = pub_keyblock;
113   PKT_secret_key *sk;
114   PKT_public_key *pk;
115   
116   if (sec_node->pkt->pkttype == PKT_SECRET_KEY
117       && node->pkt->pkttype == PKT_PUBLIC_KEY)
118     return node->pkt->pkt.public_key;
119   if (sec_node->pkt->pkttype != PKT_SECRET_SUBKEY)
120     return NULL;
121   sk = sec_node->pkt->pkt.secret_key;
122   for (; node; node = node->next)
123     if (node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
124       {
125         pk = node->pkt->pkt.public_key;
126         if (pk->keyid[0] == sk->keyid[0] && pk->keyid[1] == sk->keyid[1])
127           return pk;
128       }
129       
130   return NULL;
131 }
132 #endif /* ENABLE_CARD_SUPPORT */
133
134
135 /* TODO: Fix duplicated code between here and the check-sigs/list-sigs
136    code in keylist.c. */
137 static int
138 print_and_check_one_sig_colon( KBNODE keyblock, KBNODE node,
139                                int *inv_sigs, int *no_key, int *oth_err,
140                                int *is_selfsig, int print_without_key )
141 {
142   PKT_signature *sig = node->pkt->pkt.signature;
143   int rc, sigrc;
144
145   /* TODO: Make sure a cached sig record here still has the pk that
146      issued it.  See also keylist.c:list_keyblock_print */
147
148   switch((rc=check_key_signature(keyblock,node,is_selfsig)))
149     {
150     case 0:
151       node->flag &= ~(NODFLG_BADSIG|NODFLG_NOKEY|NODFLG_SIGERR);
152       sigrc = '!';
153       break;
154     case G10ERR_BAD_SIGN:
155       node->flag = NODFLG_BADSIG;
156       sigrc = '-';
157       if( inv_sigs )
158         ++*inv_sigs;
159       break;
160     case G10ERR_NO_PUBKEY:
161     case G10ERR_UNU_PUBKEY:
162       node->flag = NODFLG_NOKEY;
163       sigrc = '?';
164       if( no_key )
165         ++*no_key;
166       break;
167     default:
168       node->flag = NODFLG_SIGERR;
169       sigrc = '%';
170       if( oth_err )
171         ++*oth_err;
172       break;
173     }
174
175   if( sigrc != '?' || print_without_key )
176     {
177       printf("sig:%c::%d:%08lX%08lX:%lu:%lu:",
178              sigrc,sig->pubkey_algo,(ulong)sig->keyid[0],(ulong)sig->keyid[1],
179              (ulong)sig->timestamp,(ulong)sig->expiredate);
180
181       if(sig->trust_depth || sig->trust_value)
182         printf("%d %d",sig->trust_depth,sig->trust_value);
183
184       printf(":");
185
186       if(sig->trust_regexp)
187         print_string(stdout,sig->trust_regexp,strlen(sig->trust_regexp),':');
188
189       printf("::%02x%c\n",sig->sig_class,sig->flags.exportable?'x':'l');
190
191       if(opt.show_subpackets)
192         print_subpackets_colon(sig);
193     }
194
195   return (sigrc == '!');
196 }
197
198
199 /****************
200  * Print information about a signature, check it and return true
201  * if the signature is okay. NODE must be a signature packet.
202  */
203 static int
204 print_and_check_one_sig( KBNODE keyblock, KBNODE node,
205                          int *inv_sigs, int *no_key, int *oth_err,
206                         int *is_selfsig, int print_without_key )
207 {
208     PKT_signature *sig = node->pkt->pkt.signature;
209     int rc, sigrc;
210     int is_rev = sig->sig_class == 0x30;
211
212     /* TODO: Make sure a cached sig record here still has the pk that
213        issued it.  See also keylist.c:list_keyblock_print */
214
215     switch( (rc = check_key_signature( keyblock, node, is_selfsig)) ) {
216       case 0:
217         node->flag &= ~(NODFLG_BADSIG|NODFLG_NOKEY|NODFLG_SIGERR);
218         sigrc = '!';
219         break;
220       case G10ERR_BAD_SIGN:
221         node->flag = NODFLG_BADSIG;
222         sigrc = '-';
223         if( inv_sigs )
224             ++*inv_sigs;
225         break;
226       case G10ERR_NO_PUBKEY:
227       case G10ERR_UNU_PUBKEY:
228         node->flag = NODFLG_NOKEY;
229         sigrc = '?';
230         if( no_key )
231             ++*no_key;
232         break;
233       default:
234         node->flag = NODFLG_SIGERR;
235         sigrc = '%';
236         if( oth_err )
237             ++*oth_err;
238         break;
239     }
240     if( sigrc != '?' || print_without_key ) {
241         tty_printf("%s%c%c %c%c%c%c%c%c %s %s",
242                    is_rev? "rev":"sig",sigrc,
243                    (sig->sig_class-0x10>0 &&
244                     sig->sig_class-0x10<4)?'0'+sig->sig_class-0x10:' ',
245                    sig->flags.exportable?' ':'L',
246                    sig->flags.revocable?' ':'R',
247                    sig->flags.policy_url?'P':' ',
248                    sig->flags.notation?'N':' ',
249                    sig->flags.expired?'X':' ',
250                    (sig->trust_depth>9)?'T':
251                    (sig->trust_depth>0)?'0'+sig->trust_depth:' ',
252                    keystr(sig->keyid),datestr_from_sig(sig));
253         if(opt.list_options&LIST_SHOW_SIG_EXPIRE)
254           tty_printf(" %s",expirestr_from_sig(sig));
255         tty_printf("  ");
256         if( sigrc == '%' )
257             tty_printf("[%s] ", g10_errstr(rc) );
258         else if( sigrc == '?' )
259             ;
260         else if( *is_selfsig ) {
261             tty_printf( is_rev? _("[revocation]")
262                               : _("[self-signature]") );
263         }
264         else
265           {
266             size_t n;
267             char *p = get_user_id( sig->keyid, &n );
268             tty_print_utf8_string2(p, n, opt.screen_columns-keystrlen()-26-
269                                ((opt.list_options&LIST_SHOW_SIG_EXPIRE)?11:0));
270             xfree(p);
271           }
272         tty_printf("\n");
273
274         if(sig->flags.policy_url && (opt.list_options&LIST_SHOW_POLICY_URLS))
275           show_policy_url(sig,3,0);
276
277         if(sig->flags.notation && (opt.list_options&LIST_SHOW_NOTATIONS))
278           show_notation(sig,3,0,
279                         ((opt.list_options&LIST_SHOW_STD_NOTATIONS)?1:0)+
280                         ((opt.list_options&LIST_SHOW_USER_NOTATIONS)?2:0));
281
282         if(sig->flags.pref_ks && (opt.list_options&LIST_SHOW_KEYSERVER_URLS))
283           show_keyserver_url(sig,3,0);
284     }
285
286     return (sigrc == '!');
287 }
288
289
290
291 /****************
292  * Check the keysigs and set the flags to indicate errors.
293  * Returns true if error found.
294  */
295 static int
296 check_all_keysigs( KBNODE keyblock, int only_selected )
297 {
298     KBNODE kbctx;
299     KBNODE node;
300     int inv_sigs = 0;
301     int no_key = 0;
302     int oth_err = 0;
303     int has_selfsig = 0;
304     int mis_selfsig = 0;
305     int selected = !only_selected;
306     int anyuid = 0;
307
308     for( kbctx=NULL; (node=walk_kbnode( keyblock, &kbctx, 0)) ; ) {
309         if( node->pkt->pkttype == PKT_USER_ID ) {
310             PKT_user_id *uid = node->pkt->pkt.user_id;
311
312             if( only_selected )
313                 selected = (node->flag & NODFLG_SELUID);
314             if( selected ) {
315                 tty_printf("uid  ");
316                 tty_print_utf8_string( uid->name, uid->len );
317                 tty_printf("\n");
318                 if( anyuid && !has_selfsig )
319                     mis_selfsig++;
320                 has_selfsig = 0;
321                 anyuid = 1;
322             }
323         }
324         else if( selected && node->pkt->pkttype == PKT_SIGNATURE
325                  && ( (node->pkt->pkt.signature->sig_class&~3) == 0x10
326                      || node->pkt->pkt.signature->sig_class == 0x30 )  ) {
327             int selfsig;
328
329             if( print_and_check_one_sig( keyblock, node, &inv_sigs,
330                                         &no_key, &oth_err, &selfsig, 0 ) ) {
331                 if( selfsig )
332                     has_selfsig = 1;
333             }
334             /* Hmmm: should we update the trustdb here? */
335         }
336     }
337     if( !has_selfsig )
338         mis_selfsig++;
339     if( inv_sigs == 1 )
340         tty_printf(_("1 bad signature\n") );
341     else if( inv_sigs )
342         tty_printf(_("%d bad signatures\n"), inv_sigs );
343     if( no_key == 1 )
344         tty_printf(_("1 signature not checked due to a missing key\n") );
345     else if( no_key )
346         tty_printf(_("%d signatures not checked due to missing keys\n"), no_key );
347     if( oth_err == 1 )
348         tty_printf(_("1 signature not checked due to an error\n") );
349     else if( oth_err )
350         tty_printf(_("%d signatures not checked due to errors\n"), oth_err );
351     if( mis_selfsig == 1 )
352         tty_printf(_("1 user ID without valid self-signature detected\n"));
353     else if( mis_selfsig  )
354         tty_printf(_("%d user IDs without valid self-signatures detected\n"),
355                                                                     mis_selfsig);
356
357     return inv_sigs || no_key || oth_err || mis_selfsig;
358 }
359
360
361 static int
362 sign_mk_attrib( PKT_signature *sig, void *opaque )
363 {
364     struct sign_attrib *attrib = opaque;
365     byte buf[8];
366
367     if( attrib->non_exportable ) {
368         buf[0] = 0; /* not exportable */
369         build_sig_subpkt( sig, SIGSUBPKT_EXPORTABLE, buf, 1 );
370     }
371
372     if( attrib->non_revocable ) {
373         buf[0] = 0; /* not revocable */
374         build_sig_subpkt( sig, SIGSUBPKT_REVOCABLE, buf, 1 );
375     }
376
377     if( attrib->reason )
378         revocation_reason_build_cb( sig, attrib->reason );
379
380     if(attrib->trust_depth)
381       {
382         /* Not critical.  If someone doesn't understand trust sigs,
383            this can still be a valid regular signature. */
384         buf[0] = attrib->trust_depth;
385         buf[1] = attrib->trust_value;
386         build_sig_subpkt(sig,SIGSUBPKT_TRUST,buf,2);
387
388         /* Critical.  If someone doesn't understands regexps, this
389            whole sig should be invalid.  Note the +1 for the length -
390            regexps are null terminated. */
391         if(attrib->trust_regexp)
392           build_sig_subpkt(sig,SIGSUBPKT_FLAG_CRITICAL|SIGSUBPKT_REGEXP,
393                            attrib->trust_regexp,
394                            strlen(attrib->trust_regexp)+1);
395       }
396
397     return 0;
398 }
399
400 static void
401 trustsig_prompt(byte *trust_value,byte *trust_depth,char **regexp)
402 {
403   char *p;
404
405   *trust_value=0;
406   *trust_depth=0;
407   *regexp=NULL;
408
409   /* Same string as pkclist.c:do_edit_ownertrust */
410   tty_printf(_("Please decide how far you trust this user to correctly verify"
411                " other users' keys\n(by looking at passports, checking"
412                " fingerprints from different sources, etc.)\n"));
413   tty_printf("\n");
414   tty_printf (_("  %d = I trust marginally\n"), 1);
415   tty_printf (_("  %d = I trust fully\n"), 2);
416   tty_printf("\n");
417
418   while(*trust_value==0)
419     {
420       p = cpr_get("trustsig_prompt.trust_value",_("Your selection? "));
421       trim_spaces(p);
422       cpr_kill_prompt();
423       /* 60 and 120 are as per RFC2440 */
424       if(p[0]=='1' && !p[1])
425         *trust_value=60;
426       else if(p[0]=='2' && !p[1])
427         *trust_value=120;
428       xfree(p);
429     }
430
431   tty_printf("\n");
432
433   tty_printf(_(
434               "Please enter the depth of this trust signature.\n"
435               "A depth greater than 1 allows the key you are signing to make\n"
436               "trust signatures on your behalf.\n"));
437   tty_printf("\n");
438
439   while(*trust_depth==0)
440     {
441       p = cpr_get("trustsig_prompt.trust_depth",_("Your selection? "));
442       trim_spaces(p);
443       cpr_kill_prompt();
444       *trust_depth=atoi(p);
445       xfree(p);
446     }
447
448   tty_printf("\n");
449
450   tty_printf(_("Please enter a domain to restrict this signature, "
451                "or enter for none.\n"));
452
453   tty_printf("\n");
454
455   p=cpr_get("trustsig_prompt.trust_regexp",_("Your selection? "));
456   trim_spaces(p);
457   cpr_kill_prompt();
458
459   if(strlen(p)>0)
460     {
461       char *q=p;
462       int regexplen=100,ind;
463
464       *regexp=xmalloc(regexplen);
465
466       /* Now mangle the domain the user entered into a regexp.  To do
467          this, \-escape everything that isn't alphanumeric, and attach
468          "<[^>]+[@.]" to the front, and ">$" to the end. */
469
470       strcpy(*regexp,"<[^>]+[@.]");
471       ind=strlen(*regexp);
472
473       while(*q)
474         {
475           if(!((*q>='A' && *q<='Z')
476                || (*q>='a' && *q<='z') || (*q>='0' && *q<='9')))
477             (*regexp)[ind++]='\\';
478
479           (*regexp)[ind++]=*q;
480
481           if((regexplen-ind)<3)
482             {
483               regexplen+=100;
484               *regexp=xrealloc(*regexp,regexplen);
485             }
486
487           q++;
488         }
489
490       (*regexp)[ind]='\0';
491       strcat(*regexp,">$");
492     }
493
494   xfree(p);
495   tty_printf("\n");
496 }
497
498 /****************
499  * Loop over all locusr and and sign the uids after asking.
500  * If no user id is marked, all user ids will be signed;
501  * if some user_ids are marked those will be signed.
502  */
503 static int
504 sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
505            int local, int nonrevocable, int trust, int interactive )
506 {
507     int rc = 0;
508     SK_LIST sk_list = NULL;
509     SK_LIST sk_rover = NULL;
510     PKT_secret_key *sk = NULL;
511     KBNODE node, uidnode;
512     PKT_public_key *primary_pk=NULL;
513     int select_all = !count_selected_uids(keyblock) || interactive;
514     int all_v3=1;
515
516     /* Are there any non-v3 sigs on this key already? */
517     if(PGP2)
518       for(node=keyblock;node;node=node->next)
519         if(node->pkt->pkttype==PKT_SIGNATURE &&
520            node->pkt->pkt.signature->version>3)
521           {
522             all_v3=0;
523             break;
524           }
525
526     /* build a list of all signators.
527      *    
528      * We use the CERT flag to request the primary which must always
529      * be one which is capable of signing keys.  I can't see a reason
530      * why to sign keys using a subkey.  Implementation of USAGE_CERT
531      * is just a hack in getkey.c and does not mean that a subkey
532      * marked as certification capable will be used */
533     rc=build_sk_list( locusr, &sk_list, 0, PUBKEY_USAGE_SIG|PUBKEY_USAGE_CERT);
534     if( rc )
535         goto leave;
536
537     /* loop over all signators */
538     for( sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next ) {
539         u32 sk_keyid[2],pk_keyid[2];
540         char *p,*trust_regexp=NULL;
541         int force_v4=0,class=0,selfsig=0;
542         u32 duration=0,timestamp=0;
543         byte trust_depth=0,trust_value=0;
544
545         if(local || nonrevocable || trust ||
546            opt.cert_policy_url || opt.cert_notation_data)
547           force_v4=1;
548
549         /* we have to use a copy of the sk, because make_keysig_packet
550          * may remove the protection from sk and if we did other
551          * changes to the secret key, we would save the unprotected
552          * version */
553         if( sk )
554             free_secret_key(sk);
555         sk = copy_secret_key( NULL, sk_rover->sk );
556         keyid_from_sk( sk, sk_keyid );
557         /* set mark A for all selected user ids */
558         for( node=keyblock; node; node = node->next ) {
559             if( select_all || (node->flag & NODFLG_SELUID) )
560                 node->flag |= NODFLG_MARK_A;
561             else
562                 node->flag &= ~NODFLG_MARK_A;
563         }
564         /* reset mark for uids which are already signed */
565         uidnode = NULL;
566         for( node=keyblock; node; node = node->next ) {
567             if( node->pkt->pkttype == PKT_PUBLIC_KEY ) {
568                 primary_pk=node->pkt->pkt.public_key;
569                 keyid_from_pk( primary_pk, pk_keyid );
570
571                 /* Is this a self-sig? */
572                 if(pk_keyid[0]==sk_keyid[0] && pk_keyid[1]==sk_keyid[1])
573                   {
574                     selfsig=1;
575                     /* Do not force a v4 sig here, otherwise it would
576                        be difficult to remake a v3 selfsig.  If this
577                        is a v3->v4 promotion case, then we set
578                        force_v4 later anyway. */
579                     force_v4=0;
580                   }
581             }
582             else if( node->pkt->pkttype == PKT_USER_ID )
583               {
584                 uidnode = (node->flag & NODFLG_MARK_A)? node : NULL;
585                 if(uidnode)
586                   {
587                     int yesreally=0;
588                     char *user=utf8_to_native(uidnode->pkt->pkt.user_id->name,
589                                               uidnode->pkt->pkt.user_id->len,
590                                               0);
591
592                     if(uidnode->pkt->pkt.user_id->is_revoked)
593                       {
594                         tty_printf(_("User ID \"%s\" is revoked."),user);
595
596                         if(selfsig)
597                           tty_printf("\n");
598                         else if(opt.expert)
599                           {
600                             tty_printf("\n");
601                             /* No, so remove the mark and continue */
602                             if(!cpr_get_answer_is_yes("sign_uid.revoke_okay",
603                                                       _("Are you sure you "
604                                                         "still want to sign "
605                                                         "it? (y/N) ")))
606                               {
607                                 uidnode->flag &= ~NODFLG_MARK_A;
608                                 uidnode=NULL;
609                               }
610                             else if(interactive)
611                               yesreally=1;
612                           }
613                         else
614                           {
615                             uidnode->flag &= ~NODFLG_MARK_A;
616                             uidnode=NULL;
617                             tty_printf(_("  Unable to sign.\n"));
618                           }
619                       }
620                     else if(uidnode->pkt->pkt.user_id->is_expired)
621                       {
622                         tty_printf(_("User ID \"%s\" is expired."),user);
623
624                         if(selfsig)
625                           tty_printf("\n");
626                         else if(opt.expert)
627                           {
628                             tty_printf("\n");
629                             /* No, so remove the mark and continue */
630                             if(!cpr_get_answer_is_yes("sign_uid.expire_okay",
631                                                       _("Are you sure you "
632                                                         "still want to sign "
633                                                         "it? (y/N) ")))
634                               {
635                                 uidnode->flag &= ~NODFLG_MARK_A;
636                                 uidnode=NULL;
637                               }
638                             else if(interactive)
639                               yesreally=1;
640                           }
641                         else
642                           {
643                             uidnode->flag &= ~NODFLG_MARK_A;
644                             uidnode=NULL;
645                             tty_printf(_("  Unable to sign.\n"));
646                           }
647                       }
648                     else if(!uidnode->pkt->pkt.user_id->created && !selfsig)
649                       {
650                         tty_printf(_("User ID \"%s\" is not self-signed."),
651                                    user);
652
653                         if(opt.expert)
654                           {
655                             tty_printf("\n");
656                             /* No, so remove the mark and continue */
657                             if(!cpr_get_answer_is_yes("sign_uid.nosig_okay",
658                                                       _("Are you sure you "
659                                                         "still want to sign "
660                                                         "it? (y/N) ")))
661                               {
662                                 uidnode->flag &= ~NODFLG_MARK_A;
663                                 uidnode=NULL;
664                               }
665                             else if(interactive)
666                               yesreally=1;
667                           }
668                         else
669                           {
670                             uidnode->flag &= ~NODFLG_MARK_A;
671                             uidnode=NULL;
672                             tty_printf(_("  Unable to sign.\n"));
673                           }
674                       }
675
676                     if(uidnode && interactive && !yesreally)
677                       {
678                         tty_printf(_("User ID \"%s\" is signable.  "),user);
679                         if(!cpr_get_answer_is_yes("sign_uid.sign_okay",
680                                                   _("Sign it? (y/N) ")))
681                           {
682                             uidnode->flag &= ~NODFLG_MARK_A;
683                             uidnode=NULL;
684                           }
685                       }
686
687                     xfree(user);
688                   }
689               }
690             else if( uidnode && node->pkt->pkttype == PKT_SIGNATURE
691                 && (node->pkt->pkt.signature->sig_class&~3) == 0x10 ) {
692                 if( sk_keyid[0] == node->pkt->pkt.signature->keyid[0]
693                     && sk_keyid[1] == node->pkt->pkt.signature->keyid[1] ) {
694                     char buf[50];
695                     char *user=utf8_to_native(uidnode->pkt->pkt.user_id->name,
696                                               uidnode->pkt->pkt.user_id->len,
697                                               0);
698
699                     /* It's a v3 self-sig.  Make it into a v4 self-sig? */
700                     if(node->pkt->pkt.signature->version<4 && selfsig)
701                       {
702                         tty_printf(_("The self-signature on \"%s\"\n"
703                                      "is a PGP 2.x-style signature.\n"),user);
704  
705                         /* Note that the regular PGP2 warning below
706                            still applies if there are no v4 sigs on
707                            this key at all. */
708
709                         if(opt.expert)
710                           if(cpr_get_answer_is_yes("sign_uid.v4_promote_okay",
711                                                    _("Do you want to promote "
712                                                      "it to an OpenPGP self-"
713                                                      "signature? (y/N) ")))
714                             {
715                               force_v4=1;
716                               node->flag|=NODFLG_DELSIG;
717                               xfree(user);
718                               continue;
719                             }
720                       }
721
722                     /* Is the current signature expired? */
723                     if(node->pkt->pkt.signature->flags.expired)
724                       {
725                         tty_printf(_("Your current signature on \"%s\"\n"
726                                      "has expired.\n"),user);
727
728                         if(cpr_get_answer_is_yes("sign_uid.replace_expired_okay",
729                                                  _("Do you want to issue a "
730                                                    "new signature to replace "
731                                                    "the expired one? (y/N) ")))
732                           {
733                             /* Mark these for later deletion.  We
734                                don't want to delete them here, just in
735                                case the replacement signature doesn't
736                                happen for some reason.  We only delete
737                                these after the replacement is already
738                                in place. */
739
740                             node->flag|=NODFLG_DELSIG;
741                             xfree(user);
742                             continue;
743                           }
744                       }
745
746                     if(!node->pkt->pkt.signature->flags.exportable && !local)
747                       {
748                         /* It's a local sig, and we want to make a
749                            exportable sig. */
750                         tty_printf(_("Your current signature on \"%s\"\n"
751                                      "is a local signature.\n"),user);
752
753                         if(cpr_get_answer_is_yes("sign_uid.local_promote_okay",
754                                                  _("Do you want to promote "
755                                                    "it to a full exportable "
756                                                    "signature? (y/N) ")))
757                           {
758                             /* Mark these for later deletion.  We
759                                don't want to delete them here, just in
760                                case the replacement signature doesn't
761                                happen for some reason.  We only delete
762                                these after the replacement is already
763                                in place. */
764
765                             node->flag|=NODFLG_DELSIG;
766                             xfree(user);
767                             continue;
768                           }
769                       }
770
771                     /* Fixme: see whether there is a revocation in which
772                      * case we should allow to sign it again. */
773                     if (!node->pkt->pkt.signature->flags.exportable && local)
774                       tty_printf(_(
775                               "\"%s\" was already locally signed by key %s\n"),
776                                  user,keystr_from_sk(sk));
777                     else
778                       tty_printf(_("\"%s\" was already signed by key %s\n"),
779                                  user,keystr_from_sk(sk));
780
781                     if(opt.expert
782                        && cpr_get_answer_is_yes("sign_uid.dupe_okay",
783                                                 _("Do you want to sign it "
784                                                   "again anyway? (y/N) ")))
785                       {
786                         /* Don't delete the old sig here since this is
787                            an --expert thing. */
788                         xfree(user);
789                         continue;
790                       }
791
792                     sprintf (buf, "%08lX%08lX",
793                              (ulong)sk->keyid[0], (ulong)sk->keyid[1] );
794                     write_status_text (STATUS_ALREADY_SIGNED, buf);
795                     uidnode->flag &= ~NODFLG_MARK_A; /* remove mark */
796
797                     xfree(user);
798                 }
799             }
800         }
801
802         /* check whether any uids are left for signing */
803         if( !count_uids_with_flag(keyblock, NODFLG_MARK_A) )
804           {
805             tty_printf(_("Nothing to sign with key %s\n"),keystr_from_sk(sk));
806             continue;
807           }
808
809         /* Ask whether we really should sign these user id(s) */
810         tty_printf("\n");
811         show_key_with_all_names( keyblock, 1, 0, 1, 0, 0 );
812         tty_printf("\n");
813
814         if(primary_pk->expiredate && !selfsig)
815           {
816             u32 now=make_timestamp();
817
818             if(primary_pk->expiredate<=now)
819               {
820                 tty_printf(_("This key has expired!"));
821
822                 if(opt.expert)
823                   {
824                     tty_printf("  ");
825                     if(!cpr_get_answer_is_yes("sign_uid.expired_okay",
826                                               _("Are you sure you still "
827                                                 "want to sign it? (y/N) ")))
828                       continue;
829                   }
830                 else
831                   {
832                     tty_printf(_("  Unable to sign.\n"));
833                     continue;
834                   }
835               }
836             else
837               {
838                 tty_printf(_("This key is due to expire on %s.\n"),
839                            expirestr_from_pk(primary_pk));
840
841                 if(opt.ask_cert_expire)
842                   {
843                     char *answer=cpr_get("sign_uid.expire",
844                                          _("Do you want your signature to "
845                                            "expire at the same time? (Y/n) "));
846                     if(answer_is_yes_no_default(answer,1))
847                       {
848                         /* This fixes the signature timestamp we're
849                            going to make as now.  This is so the
850                            expiration date is exactly correct, and not
851                            a few seconds off (due to the time it takes
852                            to answer the questions, enter the
853                            passphrase, etc). */
854                         timestamp=now;
855                         duration=primary_pk->expiredate-now;
856                         force_v4=1;
857                       }
858
859                     cpr_kill_prompt();
860                     xfree(answer);
861                   }
862               }
863           }
864
865         /* Only ask for duration if we haven't already set it to match
866            the expiration of the pk */
867         if(!duration && !selfsig)
868           {
869             if(opt.ask_cert_expire)
870               duration=ask_expire_interval(1,opt.def_cert_expire);
871             else
872               duration=parse_expire_string(opt.def_cert_expire);
873           }
874
875         if(duration)
876           force_v4=1;
877
878         /* Is --pgp2 on, it's a v3 key, all the sigs on the key are
879            currently v3 and we're about to sign it with a v4 sig?  If
880            so, danger! */
881         if(PGP2 && all_v3 &&
882            (sk->version>3 || force_v4) && primary_pk->version<=3)
883           {
884             tty_printf(_("You may not make an OpenPGP signature on a "
885                          "PGP 2.x key while in --pgp2 mode.\n"));
886             tty_printf(_("This would make the key unusable in PGP 2.x.\n"));
887
888             if(opt.expert)
889               {
890                 if(!cpr_get_answer_is_yes("sign_uid.v4_on_v3_okay",
891                                           _("Are you sure you still "
892                                             "want to sign it? (y/N) ")))
893                   continue;
894
895                 all_v3=0;
896               }
897             else
898               continue;
899           }
900
901         if(selfsig)
902           ;
903         else
904           {
905             if(opt.batch || !opt.ask_cert_level)
906               class=0x10+opt.def_cert_level;
907             else
908               {
909                 char *answer;
910
911                 tty_printf(_("How carefully have you verified the key you are "
912                              "about to sign actually belongs\nto the person "
913                              "named above?  If you don't know what to "
914                              "answer, enter \"0\".\n"));
915                 tty_printf("\n");
916                 tty_printf(_("   (0) I will not answer.%s\n"),
917                            opt.def_cert_level==0?" (default)":"");
918                 tty_printf(_("   (1) I have not checked at all.%s\n"),
919                            opt.def_cert_level==1?" (default)":"");
920                 tty_printf(_("   (2) I have done casual checking.%s\n"),
921                            opt.def_cert_level==2?" (default)":"");
922                 tty_printf(_("   (3) I have done very careful checking.%s\n"),
923                            opt.def_cert_level==3?" (default)":"");
924                 tty_printf("\n");
925
926                 while(class==0)
927                   {
928                     answer = cpr_get("sign_uid.class",_("Your selection? "
929                                         "(enter `?' for more information): "));
930                     if(answer[0]=='\0')
931                       class=0x10+opt.def_cert_level; /* Default */
932                     else if(ascii_strcasecmp(answer,"0")==0)
933                       class=0x10; /* Generic */
934                     else if(ascii_strcasecmp(answer,"1")==0)
935                       class=0x11; /* Persona */
936                     else if(ascii_strcasecmp(answer,"2")==0)
937                       class=0x12; /* Casual */
938                     else if(ascii_strcasecmp(answer,"3")==0)
939                       class=0x13; /* Positive */
940                     else
941                       tty_printf(_("Invalid selection.\n"));
942
943                     xfree(answer);
944                   }
945               }
946
947             if(trust)
948               trustsig_prompt(&trust_value,&trust_depth,&trust_regexp);
949           }
950
951         p=get_user_id_native(sk_keyid);
952         tty_printf(_("Are you sure that you want to sign this key with your\n"
953                      "key \"%s\" (%s)\n"),p,keystr_from_sk(sk));
954         xfree(p);
955
956         if(selfsig)
957           {
958             tty_printf("\n");
959             tty_printf(_("This will be a self-signature.\n"));
960
961             if( local )
962               {
963                 tty_printf("\n");
964                 tty_printf(
965                          _("WARNING: the signature will not be marked "
966                            "as non-exportable.\n"));
967               }
968
969             if( nonrevocable )
970               {
971                 tty_printf("\n");
972                 tty_printf(
973                          _("WARNING: the signature will not be marked "
974                            "as non-revocable.\n"));
975               }
976           }
977         else
978           {
979             if( local )
980               {
981                 tty_printf("\n");
982                 tty_printf(
983                      _("The signature will be marked as non-exportable.\n"));
984               }
985
986             if( nonrevocable )
987               {
988                 tty_printf("\n");
989                 tty_printf(
990                       _("The signature will be marked as non-revocable.\n"));
991               }
992
993             switch(class)
994               {
995               case 0x11:
996                 tty_printf("\n");
997                 tty_printf(_("I have not checked this key at all.\n"));
998                 break;
999
1000               case 0x12:
1001                 tty_printf("\n");
1002                 tty_printf(_("I have checked this key casually.\n"));
1003                 break;
1004
1005               case 0x13:
1006                 tty_printf("\n");
1007                 tty_printf(_("I have checked this key very carefully.\n"));
1008                 break;
1009               }
1010           }
1011
1012         tty_printf("\n");
1013
1014         if( opt.batch && opt.answer_yes )
1015           ;
1016         else if( !cpr_get_answer_is_yes("sign_uid.okay",
1017                                         _("Really sign? (y/N) ")) )
1018             continue;
1019
1020         /* now we can sign the user ids */
1021       reloop: /* (must use this, because we are modifing the list) */
1022         primary_pk = NULL;
1023         for( node=keyblock; node; node = node->next ) {
1024             if( node->pkt->pkttype == PKT_PUBLIC_KEY )
1025                 primary_pk = node->pkt->pkt.public_key;
1026             else if( node->pkt->pkttype == PKT_USER_ID
1027                      && (node->flag & NODFLG_MARK_A) ) {
1028                 PACKET *pkt;
1029                 PKT_signature *sig;
1030                 struct sign_attrib attrib;
1031
1032                 assert( primary_pk );
1033                 memset( &attrib, 0, sizeof attrib );
1034                 attrib.non_exportable = local;
1035                 attrib.non_revocable = nonrevocable;
1036                 attrib.trust_depth = trust_depth;
1037                 attrib.trust_value = trust_value;
1038                 attrib.trust_regexp = trust_regexp;
1039                 node->flag &= ~NODFLG_MARK_A;
1040
1041                 /* we force creation of a v4 signature for local
1042                  * signatures, otherwise we would not generate the
1043                  * subpacket with v3 keys and the signature becomes
1044                  * exportable */
1045
1046                 if(selfsig)
1047                   rc = make_keysig_packet( &sig, primary_pk,
1048                                            node->pkt->pkt.user_id,
1049                                            NULL,
1050                                            sk,
1051                                            0x13, 0, force_v4?4:0, 0, 0,
1052                                            keygen_add_std_prefs, primary_pk);
1053                 else
1054                   rc = make_keysig_packet( &sig, primary_pk,
1055                                            node->pkt->pkt.user_id,
1056                                            NULL,
1057                                            sk,
1058                                            class, 0, force_v4?4:0,
1059                                            timestamp, duration,
1060                                            sign_mk_attrib, &attrib );
1061                 if( rc ) {
1062                     log_error(_("signing failed: %s\n"), g10_errstr(rc));
1063                     goto leave;
1064                 }
1065
1066                 *ret_modified = 1; /* we changed the keyblock */
1067                 update_trust = 1;
1068
1069                 pkt = xmalloc_clear( sizeof *pkt );
1070                 pkt->pkttype = PKT_SIGNATURE;
1071                 pkt->pkt.signature = sig;
1072                 insert_kbnode( node, new_kbnode(pkt), PKT_SIGNATURE );
1073                 goto reloop;
1074             }
1075         }
1076
1077         /* Delete any sigs that got promoted */
1078         for( node=keyblock; node; node = node->next )
1079           if( node->flag & NODFLG_DELSIG)
1080             delete_kbnode(node);
1081     } /* end loop over signators */
1082
1083   leave:
1084     release_sk_list( sk_list );
1085     if( sk )
1086         free_secret_key(sk);
1087     return rc;
1088 }
1089
1090
1091
1092 /****************
1093  * Change the passphrase of the primary and all secondary keys.
1094  * We use only one passphrase for all keys.
1095  */
1096 static int
1097 change_passphrase( KBNODE keyblock )
1098 {
1099     int rc = 0;
1100     int changed=0;
1101     KBNODE node;
1102     PKT_secret_key *sk;
1103     char *passphrase = NULL;
1104     int no_primary_secrets = 0;
1105     int any;
1106
1107     node = find_kbnode( keyblock, PKT_SECRET_KEY );
1108     if( !node ) {
1109         log_error("Oops; secret key not found anymore!\n");
1110         goto leave;
1111     }
1112     sk = node->pkt->pkt.secret_key;
1113
1114     for (any = 0, node=keyblock; node; node = node->next) {
1115         if (node->pkt->pkttype == PKT_SECRET_KEY 
1116             || node->pkt->pkttype == PKT_SECRET_SUBKEY) {
1117             PKT_secret_key *tmpsk = node->pkt->pkt.secret_key;
1118             if (!(tmpsk->is_protected
1119                   && (tmpsk->protect.s2k.mode == 1001 
1120                       || tmpsk->protect.s2k.mode == 1002))) {
1121                 any = 1;
1122                 break;
1123             }
1124         }
1125     }
1126     if (!any) {
1127         tty_printf (_("Key has only stub or on-card key items - "
1128                       "no passphrase to change.\n"));
1129         goto leave;
1130     }
1131         
1132     /* See how to handle this key.  */
1133     switch( is_secret_key_protected( sk ) ) {
1134       case -1:
1135         rc = G10ERR_PUBKEY_ALGO;
1136         break;
1137       case 0:
1138         tty_printf(_("This key is not protected.\n"));
1139         break;
1140       default:
1141         if( sk->protect.s2k.mode == 1001 ) {
1142             tty_printf(_("Secret parts of primary key are not available.\n"));
1143             no_primary_secrets = 1;
1144         }
1145         else if( sk->protect.s2k.mode == 1002 ) {
1146             tty_printf(_("Secret parts of primary key are stored on-card.\n"));
1147             no_primary_secrets = 1;
1148         }
1149         else {
1150             tty_printf(_("Key is protected.\n"));
1151             rc = check_secret_key( sk, 0 );
1152             if( !rc )
1153                 passphrase = get_last_passphrase();
1154         }
1155         break;
1156     }
1157
1158     /* Unprotect all subkeys (use the supplied passphrase or ask)*/
1159     for(node=keyblock; !rc && node; node = node->next ) {
1160         if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
1161             PKT_secret_key *subsk = node->pkt->pkt.secret_key;
1162             if ( !(subsk->is_protected
1163                    && (subsk->protect.s2k.mode == 1001 
1164                        || subsk->protect.s2k.mode == 1002))) {
1165                 set_next_passphrase( passphrase );
1166                 rc = check_secret_key( subsk, 0 );
1167                 if( !rc && !passphrase )
1168                     passphrase = get_last_passphrase();
1169             }
1170         }
1171     }
1172
1173     if( rc )
1174         tty_printf(_("Can't edit this key: %s\n"), g10_errstr(rc));
1175     else {
1176         DEK *dek = NULL;
1177         STRING2KEY *s2k = xmalloc_secure( sizeof *s2k );
1178         const char *errtext = NULL;
1179
1180         tty_printf(_("Enter the new passphrase for this secret key.\n\n") );
1181
1182         set_next_passphrase( NULL );
1183         for(;;) {
1184             s2k->mode = opt.s2k_mode;
1185             s2k->hash_algo = S2K_DIGEST_ALGO;
1186             dek = passphrase_to_dek( NULL, 0, opt.s2k_cipher_algo,
1187                                      s2k, 2, errtext, NULL);
1188             if( !dek ) {
1189                 errtext = N_("passphrase not correctly repeated; try again");
1190                 tty_printf ("%s.\n", _(errtext));
1191             }
1192             else if( !dek->keylen ) {
1193                 rc = 0;
1194                 tty_printf(_( "You don't want a passphrase -"
1195                             " this is probably a *bad* idea!\n\n"));
1196                 if( cpr_get_answer_is_yes("change_passwd.empty.okay",
1197                                _("Do you really want to do this? (y/N) ")))
1198                   {
1199                     changed++;
1200                     break;
1201                   }
1202             }
1203             else { /* okay */
1204                 rc = 0;
1205                 if( !no_primary_secrets ) {
1206                     sk->protect.algo = dek->algo;
1207                     sk->protect.s2k = *s2k;
1208                     rc = protect_secret_key( sk, dek );
1209                 }
1210                 for(node=keyblock; !rc && node; node = node->next ) {
1211                     if( node->pkt->pkttype == PKT_SECRET_SUBKEY ) {
1212                         PKT_secret_key *subsk = node->pkt->pkt.secret_key;
1213                         if ( !(subsk->is_protected
1214                                && (subsk->protect.s2k.mode == 1001 
1215                                    || subsk->protect.s2k.mode == 1002))) {
1216                             subsk->protect.algo = dek->algo;
1217                             subsk->protect.s2k = *s2k;
1218                             rc = protect_secret_key( subsk, dek );
1219                         }
1220                     }
1221                 }
1222                 if( rc )
1223                     log_error("protect_secret_key failed: %s\n",
1224                               g10_errstr(rc) );
1225                 else
1226                     changed++;
1227                 break;
1228             }
1229         }
1230         xfree(s2k);
1231         xfree(dek);
1232     }
1233
1234   leave:
1235     xfree( passphrase );
1236     set_next_passphrase( NULL );
1237     return changed && !rc;
1238 }
1239
1240
1241 /****************
1242  * There are some keys out (due to a bug in gnupg), where the sequence
1243  * of the packets is wrong.  This function fixes that.
1244  * Returns: true if the keyblock has been fixed.
1245  *
1246  * Note:  This function does not work if there is more than one user ID.
1247  */
1248 static int
1249 fix_keyblock( KBNODE keyblock )
1250 {
1251     KBNODE node, last, subkey;
1252     int fixed=0;
1253
1254     /* locate key signatures of class 0x10..0x13 behind sub key packets */
1255     for( subkey=last=NULL, node = keyblock; node;
1256                                             last=node, node = node->next ) {
1257         switch( node->pkt->pkttype ) {
1258           case PKT_PUBLIC_SUBKEY:
1259           case PKT_SECRET_SUBKEY:
1260             if( !subkey )
1261                 subkey = last; /* actually it is the one before the subkey */
1262             break;
1263           case PKT_SIGNATURE:
1264             if( subkey ) {
1265                 PKT_signature *sig = node->pkt->pkt.signature;
1266                 if( sig->sig_class >= 0x10 && sig->sig_class <= 0x13 ) {
1267                     log_info(_(
1268                         "moving a key signature to the correct place\n"));
1269                     last->next = node->next;
1270                     node->next = subkey->next;
1271                     subkey->next = node;
1272                     node = last;
1273                     fixed=1;
1274                 }
1275             }
1276             break;
1277           default: break;
1278         }
1279     }
1280
1281     return fixed;
1282 }
1283
1284 static int
1285 parse_sign_type(const char *str,int *localsig,int *nonrevokesig,int *trustsig)
1286 {
1287   const char *p=str;
1288
1289   while(*p)
1290     {
1291       if(ascii_strncasecmp(p,"l",1)==0)
1292         {
1293           *localsig=1;
1294           p++;
1295         }
1296       else if(ascii_strncasecmp(p,"nr",2)==0)
1297         {
1298           *nonrevokesig=1;
1299           p+=2;
1300         }
1301       else if(ascii_strncasecmp(p,"t",1)==0)
1302         {
1303           *trustsig=1;
1304           p++;
1305         }
1306       else
1307         return 0;
1308     }
1309
1310   return 1;
1311 }
1312
1313 \f
1314 /****************
1315  * Menu driven key editor.  If seckey_check is true, then a secret key
1316  * that matches username will be looked for.  If it is false, not all
1317  * commands will be available.
1318  *
1319  * Note: to keep track of some selection we use node->mark MARKBIT_xxxx.
1320  */
1321
1322 /* Need an SK for this command */
1323 #define KEYEDIT_NEED_SK 1
1324 /* Cannot be viewing the SK for this command */
1325 #define KEYEDIT_NOT_SK  2
1326 /* Must be viewing the SK for this command */
1327 #define KEYEDIT_ONLY_SK 4
1328 /* Match the tail of the string */
1329 #define KEYEDIT_TAIL_MATCH 8
1330
1331 enum cmdids
1332   {
1333     cmdNONE = 0,
1334     cmdQUIT, cmdHELP, cmdFPR, cmdLIST, cmdSELUID, cmdCHECK, cmdSIGN,
1335     cmdREVSIG, cmdREVKEY, cmdREVUID, cmdDELSIG, cmdPRIMARY, cmdDEBUG,
1336     cmdSAVE, cmdADDUID, cmdADDPHOTO, cmdDELUID, cmdADDKEY, cmdDELKEY,
1337     cmdADDREVOKER, cmdTOGGLE, cmdSELKEY, cmdPASSWD, cmdTRUST, cmdPREF,
1338     cmdEXPIRE, cmdENABLEKEY, cmdDISABLEKEY, cmdSHOWPREF, cmdSETPREF,
1339     cmdPREFKS, cmdINVCMD, cmdSHOWPHOTO, cmdUPDTRUST, cmdCHKTRUST,
1340     cmdADDCARDKEY, cmdKEYTOCARD, cmdBKUPTOCARD, cmdCLEAN, cmdNOP
1341   };
1342
1343 static struct
1344 {
1345   const char *name;
1346   enum cmdids id;
1347   int flags;
1348   const char *desc;
1349 } cmds[] =
1350   { 
1351     { "quit"    , cmdQUIT      , 0, N_("quit this menu") },
1352     { "q"       , cmdQUIT      , 0, NULL   },
1353     { "save"    , cmdSAVE      , 0, N_("save and quit") },
1354     { "help"    , cmdHELP      , 0, N_("show this help") },
1355     { "?"       , cmdHELP      , 0, NULL   },
1356     { "fpr"     , cmdFPR       , 0, N_("show key fingerprint") },
1357     { "list"    , cmdLIST      , 0, N_("list key and user IDs") },
1358     { "l"       , cmdLIST      , 0, NULL   },
1359     { "uid"     , cmdSELUID    , 0, N_("select user ID N") },
1360     { "key"     , cmdSELKEY    , 0, N_("select subkey N") },
1361     { "check"   , cmdCHECK     , 0, N_("check signatures") },
1362     { "c"       , cmdCHECK     , 0, NULL },
1363     { "sign"    , cmdSIGN      , KEYEDIT_NOT_SK|KEYEDIT_TAIL_MATCH,
1364       N_("sign selected user IDs [* see below for related commands]") },
1365     { "s"       , cmdSIGN      , KEYEDIT_NOT_SK, NULL },
1366     /* "lsign" and friends will never match since "sign" comes first
1367        and it is a tail match.  They are just here so they show up in
1368        the help menu. */
1369     { "lsign"   , cmdNOP       , 0, N_("sign selected user IDs locally") },
1370     { "tsign"   , cmdNOP       , 0,
1371       N_("sign selected user IDs with a trust signature") },
1372     { "nrsign"  , cmdNOP       , 0,
1373       N_("sign selected user IDs with a non-revocable signature") },
1374
1375     { "debug"   , cmdDEBUG     , 0, NULL },
1376     { "adduid"  , cmdADDUID    , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1377       N_("add a user ID") },
1378     { "addphoto", cmdADDPHOTO  , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1379       N_("add a photo ID") },
1380     { "deluid"  , cmdDELUID    , KEYEDIT_NOT_SK,
1381       N_("delete selected user IDs") },
1382     /* delphoto is really deluid in disguise */
1383     { "delphoto", cmdDELUID    , KEYEDIT_NOT_SK, NULL },
1384
1385     { "addkey"  , cmdADDKEY    , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1386       N_("add a subkey") },
1387
1388 #ifdef ENABLE_CARD_SUPPORT
1389     { "addcardkey", cmdADDCARDKEY , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1390       N_("add a key to a smartcard") },
1391     { "keytocard", cmdKEYTOCARD , KEYEDIT_NEED_SK|KEYEDIT_ONLY_SK, 
1392       N_("move a key to a smartcard")},
1393     { "bkuptocard", cmdBKUPTOCARD , KEYEDIT_NEED_SK|KEYEDIT_ONLY_SK, 
1394       N_("move a backup key to a smartcard")},
1395 #endif /*ENABLE_CARD_SUPPORT*/
1396
1397     { "delkey"  , cmdDELKEY    , KEYEDIT_NOT_SK,
1398       N_("delete selected subkeys") },
1399     { "addrevoker",cmdADDREVOKER,KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1400       N_("add a revocation key") },
1401     { "delsig"  , cmdDELSIG    , KEYEDIT_NOT_SK,
1402       N_("delete signatures from the selected user IDs") },
1403     { "expire"  , cmdEXPIRE    , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1404       N_("change the expiration date for the key or selected subkeys") },
1405     { "primary" , cmdPRIMARY   , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1406       N_("flag the selected user ID as primary")},
1407     { "toggle"  , cmdTOGGLE    , KEYEDIT_NEED_SK,
1408       N_("toggle between the secret and public key listings") },
1409     { "t"       , cmdTOGGLE    , KEYEDIT_NEED_SK, NULL },
1410     { "pref"    , cmdPREF      , KEYEDIT_NOT_SK,
1411       N_("list preferences (expert)")},
1412     { "showpref", cmdSHOWPREF  , KEYEDIT_NOT_SK,
1413       N_("list preferences (verbose)") },
1414     { "setpref" , cmdSETPREF   , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1415       N_("set preference list for the selected user IDs") },
1416     /* Alias */
1417     { "updpref" , cmdSETPREF   , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK, NULL },
1418
1419     { "keyserver",cmdPREFKS    , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1420       N_("set preferred keyserver URL for the selected user IDs")},
1421     { "passwd"  , cmdPASSWD    , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1422       N_("change the passphrase") },
1423     /* Alias */
1424     { "password", cmdPASSWD    , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK, NULL },
1425
1426     { "trust"   , cmdTRUST     , KEYEDIT_NOT_SK, N_("change the ownertrust") },
1427     { "revsig"  , cmdREVSIG    , KEYEDIT_NOT_SK,
1428       N_("revoke signatures on the selected user IDs") },
1429     { "revuid"  , cmdREVUID    , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1430       N_("revoke selected user IDs") },
1431     /* Alias */
1432     { "revphoto", cmdREVUID    , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK, NULL },
1433
1434     { "revkey"  , cmdREVKEY    , KEYEDIT_NOT_SK|KEYEDIT_NEED_SK,
1435       N_("revoke key or selected subkeys") },
1436     { "enable"  , cmdENABLEKEY , KEYEDIT_NOT_SK, N_("enable key") },
1437     { "disable" , cmdDISABLEKEY, KEYEDIT_NOT_SK, N_("disable key") },
1438     { "showphoto",cmdSHOWPHOTO , 0, N_("show selected photo IDs") },
1439     { "clean",    cmdCLEAN     , KEYEDIT_NOT_SK,
1440       N_("clean unusable parts from key") },
1441     { NULL, cmdNONE, 0, NULL }
1442   };
1443
1444
1445 #ifdef HAVE_LIBREADLINE
1446
1447 /* These two functions are used by readline for command completion. */
1448
1449 static char *
1450 command_generator(const char *text,int state)
1451 {
1452   static int list_index,len;
1453   const char *name;
1454
1455   /* If this is a new word to complete, initialize now.  This includes
1456      saving the length of TEXT for efficiency, and initializing the
1457      index variable to 0. */
1458   if(!state)
1459     {
1460       list_index=0;
1461       len=strlen(text);
1462     }
1463
1464   /* Return the next partial match */
1465   while((name=cmds[list_index].name))
1466     {
1467       /* Only complete commands that have help text */
1468       if(cmds[list_index++].desc && strncmp(name,text,len)==0)
1469         return strdup(name);
1470     }
1471
1472   return NULL;
1473 }
1474
1475 static char **
1476 keyedit_completion(const char *text, int start, int end)
1477 {
1478   /* If we are at the start of a line, we try and command-complete.
1479      If not, just do nothing for now. */
1480
1481   if(start==0)
1482     return rl_completion_matches(text,command_generator);
1483
1484   rl_attempted_completion_over=1;
1485
1486   return NULL;
1487 }
1488 #endif /* HAVE_LIBREADLINE */
1489
1490
1491 void
1492 keyedit_menu( const char *username, STRLIST locusr,
1493               STRLIST commands, int quiet, int seckey_check )
1494 {
1495     enum cmdids cmd = 0;
1496     int rc = 0;
1497     KBNODE keyblock = NULL;
1498     KEYDB_HANDLE kdbhd = NULL;
1499     KBNODE sec_keyblock = NULL;
1500     KEYDB_HANDLE sec_kdbhd = NULL;
1501     KBNODE cur_keyblock;
1502     char *answer = NULL;
1503     int redisplay = 1;
1504     int modified = 0;
1505     int sec_modified = 0;
1506     int toggle;
1507     int have_commands = !!commands;
1508
1509     if ( opt.command_fd != -1 )
1510         ;
1511     else if( opt.batch && !have_commands )
1512       {
1513         log_error(_("can't do this in batch mode\n"));
1514         goto leave;
1515       }
1516
1517 #ifdef HAVE_W32_SYSTEM
1518     /* Due to Windows peculiarities we need to make sure that the
1519        trustdb stale check is done before we open another file
1520        (i.e. by searching for a key).  In theory we could make sure
1521        that the files are closed after use but the open/close caches
1522        inhibits that and flushing the cache right before the stale
1523        check is not easy to implement.  Thus we take the easy way out
1524        and run the stale check as early as possible.  Note, that for
1525        non- W32 platforms it is run indirectly trough a call to
1526        get_validity ().  */
1527     check_trustdb_stale ();
1528 #endif
1529
1530     /* Get the public key */
1531     rc = get_pubkey_byname (NULL, username, &keyblock, &kdbhd, 1);
1532     if( rc )
1533         goto leave;
1534     if( fix_keyblock( keyblock ) )
1535         modified++;
1536     if( collapse_uids( &keyblock ) )
1537         modified++;
1538     reorder_keyblock(keyblock);
1539
1540     if(seckey_check)
1541       {/* see whether we have a matching secret key */
1542         PKT_public_key *pk = keyblock->pkt->pkt.public_key;
1543
1544         sec_kdbhd = keydb_new (1);
1545         {
1546             byte afp[MAX_FINGERPRINT_LEN];
1547             size_t an;
1548
1549             fingerprint_from_pk (pk, afp, &an);
1550             while (an < MAX_FINGERPRINT_LEN) 
1551                 afp[an++] = 0;
1552             rc = keydb_search_fpr (sec_kdbhd, afp);
1553         }
1554         if (!rc)
1555           {
1556             rc = keydb_get_keyblock (sec_kdbhd, &sec_keyblock);
1557             if (rc)
1558               {
1559                 log_error (_("error reading secret keyblock \"%s\": %s\n"),
1560                            username, g10_errstr(rc));
1561               }
1562             else
1563               {
1564                 merge_keys_and_selfsig( sec_keyblock );
1565                 if( fix_keyblock( sec_keyblock ) )
1566                   sec_modified++;
1567               }
1568           }
1569
1570         if (rc) {
1571             sec_keyblock = NULL;
1572             keydb_release (sec_kdbhd); sec_kdbhd = NULL;
1573             rc = 0;
1574         }
1575
1576         if( sec_keyblock && !quiet )
1577           tty_printf(_("Secret key is available.\n"));
1578     }
1579
1580     toggle = 0;
1581     cur_keyblock = keyblock;
1582     for(;;) { /* main loop */
1583         int i, arg_number, photo;
1584         const char *arg_string = "";
1585         char *p;
1586         PKT_public_key *pk=keyblock->pkt->pkt.public_key;
1587
1588         tty_printf("\n");
1589
1590         if( redisplay && !quiet )
1591           {
1592             show_key_with_all_names( cur_keyblock, 0, 1, 0, 1, 0 );
1593             tty_printf("\n");
1594             redisplay = 0;
1595           }
1596         do {
1597             xfree(answer);
1598             if( have_commands ) {
1599                 if( commands ) {
1600                     answer = xstrdup( commands->d );
1601                     commands = commands->next;
1602                 }
1603                 else if( opt.batch ) {
1604                     answer = xstrdup("quit");
1605                 }
1606                 else
1607                     have_commands = 0;
1608             }
1609             if( !have_commands )
1610               {
1611                 tty_enable_completion(keyedit_completion);
1612                 answer = cpr_get_no_help("keyedit.prompt", _("Command> "));
1613                 cpr_kill_prompt();
1614                 tty_disable_completion();
1615               }
1616             trim_spaces(answer);
1617         } while( *answer == '#' );
1618
1619         arg_number = 0; /* Yes, here is the init which egcc complains about */
1620         photo = 0; /* This too */
1621         if( !*answer )
1622             cmd = cmdLIST;
1623         else if( *answer == CONTROL_D )
1624             cmd = cmdQUIT;
1625         else if( digitp(answer ) ) {
1626             cmd = cmdSELUID;
1627             arg_number = atoi(answer);
1628         }
1629         else {
1630             if( (p=strchr(answer,' ')) ) {
1631                 *p++ = 0;
1632                 trim_spaces(answer);
1633                 trim_spaces(p);
1634                 arg_number = atoi(p);
1635                 arg_string = p;
1636             }
1637
1638             for(i=0; cmds[i].name; i++ )
1639               {
1640                 if(cmds[i].flags & KEYEDIT_TAIL_MATCH)
1641                   {
1642                     size_t l=strlen(cmds[i].name);
1643                     size_t a=strlen(answer);
1644                     if(a>=l)
1645                       {
1646                         if(ascii_strcasecmp(&answer[a-l],cmds[i].name)==0)
1647                           {
1648                             answer[a-l]='\0';
1649                             break;
1650                           }
1651                       }
1652                   }
1653                 else if( !ascii_strcasecmp( answer, cmds[i].name ) )
1654                   break;
1655               }
1656             if((cmds[i].flags & KEYEDIT_NEED_SK) && !sec_keyblock )
1657               {
1658                 tty_printf(_("Need the secret key to do this.\n"));
1659                 cmd = cmdNOP;
1660               }
1661             else if(((cmds[i].flags & KEYEDIT_NOT_SK) && sec_keyblock
1662                      && toggle)
1663                     ||((cmds[i].flags & KEYEDIT_ONLY_SK) && sec_keyblock
1664                        && !toggle))
1665               {
1666                 tty_printf(_("Please use the command \"toggle\" first.\n"));
1667                 cmd = cmdNOP;
1668               }
1669             else
1670               cmd = cmds[i].id;
1671         }
1672         switch( cmd )
1673           {
1674           case cmdHELP:
1675             for(i=0; cmds[i].name; i++ )
1676               {
1677                 if((cmds[i].flags & KEYEDIT_NEED_SK) && !sec_keyblock )
1678                   ; /* skip if we do not have the secret key */
1679                 else if( cmds[i].desc )
1680                   tty_printf("%-11s %s\n", cmds[i].name, _(cmds[i].desc) );
1681               }
1682
1683             tty_printf("\n");
1684             tty_printf(_(
1685 "* The `sign' command may be prefixed with an `l' for local "
1686 "signatures (lsign),\n"
1687 "  a `t' for trust signatures (tsign), an `nr' for non-revocable signatures\n"
1688 "  (nrsign), or any combination thereof (ltsign, tnrsign, etc.).\n"));
1689
1690             break;
1691
1692           case cmdLIST:
1693             redisplay = 1;
1694             break;
1695
1696           case cmdFPR:
1697             show_key_and_fingerprint( keyblock );
1698             break;
1699
1700           case cmdSELUID:
1701             if(strlen(arg_string)==NAMEHASH_LEN*2)
1702               redisplay=menu_select_uid_namehash(cur_keyblock,arg_string);
1703             else
1704               redisplay=menu_select_uid(cur_keyblock,arg_number);
1705             break;
1706
1707           case cmdSELKEY:
1708             if( menu_select_key( cur_keyblock, arg_number ) )
1709                 redisplay = 1;
1710             break;
1711
1712           case cmdCHECK:
1713             /* we can only do this with the public key becuase the
1714              * check functions can't cope with secret keys and it
1715              * is questionable whether this would make sense at all */
1716             check_all_keysigs( keyblock, count_selected_uids(keyblock) );
1717             break;
1718
1719           case cmdSIGN: /* sign (only the public key) */
1720             {
1721               int localsig=0,nonrevokesig=0,trustsig=0,interactive=0;
1722
1723               if( pk->is_revoked )
1724                 {
1725                   tty_printf(_("Key is revoked."));
1726
1727                   if(opt.expert)
1728                     {
1729                       tty_printf("  ");
1730                       if(!cpr_get_answer_is_yes("keyedit.sign_revoked.okay",
1731                                                 _("Are you sure you still want"
1732                                                   " to sign it? (y/N) ")))
1733                         break;
1734                     }
1735                   else
1736                     {
1737                       tty_printf(_("  Unable to sign.\n"));
1738                       break;
1739                     }
1740                 }
1741
1742               if(count_uids(keyblock) > 1 && !count_selected_uids(keyblock)
1743                  && !cpr_get_answer_is_yes("keyedit.sign_all.okay",
1744                                            _("Really sign all user IDs?"
1745                                              " (y/N) ")))
1746                 {
1747                   if(opt.interactive)
1748                     interactive=1;
1749                   else
1750                     {
1751                       tty_printf(_("Hint: Select the user IDs to sign\n"));
1752                       have_commands = 0;
1753                       break;
1754                     }
1755
1756                 }
1757               /* What sort of signing are we doing? */
1758               if(!parse_sign_type(answer,&localsig,&nonrevokesig,&trustsig))
1759                 {
1760                   tty_printf(_("Unknown signature type `%s'\n"),answer);
1761                   break;
1762                 }
1763
1764               sign_uids(keyblock, locusr, &modified,
1765                         localsig, nonrevokesig, trustsig, interactive);
1766             }
1767             break;
1768
1769           case cmdDEBUG:
1770             dump_kbnode( cur_keyblock );
1771             break;
1772
1773           case cmdTOGGLE:
1774             toggle = !toggle;
1775             cur_keyblock = toggle? sec_keyblock : keyblock;
1776             redisplay = 1;
1777             break;
1778
1779           case cmdADDPHOTO:
1780             if (RFC2440 || RFC1991 || PGP2)
1781               {
1782                 tty_printf(
1783                    _("This command is not allowed while in %s mode.\n"),
1784                    RFC2440?"OpenPGP":PGP2?"PGP2":"RFC-1991");
1785                 break;
1786               }
1787             photo=1;
1788             /* fall through */
1789
1790           case cmdADDUID:
1791             if( menu_adduid( keyblock, sec_keyblock, photo ) )
1792               {
1793                 update_trust = 1;
1794                 redisplay = 1;
1795                 sec_modified = modified = 1;
1796                 merge_keys_and_selfsig( sec_keyblock );
1797                 merge_keys_and_selfsig( keyblock );
1798               }
1799             break;
1800
1801           case cmdDELUID: {
1802                 int n1;
1803
1804                 if( !(n1=count_selected_uids(keyblock)) )
1805                     tty_printf(_("You must select at least one user ID.\n"));
1806                 else if( real_uids_left(keyblock) < 1 )
1807                     tty_printf(_("You can't delete the last user ID!\n"));
1808                 else if( cpr_get_answer_is_yes("keyedit.remove.uid.okay",
1809                 n1 > 1? _("Really remove all selected user IDs? (y/N) ")
1810                             : _("Really remove this user ID? (y/N) ")
1811                        ) ) {
1812                     menu_deluid( keyblock, sec_keyblock );
1813                     redisplay = 1;
1814                     modified = 1;
1815                     if( sec_keyblock )
1816                        sec_modified = 1;
1817                 }
1818             }
1819             break;
1820
1821           case cmdDELSIG: {
1822                 int n1;
1823
1824                 if( !(n1=count_selected_uids(keyblock)) )
1825                     tty_printf(_("You must select at least one user ID.\n"));
1826                 else if( menu_delsig( keyblock ) ) {
1827                     /* no redisplay here, because it may scroll away some
1828                      * status output of delsig */
1829                     modified = 1;
1830                 }
1831             }
1832             break;
1833
1834           case cmdADDKEY:
1835             if( generate_subkeypair( keyblock, sec_keyblock ) ) {
1836                 redisplay = 1;
1837                 sec_modified = modified = 1;
1838                 merge_keys_and_selfsig( sec_keyblock );
1839                 merge_keys_and_selfsig( keyblock );
1840             }
1841             break;
1842
1843 #ifdef ENABLE_CARD_SUPPORT
1844           case cmdADDCARDKEY:
1845             if (card_generate_subkey (keyblock, sec_keyblock)) {
1846                 redisplay = 1;
1847                 sec_modified = modified = 1;
1848                 merge_keys_and_selfsig( sec_keyblock );
1849                 merge_keys_and_selfsig( keyblock );
1850             }
1851             break;
1852
1853         case cmdKEYTOCARD:
1854           {
1855             KBNODE node=NULL;
1856             switch ( count_selected_keys (sec_keyblock) )
1857               {
1858               case 0:
1859                 if (cpr_get_answer_is_yes("keyedit.keytocard.use_primary",
1860                                      _("Really move the primary key? (y/N) ")))
1861                   node = sec_keyblock;
1862                 break;
1863               case 1:
1864                 for (node = sec_keyblock; node; node = node->next )
1865                   {
1866                     if (node->pkt->pkttype == PKT_SECRET_SUBKEY 
1867                         && node->flag & NODFLG_SELKEY)
1868                       break;
1869                   }
1870                 break;
1871               default:
1872                 tty_printf(_("You must select exactly one key.\n"));
1873                 break;
1874               }
1875             if (node)
1876               {
1877                 PKT_public_key *xxpk = find_pk_from_sknode (keyblock, node);
1878                 if (card_store_subkey (node, xxpk?xxpk->pubkey_usage:0))
1879                   {
1880                     redisplay = 1;
1881                     sec_modified = 1;
1882                   }
1883               }
1884           }
1885           break;
1886
1887         case cmdBKUPTOCARD:
1888           {
1889             /* Ask for a filename, check whether this is really a
1890                backup key as generated by the card generation, parse
1891                that key and store it on card. */
1892             KBNODE node;
1893             const char *fname;
1894             PACKET *pkt;
1895             IOBUF a;
1896
1897             fname = arg_string;
1898             if (!*fname)
1899               {
1900                 tty_printf (_("Command expects a filename argument\n"));
1901                 break;
1902               }
1903
1904             /* Open that file.  */
1905             a = iobuf_open (fname);
1906             if (a && is_secured_file (iobuf_get_fd (a)))
1907               {
1908                 iobuf_close (a);
1909                 a = NULL;
1910                 errno = EPERM;
1911               }
1912             if (!a)
1913               {
1914                 tty_printf (_("Can't open `%s': %s\n"),
1915                             fname, strerror(errno));
1916                 break;
1917               }
1918             
1919             /* Parse and check that file.  */
1920             pkt = xmalloc (sizeof *pkt);
1921             init_packet (pkt);
1922             rc = parse_packet (a, pkt);
1923             iobuf_close (a);
1924             iobuf_ioctl (NULL, 2, 0, (char*)fname); /* (invalidate cache).  */
1925             if (!rc 
1926                 && pkt->pkttype != PKT_SECRET_KEY 
1927                 && pkt->pkttype != PKT_SECRET_SUBKEY)
1928               rc = G10ERR_NO_SECKEY;
1929             if (rc)
1930               {
1931                 tty_printf(_("Error reading backup key from `%s': %s\n"),
1932                            fname, g10_errstr (rc));
1933                 free_packet (pkt);
1934                 xfree (pkt);
1935                 break;
1936               }
1937             node = new_kbnode (pkt);
1938
1939             /* Store it.  */
1940             if (card_store_subkey (node, 0))
1941               {
1942                 redisplay = 1;
1943                 sec_modified = 1;
1944               }
1945             release_kbnode (node);
1946           }
1947           break;
1948
1949 #endif /* ENABLE_CARD_SUPPORT */
1950
1951           case cmdDELKEY: {
1952                 int n1;
1953
1954                 if( !(n1=count_selected_keys( keyblock )) )
1955                     tty_printf(_("You must select at least one key.\n"));
1956                 else if( !cpr_get_answer_is_yes( "keyedit.remove.subkey.okay",
1957                        n1 > 1?
1958                    _("Do you really want to delete the selected keys? (y/N) "):
1959                         _("Do you really want to delete this key? (y/N) ")
1960                        ))
1961                     ;
1962                 else {
1963                     menu_delkey( keyblock, sec_keyblock );
1964                     redisplay = 1;
1965                     modified = 1;
1966                     if( sec_keyblock )
1967                        sec_modified = 1;
1968                 }
1969             }
1970             break;
1971
1972           case cmdADDREVOKER:
1973             {
1974               int sensitive=0;
1975
1976               if(ascii_strcasecmp(arg_string,"sensitive")==0)
1977                 sensitive=1;
1978               if( menu_addrevoker( keyblock, sec_keyblock, sensitive ) ) {
1979                 redisplay = 1;
1980                 sec_modified = modified = 1;
1981                 merge_keys_and_selfsig( sec_keyblock );
1982                 merge_keys_and_selfsig( keyblock );
1983               }
1984             }
1985             break;
1986
1987           case cmdREVUID: {
1988                 int n1;
1989
1990                 if( !(n1=count_selected_uids(keyblock)) )
1991                     tty_printf(_("You must select at least one user ID.\n"));
1992                 else if( cpr_get_answer_is_yes(
1993                             "keyedit.revoke.uid.okay",
1994                        n1 > 1? _("Really revoke all selected user IDs? (y/N) ")
1995                              : _("Really revoke this user ID? (y/N) ")
1996                        ) ) {
1997                   if(menu_revuid(keyblock,sec_keyblock))
1998                     {
1999                       modified=1;
2000                       redisplay=1;
2001                     }
2002                 }
2003             }
2004             break;
2005
2006           case cmdREVKEY:
2007             {
2008               int n1;
2009
2010               if( !(n1=count_selected_keys( keyblock )) )
2011                 {
2012                   if(cpr_get_answer_is_yes("keyedit.revoke.subkey.okay",
2013                                            _("Do you really want to revoke"
2014                                              " the entire key? (y/N) ")))
2015                     {
2016                       if(menu_revkey(keyblock,sec_keyblock))
2017                         modified=1;
2018
2019                       redisplay=1;
2020                     }
2021                 }
2022               else if(cpr_get_answer_is_yes("keyedit.revoke.subkey.okay",
2023                                             n1 > 1?
2024                                             _("Do you really want to revoke"
2025                                               " the selected subkeys? (y/N) "):
2026                                             _("Do you really want to revoke"
2027                                               " this subkey? (y/N) ")))
2028                 {
2029                   if( menu_revsubkey( keyblock, sec_keyblock ) )
2030                     modified = 1;
2031
2032                   redisplay = 1;
2033                 }
2034
2035               if(modified)
2036                 merge_keys_and_selfsig( keyblock );
2037             }
2038             break;
2039
2040           case cmdEXPIRE:
2041             if( menu_expire( keyblock, sec_keyblock ) )
2042               {
2043                 merge_keys_and_selfsig( sec_keyblock );
2044                 merge_keys_and_selfsig( keyblock );
2045                 sec_modified = 1;
2046                 modified = 1;
2047                 redisplay = 1;
2048               }
2049             break;
2050
2051           case cmdPRIMARY:
2052             if( menu_set_primary_uid ( keyblock, sec_keyblock ) ) {
2053                 merge_keys_and_selfsig( keyblock );
2054                 modified = 1;
2055                 redisplay = 1;
2056             }
2057             break;
2058
2059           case cmdPASSWD:
2060             if( change_passphrase( sec_keyblock ) )
2061                 sec_modified = 1;
2062             break;
2063
2064           case cmdTRUST:
2065             if(opt.trust_model==TM_EXTERNAL)
2066               {
2067                 tty_printf(_("Owner trust may not be set while "
2068                              "using an user provided trust database\n"));
2069                 break;
2070               }
2071
2072             show_key_with_all_names( keyblock, 0, 0, 0, 1, 0 );
2073             tty_printf("\n");
2074             if( edit_ownertrust( find_kbnode( keyblock,
2075                                  PKT_PUBLIC_KEY )->pkt->pkt.public_key, 1 ) ) {
2076                 redisplay = 1;
2077                 /* No real need to set update_trust here as
2078                    edit_ownertrust() calls revalidation_mark()
2079                    anyway. */
2080                 update_trust=1;
2081             }
2082             break;
2083
2084           case cmdPREF:
2085             show_key_with_all_names( keyblock, 0, 0, 0, 0, 1 );
2086             break;
2087
2088           case cmdSHOWPREF:
2089             show_key_with_all_names( keyblock, 0, 0, 0, 0, 2 );
2090             break;
2091
2092           case cmdSETPREF:
2093             {
2094               PKT_user_id *tempuid;
2095
2096               keygen_set_std_prefs(!*arg_string?"default" : arg_string, 0);
2097
2098               tempuid=keygen_get_std_prefs();
2099               tty_printf(_("Set preference list to:\n"));
2100               show_prefs(tempuid,NULL,1);
2101               free_user_id(tempuid);
2102
2103               if(cpr_get_answer_is_yes("keyedit.setpref.okay",
2104                                        count_selected_uids (keyblock)?
2105                                        _("Really update the preferences"
2106                                          " for the selected user IDs? (y/N) "):
2107                                        _("Really update the preferences? (y/N) ")))
2108                 {
2109                   if ( menu_set_preferences (keyblock, sec_keyblock) )
2110                     {
2111                       merge_keys_and_selfsig (keyblock);
2112                       modified = 1;
2113                       redisplay = 1;
2114                     }
2115                 }
2116             }
2117             break;
2118
2119           case cmdPREFKS:
2120             if( menu_set_keyserver_url ( *arg_string?arg_string:NULL,
2121                                          keyblock, sec_keyblock ) )
2122               {
2123                 merge_keys_and_selfsig( keyblock );
2124                 modified = 1;
2125                 redisplay = 1;
2126               }
2127             break;
2128
2129           case cmdNOP:
2130             break;
2131
2132           case cmdREVSIG:
2133             if( menu_revsig( keyblock ) ) {
2134                 redisplay = 1;
2135                 modified = 1;
2136             }
2137             break;
2138
2139           case cmdENABLEKEY:
2140           case cmdDISABLEKEY:
2141             if( enable_disable_key( keyblock, cmd == cmdDISABLEKEY ) ) {
2142                 redisplay = 1;
2143                 modified = 1;
2144             }
2145             break;
2146
2147           case cmdSHOWPHOTO:
2148             menu_showphoto(keyblock);
2149             break;
2150
2151           case cmdCLEAN:
2152             {
2153               if(*arg_string)
2154                 {
2155                   if(ascii_strcasecmp(arg_string,"sigs")==0
2156                      || ascii_strcasecmp(arg_string,"signatures")==0
2157                      || ascii_strcasecmp(arg_string,"certs")==0
2158                      || ascii_strcasecmp(arg_string,"certificates")==0)
2159                     modified=menu_clean_sigs_from_uids(keyblock);
2160                   else if(ascii_strcasecmp(arg_string,"uids")==0)
2161                     redisplay=modified=menu_clean_uids_from_key(keyblock);
2162                   else
2163                     tty_printf("Unable to clean `%s'\n",arg_string);
2164                 }
2165               else
2166                 {
2167                   modified=menu_clean_sigs_from_uids(keyblock);
2168                   modified+=menu_clean_uids_from_key(keyblock);
2169                   redisplay=modified;
2170                 }
2171             }
2172             break;
2173
2174           case cmdQUIT:
2175             if( have_commands )
2176                 goto leave;
2177             if( !modified && !sec_modified )
2178                 goto leave;
2179             if( !cpr_get_answer_is_yes("keyedit.save.okay",
2180                                         _("Save changes? (y/N) ")) ) {
2181                 if( cpr_enabled()
2182                     || cpr_get_answer_is_yes("keyedit.cancel.okay",
2183                                              _("Quit without saving? (y/N) ")))
2184                     goto leave;
2185                 break;
2186             }
2187             /* fall thru */
2188           case cmdSAVE:
2189             if( modified || sec_modified  ) {
2190                 if( modified ) {
2191                     rc = keydb_update_keyblock (kdbhd, keyblock);
2192                     if( rc ) {
2193                         log_error(_("update failed: %s\n"), g10_errstr(rc) );
2194                         break;
2195                     }
2196                 }
2197                 if( sec_modified ) {
2198                     rc = keydb_update_keyblock (sec_kdbhd, sec_keyblock );
2199                     if( rc ) {
2200                         log_error( _("update secret failed: %s\n"),
2201                                    g10_errstr(rc) );
2202                         break;
2203                     }
2204                 }
2205             }
2206             else
2207                 tty_printf(_("Key not changed so no update needed.\n"));
2208
2209             if( update_trust )
2210               {
2211                 revalidation_mark ();
2212                 update_trust=0;
2213               }
2214             goto leave;
2215
2216           case cmdINVCMD:
2217           default:
2218             tty_printf("\n");
2219             tty_printf(_("Invalid command  (try \"help\")\n"));
2220             break;
2221         }
2222     } /* end main loop */
2223
2224   leave:
2225     release_kbnode( keyblock );
2226     release_kbnode( sec_keyblock );
2227     keydb_release (kdbhd);
2228     xfree(answer);
2229 }
2230
2231
2232 \f
2233 /****************
2234  * show preferences of a public keyblock.
2235  */
2236 static void
2237 show_prefs (PKT_user_id *uid, PKT_signature *selfsig, int verbose)
2238 {
2239     const prefitem_t fake={0,0};
2240     const prefitem_t *prefs;
2241     int i;
2242
2243     if( !uid )
2244         return;
2245
2246     if( uid->prefs )
2247         prefs=uid->prefs;
2248     else if(verbose)
2249         prefs=&fake;
2250     else
2251       return;
2252
2253     if (verbose) {
2254         int any, des_seen=0, sha1_seen=0, uncomp_seen=0;
2255
2256         tty_printf ("     ");
2257         tty_printf (_("Cipher: "));
2258         for(i=any=0; prefs[i].type; i++ ) {
2259             if( prefs[i].type == PREFTYPE_SYM ) {
2260                 const char *s = cipher_algo_to_string (prefs[i].value);
2261                 
2262                 if (any)
2263                     tty_printf (", ");
2264                 any = 1;
2265                 /* We don't want to display strings for experimental algos */
2266                 if (s && prefs[i].value < 100 )
2267                     tty_printf ("%s", s );
2268                 else
2269                     tty_printf ("[%d]", prefs[i].value);
2270                 if (prefs[i].value == CIPHER_ALGO_3DES )
2271                     des_seen = 1;
2272             }    
2273         }
2274         if (!des_seen) {
2275             if (any)
2276                 tty_printf (", ");
2277             tty_printf ("%s",cipher_algo_to_string(CIPHER_ALGO_3DES));
2278         }
2279         tty_printf ("\n     ");
2280         tty_printf (_("Digest: "));
2281         for(i=any=0; prefs[i].type; i++ ) {
2282             if( prefs[i].type == PREFTYPE_HASH ) {
2283                 const char *s = digest_algo_to_string (prefs[i].value);
2284                 
2285                 if (any)
2286                     tty_printf (", ");
2287                 any = 1;
2288                 /* We don't want to display strings for experimental algos */
2289                 if (s && prefs[i].value < 100 )
2290                     tty_printf ("%s", s );
2291                 else
2292                     tty_printf ("[%d]", prefs[i].value);
2293                 if (prefs[i].value == DIGEST_ALGO_SHA1 )
2294                     sha1_seen = 1;
2295             }
2296         }
2297         if (!sha1_seen) {
2298             if (any)
2299                 tty_printf (", ");
2300             tty_printf ("%s",digest_algo_to_string(DIGEST_ALGO_SHA1));
2301         }
2302         tty_printf ("\n     ");
2303         tty_printf (_("Compression: "));
2304         for(i=any=0; prefs[i].type; i++ ) {
2305             if( prefs[i].type == PREFTYPE_ZIP ) {
2306                 const char *s=compress_algo_to_string(prefs[i].value);
2307                 
2308                 if (any)
2309                     tty_printf (", ");
2310                 any = 1;
2311                 /* We don't want to display strings for experimental algos */
2312                 if (s && prefs[i].value < 100 )
2313                     tty_printf ("%s", s );
2314                 else
2315                     tty_printf ("[%d]", prefs[i].value);
2316                 if (prefs[i].value == COMPRESS_ALGO_NONE )
2317                     uncomp_seen = 1;
2318             }
2319         }
2320         if (!uncomp_seen) {
2321             if (any)
2322                 tty_printf (", ");
2323             else {
2324               tty_printf ("%s",compress_algo_to_string(COMPRESS_ALGO_ZIP));
2325               tty_printf (", ");
2326             }
2327             tty_printf ("%s",compress_algo_to_string(COMPRESS_ALGO_NONE));
2328         }
2329         if(uid->mdc_feature || !uid->ks_modify)
2330           {
2331             tty_printf ("\n     ");
2332             tty_printf (_("Features: "));
2333             any=0;
2334             if(uid->mdc_feature)
2335               {
2336                 tty_printf ("MDC");
2337                 any=1;
2338               }
2339             if(!uid->ks_modify)
2340               {
2341                 if(any)
2342                   tty_printf (", ");
2343                 tty_printf (_("Keyserver no-modify"));
2344               }
2345           }
2346         tty_printf("\n");
2347
2348         if(selfsig)
2349           {
2350             const byte *pref_ks;
2351             size_t pref_ks_len;
2352
2353             pref_ks=parse_sig_subpkt(selfsig->hashed,
2354                                      SIGSUBPKT_PREF_KS,&pref_ks_len);
2355             if(pref_ks && pref_ks_len)
2356               {
2357                 tty_printf ("     ");
2358                 tty_printf(_("Preferred keyserver: "));
2359                 tty_print_utf8_string(pref_ks,pref_ks_len);
2360                 tty_printf("\n");
2361               }
2362           }
2363     }
2364     else {
2365         tty_printf("    ");
2366         for(i=0; prefs[i].type; i++ ) {
2367             tty_printf( " %c%d", prefs[i].type == PREFTYPE_SYM   ? 'S' :
2368                                  prefs[i].type == PREFTYPE_HASH  ? 'H' :
2369                                  prefs[i].type == PREFTYPE_ZIP ? 'Z':'?',
2370                                  prefs[i].value);
2371         }
2372         if (uid->mdc_feature)
2373             tty_printf (" [mdc]");
2374         if (!uid->ks_modify)
2375             tty_printf (" [no-ks-modify]");
2376         tty_printf("\n");
2377     }
2378 }
2379
2380
2381 /* This is the version of show_key_with_all_names used when
2382    opt.with_colons is used.  It prints all available data in a easy to
2383    parse format and does not translate utf8 */
2384 static void
2385 show_key_with_all_names_colon (KBNODE keyblock)
2386 {
2387   KBNODE node;
2388   int i, j, ulti_hack=0;
2389   byte pk_version=0;
2390   PKT_public_key *primary=NULL;
2391
2392   /* the keys */
2393   for ( node = keyblock; node; node = node->next )
2394     {
2395       if (node->pkt->pkttype == PKT_PUBLIC_KEY
2396           || (node->pkt->pkttype == PKT_PUBLIC_SUBKEY) )
2397         {
2398           PKT_public_key *pk = node->pkt->pkt.public_key;
2399           u32 keyid[2];
2400
2401           if (node->pkt->pkttype == PKT_PUBLIC_KEY)
2402             {
2403               pk_version = pk->version;
2404               primary=pk;
2405             }
2406
2407           keyid_from_pk (pk, keyid);
2408
2409           fputs (node->pkt->pkttype == PKT_PUBLIC_KEY?"pub:":"sub:", stdout);
2410           if (!pk->is_valid)
2411             putchar ('i');
2412           else if (pk->is_revoked)
2413             putchar ('r');
2414           else if (pk->has_expired)
2415             putchar ('e');
2416           else if (!(opt.fast_list_mode || opt.no_expensive_trust_checks ))
2417             {
2418               int trust = get_validity_info (pk, NULL);
2419               if(trust=='u')
2420                 ulti_hack=1;
2421               putchar (trust);
2422             }
2423
2424           printf (":%u:%d:%08lX%08lX:%lu:%lu::",
2425                   nbits_from_pk (pk),
2426                   pk->pubkey_algo,
2427                   (ulong)keyid[0], (ulong)keyid[1],
2428                   (ulong)pk->timestamp,
2429                   (ulong)pk->expiredate );
2430           if (node->pkt->pkttype==PKT_PUBLIC_KEY
2431               && !(opt.fast_list_mode || opt.no_expensive_trust_checks ))
2432             putchar(get_ownertrust_info (pk));
2433           putchar(':');
2434           putchar('\n');
2435           
2436           print_fingerprint (pk, NULL, 0);
2437
2438           /* print the revoker record */
2439           if( !pk->revkey && pk->numrevkeys )
2440             BUG();
2441           else
2442             {
2443               for (i=0; i < pk->numrevkeys; i++)
2444                 {
2445                   byte *p;
2446
2447                   printf ("rvk:::%d::::::", pk->revkey[i].algid);
2448                   p = pk->revkey[i].fpr;
2449                   for (j=0; j < 20; j++, p++ )
2450                     printf ("%02X", *p);
2451                   printf (":%02x%s:\n", pk->revkey[i].class,
2452                           (pk->revkey[i].class&0x40)?"s":"");
2453                 }
2454             }
2455         }
2456     }
2457   
2458     /* the user ids */
2459     i = 0;
2460     for (node = keyblock; node; node = node->next) 
2461       {
2462         if ( node->pkt->pkttype == PKT_USER_ID )
2463           {
2464             PKT_user_id *uid = node->pkt->pkt.user_id;
2465
2466             ++i;
2467
2468             if(uid->attrib_data)
2469               printf("uat:");
2470             else
2471               printf("uid:");
2472
2473             if ( uid->is_revoked )
2474               printf("r::::::::");
2475             else if ( uid->is_expired )
2476               printf("e::::::::");
2477             else if ( opt.fast_list_mode || opt.no_expensive_trust_checks )
2478               printf("::::::::");
2479             else
2480               {
2481                 int uid_validity;
2482
2483                 if( primary && !ulti_hack )
2484                   uid_validity = get_validity_info( primary, uid );
2485                 else
2486                   uid_validity = 'u';
2487                 printf("%c::::::::",uid_validity);
2488               }
2489
2490             if(uid->attrib_data)
2491               printf ("%u %lu",uid->numattribs,uid->attrib_len);
2492             else
2493               print_string (stdout, uid->name, uid->len, ':');
2494
2495             putchar (':');
2496             /* signature class */
2497             putchar (':');
2498             /* capabilities */
2499             putchar (':');
2500             /* preferences */
2501             if (pk_version>3 || uid->selfsigversion>3)
2502               {
2503                 const prefitem_t *prefs = uid->prefs;
2504                 
2505                 for (j=0; prefs && prefs[j].type; j++)
2506                   {
2507                     if (j)
2508                       putchar (' ');
2509                     printf ("%c%d", prefs[j].type == PREFTYPE_SYM   ? 'S' :
2510                             prefs[j].type == PREFTYPE_HASH  ? 'H' :
2511                             prefs[j].type == PREFTYPE_ZIP ? 'Z':'?',
2512                             prefs[j].value);
2513                   } 
2514                 if (uid->mdc_feature)
2515                   printf (",mdc");
2516                 if (!uid->ks_modify)
2517                   printf (",no-ks-modify");
2518               } 
2519             putchar (':');
2520             /* flags */
2521             printf ("%d,", i);
2522             if (uid->is_primary)
2523               putchar ('p');
2524             if (uid->is_revoked)
2525               putchar ('r');
2526             if (uid->is_expired)
2527               putchar ('e');
2528             if ((node->flag & NODFLG_SELUID))
2529               putchar ('s');
2530             if ((node->flag & NODFLG_MARK_A))
2531               putchar ('m');
2532             putchar (':');
2533             putchar('\n');
2534           }
2535       }
2536 }
2537
2538
2539 /****************
2540  * Display the key a the user ids, if only_marked is true, do only
2541  * so for user ids with mark A flag set and dont display the index number
2542  */
2543 static void
2544 show_key_with_all_names( KBNODE keyblock, int only_marked, int with_revoker,
2545                          int with_fpr, int with_subkeys, int with_prefs )
2546 {
2547     KBNODE node;
2548     int i;
2549     int do_warn = 0;
2550     byte pk_version=0;
2551     PKT_public_key *primary=NULL;
2552
2553     if (opt.with_colons)
2554       {
2555         show_key_with_all_names_colon (keyblock);
2556         return;
2557       }
2558
2559     /* the keys */
2560     for( node = keyblock; node; node = node->next ) {
2561         if( node->pkt->pkttype == PKT_PUBLIC_KEY
2562             || (with_subkeys && node->pkt->pkttype == PKT_PUBLIC_SUBKEY
2563                 && !is_deleted_kbnode(node)) ) {
2564             PKT_public_key *pk = node->pkt->pkt.public_key;
2565             const char *otrust="err",*trust="err";
2566
2567             if( node->pkt->pkttype == PKT_PUBLIC_KEY ) {
2568                 /* do it here, so that debug messages don't clutter the
2569                  * output */
2570                 static int did_warn = 0;
2571
2572                 trust = get_validity_string (pk, NULL);
2573                 otrust = get_ownertrust_string (pk);
2574
2575                 /* Show a warning once */
2576                 if (!did_warn
2577                     && (get_validity (pk, NULL) & TRUST_FLAG_PENDING_CHECK)) {
2578                     did_warn = 1;
2579                     do_warn = 1;
2580                 }
2581
2582                 pk_version=pk->version;
2583                 primary=pk;
2584             }
2585
2586             if(pk->is_revoked)
2587               {
2588                 char *user=get_user_id_string_native(pk->revoked.keyid);
2589                 const char *algo=pubkey_algo_to_string(pk->revoked.algo);
2590                 tty_printf(_("This key was revoked on %s by %s key %s\n"),
2591                            revokestr_from_pk(pk),algo?algo:"?",user);
2592                 xfree(user);
2593               }
2594
2595             if(with_revoker)
2596               {
2597                 if( !pk->revkey && pk->numrevkeys )
2598                   BUG();
2599                 else
2600                   for(i=0;i<pk->numrevkeys;i++)
2601                     {
2602                       u32 r_keyid[2];
2603                       char *user;
2604                       const char *algo=
2605                         pubkey_algo_to_string(pk->revkey[i].algid);
2606
2607                       keyid_from_fingerprint(pk->revkey[i].fpr,
2608                                              MAX_FINGERPRINT_LEN,r_keyid);
2609
2610                       user=get_user_id_string_native(r_keyid);
2611                       tty_printf(_("This key may be revoked by %s key %s"),
2612                                  algo?algo:"?",user);
2613
2614                       if(pk->revkey[i].class&0x40)
2615                         {
2616                           tty_printf(" ");
2617                           tty_printf(_("(sensitive)"));
2618                         }
2619
2620                       tty_printf ("\n");
2621                       xfree(user);
2622                     }
2623               }
2624
2625             keyid_from_pk(pk,NULL);
2626             tty_printf("%s%c %4u%c/%s  ",
2627                        node->pkt->pkttype == PKT_PUBLIC_KEY? "pub":"sub",
2628                        (node->flag & NODFLG_SELKEY)? '*':' ',
2629                        nbits_from_pk( pk ),
2630                        pubkey_letter( pk->pubkey_algo ),
2631                        keystr(pk->keyid));
2632
2633             tty_printf(_("created: %s"),datestr_from_pk(pk));
2634             tty_printf("  ");
2635             if(pk->is_revoked)
2636               tty_printf(_("revoked: %s"),revokestr_from_pk(pk));
2637             else if(pk->has_expired)
2638               tty_printf(_("expired: %s"),expirestr_from_pk(pk));
2639             else
2640               tty_printf(_("expires: %s"),expirestr_from_pk(pk));
2641             tty_printf("  ");
2642             tty_printf(_("usage: %s"),usagestr_from_pk(pk));
2643             tty_printf("\n");
2644
2645             if( node->pkt->pkttype == PKT_PUBLIC_KEY )
2646               {
2647                 if(opt.trust_model!=TM_ALWAYS)
2648                   {
2649                     tty_printf("%*s", (int)keystrlen()+13,"");
2650                     /* Ownertrust is only meaningful for the PGP or
2651                        classic trust models */
2652                     if(opt.trust_model==TM_PGP || opt.trust_model==TM_CLASSIC)
2653                       {
2654                         int width=14-strlen(otrust);
2655                         if(width<=0)
2656                           width=1;
2657                         tty_printf(_("trust: %s"), otrust);
2658                         tty_printf("%*s",width,"");
2659                       }
2660                     
2661                     tty_printf(_("validity: %s"), trust );
2662                     tty_printf("\n");
2663                   }
2664                 if( node->pkt->pkttype == PKT_PUBLIC_KEY
2665                     && (get_ownertrust (pk)&TRUST_FLAG_DISABLED))
2666                   {
2667                     tty_printf("*** ");
2668                     tty_printf(_("This key has been disabled"));
2669                     tty_printf("\n");
2670                   }
2671               }
2672
2673             if( node->pkt->pkttype == PKT_PUBLIC_KEY && with_fpr )
2674               {
2675                 print_fingerprint ( pk, NULL, 2 );
2676                 tty_printf("\n");
2677               }
2678         }
2679         else if( node->pkt->pkttype == PKT_SECRET_KEY
2680             || (with_subkeys && node->pkt->pkttype == PKT_SECRET_SUBKEY) )
2681           {
2682             PKT_secret_key *sk = node->pkt->pkt.secret_key;
2683             tty_printf("%s%c %4u%c/%s  ",
2684                        node->pkt->pkttype == PKT_SECRET_KEY? "sec":"ssb",
2685                        (node->flag & NODFLG_SELKEY)? '*':' ',
2686                        nbits_from_sk( sk ),
2687                        pubkey_letter( sk->pubkey_algo ),
2688                        keystr_from_sk(sk));
2689             tty_printf(_("created: %s"),datestr_from_sk(sk));
2690             tty_printf("  ");
2691             tty_printf(_("expires: %s"),expirestr_from_sk(sk));
2692             tty_printf("\n");
2693             if (sk->is_protected && sk->protect.s2k.mode == 1002)
2694               {
2695                 tty_printf("                     ");
2696                 tty_printf(_("card-no: ")); 
2697                 if (sk->protect.ivlen == 16
2698                     && !memcmp (sk->protect.iv, "\xD2\x76\x00\x01\x24\x01", 6))
2699                   { /* This is an OpenPGP card. */
2700                     for (i=8; i < 14; i++)
2701                       {
2702                         if (i == 10)
2703                           tty_printf (" ");
2704                         tty_printf ("%02X", sk->protect.iv[i]);
2705                       }
2706                   }
2707                 else
2708                   { /* Something is wrong: Print all. */
2709                     for (i=0; i < sk->protect.ivlen; i++)
2710                       tty_printf ("%02X", sk->protect.iv[i]);
2711                   }
2712                 tty_printf ("\n");
2713               }
2714           }
2715     }
2716     
2717     /* the user ids */
2718
2719     i = 0;
2720     for( node = keyblock; node; node = node->next )
2721       {
2722         if( node->pkt->pkttype == PKT_USER_ID
2723             && !is_deleted_kbnode(node))
2724           {
2725             PKT_user_id *uid = node->pkt->pkt.user_id;
2726             ++i;
2727             if( !only_marked || (only_marked && (node->flag & NODFLG_MARK_A)))
2728               {
2729                 if(!only_marked && primary)
2730                   tty_printf("%s ",uid_trust_string_fixed(primary,uid));
2731
2732                 if( only_marked )
2733                   tty_printf("     ");
2734                 else if( node->flag & NODFLG_SELUID )
2735                   tty_printf("(%d)* ", i);
2736                 else if( uid->is_primary )
2737                   tty_printf("(%d). ", i);
2738                 else
2739                   tty_printf("(%d)  ", i);
2740                 tty_print_utf8_string( uid->name, uid->len );
2741                 tty_printf("\n");
2742                 if( with_prefs )
2743                   {
2744                     if(pk_version>3 || uid->selfsigversion>3)
2745                       {
2746                         PKT_signature *selfsig=NULL;
2747                         KBNODE signode;
2748
2749                         for(signode=node->next;
2750                             signode && signode->pkt->pkttype==PKT_SIGNATURE;
2751                             signode=signode->next)
2752                           {
2753                             if(signode->pkt->pkt.signature->
2754                                flags.chosen_selfsig)
2755                               {
2756                                 selfsig=signode->pkt->pkt.signature;
2757                                 break;
2758                               }
2759                           }
2760
2761                         show_prefs (uid, selfsig, with_prefs == 2);
2762                       }
2763                     else
2764                       tty_printf(_("There are no preferences on a"
2765                                    " PGP 2.x-style user ID.\n"));
2766                   }
2767               }
2768           }
2769       }
2770
2771     if (do_warn)
2772         tty_printf (_("Please note that the shown key validity"
2773                       " is not necessarily correct\n"
2774                       "unless you restart the program.\n")); 
2775 }
2776
2777
2778 /* Display basic key information.  This fucntion is suitable to show
2779    information on the key without any dependencies on the trustdb or
2780    any other internal GnuPG stuff.  KEYBLOCK may either be a public or
2781    a secret key.*/
2782 void
2783 show_basic_key_info ( KBNODE keyblock )
2784 {
2785   KBNODE node;
2786   int i;
2787
2788   /* The primary key */
2789   for (node = keyblock; node; node = node->next)
2790     {
2791       if (node->pkt->pkttype == PKT_PUBLIC_KEY)
2792         {
2793           PKT_public_key *pk = node->pkt->pkt.public_key;
2794           
2795           /* Note, we use the same format string as in other show
2796              functions to make the translation job easier. */
2797           tty_printf ("%s  %4u%c/%s  ",
2798                       node->pkt->pkttype == PKT_PUBLIC_KEY? "pub":"sub",
2799                       nbits_from_pk( pk ),
2800                       pubkey_letter( pk->pubkey_algo ),
2801                       keystr_from_pk(pk));
2802           tty_printf(_("created: %s"),datestr_from_pk(pk));
2803           tty_printf("  ");
2804           tty_printf(_("expires: %s"),expirestr_from_pk(pk));
2805           tty_printf("\n");
2806           print_fingerprint ( pk, NULL, 3 );
2807           tty_printf("\n");
2808         }
2809       else if (node->pkt->pkttype == PKT_SECRET_KEY)
2810         {
2811           PKT_secret_key *sk = node->pkt->pkt.secret_key;
2812           tty_printf("%s  %4u%c/%s",
2813                      node->pkt->pkttype == PKT_SECRET_KEY? "sec":"ssb",
2814                      nbits_from_sk( sk ),
2815                      pubkey_letter( sk->pubkey_algo ),
2816                      keystr_from_sk(sk));
2817           tty_printf(_("created: %s"),datestr_from_sk(sk));
2818           tty_printf("  ");
2819           tty_printf(_("expires: %s"),expirestr_from_sk(sk));
2820           tty_printf("\n");
2821           print_fingerprint (NULL, sk, 3 );
2822           tty_printf("\n");
2823         }
2824     }
2825
2826   /* The user IDs. */
2827   for (i=0, node = keyblock; node; node = node->next)
2828     {
2829       if (node->pkt->pkttype == PKT_USER_ID)
2830         {
2831           PKT_user_id *uid = node->pkt->pkt.user_id;
2832           ++i;
2833      
2834           tty_printf ("     ");
2835           if (uid->is_revoked)
2836             tty_printf("[%s] ",_("revoked"));
2837           else if ( uid->is_expired )
2838             tty_printf("[%s] ",_("expired"));
2839           tty_print_utf8_string (uid->name, uid->len);
2840           tty_printf ("\n");
2841         }
2842     }
2843 }
2844
2845 static void
2846 show_key_and_fingerprint( KBNODE keyblock )
2847 {
2848   KBNODE node;
2849   PKT_public_key *pk = NULL;
2850
2851   for( node = keyblock; node; node = node->next )
2852     {
2853       if( node->pkt->pkttype == PKT_PUBLIC_KEY )
2854         {
2855           pk = node->pkt->pkt.public_key;
2856           tty_printf("pub   %4u%c/%s %s ",
2857                      nbits_from_pk( pk ),
2858                      pubkey_letter( pk->pubkey_algo ),
2859                      keystr_from_pk(pk),
2860                      datestr_from_pk(pk) );
2861         }
2862       else if( node->pkt->pkttype == PKT_USER_ID )
2863         {
2864           PKT_user_id *uid = node->pkt->pkt.user_id;
2865           tty_print_utf8_string( uid->name, uid->len );
2866           break;
2867         }
2868     }
2869   tty_printf("\n");
2870   if( pk )
2871     print_fingerprint( pk, NULL, 2 );
2872 }
2873
2874
2875 /* Show a warning if no uids on the key have the primary uid flag
2876    set. */
2877 static void
2878 no_primary_warning(KBNODE keyblock)
2879 {
2880   KBNODE node;
2881   int have_primary=0,uid_count=0;
2882
2883   /* TODO: if we ever start behaving differently with a primary or
2884      non-primary attribute ID, we will need to check for attributes
2885      here as well. */
2886
2887   for(node=keyblock; node; node = node->next)
2888     {
2889       if(node->pkt->pkttype==PKT_USER_ID
2890          && node->pkt->pkt.user_id->attrib_data==NULL)
2891         {
2892           uid_count++;
2893
2894           if(node->pkt->pkt.user_id->is_primary==2)
2895             {
2896               have_primary=1;
2897               break;
2898             }
2899         }
2900     }
2901
2902   if(uid_count>1 && !have_primary)
2903     log_info(_("WARNING: no user ID has been marked as primary.  This command"
2904                " may\n              cause a different user ID to become"
2905                " the assumed primary.\n"));
2906 }
2907
2908 /****************
2909  * Ask for a new user id, do the selfsignature and put it into
2910  * both keyblocks.
2911  * Return true if there is a new user id
2912  */
2913 static int
2914 menu_adduid( KBNODE pub_keyblock, KBNODE sec_keyblock, int photo)
2915 {
2916     PKT_user_id *uid;
2917     PKT_public_key *pk=NULL;
2918     PKT_secret_key *sk=NULL;
2919     PKT_signature *sig=NULL;
2920     PACKET *pkt;
2921     KBNODE node;
2922     KBNODE pub_where=NULL, sec_where=NULL;
2923     int rc;
2924
2925     for( node = pub_keyblock; node; pub_where = node, node = node->next ) {
2926         if( node->pkt->pkttype == PKT_PUBLIC_KEY )
2927             pk = node->pkt->pkt.public_key;
2928         else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
2929             break;
2930     }
2931     if( !node ) /* no subkey */
2932         pub_where = NULL;
2933     for( node = sec_keyblock; node; sec_where = node, node = node->next ) {
2934         if( node->pkt->pkttype == PKT_SECRET_KEY )
2935             sk = copy_secret_key( NULL, node->pkt->pkt.secret_key);
2936         else if( node->pkt->pkttype == PKT_SECRET_SUBKEY )
2937             break;
2938     }
2939     if( !node ) /* no subkey */
2940         sec_where = NULL;
2941     assert(pk && sk);
2942
2943     if(photo) {
2944       int hasattrib=0;
2945
2946       for( node = pub_keyblock; node; node = node->next )
2947         if( node->pkt->pkttype == PKT_USER_ID &&
2948             node->pkt->pkt.user_id->attrib_data!=NULL)
2949           {
2950             hasattrib=1;
2951             break;
2952           }
2953
2954       /* It is legal but bad for compatibility to add a photo ID to a
2955          v3 key as it means that PGP2 will not be able to use that key
2956          anymore.  Also, PGP may not expect a photo on a v3 key.
2957          Don't bother to ask this if the key already has a photo - any
2958          damage has already been done at that point. -dms */
2959       if(pk->version==3 && !hasattrib)
2960         {
2961           if(opt.expert)
2962             {
2963               tty_printf(_("WARNING: This is a PGP2-style key.  "
2964                            "Adding a photo ID may cause some versions\n"
2965                            "         of PGP to reject this key.\n"));
2966
2967               if(!cpr_get_answer_is_yes("keyedit.v3_photo.okay",
2968                                         _("Are you sure you still want "
2969                                           "to add it? (y/N) ")))
2970                 return 0;
2971             }
2972           else
2973             {
2974               tty_printf(_("You may not add a photo ID to "
2975                            "a PGP2-style key.\n"));
2976               return 0;
2977             }
2978         }
2979
2980       uid = generate_photo_id(pk);
2981     } else
2982       uid = generate_user_id();
2983     if( !uid )
2984         return 0;
2985
2986     rc = make_keysig_packet( &sig, pk, uid, NULL, sk, 0x13, 0, 0, 0, 0,
2987                              keygen_add_std_prefs, pk );
2988     free_secret_key( sk );
2989     if( rc ) {
2990         log_error("signing failed: %s\n", g10_errstr(rc) );
2991         free_user_id(uid);
2992         return 0;
2993     }
2994
2995     /* insert/append to secret keyblock */
2996     pkt = xmalloc_clear( sizeof *pkt );
2997     pkt->pkttype = PKT_USER_ID;
2998     pkt->pkt.user_id = scopy_user_id(uid);
2999     node = new_kbnode(pkt);
3000     if( sec_where )
3001         insert_kbnode( sec_where, node, 0 );
3002     else
3003         add_kbnode( sec_keyblock, node );
3004     pkt = xmalloc_clear( sizeof *pkt );
3005     pkt->pkttype = PKT_SIGNATURE;
3006     pkt->pkt.signature = copy_signature(NULL, sig);
3007     if( sec_where )
3008         insert_kbnode( node, new_kbnode(pkt), 0 );
3009     else
3010         add_kbnode( sec_keyblock, new_kbnode(pkt) );
3011     /* insert/append to public keyblock */
3012     pkt = xmalloc_clear( sizeof *pkt );
3013     pkt->pkttype = PKT_USER_ID;
3014     pkt->pkt.user_id = uid;
3015     node = new_kbnode(pkt);
3016     if( pub_where )
3017         insert_kbnode( pub_where, node, 0 );
3018     else
3019         add_kbnode( pub_keyblock, node );
3020     pkt = xmalloc_clear( sizeof *pkt );
3021     pkt->pkttype = PKT_SIGNATURE;
3022     pkt->pkt.signature = copy_signature(NULL, sig);
3023     if( pub_where )
3024         insert_kbnode( node, new_kbnode(pkt), 0 );
3025     else
3026         add_kbnode( pub_keyblock, new_kbnode(pkt) );
3027     return 1;
3028 }
3029
3030
3031 /****************
3032  * Remove all selected userids from the keyrings
3033  */
3034 static void
3035 menu_deluid( KBNODE pub_keyblock, KBNODE sec_keyblock )
3036 {
3037     KBNODE node;
3038     int selected=0;
3039
3040     for( node = pub_keyblock; node; node = node->next ) {
3041         if( node->pkt->pkttype == PKT_USER_ID ) {
3042             selected = node->flag & NODFLG_SELUID;
3043             if( selected ) {
3044                 /* Only cause a trust update if we delete a
3045                    non-revoked user id */
3046                 if(!node->pkt->pkt.user_id->is_revoked)
3047                   update_trust=1;
3048                 delete_kbnode( node );
3049                 if( sec_keyblock ) {
3050                     KBNODE snode;
3051                     int s_selected = 0;
3052                     PKT_user_id *uid = node->pkt->pkt.user_id;
3053                     for( snode = sec_keyblock; snode; snode = snode->next ) {
3054                         if( snode->pkt->pkttype == PKT_USER_ID ) {
3055                             PKT_user_id *suid = snode->pkt->pkt.user_id;
3056
3057                             s_selected =
3058                                 (uid->len == suid->len
3059                                  && !memcmp( uid->name, suid->name, uid->len));
3060                             if( s_selected )
3061                                 delete_kbnode( snode );
3062                         }
3063                         else if( s_selected
3064                                  && snode->pkt->pkttype == PKT_SIGNATURE )
3065                             delete_kbnode( snode );
3066                         else if( snode->pkt->pkttype == PKT_SECRET_SUBKEY )
3067                             s_selected = 0;
3068                     }
3069                 }
3070             }
3071         }
3072         else if( selected && node->pkt->pkttype == PKT_SIGNATURE )
3073             delete_kbnode( node );
3074         else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
3075             selected = 0;
3076     }
3077     commit_kbnode( &pub_keyblock );
3078     if( sec_keyblock )
3079         commit_kbnode( &sec_keyblock );
3080 }
3081
3082
3083 static int
3084 menu_delsig( KBNODE pub_keyblock )
3085 {
3086     KBNODE node;
3087     PKT_user_id *uid = NULL;
3088     int changed=0;
3089
3090     for( node = pub_keyblock; node; node = node->next ) {
3091         if( node->pkt->pkttype == PKT_USER_ID ) {
3092             uid = (node->flag & NODFLG_SELUID)? node->pkt->pkt.user_id : NULL;
3093         }
3094         else if( uid && node->pkt->pkttype == PKT_SIGNATURE ) {
3095            int okay, valid, selfsig, inv_sig, no_key, other_err;
3096
3097             tty_printf("uid  ");
3098             tty_print_utf8_string( uid->name, uid->len );
3099             tty_printf("\n");
3100
3101             okay = inv_sig = no_key = other_err = 0;
3102             if(opt.with_colons)
3103               valid = print_and_check_one_sig_colon( pub_keyblock, node,
3104                                                &inv_sig, &no_key, &other_err,
3105                                                &selfsig, 1 );
3106             else
3107               valid = print_and_check_one_sig( pub_keyblock, node,
3108                                                &inv_sig, &no_key, &other_err,
3109                                                &selfsig, 1 );
3110
3111            if( valid ) {
3112                okay = cpr_get_answer_yes_no_quit(
3113                    "keyedit.delsig.valid",
3114                    _("Delete this good signature? (y/N/q)"));
3115
3116                /* Only update trust if we delete a good signature.
3117                   The other two cases do not affect trust. */
3118                if(okay)
3119                  update_trust=1;
3120            }
3121            else if( inv_sig || other_err )
3122                okay = cpr_get_answer_yes_no_quit(
3123                    "keyedit.delsig.invalid",
3124                    _("Delete this invalid signature? (y/N/q)"));
3125            else if( no_key )
3126                okay = cpr_get_answer_yes_no_quit(
3127                    "keyedit.delsig.unknown",
3128                    _("Delete this unknown signature? (y/N/q)"));
3129
3130             if( okay == -1 )
3131                 break;
3132            if( okay && selfsig && !cpr_get_answer_is_yes(
3133                                "keyedit.delsig.selfsig",
3134                               _("Really delete this self-signature? (y/N)") ))
3135                 okay = 0;
3136             if( okay ) {
3137                 delete_kbnode( node );
3138                 changed++;
3139             }
3140
3141         }
3142         else if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY )
3143             uid = NULL;
3144     }
3145
3146     if( changed ) {
3147         commit_kbnode( &pub_keyblock );
3148         tty_printf( changed == 1? _("Deleted %d signature.\n")
3149                                 : _("Deleted %d signatures.\n"), changed );
3150     }
3151     else
3152         tty_printf( _("Nothing deleted.\n") );
3153
3154     return changed;
3155 }
3156
3157 static int
3158 menu_clean_sigs_from_uids(KBNODE keyblock)
3159 {
3160   KBNODE uidnode;
3161   int modified=0;
3162   int select_all=!count_selected_uids(keyblock);
3163
3164   for(uidnode=keyblock->next;uidnode;uidnode=uidnode->next)
3165     {
3166       if(uidnode->pkt->pkttype==PKT_USER_ID
3167          && (uidnode->flag&NODFLG_SELUID || select_all))
3168         {
3169           int deleted;
3170           char *user=utf8_to_native(uidnode->pkt->pkt.user_id->name,
3171                                     uidnode->pkt->pkt.user_id->len,
3172                                     0);
3173           deleted=clean_sigs_from_uid(keyblock,uidnode,opt.verbose);
3174           if(deleted)
3175             {
3176               tty_printf(deleted==1?
3177                          "User ID \"%s\": %d signature removed.\n":
3178                          "User ID \"%s\": %d signatures removed.\n",
3179                          user,deleted);
3180               modified=1;
3181             }
3182           else
3183             tty_printf(_("User ID \"%s\": already clean.\n"),user);
3184
3185           xfree(user);
3186         }
3187     }
3188
3189   return modified;
3190 }
3191
3192 static int
3193 menu_clean_uids_from_key(KBNODE keyblock)
3194 {
3195   int modified=clean_uids_from_key(keyblock,0);
3196
3197   if(modified)
3198     {
3199       KBNODE node,uidnode=NULL;
3200
3201       for(node=keyblock->next;node;node=node->next)
3202         {
3203           if(node->pkt->pkttype==PKT_USER_ID)
3204             uidnode=node;
3205           else if(uidnode && node->pkt->pkttype==PKT_SIGNATURE
3206                   && is_deleted_kbnode(node))
3207             {
3208               const char *reason;
3209               char *user=utf8_to_native(uidnode->pkt->pkt.user_id->name,
3210                                         uidnode->pkt->pkt.user_id->len,0);
3211
3212               if(uidnode->pkt->pkt.user_id->is_revoked)
3213                 reason=_("revoked");
3214               else if(uidnode->pkt->pkt.user_id->is_expired)
3215                 reason=_("expired");
3216               else
3217                 reason=_("invalid");
3218
3219               tty_printf("User ID \"%s\" compacted: %s\n",user,reason);
3220
3221               uidnode=NULL;
3222
3223               xfree(user);
3224             }
3225         }
3226     }
3227   else
3228     tty_printf("No user IDs are compactable.\n");
3229
3230   return modified;
3231 }
3232
3233 /****************
3234  * Remove some of the secondary keys
3235  */
3236 static void
3237 menu_delkey( KBNODE pub_keyblock, KBNODE sec_keyblock )
3238 {
3239     KBNODE node;
3240     int selected=0;
3241
3242     for( node = pub_keyblock; node; node = node->next ) {
3243         if( node->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
3244             selected = node->flag & NODFLG_SELKEY;
3245             if( selected ) {
3246                 delete_kbnode( node );
3247                 if( sec_keyblock ) {
3248                     KBNODE snode;
3249                     int s_selected = 0;
3250                     u32 ki[2];
3251
3252                     keyid_from_pk( node->pkt->pkt.public_key, ki );
3253                     for( snode = sec_keyblock; snode; snode = snode->nex