Imported Upstream version 1.5.5
[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 <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 show_usage (int ex)
42 {
43   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          , stderr);
57   exit (ex);
58 }
59
60
61 int
62 main (int argc, char **argv)
63 {
64   int last_argc = -1;
65   gpgme_error_t err;
66   gpgme_ctx_t ctx;
67   gpgme_keylist_mode_t mode = 0;
68   gpgme_key_t key;
69   gpgme_keylist_result_t result;
70   int import = 0;
71   gpgme_key_t keyarray[100];
72   int keyidx = 0;
73   gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP;
74   int only_secret = 0;
75
76   if (argc)
77     { argc--; argv++; }
78
79   while (argc && last_argc != argc )
80     {
81       last_argc = argc;
82       if (!strcmp (*argv, "--"))
83         {
84           argc--; argv++;
85           break;
86         }
87       else if (!strcmp (*argv, "--help"))
88         show_usage (0);
89       else if (!strcmp (*argv, "--verbose"))
90         {
91           verbose = 1;
92           argc--; argv++;
93         }
94       else if (!strcmp (*argv, "--openpgp"))
95         {
96           protocol = GPGME_PROTOCOL_OpenPGP;
97           argc--; argv++;
98         }
99       else if (!strcmp (*argv, "--cms"))
100         {
101           protocol = GPGME_PROTOCOL_CMS;
102           argc--; argv++;
103         }
104       else if (!strcmp (*argv, "--secret"))
105         {
106           only_secret = 1;
107           argc--; argv++;
108         }
109       else if (!strcmp (*argv, "--local"))
110         {
111           mode |= GPGME_KEYLIST_MODE_LOCAL;
112           argc--; argv++;
113         }
114       else if (!strcmp (*argv, "--extern"))
115         {
116           mode |= GPGME_KEYLIST_MODE_EXTERN;
117           argc--; argv++;
118         }
119       else if (!strcmp (*argv, "--sigs"))
120         {
121           mode |= GPGME_KEYLIST_MODE_SIGS;
122           argc--; argv++;
123         }
124       else if (!strcmp (*argv, "--sig-notations"))
125         {
126           mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS;
127           argc--; argv++;
128         }
129       else if (!strcmp (*argv, "--ephemeral"))
130         {
131           mode |= GPGME_KEYLIST_MODE_EPHEMERAL;
132           argc--; argv++;
133         }
134       else if (!strcmp (*argv, "--validate"))
135         {
136           mode |= GPGME_KEYLIST_MODE_VALIDATE;
137           argc--; argv++;
138         }
139       else if (!strcmp (*argv, "--import"))
140         {
141           import = 1;
142           argc--; argv++;
143         }
144       else if (!strncmp (*argv, "--", 2))
145         show_usage (1);
146
147     }
148
149   if (argc > 1)
150     show_usage (1);
151
152   init_gpgme (protocol);
153
154   err = gpgme_new (&ctx);
155   fail_if_err (err);
156   gpgme_set_protocol (ctx, protocol);
157
158   gpgme_set_keylist_mode (ctx, mode);
159
160   err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, only_secret);
161   fail_if_err (err);
162
163   while (!(err = gpgme_op_keylist_next (ctx, &key)))
164     {
165       gpgme_user_id_t uid;
166       int nuids;
167
168
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++)
184         {
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": "[?]");
193         }
194
195       putchar ('\n');
196
197       if (import)
198         {
199           if (keyidx < DIM (keyarray)-1)
200             keyarray[keyidx++] = key;
201           else
202             {
203               fprintf (stderr, PGM": too many keys in import mode"
204                        "- skipping this key\n");
205               gpgme_key_unref (key);
206             }
207         }
208       else
209         gpgme_key_unref (key);
210     }
211   if (gpgme_err_code (err) != GPG_ERR_EOF)
212     fail_if_err (err);
213   err = gpgme_op_keylist_end (ctx);
214   fail_if_err (err);
215   keyarray[keyidx] = NULL;
216
217   result = gpgme_op_keylist_result (ctx);
218   if (result->truncated)
219     {
220       fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
221       exit (1);
222     }
223
224   if (import)
225     {
226       gpgme_import_result_t impres;
227
228       err = gpgme_op_import_keys (ctx, keyarray);
229       fail_if_err (err);
230       impres = gpgme_op_import_result (ctx);
231       if (!impres)
232         {
233           fprintf (stderr, PGM ": no import result returned\n");
234           exit (1);
235         }
236       print_import_result (impres);
237     }
238
239   for (keyidx=0; keyarray[keyidx]; keyidx++)
240     gpgme_key_unref (keyarray[keyidx]);
241
242   gpgme_release (ctx);
243   return 0;
244 }