diff --git a/g10/card-util.c b/g10/card-util.c index 7e329bb6b..1b9461e0a 100644 --- a/g10/card-util.c +++ b/g10/card-util.c @@ -680,9 +680,8 @@ current_card_status (ctrl_t ctrl, estream_t fp, if ( thefpr && !fpr_is_ff (thefpr, thefprlen) && !get_pubkey_byfprint (ctrl, pk, &keyblock, thefpr, thefprlen)) { - print_pubkey_info (ctrl, fp, pk); - if (keyblock) - print_card_key_info (fp, keyblock); + print_key_info (ctrl, fp, 0, pk, 0); + print_card_key_info (fp, keyblock); } else tty_fprintf (fp, "[none]\n"); diff --git a/g10/delkey.c b/g10/delkey.c index 276080532..6f816080e 100644 --- a/g10/delkey.c +++ b/g10/delkey.c @@ -135,10 +135,7 @@ do_delete_key (ctrl_t ctrl, const char *username, int secret, int force, } else { - if (secret) - print_seckey_info (ctrl, pk); - else - print_pubkey_info (ctrl, NULL, pk ); + print_key_info (ctrl, NULL, 0, pk, secret); tty_printf( "\n" ); yes = cpr_get_answer_is_yes diff --git a/g10/keyedit.c b/g10/keyedit.c index 7f4c5a509..cb05914e1 100644 --- a/g10/keyedit.c +++ b/g10/keyedit.c @@ -4356,7 +4356,7 @@ menu_addrevoker (ctrl_t ctrl, kbnode_t pub_keyblock, int sensitive) continue; } - print_pubkey_info (ctrl, NULL, revoker_pk); + print_key_info (ctrl, NULL, 0, revoker_pk, 0); print_fingerprint (ctrl, NULL, revoker_pk, 2); tty_printf ("\n"); diff --git a/g10/keylist.c b/g10/keylist.c index 8d5b2e0b9..50625d0b7 100644 --- a/g10/keylist.c +++ b/g10/keylist.c @@ -165,43 +165,15 @@ secret_key_list (ctrl_t ctrl, strlist_t list) list_one (ctrl, list, 1, 0); } -char * -format_seckey_info (ctrl_t ctrl, PKT_public_key *pk) -{ - u32 keyid[2]; - char *p; - char pkstrbuf[PUBKEY_STRING_SIZE]; - char *info; - - keyid_from_pk (pk, keyid); - p = get_user_id_native (ctrl, keyid); - - info = xtryasprintf ("sec %s/%s %s %s", - pubkey_string (pk, pkstrbuf, sizeof pkstrbuf), - keystr (keyid), datestr_from_pk (pk), p); - - xfree (p); - - return info; -} - -void -print_seckey_info (ctrl_t ctrl, PKT_public_key *pk) -{ - char *p = format_seckey_info (ctrl, pk); - tty_printf ("\n%s\n", p); - xfree (p); -} - -/* Print information about the public key. With FP passed as NULL, - the tty output interface is used, otherwise output is directed to - the given stream. */ -void -print_pubkey_info (ctrl_t ctrl, estream_t fp, PKT_public_key *pk) + +/* Helper for print_key_info and print_key_info_log. */ +static char * +format_key_info (ctrl_t ctrl, PKT_public_key *pk, int secret) { u32 keyid[2]; char *p; char pkstrbuf[PUBKEY_STRING_SIZE]; + char *result; keyid_from_pk (pk, keyid); @@ -212,13 +184,59 @@ print_pubkey_info (ctrl_t ctrl, estream_t fp, PKT_public_key *pk) else p = get_user_id_native (ctrl, keyid); - if (!fp) - tty_printf ("\n"); - tty_fprintf (fp, "%s %s/%s %s %s\n", - pk->flags.primary? "pub":"sub", - pubkey_string (pk, pkstrbuf, sizeof pkstrbuf), - keystr (keyid), datestr_from_pk (pk), p); + result = xtryasprintf ("%s %s/%s %s %s", + secret? (pk->flags.primary? "sec":"ssb") + /* */ : (pk->flags.primary? "pub":"sub"), + pubkey_string (pk, pkstrbuf, sizeof pkstrbuf), + keystr (keyid), datestr_from_pk (pk), p); xfree (p); + return result; +} + + +/* Print basic information about a public or secret key. With FP + * passed as NULL, the tty output interface is used, otherwise output + * is directed to the given stream. INDENT gives the requested + * indentation; if that is a negative value indentation is suppressed + * for the first line. SECRET tells that the PK has a secret part. + * FIXME: This is similar in use to print_key_line and thus both + * functions should eventually be united. + */ +void +print_key_info (ctrl_t ctrl, estream_t fp, + int indent, PKT_public_key *pk, int secret) +{ + int indentabs = indent >= 0? indent : -indent; + char *info; + + /* Note: Negative values for INDENT are not yet needed. */ + + info = format_key_info (ctrl, pk, secret); + + if (!fp && indent >= 0) + tty_printf ("\n"); /* (Backward compatibility to old code) */ + tty_fprintf (fp, "%*s%s\n", indentabs, "", + info? info : "[Ooops - out of core]"); + + xfree (info); +} + + +/* Same as print_key_info put print using the log functions at + * LOGLEVEL. */ +void +print_key_info_log (ctrl_t ctrl, int loglevel, + int indent, PKT_public_key *pk, int secret) +{ + int indentabs = indent >= 0? indent : -indent; + char *info; + + info = format_key_info (ctrl, pk, secret); + + log_log (loglevel, "%*s%s\n", indentabs, "", + info? info : "[Ooops - out of core]"); + + xfree (info); } diff --git a/g10/main.h b/g10/main.h index 34a932b16..134d8b950 100644 --- a/g10/main.h +++ b/g10/main.h @@ -470,9 +470,10 @@ void show_keyserver_url(PKT_signature *sig,int indent,int mode); void show_notation(PKT_signature *sig,int indent,int mode,int which); void dump_attribs (const PKT_user_id *uid, PKT_public_key *pk); void set_attrib_fd(int fd); -char *format_seckey_info (ctrl_t ctrl, PKT_public_key *pk); -void print_seckey_info (ctrl_t ctrl, PKT_public_key *pk); -void print_pubkey_info (ctrl_t ctrl, estream_t fp, PKT_public_key *pk); +void print_key_info (ctrl_t ctrl, estream_t fp, int indent, + PKT_public_key *pk, int secret); +void print_key_info_log (ctrl_t ctrl, int loglevel, int indent, + PKT_public_key *pk, int secret); void print_card_key_info (estream_t fp, KBNODE keyblock); void print_key_line (ctrl_t ctrl, estream_t fp, PKT_public_key *pk, int secret); diff --git a/g10/pkclist.c b/g10/pkclist.c index 46258bf85..20eb00cea 100644 --- a/g10/pkclist.c +++ b/g10/pkclist.c @@ -475,7 +475,7 @@ do_we_trust_pre (ctrl_t ctrl, PKT_public_key *pk, unsigned int trustlevel ) if( !opt.batch && !rc ) { - print_pubkey_info (ctrl, NULL,pk); + print_key_info (ctrl, NULL, 0, pk, 0); print_fingerprint (ctrl, NULL, pk, 2); tty_printf("\n"); diff --git a/g10/revoke.c b/g10/revoke.c index e63060cb9..0a93c31f9 100644 --- a/g10/revoke.c +++ b/g10/revoke.c @@ -305,11 +305,11 @@ gen_desig_revoke (ctrl_t ctrl, const char *uname, strlist_t locusr) any = 1; - print_pubkey_info (ctrl, NULL, pk); + print_key_info (ctrl, NULL, 0, pk, 0); tty_printf ("\n"); tty_printf (_("To be revoked by:\n")); - print_seckey_info (ctrl, pk2); + print_key_info (ctrl, NULL, 0, pk2, 1); if(pk->revkey[i].class&0x40) tty_printf(_("(This is a sensitive revocation key)\n")); @@ -669,30 +669,26 @@ gen_revoke (ctrl_t ctrl, const char *uname) rc = keydb_search (kdbhd, &desc, 1, NULL); if (gpg_err_code (rc) == GPG_ERR_NOT_FOUND) - /* Not ambiguous. */ { + /* Not ambiguous. */ } else if (rc == 0) - /* Ambiguous. */ { - char *info; - + /* Ambiguous. */ /* TRANSLATORS: The %s prints a key specification which for example has been given at the command line. Several lines lines with secret key infos are printed after this message. */ log_error (_("'%s' matches multiple secret keys:\n"), uname); - info = format_seckey_info (ctrl, keyblock->pkt->pkt.public_key); - log_error (" %s\n", info); - xfree (info); + print_key_info_log (ctrl, GPGRT_LOGLVL_ERROR, 2, + keyblock->pkt->pkt.public_key, 1); release_kbnode (keyblock); rc = keydb_get_keyblock (kdbhd, &keyblock); while (! rc) { - info = format_seckey_info (ctrl, keyblock->pkt->pkt.public_key); - log_info (" %s\n", info); - xfree (info); + print_key_info_log (ctrl, GPGRT_LOGLVL_INFO, 2, + keyblock->pkt->pkt.public_key, 1); release_kbnode (keyblock); keyblock = NULL; @@ -726,7 +722,7 @@ gen_revoke (ctrl_t ctrl, const char *uname) } keyid_from_pk (psk, keyid ); - print_seckey_info (ctrl, psk); + print_key_info (ctrl, NULL, 0, psk, 1); tty_printf("\n"); if (!cpr_get_answer_is_yes ("gen_revoke.okay",