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
)
649 return G10ERR_INV_ARG
;
651 rc
= keydb_search_reset (hd
); /* this does reset hd->current */
655 /* If we have a primary set, try that one first */
658 for ( ; hd
->current
>= 0 && hd
->current
< hd
->used
; hd
->current
++)
660 if(hd
->active
[hd
->current
].token
==primary_keyring
)
662 if(keyring_is_writable (hd
->active
[hd
->current
].token
))
669 rc
= keydb_search_reset (hd
); /* this does reset hd->current */
674 for ( ; hd
->current
>= 0 && hd
->current
< hd
->used
; hd
->current
++)
676 switch (hd
->active
[hd
->current
].type
)
678 case KEYDB_RESOURCE_TYPE_NONE
:
681 case KEYDB_RESOURCE_TYPE_KEYRING
:
682 if (keyring_is_writable (hd
->active
[hd
->current
].token
))
683 return 0; /* found (hd->current is set to it) */
692 * Rebuild the caches of all key resources.
695 keydb_rebuild_caches (int noisy
)
699 for (i
=0; i
< used_resources
; i
++)
701 if (all_resources
[i
].secret
)
703 switch (all_resources
[i
].type
)
705 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
707 case KEYDB_RESOURCE_TYPE_KEYRING
:
708 rc
= keyring_rebuild_cache (all_resources
[i
].token
,noisy
);
710 log_error (_("failed to rebuild keyring cache: %s\n"),
720 * Start the next search on this handle right at the beginning
723 keydb_search_reset (KEYDB_HANDLE hd
)
728 return G10ERR_INV_ARG
;
732 /* and reset all resources */
733 for (i
=0; !rc
&& i
< hd
->used
; i
++) {
734 switch (hd
->active
[i
].type
) {
735 case KEYDB_RESOURCE_TYPE_NONE
:
737 case KEYDB_RESOURCE_TYPE_KEYRING
:
738 rc
= keyring_search_reset (hd
->active
[i
].u
.kr
);
747 * Search through all keydb resources, starting at the current position,
748 * for a keyblock which contains one of the keys described in the DESC array.
751 keydb_search2 (KEYDB_HANDLE hd
, KEYDB_SEARCH_DESC
*desc
,
752 size_t ndesc
, size_t *descindex
)
757 return G10ERR_INV_ARG
;
759 while (rc
== -1 && hd
->current
>= 0 && hd
->current
< hd
->used
) {
760 switch (hd
->active
[hd
->current
].type
) {
761 case KEYDB_RESOURCE_TYPE_NONE
:
762 BUG(); /* we should never see it here */
764 case KEYDB_RESOURCE_TYPE_KEYRING
:
765 rc
= keyring_search (hd
->active
[hd
->current
].u
.kr
, desc
,
769 if (rc
== -1) /* EOF -> switch to next resource */
772 hd
->found
= hd
->current
;
779 keydb_search_first (KEYDB_HANDLE hd
)
781 KEYDB_SEARCH_DESC desc
;
783 memset (&desc
, 0, sizeof desc
);
784 desc
.mode
= KEYDB_SEARCH_MODE_FIRST
;
785 return keydb_search (hd
, &desc
, 1);
789 keydb_search_next (KEYDB_HANDLE hd
)
791 KEYDB_SEARCH_DESC desc
;
793 memset (&desc
, 0, sizeof desc
);
794 desc
.mode
= KEYDB_SEARCH_MODE_NEXT
;
795 return keydb_search (hd
, &desc
, 1);
799 keydb_search_kid (KEYDB_HANDLE hd
, u32
*kid
)
801 KEYDB_SEARCH_DESC desc
;
803 memset (&desc
, 0, sizeof desc
);
804 desc
.mode
= KEYDB_SEARCH_MODE_LONG_KID
;
805 desc
.u
.kid
[0] = kid
[0];
806 desc
.u
.kid
[1] = kid
[1];
807 return keydb_search (hd
, &desc
, 1);
811 keydb_search_fpr (KEYDB_HANDLE hd
, const byte
*fpr
)
813 KEYDB_SEARCH_DESC desc
;
815 memset (&desc
, 0, sizeof desc
);
816 desc
.mode
= KEYDB_SEARCH_MODE_FPR
;
817 memcpy (desc
.u
.fpr
, fpr
, MAX_FINGERPRINT_LEN
);
818 return keydb_search (hd
, &desc
, 1);