8dddb5bc8d6dcab197d6736b2566a3f2c3d59869
[gpgme.git] / tests / run-export.c
1 /* pgp-export.c  - Helper to run an export command
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-export"
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] USERIDS\n\n"
44          "Options:\n"
45          "  --verbose        run in verbose mode\n"
46          "  --extern         send keys to the keyserver (TAKE CARE!)\n"
47          , stderr);
48   exit (ex);
49 }
50
51 int 
52 main (int argc, char **argv)
53 {
54   int last_argc = -1;
55   gpgme_error_t err;
56   gpgme_ctx_t ctx;
57   gpgme_key_t key;
58   gpgme_keylist_result_t result;
59   gpgme_key_t keyarray[100];
60   int keyidx = 0;
61   gpgme_data_t out;
62   gpgme_export_mode_t mode = 0;
63
64   if (argc)
65     { argc--; argv++; }
66
67   while (argc && last_argc != argc )
68     {
69       last_argc = argc;
70       if (!strcmp (*argv, "--"))
71         {
72           argc--; argv++;
73           break;
74         }
75       else if (!strcmp (*argv, "--help"))
76         show_usage (0);
77       else if (!strcmp (*argv, "--verbose"))
78         {
79           verbose = 1;
80           argc--; argv++;
81         }
82       else if (!strcmp (*argv, "--extern"))
83         {
84           mode |= GPGME_KEYLIST_MODE_EXTERN;
85           argc--; argv++;
86         }
87       else if (!strncmp (*argv, "--", 2))
88         show_usage (1);
89       
90     }          
91  
92   if (!argc)
93     show_usage (1);
94
95   init_gpgme (GPGME_PROTOCOL_OpenPGP);
96
97   err = gpgme_new (&ctx);
98   fail_if_err (err);
99   gpgme_set_protocol (ctx, GPGME_PROTOCOL_OpenPGP);
100
101   /* Lookup the keys.  */
102   err = gpgme_op_keylist_ext_start (ctx, (const char**)argv, 0, 0);
103   fail_if_err (err);
104     
105   while (!(err = gpgme_op_keylist_next (ctx, &key)))
106     {
107       printf ("keyid: %s  (fpr: %s)\n",
108               key->subkeys?nonnull (key->subkeys->keyid):"?",
109               key->subkeys?nonnull (key->subkeys->fpr):"?");
110
111       if (keyidx < DIM (keyarray)-1)
112         keyarray[keyidx++] = key;
113       else
114         {
115           fprintf (stderr, PGM": too many keys"
116                    "- skipping this key\n");
117           gpgme_key_unref (key);
118         }
119     }
120   if (gpg_err_code (err) != GPG_ERR_EOF)
121     fail_if_err (err);
122   err = gpgme_op_keylist_end (ctx);
123   fail_if_err (err);
124   keyarray[keyidx] = NULL;
125
126   result = gpgme_op_keylist_result (ctx);
127   if (result->truncated)
128     {
129       fprintf (stderr, PGM ": key listing unexpectedly truncated\n");
130       exit (1);
131     }
132
133   /* Now for the actual export.  */
134   if ((mode & GPGME_KEYLIST_MODE_EXTERN))
135     printf ("sending keys to keyserver\n");
136
137   err = gpgme_data_new (&out);
138   fail_if_err (err);
139
140   gpgme_set_armor (ctx, 1);
141   err = gpgme_op_export_keys (ctx, keyarray, mode, 
142                               (mode & GPGME_KEYLIST_MODE_EXTERN)? NULL:out);
143   fail_if_err (err);
144
145   fflush (NULL);
146   if (!(mode & GPGME_KEYLIST_MODE_EXTERN))
147     {
148       fputs ("Begin Result:\n", stdout);
149       print_data (out);
150       fputs ("End Result.\n", stdout);
151     }
152
153   /* Cleanup.  */
154   gpgme_data_release (out);
155
156   for (keyidx=0; keyarray[keyidx]; keyidx++)
157     gpgme_key_unref (keyarray[keyidx]);
158
159   gpgme_release (ctx);
160   return 0;
161 }