cscvs to tla changeset 46
[onak.git] / keydb.c
1 /*
2  * keydb.c - Routines for DB access that just use store/fetch.
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2002 Project Purple
7  */
8
9 /**
10  *      The routines in this file are meant to be used as an initial step when
11  *      adding a new db access module. They provide various functions required
12  *      of the db access module using only the store and fetch functions. As
13  *      they need to parse the actual OpenPGP data to work they are a lot
14  *      slower than custom functions however.
15  */
16
17 #include <stdio.h>
18
19 #include "decodekey.h"
20 #include "hash.h"
21 #include "keydb.h"
22 #include "keyid.h"
23 #include "keystructs.h"
24 #include "mem.h"
25 #include "parsekey.h"
26
27 #ifdef NEED_KEYID2UID
28 /**
29  *      keyid2uid - Takes a keyid and returns the primary UID for it.
30  *      @keyid: The keyid to lookup.
31  */
32 char *keyid2uid(uint64_t keyid)
33 {
34         struct openpgp_publickey *publickey = NULL;
35         struct openpgp_signedpacket_list *curuid = NULL;
36         char buf[1024];
37
38         buf[0]=0;
39         if (fetch_key(keyid, &publickey, false) && publickey != NULL) {
40                 curuid = publickey->uids;
41                 while (curuid != NULL && buf[0] == 0) {
42                         if (curuid->packet->tag == 13) {
43                                 snprintf(buf, 1023, "%.*s",
44                                                 (int) curuid->packet->length,
45                                                 curuid->packet->data);
46                         }
47                         curuid = curuid -> next;
48                 }
49                 free_publickey(publickey);
50         }
51
52         if (buf[0] == 0) {
53                 return NULL;
54         } else {
55                 return strdup(buf);
56         }
57 }
58 #endif
59
60 #ifdef NEED_GETKEYSIGS
61 /**
62  *      getkeysigs - Gets a linked list of the signatures on a key.
63  *      @keyid: The keyid to get the sigs for.
64  *
65  *      This function gets the list of signatures on a key. Used for key 
66  *      indexing and doing stats bits.
67  */
68 struct ll *getkeysigs(uint64_t keyid)
69 {
70         struct ll *sigs = NULL;
71         struct openpgp_signedpacket_list *uids = NULL;
72         struct openpgp_publickey *publickey = NULL;
73
74         fetch_key(keyid, &publickey, false);
75         
76         if (publickey != NULL) {
77                 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
78                         sigs = keysigs(sigs, uids->sigs);
79                 }
80                 free_publickey(publickey);
81         }
82
83         return sigs;
84 }
85 #endif
86
87 /**
88  *      cached_getkeysigs - Gets the signatures on a key.
89  *      @keyid: The key we want the signatures for.
90  *      
91  *      This function gets the signatures on a key. It's the same as the
92  *      getkeysigs function above except we use the hash module to cache the
93  *      data so if we need it again it's already loaded.
94  */
95 struct ll *cached_getkeysigs(uint64_t keyid)
96 {
97         struct stats_key *key = NULL;
98
99         if (keyid == 0)  {
100                 return NULL;
101         }
102
103         key = findinhash(keyid);
104         if (key == NULL) {
105                 key = malloc(sizeof(*key));
106                 if (key != NULL) {
107                         key->keyid = keyid;
108                         key->colour = 0;
109                         key->parent = 0;
110                         key->sigs = NULL;
111                         key->gotsigs = false;
112                         addtohash(key);
113                 } else {
114                         perror("cached_getkeysigs()");
115                         return NULL;
116                 }
117         }
118         if (key->gotsigs == false) {
119                 key->sigs = getkeysigs(key->keyid);
120                 key->gotsigs = true;
121         }
122
123         return key->sigs;
124 }
125
126 #ifdef NEED_GETFULLKEYID
127 /**
128  *      getfullkeyid - Maps a 32bit key id to a 64bit one.
129  *      @keyid: The 32bit keyid.
130  *
131  *      This function maps a 32bit key id to the full 64bit one. It returns the
132  *      full keyid. If the key isn't found a keyid of 0 is returned.
133  */
134 uint64_t getfullkeyid(uint64_t keyid)
135 {
136         struct openpgp_publickey *publickey = NULL;
137
138         if (keyid < 0x100000000LL) {
139                 fetch_key(keyid, &publickey, false);
140                 if (publickey != NULL) {
141                         keyid = get_keyid(publickey);
142                         free_publickey(publickey);
143                         publickey = NULL;
144                 } else {
145                         keyid = 0;
146                 }
147         }
148         
149         return keyid;
150 }
151 #endif