1 /* keydb.c - key database dispatcher
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 * 2008 Free Software Foundation, Inc.
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * GnuPG is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, see <http://www.gnu.org/licenses/>.
27 #include <sys/types.h>
34 #include "main.h" /*try_make_homedir ()*/
40 static int active_handles
;
43 KEYDB_RESOURCE_TYPE_NONE
= 0,
44 KEYDB_RESOURCE_TYPE_KEYRING
46 #define MAX_KEYDB_RESOURCES 40
48 struct resource_item
{
49 KeydbResourceType type
;
57 static struct resource_item all_resources
[MAX_KEYDB_RESOURCES
];
58 static int used_resources
;
59 static void *primary_keyring
=NULL
;
65 int used
; /* items in active */
66 struct resource_item active
[MAX_KEYDB_RESOURCES
];
70 static int lock_all (KEYDB_HANDLE hd
);
71 static void unlock_all (KEYDB_HANDLE hd
);
74 /* Handle the creation of a keyring if it does not yet exist. Take
75 into acount that other processes might have the keyring already
76 locked. This lock check does not work if the directory itself is
79 maybe_create_keyring (char *filename
, int force
)
81 DOTLOCK lockhd
= NULL
;
85 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
);
105 /* Windows may either have a slash or a backslash. Take care of it. */
106 char *p
= strrchr (filename
, '/');
107 if (!last_slash_in_filename
|| p
> last_slash_in_filename
)
108 last_slash_in_filename
= p
;
110 #endif /*HAVE_W32_SYSTEM*/
111 if (!last_slash_in_filename
)
112 return gpg_error (GPG_ERR_ENOENT
); /* No slash at all - should
113 not happen though. */
114 save_slash
= *last_slash_in_filename
;
115 *last_slash_in_filename
= 0;
116 if (access(filename
, F_OK
))
123 try_make_homedir (filename
);
125 if (access (filename
, F_OK
))
127 rc
= gpg_error_from_syserror ();
128 *last_slash_in_filename
= save_slash
;
132 *last_slash_in_filename
= save_slash
;
134 /* To avoid races with other instances of gpg trying to create or
135 update the keyring (it is removed during an update for a short
136 time), we do the next stuff in a locked state. */
137 lockhd
= create_dotlock (filename
);
140 /* A reason for this to fail is that the directory is not
141 writable. However, this whole locking stuff does not make
142 sense if this is the case. An empty non-writable directory
143 with no keyring is not really useful at all. */
145 log_info ("can't allocate lock for `%s'\n", filename
);
148 return gpg_error (GPG_ERR_ENOENT
);
150 return gpg_error (GPG_ERR_GENERAL
);
153 if ( make_dotlock (lockhd
, -1) )
155 /* This is something bad. Probably a stale lockfile. */
156 log_info ("can't lock `%s'\n", filename
);
161 /* Now the real test while we are locked. */
162 if (!access(filename
, F_OK
))
164 rc
= 0; /* Okay, we may access the file now. */
168 /* The file does not yet exist, create it now. */
169 oldmask
= umask (077);
170 if (is_secured_filename (filename
))
176 iobuf
= iobuf_create (filename
);
180 rc
= gpg_error_from_syserror ();
181 log_error ( _("error creating keyring `%s': %s\n"),
182 filename
, strerror(errno
));
187 log_info (_("keyring `%s' created\n"), filename
);
190 /* Must invalidate that ugly cache */
191 iobuf_ioctl (NULL
, 2, 0, filename
);
197 release_dotlock (lockhd
);
198 destroy_dotlock (lockhd
);
205 * Register a resource (which currently may only be a keyring file).
206 * The first keyring which is added by this function is
207 * created if it does not exist.
208 * Note: this function may be called before secure memory is
211 * Flag 2 == mark resource as primary
212 * Flag 4 == This is a default resources
215 keydb_add_resource (const char *url
, int flags
, int secret
)
217 static int any_secret
, any_public
;
218 const char *resname
= url
;
219 char *filename
= NULL
;
222 KeydbResourceType rt
= KEYDB_RESOURCE_TYPE_NONE
;
225 /* Do we have an URL?
226 * gnupg-ring:filename := this is a plain keyring
227 * filename := See what is is, but create as plain keyring.
229 if (strlen (resname
) > 11) {
230 if (!strncmp( resname
, "gnupg-ring:", 11) ) {
231 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
234 #if !defined(HAVE_DRIVE_LETTERS) && !defined(__riscos__)
235 else if (strchr (resname
, ':')) {
236 log_error ("invalid key resource URL `%s'\n", url
);
240 #endif /* !HAVE_DRIVE_LETTERS && !__riscos__ */
243 if (*resname
!= DIRSEP_C
) { /* do tilde expansion etc */
244 if (strchr(resname
, DIRSEP_C
) )
245 filename
= make_filename (resname
, NULL
);
247 filename
= make_filename (opt
.homedir
, resname
, NULL
);
250 filename
= xstrdup (resname
);
253 force
= secret
? !any_secret
: !any_public
;
255 /* see whether we can determine the filetype */
256 if (rt
== KEYDB_RESOURCE_TYPE_NONE
) {
257 FILE *fp
= fopen( filename
, "rb" );
262 if (fread( &magic
, 4, 1, fp
) == 1 ) {
263 if (magic
== 0x13579ace || magic
== 0xce9a5713)
264 ; /* GDBM magic - no more support */
266 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
268 else /* maybe empty: assume ring */
269 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
272 else /* no file yet: create ring */
273 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
277 case KEYDB_RESOURCE_TYPE_NONE
:
278 log_error ("unknown type of key resource `%s'\n", url
);
282 case KEYDB_RESOURCE_TYPE_KEYRING
:
283 rc
= maybe_create_keyring (filename
, force
);
287 if(keyring_register_filename (filename
, secret
, &token
))
289 if (used_resources
>= MAX_KEYDB_RESOURCES
)
290 rc
= G10ERR_RESOURCE_LIMIT
;
294 primary_keyring
=token
;
295 all_resources
[used_resources
].type
= rt
;
296 all_resources
[used_resources
].u
.kr
= NULL
; /* Not used here */
297 all_resources
[used_resources
].token
= token
;
298 all_resources
[used_resources
].secret
= secret
;
304 /* This keyring was already registered, so ignore it.
305 However, we can still mark it as primary even if it was
306 already registered. */
308 primary_keyring
=token
;
313 log_error ("resource type of `%s' not supported\n", url
);
318 /* fixme: check directory permissions and print a warning */
323 /* Secret keyrings are not required in all cases. To avoid
324 having gpg return failure we use log_info here if the
325 rewsource is a secret one and marked as default
327 if ((flags
&4) && secret
)
328 log_info (_("keyblock resource `%s': %s\n"),
329 filename
, g10_errstr(rc
));
331 log_error (_("keyblock resource `%s': %s\n"),
332 filename
, g10_errstr(rc
));
346 keydb_new (int secret
)
351 hd
= xmalloc_clear (sizeof *hd
);
354 assert (used_resources
<= MAX_KEYDB_RESOURCES
);
355 for (i
=j
=0; i
< used_resources
; i
++)
357 if (!all_resources
[i
].secret
!= !secret
)
359 switch (all_resources
[i
].type
)
361 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
363 case KEYDB_RESOURCE_TYPE_KEYRING
:
364 hd
->active
[j
].type
= all_resources
[i
].type
;
365 hd
->active
[j
].token
= all_resources
[i
].token
;
366 hd
->active
[j
].secret
= all_resources
[i
].secret
;
367 hd
->active
[j
].u
.kr
= keyring_new (all_resources
[i
].token
, secret
);
368 if (!hd
->active
[j
].u
.kr
) {
370 return NULL
; /* fixme: release all previously allocated handles*/
383 keydb_release (KEYDB_HANDLE hd
)
389 assert (active_handles
> 0);
393 for (i
=0; i
< hd
->used
; i
++) {
394 switch (hd
->active
[i
].type
) {
395 case KEYDB_RESOURCE_TYPE_NONE
:
397 case KEYDB_RESOURCE_TYPE_KEYRING
:
398 keyring_release (hd
->active
[i
].u
.kr
);
408 * Return the name of the current resource. This is function first
409 * looks for the last found found, then for the current search
410 * position, and last returns the first available resource. The
411 * returned string is only valid as long as the handle exists. This
412 * function does only return NULL if no handle is specified, in all
413 * other error cases an empty string is returned.
416 keydb_get_resource_name (KEYDB_HANDLE hd
)
419 const char *s
= NULL
;
424 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
426 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
431 switch (hd
->active
[idx
].type
) {
432 case KEYDB_RESOURCE_TYPE_NONE
:
435 case KEYDB_RESOURCE_TYPE_KEYRING
:
436 s
= keyring_get_resource_name (hd
->active
[idx
].u
.kr
);
446 lock_all (KEYDB_HANDLE hd
)
450 for (i
=0; !rc
&& i
< hd
->used
; i
++) {
451 switch (hd
->active
[i
].type
) {
452 case KEYDB_RESOURCE_TYPE_NONE
:
454 case KEYDB_RESOURCE_TYPE_KEYRING
:
455 rc
= keyring_lock (hd
->active
[i
].u
.kr
, 1);
461 /* revert the already set locks */
462 for (i
--; i
>= 0; i
--) {
463 switch (hd
->active
[i
].type
) {
464 case KEYDB_RESOURCE_TYPE_NONE
:
466 case KEYDB_RESOURCE_TYPE_KEYRING
:
467 keyring_lock (hd
->active
[i
].u
.kr
, 0);
479 unlock_all (KEYDB_HANDLE hd
)
486 for (i
=hd
->used
-1; i
>= 0; i
--) {
487 switch (hd
->active
[i
].type
) {
488 case KEYDB_RESOURCE_TYPE_NONE
:
490 case KEYDB_RESOURCE_TYPE_KEYRING
:
491 keyring_lock (hd
->active
[i
].u
.kr
, 0);
500 * Return the last found keyring. Caller must free it.
501 * The returned keyblock has the kbode flag bit 0 set for the node with
502 * the public key used to locate the keyblock or flag bit 1 set for
506 keydb_get_keyblock (KEYDB_HANDLE hd
, KBNODE
*ret_kb
)
511 return G10ERR_INV_ARG
;
513 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
514 return -1; /* nothing found */
516 switch (hd
->active
[hd
->found
].type
) {
517 case KEYDB_RESOURCE_TYPE_NONE
:
518 rc
= G10ERR_GENERAL
; /* oops */
520 case KEYDB_RESOURCE_TYPE_KEYRING
:
521 rc
= keyring_get_keyblock (hd
->active
[hd
->found
].u
.kr
, ret_kb
);
529 * update the current keyblock with KB
532 keydb_update_keyblock (KEYDB_HANDLE hd
, KBNODE kb
)
537 return G10ERR_INV_ARG
;
539 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
540 return -1; /* nothing found */
549 switch (hd
->active
[hd
->found
].type
) {
550 case KEYDB_RESOURCE_TYPE_NONE
:
551 rc
= G10ERR_GENERAL
; /* oops */
553 case KEYDB_RESOURCE_TYPE_KEYRING
:
554 rc
= keyring_update_keyblock (hd
->active
[hd
->found
].u
.kr
, kb
);
564 * Insert a new KB into one of the resources.
567 keydb_insert_keyblock (KEYDB_HANDLE hd
, KBNODE kb
)
573 return G10ERR_INV_ARG
;
578 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
580 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
583 return G10ERR_GENERAL
;
589 switch (hd
->active
[idx
].type
) {
590 case KEYDB_RESOURCE_TYPE_NONE
:
591 rc
= G10ERR_GENERAL
; /* oops */
593 case KEYDB_RESOURCE_TYPE_KEYRING
:
594 rc
= keyring_insert_keyblock (hd
->active
[idx
].u
.kr
, kb
);
604 * The current keyblock will be deleted.
607 keydb_delete_keyblock (KEYDB_HANDLE hd
)
612 return G10ERR_INV_ARG
;
614 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
615 return -1; /* nothing found */
624 switch (hd
->active
[hd
->found
].type
) {
625 case KEYDB_RESOURCE_TYPE_NONE
:
626 rc
= G10ERR_GENERAL
; /* oops */
628 case KEYDB_RESOURCE_TYPE_KEYRING
:
629 rc
= keyring_delete_keyblock (hd
->active
[hd
->found
].u
.kr
);
639 * Locate the default writable key resource, so that the next
640 * operation (which is only relevant for inserts) will be done on this
644 keydb_locate_writable (KEYDB_HANDLE hd
, const char *reserved
)
651 return G10ERR_INV_ARG
;
653 rc
= keydb_search_reset (hd
); /* this does reset hd->current */
657 /* If we have a primary set, try that one first */
660 for ( ; hd
->current
>= 0 && hd
->current
< hd
->used
; hd
->current
++)
662 if(hd
->active
[hd
->current
].token
==primary_keyring
)
664 if(keyring_is_writable (hd
->active
[hd
->current
].token
))
671 rc
= keydb_search_reset (hd
); /* this does reset hd->current */
676 for ( ; hd
->current
>= 0 && hd
->current
< hd
->used
; hd
->current
++)
678 switch (hd
->active
[hd
->current
].type
)
680 case KEYDB_RESOURCE_TYPE_NONE
:
683 case KEYDB_RESOURCE_TYPE_KEYRING
:
684 if (keyring_is_writable (hd
->active
[hd
->current
].token
))
685 return 0; /* found (hd->current is set to it) */
694 * Rebuild the caches of all key resources.
697 keydb_rebuild_caches (int noisy
)
701 for (i
=0; i
< used_resources
; i
++)
703 if (all_resources
[i
].secret
)
705 switch (all_resources
[i
].type
)
707 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
709 case KEYDB_RESOURCE_TYPE_KEYRING
:
710 rc
= keyring_rebuild_cache (all_resources
[i
].token
,noisy
);
712 log_error (_("failed to rebuild keyring cache: %s\n"),
722 * Start the next search on this handle right at the beginning
725 keydb_search_reset (KEYDB_HANDLE hd
)
730 return G10ERR_INV_ARG
;
734 /* and reset all resources */
735 for (i
=0; !rc
&& i
< hd
->used
; i
++) {
736 switch (hd
->active
[i
].type
) {
737 case KEYDB_RESOURCE_TYPE_NONE
:
739 case KEYDB_RESOURCE_TYPE_KEYRING
:
740 rc
= keyring_search_reset (hd
->active
[i
].u
.kr
);
749 * Search through all keydb resources, starting at the current position,
750 * for a keyblock which contains one of the keys described in the DESC array.
753 keydb_search2 (KEYDB_HANDLE hd
, KEYDB_SEARCH_DESC
*desc
,
754 size_t ndesc
, size_t *descindex
)
759 return G10ERR_INV_ARG
;
761 while (rc
== -1 && hd
->current
>= 0 && hd
->current
< hd
->used
) {
762 switch (hd
->active
[hd
->current
].type
) {
763 case KEYDB_RESOURCE_TYPE_NONE
:
764 BUG(); /* we should never see it here */
766 case KEYDB_RESOURCE_TYPE_KEYRING
:
767 rc
= keyring_search (hd
->active
[hd
->current
].u
.kr
, desc
,
771 if (rc
== -1) /* EOF -> switch to next resource */
774 hd
->found
= hd
->current
;
781 keydb_search_first (KEYDB_HANDLE hd
)
783 KEYDB_SEARCH_DESC desc
;
785 memset (&desc
, 0, sizeof desc
);
786 desc
.mode
= KEYDB_SEARCH_MODE_FIRST
;
787 return keydb_search (hd
, &desc
, 1);
791 keydb_search_next (KEYDB_HANDLE hd
)
793 KEYDB_SEARCH_DESC desc
;
795 memset (&desc
, 0, sizeof desc
);
796 desc
.mode
= KEYDB_SEARCH_MODE_NEXT
;
797 return keydb_search (hd
, &desc
, 1);
801 keydb_search_kid (KEYDB_HANDLE hd
, u32
*kid
)
803 KEYDB_SEARCH_DESC desc
;
805 memset (&desc
, 0, sizeof desc
);
806 desc
.mode
= KEYDB_SEARCH_MODE_LONG_KID
;
807 desc
.u
.kid
[0] = kid
[0];
808 desc
.u
.kid
[1] = kid
[1];
809 return keydb_search (hd
, &desc
, 1);
813 keydb_search_fpr (KEYDB_HANDLE hd
, const byte
*fpr
)
815 KEYDB_SEARCH_DESC desc
;
817 memset (&desc
, 0, sizeof desc
);
818 desc
.mode
= KEYDB_SEARCH_MODE_FPR
;
819 memcpy (desc
.u
.fpr
, fpr
, MAX_FINGERPRINT_LEN
);
820 return keydb_search (hd
, &desc
, 1);