~ubuntu-branches/ubuntu/oneiric/gnupg2/oneiric-updates

« back to all changes in this revision

Viewing changes to g10/delkey.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Viehmann
  • Date: 2008-10-04 10:25:53 UTC
  • mfrom: (5.1.15 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081004102553-fv62pp8dsitxli47
Tags: 2.0.9-3.1
* Non-maintainer upload.
* agent/gpg-agent.c: Deinit the threading library before exec'ing
  the command to run in --daemon mode. And because that still doesn't
  restore the sigprocmask, do that manually. Closes: #499569

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* delkey.c - delete keys
2
 
 * Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
 
2
 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004,
 
3
 *               2005, 2006 Free Software Foundation, Inc.
3
4
 *
4
5
 * This file is part of GnuPG.
5
6
 *
6
7
 * GnuPG is free software; you can redistribute it and/or modify
7
8
 * 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
 * the Free Software Foundation; either version 3 of the License, or
9
10
 * (at your option) any later version.
10
11
 *
11
12
 * GnuPG is distributed in the hope that it will be useful,
14
15
 * GNU General Public License for more details.
15
16
 *
16
17
 * 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
 
18
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19
19
 */
20
20
 
21
21
#include <config.h>
26
26
#include <assert.h>
27
27
#include <ctype.h>
28
28
 
 
29
#include "gpg.h"
29
30
#include "options.h"
30
31
#include "packet.h"
31
 
#include "errors.h"
 
32
#include "status.h"
32
33
#include "iobuf.h"
33
34
#include "keydb.h"
34
 
#include "memory.h"
35
35
#include "util.h"
36
36
#include "main.h"
37
37
#include "trustdb.h"
47
47
 * key can't be deleted for that reason.
48
48
 */
49
49
static int
50
 
do_delete_key( const char *username, int secret, int *r_sec_avail )
 
50
do_delete_key( const char *username, int secret, int force, int *r_sec_avail )
51
51
{
52
52
    int rc = 0;
53
53
    KBNODE keyblock = NULL;
68
68
    exactmatch = (desc.mode == KEYDB_SEARCH_MODE_FPR
69
69
                  || desc.mode == KEYDB_SEARCH_MODE_FPR16
70
70
                  || desc.mode == KEYDB_SEARCH_MODE_FPR20);
71
 
    rc = desc.mode? keydb_search (hd, &desc, 1):GPG_ERR_INV_USER_ID;
 
71
    rc = desc.mode? keydb_search (hd, &desc, 1):G10ERR_INV_USER_ID;
72
72
    if (rc) {
73
 
        log_error (_("key `%s' not found: %s\n"), username, gpg_strerror (rc));
 
73
        log_error (_("key \"%s\" not found: %s\n"), username, g10_errstr (rc));
74
74
        write_status_text( STATUS_DELETE_PROBLEM, "1" );
75
75
        goto leave;
76
76
    }
78
78
    /* read the keyblock */
79
79
    rc = keydb_get_keyblock (hd, &keyblock );
80
80
    if (rc) {
81
 
        log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc) );
 
81
        log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) );
82
82
        goto leave;
83
83
    }
84
84
 
86
86
    node = find_kbnode( keyblock, secret? PKT_SECRET_KEY:PKT_PUBLIC_KEY );
87
87
    if( !node ) {
88
88
        log_error("Oops; key not found anymore!\n");
89
 
        rc = GPG_ERR_GENERAL;
 
89
        rc = G10ERR_GENERAL;
90
90
        goto leave;
91
91
    }
92
92
 
93
 
    if( secret ) {
 
93
    if( secret )
 
94
      {
94
95
        sk = node->pkt->pkt.secret_key;
95
96
        keyid_from_sk( sk, keyid );
96
 
    }
97
 
    else {
 
97
      }
 
98
    else
 
99
      {
 
100
        /* public */
98
101
        pk = node->pkt->pkt.public_key;
99
102
        keyid_from_pk( pk, keyid );
100
 
        rc = seckey_available( keyid );
101
 
        if( !rc ) {
102
 
            *r_sec_avail = 1;
103
 
            rc = -1;
104
 
            goto leave;
105
 
        }
106
 
        else if( rc != GPG_ERR_NO_SECKEY ) {
107
 
            log_error("%s: get secret key: %s\n", username, gpg_strerror (rc) );
108
 
        }
109
 
        else
110
 
            rc = 0;
111
 
    }
 
103
 
 
104
        if(!force)
 
105
          {
 
106
            rc = seckey_available( keyid );
 
107
            if( !rc )
 
108
              {
 
109
                *r_sec_avail = 1;
 
110
                rc = -1;
 
111
                goto leave;
 
112
              }
 
113
            else if( rc != G10ERR_NO_SECKEY )
 
114
              log_error("%s: get secret key: %s\n", username, g10_errstr(rc) );
 
115
            else
 
116
              rc = 0;
 
117
          }
 
118
      }
112
119
 
113
120
    if( rc )
114
121
        rc = 0;
116
123
        okay++;
117
124
    else if( opt.batch && secret )
118
125
      {
119
 
        log_error(_("can't do that in batchmode\n"));
 
126
        log_error(_("can't do this in batch mode\n"));
120
127
        log_info (_("(unless you specify the key by fingerprint)\n"));
121
128
      }
122
129
    else if( opt.batch && opt.answer_yes )
123
130
        okay++;
124
131
    else if( opt.batch )
125
132
      {
126
 
        log_error(_("can't do that in batchmode without \"--yes\"\n"));
 
133
        log_error(_("can't do this in batch mode without \"--yes\"\n"));
127
134
        log_info (_("(unless you specify the key by fingerprint)\n"));
128
135
      }
129
136
    else {
130
137
        if( secret )
131
138
            print_seckey_info( sk );
132
139
        else
133
 
            print_pubkey_info (NULL, pk );
 
140
            print_pubkey_info(NULL, pk );
134
141
        tty_printf( "\n" );
135
142
 
136
143
        yes = cpr_get_answer_is_yes( secret? "delete_key.secret.okay"
137
144
                                           : "delete_key.okay",
138
 
                              _("Delete this key from the keyring? "));
 
145
                              _("Delete this key from the keyring? (y/N) "));
139
146
        if( !cpr_enabled() && secret && yes ) {
140
147
            /* I think it is not required to check a passphrase; if
141
148
             * the user is so stupid as to let others access his secret keyring
143
150
             * basic texts about security.
144
151
             */
145
152
            yes = cpr_get_answer_is_yes("delete_key.secret.okay",
146
 
                         _("This is a secret key! - really delete? "));
 
153
                         _("This is a secret key! - really delete? (y/N) "));
147
154
        }
148
155
        if( yes )
149
156
            okay++;
153
160
    if( okay ) {
154
161
        rc = keydb_delete_keyblock (hd);
155
162
        if (rc) {
156
 
            log_error (_("deleting keyblock failed: %s\n"), gpg_strerror (rc) );
 
163
            log_error (_("deleting keyblock failed: %s\n"), g10_errstr(rc) );
157
164
            goto leave;
158
165
        }
159
166
 
177
184
 * Delete a public or secret key from a keyring.
178
185
 */
179
186
int
180
 
delete_keys( STRLIST names, int secret, int allow_both )
 
187
delete_keys( strlist_t names, int secret, int allow_both )
181
188
{
182
 
    int rc, avail;
 
189
    int rc, avail, force=(!allow_both && !secret && opt.expert);
 
190
 
 
191
    /* Force allows us to delete a public key even if a secret key
 
192
       exists. */
183
193
 
184
194
    for(;names;names=names->next) {
185
 
       rc = do_delete_key (names->d, secret, &avail );
 
195
       rc = do_delete_key (names->d, secret, force, &avail );
186
196
       if ( rc && avail ) { 
187
197
         if ( allow_both ) {
188
 
           rc = do_delete_key (names->d, 1, &avail );
 
198
           rc = do_delete_key (names->d, 1, 0, &avail );
189
199
           if ( !rc )
190
 
             rc = do_delete_key (names->d, 0, &avail );
 
200
             rc = do_delete_key (names->d, 0, 0, &avail );
191
201
         }
192
202
         else {
193
203
           log_error(_(
200
210
       }
201
211
 
202
212
       if(rc) {
203
 
         log_error("%s: delete key failed: %s\n", names->d, gpg_strerror (rc) );
 
213
         log_error("%s: delete key failed: %s\n", names->d, g10_errstr(rc) );
204
214
         return rc;
205
215
       }
206
216
    }