diff options
Diffstat (limited to 'sm/keydb.c')
-rw-r--r-- | sm/keydb.c | 248 |
1 files changed, 123 insertions, 125 deletions
diff --git a/sm/keydb.c b/sm/keydb.c index ddf9a4830..35343f3dc 100644 --- a/sm/keydb.c +++ b/sm/keydb.c @@ -29,7 +29,7 @@ #include "gpgsm.h" #include "../kbx/keybox.h" -#include "keydb.h" +#include "keydb.h" #include "i18n.h" static int active_handles; @@ -71,7 +71,7 @@ static void unlock_all (KEYDB_HANDLE hd); * Register a resource (which currently may only be a keybox file). * The first keybox which is added by this function is created if it * does not exist. If AUTO_CREATED is not NULL it will be set to true - * if the function has created a a new keybox. + * if the function has created a a new keybox. */ int keydb_add_resource (const char *url, int force, int secret, int *auto_created) @@ -79,7 +79,7 @@ keydb_add_resource (const char *url, int force, int secret, int *auto_created) static int any_secret, any_public; const char *resname = url; char *filename = NULL; - int rc = 0; + int rc = 0; FILE *fp; KeydbResourceType rt = KEYDB_RESOURCE_TYPE_NONE; const char *created_fname = NULL; @@ -91,7 +91,7 @@ keydb_add_resource (const char *url, int force, int secret, int *auto_created) gnupg-kbx:filename := this is a plain keybox filename := See what is is, but create as plain keybox. */ - if (strlen (resname) > 10) + if (strlen (resname) > 10) { if (!strncmp (resname, "gnupg-kbx:", 10) ) { @@ -117,20 +117,20 @@ keydb_add_resource (const char *url, int force, int secret, int *auto_created) } else filename = xstrdup (resname); - + if (!force) force = secret? !any_secret : !any_public; - + /* see whether we can determine the filetype */ if (rt == KEYDB_RESOURCE_TYPE_NONE) { FILE *fp2 = fopen( filename, "rb" ); - + if (fp2) { u32 magic; - + /* FIXME: check for the keybox magic */ - if (fread( &magic, 4, 1, fp2) == 1 ) + if (fread( &magic, 4, 1, fp2) == 1 ) { if (magic == 0x13579ace || magic == 0xce9a5713) ; /* GDBM magic - no more support */ @@ -144,14 +144,14 @@ keydb_add_resource (const char *url, int force, int secret, int *auto_created) else /* no file yet: create ring */ rt = KEYDB_RESOURCE_TYPE_KEYBOX; } - + switch (rt) { case KEYDB_RESOURCE_TYPE_NONE: log_error ("unknown type of key resource `%s'\n", url ); rc = gpg_error (GPG_ERR_GENERAL); goto leave; - + case KEYDB_RESOURCE_TYPE_KEYBOX: fp = fopen (filename, "rb"); if (!fp && !force) @@ -159,13 +159,13 @@ keydb_add_resource (const char *url, int force, int secret, int *auto_created) rc = gpg_error (gpg_err_code_from_errno (errno)); goto leave; } - + if (!fp) { /* no file */ #if 0 /* no autocreate of the homedirectory yet */ { char *last_slash_in_filename; - + last_slash_in_filename = strrchr (filename, DIRSEP_C); *last_slash_in_filename = 0; if (access (filename, F_OK)) @@ -202,13 +202,13 @@ keydb_add_resource (const char *url, int force, int secret, int *auto_created) fp = NULL; /* now register the file */ { - + void *token = keybox_register_file (filename, secret); if (!token) ; /* already registered - ignore it */ else if (used_resources >= MAX_KEYDB_RESOURCES) rc = gpg_error (GPG_ERR_RESOURCE_LIMIT); - else + else { all_resources[used_resources].type = rt; all_resources[used_resources].u.kr = NULL; /* Not used here */ @@ -224,7 +224,7 @@ keydb_add_resource (const char *url, int force, int secret, int *auto_created) if (!make_dotlock (all_resources[used_resources].lockhandle, 0)) { KEYBOX_HANDLE kbxhd = keybox_new (token, secret); - + if (kbxhd) { keybox_compress (kbxhd); @@ -232,7 +232,7 @@ keydb_add_resource (const char *url, int force, int secret, int *auto_created) } release_dotlock (all_resources[used_resources].lockhandle); } - + used_resources++; } } @@ -264,10 +264,10 @@ keydb_new (int secret) { KEYDB_HANDLE hd; int i, j; - + hd = xcalloc (1, sizeof *hd); hd->found = -1; - + assert (used_resources <= MAX_KEYDB_RESOURCES); for (i=j=0; i < used_resources; i++) { @@ -283,7 +283,7 @@ keydb_new (int secret) hd->active[j].secret = all_resources[i].secret; hd->active[j].lockhandle = all_resources[i].lockhandle; hd->active[j].u.kr = keybox_new (all_resources[i].token, secret); - if (!hd->active[j].u.kr) + if (!hd->active[j].u.kr) { xfree (hd); return NULL; /* fixme: release all previously allocated handles*/ @@ -293,16 +293,16 @@ keydb_new (int secret) } } hd->used = j; - + active_handles++; return hd; } -void +void keydb_release (KEYDB_HANDLE hd) { int i; - + if (!hd) return; assert (active_handles > 0); @@ -311,7 +311,7 @@ keydb_release (KEYDB_HANDLE hd) unlock_all (hd); for (i=0; i < hd->used; i++) { - switch (hd->active[i].type) + switch (hd->active[i].type) { case KEYDB_RESOURCE_TYPE_NONE: break; @@ -336,27 +336,27 @@ keydb_get_resource_name (KEYDB_HANDLE hd) { int idx; const char *s = NULL; - - if (!hd) + + if (!hd) return NULL; - if ( hd->found >= 0 && hd->found < hd->used) + if ( hd->found >= 0 && hd->found < hd->used) idx = hd->found; - else if ( hd->current >= 0 && hd->current < hd->used) + else if ( hd->current >= 0 && hd->current < hd->used) idx = hd->current; else idx = 0; - switch (hd->active[idx].type) + switch (hd->active[idx].type) { case KEYDB_RESOURCE_TYPE_NONE: - s = NULL; + s = NULL; break; case KEYDB_RESOURCE_TYPE_KEYBOX: s = keybox_get_resource_name (hd->active[idx].u.kr); break; } - + return s? s: ""; } @@ -374,7 +374,7 @@ keydb_set_ephemeral (KEYDB_HANDLE hd, int yes) { for (i=0; i < hd->used; i++) { - switch (hd->active[i].type) + switch (hd->active[i].type) { case KEYDB_RESOURCE_TYPE_NONE: break; @@ -384,7 +384,7 @@ keydb_set_ephemeral (KEYDB_HANDLE hd, int yes) } } } - + i = hd->is_ephemeral; hd->is_ephemeral = yes; return i; @@ -407,7 +407,7 @@ keydb_lock (KEYDB_HANDLE hd) -static int +static int lock_all (KEYDB_HANDLE hd) { int i, rc = 0; @@ -415,9 +415,9 @@ lock_all (KEYDB_HANDLE hd) /* Fixme: This locking scheme may lead to deadlock if the resources are not added in the same order by all processes. We are currently only allowing one resource so it is not a problem. */ - for (i=0; i < hd->used; i++) + for (i=0; i < hd->used; i++) { - switch (hd->active[i].type) + switch (hd->active[i].type) { case KEYDB_RESOURCE_TYPE_NONE: break; @@ -430,12 +430,12 @@ lock_all (KEYDB_HANDLE hd) break; } - if (rc) + if (rc) { /* revert the already set locks */ - for (i--; i >= 0; i--) + for (i--; i >= 0; i--) { - switch (hd->active[i].type) + switch (hd->active[i].type) { case KEYDB_RESOURCE_TYPE_NONE: break; @@ -459,13 +459,13 @@ static void unlock_all (KEYDB_HANDLE hd) { int i; - + if (!hd->locked) return; - for (i=hd->used-1; i >= 0; i--) + for (i=hd->used-1; i >= 0; i--) { - switch (hd->active[i].type) + switch (hd->active[i].type) { case KEYDB_RESOURCE_TYPE_NONE: break; @@ -483,7 +483,7 @@ unlock_all (KEYDB_HANDLE hd) /* * Return the last found keybox. Caller must free it. * The returned keyblock has the kbode flag bit 0 set for the node with - * the public key used to locate the keyblock or flag bit 1 set for + * the public key used to locate the keyblock or flag bit 1 set for * the user ID node. */ int @@ -494,7 +494,7 @@ keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb) if (!hd) return G10ERR_INV_ARG; - if ( hd->found < 0 || hd->found >= hd->used) + if ( hd->found < 0 || hd->found >= hd->used) return -1; /* nothing found */ switch (hd->active[hd->found].type) { @@ -509,7 +509,7 @@ keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb) return rc; } -/* +/* * update the current keyblock with KB */ int @@ -520,7 +520,7 @@ keydb_update_keyblock (KEYDB_HANDLE hd, KBNODE kb) if (!hd) return G10ERR_INV_ARG; - if ( hd->found < 0 || hd->found >= hd->used) + if ( hd->found < 0 || hd->found >= hd->used) return -1; /* nothing found */ if( opt.dry_run ) @@ -543,8 +543,8 @@ keydb_update_keyblock (KEYDB_HANDLE hd, KBNODE kb) } -/* - * Insert a new KB into one of the resources. +/* + * Insert a new KB into one of the resources. */ int keydb_insert_keyblock (KEYDB_HANDLE hd, KBNODE kb) @@ -552,15 +552,15 @@ keydb_insert_keyblock (KEYDB_HANDLE hd, KBNODE kb) int rc = -1; int idx; - if (!hd) + if (!hd) return G10ERR_INV_ARG; if( opt.dry_run ) return 0; - if ( hd->found >= 0 && hd->found < hd->used) + if ( hd->found >= 0 && hd->found < hd->used) idx = hd->found; - else if ( hd->current >= 0 && hd->current < hd->used) + else if ( hd->current >= 0 && hd->current < hd->used) idx = hd->current; else return G10ERR_GENERAL; @@ -598,11 +598,11 @@ keydb_get_cert (KEYDB_HANDLE hd, ksba_cert_t *r_cert) if (!hd) return gpg_error (GPG_ERR_INV_VALUE); - - if ( hd->found < 0 || hd->found >= hd->used) + + if ( hd->found < 0 || hd->found >= hd->used) return -1; /* nothing found */ - - switch (hd->active[hd->found].type) + + switch (hd->active[hd->found].type) { case KEYDB_RESOURCE_TYPE_NONE: rc = gpg_error (GPG_ERR_GENERAL); /* oops */ @@ -611,7 +611,7 @@ keydb_get_cert (KEYDB_HANDLE hd, ksba_cert_t *r_cert) rc = keybox_get_cert (hd->active[hd->found].u.kr, r_cert); break; } - + return rc; } @@ -626,11 +626,11 @@ keydb_get_flags (KEYDB_HANDLE hd, int which, int idx, unsigned int *value) if (!hd) return gpg_error (GPG_ERR_INV_VALUE); - - if ( hd->found < 0 || hd->found >= hd->used) + + if ( hd->found < 0 || hd->found >= hd->used) return gpg_error (GPG_ERR_NOTHING_FOUND); - - switch (hd->active[hd->found].type) + + switch (hd->active[hd->found].type) { case KEYDB_RESOURCE_TYPE_NONE: err = gpg_error (GPG_ERR_GENERAL); /* oops */ @@ -639,7 +639,7 @@ keydb_get_flags (KEYDB_HANDLE hd, int which, int idx, unsigned int *value) err = keybox_get_flags (hd->active[hd->found].u.kr, which, idx, value); break; } - + return err; } @@ -656,14 +656,14 @@ keydb_set_flags (KEYDB_HANDLE hd, int which, int idx, unsigned int value) if (!hd) return gpg_error (GPG_ERR_INV_VALUE); - - if ( hd->found < 0 || hd->found >= hd->used) + + if ( hd->found < 0 || hd->found >= hd->used) return gpg_error (GPG_ERR_NOTHING_FOUND); - + if (!hd->locked) return gpg_error (GPG_ERR_NOT_LOCKED); - switch (hd->active[hd->found].type) + switch (hd->active[hd->found].type) { case KEYDB_RESOURCE_TYPE_NONE: err = gpg_error (GPG_ERR_GENERAL); /* oops */ @@ -672,12 +672,12 @@ keydb_set_flags (KEYDB_HANDLE hd, int which, int idx, unsigned int value) err = keybox_set_flags (hd->active[hd->found].u.kr, which, idx, value); break; } - + return err; } -/* - * Insert a new Certificate into one of the resources. +/* + * Insert a new Certificate into one of the resources. */ int keydb_insert_cert (KEYDB_HANDLE hd, ksba_cert_t cert) @@ -685,16 +685,16 @@ keydb_insert_cert (KEYDB_HANDLE hd, ksba_cert_t cert) int rc = -1; int idx; unsigned char digest[20]; - - if (!hd) + + if (!hd) return gpg_error (GPG_ERR_INV_VALUE); if (opt.dry_run) return 0; - - if ( hd->found >= 0 && hd->found < hd->used) + + if ( hd->found >= 0 && hd->found < hd->used) idx = hd->found; - else if ( hd->current >= 0 && hd->current < hd->used) + else if ( hd->current >= 0 && hd->current < hd->used) idx = hd->current; else return gpg_error (GPG_ERR_GENERAL); @@ -704,7 +704,7 @@ keydb_insert_cert (KEYDB_HANDLE hd, ksba_cert_t cert) gpgsm_get_fingerprint (cert, GCRY_MD_SHA1, digest, NULL); /* kludge*/ - switch (hd->active[idx].type) + switch (hd->active[idx].type) { case KEYDB_RESOURCE_TYPE_NONE: rc = gpg_error (GPG_ERR_GENERAL); @@ -713,7 +713,7 @@ keydb_insert_cert (KEYDB_HANDLE hd, ksba_cert_t cert) rc = keybox_insert_cert (hd->active[idx].u.kr, cert, digest); break; } - + unlock_all (hd); return rc; } @@ -726,11 +726,11 @@ keydb_update_cert (KEYDB_HANDLE hd, ksba_cert_t cert) { int rc = 0; unsigned char digest[20]; - + if (!hd) return gpg_error (GPG_ERR_INV_VALUE); - if ( hd->found < 0 || hd->found >= hd->used) + if ( hd->found < 0 || hd->found >= hd->used) return -1; /* nothing found */ if (opt.dry_run) @@ -742,7 +742,7 @@ keydb_update_cert (KEYDB_HANDLE hd, ksba_cert_t cert) gpgsm_get_fingerprint (cert, GCRY_MD_SHA1, digest, NULL); /* kludge*/ - switch (hd->active[hd->found].type) + switch (hd->active[hd->found].type) { case KEYDB_RESOURCE_TYPE_NONE: rc = gpg_error (GPG_ERR_GENERAL); /* oops */ @@ -757,25 +757,25 @@ keydb_update_cert (KEYDB_HANDLE hd, ksba_cert_t cert) } -/* +/* * The current keyblock or cert will be deleted. */ int keydb_delete (KEYDB_HANDLE hd, int unlock) { int rc = -1; - + if (!hd) return gpg_error (GPG_ERR_INV_VALUE); - if ( hd->found < 0 || hd->found >= hd->used) + if ( hd->found < 0 || hd->found >= hd->used) return -1; /* nothing found */ if( opt.dry_run ) return 0; if (!hd->locked) - return gpg_error (GPG_ERR_NOT_LOCKED); + return gpg_error (GPG_ERR_NOT_LOCKED); switch (hd->active[hd->found].type) { @@ -797,7 +797,7 @@ keydb_delete (KEYDB_HANDLE hd, int unlock) /* * Locate the default writable key resource, so that the next * operation (which is only relevant for inserts) will be done on this - * resource. + * resource. */ int keydb_locate_writable (KEYDB_HANDLE hd, const char *reserved) @@ -805,17 +805,17 @@ keydb_locate_writable (KEYDB_HANDLE hd, const char *reserved) int rc; (void)reserved; - + if (!hd) return gpg_error (GPG_ERR_INV_VALUE); - + rc = keydb_search_reset (hd); /* this does reset hd->current */ if (rc) return rc; - - for ( ; hd->current >= 0 && hd->current < hd->used; hd->current++) + + for ( ; hd->current >= 0 && hd->current < hd->used; hd->current++) { - switch (hd->active[hd->current].type) + switch (hd->active[hd->current].type) { case KEYDB_RESOURCE_TYPE_NONE: BUG(); @@ -826,7 +826,7 @@ keydb_locate_writable (KEYDB_HANDLE hd, const char *reserved) break; } } - + return -1; } @@ -837,7 +837,7 @@ void keydb_rebuild_caches (void) { int i; - + for (i=0; i < used_resources; i++) { if (all_resources[i].secret) @@ -858,23 +858,23 @@ keydb_rebuild_caches (void) -/* +/* * Start the next search on this handle right at the beginning */ -int +int keydb_search_reset (KEYDB_HANDLE hd) { int i, rc = 0; - + if (!hd) return gpg_error (GPG_ERR_INV_VALUE); - hd->current = 0; + hd->current = 0; hd->found = -1; /* and reset all resources */ - for (i=0; !rc && i < hd->used; i++) + for (i=0; !rc && i < hd->used; i++) { - switch (hd->active[i].type) + switch (hd->active[i].type) { case KEYDB_RESOURCE_TYPE_NONE: break; @@ -887,21 +887,21 @@ keydb_search_reset (KEYDB_HANDLE hd) all modules*/ } -/* +/* * Search through all keydb resources, starting at the current position, * for a keyblock which contains one of the keys described in the DESC array. */ -int +int keydb_search (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc, size_t ndesc) { int rc = -1; - + if (!hd) return gpg_error (GPG_ERR_INV_VALUE); - while (rc == -1 && hd->current >= 0 && hd->current < hd->used) + while (rc == -1 && hd->current >= 0 && hd->current < hd->used) { - switch (hd->active[hd->current].type) + switch (hd->active[hd->current].type) { case KEYDB_RESOURCE_TYPE_NONE: BUG(); /* we should never see it here */ @@ -911,12 +911,12 @@ keydb_search (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc, size_t ndesc) break; } if (rc == -1) /* EOF -> switch to next resource */ - hd->current++; + hd->current++; else if (!rc) hd->found = hd->current; } - - return rc; + + return rc; } @@ -924,7 +924,7 @@ int keydb_search_first (KEYDB_HANDLE hd) { KEYDB_SEARCH_DESC desc; - + memset (&desc, 0, sizeof desc); desc.mode = KEYDB_SEARCH_MODE_FIRST; return keydb_search (hd, &desc, 1); @@ -934,7 +934,7 @@ int keydb_search_next (KEYDB_HANDLE hd) { KEYDB_SEARCH_DESC desc; - + memset (&desc, 0, sizeof desc); desc.mode = KEYDB_SEARCH_MODE_NEXT; return keydb_search (hd, &desc, 1); @@ -946,7 +946,7 @@ keydb_search_kid (KEYDB_HANDLE hd, u32 *kid) KEYDB_SEARCH_DESC desc; (void)kid; - + memset (&desc, 0, sizeof desc); desc.mode = KEYDB_SEARCH_MODE_LONG_KID; desc.u.kid[0] = kid[0]; @@ -958,7 +958,7 @@ int keydb_search_fpr (KEYDB_HANDLE hd, const byte *fpr) { KEYDB_SEARCH_DESC desc; - + memset (&desc, 0, sizeof desc); desc.mode = KEYDB_SEARCH_MODE_FPR; memcpy (desc.u.fpr, fpr, 20); @@ -970,7 +970,7 @@ keydb_search_issuer (KEYDB_HANDLE hd, const char *issuer) { KEYDB_SEARCH_DESC desc; int rc; - + memset (&desc, 0, sizeof desc); desc.mode = KEYDB_SEARCH_MODE_ISSUER; desc.u.name = issuer; @@ -985,7 +985,7 @@ keydb_search_issuer_sn (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC desc; int rc; const unsigned char *s; - + memset (&desc, 0, sizeof desc); desc.mode = KEYDB_SEARCH_MODE_ISSUER_SN; s = serial; @@ -1007,7 +1007,7 @@ keydb_search_subject (KEYDB_HANDLE hd, const char *name) { KEYDB_SEARCH_DESC desc; int rc; - + memset (&desc, 0, sizeof desc); desc.mode = KEYDB_SEARCH_MODE_SUBJECT; desc.u.name = name; @@ -1046,7 +1046,7 @@ keydb_store_cert (ksba_cert_t cert, int ephemeral, int *existed) if (ephemeral) keydb_set_ephemeral (kh, 1); - + rc = lock_all (kh); if (rc) return rc; @@ -1081,7 +1081,7 @@ keydb_store_cert (ksba_cert_t cert, int ephemeral, int *existed) keydb_release (kh); return rc; } - keydb_release (kh); + keydb_release (kh); return 0; } @@ -1090,8 +1090,8 @@ keydb_store_cert (ksba_cert_t cert, int ephemeral, int *existed) transaction by locating the certificate in the DB and updating the flags. */ gpg_error_t -keydb_set_cert_flags (ksba_cert_t cert, int ephemeral, - int which, int idx, +keydb_set_cert_flags (ksba_cert_t cert, int ephemeral, + int which, int idx, unsigned int mask, unsigned int value) { KEYDB_HANDLE kh; @@ -1156,7 +1156,7 @@ keydb_set_cert_flags (ksba_cert_t cert, int ephemeral, } } - keydb_release (kh); + keydb_release (kh); return 0; } @@ -1175,7 +1175,7 @@ keydb_clear_some_cert_flags (ctrl_t ctrl, strlist_t names) unsigned int old_value, value; (void)ctrl; - + hd = keydb_new (0); if (!hd) { @@ -1187,7 +1187,7 @@ keydb_clear_some_cert_flags (ctrl_t ctrl, strlist_t names) ndesc = 1; else { - for (sl=names, ndesc=0; sl; sl = sl->next, ndesc++) + for (sl=names, ndesc=0; sl; sl = sl->next, ndesc++) ; } @@ -1201,9 +1201,9 @@ keydb_clear_some_cert_flags (ctrl_t ctrl, strlist_t names) if (!names) desc[0].mode = KEYDB_SEARCH_MODE_FIRST; - else + else { - for (ndesc=0, sl=names; sl; sl = sl->next) + for (ndesc=0, sl=names; sl; sl = sl->next) { rc = classify_user_id (sl->d, desc+ndesc); if (rc) @@ -1226,7 +1226,7 @@ keydb_clear_some_cert_flags (ctrl_t ctrl, strlist_t names) while (!(rc = keydb_search (hd, desc, ndesc))) { - if (!names) + if (!names) desc[0].mode = KEYDB_SEARCH_MODE_NEXT; err = keydb_get_flags (hd, KEYBOX_FLAG_VALIDITY, 0, &old_value); @@ -1236,7 +1236,7 @@ keydb_clear_some_cert_flags (ctrl_t ctrl, strlist_t names) gpg_strerror (err)); goto leave; } - + value = (old_value & ~VALIDITY_REVOKED); if (value != old_value) { @@ -1250,10 +1250,8 @@ keydb_clear_some_cert_flags (ctrl_t ctrl, strlist_t names) } if (rc && rc != -1) log_error ("keydb_search failed: %s\n", gpg_strerror (rc)); - + leave: xfree (desc); keydb_release (hd); } - - |