2  * keydb.c - Routines to store and fetch keys.
 
   4  * Jonathan McDowell <noodles@earth.li>
 
   6  * Copyright 2002 Project Purple
 
  20 #include "keystructs.h"
 
  25 #define DBDIR "/home/noodles/onak-0.0.1/db"
 
  28  *      keydb_fetchchar - Fetches a char from a file.
 
  30 static int keydb_fetchchar(void *fd, size_t count, unsigned char *c)
 
  32         return !(read( *(int *) fd, c, count));
 
  36  *      keydb_putchar - Puts a char to a file.
 
  38 static int keydb_putchar(void *fd, unsigned char c)
 
  40         return !(write( *(int *) fd, &c, sizeof(c)));
 
  44  *      initdb - Initialize the key database.
 
  46  *      This is just a no-op for flat file access.
 
  53  *      cleanupdb - De-initialize the key database.
 
  55  *      This is just a no-op for flat file access.
 
  62  *      fetch_key - Given a keyid fetch the key from storage.
 
  63  *      @keyid: The keyid to fetch.
 
  64  *      @publickey: A pointer to a structure to return the key in.
 
  66  *      We use the hex representation of the keyid as the filename to fetch the
 
  67  *      key from. The key is stored in the file as a binary OpenPGP stream of
 
  68  *      packets, so we can just use read_openpgp_stream() to read the packets
 
  69  *      in and then parse_keys() to parse the packets into a publickey
 
  72 int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey)
 
  74         struct openpgp_packet_list *packets = NULL;
 
  78         snprintf(keyfile, 1023, "%s/0x%llX", DBDIR, keyid & 0xFFFFFFFF);
 
  79         fd = open(keyfile, O_RDONLY); // | O_SHLOCK);
 
  82                 read_openpgp_stream(keydb_fetchchar, &fd, &packets);
 
  83                 parse_keys(packets, publickey);
 
  91  *      store_key - Takes a key and stores it.
 
  92  *      @publickey: A pointer to the public key to store.
 
  94  *      Again we just use the hex representation of the keyid as the filename
 
  95  *      to store the key to. We flatten the public key to a list of OpenPGP
 
  96  *      packets and then use write_openpgp_stream() to write the stream out to
 
  99 int store_key(struct openpgp_publickey *publickey)
 
 101         struct openpgp_packet_list *packets = NULL;
 
 102         struct openpgp_packet_list *list_end = NULL;
 
 103         struct openpgp_publickey *next = NULL;
 
 107         snprintf(keyfile, 1023, "%s/0x%llX", DBDIR,
 
 108                         get_keyid(publickey) & 0xFFFFFFFF);
 
 109         fd = open(keyfile, O_WRONLY | O_CREAT, 0664); // | O_EXLOCK);
 
 112                 next = publickey -> next;
 
 113                 publickey -> next = NULL;
 
 114                 flatten_publickey(publickey, &packets, &list_end);
 
 115                 publickey -> next = next;
 
 117                 write_openpgp_stream(keydb_putchar, &fd, packets);
 
 125  *      delete_key - Given a keyid delete the key from storage.
 
 126  *      @keyid: The keyid to delete.
 
 128  *      This function deletes a public key from whatever storage mechanism we
 
 129  *      are using. Returns 0 if the key existed.
 
 131 int delete_key(uint64_t keyid)
 
 135         snprintf(keyfile, 1023, "%s/0x%llX", DBDIR,
 
 138         return unlink(keyfile);
 
 142  * Include the basic keydb routines.