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 <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 "run-keylist"
34 #include "run-support.h"
43 fputs ("usage: " PGM " [options] [USERID]\n\n"
45 " --verbose run in verbose mode\n"
46 " --openpgp use the OpenPGP protocol (default)\n"
47 " --cms use the CMS protocol\n"
48 " --secret list only secret keys\n"
49 " --local use GPGME_KEYLIST_MODE_LOCAL\n"
50 " --extern use GPGME_KEYLIST_MODE_EXTERN\n"
51 " --sigs use GPGME_KEYLIST_MODE_SIGS\n"
52 " --sig-notations use GPGME_KEYLIST_MODE_SIG_NOTATIONS\n"
53 " --ephemeral use GPGME_KEYLIST_MODE_EPHEMERAL\n"
54 " --validate use GPGME_KEYLIST_MODE_VALIDATE\n"
55 " --import import all keys\n"
62 main (int argc, char **argv)
67 gpgme_keylist_mode_t mode = 0;
69 gpgme_keylist_result_t result;
71 gpgme_key_t keyarray[100];
73 gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
79 while (argc && last_argc != argc )
82 if (!strcmp (*argv, "--"))
87 else if (!strcmp (*argv, "--help"))
89 else if (!strcmp (*argv, "--verbose"))
94 else if (!strcmp (*argv, "--openpgp"))
96 protocol = GPGME_PROTOCOL_OpenPGP;
99 else if (!strcmp (*argv, "--cms"))
101 protocol = GPGME_PROTOCOL_CMS;
104 else if (!strcmp (*argv, "--secret"))
109 else if (!strcmp (*argv, "--local"))
111 mode |= GPGME_KEYLIST_MODE_LOCAL;
114 else if (!strcmp (*argv, "--extern"))
116 mode |= GPGME_KEYLIST_MODE_EXTERN;
119 else if (!strcmp (*argv, "--sigs"))
121 mode |= GPGME_KEYLIST_MODE_SIGS;
124 else if (!strcmp (*argv, "--sig-notations"))
126 mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
129 else if (!strcmp (*argv, "--ephemeral"))
131 mode |= GPGME_KEYLIST_MODE_EPHEMERAL;
134 else if (!strcmp (*argv, "--validate"))
136 mode |= GPGME_KEYLIST_MODE_VALIDATE;
139 else if (!strcmp (*argv, "--import"))
144 else if (!strncmp (*argv, "--", 2))
152 init_gpgme (protocol);
154 err = gpgme_new (&ctx);
156 gpgme_set_protocol (ctx, protocol);
158 gpgme_set_keylist_mode (ctx, mode);
160 err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, only_secret);
163 while (!(err = gpgme_op_keylist_next (ctx, &key)))
169 printf ("keyid : %s\n", key->subkeys?nonnull (key->subkeys->keyid):"?");
170 printf ("fpr : %s\n", key->subkeys?nonnull (key->subkeys->fpr):"?");
171 printf ("caps : %s%s%s%s\n",
172 key->can_encrypt? "e":"",
173 key->can_sign? "s":"",
174 key->can_certify? "c":"",
175 key->can_authenticate? "a":"");
176 printf ("flags :%s%s%s%s%s%s\n",
177 key->secret? " secret":"",
178 key->revoked? " revoked":"",
179 key->expired? " expired":"",
180 key->disabled? " disabled":"",
181 key->invalid? " invalid":"",
182 key->is_qualified? " qualifid":"");
183 for (nuids=0, uid=key->uids; uid; uid = uid->next, nuids++)
185 printf ("userid %d: %s\n", nuids, nonnull(uid->uid));
186 printf ("valid %d: %s\n", nuids,
187 uid->validity == GPGME_VALIDITY_UNKNOWN? "unknown":
188 uid->validity == GPGME_VALIDITY_UNDEFINED? "undefined":
189 uid->validity == GPGME_VALIDITY_NEVER? "never":
190 uid->validity == GPGME_VALIDITY_MARGINAL? "marginal":
191 uid->validity == GPGME_VALIDITY_FULL? "full":
192 uid->validity == GPGME_VALIDITY_ULTIMATE? "ultimate": "[?]");
199 if (keyidx < DIM (keyarray)-1)
200 keyarray[keyidx++] = key;
203 fprintf (stderr, PGM": too many keys in import mode"
204 "- skipping this key\n");
205 gpgme_key_unref (key);
209 gpgme_key_unref (key);
211 if (gpgme_err_code (err) != GPG_ERR_EOF)
213 err = gpgme_op_keylist_end (ctx);
215 keyarray[keyidx] = NULL;
217 result = gpgme_op_keylist_result (ctx);
218 if (result->truncated)
220 fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
226 gpgme_import_result_t impres;
228 err = gpgme_op_import_keys (ctx, keyarray);
230 impres = gpgme_op_import_result (ctx);
233 fprintf (stderr, PGM ": no import result returned\n");
236 print_import_result (impres);
239 for (keyidx=0; keyarray[keyidx]; keyidx++)
240 gpgme_key_unref (keyarray[keyidx]);