1 /* run-keylist.c - Helper to show a key listing.
2 Copyright (C) 2008, 2009 g10 Code GmbH
4 This file is part of GPGME.
6 GPGME is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of
9 the License, or (at your option) any later version.
11 GPGME is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with this program; if not, see <https://www.gnu.org/licenses/>.
20 /* We need to include config.h so that we know whether we are building
21 with large file system (LFS) support. */
33 #define PGM "run-keylist"
35 #include "run-support.h"
44 fputs ("usage: " PGM " [options] [USERID_or_FILE]\n\n"
46 " --verbose run in verbose mode\n"
47 " --openpgp use the OpenPGP protocol (default)\n"
48 " --cms use the CMS protocol\n"
49 " --secret list only secret keys\n"
50 " --with-secret list pubkeys with secret info filled\n"
51 " --local use GPGME_KEYLIST_MODE_LOCAL\n"
52 " --extern use GPGME_KEYLIST_MODE_EXTERN\n"
53 " --sigs use GPGME_KEYLIST_MODE_SIGS\n"
54 " --tofu use GPGME_KEYLIST_MODE_TOFU\n"
55 " --sig-notations use GPGME_KEYLIST_MODE_SIG_NOTATIONS\n"
56 " --ephemeral use GPGME_KEYLIST_MODE_EPHEMERAL\n"
57 " --validate use GPGME_KEYLIST_MODE_VALIDATE\n"
58 " --import import all keys\n"
59 " --offline use offline mode\n"
60 " --from-file list all keys in the given file\n"
61 " --from-wkd list key from a web key directory\n"
62 " --require-gnupg required at least the given GnuPG version\n"
69 isotimestr (unsigned long value)
72 static char buffer[25+5];
80 snprintf (buffer, sizeof buffer, "%04d-%02d-%02d %02d:%02d:%02d",
81 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
82 tp->tm_hour, tp->tm_min, tp->tm_sec);
89 main (int argc, char **argv)
94 gpgme_keylist_mode_t mode = 0;
96 gpgme_subkey_t subkey;
97 gpgme_keylist_result_t result;
99 gpgme_key_t keyarray[100];
101 gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
106 gpgme_data_t data = NULL;
112 while (argc && last_argc != argc )
115 if (!strcmp (*argv, "--"))
120 else if (!strcmp (*argv, "--help"))
122 else if (!strcmp (*argv, "--verbose"))
127 else if (!strcmp (*argv, "--openpgp"))
129 protocol = GPGME_PROTOCOL_OpenPGP;
132 else if (!strcmp (*argv, "--cms"))
134 protocol = GPGME_PROTOCOL_CMS;
137 else if (!strcmp (*argv, "--secret"))
142 else if (!strcmp (*argv, "--local"))
144 mode |= GPGME_KEYLIST_MODE_LOCAL;
147 else if (!strcmp (*argv, "--extern"))
149 mode |= GPGME_KEYLIST_MODE_EXTERN;
152 else if (!strcmp (*argv, "--tofu"))
154 mode |= GPGME_KEYLIST_MODE_WITH_TOFU;
157 else if (!strcmp (*argv, "--sigs"))
159 mode |= GPGME_KEYLIST_MODE_SIGS;
162 else if (!strcmp (*argv, "--sig-notations"))
164 mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
167 else if (!strcmp (*argv, "--ephemeral"))
169 mode |= GPGME_KEYLIST_MODE_EPHEMERAL;
172 else if (!strcmp (*argv, "--validate"))
174 mode |= GPGME_KEYLIST_MODE_VALIDATE;
177 else if (!strcmp (*argv, "--with-secret"))
179 mode |= GPGME_KEYLIST_MODE_WITH_SECRET;
182 else if (!strcmp (*argv, "--import"))
187 else if (!strcmp (*argv, "--offline"))
192 else if (!strcmp (*argv, "--from-file"))
197 else if (!strcmp (*argv, "--require-gnupg"))
202 gpgme_set_global_flag ("require-gnupg", *argv);
205 else if (!strcmp (*argv, "--from-wkd"))
208 mode |= GPGME_KEYLIST_MODE_LOCATE;
211 else if (!strncmp (*argv, "--", 2))
217 else if (from_file && !argc)
220 init_gpgme (protocol);
222 err = gpgme_new (&ctx);
224 gpgme_set_protocol (ctx, protocol);
226 gpgme_set_keylist_mode (ctx, mode);
228 gpgme_set_offline (ctx, offline);
232 err = gpgme_set_ctx_flag (ctx, "auto-key-locate",
233 "clear,nodefault,wkd");
239 err = gpgme_data_new_from_file (&data, *argv, 1);
242 err = gpgme_op_keylist_from_data_start (ctx, data, 0);
245 err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, only_secret);
248 while (!(err = gpgme_op_keylist_next (ctx, &key)))
251 gpgme_tofu_info_t ti;
257 printf ("keyid : %s\n", key->subkeys?nonnull (key->subkeys->keyid):"?");
258 printf ("fpr : %s\n", key->subkeys?nonnull (key->subkeys->fpr):"?");
259 if (key->subkeys && key->subkeys->keygrip)
260 printf ("grip : %s\n", key->subkeys->keygrip);
261 if (key->subkeys && key->subkeys->curve)
262 printf ("curve : %s\n", key->subkeys->curve);
263 printf ("caps : %s%s%s%s\n",
264 key->can_encrypt? "e":"",
265 key->can_sign? "s":"",
266 key->can_certify? "c":"",
267 key->can_authenticate? "a":"");
268 printf ("flags :%s%s%s%s%s%s%s%s\n",
269 key->secret? " secret":"",
270 key->revoked? " revoked":"",
271 key->expired? " expired":"",
272 key->disabled? " disabled":"",
273 key->invalid? " invalid":"",
274 key->is_qualified? " qualified":"",
275 key->subkeys && key->subkeys->is_de_vs? " de-vs":"",
276 key->subkeys && key->subkeys->is_cardkey? " cardkey":"");
277 printf ("upd : %lu (%u)\n", key->last_update, key->origin);
279 subkey = key->subkeys;
281 subkey = subkey->next;
282 for (nsub=1; subkey; subkey = subkey->next, nsub++)
284 printf ("fpr %2d: %s\n", nsub, nonnull (subkey->fpr));
286 printf ("grip %2d: %s\n", nsub, subkey->keygrip);
288 printf ("curve %2d: %s\n", nsub, subkey->curve);
289 printf ("caps %2d: %s%s%s%s\n",
291 subkey->can_encrypt? "e":"",
292 subkey->can_sign? "s":"",
293 subkey->can_certify? "c":"",
294 subkey->can_authenticate? "a":"");
295 printf ("flags %2d:%s%s%s%s%s%s%s%s\n",
297 subkey->secret? " secret":"",
298 subkey->revoked? " revoked":"",
299 subkey->expired? " expired":"",
300 subkey->disabled? " disabled":"",
301 subkey->invalid? " invalid":"",
302 subkey->is_qualified? " qualified":"",
303 subkey->is_de_vs? " de-vs":"",
304 subkey->is_cardkey? " cardkey":"");
306 for (nuids=0, uid=key->uids; uid; uid = uid->next, nuids++)
308 printf ("userid %d: %s\n", nuids, nonnull(uid->uid));
309 printf (" mbox: %s\n", nonnull(uid->address));
310 if (uid->email && uid->email != uid->address)
311 printf (" email: %s\n", uid->email);
313 printf (" name: %s\n", uid->name);
315 printf (" cmmnt: %s\n", uid->comment);
316 printf (" upd: %lu (%u)\n", uid->last_update, uid->origin);
317 printf (" valid: %s\n",
318 uid->validity == GPGME_VALIDITY_UNKNOWN? "unknown":
319 uid->validity == GPGME_VALIDITY_UNDEFINED? "undefined":
320 uid->validity == GPGME_VALIDITY_NEVER? "never":
321 uid->validity == GPGME_VALIDITY_MARGINAL? "marginal":
322 uid->validity == GPGME_VALIDITY_FULL? "full":
323 uid->validity == GPGME_VALIDITY_ULTIMATE? "ultimate": "[?]");
324 if ((ti = uid->tofu))
326 printf (" tofu: %u (%s)\n", ti->validity,
327 ti->validity == 0? "conflict" :
328 ti->validity == 1? "no history" :
329 ti->validity == 2? "little history" :
330 ti->validity == 3? "enough history" :
331 ti->validity == 4? "lot of history" : "?");
332 printf (" policy: %u (%s)\n", ti->policy,
333 ti->policy == GPGME_TOFU_POLICY_NONE? "none" :
334 ti->policy == GPGME_TOFU_POLICY_AUTO? "auto" :
335 ti->policy == GPGME_TOFU_POLICY_GOOD? "good" :
336 ti->policy == GPGME_TOFU_POLICY_UNKNOWN? "unknown" :
337 ti->policy == GPGME_TOFU_POLICY_BAD? "bad" :
338 ti->policy == GPGME_TOFU_POLICY_ASK? "ask" : "?");
339 printf (" nsigs: %hu\n", ti->signcount);
340 printf (" first: %s\n", isotimestr (ti->signfirst));
341 printf (" last: %s\n", isotimestr (ti->signlast));
342 printf (" nencr: %hu\n", ti->encrcount);
343 printf (" first: %s\n", isotimestr (ti->encrfirst));
344 printf (" last: %s\n", isotimestr (ti->encrlast));
346 for (nsigs=0, ks=uid->signatures; ks; ks = ks->next, nsigs++)
348 printf ("signature %d: %s\n", nsigs, nonnull (ks->uid));
349 printf (" keyid: %s\n", nonnull (ks->keyid));
350 printf (" created: %s\n", isotimestr(ks->timestamp));
351 printf (" expires: %s\n", isotimestr(ks->expires));
352 printf (" class: %x\n", ks->sig_class);
360 if (keyidx < DIM (keyarray)-1)
361 keyarray[keyidx++] = key;
364 fprintf (stderr, PGM": too many keys in import mode"
365 "- skipping this key\n");
366 gpgme_key_unref (key);
370 gpgme_key_unref (key);
372 if (gpgme_err_code (err) != GPG_ERR_EOF)
374 err = gpgme_op_keylist_end (ctx);
376 keyarray[keyidx] = NULL;
377 gpgme_data_release (data);
379 result = gpgme_op_keylist_result (ctx);
380 if (result->truncated)
382 fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
388 gpgme_import_result_t impres;
390 err = gpgme_op_import_keys (ctx, keyarray);
392 impres = gpgme_op_import_result (ctx);
395 fprintf (stderr, PGM ": no import result returned\n");
398 print_import_result (impres);
401 for (keyidx=0; keyarray[keyidx]; keyidx++)
402 gpgme_key_unref (keyarray[keyidx]);