1 |
/* export.c - encrypt functions |
2 |
* Copyright (C) 2000 Werner Koch (dd9jn), g10 Code GmbH |
3 |
* Copyright (C) 2001-2004 Timo Schulz |
4 |
* |
5 |
* This file is part of MyGPGME. |
6 |
* |
7 |
* MyGPGME is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License as published by |
9 |
* the Free Software Foundation; either version 2 of the License, or |
10 |
* (at your option) any later version. |
11 |
* |
12 |
* MyGPGME is distributed in the hope that it will be useful, |
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
* GNU General Public License for more details. |
16 |
* |
17 |
* You should have received a copy of the GNU General Public License |
18 |
* along with this program; if not, write to the Free Software |
19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
20 |
*/ |
21 |
|
22 |
#include <stdio.h> |
23 |
#include <stdlib.h> |
24 |
#include <string.h> |
25 |
#include <assert.h> |
26 |
|
27 |
#include "util.h" |
28 |
#include "context.h" |
29 |
#include "ops.h" |
30 |
|
31 |
|
32 |
static void |
33 |
export_status_handler (gpgme_ctx_t ctx, gpg_status_code_t code, char *args) |
34 |
{ |
35 |
DEBUG2 ("export_status: code=%d args=`%s'\n", code, args ); |
36 |
/* FIXME: Need to do more */ |
37 |
} |
38 |
|
39 |
|
40 |
static gpgme_error_t |
41 |
export_start (gpgme_ctx_t ctx, gpgme_recipients_t recp, gpgme_data_t keydata) |
42 |
{ |
43 |
struct user_id_s *u; |
44 |
gpgme_error_t rc = 0; |
45 |
size_t nkeys = 0; |
46 |
|
47 |
fail_on_pending_request( ctx ); |
48 |
ctx->pending = 1; |
49 |
|
50 |
/* create a process object */ |
51 |
_gpgme_gpg_release( &ctx->gpg ); |
52 |
rc = _gpgme_gpg_new( &ctx->gpg ); |
53 |
if( rc ) |
54 |
goto leave; |
55 |
|
56 |
if( ctx->use_logging ) |
57 |
_gpgme_gpg_set_logging_handler( ctx->gpg, ctx ); |
58 |
_gpgme_gpg_set_status_handler ( ctx->gpg, export_status_handler, ctx ); |
59 |
|
60 |
/* build the commandline */ |
61 |
_gpgme_add_comment (ctx); |
62 |
if (ctx->use_armor) |
63 |
_gpgme_gpg_add_arg( ctx->gpg, "--armor" ); |
64 |
_gpgme_gpg_add_arg( ctx->gpg, ctx->with_secret_key? "--export-secret-keys" |
65 |
: "--export" ); |
66 |
if( !keydata || gpgme_data_get_type( keydata ) != GPGME_DATA_TYPE_NONE ) { |
67 |
rc = mk_error( Invalid_Value ); |
68 |
goto leave; |
69 |
} |
70 |
_gpgme_data_set_mode( keydata, GPGME_DATA_MODE_IN ); |
71 |
_gpgme_gpg_add_data( ctx->gpg, keydata, 1 ); |
72 |
_gpgme_gpg_add_arg( ctx->gpg, "--" ); |
73 |
|
74 |
for( u = recp->list; u; u = u->next ) { |
75 |
_gpgme_gpg_add_arg( ctx->gpg, u->name ); |
76 |
nkeys++; |
77 |
} |
78 |
if( nkeys > 1 && ctx->with_secret_key ) { |
79 |
/* If we export the secret key, it's not a good idea to do this |
80 |
when we also export other keys. In this mode, it's only allowed |
81 |
to export one public and one private key. */ |
82 |
rc = mk_error( Invalid_Mode ); |
83 |
goto leave; |
84 |
} |
85 |
rc = _gpgme_gpg_spawn( ctx->gpg, ctx ); |
86 |
|
87 |
leave: |
88 |
if( rc ) { |
89 |
ctx->pending = 0; |
90 |
_gpgme_gpg_release( &ctx->gpg ); |
91 |
} |
92 |
return rc; |
93 |
} |
94 |
|
95 |
|
96 |
static gpgme_error_t |
97 |
file_export_start( gpgme_ctx_t ctx, gpgme_recipients_t recp, const char *output ) |
98 |
{ |
99 |
struct user_id_s *u; |
100 |
gpgme_error_t rc; |
101 |
|
102 |
if( !output ) |
103 |
return mk_error( Invalid_Value ); |
104 |
|
105 |
fail_on_pending_request( ctx ); |
106 |
ctx->pending = 1; |
107 |
|
108 |
_gpgme_gpg_release( &ctx->gpg ); |
109 |
rc =_gpgme_gpg_new( &ctx->gpg ); |
110 |
if( rc ) |
111 |
goto leave; |
112 |
|
113 |
_gpgme_gpg_add_arg (ctx->gpg, "--yes"); |
114 |
_gpgme_gpg_add_arg (ctx->gpg, "--export"); |
115 |
if (ctx->use_armor) |
116 |
_gpgme_gpg_add_arg( ctx->gpg, "--armor" ); |
117 |
_gpgme_add_comment (ctx); |
118 |
_gpgme_gpg_add_arg (ctx->gpg, "--output"); |
119 |
_gpgme_gpg_add_arg (ctx->gpg, output); |
120 |
for (u = recp->list; u; u = u->next) |
121 |
_gpgme_gpg_add_arg (ctx->gpg, u->name); |
122 |
|
123 |
rc = _gpgme_gpg_spawn( ctx->gpg, ctx ); |
124 |
|
125 |
leave: |
126 |
if( rc ) { |
127 |
ctx->pending = 0; |
128 |
_gpgme_gpg_release( &ctx->gpg ); |
129 |
} |
130 |
return rc; |
131 |
} /* file_export_start */ |
132 |
|
133 |
|
134 |
/** |
135 |
* gpgme_op_export: |
136 |
* @c: the context |
137 |
* @recp: a list of recipients or NULL |
138 |
* @keydata: Returns the keys |
139 |
* |
140 |
* This function can be used to extract public keys from the GnuPG key |
141 |
* database either in armored (by using gpgme_set_armor()) or in plain |
142 |
* binary form. The function expects a list of user IDs in @recp for |
143 |
* whom the public keys are to be exportedkinit |
144 |
* |
145 |
* |
146 |
* Return value: 0 for success or an error code |
147 |
**/ |
148 |
gpgme_error_t |
149 |
gpgme_op_export( gpgme_ctx_t ctx, gpgme_recipients_t recp, gpgme_data_t keydata ) |
150 |
{ |
151 |
gpgme_error_t err; |
152 |
|
153 |
err = export_start( ctx, recp, keydata ); |
154 |
if( !err ) { |
155 |
gpgme_wait( ctx, 1 ); |
156 |
ctx->pending = 0; |
157 |
if( gpgme_data_get_type( keydata ) == GPGME_DATA_TYPE_NONE ) |
158 |
err = mk_error( General_Error ); |
159 |
else if( gpgme_get_process_rc( ctx ) ) |
160 |
err = mk_error( Internal_GPG_Problem ); |
161 |
} |
162 |
return err; |
163 |
} /* gpgme_op_export */ |
164 |
|
165 |
|
166 |
gpgme_error_t |
167 |
gpgme_op_file_export( gpgme_ctx_t ctx, gpgme_recipients_t recp, const char *output ) |
168 |
{ |
169 |
gpgme_error_t err; |
170 |
|
171 |
err = file_export_start( ctx, recp, output ); |
172 |
if( !err ) { |
173 |
gpgme_wait( ctx, 1 ); |
174 |
ctx->pending = 0; |
175 |
if( gpgme_get_process_rc( ctx ) ) |
176 |
err = mk_error( Internal_GPG_Problem ); |
177 |
} |
178 |
|
179 |
return err; |
180 |
} /* gpgme_op_file_export */ |
181 |
|
182 |
|
183 |
gpgme_error_t |
184 |
gpgme_op_clip_export (gpgme_recipients_t rset) |
185 |
{ |
186 |
gpgme_error_t err = 0; |
187 |
gpgme_ctx_t ctx = NULL; |
188 |
gpgme_data_t keydata = NULL; |
189 |
|
190 |
err = gpgme_new (&ctx); |
191 |
if (!err) |
192 |
gpgme_control( ctx, GPGME_CTRL_ARMOR, 1 ); |
193 |
if (!err) |
194 |
err = gpgme_data_new (&keydata); |
195 |
if( !err ) |
196 |
err = gpgme_op_export (ctx, rset, keydata); |
197 |
if (!err) { |
198 |
gpgme_data_change_version (&keydata); |
199 |
gpgme_data_release_and_set_clipboard (keydata); |
200 |
} |
201 |
gpgme_release (ctx); |
202 |
return err; |
203 |
} /* gpgme_op_clip_export */ |