1 /* pgp-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 <http://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. */
32 #define PGM "pgp-keylist"
34 #include "t-support.h"
41 nonnull (const char *s)
43 return s? s :"[none]";
48 print_import_result (gpgme_import_result_t r)
50 gpgme_import_status_t st;
52 printf ("key import results:\n"
60 " new signatures: %d\n"
61 " new revocations: %d\n"
63 " secret imported: %d\n"
64 " secret unchanged: %d\n"
65 " skipped new keys: %d\n"
66 " not imported: %d\n",
82 for (st=r->imports; st; st = st->next)
84 printf (" fpr: %s err: %d (%s) status:", nonnull (st->fpr),
85 st->result, gpg_strerror (st->result));
86 if (st->status & GPGME_IMPORT_NEW)
87 fputs (" new", stdout);
88 if (st->status & GPGME_IMPORT_UID)
89 fputs (" uid", stdout);
90 if (st->status & GPGME_IMPORT_SIG)
91 fputs (" sig", stdout);
92 if (st->status & GPGME_IMPORT_SUBKEY)
93 fputs (" subkey", stdout);
94 if (st->status & GPGME_IMPORT_SECRET)
95 fputs (" secret", stdout);
104 fputs ("usage: " PGM " [options] [USERID]\n\n"
106 " --verbose run in verbose mode\n"
107 " --local use GPGME_KEYLIST_MODE_LOCAL\n"
108 " --extern use GPGME_KEYLIST_MODE_EXTERN\n"
109 " --sigs use GPGME_KEYLIST_MODE_SIGS\n"
110 " --sig-notations use GPGME_KEYLIST_MODE_SIG_NOTATIONS\n"
111 " --ephemeral use GPGME_KEYLIST_MODE_EPHEMERAL\n"
112 " --validate use GPGME_KEYLIST_MODE_VALIDATE\n"
113 " --import import all keys\n"
119 main (int argc, char **argv)
124 gpgme_keylist_mode_t mode = 0;
126 gpgme_keylist_result_t result;
128 gpgme_key_t keyarray[100];
134 while (argc && last_argc != argc )
137 if (!strcmp (*argv, "--"))
142 else if (!strcmp (*argv, "--help"))
144 else if (!strcmp (*argv, "--verbose"))
149 else if (!strcmp (*argv, "--local"))
151 mode |= GPGME_KEYLIST_MODE_LOCAL;
154 else if (!strcmp (*argv, "--extern"))
156 mode |= GPGME_KEYLIST_MODE_EXTERN;
159 else if (!strcmp (*argv, "--sigs"))
161 mode |= GPGME_KEYLIST_MODE_SIGS;
164 else if (!strcmp (*argv, "--sig-notations"))
166 mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
169 else if (!strcmp (*argv, "--ephemeral"))
171 mode |= GPGME_KEYLIST_MODE_EPHEMERAL;
174 else if (!strcmp (*argv, "--validate"))
176 mode |= GPGME_KEYLIST_MODE_VALIDATE;
179 else if (!strcmp (*argv, "--import"))
184 else if (!strncmp (*argv, "--", 2))
192 init_gpgme (GPGME_PROTOCOL_OpenPGP);
194 err = gpgme_new (&ctx);
196 gpgme_set_protocol (ctx, GPGME_PROTOCOL_OpenPGP);
198 gpgme_set_keylist_mode (ctx, mode);
200 err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, 0);
203 while (!(err = gpgme_op_keylist_next (ctx, &key)))
209 printf ("keyid : %s\n", key->subkeys?nonnull (key->subkeys->keyid):"?");
210 printf ("fpr : %s\n", key->subkeys?nonnull (key->subkeys->fpr):"?");
211 printf ("caps : %s%s%s%s\n",
212 key->can_encrypt? "e":"",
213 key->can_sign? "s":"",
214 key->can_certify? "c":"",
215 key->can_authenticate? "a":"");
216 printf ("flags :%s%s%s%s%s%s\n",
217 key->secret? " secret":"",
218 key->revoked? " revoked":"",
219 key->expired? " expired":"",
220 key->disabled? " disabled":"",
221 key->invalid? " invalid":"",
222 key->is_qualified? " qualifid":"");
223 for (nuids=0, uid=key->uids; uid; uid = uid->next, nuids++)
225 printf ("userid %d: %s\n", nuids, nonnull(uid->uid));
226 printf ("valid %d: %s\n", nuids,
227 uid->validity == GPGME_VALIDITY_UNKNOWN? "unknown":
228 uid->validity == GPGME_VALIDITY_UNDEFINED? "undefined":
229 uid->validity == GPGME_VALIDITY_NEVER? "never":
230 uid->validity == GPGME_VALIDITY_MARGINAL? "marginal":
231 uid->validity == GPGME_VALIDITY_FULL? "full":
232 uid->validity == GPGME_VALIDITY_ULTIMATE? "ultimate": "[?]");
239 if (keyidx < DIM (keyarray)-1)
240 keyarray[keyidx++] = key;
243 fprintf (stderr, PGM": too many keys in import mode"
244 "- skipping this key\n");
245 gpgme_key_unref (key);
249 gpgme_key_unref (key);
251 if (gpg_err_code (err) != GPG_ERR_EOF)
253 err = gpgme_op_keylist_end (ctx);
255 keyarray[keyidx] = NULL;
257 result = gpgme_op_keylist_result (ctx);
258 if (result->truncated)
260 fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
266 gpgme_import_result_t impres;
268 err = gpgme_op_import_keys (ctx, keyarray);
270 impres = gpgme_op_import_result (ctx);
273 fprintf (stderr, PGM ": no import result returned\n");
276 print_import_result (impres);
279 for (keyidx=0; keyarray[keyidx]; keyidx++)
280 gpgme_key_unref (keyarray[keyidx]);