1 /* keydb.c - key database dispatcher
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005 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,
28 #include <sys/types.h>
35 #include "main.h" /*try_make_homedir ()*/
41 static int active_handles
;
44 KEYDB_RESOURCE_TYPE_NONE
= 0,
45 KEYDB_RESOURCE_TYPE_KEYRING
47 #define MAX_KEYDB_RESOURCES 40
49 struct resource_item
{
50 KeydbResourceType type
;
58 static struct resource_item all_resources
[MAX_KEYDB_RESOURCES
];
59 static int used_resources
;
60 static void *primary_keyring
=NULL
;
66 int used
; /* items in active */
67 struct resource_item active
[MAX_KEYDB_RESOURCES
];
71 static int lock_all (KEYDB_HANDLE hd
);
72 static void unlock_all (KEYDB_HANDLE hd
);
75 /* Handle the creation of a keyring if it does not yet exist. Take
76 into acount that other processes might have the keyring already
77 locked. This lock check does not work if the directory itself is
80 maybe_create_keyring (char *filename
, int force
)
82 DOTLOCK lockhd
= NULL
;
86 char *last_slash_in_filename
;
88 /* A quick test whether the filename already exists. */
89 if (!access (filename
, F_OK
))
92 /* If we don't want to create a new file at all, there is no need to
93 go any further - bail out right here. */
95 return gpg_error (GPG_ERR_ENOENT
);
97 /* First of all we try to create the home directory. Note, that we
98 don't do any locking here because any sane application of gpg
99 would create the home directory by itself and not rely on gpg's
100 tricky auto-creation which is anyway only done for some home
101 directory name patterns. */
102 last_slash_in_filename
= strrchr (filename
, DIRSEP_C
);
103 *last_slash_in_filename
= 0;
104 if (access(filename
, F_OK
))
111 try_make_homedir (filename
);
113 if (access (filename
, F_OK
))
115 rc
= gpg_error_from_errno (errno
);
116 *last_slash_in_filename
= DIRSEP_C
;
120 *last_slash_in_filename
= DIRSEP_C
;
123 /* To avoid races with other instances of gpg trying to create or
124 update the keyring (it is removed during an update for a short
125 time), we do the next stuff in a locked state. */
126 lockhd
= create_dotlock (filename
);
129 /* A reason for this to fail is that the directory is not
130 writable. However, this whole locking stuff does not make
131 sense if this is the case. An empty non-writable directory
132 with no keyring is not really useful at all. */
134 log_info ("can't allocate lock for `%s'\n", filename
);
137 return gpg_error (GPG_ERR_ENOENT
);
139 return gpg_error (GPG_ERR_GENERAL
);
142 if ( make_dotlock (lockhd
, -1) )
144 /* This is something bad. Probably a stale lockfile. */
145 log_info ("can't lock `%s'\n", filename
);
150 /* Now the real test while we are locked. */
151 if (!access(filename
, F_OK
))
153 rc
= 0; /* Okay, we may access the file now. */
157 /* The file does not yet exist, create it now. */
158 oldmask
= umask (077);
159 if (is_secured_filename (filename
))
165 iobuf
= iobuf_create (filename
);
169 rc
= gpg_error_from_errno (errno
);
170 log_error ( _("error creating keyring `%s': %s\n"),
171 filename
, strerror(errno
));
176 log_info (_("keyring `%s' created\n"), filename
);
179 /* Must invalidate that ugly cache */
180 iobuf_ioctl (NULL
, 2, 0, filename
);
186 release_dotlock (lockhd
);
187 destroy_dotlock (lockhd
);
194 * Register a resource (which currently may only be a keyring file).
195 * The first keyring which is added by this function is
196 * created if it does not exist.
197 * Note: this function may be called before secure memory is
200 * Flag 2 == mark resource as primary
201 * Flag 4 == This is a default resources
204 keydb_add_resource (const char *url
, int flags
, int secret
)
206 static int any_secret
, any_public
;
207 const char *resname
= url
;
208 char *filename
= NULL
;
211 KeydbResourceType rt
= KEYDB_RESOURCE_TYPE_NONE
;
214 /* Do we have an URL?
215 * gnupg-ring:filename := this is a plain keyring
216 * filename := See what is is, but create as plain keyring.
218 if (strlen (resname
) > 11) {
219 if (!strncmp( resname
, "gnupg-ring:", 11) ) {
220 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
223 #if !defined(HAVE_DRIVE_LETTERS) && !defined(__riscos__)
224 else if (strchr (resname
, ':')) {
225 log_error ("invalid key resource URL `%s'\n", url
);
229 #endif /* !HAVE_DRIVE_LETTERS && !__riscos__ */
232 if (*resname
!= DIRSEP_C
) { /* do tilde expansion etc */
233 if (strchr(resname
, DIRSEP_C
) )
234 filename
= make_filename (resname
, NULL
);
236 filename
= make_filename (opt
.homedir
, resname
, NULL
);
239 filename
= xstrdup (resname
);
242 force
= secret
? !any_secret
: !any_public
;
244 /* see whether we can determine the filetype */
245 if (rt
== KEYDB_RESOURCE_TYPE_NONE
) {
246 FILE *fp
= fopen( filename
, "rb" );
251 if (fread( &magic
, 4, 1, fp
) == 1 ) {
252 if (magic
== 0x13579ace || magic
== 0xce9a5713)
253 ; /* GDBM magic - no more support */
255 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
257 else /* maybe empty: assume ring */
258 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
261 else /* no file yet: create ring */
262 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
266 case KEYDB_RESOURCE_TYPE_NONE
:
267 log_error ("unknown type of key resource `%s'\n", url
);
271 case KEYDB_RESOURCE_TYPE_KEYRING
:
272 rc
= maybe_create_keyring (filename
, force
);
276 if(keyring_register_filename (filename
, secret
, &token
))
278 if (used_resources
>= MAX_KEYDB_RESOURCES
)
279 rc
= G10ERR_RESOURCE_LIMIT
;
283 primary_keyring
=token
;
284 all_resources
[used_resources
].type
= rt
;
285 all_resources
[used_resources
].u
.kr
= NULL
; /* Not used here */
286 all_resources
[used_resources
].token
= token
;
287 all_resources
[used_resources
].secret
= secret
;
293 /* This keyring was already registered, so ignore it.
294 However, we can still mark it as primary even if it was
295 already registered. */
297 primary_keyring
=token
;
302 log_error ("resource type of `%s' not supported\n", url
);
307 /* fixme: check directory permissions and print a warning */
312 /* Secret keyrings are not required in all cases. To avoid
313 having gpg return failure we use log_info here if the
314 rewsource is a secret one and marked as default
316 if ((flags
&4) && secret
)
317 log_info (_("keyblock resource `%s': %s\n"),
318 filename
, g10_errstr(rc
));
320 log_error (_("keyblock resource `%s': %s\n"),
321 filename
, g10_errstr(rc
));
335 keydb_new (int secret
)
340 hd
= xmalloc_clear (sizeof *hd
);
343 assert (used_resources
<= MAX_KEYDB_RESOURCES
);
344 for (i
=j
=0; i
< used_resources
; i
++)
346 if (!all_resources
[i
].secret
!= !secret
)
348 switch (all_resources
[i
].type
)
350 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
352 case KEYDB_RESOURCE_TYPE_KEYRING
:
353 hd
->active
[j
].type
= all_resources
[i
].type
;
354 hd
->active
[j
].token
= all_resources
[i
].token
;
355 hd
->active
[j
].secret
= all_resources
[i
].secret
;
356 hd
->active
[j
].u
.kr
= keyring_new (all_resources
[i
].token
, secret
);
357 if (!hd
->active
[j
].u
.kr
) {
359 return NULL
; /* fixme: release all previously allocated handles*/
372 keydb_release (KEYDB_HANDLE hd
)
378 assert (active_handles
> 0);
382 for (i
=0; i
< hd
->used
; i
++) {
383 switch (hd
->active
[i
].type
) {
384 case KEYDB_RESOURCE_TYPE_NONE
:
386 case KEYDB_RESOURCE_TYPE_KEYRING
:
387 keyring_release (hd
->active
[i
].u
.kr
);
397 * Return the name of the current resource. This is function first
398 * looks for the last found found, then for the current search
399 * position, and last returns the first available resource. The
400 * returned string is only valid as long as the handle exists. This
401 * function does only return NULL if no handle is specified, in all
402 * other error cases an empty string is returned.
405 keydb_get_resource_name (KEYDB_HANDLE hd
)
408 const char *s
= NULL
;
413 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
415 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
420 switch (hd
->active
[idx
].type
) {
421 case KEYDB_RESOURCE_TYPE_NONE
:
424 case KEYDB_RESOURCE_TYPE_KEYRING
:
425 s
= keyring_get_resource_name (hd
->active
[idx
].u
.kr
);
435 lock_all (KEYDB_HANDLE hd
)
439 for (i
=0; !rc
&& i
< hd
->used
; i
++) {
440 switch (hd
->active
[i
].type
) {
441 case KEYDB_RESOURCE_TYPE_NONE
:
443 case KEYDB_RESOURCE_TYPE_KEYRING
:
444 rc
= keyring_lock (hd
->active
[i
].u
.kr
, 1);
450 /* revert the already set locks */
451 for (i
--; i
>= 0; i
--) {
452 switch (hd
->active
[i
].type
) {
453 case KEYDB_RESOURCE_TYPE_NONE
:
455 case KEYDB_RESOURCE_TYPE_KEYRING
:
456 keyring_lock (hd
->active
[i
].u
.kr
, 0);
468 unlock_all (KEYDB_HANDLE hd
)
475 for (i
=hd
->used
-1; i
>= 0; i
--) {
476 switch (hd
->active
[i
].type
) {
477 case KEYDB_RESOURCE_TYPE_NONE
:
479 case KEYDB_RESOURCE_TYPE_KEYRING
:
480 keyring_lock (hd
->active
[i
].u
.kr
, 0);
489 * Return the last found keyring. Caller must free it.
490 * The returned keyblock has the kbode flag bit 0 set for the node with
491 * the public key used to locate the keyblock or flag bit 1 set for
495 keydb_get_keyblock (KEYDB_HANDLE hd
, KBNODE
*ret_kb
)
500 return G10ERR_INV_ARG
;
502 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
503 return -1; /* nothing found */
505 switch (hd
->active
[hd
->found
].type
) {
506 case KEYDB_RESOURCE_TYPE_NONE
:
507 rc
= G10ERR_GENERAL
; /* oops */
509 case KEYDB_RESOURCE_TYPE_KEYRING
:
510 rc
= keyring_get_keyblock (hd
->active
[hd
->found
].u
.kr
, ret_kb
);
518 * update the current keyblock with KB
521 keydb_update_keyblock (KEYDB_HANDLE hd
, KBNODE kb
)
526 return G10ERR_INV_ARG
;
528 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
529 return -1; /* nothing found */
538 switch (hd
->active
[hd
->found
].type
) {
539 case KEYDB_RESOURCE_TYPE_NONE
:
540 rc
= G10ERR_GENERAL
; /* oops */
542 case KEYDB_RESOURCE_TYPE_KEYRING
:
543 rc
= keyring_update_keyblock (hd
->active
[hd
->found
].u
.kr
, kb
);
553 * Insert a new KB into one of the resources.
556 keydb_insert_keyblock (KEYDB_HANDLE hd
, KBNODE kb
)
562 return G10ERR_INV_ARG
;
567 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
569 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
572 return G10ERR_GENERAL
;
578 switch (hd
->active
[idx
].type
) {
579 case KEYDB_RESOURCE_TYPE_NONE
:
580 rc
= G10ERR_GENERAL
; /* oops */
582 case KEYDB_RESOURCE_TYPE_KEYRING
:
583 rc
= keyring_insert_keyblock (hd
->active
[idx
].u
.kr
, kb
);
593 * The current keyblock will be deleted.
596 keydb_delete_keyblock (KEYDB_HANDLE hd
)
601 return G10ERR_INV_ARG
;
603 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
604 return -1; /* nothing found */
613 switch (hd
->active
[hd
->found
].type
) {
614 case KEYDB_RESOURCE_TYPE_NONE
:
615 rc
= G10ERR_GENERAL
; /* oops */
617 case KEYDB_RESOURCE_TYPE_KEYRING
:
618 rc
= keyring_delete_keyblock (hd
->active
[hd
->found
].u
.kr
);
628 * Locate the default writable key resource, so that the next
629 * operation (which is only relevant for inserts) will be done on this
633 keydb_locate_writable (KEYDB_HANDLE hd
, const char *reserved
)
638 return G10ERR_INV_ARG
;
640 rc
= keydb_search_reset (hd
); /* this does reset hd->current */
644 /* If we have a primary set, try that one first */
647 for ( ; hd
->current
>= 0 && hd
->current
< hd
->used
; hd
->current
++)
649 if(hd
->active
[hd
->current
].token
==primary_keyring
)
651 if(keyring_is_writable (hd
->active
[hd
->current
].token
))
658 rc
= keydb_search_reset (hd
); /* this does reset hd->current */
663 for ( ; hd
->current
>= 0 && hd
->current
< hd
->used
; hd
->current
++)
665 switch (hd
->active
[hd
->current
].type
)
667 case KEYDB_RESOURCE_TYPE_NONE
:
670 case KEYDB_RESOURCE_TYPE_KEYRING
:
671 if (keyring_is_writable (hd
->active
[hd
->current
].token
))
672 return 0; /* found (hd->current is set to it) */
681 * Rebuild the caches of all key resources.
684 keydb_rebuild_caches (int noisy
)
688 for (i
=0; i
< used_resources
; i
++)
690 if (all_resources
[i
].secret
)
692 switch (all_resources
[i
].type
)
694 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
696 case KEYDB_RESOURCE_TYPE_KEYRING
:
697 rc
= keyring_rebuild_cache (all_resources
[i
].token
,noisy
);
699 log_error (_("failed to rebuild keyring cache: %s\n"),
709 * Start the next search on this handle right at the beginning
712 keydb_search_reset (KEYDB_HANDLE hd
)
717 return G10ERR_INV_ARG
;
721 /* and reset all resources */
722 for (i
=0; !rc
&& i
< hd
->used
; i
++) {
723 switch (hd
->active
[i
].type
) {
724 case KEYDB_RESOURCE_TYPE_NONE
:
726 case KEYDB_RESOURCE_TYPE_KEYRING
:
727 rc
= keyring_search_reset (hd
->active
[i
].u
.kr
);
736 * Search through all keydb resources, starting at the current position,
737 * for a keyblock which contains one of the keys described in the DESC array.
740 keydb_search2 (KEYDB_HANDLE hd
, KEYDB_SEARCH_DESC
*desc
,
741 size_t ndesc
, size_t *descindex
)
746 return G10ERR_INV_ARG
;
748 while (rc
== -1 && hd
->current
>= 0 && hd
->current
< hd
->used
) {
749 switch (hd
->active
[hd
->current
].type
) {
750 case KEYDB_RESOURCE_TYPE_NONE
:
751 BUG(); /* we should never see it here */
753 case KEYDB_RESOURCE_TYPE_KEYRING
:
754 rc
= keyring_search (hd
->active
[hd
->current
].u
.kr
, desc
,
758 if (rc
== -1) /* EOF -> switch to next resource */
761 hd
->found
= hd
->current
;
768 keydb_search_first (KEYDB_HANDLE hd
)
770 KEYDB_SEARCH_DESC desc
;
772 memset (&desc
, 0, sizeof desc
);
773 desc
.mode
= KEYDB_SEARCH_MODE_FIRST
;
774 return keydb_search (hd
, &desc
, 1);
778 keydb_search_next (KEYDB_HANDLE hd
)
780 KEYDB_SEARCH_DESC desc
;
782 memset (&desc
, 0, sizeof desc
);
783 desc
.mode
= KEYDB_SEARCH_MODE_NEXT
;
784 return keydb_search (hd
, &desc
, 1);
788 keydb_search_kid (KEYDB_HANDLE hd
, u32
*kid
)
790 KEYDB_SEARCH_DESC desc
;
792 memset (&desc
, 0, sizeof desc
);
793 desc
.mode
= KEYDB_SEARCH_MODE_LONG_KID
;
794 desc
.u
.kid
[0] = kid
[0];
795 desc
.u
.kid
[1] = kid
[1];
796 return keydb_search (hd
, &desc
, 1);
800 keydb_search_fpr (KEYDB_HANDLE hd
, const byte
*fpr
)
802 KEYDB_SEARCH_DESC desc
;
804 memset (&desc
, 0, sizeof desc
);
805 desc
.mode
= KEYDB_SEARCH_MODE_FPR
;
806 memcpy (desc
.u
.fpr
, fpr
, MAX_FINGERPRINT_LEN
);
807 return keydb_search (hd
, &desc
, 1);