1 /* keydb.c - key database dispatcher
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 * 2008, 2009 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.
213 * Flag 8 - Open as read-only.
216 keydb_add_resource (const char *url
, int flags
, int secret
)
218 static int any_secret
, any_public
;
219 const char *resname
= url
;
220 char *filename
= NULL
;
221 int force
= (flags
&1);
222 int readonly
= !!(flags
&8);
224 KeydbResourceType rt
= KEYDB_RESOURCE_TYPE_NONE
;
230 /* Do we have an URL?
231 * gnupg-ring:filename := this is a plain keyring
232 * filename := See what is is, but create as plain keyring.
234 if (strlen (resname
) > 11) {
235 if (!strncmp( resname
, "gnupg-ring:", 11) ) {
236 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
239 #if !defined(HAVE_DRIVE_LETTERS) && !defined(__riscos__)
240 else if (strchr (resname
, ':')) {
241 log_error ("invalid key resource URL `%s'\n", url
);
245 #endif /* !HAVE_DRIVE_LETTERS && !__riscos__ */
248 if (*resname
!= DIRSEP_C
) { /* do tilde expansion etc */
249 if (strchr(resname
, DIRSEP_C
) )
250 filename
= make_filename (resname
, NULL
);
252 filename
= make_filename (opt
.homedir
, resname
, NULL
);
255 filename
= xstrdup (resname
);
257 if (!force
&& !readonly
)
258 force
= secret
? !any_secret
: !any_public
;
260 /* See whether we can determine the filetype. */
261 if (rt
== KEYDB_RESOURCE_TYPE_NONE
) {
262 FILE *fp
= fopen( filename
, "rb" );
267 if (fread( &magic
, 4, 1, fp
) == 1 ) {
268 if (magic
== 0x13579ace || magic
== 0xce9a5713)
269 ; /* GDBM magic - no more support */
271 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
273 else /* maybe empty: assume ring */
274 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
277 else /* no file yet: create ring */
278 rt
= KEYDB_RESOURCE_TYPE_KEYRING
;
282 case KEYDB_RESOURCE_TYPE_NONE
:
283 log_error ("unknown type of key resource `%s'\n", url
);
287 case KEYDB_RESOURCE_TYPE_KEYRING
:
288 rc
= maybe_create_keyring (filename
, force
);
292 if(keyring_register_filename (filename
, secret
, readonly
, &token
))
294 if (used_resources
>= MAX_KEYDB_RESOURCES
)
295 rc
= G10ERR_RESOURCE_LIMIT
;
299 primary_keyring
=token
;
300 all_resources
[used_resources
].type
= rt
;
301 all_resources
[used_resources
].u
.kr
= NULL
; /* Not used here */
302 all_resources
[used_resources
].token
= token
;
303 all_resources
[used_resources
].secret
= secret
;
309 /* This keyring was already registered, so ignore it.
310 However, we can still mark it as primary even if it was
311 already registered. */
313 primary_keyring
=token
;
318 log_error ("resource type of `%s' not supported\n", url
);
323 /* fixme: check directory permissions and print a warning */
328 /* Secret keyrings are not required in all cases. To avoid
329 having gpg return failure we use log_info here if the
330 rewsource is a secret one and marked as default
332 if ((flags
&4) && secret
)
333 log_info (_("keyblock resource `%s': %s\n"),
334 filename
, g10_errstr(rc
));
336 log_error (_("keyblock resource `%s': %s\n"),
337 filename
, g10_errstr(rc
));
351 keydb_new (int secret
)
356 hd
= xmalloc_clear (sizeof *hd
);
359 assert (used_resources
<= MAX_KEYDB_RESOURCES
);
360 for (i
=j
=0; i
< used_resources
; i
++)
362 if (!all_resources
[i
].secret
!= !secret
)
364 switch (all_resources
[i
].type
)
366 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
368 case KEYDB_RESOURCE_TYPE_KEYRING
:
369 hd
->active
[j
].type
= all_resources
[i
].type
;
370 hd
->active
[j
].token
= all_resources
[i
].token
;
371 hd
->active
[j
].secret
= all_resources
[i
].secret
;
372 hd
->active
[j
].u
.kr
= keyring_new (all_resources
[i
].token
, secret
);
373 if (!hd
->active
[j
].u
.kr
) {
375 return NULL
; /* fixme: release all previously allocated handles*/
388 keydb_release (KEYDB_HANDLE hd
)
394 assert (active_handles
> 0);
398 for (i
=0; i
< hd
->used
; i
++) {
399 switch (hd
->active
[i
].type
) {
400 case KEYDB_RESOURCE_TYPE_NONE
:
402 case KEYDB_RESOURCE_TYPE_KEYRING
:
403 keyring_release (hd
->active
[i
].u
.kr
);
413 * Return the name of the current resource. This is function first
414 * looks for the last found found, then for the current search
415 * position, and last returns the first available resource. The
416 * returned string is only valid as long as the handle exists. This
417 * function does only return NULL if no handle is specified, in all
418 * other error cases an empty string is returned.
421 keydb_get_resource_name (KEYDB_HANDLE hd
)
424 const char *s
= NULL
;
429 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
431 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
436 switch (hd
->active
[idx
].type
) {
437 case KEYDB_RESOURCE_TYPE_NONE
:
440 case KEYDB_RESOURCE_TYPE_KEYRING
:
441 s
= keyring_get_resource_name (hd
->active
[idx
].u
.kr
);
451 lock_all (KEYDB_HANDLE hd
)
455 for (i
=0; !rc
&& i
< hd
->used
; i
++) {
456 switch (hd
->active
[i
].type
) {
457 case KEYDB_RESOURCE_TYPE_NONE
:
459 case KEYDB_RESOURCE_TYPE_KEYRING
:
460 rc
= keyring_lock (hd
->active
[i
].u
.kr
, 1);
466 /* revert the already set locks */
467 for (i
--; i
>= 0; i
--) {
468 switch (hd
->active
[i
].type
) {
469 case KEYDB_RESOURCE_TYPE_NONE
:
471 case KEYDB_RESOURCE_TYPE_KEYRING
:
472 keyring_lock (hd
->active
[i
].u
.kr
, 0);
484 unlock_all (KEYDB_HANDLE hd
)
491 for (i
=hd
->used
-1; i
>= 0; i
--) {
492 switch (hd
->active
[i
].type
) {
493 case KEYDB_RESOURCE_TYPE_NONE
:
495 case KEYDB_RESOURCE_TYPE_KEYRING
:
496 keyring_lock (hd
->active
[i
].u
.kr
, 0);
505 * Return the last found keyring. Caller must free it.
506 * The returned keyblock has the kbode flag bit 0 set for the node with
507 * the public key used to locate the keyblock or flag bit 1 set for
511 keydb_get_keyblock (KEYDB_HANDLE hd
, KBNODE
*ret_kb
)
516 return G10ERR_INV_ARG
;
518 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
519 return -1; /* nothing found */
521 switch (hd
->active
[hd
->found
].type
) {
522 case KEYDB_RESOURCE_TYPE_NONE
:
523 rc
= G10ERR_GENERAL
; /* oops */
525 case KEYDB_RESOURCE_TYPE_KEYRING
:
526 rc
= keyring_get_keyblock (hd
->active
[hd
->found
].u
.kr
, ret_kb
);
534 * update the current keyblock with KB
537 keydb_update_keyblock (KEYDB_HANDLE hd
, KBNODE kb
)
542 return G10ERR_INV_ARG
;
544 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
545 return -1; /* nothing found */
554 switch (hd
->active
[hd
->found
].type
) {
555 case KEYDB_RESOURCE_TYPE_NONE
:
556 rc
= G10ERR_GENERAL
; /* oops */
558 case KEYDB_RESOURCE_TYPE_KEYRING
:
559 rc
= keyring_update_keyblock (hd
->active
[hd
->found
].u
.kr
, kb
);
569 * Insert a new KB into one of the resources.
572 keydb_insert_keyblock (KEYDB_HANDLE hd
, KBNODE kb
)
578 return G10ERR_INV_ARG
;
583 if ( hd
->found
>= 0 && hd
->found
< hd
->used
)
585 else if ( hd
->current
>= 0 && hd
->current
< hd
->used
)
588 return G10ERR_GENERAL
;
594 switch (hd
->active
[idx
].type
) {
595 case KEYDB_RESOURCE_TYPE_NONE
:
596 rc
= G10ERR_GENERAL
; /* oops */
598 case KEYDB_RESOURCE_TYPE_KEYRING
:
599 rc
= keyring_insert_keyblock (hd
->active
[idx
].u
.kr
, kb
);
609 * The current keyblock will be deleted.
612 keydb_delete_keyblock (KEYDB_HANDLE hd
)
617 return G10ERR_INV_ARG
;
619 if ( hd
->found
< 0 || hd
->found
>= hd
->used
)
620 return -1; /* nothing found */
629 switch (hd
->active
[hd
->found
].type
) {
630 case KEYDB_RESOURCE_TYPE_NONE
:
631 rc
= G10ERR_GENERAL
; /* oops */
633 case KEYDB_RESOURCE_TYPE_KEYRING
:
634 rc
= keyring_delete_keyblock (hd
->active
[hd
->found
].u
.kr
);
644 * Locate the default writable key resource, so that the next
645 * operation (which is only relevant for inserts) will be done on this
649 keydb_locate_writable (KEYDB_HANDLE hd
, const char *reserved
)
656 return G10ERR_INV_ARG
;
658 rc
= keydb_search_reset (hd
); /* this does reset hd->current */
662 /* If we have a primary set, try that one first */
665 for ( ; hd
->current
>= 0 && hd
->current
< hd
->used
; hd
->current
++)
667 if(hd
->active
[hd
->current
].token
==primary_keyring
)
669 if(keyring_is_writable (hd
->active
[hd
->current
].token
))
676 rc
= keydb_search_reset (hd
); /* this does reset hd->current */
681 for ( ; hd
->current
>= 0 && hd
->current
< hd
->used
; hd
->current
++)
683 switch (hd
->active
[hd
->current
].type
)
685 case KEYDB_RESOURCE_TYPE_NONE
:
688 case KEYDB_RESOURCE_TYPE_KEYRING
:
689 if (keyring_is_writable (hd
->active
[hd
->current
].token
))
690 return 0; /* found (hd->current is set to it) */
699 * Rebuild the caches of all key resources.
702 keydb_rebuild_caches (int noisy
)
706 for (i
=0; i
< used_resources
; i
++)
708 if (all_resources
[i
].secret
)
710 if (!keyring_is_writable (all_resources
[i
].token
))
712 switch (all_resources
[i
].type
)
714 case KEYDB_RESOURCE_TYPE_NONE
: /* ignore */
716 case KEYDB_RESOURCE_TYPE_KEYRING
:
717 rc
= keyring_rebuild_cache (all_resources
[i
].token
,noisy
);
719 log_error (_("failed to rebuild keyring cache: %s\n"),
729 * Start the next search on this handle right at the beginning
732 keydb_search_reset (KEYDB_HANDLE hd
)
737 return G10ERR_INV_ARG
;
741 /* and reset all resources */
742 for (i
=0; !rc
&& i
< hd
->used
; i
++) {
743 switch (hd
->active
[i
].type
) {
744 case KEYDB_RESOURCE_TYPE_NONE
:
746 case KEYDB_RESOURCE_TYPE_KEYRING
:
747 rc
= keyring_search_reset (hd
->active
[i
].u
.kr
);
756 * Search through all keydb resources, starting at the current position,
757 * for a keyblock which contains one of the keys described in the DESC array.
760 keydb_search2 (KEYDB_HANDLE hd
, KEYDB_SEARCH_DESC
*desc
,
761 size_t ndesc
, size_t *descindex
)
766 return G10ERR_INV_ARG
;
768 while (rc
== -1 && hd
->current
>= 0 && hd
->current
< hd
->used
) {
769 switch (hd
->active
[hd
->current
].type
) {
770 case KEYDB_RESOURCE_TYPE_NONE
:
771 BUG(); /* we should never see it here */
773 case KEYDB_RESOURCE_TYPE_KEYRING
:
774 rc
= keyring_search (hd
->active
[hd
->current
].u
.kr
, desc
,
778 if (rc
== -1) /* EOF -> switch to next resource */
781 hd
->found
= hd
->current
;
788 keydb_search_first (KEYDB_HANDLE hd
)
790 KEYDB_SEARCH_DESC desc
;
792 memset (&desc
, 0, sizeof desc
);
793 desc
.mode
= KEYDB_SEARCH_MODE_FIRST
;
794 return keydb_search (hd
, &desc
, 1);
798 keydb_search_next (KEYDB_HANDLE hd
)
800 KEYDB_SEARCH_DESC desc
;
802 memset (&desc
, 0, sizeof desc
);
803 desc
.mode
= KEYDB_SEARCH_MODE_NEXT
;
804 return keydb_search (hd
, &desc
, 1);
808 keydb_search_kid (KEYDB_HANDLE hd
, u32
*kid
)
810 KEYDB_SEARCH_DESC desc
;
812 memset (&desc
, 0, sizeof desc
);
813 desc
.mode
= KEYDB_SEARCH_MODE_LONG_KID
;
814 desc
.u
.kid
[0] = kid
[0];
815 desc
.u
.kid
[1] = kid
[1];
816 return keydb_search (hd
, &desc
, 1);
820 keydb_search_fpr (KEYDB_HANDLE hd
, const byte
*fpr
)
822 KEYDB_SEARCH_DESC desc
;
824 memset (&desc
, 0, sizeof desc
);
825 desc
.mode
= KEYDB_SEARCH_MODE_FPR
;
826 memcpy (desc
.u
.fpr
, fpr
, MAX_FINGERPRINT_LEN
);
827 return keydb_search (hd
, &desc
, 1);