Remove CVS Id tags.
[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  *      @revoked: Is the key revoked?
65  *
66  *      This function gets the list of signatures on a key. Used for key 
67  *      indexing and doing stats bits. If revoked is non-NULL then if the key
68  *      is revoked it's set to true.
69  */
70 struct ll *getkeysigs(uint64_t keyid, bool *revoked)
71 {
72         struct ll *sigs = NULL;
73         struct openpgp_signedpacket_list *uids = NULL;
74         struct openpgp_publickey *publickey = NULL;
75
76         fetch_key(keyid, &publickey, false);
77         
78         if (publickey != NULL) {
79                 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
80                         sigs = keysigs(sigs, uids->sigs);
81                 }
82                 if (revoked != NULL) {
83                         *revoked = (publickey->revocations != NULL);
84                 }
85                 free_publickey(publickey);
86         }
87
88         return sigs;
89 }
90 #endif
91
92 /**
93  *      cached_getkeysigs - Gets the signatures on a key.
94  *      @keyid: The key we want the signatures for.
95  *      
96  *      This function gets the signatures on a key. It's the same as the
97  *      getkeysigs function above except we use the hash module to cache the
98  *      data so if we need it again it's already loaded.
99  */
100 struct ll *cached_getkeysigs(uint64_t keyid)
101 {
102         struct stats_key *key = NULL;
103         struct stats_key *signedkey = NULL;
104         struct ll        *cursig = NULL;
105         bool              revoked = false;
106
107         if (keyid == 0)  {
108                 return NULL;
109         }
110
111         key = createandaddtohash(keyid);
112
113         if (key->gotsigs == false) {
114                 key->sigs = getkeysigs(key->keyid, &revoked);
115                 key->revoked = revoked;
116                 for (cursig = key->sigs; cursig != NULL;
117                                 cursig = cursig->next) {
118                         signedkey = (struct stats_key *) cursig->object;
119                         signedkey->signs = lladd(signedkey->signs, key);
120                 }
121                 key->gotsigs = true;
122         }
123
124         return key->sigs;
125 }
126
127 #ifdef NEED_GETFULLKEYID
128 /**
129  *      getfullkeyid - Maps a 32bit key id to a 64bit one.
130  *      @keyid: The 32bit keyid.
131  *
132  *      This function maps a 32bit key id to the full 64bit one. It returns the
133  *      full keyid. If the key isn't found a keyid of 0 is returned.
134  */
135 uint64_t getfullkeyid(uint64_t keyid)
136 {
137         struct openpgp_publickey *publickey = NULL;
138
139         if (keyid < 0x100000000LL) {
140                 fetch_key(keyid, &publickey, false);
141                 if (publickey != NULL) {
142                         keyid = get_keyid(publickey);
143                         free_publickey(publickey);
144                         publickey = NULL;
145                 } else {
146                         keyid = 0;
147                 }
148         }
149         
150         return keyid;
151 }
152 #endif