2  * keydb_fs.c - Routines to store and fetch keys in a filesystem hierarchy.
 
   4  * Copyright 2004 Daniel Silverstone <dsilvers@digital-scurf.org>
 
   6  * This program is free software: you can redistribute it and/or modify it
 
   7  * under the terms of the GNU General Public License as published by the Free
 
   8  * Software Foundation; version 2 of the License.
 
  10  * This program is distributed in the hope that it will be useful, but WITHOUT
 
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
  12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 
  15  * You should have received a copy of the GNU General Public License along with
 
  16  * this program; if not, write to the Free Software Foundation, Inc., 51
 
  17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
  20 #include <sys/types.h>
 
  32 #include "charfuncs.h"
 
  33 #include "decodekey.h"
 
  36 #include "keystructs.h"
 
  39 #include "onak-conf.h"
 
  44 /* Hack: We should really dynamically allocate our path buffers */
 
  49 static int keydb_lockfile_fd = -1;
 
  50 static bool keydb_lockfile_readonly;
 
  52 /*****************************************************************************/
 
  54 /* Helper functions */
 
  56 #define FNV_offset_basis 2166136261ul
 
  57 #define FNV_mixing_prime 16777619ul
 
  59 static uint32_t calchash(uint8_t * ptr)
 
  61         register uint32_t h = FNV_offset_basis;
 
  62         register uint32_t p = FNV_mixing_prime;
 
  63         register uint32_t n = strlen((char *) ptr);
 
  64         register uint8_t *c = ptr;
 
  69         return h ? h : 1;       /* prevent a hash of zero happening */
 
  73 static void keypath(char *buffer, size_t length, uint64_t _keyid)
 
  75         uint64_t keyid = _keyid << 32;
 
  76         snprintf(buffer, length, "%s/key/%02X/%02X/%08X/%016" PRIX64,
 
  77                  config.db_dir, (uint8_t) ((keyid >> 56) & 0xFF),
 
  78                  (uint8_t) ((keyid >> 48) & 0xFF),
 
  79                  (uint32_t) (keyid >> 32), _keyid);
 
  82 static void keydir(char *buffer, size_t length, uint64_t _keyid)
 
  84         uint64_t keyid = _keyid << 32;
 
  85         snprintf(buffer, length, "%s/key/%02X/%02X/%08X", config.db_dir,
 
  86                  (uint8_t) ((keyid >> 56) & 0xFF),
 
  87                  (uint8_t) ((keyid >> 48) & 0xFF),
 
  88                  (uint32_t) (keyid >> 32));
 
  91 static void prove_path_to(uint64_t keyid, char *what)
 
  93         static char buffer[PATH_MAX];
 
  94         snprintf(buffer, sizeof(buffer), "%s/%s", config.db_dir, what);
 
  97         snprintf(buffer, sizeof(buffer), "%s/%s/%02X", config.db_dir, what,
 
  98                  (uint8_t) ((keyid >> 24) & 0xFF));
 
 101         snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X", config.db_dir,
 
 103                  (uint8_t) ((keyid >> 24) & 0xFF),
 
 104                  (uint8_t) ((keyid >> 16) & 0xFF));
 
 107         snprintf(buffer, sizeof(buffer), "%s/%s/%02X/%02X/%08X", config.db_dir,
 
 109                  (uint8_t) ((keyid >> 24) & 0xFF),
 
 110                  (uint8_t) ((keyid >> 16) & 0xFF), (uint32_t) (keyid));
 
 114 static void wordpath(char *buffer, size_t length, char *word, uint32_t hash,
 
 117         snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s/%016" PRIX64,
 
 118                  config.db_dir, (uint8_t) ((hash >> 24) & 0xFF),
 
 119                  (uint8_t) ((hash >> 16) & 0xFF), hash, word, keyid);
 
 122 static void worddir(char *buffer, size_t length, char *word, uint32_t hash)
 
 124         snprintf(buffer, length, "%s/words/%02X/%02X/%08X/%s", config.db_dir,
 
 125                  (uint8_t) ((hash >> 24) & 0xFF),
 
 126                  (uint8_t) ((hash >> 16) & 0xFF), hash, word);
 
 129 static void subkeypath(char *buffer, size_t length, uint64_t subkey,
 
 132         snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X/%016" PRIX64,
 
 134                  (uint8_t) ((subkey >> 24) & 0xFF),
 
 135                  (uint8_t) ((subkey >> 16) & 0xFF),
 
 136                  (uint32_t) (subkey & 0xFFFFFFFF),
 
 140 static void skshashpath(char *buffer, size_t length,
 
 141                 const struct skshash *hash)
 
 143         snprintf(buffer, length, "%s/skshash/%02X/%02X/%02X%02X%02X%02X/"
 
 144                 "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
 
 146                  hash->hash[0], hash->hash[1],
 
 147                  hash->hash[0], hash->hash[1], hash->hash[2], hash->hash[3],
 
 148                  hash->hash[4], hash->hash[5], hash->hash[6], hash->hash[7],
 
 149                  hash->hash[8], hash->hash[9], hash->hash[10], hash->hash[11],
 
 150                  hash->hash[12], hash->hash[13], hash->hash[14],
 
 153 static void subkeydir(char *buffer, size_t length, uint64_t subkey)
 
 155         snprintf(buffer, length, "%s/subkeys/%02X/%02X/%08X",
 
 157                  (uint8_t) ((subkey >> 24) & 0xFF),
 
 158                  (uint8_t) ((subkey >> 16) & 0xFF),
 
 159                  (uint32_t) (subkey & 0xFFFFFFFF));
 
 162 /*****************************************************************************/
 
 165  *      initdb - Initialize the key database.
 
 167 static void fs_initdb(bool readonly)
 
 169         char buffer[PATH_MAX];
 
 171         keydb_lockfile_readonly = readonly;
 
 173         snprintf(buffer, sizeof(buffer), "%s/.lock", config.db_dir);
 
 175         if (access(config.db_dir, R_OK | W_OK | X_OK) == -1) {
 
 176                 if (errno != ENOENT) {
 
 177                         logthing(LOGTHING_CRITICAL,
 
 178                                  "Unable to access keydb_fs root of '%s'. (%s)",
 
 179                                  config.db_dir, strerror(errno));
 
 180                         exit(1);        /* Lacking rwx on the key dir */
 
 182                 mkdir(config.db_dir, 0777);
 
 183                 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
 
 185         chdir(config.db_dir);
 
 186         if (keydb_lockfile_fd == -1)
 
 187                 keydb_lockfile_fd = open(buffer,
 
 188                                          (keydb_lockfile_readonly) ?
 
 190         if (keydb_lockfile_fd == -1)
 
 191                 keydb_lockfile_fd = open(buffer, O_RDWR | O_CREAT, 0600);
 
 192         if (keydb_lockfile_fd == -1) {
 
 193                 logthing(LOGTHING_CRITICAL,
 
 194                          "Unable to open lockfile '%s'. (%s)",
 
 195                          buffer, strerror(errno));
 
 196                 exit(1);        /* Lacking rwx on the key dir */
 
 201  *      cleanupdb - De-initialize the key database.
 
 203 static void fs_cleanupdb(void)
 
 205         /* Mmmm nothing to do here? */
 
 206         close(keydb_lockfile_fd);
 
 210  *      starttrans - Start a transaction.
 
 212 static bool fs_starttrans(void)
 
 214         struct flock lockstruct;
 
 217             F_RDLCK | ((keydb_lockfile_readonly) ? 0 : F_WRLCK);
 
 218         lockstruct.l_whence = SEEK_SET;
 
 219         lockstruct.l_start = 0;
 
 220         lockstruct.l_len = 1;
 
 222         while (fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct) == -1) {
 
 223                 if (remaining-- == 0)
 
 224                         return false;   /* Hope to hell that noodles DTRT */
 
 231  *      endtrans - End a transaction.
 
 233 static void fs_endtrans(void)
 
 235         struct flock lockstruct;
 
 237         lockstruct.l_type = F_UNLCK;
 
 238         lockstruct.l_whence = SEEK_SET;
 
 239         lockstruct.l_start = 0;
 
 240         lockstruct.l_len = 1;
 
 241         fcntl(keydb_lockfile_fd, F_SETLK, &lockstruct);
 
 244 static uint64_t fs_getfullkeyid(uint64_t keyid)
 
 246         static char buffer[PATH_MAX];
 
 248         struct dirent *de = NULL;
 
 251         keydir(buffer, sizeof(buffer), keyid);
 
 257                         if (de && de->d_name[0] != '.') {
 
 258                                 ret = strtoull(de->d_name, NULL, 16);
 
 260                 } while (de && de->d_name[0] == '.');
 
 265                 subkeydir(buffer, sizeof(buffer), keyid);
 
 271                                 if (de && de->d_name[0] != '.') {
 
 272                                         ret = strtoull(de->d_name, NULL, 16);
 
 274                         } while (de && de->d_name[0] == '.');
 
 283  *      fetch_key - Given a keyid fetch the key from storage.
 
 284  *      @keyid: The keyid to fetch.
 
 285  *      @publickey: A pointer to a structure to return the key in.
 
 286  *      @intrans: If we're already in a transaction.
 
 288 static int fs_fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
 
 291         static char buffer[PATH_MAX];
 
 293         struct openpgp_packet_list *packets = NULL;
 
 298         if ((keyid >> 32) == 0)
 
 299                 keyid = fs_getfullkeyid(keyid);
 
 301         keypath(buffer, sizeof(buffer), keyid);
 
 302         if ((fd = open(buffer, O_RDONLY)) != -1) {
 
 303                 /* File is present, load it in... */
 
 304                 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
 
 305                 parse_keys(packets, publickey);
 
 306                 free_packet_list(packets);
 
 318  *      store_key - Takes a key and stores it.
 
 319  *      @publickey: A pointer to the public key to store.
 
 320  *      @intrans: If we're already in a transaction.
 
 321  *      @update: If true the key exists and should be updated.
 
 323 static int fs_store_key(struct openpgp_publickey *publickey, bool intrans,
 
 326         static char buffer[PATH_MAX];
 
 327         static char wbuffer[PATH_MAX];
 
 329         struct openpgp_packet_list *packets = NULL;
 
 330         struct openpgp_packet_list *list_end = NULL;
 
 331         struct openpgp_publickey *next = NULL;
 
 332         uint64_t keyid = get_keyid(publickey);
 
 333         struct ll *wordlist = NULL, *wl = NULL;
 
 335         uint64_t *subkeyids = NULL;
 
 343         prove_path_to(keyid, "key");
 
 344         keypath(buffer, sizeof(buffer), keyid);
 
 347              open(buffer, O_WRONLY | (update ? O_TRUNC : O_CREAT),
 
 349                 next = publickey->next;
 
 350                 publickey->next = NULL;
 
 351                 flatten_publickey(publickey, &packets, &list_end);
 
 352                 publickey->next = next;
 
 354                 write_openpgp_stream(file_putchar, &fd, packets);
 
 356                 free_packet_list(packets);
 
 362                 wl = wordlist = makewordlistfromkey(wordlist, publickey);
 
 364                         uint32_t hash = calchash((uint8_t *) (wl->object));
 
 365                         prove_path_to(hash, "words");
 
 367                         worddir(wbuffer, sizeof(wbuffer), wl->object, hash);
 
 368                         mkdir(wbuffer, 0777);
 
 369                         wordpath(wbuffer, sizeof(wbuffer), wl->object, hash,
 
 371                         link(buffer, wbuffer);
 
 375                 llfree(wordlist, free);
 
 377                 subkeyids = keysubkeys(publickey);
 
 379                 while (subkeyids != NULL && subkeyids[i] != 0) {
 
 380                         prove_path_to(subkeyids[i], "subkeys");
 
 382                         subkeydir(wbuffer, sizeof(wbuffer), subkeyids[i]);
 
 383                         mkdir(wbuffer, 0777);
 
 384                         subkeypath(wbuffer, sizeof(wbuffer), subkeyids[i],
 
 386                         link(buffer, wbuffer);
 
 390                 if (subkeyids != NULL) {
 
 395                 get_skshash(publickey, &hash);
 
 396                 hashid = (hash.hash[0] << 24) + (hash.hash[1] << 16) +
 
 397                                 (hash.hash[2] << 8) + hash.hash[3];
 
 398                 prove_path_to(hashid, "skshash");
 
 399                 skshashpath(wbuffer, sizeof(wbuffer), &hash);
 
 400                 link(buffer, wbuffer);
 
 409  *      delete_key - Given a keyid delete the key from storage.
 
 410  *      @keyid: The keyid to delete.
 
 411  *      @intrans: If we're already in a transaction.
 
 413 static int fs_delete_key(uint64_t keyid, bool intrans)
 
 415         static char buffer[PATH_MAX];
 
 417         struct openpgp_publickey *pk = NULL;
 
 419         struct ll *wordlist = NULL, *wl = NULL;
 
 420         uint64_t *subkeyids = NULL;
 
 423         if ((keyid >> 32) == 0)
 
 424                 keyid = fs_getfullkeyid(keyid);
 
 429         ret = fs_fetch_key(keyid, &pk, true);
 
 432                 logthing(LOGTHING_DEBUG, "Wordlist for key %016" PRIX64,
 
 434                 wl = wordlist = makewordlistfromkey(wordlist, pk);
 
 435                 logthing(LOGTHING_DEBUG,
 
 436                          "Wordlist for key %016" PRIX64 " done", keyid);
 
 438                         uint32_t hash = calchash((uint8_t *) (wl->object));
 
 439                         prove_path_to(hash, "words");
 
 441                         wordpath(buffer, sizeof(buffer), wl->object, hash,
 
 448                 subkeyids = keysubkeys(pk);
 
 450                 while (subkeyids != NULL && subkeyids[i] != 0) {
 
 451                         prove_path_to(subkeyids[i], "subkeys");
 
 453                         subkeypath(buffer, sizeof(buffer), subkeyids[i],
 
 459                 if (subkeyids != NULL) {
 
 464                 get_skshash(pk, &hash);
 
 465                 skshashpath(buffer, sizeof(buffer), &hash);
 
 469         keypath(buffer, sizeof(buffer), keyid);
 
 477 static struct ll *internal_get_key_by_word(char *word, struct ll *mct)
 
 479         struct ll *keys = NULL;
 
 481         char buffer[PATH_MAX];
 
 482         uint32_t hash = calchash((uint8_t *) (word));
 
 485         worddir(buffer, sizeof(buffer), word, hash);
 
 487         logthing(LOGTHING_DEBUG, "Scanning for word %s in dir %s", word,
 
 492                         if (de && de->d_name[0] != '.') {
 
 494                                     || (llfind(mct, de->d_name,
 
 495                                         (int (*)(const void *, const void *))
 
 498                                         logthing(LOGTHING_DEBUG,
 
 499                                                  "Found %s // %s", word,
 
 513  *      fetch_key_text - Trys to find the keys that contain the supplied text.
 
 514  *      @search: The text to search for.
 
 515  *      @publickey: A pointer to a structure to return the key in.
 
 517 static int fs_fetch_key_text(const char *search,
 
 518                    struct openpgp_publickey **publickey)
 
 520         struct ll *wordlist = NULL, *wl = NULL;
 
 521         struct ll *keylist = NULL;
 
 522         char      *searchtext = NULL;
 
 525         logthing(LOGTHING_DEBUG, "Search was '%s'", search);
 
 527         searchtext = strdup(search);
 
 528         wl = wordlist = makewordlist(wordlist, searchtext);
 
 530         keylist = internal_get_key_by_word(wordlist->object, NULL);
 
 533                 llfree(wordlist, NULL);
 
 542                     internal_get_key_by_word(wl->object, keylist);
 
 544                         llfree(wordlist, NULL);
 
 545                         llfree(keylist, free);
 
 550                 llfree(keylist, free);
 
 555         llfree(wordlist, NULL);
 
 557         /* Now add the keys... */
 
 560                 logthing(LOGTHING_DEBUG, "Adding key: %s", wl->object);
 
 562                     fs_fetch_key(strtoull(wl->object, NULL, 16), publickey,
 
 564                 if (addedkeys >= config.maxkeys)
 
 569         llfree(keylist, free);
 
 577  *      fetch_key_skshash - Given an SKS hash fetch the key from storage.
 
 578  *      @hash: The hash to fetch.
 
 579  *      @publickey: A pointer to a structure to return the key in.
 
 580  *      @intrans: If we're already in a transaction.
 
 582 static int fs_fetch_key_skshash(const struct skshash *hash,
 
 583               struct openpgp_publickey **publickey)
 
 585         static char buffer[PATH_MAX];
 
 587         struct openpgp_packet_list *packets = NULL;
 
 589         skshashpath(buffer, sizeof(buffer), hash);
 
 590         if ((fd = open(buffer, O_RDONLY)) != -1) {
 
 591                 read_openpgp_stream(file_fetchchar, &fd, &packets, 0);
 
 592                 parse_keys(packets, publickey);
 
 593                 free_packet_list(packets);
 
 603  *      iterate_keys - call a function once for each key in the db.
 
 604  *      @iterfunc: The function to call.
 
 605  *      @ctx: A context pointer
 
 607  *      Calls iterfunc once for each key in the database. ctx is passed
 
 608  *      unaltered to iterfunc. This function is intended to aid database dumps
 
 609  *      and statistic calculations.
 
 611  *      Returns the number of keys we iterated over.
 
 613 static int fs_iterate_keys(void (*iterfunc)(void *ctx,
 
 614                 struct openpgp_publickey *key), void *ctx)
 
 620  * Include the basic keydb routines.
 
 622 #define NEED_KEYID2UID 1
 
 623 #define NEED_GETKEYSIGS 1
 
 624 #define NEED_UPDATEKEYS 1
 
 627 struct dbfuncs keydb_fs_funcs = {
 
 629         .cleanupdb              = fs_cleanupdb,
 
 630         .starttrans             = fs_starttrans,
 
 631         .endtrans               = fs_endtrans,
 
 632         .fetch_key              = fs_fetch_key,
 
 633         .fetch_key_text         = fs_fetch_key_text,
 
 634         .fetch_key_skshash      = fs_fetch_key_skshash,
 
 635         .store_key              = fs_store_key,
 
 636         .update_keys            = generic_update_keys,
 
 637         .delete_key             = fs_delete_key,
 
 638         .getkeysigs             = generic_getkeysigs,
 
 639         .cached_getkeysigs      = generic_cached_getkeysigs,
 
 640         .keyid2uid              = generic_keyid2uid,
 
 641         .getfullkeyid           = fs_getfullkeyid,
 
 642         .iterate_keys           = fs_iterate_keys,