Fix libzypp build error
[platform/upstream/gpgme.git] / tests / run-keylist.c
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 <https://gnu.org/licenses/>.
18  * SPDX-License-Identifier: LGPL-2.1-or-later
19  */
20
21 /* We need to include config.h so that we know whether we are building
22    with large file system (LFS) support. */
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <time.h>
31
32 #include <gpgme.h>
33
34 #define PGM "run-keylist"
35
36 #include "run-support.h"
37
38
39 static int verbose;
40
41
42 static int
43 show_usage (int ex)
44 {
45   fputs ("usage: " PGM " [options] [USERID_or_FILE]\n\n"
46          "Options:\n"
47          "  --verbose        run in verbose mode\n"
48          "  --openpgp        use the OpenPGP protocol (default)\n"
49          "  --cms            use the CMS protocol\n"
50          "  --secret         list only secret keys\n"
51          "  --with-secret    list pubkeys with secret info filled\n"
52          "  --local          use GPGME_KEYLIST_MODE_LOCAL\n"
53          "  --extern         use GPGME_KEYLIST_MODE_EXTERN\n"
54          "  --sigs           use GPGME_KEYLIST_MODE_SIGS\n"
55          "  --tofu           use GPGME_KEYLIST_MODE_TOFU\n"
56          "  --sig-notations  use GPGME_KEYLIST_MODE_SIG_NOTATIONS\n"
57          "  --ephemeral      use GPGME_KEYLIST_MODE_EPHEMERAL\n"
58          "  --v5fpr          use GPGME_KEYLIST_MODE_V5FPR\n"
59          "  --validate       use GPGME_KEYLIST_MODE_VALIDATE\n"
60          "  --import         import all keys\n"
61          "  --offline        use offline mode\n"
62          "  --no-trust-check disable automatic trust database check\n"
63          "  --from-file      list all keys in the given file\n"
64          "  --from-wkd       list key from a web key directory\n"
65          "  --require-gnupg  required at least the given GnuPG version\n"
66          "  --trust-model    use the specified trust-model\n"
67          , stderr);
68   exit (ex);
69 }
70
71
72 static const char *
73 isotimestr (unsigned long value)
74 {
75   time_t t;
76   static char buffer[25+5];
77   struct tm *tp;
78
79   if (!value)
80     return "none";
81   t = value;
82
83   tp = gmtime (&t);
84   snprintf (buffer, sizeof buffer, "%04d-%02d-%02d %02d:%02d:%02d",
85             1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday,
86             tp->tm_hour, tp->tm_min, tp->tm_sec);
87   return buffer;
88 }
89
90
91
92 int
93 main (int argc, char **argv)
94 {
95   int last_argc = -1;
96   gpgme_error_t err;
97   gpgme_ctx_t ctx;
98   gpgme_keylist_mode_t mode = 0;
99   gpgme_key_t key;
100   gpgme_subkey_t subkey;
101   gpgme_keylist_result_t result;
102   int import = 0;
103   gpgme_key_t keyarray[100];
104   int keyidx = 0;
105   gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
106   int only_secret = 0;
107   int offline = 0;
108   int no_trust_check = 0;
109   int from_file = 0;
110   int from_wkd = 0;
111   gpgme_data_t data = NULL;
112   char *trust_model = NULL;
113
114
115   if (argc)
116     { argc--; argv++; }
117
118   while (argc && last_argc != argc )
119     {
120       last_argc = argc;
121       if (!strcmp (*argv, "--"))
122         {
123           argc--; argv++;
124           break;
125         }
126       else if (!strcmp (*argv, "--help"))
127         show_usage (0);
128       else if (!strcmp (*argv, "--verbose"))
129         {
130           verbose = 1;
131           argc--; argv++;
132         }
133       else if (!strcmp (*argv, "--openpgp"))
134         {
135           protocol = GPGME_PROTOCOL_OpenPGP;
136           argc--; argv++;
137         }
138       else if (!strcmp (*argv, "--cms"))
139         {
140           protocol = GPGME_PROTOCOL_CMS;
141           argc--; argv++;
142         }
143       else if (!strcmp (*argv, "--secret"))
144         {
145           only_secret = 1;
146           argc--; argv++;
147         }
148       else if (!strcmp (*argv, "--local"))
149         {
150           mode |= GPGME_KEYLIST_MODE_LOCAL;
151           argc--; argv++;
152         }
153       else if (!strcmp (*argv, "--extern"))
154         {
155           mode |= GPGME_KEYLIST_MODE_EXTERN;
156           argc--; argv++;
157         }
158       else if (!strcmp (*argv, "--tofu"))
159         {
160           mode |= GPGME_KEYLIST_MODE_WITH_TOFU;
161           argc--; argv++;
162         }
163       else if (!strcmp (*argv, "--sigs"))
164         {
165           mode |= GPGME_KEYLIST_MODE_SIGS;
166           argc--; argv++;
167         }
168       else if (!strcmp (*argv, "--sig-notations"))
169         {
170           mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
171           argc--; argv++;
172         }
173       else if (!strcmp (*argv, "--ephemeral"))
174         {
175           mode |= GPGME_KEYLIST_MODE_EPHEMERAL;
176           argc--; argv++;
177         }
178       else if (!strcmp (*argv, "--validate"))
179         {
180           mode |= GPGME_KEYLIST_MODE_VALIDATE;
181           argc--; argv++;
182         }
183       else if (!strcmp (*argv, "--with-secret"))
184         {
185           mode |= GPGME_KEYLIST_MODE_WITH_SECRET;
186           argc--; argv++;
187         }
188       else if (!strcmp (*argv, "--v5fpr"))
189         {
190           mode |= GPGME_KEYLIST_MODE_WITH_V5FPR;
191           argc--; argv++;
192         }
193       else if (!strcmp (*argv, "--import"))
194         {
195           import = 1;
196           argc--; argv++;
197         }
198       else if (!strcmp (*argv, "--offline"))
199         {
200           offline = 1;
201           argc--; argv++;
202         }
203       else if (!strcmp (*argv, "--no-trust-check"))
204         {
205           no_trust_check = 1;
206           argc--; argv++;
207         }
208       else if (!strcmp (*argv, "--from-file"))
209         {
210           from_file = 1;
211           argc--; argv++;
212         }
213       else if (!strcmp (*argv, "--require-gnupg"))
214         {
215           argc--; argv++;
216           if (!argc)
217             show_usage (1);
218           gpgme_set_global_flag ("require-gnupg", *argv);
219           argc--; argv++;
220         }
221       else if (!strcmp (*argv, "--from-wkd"))
222         {
223           argc--; argv++;
224           mode |= GPGME_KEYLIST_MODE_LOCATE;
225           from_wkd = 1;
226         }
227       else if (!strcmp (*argv, "--trust-model"))
228         {
229           argc--; argv++;
230           if (!argc)
231             show_usage (1);
232           trust_model = strdup (*argv);
233           argc--; argv++;
234         }
235       else if (!strncmp (*argv, "--", 2))
236         show_usage (1);
237     }
238
239   if (argc > 1)
240     show_usage (1);
241   else if (from_file && !argc)
242     show_usage (1);
243
244   init_gpgme (protocol);
245
246   err = gpgme_new (&ctx);
247   fail_if_err (err);
248   gpgme_set_protocol (ctx, protocol);
249
250   gpgme_set_keylist_mode (ctx, mode);
251
252   gpgme_set_offline (ctx, offline);
253
254   if (no_trust_check)
255     {
256       err = gpgme_set_ctx_flag (ctx, "no-auto-check-trustdb", "1");
257       fail_if_err (err);
258     }
259
260   if (trust_model)
261     {
262       err = gpgme_set_ctx_flag (ctx, "trust-model", trust_model);
263       fail_if_err (err);
264     }
265
266   if (from_wkd)
267     {
268       err = gpgme_set_ctx_flag (ctx, "auto-key-locate",
269                                 "clear,nodefault,wkd");
270       fail_if_err (err);
271     }
272
273   if (from_file)
274     {
275       err = gpgme_data_new_from_file (&data, *argv, 1);
276       fail_if_err (err);
277
278       err = gpgme_op_keylist_from_data_start (ctx, data, 0);
279     }
280   else
281     err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, only_secret);
282   fail_if_err (err);
283
284   while (!(err = gpgme_op_keylist_next (ctx, &key)))
285     {
286       gpgme_user_id_t uid;
287       gpgme_tofu_info_t ti;
288       gpgme_key_sig_t ks;
289       int nuids;
290       int nsub;
291       int nsigs;
292
293       printf ("keyid   : %s\n", key->subkeys?nonnull (key->subkeys->keyid):"?");
294       printf ("can_cap : %s%s%s%s\n",
295               key->can_encrypt? "e":"",
296               key->can_sign? "s":"",
297               key->can_certify? "c":"",
298               key->can_authenticate? "a":"");
299       printf ("has_cap : %s%s%s%s\n",
300               key->has_encrypt? "e":"",
301               key->has_sign? "s":"",
302               key->has_certify? "c":"",
303               key->has_authenticate? "a":"");
304       printf ("flags   :%s%s%s%s%s%s%s%s\n",
305               key->secret? " secret":"",
306               key->revoked? " revoked":"",
307               key->expired? " expired":"",
308               key->disabled? " disabled":"",
309               key->invalid? " invalid":"",
310               key->is_qualified? " qualified":"",
311               key->subkeys && key->subkeys->is_de_vs? " de-vs":"",
312               key->subkeys && key->subkeys->is_cardkey? " cardkey":"");
313       printf ("upd     : %lu (%u)\n", key->last_update, key->origin);
314
315       subkey = key->subkeys;
316       for (nsub=0; subkey; subkey = subkey->next, nsub++)
317         {
318           printf ("fpr   %2d: %s\n", nsub, nonnull (subkey->fpr));
319           if (subkey->v5fpr)
320             printf ("v5fpr %2d: %s\n", nsub, nonnull (subkey->v5fpr));
321           if (subkey->keygrip)
322             printf ("grip  %2d: %s\n", nsub, subkey->keygrip);
323           if (subkey->curve)
324             printf ("curve %2d: %s\n", nsub, subkey->curve);
325           printf ("caps  %2d: %s%s%s%s%s%s\n",
326                   nsub,
327                   subkey->can_encrypt? "e":"",
328                   subkey->can_sign? "s":"",
329                   subkey->can_certify? "c":"",
330                   subkey->can_authenticate? "a":"",
331                   subkey->can_renc? "r":"",
332                   subkey->can_timestamp? "t":"");
333           printf ("flags %2d:%s%s%s%s%s%s%s%s%s\n",
334                   nsub,
335                   subkey->secret? " secret":"",
336                   subkey->revoked? " revoked":"",
337                   subkey->expired? " expired":"",
338                   subkey->disabled? " disabled":"",
339                   subkey->invalid? " invalid":"",
340                   subkey->is_group_owned? " group":"",
341                   subkey->is_qualified? " qualified":"",
342                   subkey->is_de_vs? " de-vs":"",
343                   subkey->is_cardkey? " cardkey":"");
344         }
345       for (nuids=0, uid=key->uids; uid; uid = uid->next, nuids++)
346         {
347           printf ("userid %d: %s\n", nuids, nonnull(uid->uid));
348           printf ("    mbox: %s\n", nonnull(uid->address));
349           if (uid->email && uid->email != uid->address)
350             printf ("   email: %s\n", uid->email);
351           if (uid->name)
352             printf ("    name: %s\n", uid->name);
353           if (uid->comment)
354             printf ("   cmmnt: %s\n", uid->comment);
355           if (uid->uidhash)
356             printf (" uidhash: %s\n", uid->uidhash);
357           printf ("     upd: %lu (%u)\n", uid->last_update, uid->origin);
358           printf ("   valid: %s\n",
359                   uid->validity == GPGME_VALIDITY_UNKNOWN? "unknown":
360                   uid->validity == GPGME_VALIDITY_UNDEFINED? "undefined":
361                   uid->validity == GPGME_VALIDITY_NEVER? "never":
362                   uid->validity == GPGME_VALIDITY_MARGINAL? "marginal":
363                   uid->validity == GPGME_VALIDITY_FULL? "full":
364                   uid->validity == GPGME_VALIDITY_ULTIMATE? "ultimate": "[?]");
365           if ((ti = uid->tofu))
366             {
367               printf ("    tofu: %u (%s)\n", ti->validity,
368                       ti->validity == 0? "conflict" :
369                       ti->validity == 1? "no history" :
370                       ti->validity == 2? "little history" :
371                       ti->validity == 3? "enough history" :
372                       ti->validity == 4? "lot of history" : "?");
373               printf ("  policy: %u (%s)\n", ti->policy,
374                       ti->policy == GPGME_TOFU_POLICY_NONE? "none" :
375                       ti->policy == GPGME_TOFU_POLICY_AUTO? "auto" :
376                       ti->policy == GPGME_TOFU_POLICY_GOOD? "good" :
377                       ti->policy == GPGME_TOFU_POLICY_UNKNOWN? "unknown" :
378                       ti->policy == GPGME_TOFU_POLICY_BAD? "bad" :
379                       ti->policy == GPGME_TOFU_POLICY_ASK? "ask" : "?");
380               printf ("   nsigs: %hu\n", ti->signcount);
381               printf ("   first: %s\n", isotimestr (ti->signfirst));
382               printf ("    last: %s\n", isotimestr (ti->signlast));
383               printf ("   nencr: %hu\n", ti->encrcount);
384               printf ("   first: %s\n", isotimestr (ti->encrfirst));
385               printf ("    last: %s\n", isotimestr (ti->encrlast));
386             }
387           for (nsigs=0, ks=uid->signatures; ks; ks = ks->next, nsigs++)
388             {
389               printf ("signature %d: %s\n", nsigs, nonnull (ks->uid));
390               printf ("       keyid: %s\n", nonnull (ks->keyid));
391               printf ("     created: %s\n", isotimestr(ks->timestamp));
392               printf ("     expires: %s\n", isotimestr(ks->expires));
393               printf ("       class: %x\n", ks->sig_class);
394               printf (" trust depth: %u\n", ks->trust_depth);
395               printf (" trust value: %u\n", ks->trust_value);
396               printf (" trust scope: %s\n", nonnull (ks->trust_scope));
397             }
398         }
399
400       putchar ('\n');
401
402       if (import)
403         {
404           if (keyidx < DIM (keyarray)-1)
405             keyarray[keyidx++] = key;
406           else
407             {
408               fprintf (stderr, PGM": too many keys in import mode"
409                        "- skipping this key\n");
410               gpgme_key_unref (key);
411             }
412         }
413       else
414         gpgme_key_unref (key);
415     }
416   if (gpgme_err_code (err) != GPG_ERR_EOF)
417     fail_if_err (err);
418   err = gpgme_op_keylist_end (ctx);
419   fail_if_err (err);
420   keyarray[keyidx] = NULL;
421   gpgme_data_release (data);
422
423   result = gpgme_op_keylist_result (ctx);
424   if (result->truncated)
425     {
426       fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
427       exit (1);
428     }
429
430   if (import)
431     {
432       gpgme_import_result_t impres;
433
434       err = gpgme_op_import_keys (ctx, keyarray);
435       fail_if_err (err);
436       impres = gpgme_op_import_result (ctx);
437       if (!impres)
438         {
439           fprintf (stderr, PGM ": no import result returned\n");
440           exit (1);
441         }
442       print_import_result (impres);
443     }
444
445   for (keyidx=0; keyarray[keyidx]; keyidx++)
446     gpgme_key_unref (keyarray[keyidx]);
447
448   free (trust_model);
449
450   gpgme_release (ctx);
451   return 0;
452 }