cscvs to tla changeset 1
[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 /**
27  *      keyid2uid - Takes a keyid and returns the primary UID for it.
28  *      @keyid: The keyid to lookup.
29  */
30 char *keyid2uid(uint64_t keyid)
31 {
32         struct openpgp_publickey *publickey = NULL;
33         struct openpgp_signedpacket_list *curuid = NULL;
34         static char buf[1024];
35
36         buf[0]=0;
37         if (fetch_key(keyid, &publickey) && publickey != NULL) {
38                 curuid = publickey->uids;
39                 while (curuid != NULL && buf[0] == 0) {
40                         if (curuid->packet->tag == 13) {
41                                 snprintf(buf, 1023, "%.*s",
42                                                 (int) curuid->packet->length,
43                                                 curuid->packet->data);
44                         }
45                         curuid = curuid -> next;
46                 }
47                 free_publickey(publickey);
48         }
49
50         if (buf[0] == 0) {
51                 return NULL;
52         } else {
53                 return buf;
54         }
55 }
56
57 /**
58  *      getkeysigs - Gets a linked list of the signatures on a key.
59  *      @keyid: The keyid to get the sigs for.
60  *
61  *      This function gets the list of signatures on a key. Used for key 
62  *      indexing and doing stats bits.
63  */
64 struct ll *getkeysigs(uint64_t keyid)
65 {
66         struct ll *sigs = NULL;
67         struct openpgp_signedpacket_list *uids = NULL;
68         struct openpgp_publickey *publickey = NULL;
69
70         fetch_key(keyid, &publickey);
71         
72         if (publickey != NULL) {
73                 for (uids = publickey->uids; uids != NULL; uids = uids->next) {
74                         sigs = keysigs(sigs, uids->sigs);
75                 }
76                 free_publickey(publickey);
77         }
78         
79         return sigs;
80 }