Line data Source code
1 : /* delete.c - Delete a key.
2 : Copyright (C) 2001, 2002, 2003, 2004 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, write to the Free Software
18 : Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 : 02111-1307, USA. */
20 :
21 : #if HAVE_CONFIG_H
22 : #include <config.h>
23 : #endif
24 : #include <stdlib.h>
25 : #include <errno.h>
26 :
27 : #include "debug.h"
28 : #include "gpgme.h"
29 : #include "context.h"
30 : #include "ops.h"
31 :
32 :
33 : static gpgme_error_t
34 0 : delete_status_handler (void *priv, gpgme_status_code_t code, char *args)
35 : {
36 0 : if (code == GPGME_STATUS_DELETE_PROBLEM)
37 : {
38 : enum delete_problem
39 : {
40 : DELETE_No_Problem = 0,
41 : DELETE_No_Such_Key = 1,
42 : DELETE_Must_Delete_Secret_Key = 2,
43 : DELETE_Ambiguous_Specification = 3
44 : };
45 : long problem;
46 : char *tail;
47 :
48 0 : gpg_err_set_errno (0);
49 0 : problem = strtol (args, &tail, 0);
50 0 : if (errno || (*tail && *tail != ' '))
51 0 : return trace_gpg_error (GPG_ERR_INV_ENGINE);
52 :
53 0 : switch (problem)
54 : {
55 : case DELETE_No_Problem:
56 0 : break;
57 :
58 : case DELETE_No_Such_Key:
59 0 : return gpg_error (GPG_ERR_NO_PUBKEY);
60 :
61 : case DELETE_Must_Delete_Secret_Key:
62 0 : return gpg_error (GPG_ERR_CONFLICT);
63 :
64 : case DELETE_Ambiguous_Specification:
65 0 : return gpg_error (GPG_ERR_AMBIGUOUS_NAME);
66 :
67 : default:
68 0 : return gpg_error (GPG_ERR_GENERAL);
69 : }
70 : }
71 0 : return 0;
72 : }
73 :
74 :
75 : static gpgme_error_t
76 0 : delete_start (gpgme_ctx_t ctx, int synchronous, const gpgme_key_t key,
77 : int allow_secret)
78 : {
79 : gpgme_error_t err;
80 :
81 0 : err = _gpgme_op_reset (ctx, synchronous);
82 0 : if (err)
83 0 : return err;
84 :
85 0 : _gpgme_engine_set_status_handler (ctx->engine, delete_status_handler, ctx);
86 :
87 0 : return _gpgme_engine_op_delete (ctx->engine, key, allow_secret);
88 : }
89 :
90 :
91 : /* Delete KEY from the keyring. If ALLOW_SECRET is non-zero, secret
92 : keys are also deleted. */
93 : gpgme_error_t
94 0 : gpgme_op_delete_start (gpgme_ctx_t ctx, const gpgme_key_t key,
95 : int allow_secret)
96 : {
97 : gpgme_error_t err;
98 :
99 0 : TRACE_BEG3 (DEBUG_CTX, "gpgme_op_delete", ctx,
100 : "key=%p (%s), allow_secret=%i", key,
101 : (key->subkeys && key->subkeys->fpr) ?
102 : key->subkeys->fpr : "invalid", allow_secret);
103 :
104 0 : if (!ctx)
105 0 : return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
106 :
107 0 : err = delete_start (ctx, 0, key, allow_secret);
108 0 : return TRACE_ERR (err);
109 : }
110 :
111 :
112 : /* Delete KEY from the keyring. If ALLOW_SECRET is non-zero, secret
113 : keys are also deleted. */
114 : gpgme_error_t
115 0 : gpgme_op_delete (gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret)
116 : {
117 : gpgme_error_t err;
118 :
119 0 : TRACE_BEG3 (DEBUG_CTX, "gpgme_op_delete", ctx,
120 : "key=%p (%s), allow_secret=%i", key,
121 : (key->subkeys && key->subkeys->fpr) ?
122 : key->subkeys->fpr : "invalid", allow_secret);
123 :
124 0 : if (!ctx)
125 0 : return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
126 :
127 0 : err = delete_start (ctx, 1, key, allow_secret);
128 0 : if (!err)
129 0 : err = _gpgme_wait_one (ctx);
130 0 : return err;
131 : }
|