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]\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 " --local use GPGME_KEYLIST_MODE_LOCAL\n"
51 " --extern use GPGME_KEYLIST_MODE_EXTERN\n"
52 " --sigs use GPGME_KEYLIST_MODE_SIGS\n"
53 " --tofu use GPGME_KEYLIST_MODE_TOFU\n"
54 " --sig-notations use GPGME_KEYLIST_MODE_SIG_NOTATIONS\n"
55 " --ephemeral use GPGME_KEYLIST_MODE_EPHEMERAL\n"
56 " --validate use GPGME_KEYLIST_MODE_VALIDATE\n"
57 " --import import all keys\n"
58 " --offline use offline mode\n"
59 " --require-gnupg required at least the given GnuPG version\n"
66 isotimestr (unsigned long value)
69 static char buffer[25+5];
77 snprintf (buffer, sizeof buffer, "%04d-%02d-%02d %02d:%02d:%02d",
78 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
79 tp->tm_hour, tp->tm_min, tp->tm_sec);
86 main (int argc, char **argv)
91 gpgme_keylist_mode_t mode = 0;
93 gpgme_subkey_t subkey;
94 gpgme_keylist_result_t result;
96 gpgme_key_t keyarray[100];
98 gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
105 while (argc && last_argc != argc )
108 if (!strcmp (*argv, "--"))
113 else if (!strcmp (*argv, "--help"))
115 else if (!strcmp (*argv, "--verbose"))
120 else if (!strcmp (*argv, "--openpgp"))
122 protocol = GPGME_PROTOCOL_OpenPGP;
125 else if (!strcmp (*argv, "--cms"))
127 protocol = GPGME_PROTOCOL_CMS;
130 else if (!strcmp (*argv, "--secret"))
135 else if (!strcmp (*argv, "--local"))
137 mode |= GPGME_KEYLIST_MODE_LOCAL;
140 else if (!strcmp (*argv, "--extern"))
142 mode |= GPGME_KEYLIST_MODE_EXTERN;
145 else if (!strcmp (*argv, "--tofu"))
147 mode |= GPGME_KEYLIST_MODE_WITH_TOFU;
150 else if (!strcmp (*argv, "--sigs"))
152 mode |= GPGME_KEYLIST_MODE_SIGS;
155 else if (!strcmp (*argv, "--sig-notations"))
157 mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
160 else if (!strcmp (*argv, "--ephemeral"))
162 mode |= GPGME_KEYLIST_MODE_EPHEMERAL;
165 else if (!strcmp (*argv, "--validate"))
167 mode |= GPGME_KEYLIST_MODE_VALIDATE;
170 else if (!strcmp (*argv, "--import"))
175 else if (!strcmp (*argv, "--offline"))
180 else if (!strcmp (*argv, "--require-gnupg"))
185 gpgme_set_global_flag ("require-gnupg", *argv);
188 else if (!strncmp (*argv, "--", 2))
195 init_gpgme (protocol);
197 err = gpgme_new (&ctx);
199 gpgme_set_protocol (ctx, protocol);
201 gpgme_set_keylist_mode (ctx, mode);
203 gpgme_set_offline (ctx, offline);
205 err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, only_secret);
208 while (!(err = gpgme_op_keylist_next (ctx, &key)))
211 gpgme_tofu_info_t ti;
215 printf ("keyid : %s\n", key->subkeys?nonnull (key->subkeys->keyid):"?");
216 printf ("fpr : %s\n", key->subkeys?nonnull (key->subkeys->fpr):"?");
217 if (key->subkeys && key->subkeys->keygrip)
218 printf ("grip : %s\n", key->subkeys->keygrip);
219 if (key->subkeys && key->subkeys->curve)
220 printf ("curve : %s\n", key->subkeys->curve);
221 printf ("caps : %s%s%s%s\n",
222 key->can_encrypt? "e":"",
223 key->can_sign? "s":"",
224 key->can_certify? "c":"",
225 key->can_authenticate? "a":"");
226 printf ("flags :%s%s%s%s%s%s%s\n",
227 key->secret? " secret":"",
228 key->revoked? " revoked":"",
229 key->expired? " expired":"",
230 key->disabled? " disabled":"",
231 key->invalid? " invalid":"",
232 key->is_qualified? " qualifid":"",
233 key->subkeys && key->subkeys->is_cardkey? " cardkey":"");
235 subkey = key->subkeys;
237 subkey = subkey->next;
238 for (nsub=1; subkey; subkey = subkey->next, nsub++)
240 printf ("fpr %2d: %s\n", nsub, nonnull (subkey->fpr));
242 printf ("grip %2d: %s\n", nsub, subkey->keygrip);
244 printf ("curve %2d: %s\n", nsub, subkey->curve);
245 printf ("caps %2d: %s%s%s%s\n",
247 subkey->can_encrypt? "e":"",
248 subkey->can_sign? "s":"",
249 subkey->can_certify? "c":"",
250 subkey->can_authenticate? "a":"");
251 printf ("flags %2d:%s%s%s%s%s%s%s\n",
253 subkey->secret? " secret":"",
254 subkey->revoked? " revoked":"",
255 subkey->expired? " expired":"",
256 subkey->disabled? " disabled":"",
257 subkey->invalid? " invalid":"",
258 subkey->is_qualified? " qualifid":"",
259 subkey->is_cardkey? " cardkey":"");
261 for (nuids=0, uid=key->uids; uid; uid = uid->next, nuids++)
263 printf ("userid %d: %s\n", nuids, nonnull(uid->uid));
264 printf (" mbox: %s\n", nonnull(uid->address));
265 if (uid->email && uid->email != uid->address)
266 printf (" email: %s\n", uid->email);
268 printf (" name: %s\n", uid->name);
270 printf (" cmmnt: %s\n", uid->comment);
271 printf (" valid: %s\n",
272 uid->validity == GPGME_VALIDITY_UNKNOWN? "unknown":
273 uid->validity == GPGME_VALIDITY_UNDEFINED? "undefined":
274 uid->validity == GPGME_VALIDITY_NEVER? "never":
275 uid->validity == GPGME_VALIDITY_MARGINAL? "marginal":
276 uid->validity == GPGME_VALIDITY_FULL? "full":
277 uid->validity == GPGME_VALIDITY_ULTIMATE? "ultimate": "[?]");
278 if ((ti = uid->tofu))
280 printf (" tofu: %u (%s)\n", ti->validity,
281 ti->validity == 0? "conflict" :
282 ti->validity == 1? "no history" :
283 ti->validity == 2? "little history" :
284 ti->validity == 3? "enough history" :
285 ti->validity == 4? "lot of history" : "?");
286 printf (" policy: %u (%s)\n", ti->policy,
287 ti->policy == GPGME_TOFU_POLICY_NONE? "none" :
288 ti->policy == GPGME_TOFU_POLICY_AUTO? "auto" :
289 ti->policy == GPGME_TOFU_POLICY_GOOD? "good" :
290 ti->policy == GPGME_TOFU_POLICY_UNKNOWN? "unknown" :
291 ti->policy == GPGME_TOFU_POLICY_BAD? "bad" :
292 ti->policy == GPGME_TOFU_POLICY_ASK? "ask" : "?");
293 printf (" nsigs: %hu\n", ti->signcount);
294 printf (" first: %s\n", isotimestr (ti->signfirst));
295 printf (" last: %s\n", isotimestr (ti->signlast));
296 printf (" nencr: %hu\n", ti->encrcount);
297 printf (" first: %s\n", isotimestr (ti->encrfirst));
298 printf (" last: %s\n", isotimestr (ti->encrlast));
306 if (keyidx < DIM (keyarray)-1)
307 keyarray[keyidx++] = key;
310 fprintf (stderr, PGM": too many keys in import mode"
311 "- skipping this key\n");
312 gpgme_key_unref (key);
316 gpgme_key_unref (key);
318 if (gpgme_err_code (err) != GPG_ERR_EOF)
320 err = gpgme_op_keylist_end (ctx);
322 keyarray[keyidx] = NULL;
324 result = gpgme_op_keylist_result (ctx);
325 if (result->truncated)
327 fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
333 gpgme_import_result_t impres;
335 err = gpgme_op_import_keys (ctx, keyarray);
337 impres = gpgme_op_import_result (ctx);
340 fprintf (stderr, PGM ": no import result returned\n");
343 print_import_result (impres);
346 for (keyidx=0; keyarray[keyidx]; keyidx++)
347 gpgme_key_unref (keyarray[keyidx]);