1 |
/* editkey-util.c |
2 |
* Copyright (C) 2001-2004 Timo Schulz |
3 |
* |
4 |
* This file is part of MyGPGME. |
5 |
* |
6 |
* MyGPGME is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* MyGPGME is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
|
21 |
#include <stdio.h> |
22 |
#include <string.h> |
23 |
#include <stdlib.h> |
24 |
|
25 |
#include "gpgme-config.h" |
26 |
#ifdef WITH_EDITKEY |
27 |
#include "gpgme.h" |
28 |
#include "context.h" |
29 |
#include "ops.h" |
30 |
#include "util.h" |
31 |
|
32 |
|
33 |
gpgme_error_t |
34 |
gpgme_editkey_new( gpgme_editkey_t * r_ctx ) |
35 |
{ |
36 |
gpgme_editkey_t ctx; |
37 |
|
38 |
if( !r_ctx ) |
39 |
return mk_error( Invalid_Value ); |
40 |
*r_ctx = NULL; |
41 |
ctx = calloc( 1, sizeof *ctx ); |
42 |
if( !ctx ) |
43 |
return mk_error( Out_Of_Core ); |
44 |
*r_ctx = ctx; |
45 |
|
46 |
return 0; |
47 |
} /* gpgme_editkey_new */ |
48 |
|
49 |
|
50 |
static void |
51 |
revoke_release (gpgme_editkey_t ctx) |
52 |
{ |
53 |
safe_free (ctx->u.revoke.reason_text); |
54 |
} |
55 |
|
56 |
|
57 |
static void |
58 |
adduid_release (gpgme_editkey_t ctx) |
59 |
{ |
60 |
safe_free (ctx->u.adduid.comment); |
61 |
safe_free (ctx->u.adduid.email); |
62 |
safe_free (ctx->u.adduid.name); |
63 |
} |
64 |
|
65 |
|
66 |
void |
67 |
gpgme_editkey_release( gpgme_editkey_t ctx ) |
68 |
{ |
69 |
if (!ctx) |
70 |
return; |
71 |
|
72 |
switch (ctx->type) { |
73 |
case GPGME_EDITKEY_REVOKE: revoke_release (ctx); break; |
74 |
case GPGME_EDITKEY_ADDUID: adduid_release (ctx); break; |
75 |
} |
76 |
ctx->type = 0; |
77 |
safe_free (ctx); |
78 |
} /* gpgme_editkey_release */ |
79 |
|
80 |
|
81 |
/** |
82 |
* DELUID |
83 |
**/ |
84 |
void |
85 |
gpgme_editkey_deluid_set_id (gpgme_editkey_t ctx, int id) |
86 |
{ |
87 |
if (!ctx) |
88 |
return; |
89 |
|
90 |
ctx->u.deluid.id = id; |
91 |
ctx->type = GPGME_EDITKEY_DELUID; |
92 |
} /* gpgme_editkey_deluid_set_uid */ |
93 |
|
94 |
/** |
95 |
* DELKEY |
96 |
**/ |
97 |
void |
98 |
gpgme_editkey_delkey_set_id( gpgme_editkey_t ctx, int id ) |
99 |
{ |
100 |
if( !ctx ) |
101 |
return; |
102 |
ctx->u.delkey.id = id; |
103 |
ctx->type = GPGME_EDITKEY_DELKEY; |
104 |
} /* gpgme_editkey_delkey_set_id */ |
105 |
|
106 |
|
107 |
/* XXX: remove the passphrase in the _set functions and have a global |
108 |
function to set it. */ |
109 |
|
110 |
/** |
111 |
* ADDUID |
112 |
**/ |
113 |
void |
114 |
gpgme_editkey_adduid_set( gpgme_editkey_t ctx, const char *name, |
115 |
const char *email, const char *comment, |
116 |
const char *passwd ) |
117 |
{ |
118 |
if( !ctx ) |
119 |
return; |
120 |
|
121 |
/* name is a MUST field */ |
122 |
ctx->u.adduid.name = strdup( name ); |
123 |
ctx->u.adduid.email = strdup( email ); |
124 |
if( !comment ) |
125 |
ctx->u.adduid.use_comment = 0; |
126 |
else { |
127 |
ctx->u.adduid.comment = strdup( comment ); |
128 |
ctx->u.adduid.use_comment = 1; |
129 |
} |
130 |
ctx->u.adduid.passwd = passwd; |
131 |
ctx->type = GPGME_EDITKEY_ADDUID; |
132 |
} /* gpgme_editkey_adduid_set */ |
133 |
|
134 |
|
135 |
gpgme_error_t |
136 |
gpgme_editkey_addrev_set (gpgme_editkey_t ctx, const char * userid, |
137 |
const char * passwd) |
138 |
{ |
139 |
if (!ctx) |
140 |
return mk_error (Invalid_Value); |
141 |
if (!userid) |
142 |
return mk_error (Invalid_Mode); |
143 |
ctx->u.addrev.passwd = passwd; |
144 |
ctx->u.addrev.uid = userid; |
145 |
ctx->type = GPGME_EDITKEY_ADDREV; |
146 |
return 0; |
147 |
} /* gpgme_editkey_addrev_set */ |
148 |
|
149 |
|
150 |
void |
151 |
gpgme_editkey_addphoto_set( gpgme_editkey_t ctx, const char *jpegfile, |
152 |
const char * passwd ) |
153 |
{ |
154 |
if( !ctx ) |
155 |
return; |
156 |
ctx->u.addphoto.jpg = jpegfile; |
157 |
ctx->u.addphoto.passwd = passwd; |
158 |
ctx->type = GPGME_EDITKEY_ADDPHOTO; |
159 |
} /* gpgme_editkey_addphoto_set */ |
160 |
|
161 |
|
162 |
/** |
163 |
* TRUST |
164 |
**/ |
165 |
void |
166 |
gpgme_editkey_trust_set( gpgme_editkey_t ctx, int val ) |
167 |
{ |
168 |
if( !ctx ) |
169 |
return; |
170 |
|
171 |
switch( val ) { |
172 |
case 0: ctx->u.trust.trust_val = NULL; break; |
173 |
case GPGME_TRUST_DONTKNOW: ctx->u.trust.trust_val = "1"; break; |
174 |
case GPGME_TRUST_NEVER: ctx->u.trust.trust_val = "2"; break; |
175 |
case GPGME_TRUST_MARGINAL: ctx->u.trust.trust_val = "3"; break; |
176 |
case GPGME_TRUST_FULLY: ctx->u.trust.trust_val = "4";break; |
177 |
case GPGME_TRUST_ULTIMATE: ctx->u.trust.trust_val = "5"; break; |
178 |
default: ctx->u.trust.trust_val = "1"; break; |
179 |
} |
180 |
|
181 |
ctx->type = GPGME_EDITKEY_TRUST; |
182 |
} /* gpgme_editkey_trust_set */ |
183 |
|
184 |
/** |
185 |
* SIGN |
186 |
**/ |
187 |
gpgme_error_t |
188 |
gpgme_editkey_sign_set (gpgme_editkey_t ctx, |
189 |
const char * passwd, |
190 |
int sigclass, |
191 |
int sigtype, |
192 |
const char * param) |
193 |
{ |
194 |
if (!ctx) |
195 |
return mk_error (Invalid_Value); |
196 |
if (sigclass < 0 || sigclass > 3) |
197 |
return mk_error (Invalid_Mode); |
198 |
switch (sigtype) { |
199 |
case GPGME_EDITKEY_SIGN: |
200 |
case GPGME_EDITKEY_TSIGN: |
201 |
case GPGME_EDITKEY_NRSIGN: |
202 |
case GPGME_EDITKEY_NRLSIGN: |
203 |
case GPGME_EDITKEY_LSIGN: |
204 |
break; |
205 |
default: |
206 |
return mk_error (Invalid_Mode); |
207 |
} |
208 |
ctx->type = sigtype; |
209 |
ctx->u.sign.passwd = passwd; |
210 |
ctx->u.sign.sig_class = sigclass; |
211 |
ctx->u.sign.exp_date = param; |
212 |
|
213 |
return 0; |
214 |
} /* gpgme_editkey_sign_set */ |
215 |
|
216 |
/** |
217 |
* ADDKEY |
218 |
**/ |
219 |
gpgme_error_t |
220 |
gpgme_editkey_addkey_set (gpgme_editkey_t ctx, const char *passwd, |
221 |
int algo, int size, int valid) |
222 |
{ |
223 |
if (!ctx) |
224 |
return mk_error (Invalid_Value); |
225 |
if (algo < 2 || algo > 6) |
226 |
return mk_error (Invalid_Mode); |
227 |
if (algo == 2 && size > 1024) |
228 |
size = 1024; |
229 |
if (size > 4096) |
230 |
size = 4096; |
231 |
|
232 |
ctx->u.addkey.algo = algo; |
233 |
ctx->u.addkey.size = size; |
234 |
ctx->u.addkey.valid = valid; |
235 |
ctx->u.addkey.passwd = passwd; |
236 |
ctx->type = GPGME_EDITKEY_ADDKEY; |
237 |
|
238 |
return 0; |
239 |
} /* gpgme_editkey_addkey_set */ |
240 |
|
241 |
|
242 |
/** |
243 |
* PASSWD |
244 |
**/ |
245 |
void |
246 |
gpgme_editkey_passwd_set( gpgme_editkey_t ctx, const char * old_passwd, |
247 |
const char * new_passwd, int allow_empty ) |
248 |
{ |
249 |
if( !ctx ) |
250 |
return; |
251 |
if( old_passwd && new_passwd ) { |
252 |
ctx->u.passwd.old_passwd = old_passwd; |
253 |
ctx->u.passwd.new_passwd = new_passwd; |
254 |
} |
255 |
else if( !old_passwd && new_passwd ) { |
256 |
/* this is the case when the key was unprotected before. then |
257 |
GPG only asks once for a passphrase. */ |
258 |
ctx->u.passwd.old_passwd = new_passwd; |
259 |
ctx->u.passwd.new_passwd = NULL; |
260 |
} |
261 |
ctx->u.passwd.allow_empty = allow_empty; |
262 |
ctx->u.passwd.send_old = 0; |
263 |
ctx->type = GPGME_EDITKEY_PASSWD; |
264 |
} /* gpgme_editkey_passwd_set */ |
265 |
|
266 |
|
267 |
/** |
268 |
* PRIMARY |
269 |
**/ |
270 |
void |
271 |
gpgme_editkey_primary_set( gpgme_editkey_t ctx, int id, const char * passwd ) |
272 |
{ |
273 |
if( !ctx ) |
274 |
return; |
275 |
|
276 |
ctx->u.primary.id = id; |
277 |
ctx->u.primary.passwd = passwd; |
278 |
ctx->type = GPGME_EDITKEY_PRIMARY; |
279 |
} /* gpgme_editkey_primary_set */ |
280 |
|
281 |
|
282 |
/** |
283 |
* EXPIRE |
284 |
**/ |
285 |
void |
286 |
gpgme_editkey_expire_set( gpgme_editkey_t ctx, int id, int days, const char *date, |
287 |
const char *passwd ) |
288 |
{ |
289 |
if( !ctx ) |
290 |
return; |
291 |
ctx->u.expire.id = id; |
292 |
if( days && !date ) |
293 |
ctx->u.expire.days = days; |
294 |
else if( date && !days ) { |
295 |
ctx->u.expire.date = date; |
296 |
ctx->u.expire.days = 0; |
297 |
} |
298 |
ctx->u.expire.passwd = passwd; |
299 |
ctx->type = GPGME_EDITKEY_EXPIRE; |
300 |
} /* gpgme_editkey_expire_set */ |
301 |
|
302 |
|
303 |
/** |
304 |
* REVSIG |
305 |
**/ |
306 |
void |
307 |
gpgme_editkey_revsig_set( gpgme_editkey_t ctx, int id, const char *passwd ) |
308 |
{ |
309 |
if( !ctx ) |
310 |
return; |
311 |
ctx->u.revsig.id = id; |
312 |
ctx->u.revsig.passwd = passwd; |
313 |
ctx->type = GPGME_EDITKEY_REVSIG; |
314 |
} /* gpgme_editkey_revsig_set */ |
315 |
|
316 |
|
317 |
/** |
318 |
* REVKEY |
319 |
**/ |
320 |
void |
321 |
gpgme_editkey_revkey_set( gpgme_editkey_t ctx, int id, int reason, |
322 |
const char * passwd ) |
323 |
{ |
324 |
if( !ctx ) |
325 |
return; |
326 |
ctx->u.revkey.id = id; |
327 |
ctx->u.revkey.reason = reason; |
328 |
ctx->u.revkey.passwd = passwd; |
329 |
ctx->type = GPGME_EDITKEY_REVKEY; |
330 |
} |
331 |
|
332 |
/** |
333 |
* DELSIG |
334 |
**/ |
335 |
void |
336 |
gpgme_editkey_delsig_set (gpgme_editkey_t ctx, int uid, int signo) |
337 |
{ |
338 |
if (!ctx) |
339 |
return; |
340 |
ctx->u.delsig.currno = 0; |
341 |
ctx->u.delsig.signo = signo; |
342 |
ctx->u.delsig.uid = uid; |
343 |
ctx->type = GPGME_EDITKEY_DELSIG; |
344 |
} |
345 |
|
346 |
|
347 |
/** |
348 |
* ENABLE |
349 |
**/ |
350 |
void |
351 |
gpgme_editkey_enable_set( gpgme_editkey_t ctx ) |
352 |
{ |
353 |
if( ctx ) |
354 |
ctx->type = GPGME_EDITKEY_ENABLE; |
355 |
} |
356 |
|
357 |
|
358 |
/** |
359 |
* DISABLE |
360 |
**/ |
361 |
void |
362 |
gpgme_editkey_disable_set( gpgme_editkey_t ctx ) |
363 |
{ |
364 |
if( ctx ) |
365 |
ctx->type = GPGME_EDITKEY_DISABLE; |
366 |
} |
367 |
|
368 |
|
369 |
/** |
370 |
* SETPREF |
371 |
**/ |
372 |
void |
373 |
gpgme_editkey_setpref_set (gpgme_editkey_t ctx, const char * new_prefs, |
374 |
int uid_idx, const char * passwd) |
375 |
{ |
376 |
if (!ctx) |
377 |
return; |
378 |
ctx->u.pref.id = 0; |
379 |
ctx->u.pref.uid_idx = uid_idx; |
380 |
ctx->u.pref.passwd = passwd; |
381 |
ctx->u.pref.new_prefs = new_prefs; |
382 |
ctx->type = GPGME_EDITKEY_SETPREF; |
383 |
} |
384 |
|
385 |
|
386 |
/** |
387 |
* KEYSERVER |
388 |
**/ |
389 |
void |
390 |
gpgme_editkey_keyserver_set (gpgme_editkey_t ctx, const char *url, |
391 |
int uid_idx, const char * passwd) |
392 |
{ |
393 |
if (!ctx) |
394 |
return; |
395 |
ctx->u.keyserv.uid_idx = uid_idx; |
396 |
ctx->u.keyserv.url = url; |
397 |
ctx->u.keyserv.passwd = passwd; |
398 |
ctx->type = GPGME_EDITKEY_KEYSERV; |
399 |
} |
400 |
|
401 |
void |
402 |
gpgme_editkey_make_invalid (gpgme_editkey_t ctx) |
403 |
{ |
404 |
if( !ctx ) |
405 |
return; |
406 |
ctx->type = 0; |
407 |
} /* gpgme_editkey_make_invalid */ |
408 |
|
409 |
|
410 |
int |
411 |
gpgme_editkey_is_valid( gpgme_editkey_t ctx ) |
412 |
{ |
413 |
return ctx && ctx->type > 0 ? 1 : 0; |
414 |
} /* gpgme_editkey_is_valid */ |
415 |
|
416 |
|
417 |
int |
418 |
gpgme_editkey_is_secret( gpgme_editkey_t ctx, int val ) |
419 |
{ |
420 |
if( ctx && val ) { |
421 |
ctx->key_pair = val; |
422 |
return 0; |
423 |
} |
424 |
return ctx && ctx->key_pair; |
425 |
} /* gpgme_editkey_is_secret */ |
426 |
#endif /* WITH_EDITKEY */ |