2006-09-06 Marcus Brinkmann <marcus@g10code.de>
[gnupg.git] / sm / keydb.c
blob3fc7d31b931a75e08480bfcb04d631a88e910f8a
1 /* keydb.c - key database dispatcher
2 * Copyright (C) 2001, 2003, 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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19 * USA.
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
32 #include "gpgsm.h"
33 #include "../kbx/keybox.h"
34 #include "keydb.h"
35 #include "i18n.h"
37 static int active_handles;
39 typedef enum {
40 KEYDB_RESOURCE_TYPE_NONE = 0,
41 KEYDB_RESOURCE_TYPE_KEYBOX
42 } KeydbResourceType;
43 #define MAX_KEYDB_RESOURCES 20
45 struct resource_item {
46 KeydbResourceType type;
47 union {
48 KEYBOX_HANDLE kr;
49 } u;
50 void *token;
51 int secret;
52 DOTLOCK lockhandle;
55 static struct resource_item all_resources[MAX_KEYDB_RESOURCES];
56 static int used_resources;
58 struct keydb_handle {
59 int locked;
60 int found;
61 int current;
62 int is_ephemeral;
63 int used; /* items in active */
64 struct resource_item active[MAX_KEYDB_RESOURCES];
68 static int lock_all (KEYDB_HANDLE hd);
69 static void unlock_all (KEYDB_HANDLE hd);
73 * Register a resource (which currently may only be a keybox file).
74 * The first keybox which is added by this function is
75 * created if it does not exist.
76 * Note: this function may be called before secure memory is
77 * available.
79 int
80 keydb_add_resource (const char *url, int force, int secret)
82 static int any_secret, any_public;
83 const char *resname = url;
84 char *filename = NULL;
85 int rc = 0;
86 FILE *fp;
87 KeydbResourceType rt = KEYDB_RESOURCE_TYPE_NONE;
88 const char *created_fname = NULL;
90 /* Do we have an URL?
91 gnupg-kbx:filename := this is a plain keybox
92 filename := See what is is, but create as plain keybox.
94 if (strlen (resname) > 10)
96 if (!strncmp (resname, "gnupg-kbx:", 10) )
98 rt = KEYDB_RESOURCE_TYPE_KEYBOX;
99 resname += 10;
101 #if !defined(HAVE_DRIVE_LETTERS) && !defined(__riscos__)
102 else if (strchr (resname, ':'))
104 log_error ("invalid key resource URL `%s'\n", url );
105 rc = gpg_error (GPG_ERR_GENERAL);
106 goto leave;
108 #endif /* !HAVE_DRIVE_LETTERS && !__riscos__ */
111 if (*resname != DIRSEP_C )
112 { /* do tilde expansion etc */
113 if (strchr(resname, DIRSEP_C) )
114 filename = make_filename (resname, NULL);
115 else
116 filename = make_filename (opt.homedir, resname, NULL);
118 else
119 filename = xstrdup (resname);
121 if (!force)
122 force = secret? !any_secret : !any_public;
124 /* see whether we can determine the filetype */
125 if (rt == KEYDB_RESOURCE_TYPE_NONE)
127 FILE *fp2 = fopen( filename, "rb" );
129 if (fp2) {
130 u32 magic;
132 /* FIXME: check for the keybox magic */
133 if (fread( &magic, 4, 1, fp2) == 1 )
135 if (magic == 0x13579ace || magic == 0xce9a5713)
136 ; /* GDBM magic - no more support */
137 else
138 rt = KEYDB_RESOURCE_TYPE_KEYBOX;
140 else /* maybe empty: assume ring */
141 rt = KEYDB_RESOURCE_TYPE_KEYBOX;
142 fclose (fp2);
144 else /* no file yet: create ring */
145 rt = KEYDB_RESOURCE_TYPE_KEYBOX;
148 switch (rt)
150 case KEYDB_RESOURCE_TYPE_NONE:
151 log_error ("unknown type of key resource `%s'\n", url );
152 rc = gpg_error (GPG_ERR_GENERAL);
153 goto leave;
155 case KEYDB_RESOURCE_TYPE_KEYBOX:
156 fp = fopen (filename, "rb");
157 if (!fp && !force)
159 rc = gpg_error (gpg_err_code_from_errno (errno));
160 goto leave;
163 if (!fp)
164 { /* no file */
165 #if 0 /* no autocreate of the homedirectory yet */
167 char *last_slash_in_filename;
169 last_slash_in_filename = strrchr (filename, DIRSEP_C);
170 *last_slash_in_filename = 0;
171 if (access (filename, F_OK))
172 { /* on the first time we try to create the default
173 homedir and in this case the process will be
174 terminated, so that on the next invocation can
175 read the options file in on startup */
176 try_make_homedir (filename);
177 rc = gpg_error (GPG_ERR_FILE_OPEN_ERROR);
178 *last_slash_in_filename = DIRSEP_C;
179 goto leave;
181 *last_slash_in_filename = DIRSEP_C;
183 #endif
184 fp = fopen (filename, "w");
185 if (!fp)
187 rc = gpg_error (gpg_err_code_from_errno (errno));
188 log_error (_("error creating keybox `%s': %s\n"),
189 filename, strerror(errno));
190 if (errno == ENOENT)
191 log_info (_("you may want to start the gpg-agent first\n"));
192 goto leave;
195 if (!opt.quiet)
196 log_info (_("keybox `%s' created\n"), filename);
197 created_fname = filename;
199 fclose (fp);
200 fp = NULL;
201 /* now register the file */
204 void *token = keybox_register_file (filename, secret);
205 if (!token)
206 ; /* already registered - ignore it */
207 else if (used_resources >= MAX_KEYDB_RESOURCES)
208 rc = gpg_error (GPG_ERR_RESOURCE_LIMIT);
209 else
211 all_resources[used_resources].type = rt;
212 all_resources[used_resources].u.kr = NULL; /* Not used here */
213 all_resources[used_resources].token = token;
214 all_resources[used_resources].secret = secret;
216 all_resources[used_resources].lockhandle
217 = create_dotlock (filename);
218 if (!all_resources[used_resources].lockhandle)
219 log_fatal ( _("can't create lock for `%s'\n"), filename);
221 /* Do a compress run if needed and the file is not locked. */
222 if (!make_dotlock (all_resources[used_resources].lockhandle, 0))
224 KEYBOX_HANDLE kbxhd = keybox_new (token, secret);
226 if (kbxhd)
228 keybox_compress (kbxhd);
229 keybox_release (kbxhd);
231 release_dotlock (all_resources[used_resources].lockhandle);
234 used_resources++;
239 break;
240 default:
241 log_error ("resource type of `%s' not supported\n", url);
242 rc = gpg_error (GPG_ERR_NOT_SUPPORTED);
243 goto leave;
246 /* fixme: check directory permissions and print a warning */
248 leave:
249 if (rc)
250 log_error ("keyblock resource `%s': %s\n", filename, gpg_strerror(rc));
251 else if (secret)
252 any_secret = 1;
253 else
254 any_public = 1;
255 xfree (filename);
256 return rc;
260 KEYDB_HANDLE
261 keydb_new (int secret)
263 KEYDB_HANDLE hd;
264 int i, j;
266 hd = xcalloc (1, sizeof *hd);
267 hd->found = -1;
269 assert (used_resources <= MAX_KEYDB_RESOURCES);
270 for (i=j=0; i < used_resources; i++)
272 if (!all_resources[i].secret != !secret)
273 continue;
274 switch (all_resources[i].type)
276 case KEYDB_RESOURCE_TYPE_NONE: /* ignore */
277 break;
278 case KEYDB_RESOURCE_TYPE_KEYBOX:
279 hd->active[j].type = all_resources[i].type;
280 hd->active[j].token = all_resources[i].token;
281 hd->active[j].secret = all_resources[i].secret;
282 hd->active[j].lockhandle = all_resources[i].lockhandle;
283 hd->active[j].u.kr = keybox_new (all_resources[i].token, secret);
284 if (!hd->active[j].u.kr)
286 xfree (hd);
287 return NULL; /* fixme: release all previously allocated handles*/
289 j++;
290 break;
293 hd->used = j;
295 active_handles++;
296 return hd;
299 void
300 keydb_release (KEYDB_HANDLE hd)
302 int i;
304 if (!hd)
305 return;
306 assert (active_handles > 0);
307 active_handles--;
309 unlock_all (hd);
310 for (i=0; i < hd->used; i++)
312 switch (hd->active[i].type)
314 case KEYDB_RESOURCE_TYPE_NONE:
315 break;
316 case KEYDB_RESOURCE_TYPE_KEYBOX:
317 keybox_release (hd->active[i].u.kr);
318 break;
322 xfree (hd);
326 /* Return the name of the current resource. This is function first
327 looks for the last found found, then for the current search
328 position, and last returns the first available resource. The
329 returned string is only valid as long as the handle exists. This
330 function does only return NULL if no handle is specified, in all
331 other error cases an empty string is returned. */
332 const char *
333 keydb_get_resource_name (KEYDB_HANDLE hd)
335 int idx;
336 const char *s = NULL;
338 if (!hd)
339 return NULL;
341 if ( hd->found >= 0 && hd->found < hd->used)
342 idx = hd->found;
343 else if ( hd->current >= 0 && hd->current < hd->used)
344 idx = hd->current;
345 else
346 idx = 0;
348 switch (hd->active[idx].type)
350 case KEYDB_RESOURCE_TYPE_NONE:
351 s = NULL;
352 break;
353 case KEYDB_RESOURCE_TYPE_KEYBOX:
354 s = keybox_get_resource_name (hd->active[idx].u.kr);
355 break;
358 return s? s: "";
361 /* Switch the handle into ephemeral mode and return the orginal value. */
363 keydb_set_ephemeral (KEYDB_HANDLE hd, int yes)
365 int i;
367 if (!hd)
368 return 0;
370 yes = !!yes;
371 if (hd->is_ephemeral != yes)
373 for (i=0; i < hd->used; i++)
375 switch (hd->active[i].type)
377 case KEYDB_RESOURCE_TYPE_NONE:
378 break;
379 case KEYDB_RESOURCE_TYPE_KEYBOX:
380 keybox_set_ephemeral (hd->active[i].u.kr, yes);
381 break;
386 i = hd->is_ephemeral;
387 hd->is_ephemeral = yes;
388 return i;
392 /* If the keyring has not yet been locked, lock it now. This
393 operation is required before any update opeations; it is optionaly
394 for an insert operation. The lock is released with
395 keydb_released. */
396 gpg_error_t
397 keydb_lock (KEYDB_HANDLE hd)
399 if (!hd)
400 return gpg_error (GPG_ERR_INV_HANDLE);
401 if (hd->locked)
402 return 0; /* Already locked. */
403 return lock_all (hd);
408 static int
409 lock_all (KEYDB_HANDLE hd)
411 int i, rc = 0;
413 /* Fixme: This locking scheme may lead to deadlock if the resources
414 are not added in the same order by all processes. We are
415 currently only allowing one resource so it is not a problem. */
416 for (i=0; i < hd->used; i++)
418 switch (hd->active[i].type)
420 case KEYDB_RESOURCE_TYPE_NONE:
421 break;
422 case KEYDB_RESOURCE_TYPE_KEYBOX:
423 if (hd->active[i].lockhandle)
424 rc = make_dotlock (hd->active[i].lockhandle, -1);
425 break;
427 if (rc)
428 break;
431 if (rc)
433 /* revert the already set locks */
434 for (i--; i >= 0; i--)
436 switch (hd->active[i].type)
438 case KEYDB_RESOURCE_TYPE_NONE:
439 break;
440 case KEYDB_RESOURCE_TYPE_KEYBOX:
441 if (hd->active[i].lockhandle)
442 release_dotlock (hd->active[i].lockhandle);
443 break;
447 else
448 hd->locked = 1;
450 /* make_dotlock () does not yet guarantee that errno is set, thus
451 we can't rely on the error reason and will simply use
452 EACCES. */
453 return rc? gpg_error (GPG_ERR_EACCES) : 0;
456 static void
457 unlock_all (KEYDB_HANDLE hd)
459 int i;
461 if (!hd->locked)
462 return;
464 for (i=hd->used-1; i >= 0; i--)
466 switch (hd->active[i].type)
468 case KEYDB_RESOURCE_TYPE_NONE:
469 break;
470 case KEYDB_RESOURCE_TYPE_KEYBOX:
471 if (hd->active[i].lockhandle)
472 release_dotlock (hd->active[i].lockhandle);
473 break;
476 hd->locked = 0;
480 #if 0
482 * Return the last found keybox. Caller must free it.
483 * The returned keyblock has the kbode flag bit 0 set for the node with
484 * the public key used to locate the keyblock or flag bit 1 set for
485 * the user ID node.
488 keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb)
490 int rc = 0;
492 if (!hd)
493 return G10ERR_INV_ARG;
495 if ( hd->found < 0 || hd->found >= hd->used)
496 return -1; /* nothing found */
498 switch (hd->active[hd->found].type) {
499 case KEYDB_RESOURCE_TYPE_NONE:
500 rc = G10ERR_GENERAL; /* oops */
501 break;
502 case KEYDB_RESOURCE_TYPE_KEYBOX:
503 rc = keybox_get_keyblock (hd->active[hd->found].u.kr, ret_kb);
504 break;
507 return rc;
511 * update the current keyblock with KB
514 keydb_update_keyblock (KEYDB_HANDLE hd, KBNODE kb)
516 int rc = 0;
518 if (!hd)
519 return G10ERR_INV_ARG;
521 if ( hd->found < 0 || hd->found >= hd->used)
522 return -1; /* nothing found */
524 if( opt.dry_run )
525 return 0;
527 if (!hd->locked)
528 return gpg_error (GPG_ERR_NOT_LOCKED);
530 switch (hd->active[hd->found].type) {
531 case KEYDB_RESOURCE_TYPE_NONE:
532 rc = G10ERR_GENERAL; /* oops */
533 break;
534 case KEYDB_RESOURCE_TYPE_KEYBOX:
535 rc = keybox_update_keyblock (hd->active[hd->found].u.kr, kb);
536 break;
539 unlock_all (hd);
540 return rc;
545 * Insert a new KB into one of the resources.
548 keydb_insert_keyblock (KEYDB_HANDLE hd, KBNODE kb)
550 int rc = -1;
551 int idx;
553 if (!hd)
554 return G10ERR_INV_ARG;
556 if( opt.dry_run )
557 return 0;
559 if ( hd->found >= 0 && hd->found < hd->used)
560 idx = hd->found;
561 else if ( hd->current >= 0 && hd->current < hd->used)
562 idx = hd->current;
563 else
564 return G10ERR_GENERAL;
566 rc = lock_all (hd);
567 if (rc)
568 return rc;
570 switch (hd->active[idx].type) {
571 case KEYDB_RESOURCE_TYPE_NONE:
572 rc = G10ERR_GENERAL; /* oops */
573 break;
574 case KEYDB_RESOURCE_TYPE_KEYBOX:
575 rc = keybox_insert_keyblock (hd->active[idx].u.kr, kb);
576 break;
579 unlock_all (hd);
580 return rc;
583 #endif /*disabled code*/
588 Return the last found object. Caller must free it. The returned
589 keyblock has the kbode flag bit 0 set for the node with the public
590 key used to locate the keyblock or flag bit 1 set for the user ID
591 node. */
593 keydb_get_cert (KEYDB_HANDLE hd, ksba_cert_t *r_cert)
595 int rc = 0;
597 if (!hd)
598 return gpg_error (GPG_ERR_INV_VALUE);
600 if ( hd->found < 0 || hd->found >= hd->used)
601 return -1; /* nothing found */
603 switch (hd->active[hd->found].type)
605 case KEYDB_RESOURCE_TYPE_NONE:
606 rc = gpg_error (GPG_ERR_GENERAL); /* oops */
607 break;
608 case KEYDB_RESOURCE_TYPE_KEYBOX:
609 rc = keybox_get_cert (hd->active[hd->found].u.kr, r_cert);
610 break;
613 return rc;
616 /* Return a flag of the last found object. WHICH is the flag requested;
617 it should be one of the KEYBOX_FLAG_ values. If the operation is
618 successful, the flag value will be stored at the address given by
619 VALUE. Return 0 on success or an error code. */
620 gpg_error_t
621 keydb_get_flags (KEYDB_HANDLE hd, int which, int idx, unsigned int *value)
623 int err = 0;
625 if (!hd)
626 return gpg_error (GPG_ERR_INV_VALUE);
628 if ( hd->found < 0 || hd->found >= hd->used)
629 return gpg_error (GPG_ERR_NOTHING_FOUND);
631 switch (hd->active[hd->found].type)
633 case KEYDB_RESOURCE_TYPE_NONE:
634 err = gpg_error (GPG_ERR_GENERAL); /* oops */
635 break;
636 case KEYDB_RESOURCE_TYPE_KEYBOX:
637 err = keybox_get_flags (hd->active[hd->found].u.kr, which, idx, value);
638 break;
641 return err;
644 /* Set a flag of the last found object. WHICH is the flag to be set; it
645 should be one of the KEYBOX_FLAG_ values. If the operation is
646 successful, the flag value will be stored in the keybox. Note,
647 that some flag values can't be updated and thus may return an
648 error, some other flag values may be masked out before an update.
649 Returns 0 on success or an error code. */
650 gpg_error_t
651 keydb_set_flags (KEYDB_HANDLE hd, int which, int idx, unsigned int value)
653 int err = 0;
655 if (!hd)
656 return gpg_error (GPG_ERR_INV_VALUE);
658 if ( hd->found < 0 || hd->found >= hd->used)
659 return gpg_error (GPG_ERR_NOTHING_FOUND);
661 if (!hd->locked)
662 return gpg_error (GPG_ERR_NOT_LOCKED);
664 switch (hd->active[hd->found].type)
666 case KEYDB_RESOURCE_TYPE_NONE:
667 err = gpg_error (GPG_ERR_GENERAL); /* oops */
668 break;
669 case KEYDB_RESOURCE_TYPE_KEYBOX:
670 err = keybox_set_flags (hd->active[hd->found].u.kr, which, idx, value);
671 break;
674 return err;
678 * Insert a new Certificate into one of the resources.
681 keydb_insert_cert (KEYDB_HANDLE hd, ksba_cert_t cert)
683 int rc = -1;
684 int idx;
685 unsigned char digest[20];
687 if (!hd)
688 return gpg_error (GPG_ERR_INV_VALUE);
690 if (opt.dry_run)
691 return 0;
693 if ( hd->found >= 0 && hd->found < hd->used)
694 idx = hd->found;
695 else if ( hd->current >= 0 && hd->current < hd->used)
696 idx = hd->current;
697 else
698 return gpg_error (GPG_ERR_GENERAL);
700 if (!hd->locked)
701 return gpg_error (GPG_ERR_NOT_LOCKED);
703 gpgsm_get_fingerprint (cert, GCRY_MD_SHA1, digest, NULL); /* kludge*/
705 switch (hd->active[idx].type)
707 case KEYDB_RESOURCE_TYPE_NONE:
708 rc = gpg_error (GPG_ERR_GENERAL);
709 break;
710 case KEYDB_RESOURCE_TYPE_KEYBOX:
711 rc = keybox_insert_cert (hd->active[idx].u.kr, cert, digest);
712 break;
715 unlock_all (hd);
716 return rc;
721 /* update the current keyblock with KB */
723 keydb_update_cert (KEYDB_HANDLE hd, ksba_cert_t cert)
725 int rc = 0;
726 unsigned char digest[20];
728 if (!hd)
729 return gpg_error (GPG_ERR_INV_VALUE);
731 if ( hd->found < 0 || hd->found >= hd->used)
732 return -1; /* nothing found */
734 if (opt.dry_run)
735 return 0;
737 rc = lock_all (hd);
738 if (rc)
739 return rc;
741 gpgsm_get_fingerprint (cert, GCRY_MD_SHA1, digest, NULL); /* kludge*/
743 switch (hd->active[hd->found].type)
745 case KEYDB_RESOURCE_TYPE_NONE:
746 rc = gpg_error (GPG_ERR_GENERAL); /* oops */
747 break;
748 case KEYDB_RESOURCE_TYPE_KEYBOX:
749 rc = keybox_update_cert (hd->active[hd->found].u.kr, cert, digest);
750 break;
753 unlock_all (hd);
754 return rc;
759 * The current keyblock or cert will be deleted.
762 keydb_delete (KEYDB_HANDLE hd, int unlock)
764 int rc = -1;
766 if (!hd)
767 return gpg_error (GPG_ERR_INV_VALUE);
769 if ( hd->found < 0 || hd->found >= hd->used)
770 return -1; /* nothing found */
772 if( opt.dry_run )
773 return 0;
775 if (!hd->locked)
776 return gpg_error (GPG_ERR_NOT_LOCKED);
778 switch (hd->active[hd->found].type)
780 case KEYDB_RESOURCE_TYPE_NONE:
781 rc = gpg_error (GPG_ERR_GENERAL);
782 break;
783 case KEYDB_RESOURCE_TYPE_KEYBOX:
784 rc = keybox_delete (hd->active[hd->found].u.kr);
785 break;
788 if (unlock)
789 unlock_all (hd);
790 return rc;
796 * Locate the default writable key resource, so that the next
797 * operation (which is only relevant for inserts) will be done on this
798 * resource.
801 keydb_locate_writable (KEYDB_HANDLE hd, const char *reserved)
803 int rc;
805 if (!hd)
806 return gpg_error (GPG_ERR_INV_VALUE);
808 rc = keydb_search_reset (hd); /* this does reset hd->current */
809 if (rc)
810 return rc;
812 for ( ; hd->current >= 0 && hd->current < hd->used; hd->current++)
814 switch (hd->active[hd->current].type)
816 case KEYDB_RESOURCE_TYPE_NONE:
817 BUG();
818 break;
819 case KEYDB_RESOURCE_TYPE_KEYBOX:
820 if (keybox_is_writable (hd->active[hd->current].token))
821 return 0; /* found (hd->current is set to it) */
822 break;
826 return -1;
830 * Rebuild the caches of all key resources.
832 void
833 keydb_rebuild_caches (void)
835 int i;
837 for (i=0; i < used_resources; i++)
839 if (all_resources[i].secret)
840 continue;
841 switch (all_resources[i].type)
843 case KEYDB_RESOURCE_TYPE_NONE: /* ignore */
844 break;
845 case KEYDB_RESOURCE_TYPE_KEYBOX:
846 /* rc = keybox_rebuild_cache (all_resources[i].token); */
847 /* if (rc) */
848 /* log_error (_("failed to rebuild keybox cache: %s\n"), */
849 /* g10_errstr (rc)); */
850 break;
858 * Start the next search on this handle right at the beginning
860 int
861 keydb_search_reset (KEYDB_HANDLE hd)
863 int i, rc = 0;
865 if (!hd)
866 return gpg_error (GPG_ERR_INV_VALUE);
868 hd->current = 0;
869 hd->found = -1;
870 /* and reset all resources */
871 for (i=0; !rc && i < hd->used; i++)
873 switch (hd->active[i].type)
875 case KEYDB_RESOURCE_TYPE_NONE:
876 break;
877 case KEYDB_RESOURCE_TYPE_KEYBOX:
878 rc = keybox_search_reset (hd->active[i].u.kr);
879 break;
882 return rc; /* fixme: we need to map error codes or share them with
883 all modules*/
887 * Search through all keydb resources, starting at the current position,
888 * for a keyblock which contains one of the keys described in the DESC array.
890 int
891 keydb_search (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc, size_t ndesc)
893 int rc = -1;
895 if (!hd)
896 return gpg_error (GPG_ERR_INV_VALUE);
898 while (rc == -1 && hd->current >= 0 && hd->current < hd->used)
900 switch (hd->active[hd->current].type)
902 case KEYDB_RESOURCE_TYPE_NONE:
903 BUG(); /* we should never see it here */
904 break;
905 case KEYDB_RESOURCE_TYPE_KEYBOX:
906 rc = keybox_search (hd->active[hd->current].u.kr, desc, ndesc);
907 break;
909 if (rc == -1) /* EOF -> switch to next resource */
910 hd->current++;
911 else if (!rc)
912 hd->found = hd->current;
915 return rc;
920 keydb_search_first (KEYDB_HANDLE hd)
922 KEYDB_SEARCH_DESC desc;
924 memset (&desc, 0, sizeof desc);
925 desc.mode = KEYDB_SEARCH_MODE_FIRST;
926 return keydb_search (hd, &desc, 1);
930 keydb_search_next (KEYDB_HANDLE hd)
932 KEYDB_SEARCH_DESC desc;
934 memset (&desc, 0, sizeof desc);
935 desc.mode = KEYDB_SEARCH_MODE_NEXT;
936 return keydb_search (hd, &desc, 1);
940 keydb_search_kid (KEYDB_HANDLE hd, u32 *kid)
942 KEYDB_SEARCH_DESC desc;
944 memset (&desc, 0, sizeof desc);
945 desc.mode = KEYDB_SEARCH_MODE_LONG_KID;
946 /* desc.u.kid[0] = kid[0]; */
947 /* desc.u.kid[1] = kid[1]; */
948 return keydb_search (hd, &desc, 1);
952 keydb_search_fpr (KEYDB_HANDLE hd, const byte *fpr)
954 KEYDB_SEARCH_DESC desc;
956 memset (&desc, 0, sizeof desc);
957 desc.mode = KEYDB_SEARCH_MODE_FPR;
958 memcpy (desc.u.fpr, fpr, 20);
959 return keydb_search (hd, &desc, 1);
963 keydb_search_issuer (KEYDB_HANDLE hd, const char *issuer)
965 KEYDB_SEARCH_DESC desc;
966 int rc;
968 memset (&desc, 0, sizeof desc);
969 desc.mode = KEYDB_SEARCH_MODE_ISSUER;
970 desc.u.name = issuer;
971 rc = keydb_search (hd, &desc, 1);
972 return rc;
976 keydb_search_issuer_sn (KEYDB_HANDLE hd,
977 const char *issuer, ksba_const_sexp_t serial)
979 KEYDB_SEARCH_DESC desc;
980 int rc;
981 const unsigned char *s;
983 memset (&desc, 0, sizeof desc);
984 desc.mode = KEYDB_SEARCH_MODE_ISSUER_SN;
985 s = serial;
986 if (*s !='(')
987 return gpg_error (GPG_ERR_INV_VALUE);
988 s++;
989 for (desc.snlen = 0; digitp (s); s++)
990 desc.snlen = 10*desc.snlen + atoi_1 (s);
991 if (*s !=':')
992 return gpg_error (GPG_ERR_INV_VALUE);
993 desc.sn = s+1;
994 desc.u.name = issuer;
995 rc = keydb_search (hd, &desc, 1);
996 return rc;
1000 keydb_search_subject (KEYDB_HANDLE hd, const char *name)
1002 KEYDB_SEARCH_DESC desc;
1003 int rc;
1005 memset (&desc, 0, sizeof desc);
1006 desc.mode = KEYDB_SEARCH_MODE_SUBJECT;
1007 desc.u.name = name;
1008 rc = keydb_search (hd, &desc, 1);
1009 return rc;
1013 static int
1014 classify_user_id (const char *name,
1015 KEYDB_SEARCH_DESC *desc,
1016 int *force_exact )
1018 const char *s;
1019 int hexprefix = 0;
1020 int hexlength;
1021 int mode = 0;
1023 /* clear the structure so that the mode field is set to zero unless
1024 * we set it to the correct value right at the end of this function */
1025 memset (desc, 0, sizeof *desc);
1026 *force_exact = 0;
1027 /* Skip leading spaces. Fixme: what about trailing white space? */
1028 for(s = name; *s && spacep (s); s++ )
1031 switch (*s)
1033 case 0: /* empty string is an error */
1034 return 0;
1036 case '.': /* an email address, compare from end */
1037 mode = KEYDB_SEARCH_MODE_MAILEND;
1038 s++;
1039 desc->u.name = s;
1040 break;
1042 case '<': /* an email address */
1043 mode = KEYDB_SEARCH_MODE_MAIL;
1044 s++;
1045 desc->u.name = s;
1046 break;
1048 case '@': /* part of an email address */
1049 mode = KEYDB_SEARCH_MODE_MAILSUB;
1050 s++;
1051 desc->u.name = s;
1052 break;
1054 case '=': /* exact compare */
1055 mode = KEYDB_SEARCH_MODE_EXACT;
1056 s++;
1057 desc->u.name = s;
1058 break;
1060 case '*': /* case insensitive substring search */
1061 mode = KEYDB_SEARCH_MODE_SUBSTR;
1062 s++;
1063 desc->u.name = s;
1064 break;
1066 case '+': /* compare individual words */
1067 mode = KEYDB_SEARCH_MODE_WORDS;
1068 s++;
1069 desc->u.name = s;
1070 break;
1072 case '/': /* subject's DN */
1073 s++;
1074 if (!*s || spacep (s))
1075 return 0; /* no DN or prefixed with a space */
1076 desc->u.name = s;
1077 mode = KEYDB_SEARCH_MODE_SUBJECT;
1078 break;
1080 case '#':
1082 const char *si;
1084 s++;
1085 if ( *s == '/')
1086 { /* "#/" indicates an issuer's DN */
1087 s++;
1088 if (!*s || spacep (s))
1089 return 0; /* no DN or prefixed with a space */
1090 desc->u.name = s;
1091 mode = KEYDB_SEARCH_MODE_ISSUER;
1093 else
1094 { /* serialnumber + optional issuer ID */
1095 for (si=s; *si && *si != '/'; si++)
1097 if (!strchr("01234567890abcdefABCDEF", *si))
1098 return 0; /* invalid digit in serial number*/
1100 desc->sn = (const unsigned char*)s;
1101 desc->snlen = -1;
1102 if (!*si)
1103 mode = KEYDB_SEARCH_MODE_SN;
1104 else
1106 s = si+1;
1107 if (!*s || spacep (s))
1108 return 0; /* no DN or prefixed with a space */
1109 desc->u.name = s;
1110 mode = KEYDB_SEARCH_MODE_ISSUER_SN;
1114 break;
1116 case ':': /*Unified fingerprint */
1118 const char *se, *si;
1119 int i;
1121 se = strchr (++s,':');
1122 if (!se)
1123 return 0;
1124 for (i=0,si=s; si < se; si++, i++ )
1126 if (!strchr("01234567890abcdefABCDEF", *si))
1127 return 0; /* invalid digit */
1129 if (i != 32 && i != 40)
1130 return 0; /* invalid length of fpr*/
1131 for (i=0,si=s; si < se; i++, si +=2)
1132 desc->u.fpr[i] = hextobyte(si);
1133 for (; i < 20; i++)
1134 desc->u.fpr[i]= 0;
1135 s = se + 1;
1136 mode = KEYDB_SEARCH_MODE_FPR;
1138 break;
1140 default:
1141 if (s[0] == '0' && s[1] == 'x')
1143 hexprefix = 1;
1144 s += 2;
1147 hexlength = strspn(s, "0123456789abcdefABCDEF");
1148 if (hexlength >= 8 && s[hexlength] =='!')
1150 *force_exact = 1;
1151 hexlength++; /* just for the following check */
1154 /* check if a hexadecimal number is terminated by EOS or blank */
1155 if (hexlength && s[hexlength] && !spacep (s+hexlength))
1157 if (hexprefix) /* a "0x" prefix without correct */
1158 return 0; /* termination is an error */
1159 /* The first chars looked like a hex number, but really is
1160 not */
1161 hexlength = 0;
1164 if (*force_exact)
1165 hexlength--; /* remove the bang */
1167 if (hexlength == 8
1168 || (!hexprefix && hexlength == 9 && *s == '0'))
1169 { /* short keyid */
1170 unsigned long kid;
1171 if (hexlength == 9)
1172 s++;
1173 kid = strtoul( s, NULL, 16 );
1174 desc->u.kid[4] = kid >> 24;
1175 desc->u.kid[5] = kid >> 16;
1176 desc->u.kid[6] = kid >> 8;
1177 desc->u.kid[7] = kid;
1178 mode = KEYDB_SEARCH_MODE_SHORT_KID;
1180 else if (hexlength == 16
1181 || (!hexprefix && hexlength == 17 && *s == '0'))
1182 { /* complete keyid */
1183 unsigned long kid0, kid1;
1184 char buf[9];
1185 if (hexlength == 17)
1186 s++;
1187 mem2str(buf, s, 9 );
1188 kid0 = strtoul (buf, NULL, 16);
1189 kid1 = strtoul (s+8, NULL, 16);
1190 desc->u.kid[0] = kid0 >> 24;
1191 desc->u.kid[1] = kid0 >> 16;
1192 desc->u.kid[2] = kid0 >> 8;
1193 desc->u.kid[3] = kid0;
1194 desc->u.kid[4] = kid1 >> 24;
1195 desc->u.kid[5] = kid1 >> 16;
1196 desc->u.kid[6] = kid1 >> 8;
1197 desc->u.kid[7] = kid1;
1198 mode = KEYDB_SEARCH_MODE_LONG_KID;
1200 else if (hexlength == 32
1201 || (!hexprefix && hexlength == 33 && *s == '0'))
1202 { /* md5 fingerprint */
1203 int i;
1204 if (hexlength == 33)
1205 s++;
1206 memset(desc->u.fpr+16, 0, 4);
1207 for (i=0; i < 16; i++, s+=2)
1209 int c = hextobyte(s);
1210 if (c == -1)
1211 return 0;
1212 desc->u.fpr[i] = c;
1214 mode = KEYDB_SEARCH_MODE_FPR16;
1216 else if (hexlength == 40
1217 || (!hexprefix && hexlength == 41 && *s == '0'))
1218 { /* sha1/rmd160 fingerprint */
1219 int i;
1220 if (hexlength == 41)
1221 s++;
1222 for (i=0; i < 20; i++, s+=2)
1224 int c = hextobyte(s);
1225 if (c == -1)
1226 return 0;
1227 desc->u.fpr[i] = c;
1229 mode = KEYDB_SEARCH_MODE_FPR20;
1231 else if (!hexprefix)
1233 /* The fingerprint in an X.509 listing is often delimited by
1234 colons, so we try to single this case out. */
1235 mode = 0;
1236 hexlength = strspn (s, ":0123456789abcdefABCDEF");
1237 if (hexlength == 59 && (!s[hexlength] || spacep (s+hexlength)))
1239 int i;
1241 for (i=0; i < 20; i++, s += 3)
1243 int c = hextobyte(s);
1244 if (c == -1 || (i < 19 && s[2] != ':'))
1245 break;
1246 desc->u.fpr[i] = c;
1248 if (i == 20)
1249 mode = KEYDB_SEARCH_MODE_FPR20;
1251 if (!mode) /* default is substring search */
1253 *force_exact = 0;
1254 desc->u.name = s;
1255 mode = KEYDB_SEARCH_MODE_SUBSTR;
1258 else
1259 { /* hex number with a prefix but a wrong length */
1260 return 0;
1264 desc->mode = mode;
1265 return mode;
1270 keydb_classify_name (const char *name, KEYDB_SEARCH_DESC *desc)
1272 int dummy;
1273 KEYDB_SEARCH_DESC dummy_desc;
1275 if (!desc)
1276 desc = &dummy_desc;
1278 if (!classify_user_id (name, desc, &dummy))
1279 return gpg_error (GPG_ERR_INV_NAME);
1280 return 0;
1284 /* Store the certificate in the key DB but make sure that it does not
1285 already exists. We do this simply by comparing the fingerprint.
1286 If EXISTED is not NULL it will be set to true if the certificate
1287 was already in the DB. */
1289 keydb_store_cert (ksba_cert_t cert, int ephemeral, int *existed)
1291 KEYDB_HANDLE kh;
1292 int rc;
1293 unsigned char fpr[20];
1295 if (existed)
1296 *existed = 0;
1298 if (!gpgsm_get_fingerprint (cert, 0, fpr, NULL))
1300 log_error (_("failed to get the fingerprint\n"));
1301 return gpg_error (GPG_ERR_GENERAL);
1304 kh = keydb_new (0);
1305 if (!kh)
1307 log_error (_("failed to allocate keyDB handle\n"));
1308 return gpg_error (GPG_ERR_ENOMEM);;
1311 if (ephemeral)
1312 keydb_set_ephemeral (kh, 1);
1314 rc = lock_all (kh);
1315 if (rc)
1316 return rc;
1318 rc = keydb_search_fpr (kh, fpr);
1319 if (rc != -1)
1321 keydb_release (kh);
1322 if (!rc)
1324 if (existed)
1325 *existed = 1;
1326 return 0; /* okay */
1328 log_error (_("problem looking for existing certificate: %s\n"),
1329 gpg_strerror (rc));
1330 return rc;
1333 rc = keydb_locate_writable (kh, 0);
1334 if (rc)
1336 log_error (_("error finding writable keyDB: %s\n"), gpg_strerror (rc));
1337 keydb_release (kh);
1338 return rc;
1341 rc = keydb_insert_cert (kh, cert);
1342 if (rc)
1344 log_error (_("error storing certificate: %s\n"), gpg_strerror (rc));
1345 keydb_release (kh);
1346 return rc;
1348 keydb_release (kh);
1349 return 0;
1353 /* This is basically keydb_set_flags but it implements a complete
1354 transaction by locating the certificate in the DB and updating the
1355 flags. */
1356 gpg_error_t
1357 keydb_set_cert_flags (ksba_cert_t cert, int which, int idx, unsigned int value)
1359 KEYDB_HANDLE kh;
1360 gpg_error_t err;
1361 unsigned char fpr[20];
1362 unsigned int old_value;
1364 if (!gpgsm_get_fingerprint (cert, 0, fpr, NULL))
1366 log_error (_("failed to get the fingerprint\n"));
1367 return gpg_error (GPG_ERR_GENERAL);
1370 kh = keydb_new (0);
1371 if (!kh)
1373 log_error (_("failed to allocate keyDB handle\n"));
1374 return gpg_error (GPG_ERR_ENOMEM);;
1377 err = keydb_lock (kh);
1378 if (err)
1380 log_error (_("error locking keybox: %s\n"), gpg_strerror (err));
1381 keydb_release (kh);
1382 return err;
1385 err = keydb_search_fpr (kh, fpr);
1386 if (err)
1388 log_error (_("problem re-searching certificate: %s\n"),
1389 gpg_strerror (err));
1390 keydb_release (kh);
1391 return err;
1394 err = keydb_get_flags (kh, which, idx, &old_value);
1395 if (err)
1397 log_error (_("error getting stored flags: %s\n"), gpg_strerror (err));
1398 keydb_release (kh);
1399 return err;
1401 if (value != old_value)
1403 err = keydb_set_flags (kh, which, idx, value);
1404 if (err)
1406 log_error (_("error storing flags: %s\n"), gpg_strerror (err));
1407 keydb_release (kh);
1408 return err;
1411 keydb_release (kh);
1412 return 0;
1416 /* Reset all the certificate flags we have stored with the certificates
1417 for performance reasons. */
1418 void
1419 keydb_clear_some_cert_flags (ctrl_t ctrl, STRLIST names)
1421 gpg_error_t err;
1422 KEYDB_HANDLE hd = NULL;
1423 KEYDB_SEARCH_DESC *desc = NULL;
1424 int ndesc;
1425 STRLIST sl;
1426 int rc=0;
1427 unsigned int old_value, value;
1429 hd = keydb_new (0);
1430 if (!hd)
1432 log_error ("keydb_new failed\n");
1433 goto leave;
1436 if (!names)
1437 ndesc = 1;
1438 else
1440 for (sl=names, ndesc=0; sl; sl = sl->next, ndesc++)
1444 desc = xtrycalloc (ndesc, sizeof *desc);
1445 if (!ndesc)
1447 log_error ("allocating memory failed: %s\n",
1448 gpg_strerror (out_of_core ()));
1449 goto leave;
1452 if (!names)
1453 desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
1454 else
1456 for (ndesc=0, sl=names; sl; sl = sl->next)
1458 rc = keydb_classify_name (sl->d, desc+ndesc);
1459 if (rc)
1461 log_error ("key `%s' not found: %s\n",
1462 sl->d, gpg_strerror (rc));
1463 rc = 0;
1465 else
1466 ndesc++;
1470 err = keydb_lock (hd);
1471 if (err)
1473 log_error (_("error locking keybox: %s\n"), gpg_strerror (err));
1474 goto leave;
1477 while (!(rc = keydb_search (hd, desc, ndesc)))
1479 if (!names)
1480 desc[0].mode = KEYDB_SEARCH_MODE_NEXT;
1482 err = keydb_get_flags (hd, KEYBOX_FLAG_VALIDITY, 0, &old_value);
1483 if (err)
1485 log_error (_("error getting stored flags: %s\n"),
1486 gpg_strerror (err));
1487 goto leave;
1490 value = (old_value & ~VALIDITY_REVOKED);
1491 if (value != old_value)
1493 err = keydb_set_flags (hd, KEYBOX_FLAG_VALIDITY, 0, value);
1494 if (err)
1496 log_error (_("error storing flags: %s\n"), gpg_strerror (err));
1497 goto leave;
1501 if (rc && rc != -1)
1502 log_error ("keydb_search failed: %s\n", gpg_strerror (rc));
1504 leave:
1505 xfree (desc);
1506 keydb_release (hd);