Line data Source code
1 : /* run-keylist.c - Helper to show a key listing.
2 : Copyright (C) 2008, 2009 g10 Code GmbH
3 :
4 : This file is part of GPGME.
5 :
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.
10 :
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.
15 :
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/>.
18 : */
19 :
20 : /* We need to include config.h so that we know whether we are building
21 : with large file system (LFS) support. */
22 : #ifdef HAVE_CONFIG_H
23 : #include <config.h>
24 : #endif
25 :
26 : #include <stdlib.h>
27 : #include <stdio.h>
28 : #include <string.h>
29 :
30 : #include <gpgme.h>
31 :
32 : #define PGM "run-keylist"
33 :
34 : #include "run-support.h"
35 :
36 :
37 : static int verbose;
38 :
39 :
40 : static int
41 0 : show_usage (int ex)
42 : {
43 0 : fputs ("usage: " PGM " [options] [USERID]\n\n"
44 : "Options:\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"
56 : " --offline use offline mode\n"
57 : , stderr);
58 0 : exit (ex);
59 : }
60 :
61 :
62 : int
63 0 : main (int argc, char **argv)
64 : {
65 0 : int last_argc = -1;
66 : gpgme_error_t err;
67 : gpgme_ctx_t ctx;
68 0 : gpgme_keylist_mode_t mode = 0;
69 : gpgme_key_t key;
70 : gpgme_keylist_result_t result;
71 0 : int import = 0;
72 : gpgme_key_t keyarray[100];
73 0 : int keyidx = 0;
74 0 : gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
75 0 : int only_secret = 0;
76 0 : int offline = 0;
77 :
78 0 : if (argc)
79 0 : { argc--; argv++; }
80 :
81 0 : while (argc && last_argc != argc )
82 : {
83 0 : last_argc = argc;
84 0 : if (!strcmp (*argv, "--"))
85 : {
86 0 : argc--; argv++;
87 0 : break;
88 : }
89 0 : else if (!strcmp (*argv, "--help"))
90 0 : show_usage (0);
91 0 : else if (!strcmp (*argv, "--verbose"))
92 : {
93 0 : verbose = 1;
94 0 : argc--; argv++;
95 : }
96 0 : else if (!strcmp (*argv, "--openpgp"))
97 : {
98 0 : protocol = GPGME_PROTOCOL_OpenPGP;
99 0 : argc--; argv++;
100 : }
101 0 : else if (!strcmp (*argv, "--cms"))
102 : {
103 0 : protocol = GPGME_PROTOCOL_CMS;
104 0 : argc--; argv++;
105 : }
106 0 : else if (!strcmp (*argv, "--secret"))
107 : {
108 0 : only_secret = 1;
109 0 : argc--; argv++;
110 : }
111 0 : else if (!strcmp (*argv, "--local"))
112 : {
113 0 : mode |= GPGME_KEYLIST_MODE_LOCAL;
114 0 : argc--; argv++;
115 : }
116 0 : else if (!strcmp (*argv, "--extern"))
117 : {
118 0 : mode |= GPGME_KEYLIST_MODE_EXTERN;
119 0 : argc--; argv++;
120 : }
121 0 : else if (!strcmp (*argv, "--sigs"))
122 : {
123 0 : mode |= GPGME_KEYLIST_MODE_SIGS;
124 0 : argc--; argv++;
125 : }
126 0 : else if (!strcmp (*argv, "--sig-notations"))
127 : {
128 0 : mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
129 0 : argc--; argv++;
130 : }
131 0 : else if (!strcmp (*argv, "--ephemeral"))
132 : {
133 0 : mode |= GPGME_KEYLIST_MODE_EPHEMERAL;
134 0 : argc--; argv++;
135 : }
136 0 : else if (!strcmp (*argv, "--validate"))
137 : {
138 0 : mode |= GPGME_KEYLIST_MODE_VALIDATE;
139 0 : argc--; argv++;
140 : }
141 0 : else if (!strcmp (*argv, "--import"))
142 : {
143 0 : import = 1;
144 0 : argc--; argv++;
145 : }
146 0 : else if (!strcmp (*argv, "--offline"))
147 : {
148 0 : offline = 1;
149 0 : argc--; argv++;
150 : }
151 0 : else if (!strncmp (*argv, "--", 2))
152 0 : show_usage (1);
153 :
154 : }
155 :
156 0 : if (argc > 1)
157 0 : show_usage (1);
158 :
159 0 : init_gpgme (protocol);
160 :
161 0 : err = gpgme_new (&ctx);
162 0 : fail_if_err (err);
163 0 : gpgme_set_protocol (ctx, protocol);
164 :
165 0 : gpgme_set_keylist_mode (ctx, mode);
166 :
167 0 : gpgme_set_offline (ctx, offline);
168 :
169 0 : err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, only_secret);
170 0 : fail_if_err (err);
171 :
172 0 : while (!(err = gpgme_op_keylist_next (ctx, &key)))
173 : {
174 : gpgme_user_id_t uid;
175 : int nuids;
176 :
177 :
178 0 : printf ("keyid : %s\n", key->subkeys?nonnull (key->subkeys->keyid):"?");
179 0 : printf ("fpr : %s\n", key->subkeys?nonnull (key->subkeys->fpr):"?");
180 0 : printf ("caps : %s%s%s%s\n",
181 0 : key->can_encrypt? "e":"",
182 0 : key->can_sign? "s":"",
183 0 : key->can_certify? "c":"",
184 0 : key->can_authenticate? "a":"");
185 0 : printf ("flags :%s%s%s%s%s%s\n",
186 0 : key->secret? " secret":"",
187 0 : key->revoked? " revoked":"",
188 0 : key->expired? " expired":"",
189 0 : key->disabled? " disabled":"",
190 0 : key->invalid? " invalid":"",
191 0 : key->is_qualified? " qualifid":"");
192 0 : for (nuids=0, uid=key->uids; uid; uid = uid->next, nuids++)
193 : {
194 0 : printf ("userid %d: %s\n", nuids, nonnull(uid->uid));
195 0 : printf ("valid %d: %s\n", nuids,
196 0 : uid->validity == GPGME_VALIDITY_UNKNOWN? "unknown":
197 0 : uid->validity == GPGME_VALIDITY_UNDEFINED? "undefined":
198 0 : uid->validity == GPGME_VALIDITY_NEVER? "never":
199 0 : uid->validity == GPGME_VALIDITY_MARGINAL? "marginal":
200 0 : uid->validity == GPGME_VALIDITY_FULL? "full":
201 0 : uid->validity == GPGME_VALIDITY_ULTIMATE? "ultimate": "[?]");
202 : }
203 :
204 0 : putchar ('\n');
205 :
206 0 : if (import)
207 : {
208 0 : if (keyidx < DIM (keyarray)-1)
209 0 : keyarray[keyidx++] = key;
210 : else
211 : {
212 0 : fprintf (stderr, PGM": too many keys in import mode"
213 : "- skipping this key\n");
214 0 : gpgme_key_unref (key);
215 : }
216 : }
217 : else
218 0 : gpgme_key_unref (key);
219 : }
220 0 : if (gpgme_err_code (err) != GPG_ERR_EOF)
221 0 : fail_if_err (err);
222 0 : err = gpgme_op_keylist_end (ctx);
223 0 : fail_if_err (err);
224 0 : keyarray[keyidx] = NULL;
225 :
226 0 : result = gpgme_op_keylist_result (ctx);
227 0 : if (result->truncated)
228 : {
229 0 : fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
230 0 : exit (1);
231 : }
232 :
233 0 : if (import)
234 : {
235 : gpgme_import_result_t impres;
236 :
237 0 : err = gpgme_op_import_keys (ctx, keyarray);
238 0 : fail_if_err (err);
239 0 : impres = gpgme_op_import_result (ctx);
240 0 : if (!impres)
241 : {
242 0 : fprintf (stderr, PGM ": no import result returned\n");
243 0 : exit (1);
244 : }
245 0 : print_import_result (impres);
246 : }
247 :
248 0 : for (keyidx=0; keyarray[keyidx]; keyidx++)
249 0 : gpgme_key_unref (keyarray[keyidx]);
250 :
251 0 : gpgme_release (ctx);
252 0 : return 0;
253 : }
|