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