2008-05-15 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / g10 / keyring.c
blob937502ab2910fb9b5c193e918bbc71eab552f122
1 /* keyring.c - keyring file handling
2 * Copyright (C) 2001, 2004 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG 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 3 of the License, or
9 * (at your option) any later version.
11 * GnuPG 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
30 #include "gpg.h"
31 #include "util.h"
32 #include "keyring.h"
33 #include "packet.h"
34 #include "keydb.h"
35 #include "options.h"
36 #include "main.h" /*for check_key_signature()*/
37 #include "i18n.h"
39 /* off_item is a funny named for an object used to keep track of known
40 * keys. The idea was to use the offset to seek to the known keyblock, but
41 * this is not possible if more than one process is using the keyring.
43 struct off_item {
44 struct off_item *next;
45 u32 kid[2];
46 /*off_t off;*/
49 typedef struct off_item **OffsetHashTable;
52 typedef struct keyring_name *KR_NAME;
53 struct keyring_name {
54 struct keyring_name *next;
55 int secret;
56 DOTLOCK lockhd;
57 int is_locked;
58 int did_full_scan;
59 char fname[1];
61 typedef struct keyring_name const * CONST_KR_NAME;
63 static KR_NAME kr_names;
64 static int active_handles;
66 static OffsetHashTable kr_offtbl;
67 static int kr_offtbl_ready;
70 struct keyring_handle {
71 CONST_KR_NAME resource;
72 int secret; /* this is for a secret keyring */
73 struct {
74 CONST_KR_NAME kr;
75 IOBUF iobuf;
76 int eof;
77 int error;
78 } current;
79 struct {
80 CONST_KR_NAME kr;
81 off_t offset;
82 size_t pk_no;
83 size_t uid_no;
84 unsigned int n_packets; /*used for delete and update*/
85 } found;
86 struct {
87 char *name;
88 char *pattern;
89 } word_match;
94 static int do_copy (int mode, const char *fname, KBNODE root, int secret,
95 off_t start_offset, unsigned int n_packets );
99 static struct off_item *
100 new_offset_item (void)
102 struct off_item *k;
104 k = xmalloc_clear (sizeof *k);
105 return k;
108 #if 0
109 static void
110 release_offset_items (struct off_item *k)
112 struct off_item *k2;
114 for (; k; k = k2)
116 k2 = k->next;
117 xfree (k);
120 #endif
122 static OffsetHashTable
123 new_offset_hash_table (void)
125 struct off_item **tbl;
127 tbl = xmalloc_clear (2048 * sizeof *tbl);
128 return tbl;
131 #if 0
132 static void
133 release_offset_hash_table (OffsetHashTable tbl)
135 int i;
137 if (!tbl)
138 return;
139 for (i=0; i < 2048; i++)
140 release_offset_items (tbl[i]);
141 xfree (tbl);
143 #endif
145 static struct off_item *
146 lookup_offset_hash_table (OffsetHashTable tbl, u32 *kid)
148 struct off_item *k;
150 for (k = tbl[(kid[1] & 0x07ff)]; k; k = k->next)
151 if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
152 return k;
153 return NULL;
156 static void
157 update_offset_hash_table (OffsetHashTable tbl, u32 *kid, off_t off)
159 struct off_item *k;
161 for (k = tbl[(kid[1] & 0x07ff)]; k; k = k->next)
163 if (k->kid[0] == kid[0] && k->kid[1] == kid[1])
165 /*k->off = off;*/
166 return;
170 k = new_offset_item ();
171 k->kid[0] = kid[0];
172 k->kid[1] = kid[1];
173 /*k->off = off;*/
174 k->next = tbl[(kid[1] & 0x07ff)];
175 tbl[(kid[1] & 0x07ff)] = k;
178 static void
179 update_offset_hash_table_from_kb (OffsetHashTable tbl, KBNODE node, off_t off)
181 for (; node; node = node->next)
183 if (node->pkt->pkttype == PKT_PUBLIC_KEY
184 || node->pkt->pkttype == PKT_PUBLIC_SUBKEY)
186 u32 aki[2];
187 keyid_from_pk (node->pkt->pkt.public_key, aki);
188 update_offset_hash_table (tbl, aki, off);
194 * Register a filename for plain keyring files. ptr is set to a
195 * pointer to be used to create a handles etc, or the already-issued
196 * pointer if it has already been registered. The function returns 1
197 * if a new keyring was registered.
200 keyring_register_filename (const char *fname, int secret, void **ptr)
202 KR_NAME kr;
204 if (active_handles)
205 BUG (); /* We don't allow that */
207 for (kr=kr_names; kr; kr = kr->next)
209 if (same_file_p (kr->fname, fname))
211 *ptr=kr;
212 return 0; /* Already registered. */
216 if (secret)
217 register_secured_file (fname);
219 kr = xmalloc (sizeof *kr + strlen (fname));
220 strcpy (kr->fname, fname);
221 kr->secret = !!secret;
222 kr->lockhd = NULL;
223 kr->is_locked = 0;
224 kr->did_full_scan = 0;
225 /* keep a list of all issued pointers */
226 kr->next = kr_names;
227 kr_names = kr;
229 /* create the offset table the first time a function here is used */
230 if (!kr_offtbl)
231 kr_offtbl = new_offset_hash_table ();
233 *ptr=kr;
235 return 1;
239 keyring_is_writable (void *token)
241 KR_NAME r = token;
243 return r? !access (r->fname, W_OK) : 0;
248 /* Create a new handle for the resource associated with TOKEN. SECRET
249 is just just as a cross-check.
251 The returned handle must be released using keyring_release (). */
252 KEYRING_HANDLE
253 keyring_new (void *token, int secret)
255 KEYRING_HANDLE hd;
256 KR_NAME resource = token;
258 assert (resource && !resource->secret == !secret);
260 hd = xmalloc_clear (sizeof *hd);
261 hd->resource = resource;
262 hd->secret = !!secret;
263 active_handles++;
264 return hd;
267 void
268 keyring_release (KEYRING_HANDLE hd)
270 if (!hd)
271 return;
272 assert (active_handles > 0);
273 active_handles--;
274 xfree (hd->word_match.name);
275 xfree (hd->word_match.pattern);
276 iobuf_close (hd->current.iobuf);
277 xfree (hd);
281 const char *
282 keyring_get_resource_name (KEYRING_HANDLE hd)
284 if (!hd || !hd->resource)
285 return NULL;
286 return hd->resource->fname;
291 * Lock the keyring with the given handle, or unlok if yes is false.
292 * We ignore the handle and lock all registered files.
294 int
295 keyring_lock (KEYRING_HANDLE hd, int yes)
297 KR_NAME kr;
298 int rc = 0;
300 if (yes) {
301 /* first make sure the lock handles are created */
302 for (kr=kr_names; kr; kr = kr->next) {
303 if (!keyring_is_writable(kr))
304 continue;
305 if (!kr->lockhd) {
306 kr->lockhd = create_dotlock( kr->fname );
307 if (!kr->lockhd) {
308 log_info ("can't allocate lock for `%s'\n", kr->fname );
309 rc = G10ERR_GENERAL;
313 if (rc)
314 return rc;
316 /* and now set the locks */
317 for (kr=kr_names; kr; kr = kr->next) {
318 if (!keyring_is_writable(kr))
319 continue;
320 if (kr->is_locked)
322 else if (make_dotlock (kr->lockhd, -1) ) {
323 log_info ("can't lock `%s'\n", kr->fname );
324 rc = G10ERR_GENERAL;
326 else
327 kr->is_locked = 1;
331 if (rc || !yes) {
332 for (kr=kr_names; kr; kr = kr->next) {
333 if (!keyring_is_writable(kr))
334 continue;
335 if (!kr->is_locked)
337 else if (release_dotlock (kr->lockhd))
338 log_info ("can't unlock `%s'\n", kr->fname );
339 else
340 kr->is_locked = 0;
344 return rc;
350 * Return the last found keyring. Caller must free it.
351 * The returned keyblock has the kbode flag bit 0 set for the node with
352 * the public key used to locate the keyblock or flag bit 1 set for
353 * the user ID node.
356 keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
358 PACKET *pkt;
359 int rc;
360 KBNODE keyblock = NULL, node, lastnode;
361 IOBUF a;
362 int in_cert = 0;
363 int pk_no = 0;
364 int uid_no = 0;
365 int save_mode;
367 if (ret_kb)
368 *ret_kb = NULL;
370 if (!hd->found.kr)
371 return -1; /* no successful search */
373 a = iobuf_open (hd->found.kr->fname);
374 if (!a)
376 log_error(_("can't open `%s'\n"), hd->found.kr->fname);
377 return G10ERR_KEYRING_OPEN;
380 if (iobuf_seek (a, hd->found.offset) ) {
381 log_error ("can't seek `%s'\n", hd->found.kr->fname);
382 iobuf_close(a);
383 return G10ERR_KEYRING_OPEN;
386 pkt = xmalloc (sizeof *pkt);
387 init_packet (pkt);
388 hd->found.n_packets = 0;;
389 lastnode = NULL;
390 save_mode = set_packet_list_mode(0);
391 while ((rc=parse_packet (a, pkt)) != -1) {
392 hd->found.n_packets++;
393 if (rc == G10ERR_UNKNOWN_PACKET) {
394 free_packet (pkt);
395 init_packet (pkt);
396 continue;
398 if (rc) {
399 log_error ("keyring_get_keyblock: read error: %s\n",
400 g10_errstr(rc) );
401 rc = G10ERR_INV_KEYRING;
402 break;
404 if (pkt->pkttype == PKT_COMPRESSED) {
405 log_error ("skipped compressed packet in keyring\n");
406 free_packet(pkt);
407 init_packet(pkt);
408 continue;
411 if (in_cert && (pkt->pkttype == PKT_PUBLIC_KEY
412 || pkt->pkttype == PKT_SECRET_KEY)) {
413 hd->found.n_packets--; /* fix counter */
414 break; /* ready */
417 in_cert = 1;
418 if (pkt->pkttype == PKT_RING_TRUST) {
419 /*(this code is duplicated after the loop)*/
420 if ( lastnode
421 && lastnode->pkt->pkttype == PKT_SIGNATURE
422 && (pkt->pkt.ring_trust->sigcache & 1) ) {
423 /* this is a ring trust packet with a checked signature
424 * status cache following directly a signature paket.
425 * Set the cache status into that signature packet */
426 PKT_signature *sig = lastnode->pkt->pkt.signature;
428 sig->flags.checked = 1;
429 sig->flags.valid = !!(pkt->pkt.ring_trust->sigcache & 2);
431 /* reset lastnode, so that we set the cache status only from
432 * the ring trust packet immediately folling a signature */
433 lastnode = NULL;
435 else {
436 node = lastnode = new_kbnode (pkt);
437 if (!keyblock)
438 keyblock = node;
439 else
440 add_kbnode (keyblock, node);
442 if ( pkt->pkttype == PKT_PUBLIC_KEY
443 || pkt->pkttype == PKT_PUBLIC_SUBKEY
444 || pkt->pkttype == PKT_SECRET_KEY
445 || pkt->pkttype == PKT_SECRET_SUBKEY) {
446 if (++pk_no == hd->found.pk_no)
447 node->flag |= 1;
449 else if ( pkt->pkttype == PKT_USER_ID) {
450 if (++uid_no == hd->found.uid_no)
451 node->flag |= 2;
455 pkt = xmalloc (sizeof *pkt);
456 init_packet(pkt);
458 set_packet_list_mode(save_mode);
460 if (rc == -1 && keyblock)
461 rc = 0; /* got the entire keyblock */
463 if (rc || !ret_kb)
464 release_kbnode (keyblock);
465 else {
466 /*(duplicated form the loop body)*/
467 if ( pkt && pkt->pkttype == PKT_RING_TRUST
468 && lastnode
469 && lastnode->pkt->pkttype == PKT_SIGNATURE
470 && (pkt->pkt.ring_trust->sigcache & 1) ) {
471 PKT_signature *sig = lastnode->pkt->pkt.signature;
472 sig->flags.checked = 1;
473 sig->flags.valid = !!(pkt->pkt.ring_trust->sigcache & 2);
475 *ret_kb = keyblock;
477 free_packet (pkt);
478 xfree (pkt);
479 iobuf_close(a);
481 /* Make sure that future search operations fail immediately when
482 * we know that we are working on a invalid keyring
484 if (rc == G10ERR_INV_KEYRING)
485 hd->current.error = rc;
487 return rc;
491 keyring_update_keyblock (KEYRING_HANDLE hd, KBNODE kb)
493 int rc;
495 if (!hd->found.kr)
496 return -1; /* no successful prior search */
498 if (!hd->found.n_packets) {
499 /* need to know the number of packets - do a dummy get_keyblock*/
500 rc = keyring_get_keyblock (hd, NULL);
501 if (rc) {
502 log_error ("re-reading keyblock failed: %s\n", g10_errstr (rc));
503 return rc;
505 if (!hd->found.n_packets)
506 BUG ();
509 /* The open iobuf isn't needed anymore and in fact is a problem when
510 it comes to renaming the keyring files on some operating systems,
511 so close it here */
512 iobuf_close(hd->current.iobuf);
513 hd->current.iobuf = NULL;
515 /* do the update */
516 rc = do_copy (3, hd->found.kr->fname, kb, hd->secret,
517 hd->found.offset, hd->found.n_packets );
518 if (!rc) {
519 if (!hd->secret && kr_offtbl)
521 update_offset_hash_table_from_kb (kr_offtbl, kb, 0);
523 /* better reset the found info */
524 hd->found.kr = NULL;
525 hd->found.offset = 0;
527 return rc;
531 keyring_insert_keyblock (KEYRING_HANDLE hd, KBNODE kb)
533 int rc;
534 const char *fname;
536 if (!hd)
537 fname = NULL;
538 else if (hd->found.kr)
539 fname = hd->found.kr->fname;
540 else if (hd->current.kr)
541 fname = hd->current.kr->fname;
542 else
543 fname = hd->resource? hd->resource->fname:NULL;
545 if (!fname)
546 return G10ERR_GENERAL;
548 /* close this one otherwise we will lose the position for
549 * a next search. Fixme: it would be better to adjust the position
550 * after the write opertions.
552 iobuf_close (hd->current.iobuf);
553 hd->current.iobuf = NULL;
555 /* do the insert */
556 rc = do_copy (1, fname, kb, hd->secret, 0, 0 );
557 if (!rc && !hd->secret && kr_offtbl)
559 update_offset_hash_table_from_kb (kr_offtbl, kb, 0);
562 return rc;
567 keyring_delete_keyblock (KEYRING_HANDLE hd)
569 int rc;
571 if (!hd->found.kr)
572 return -1; /* no successful prior search */
574 if (!hd->found.n_packets) {
575 /* need to know the number of packets - do a dummy get_keyblock*/
576 rc = keyring_get_keyblock (hd, NULL);
577 if (rc) {
578 log_error ("re-reading keyblock failed: %s\n", g10_errstr (rc));
579 return rc;
581 if (!hd->found.n_packets)
582 BUG ();
585 /* close this one otherwise we will lose the position for
586 * a next search. Fixme: it would be better to adjust the position
587 * after the write opertions.
589 iobuf_close (hd->current.iobuf);
590 hd->current.iobuf = NULL;
592 /* do the delete */
593 rc = do_copy (2, hd->found.kr->fname, NULL, hd->secret,
594 hd->found.offset, hd->found.n_packets );
595 if (!rc) {
596 /* better reset the found info */
597 hd->found.kr = NULL;
598 hd->found.offset = 0;
599 /* Delete is a rare operations, so we don't remove the keys
600 * from the offset table */
602 return rc;
608 * Start the next search on this handle right at the beginning
610 int
611 keyring_search_reset (KEYRING_HANDLE hd)
613 assert (hd);
615 hd->current.kr = NULL;
616 iobuf_close (hd->current.iobuf);
617 hd->current.iobuf = NULL;
618 hd->current.eof = 0;
619 hd->current.error = 0;
621 hd->found.kr = NULL;
622 hd->found.offset = 0;
623 return 0;
627 static int
628 prepare_search (KEYRING_HANDLE hd)
630 if (hd->current.error)
631 return hd->current.error; /* still in error state */
633 if (hd->current.kr && !hd->current.eof) {
634 if ( !hd->current.iobuf )
635 return G10ERR_GENERAL; /* position invalid after a modify */
636 return 0; /* okay */
639 if (!hd->current.kr && hd->current.eof)
640 return -1; /* still EOF */
642 if (!hd->current.kr) { /* start search with first keyring */
643 hd->current.kr = hd->resource;
644 if (!hd->current.kr) {
645 hd->current.eof = 1;
646 return -1; /* keyring not available */
648 assert (!hd->current.iobuf);
650 else { /* EOF */
651 iobuf_close (hd->current.iobuf);
652 hd->current.iobuf = NULL;
653 hd->current.kr = NULL;
654 hd->current.eof = 1;
655 return -1;
658 hd->current.eof = 0;
659 hd->current.iobuf = iobuf_open (hd->current.kr->fname);
660 if (!hd->current.iobuf)
662 hd->current.error = gpg_error_from_syserror ();
663 log_error(_("can't open `%s'\n"), hd->current.kr->fname );
664 return hd->current.error;
667 return 0;
671 /* A map of the all characters valid used for word_match()
672 * Valid characters are in in this table converted to uppercase.
673 * because the upper 128 bytes have special meaning, we assume
674 * that they are all valid.
675 * Note: We must use numerical values here in case that this program
676 * will be converted to those little blue HAL9000s with their strange
677 * EBCDIC character set (user ids are UTF-8).
678 * wk 2000-04-13: Hmmm, does this really make sense, given the fact that
679 * we can run gpg now on a S/390 running GNU/Linux, where the code
680 * translation is done by the device drivers?
682 static const byte word_match_chars[256] = {
683 /* 00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
684 /* 08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
685 /* 10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
686 /* 18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
687 /* 20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
688 /* 28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
689 /* 30 */ 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
690 /* 38 */ 0x38, 0x39, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
691 /* 40 */ 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
692 /* 48 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
693 /* 50 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
694 /* 58 */ 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00,
695 /* 60 */ 0x00, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
696 /* 68 */ 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
697 /* 70 */ 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
698 /* 78 */ 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x00,
699 /* 80 */ 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
700 /* 88 */ 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
701 /* 90 */ 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
702 /* 98 */ 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f,
703 /* a0 */ 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
704 /* a8 */ 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf,
705 /* b0 */ 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7,
706 /* b8 */ 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
707 /* c0 */ 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
708 /* c8 */ 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf,
709 /* d0 */ 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
710 /* d8 */ 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf,
711 /* e0 */ 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
712 /* e8 */ 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
713 /* f0 */ 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
714 /* f8 */ 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
717 /****************
718 * Do a word match (original user id starts with a '+').
719 * The pattern is already tokenized to a more suitable format:
720 * There are only the real words in it delimited by one space
721 * and all converted to uppercase.
723 * Returns: 0 if all words match.
725 * Note: This algorithm is a straightforward one and not very
726 * fast. It works for UTF-8 strings. The uidlen should
727 * be removed but due to the fact that old versions of
728 * pgp don't use UTF-8 we still use the length; this should
729 * be fixed in parse-packet (and replace \0 by some special
730 * UTF-8 encoding)
732 static int
733 word_match( const byte *uid, size_t uidlen, const byte *pattern )
735 size_t wlen, n;
736 const byte *p;
737 const byte *s;
739 for( s=pattern; *s; ) {
740 do {
741 /* skip leading delimiters */
742 while( uidlen && !word_match_chars[*uid] )
743 uid++, uidlen--;
744 /* get length of the word */
745 n = uidlen; p = uid;
746 while( n && word_match_chars[*p] )
747 p++, n--;
748 wlen = p - uid;
749 /* and compare against the current word from pattern */
750 for(n=0, p=uid; n < wlen && s[n] != ' ' && s[n] ; n++, p++ ) {
751 if( word_match_chars[*p] != s[n] )
752 break;
754 if( n == wlen && (s[n] == ' ' || !s[n]) )
755 break; /* found */
756 uid += wlen;
757 uidlen -= wlen;
758 } while( uidlen );
759 if( !uidlen )
760 return -1; /* not found */
762 /* advance to next word in pattern */
763 for(; *s != ' ' && *s ; s++ )
765 if( *s )
766 s++ ;
768 return 0; /* found */
771 /****************
772 * prepare word word_match; that is parse the name and
773 * build the pattern.
774 * caller has to free the returned pattern
776 static char*
777 prepare_word_match (const byte *name)
779 byte *pattern, *p;
780 int c;
782 /* the original length is always enough for the pattern */
783 p = pattern = xmalloc(strlen(name)+1);
784 do {
785 /* skip leading delimiters */
786 while( *name && !word_match_chars[*name] )
787 name++;
788 /* copy as long as we don't have a delimiter and convert
789 * to uppercase.
790 * fixme: how can we handle utf8 uppercasing */
791 for( ; *name && (c=word_match_chars[*name]); name++ )
792 *p++ = c;
793 *p++ = ' '; /* append pattern delimiter */
794 } while( *name );
795 p[-1] = 0; /* replace last pattern delimiter by EOS */
797 return pattern;
803 static int
804 compare_name (int mode, const char *name, const char *uid, size_t uidlen)
806 int i;
807 const char *s, *se;
809 if (mode == KEYDB_SEARCH_MODE_EXACT) {
810 for (i=0; name[i] && uidlen; i++, uidlen--)
811 if (uid[i] != name[i])
812 break;
813 if (!uidlen && !name[i])
814 return 0; /* found */
816 else if (mode == KEYDB_SEARCH_MODE_SUBSTR) {
817 if (ascii_memistr( uid, uidlen, name ))
818 return 0;
820 else if ( mode == KEYDB_SEARCH_MODE_MAIL
821 || mode == KEYDB_SEARCH_MODE_MAILSUB
822 || mode == KEYDB_SEARCH_MODE_MAILEND) {
823 for (i=0, s= uid; i < uidlen && *s != '<'; s++, i++)
825 if (i < uidlen) {
826 /* skip opening delim and one char and look for the closing one*/
827 s++; i++;
828 for (se=s+1, i++; i < uidlen && *se != '>'; se++, i++)
830 if (i < uidlen) {
831 i = se - s;
832 if (mode == KEYDB_SEARCH_MODE_MAIL) {
833 if( strlen(name)-2 == i
834 && !ascii_memcasecmp( s, name+1, i) )
835 return 0;
837 else if (mode == KEYDB_SEARCH_MODE_MAILSUB) {
838 if( ascii_memistr( s, i, name ) )
839 return 0;
841 else { /* email from end */
842 /* nyi */
847 else if (mode == KEYDB_SEARCH_MODE_WORDS)
848 return word_match (uid, uidlen, name);
849 else
850 BUG();
852 return -1; /* not found */
857 * Search through the keyring(s), starting at the current position,
858 * for a keyblock which contains one of the keys described in the DESC array.
860 int
861 keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
862 size_t ndesc, size_t *descindex)
864 int rc;
865 PACKET pkt;
866 int save_mode;
867 off_t offset, main_offset;
868 size_t n;
869 int need_uid, need_words, need_keyid, need_fpr, any_skip;
870 int pk_no, uid_no;
871 int initial_skip;
872 int use_offtbl;
873 PKT_user_id *uid = NULL;
874 PKT_public_key *pk = NULL;
875 PKT_secret_key *sk = NULL;
876 u32 aki[2];
878 /* figure out what information we need */
879 need_uid = need_words = need_keyid = need_fpr = any_skip = 0;
880 for (n=0; n < ndesc; n++)
882 switch (desc[n].mode)
884 case KEYDB_SEARCH_MODE_EXACT:
885 case KEYDB_SEARCH_MODE_SUBSTR:
886 case KEYDB_SEARCH_MODE_MAIL:
887 case KEYDB_SEARCH_MODE_MAILSUB:
888 case KEYDB_SEARCH_MODE_MAILEND:
889 need_uid = 1;
890 break;
891 case KEYDB_SEARCH_MODE_WORDS:
892 need_uid = 1;
893 need_words = 1;
894 break;
895 case KEYDB_SEARCH_MODE_SHORT_KID:
896 case KEYDB_SEARCH_MODE_LONG_KID:
897 need_keyid = 1;
898 break;
899 case KEYDB_SEARCH_MODE_FPR16:
900 case KEYDB_SEARCH_MODE_FPR20:
901 case KEYDB_SEARCH_MODE_FPR:
902 need_fpr = 1;
903 break;
904 case KEYDB_SEARCH_MODE_FIRST:
905 /* always restart the search in this mode */
906 keyring_search_reset (hd);
907 break;
908 default: break;
910 if (desc[n].skipfnc)
912 any_skip = 1;
913 need_keyid = 1;
917 rc = prepare_search (hd);
918 if (rc)
919 return rc;
921 use_offtbl = !hd->secret && kr_offtbl;
922 if (!use_offtbl)
924 else if (!kr_offtbl_ready)
925 need_keyid = 1;
926 else if (ndesc == 1 && desc[0].mode == KEYDB_SEARCH_MODE_LONG_KID)
928 struct off_item *oi;
930 oi = lookup_offset_hash_table (kr_offtbl, desc[0].u.kid);
931 if (!oi)
932 { /* We know that we don't have this key */
933 hd->found.kr = NULL;
934 hd->current.eof = 1;
935 return -1;
937 /* We could now create a positive search status and return.
938 * However the problem is that another instance of gpg may
939 * have changed the keyring so that the offsets are not valid
940 * anymore - therefore we don't do it
944 if (need_words)
946 const char *name = NULL;
948 log_debug ("word search mode does not yet work\n");
949 /* FIXME: here is a long standing bug in our function and in addition we
950 just use the first search description */
951 for (n=0; n < ndesc && !name; n++)
953 if (desc[n].mode == KEYDB_SEARCH_MODE_WORDS)
954 name = desc[n].u.name;
956 assert (name);
957 if ( !hd->word_match.name || strcmp (hd->word_match.name, name) )
959 /* name changed */
960 xfree (hd->word_match.name);
961 xfree (hd->word_match.pattern);
962 hd->word_match.name = xstrdup (name);
963 hd->word_match.pattern = prepare_word_match (name);
965 name = hd->word_match.pattern;
968 init_packet(&pkt);
969 save_mode = set_packet_list_mode(0);
971 hd->found.kr = NULL;
972 main_offset = 0;
973 pk_no = uid_no = 0;
974 initial_skip = 1; /* skip until we see the start of a keyblock */
975 while (!(rc=search_packet (hd->current.iobuf, &pkt, &offset, need_uid)))
977 byte afp[MAX_FINGERPRINT_LEN];
978 size_t an;
980 if (pkt.pkttype == PKT_PUBLIC_KEY || pkt.pkttype == PKT_SECRET_KEY)
982 main_offset = offset;
983 pk_no = uid_no = 0;
984 initial_skip = 0;
986 if (initial_skip)
988 free_packet (&pkt);
989 continue;
992 pk = NULL;
993 sk = NULL;
994 uid = NULL;
995 if ( pkt.pkttype == PKT_PUBLIC_KEY
996 || pkt.pkttype == PKT_PUBLIC_SUBKEY)
998 pk = pkt.pkt.public_key;
999 ++pk_no;
1001 if (need_fpr) {
1002 fingerprint_from_pk (pk, afp, &an);
1003 while (an < 20) /* fill up to 20 bytes */
1004 afp[an++] = 0;
1006 if (need_keyid)
1007 keyid_from_pk (pk, aki);
1009 if (use_offtbl && !kr_offtbl_ready)
1010 update_offset_hash_table (kr_offtbl, aki, main_offset);
1012 else if (pkt.pkttype == PKT_USER_ID)
1014 uid = pkt.pkt.user_id;
1015 ++uid_no;
1017 else if ( pkt.pkttype == PKT_SECRET_KEY
1018 || pkt.pkttype == PKT_SECRET_SUBKEY)
1020 sk = pkt.pkt.secret_key;
1021 ++pk_no;
1023 if (need_fpr) {
1024 fingerprint_from_sk (sk, afp, &an);
1025 while (an < 20) /* fill up to 20 bytes */
1026 afp[an++] = 0;
1028 if (need_keyid)
1029 keyid_from_sk (sk, aki);
1033 for (n=0; n < ndesc; n++)
1035 switch (desc[n].mode) {
1036 case KEYDB_SEARCH_MODE_NONE:
1037 BUG ();
1038 break;
1039 case KEYDB_SEARCH_MODE_EXACT:
1040 case KEYDB_SEARCH_MODE_SUBSTR:
1041 case KEYDB_SEARCH_MODE_MAIL:
1042 case KEYDB_SEARCH_MODE_MAILSUB:
1043 case KEYDB_SEARCH_MODE_MAILEND:
1044 case KEYDB_SEARCH_MODE_WORDS:
1045 if ( uid && !compare_name (desc[n].mode,
1046 desc[n].u.name,
1047 uid->name, uid->len))
1048 goto found;
1049 break;
1051 case KEYDB_SEARCH_MODE_SHORT_KID:
1052 if ((pk||sk) && desc[n].u.kid[1] == aki[1])
1053 goto found;
1054 break;
1055 case KEYDB_SEARCH_MODE_LONG_KID:
1056 if ((pk||sk) && desc[n].u.kid[0] == aki[0]
1057 && desc[n].u.kid[1] == aki[1])
1058 goto found;
1059 break;
1060 case KEYDB_SEARCH_MODE_FPR16:
1061 if ((pk||sk) && !memcmp (desc[n].u.fpr, afp, 16))
1062 goto found;
1063 break;
1064 case KEYDB_SEARCH_MODE_FPR20:
1065 case KEYDB_SEARCH_MODE_FPR:
1066 if ((pk||sk) && !memcmp (desc[n].u.fpr, afp, 20))
1067 goto found;
1068 break;
1069 case KEYDB_SEARCH_MODE_FIRST:
1070 if (pk||sk)
1071 goto found;
1072 break;
1073 case KEYDB_SEARCH_MODE_NEXT:
1074 if (pk||sk)
1075 goto found;
1076 break;
1077 default:
1078 rc = G10ERR_INV_ARG;
1079 goto found;
1082 free_packet (&pkt);
1083 continue;
1084 found:
1085 /* Record which desc we matched on. Note this value is only
1086 meaningful if this function returns with no errors. */
1087 if(descindex)
1088 *descindex=n;
1089 for (n=any_skip?0:ndesc; n < ndesc; n++)
1091 if (desc[n].skipfnc
1092 && desc[n].skipfnc (desc[n].skipfncvalue, aki, uid))
1093 break;
1095 if (n == ndesc)
1096 goto real_found;
1097 free_packet (&pkt);
1099 real_found:
1100 if (!rc)
1102 hd->found.offset = main_offset;
1103 hd->found.kr = hd->current.kr;
1104 hd->found.pk_no = (pk||sk)? pk_no : 0;
1105 hd->found.uid_no = uid? uid_no : 0;
1107 else if (rc == -1)
1109 hd->current.eof = 1;
1110 /* if we scanned all keyrings, we are sure that
1111 * all known key IDs are in our offtbl, mark that. */
1112 if (use_offtbl && !kr_offtbl_ready)
1114 KR_NAME kr;
1116 /* First set the did_full_scan flag for this keyring (ignore
1117 secret keyrings) */
1118 for (kr=kr_names; kr; kr = kr->next)
1120 if (!kr->secret && hd->resource == kr)
1122 kr->did_full_scan = 1;
1123 break;
1126 /* Then check whether all flags are set and if so, mark the
1127 offtbl ready */
1128 for (kr=kr_names; kr; kr = kr->next)
1130 if (!kr->secret && !kr->did_full_scan)
1131 break;
1133 if (!kr)
1134 kr_offtbl_ready = 1;
1137 else
1138 hd->current.error = rc;
1140 free_packet(&pkt);
1141 set_packet_list_mode(save_mode);
1142 return rc;
1146 static int
1147 create_tmp_file (const char *template,
1148 char **r_bakfname, char **r_tmpfname, IOBUF *r_fp)
1150 char *bakfname, *tmpfname;
1151 mode_t oldmask;
1153 *r_bakfname = NULL;
1154 *r_tmpfname = NULL;
1156 # ifdef USE_ONLY_8DOT3
1157 /* Here is another Windoze bug?:
1158 * you cant rename("pubring.gpg.tmp", "pubring.gpg");
1159 * but rename("pubring.gpg.tmp", "pubring.aaa");
1160 * works. So we replace .gpg by .bak or .tmp
1162 if (strlen (template) > 4
1163 && !strcmp (template+strlen(template)-4, EXTSEP_S "gpg") )
1165 bakfname = xmalloc (strlen (template) + 1);
1166 strcpy (bakfname, template);
1167 strcpy (bakfname+strlen(template)-4, EXTSEP_S "bak");
1169 tmpfname = xmalloc (strlen( template ) + 1 );
1170 strcpy (tmpfname,template);
1171 strcpy (tmpfname+strlen(template)-4, EXTSEP_S "tmp");
1173 else
1174 { /* file does not end with gpg; hmmm */
1175 bakfname = xmalloc (strlen( template ) + 5);
1176 strcpy (stpcpy(bakfname, template), EXTSEP_S "bak");
1178 tmpfname = xmalloc (strlen( template ) + 5);
1179 strcpy (stpcpy(tmpfname, template), EXTSEP_S "tmp");
1181 # else /* Posix file names */
1182 bakfname = xmalloc (strlen( template ) + 2);
1183 strcpy (stpcpy (bakfname,template),"~");
1185 tmpfname = xmalloc (strlen( template ) + 5);
1186 strcpy (stpcpy(tmpfname,template), EXTSEP_S "tmp");
1187 # endif /* Posix filename */
1189 /* Create the temp file with limited access */
1190 oldmask=umask(077);
1191 if (is_secured_filename (tmpfname))
1193 *r_fp = NULL;
1194 errno = EPERM;
1196 else
1197 *r_fp = iobuf_create (tmpfname);
1198 umask(oldmask);
1199 if (!*r_fp)
1201 int rc = gpg_error_from_syserror ();
1202 log_error(_("can't create `%s': %s\n"), tmpfname, strerror(errno) );
1203 xfree (tmpfname);
1204 xfree (bakfname);
1205 return rc;
1208 *r_bakfname = bakfname;
1209 *r_tmpfname = tmpfname;
1210 return 0;
1214 static int
1215 rename_tmp_file (const char *bakfname, const char *tmpfname,
1216 const char *fname, int secret )
1218 int rc=0;
1220 /* invalidate close caches*/
1221 iobuf_ioctl (NULL, 2, 0, (char*)tmpfname );
1222 iobuf_ioctl (NULL, 2, 0, (char*)bakfname );
1223 iobuf_ioctl (NULL, 2, 0, (char*)fname );
1225 /* first make a backup file except for secret keyrings */
1226 if (!secret)
1228 #if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
1229 remove (bakfname);
1230 #endif
1231 if (rename (fname, bakfname) )
1233 rc = gpg_error_from_syserror ();
1234 log_error ("renaming `%s' to `%s' failed: %s\n",
1235 fname, bakfname, strerror(errno) );
1236 return rc;
1240 /* then rename the file */
1241 #if defined(HAVE_DOSISH_SYSTEM) || defined(__riscos__)
1242 remove( fname );
1243 #endif
1244 if (secret)
1245 unregister_secured_file (fname);
1246 if (rename (tmpfname, fname) )
1248 rc = gpg_error_from_syserror ();
1249 log_error (_("renaming `%s' to `%s' failed: %s\n"),
1250 tmpfname, fname, strerror(errno) );
1251 register_secured_file (fname);
1252 if (secret)
1254 log_info(_("WARNING: 2 files with confidential"
1255 " information exists.\n"));
1256 log_info(_("%s is the unchanged one\n"), fname );
1257 log_info(_("%s is the new one\n"), tmpfname );
1258 log_info(_("Please fix this possible security flaw\n"));
1260 return rc;
1263 /* Now make sure the file has the same permissions as the original */
1265 #ifndef HAVE_DOSISH_SYSTEM
1267 struct stat statbuf;
1269 statbuf.st_mode=S_IRUSR | S_IWUSR;
1271 if(((secret && !opt.preserve_permissions) ||
1272 (stat(bakfname,&statbuf)==0)) &&
1273 (chmod(fname,statbuf.st_mode)==0))
1275 else
1276 log_error("WARNING: unable to restore permissions to `%s': %s",
1277 fname,strerror(errno));
1279 #endif
1281 return 0;
1285 static int
1286 write_keyblock (IOBUF fp, KBNODE keyblock)
1288 KBNODE kbctx = NULL, node;
1289 int rc;
1291 while ( (node = walk_kbnode (keyblock, &kbctx, 0)) )
1293 if (node->pkt->pkttype == PKT_RING_TRUST)
1294 continue; /* we write it later on our own */
1296 if ( (rc = build_packet (fp, node->pkt) ))
1298 log_error ("build_packet(%d) failed: %s\n",
1299 node->pkt->pkttype, g10_errstr(rc) );
1300 return rc;
1302 if (node->pkt->pkttype == PKT_SIGNATURE)
1303 { /* always write a signature cache packet */
1304 PKT_signature *sig = node->pkt->pkt.signature;
1305 unsigned int cacheval = 0;
1307 if (sig->flags.checked)
1309 cacheval |= 1;
1310 if (sig->flags.valid)
1311 cacheval |= 2;
1313 iobuf_put (fp, 0xb0); /* old style packet 12, 1 byte len*/
1314 iobuf_put (fp, 2); /* 2 bytes */
1315 iobuf_put (fp, 0); /* unused */
1316 if (iobuf_put (fp, cacheval))
1318 rc = gpg_error_from_syserror ();
1319 log_error ("writing sigcache packet failed\n");
1320 return rc;
1324 return 0;
1328 * Walk over all public keyrings, check the signatures and replace the
1329 * keyring with a new one where the signature cache is then updated.
1330 * This is only done for the public keyrings.
1333 keyring_rebuild_cache (void *token,int noisy)
1335 KEYRING_HANDLE hd;
1336 KEYDB_SEARCH_DESC desc;
1337 KBNODE keyblock = NULL, node;
1338 const char *lastresname = NULL, *resname;
1339 IOBUF tmpfp = NULL;
1340 char *tmpfilename = NULL;
1341 char *bakfilename = NULL;
1342 int rc;
1343 ulong count = 0, sigcount = 0;
1345 hd = keyring_new (token, 0);
1346 memset (&desc, 0, sizeof desc);
1347 desc.mode = KEYDB_SEARCH_MODE_FIRST;
1349 rc=keyring_lock (hd, 1);
1350 if(rc)
1351 goto leave;
1353 while ( !(rc = keyring_search (hd, &desc, 1, NULL)) )
1355 desc.mode = KEYDB_SEARCH_MODE_NEXT;
1356 resname = keyring_get_resource_name (hd);
1357 if (lastresname != resname )
1358 { /* we have switched to a new keyring - commit changes */
1359 if (tmpfp)
1361 if (iobuf_close (tmpfp))
1363 rc = gpg_error_from_syserror ();
1364 log_error ("error closing `%s': %s\n",
1365 tmpfilename, strerror (errno));
1366 goto leave;
1368 /* because we have switched resources, we can be sure that
1369 * the original file is closed */
1370 tmpfp = NULL;
1372 rc = lastresname? rename_tmp_file (bakfilename, tmpfilename,
1373 lastresname, 0) : 0;
1374 xfree (tmpfilename); tmpfilename = NULL;
1375 xfree (bakfilename); bakfilename = NULL;
1376 if (rc)
1377 goto leave;
1378 lastresname = resname;
1379 if (noisy && !opt.quiet)
1380 log_info (_("caching keyring `%s'\n"), resname);
1381 rc = create_tmp_file (resname, &bakfilename, &tmpfilename, &tmpfp);
1382 if (rc)
1383 goto leave;
1386 release_kbnode (keyblock);
1387 rc = keyring_get_keyblock (hd, &keyblock);
1388 if (rc)
1390 log_error ("keyring_get_keyblock failed: %s\n", g10_errstr(rc));
1391 goto leave;
1393 assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
1395 /* check all signature to set the signature's cache flags */
1396 for (node=keyblock; node; node=node->next)
1398 /* Note that this doesn't cache the result of a revocation
1399 issued by a designated revoker. This is because the pk
1400 in question does not carry the revkeys as we haven't
1401 merged the key and selfsigs. It is questionable whether
1402 this matters very much since there are very very few
1403 designated revoker revocation packets out there. */
1405 if (node->pkt->pkttype == PKT_SIGNATURE)
1407 PKT_signature *sig=node->pkt->pkt.signature;
1409 if(!opt.no_sig_cache && sig->flags.checked && sig->flags.valid
1410 && (openpgp_md_test_algo(sig->digest_algo)
1411 || openpgp_pk_test_algo(sig->pubkey_algo)))
1412 sig->flags.checked=sig->flags.valid=0;
1413 else
1414 check_key_signature (keyblock, node, NULL);
1416 sigcount++;
1420 /* write the keyblock to the temporary file */
1421 rc = write_keyblock (tmpfp, keyblock);
1422 if (rc)
1423 goto leave;
1425 if ( !(++count % 50) && noisy && !opt.quiet)
1426 log_info(_("%lu keys cached so far (%lu signatures)\n"),
1427 count, sigcount );
1429 } /* end main loop */
1430 if (rc == -1)
1431 rc = 0;
1432 if (rc)
1434 log_error ("keyring_search failed: %s\n", g10_errstr(rc));
1435 goto leave;
1437 if(noisy || opt.verbose)
1438 log_info(_("%lu keys cached (%lu signatures)\n"), count, sigcount );
1439 if (tmpfp)
1441 if (iobuf_close (tmpfp))
1443 rc = gpg_error_from_syserror ();
1444 log_error ("error closing `%s': %s\n",
1445 tmpfilename, strerror (errno));
1446 goto leave;
1448 /* because we have switched resources, we can be sure that
1449 * the original file is closed */
1450 tmpfp = NULL;
1452 rc = lastresname? rename_tmp_file (bakfilename, tmpfilename,
1453 lastresname, 0) : 0;
1454 xfree (tmpfilename); tmpfilename = NULL;
1455 xfree (bakfilename); bakfilename = NULL;
1457 leave:
1458 if (tmpfp)
1459 iobuf_cancel (tmpfp);
1460 xfree (tmpfilename);
1461 xfree (bakfilename);
1462 release_kbnode (keyblock);
1463 keyring_lock (hd, 0);
1464 keyring_release (hd);
1465 return rc;
1469 /****************
1470 * Perform insert/delete/update operation.
1471 * mode 1 = insert
1472 * 2 = delete
1473 * 3 = update
1475 static int
1476 do_copy (int mode, const char *fname, KBNODE root, int secret,
1477 off_t start_offset, unsigned int n_packets )
1479 IOBUF fp, newfp;
1480 int rc=0;
1481 char *bakfname = NULL;
1482 char *tmpfname = NULL;
1484 /* Open the source file. Because we do a rename, we have to check the
1485 permissions of the file */
1486 if (access (fname, W_OK))
1487 return gpg_error_from_syserror ();
1489 fp = iobuf_open (fname);
1490 if (mode == 1 && !fp && errno == ENOENT) {
1491 /* insert mode but file does not exist: create a new file */
1492 KBNODE kbctx, node;
1493 mode_t oldmask;
1495 oldmask=umask(077);
1496 if (!secret && is_secured_filename (fname)) {
1497 newfp = NULL;
1498 errno = EPERM;
1500 else
1501 newfp = iobuf_create (fname);
1502 umask(oldmask);
1503 if( !newfp )
1505 rc = gpg_error_from_syserror ();
1506 log_error (_("can't create `%s': %s\n"), fname, strerror(errno));
1507 return rc;
1509 if( !opt.quiet )
1510 log_info(_("%s: keyring created\n"), fname );
1512 kbctx=NULL;
1513 while ( (node = walk_kbnode( root, &kbctx, 0 )) ) {
1514 if( (rc = build_packet( newfp, node->pkt )) ) {
1515 log_error("build_packet(%d) failed: %s\n",
1516 node->pkt->pkttype, g10_errstr(rc) );
1517 iobuf_cancel(newfp);
1518 return rc;
1521 if( iobuf_close(newfp) ) {
1522 rc = gpg_error_from_syserror ();
1523 log_error ("%s: close failed: %s\n", fname, strerror(errno));
1524 return rc;
1526 return 0; /* ready */
1529 if( !fp )
1531 rc = gpg_error_from_syserror ();
1532 log_error(_("can't open `%s': %s\n"), fname, strerror(errno) );
1533 goto leave;
1536 /* Create the new file. */
1537 rc = create_tmp_file (fname, &bakfname, &tmpfname, &newfp);
1538 if (rc) {
1539 iobuf_close(fp);
1540 goto leave;
1542 if (secret)
1543 register_secured_file (tmpfname);
1545 if( mode == 1 ) { /* insert */
1546 /* copy everything to the new file */
1547 rc = copy_all_packets (fp, newfp);
1548 if( rc != -1 ) {
1549 log_error("%s: copy to `%s' failed: %s\n",
1550 fname, tmpfname, g10_errstr(rc) );
1551 iobuf_close(fp);
1552 if (secret)
1553 unregister_secured_file (tmpfname);
1554 iobuf_cancel(newfp);
1555 goto leave;
1557 rc = 0;
1560 if( mode == 2 || mode == 3 ) { /* delete or update */
1561 /* copy first part to the new file */
1562 rc = copy_some_packets( fp, newfp, start_offset );
1563 if( rc ) { /* should never get EOF here */
1564 log_error ("%s: copy to `%s' failed: %s\n",
1565 fname, tmpfname, g10_errstr(rc) );
1566 iobuf_close(fp);
1567 if (secret)
1568 unregister_secured_file (tmpfname);
1569 iobuf_cancel(newfp);
1570 goto leave;
1572 /* skip this keyblock */
1573 assert( n_packets );
1574 rc = skip_some_packets( fp, n_packets );
1575 if( rc ) {
1576 log_error("%s: skipping %u packets failed: %s\n",
1577 fname, n_packets, g10_errstr(rc));
1578 iobuf_close(fp);
1579 if (secret)
1580 unregister_secured_file (tmpfname);
1581 iobuf_cancel(newfp);
1582 goto leave;
1586 if( mode == 1 || mode == 3 ) { /* insert or update */
1587 rc = write_keyblock (newfp, root);
1588 if (rc) {
1589 iobuf_close(fp);
1590 if (secret)
1591 unregister_secured_file (tmpfname);
1592 iobuf_cancel(newfp);
1593 goto leave;
1597 if( mode == 2 || mode == 3 ) { /* delete or update */
1598 /* copy the rest */
1599 rc = copy_all_packets( fp, newfp );
1600 if( rc != -1 ) {
1601 log_error("%s: copy to `%s' failed: %s\n",
1602 fname, tmpfname, g10_errstr(rc) );
1603 iobuf_close(fp);
1604 if (secret)
1605 unregister_secured_file (tmpfname);
1606 iobuf_cancel(newfp);
1607 goto leave;
1609 rc = 0;
1612 /* close both files */
1613 if( iobuf_close(fp) ) {
1614 rc = gpg_error_from_syserror ();
1615 log_error("%s: close failed: %s\n", fname, strerror(errno) );
1616 goto leave;
1618 if( iobuf_close(newfp) ) {
1619 rc = gpg_error_from_syserror ();
1620 log_error("%s: close failed: %s\n", tmpfname, strerror(errno) );
1621 goto leave;
1624 rc = rename_tmp_file (bakfname, tmpfname, fname, secret);
1626 leave:
1627 xfree(bakfname);
1628 xfree(tmpfname);
1629 return rc;