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