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 " --local use GPGME_KEYLIST_MODE_LOCAL\n"
49 " --extern use GPGME_KEYLIST_MODE_EXTERN\n"
50 " --sigs use GPGME_KEYLIST_MODE_SIGS\n"
51 " --sig-notations use GPGME_KEYLIST_MODE_SIG_NOTATIONS\n"
52 " --ephemeral use GPGME_KEYLIST_MODE_EPHEMERAL\n"
53 " --validate use GPGME_KEYLIST_MODE_VALIDATE\n"
54 " --import import all keys\n"
61 main (int argc, char **argv)
66 gpgme_keylist_mode_t mode = 0;
68 gpgme_keylist_result_t result;
70 gpgme_key_t keyarray[100];
72 gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
77 while (argc && last_argc != argc )
80 if (!strcmp (*argv, "--"))
85 else if (!strcmp (*argv, "--help"))
87 else if (!strcmp (*argv, "--verbose"))
92 else if (!strcmp (*argv, "--openpgp"))
94 protocol = GPGME_PROTOCOL_OpenPGP;
97 else if (!strcmp (*argv, "--cms"))
99 protocol = GPGME_PROTOCOL_CMS;
102 else if (!strcmp (*argv, "--local"))
104 mode |= GPGME_KEYLIST_MODE_LOCAL;
107 else if (!strcmp (*argv, "--extern"))
109 mode |= GPGME_KEYLIST_MODE_EXTERN;
112 else if (!strcmp (*argv, "--sigs"))
114 mode |= GPGME_KEYLIST_MODE_SIGS;
117 else if (!strcmp (*argv, "--sig-notations"))
119 mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
122 else if (!strcmp (*argv, "--ephemeral"))
124 mode |= GPGME_KEYLIST_MODE_EPHEMERAL;
127 else if (!strcmp (*argv, "--validate"))
129 mode |= GPGME_KEYLIST_MODE_VALIDATE;
132 else if (!strcmp (*argv, "--import"))
137 else if (!strncmp (*argv, "--", 2))
145 init_gpgme (protocol);
147 err = gpgme_new (&ctx);
149 gpgme_set_protocol (ctx, protocol);
151 gpgme_set_keylist_mode (ctx, mode);
153 err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, 0);
156 while (!(err = gpgme_op_keylist_next (ctx, &key)))
162 printf ("keyid : %s\n", key->subkeys?nonnull (key->subkeys->keyid):"?");
163 printf ("fpr : %s\n", key->subkeys?nonnull (key->subkeys->fpr):"?");
164 printf ("caps : %s%s%s%s\n",
165 key->can_encrypt? "e":"",
166 key->can_sign? "s":"",
167 key->can_certify? "c":"",
168 key->can_authenticate? "a":"");
169 printf ("flags :%s%s%s%s%s%s\n",
170 key->secret? " secret":"",
171 key->revoked? " revoked":"",
172 key->expired? " expired":"",
173 key->disabled? " disabled":"",
174 key->invalid? " invalid":"",
175 key->is_qualified? " qualifid":"");
176 for (nuids=0, uid=key->uids; uid; uid = uid->next, nuids++)
178 printf ("userid %d: %s\n", nuids, nonnull(uid->uid));
179 printf ("valid %d: %s\n", nuids,
180 uid->validity == GPGME_VALIDITY_UNKNOWN? "unknown":
181 uid->validity == GPGME_VALIDITY_UNDEFINED? "undefined":
182 uid->validity == GPGME_VALIDITY_NEVER? "never":
183 uid->validity == GPGME_VALIDITY_MARGINAL? "marginal":
184 uid->validity == GPGME_VALIDITY_FULL? "full":
185 uid->validity == GPGME_VALIDITY_ULTIMATE? "ultimate": "[?]");
192 if (keyidx < DIM (keyarray)-1)
193 keyarray[keyidx++] = key;
196 fprintf (stderr, PGM": too many keys in import mode"
197 "- skipping this key\n");
198 gpgme_key_unref (key);
202 gpgme_key_unref (key);
204 if (gpg_err_code (err) != GPG_ERR_EOF)
206 err = gpgme_op_keylist_end (ctx);
208 keyarray[keyidx] = NULL;
210 result = gpgme_op_keylist_result (ctx);
211 if (result->truncated)
213 fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
219 gpgme_import_result_t impres;
221 err = gpgme_op_import_keys (ctx, keyarray);
223 impres = gpgme_op_import_result (ctx);
226 fprintf (stderr, PGM ": no import result returned\n");
229 print_import_result (impres);
232 for (keyidx=0; keyarray[keyidx]; keyidx++)
233 gpgme_key_unref (keyarray[keyidx]);