cscvs to tla changeset 11
[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 "keydb.h"
20 #include "keyid.h"
21 #include "keyindex.h"
22 #include "keystructs.h"
23 #include "mem.h"
24 #include "parsekey.h"
25
26 #ifdef NEED_KEYID2UID
27 /**
28  *      keyid2uid - Takes a keyid and returns the primary UID for it.
29  *      @keyid: The keyid to lookup.
30  */
31 char *keyid2uid(uint64_t keyid)
32 {
33         struct openpgp_publickey *publickey = NULL;
34         struct openpgp_signedpacket_list *curuid = NULL;
35         char buf[1024];
36
37         buf[0]=0;
38         if (fetch_key(keyid, &publickey, false) && publickey != NULL) {
39                 curuid = publickey->uids;
40                 while (curuid != NULL && buf[0] == 0) {
41                         if (curuid->packet->tag == 13) {
42                                 snprintf(buf, 1023, "%.*s",
43                                                 (int) curuid->packet->length,
44                                                 curuid->packet->data);
45                         }
46                         curuid = curuid -> next;
47                 }
48                 free_publickey(publickey);
49         }
50
51         if (buf[0] == 0) {
52                 return NULL;
53         } else {
54                 return strdup(buf);
55         }
56 }
57 #endif
58
59 #ifdef NEED_GETKEYSIGS
60 /**
61  *      getkeysigs - Gets a linked list of the signatures on a key.
62  *      @keyid: The keyid to get the sigs for.
63  *
64  *      This function gets the list of signatures on a key. Used for key 
65  *      indexing and doing stats bits.
66  */
67 struct ll *getkeysigs(uint64_t keyid)
68 {
69         struct ll *sigs = NULL;
70         struct openpgp_signedpacket_list *uids = NULL;
71         struct openpgp_publickey *publickey = NULL;
72
73         fetch_key(keyid, &publickey, false);
74         
75         if (publickey != NULL) {
76                 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
77                         sigs = keysigs(sigs, uids->sigs);
78                 }
79                 free_publickey(publickey);
80         }
81
82         return sigs;
83 }
84 #endif
85
86 #ifdef NEED_GETFULLKEYID
87 /**
88  *      getfullkeyid - Maps a 32bit key id to a 64bit one.
89  *      @keyid: The 32bit keyid.
90  *
91  *      This function maps a 32bit key id to the full 64bit one. It returns the
92  *      full keyid.
93  */
94 uint64_t getfullkeyid(uint64_t keyid)
95 {
96         struct openpgp_publickey *publickey = NULL;
97
98         if (keyid < 0x100000000LL) {
99                 fetch_key(keyid, &publickey, false);
100                 keyid = get_keyid(publickey);
101                 free_publickey(publickey);
102         }
103         
104         return keyid;
105 }
106 #endif