Only seed database for Debian install if we're using default config
[onak.git] / stats.c
1 /*
2  * stats.c - various routines to do stats on the key graph
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2000-2002 Project Purple
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 #include "cleanup.h"
13 #include "getcgi.h"
14 #include "hash.h"
15 #include "keydb.h"
16 #include "ll.h"
17 #include "onak-conf.h"
18 #include "stats.h"
19
20 /**
21  *      initcolour - Clear the key graph ready for use.
22  *      @parent: Do we want to clear the parent pointers too?
23  *
24  *      Clears the parent and colour information on all elements in the key
25  *      graph.
26  */
27 void initcolour(bool parent)
28 {
29         unsigned int loop;
30         struct ll *curkey;
31
32         /*
33          * Init the colour/parent values. We get each entry list from the hash
34          * table and walk along it, zeroing the values.
35          */
36         for (loop = 0; loop < HASHSIZE; loop++) {
37                 curkey = gethashtableentry(loop);
38                 while (curkey != NULL) {
39                         ((struct stats_key *)curkey->object)->colour = 0;
40                         if (parent) {
41                                 ((struct stats_key *)curkey->object)->parent =
42                                         0;
43                         }
44                         curkey = curkey->next;
45                 }
46         }
47 }
48
49 /**
50  *      findpath - Given 2 keys finds a path between them.
51  *      @have: The key we have.
52  *      @want: The key we want to get to.
53  *
54  *      This does a breadth first search on the key tree, starting with the
55  *      key we have. It returns as soon as a path is found or when we run out
56  *      of keys; whichever comes sooner.
57  */
58 unsigned long findpath(struct stats_key *have, struct stats_key *want)
59 {
60         struct ll *keys = NULL;
61         struct ll *oldkeys = NULL;
62         struct ll *sigs = NULL;
63         struct ll *nextkeys = NULL;
64         long curdegree = 0;
65         unsigned long count = 0;
66         
67         curdegree = 1;
68         keys = lladd(NULL, want);
69         oldkeys = keys;
70
71         while ((!cleanup()) && keys != NULL && have->colour == 0) {
72                 sigs = config.dbbackend->cached_getkeysigs(((struct stats_key *)
73                                         keys->object)->keyid);
74                 while ((!cleanup()) && sigs != NULL && have->colour == 0) {
75                         /*
76                          * Check if we've seen this key before and if not mark
77                          * it and add its sigs to the list we want to look at.
78                          */
79                         if (!((struct stats_key *)sigs->object)->disabled &&
80                             !((struct stats_key *)sigs->object)->revoked &&
81                             ((struct stats_key *)sigs->object)->colour == 0) {
82                                 count++;
83                                 ((struct stats_key *)sigs->object)->colour =
84                                         curdegree;
85                                 ((struct stats_key *)sigs->object)->parent =
86                                         ((struct stats_key *)
87                                          keys->object)->keyid;
88                                 nextkeys = lladd(nextkeys, sigs->object);
89                         }
90                         sigs = sigs->next;
91                 }
92                 keys = keys->next;
93                 if (keys == NULL) {
94                         keys = nextkeys;
95                         llfree(oldkeys, NULL);
96                         oldkeys = keys;
97                         nextkeys = NULL;
98                         curdegree++;
99                 }
100         }
101         if (oldkeys != NULL) {
102                 llfree(oldkeys, NULL);
103                 oldkeys = NULL;
104         }
105         if (nextkeys != NULL) {
106                 llfree(nextkeys, NULL);
107                 nextkeys = NULL;
108         }
109
110         return count;
111 }
112
113 /**
114  *      dofindpath - Given 2 keys displays a path between them.
115  *      @have: The key we have.
116  *      @want: The key we want to get to.
117  *      @html: Should we output in html.
118  *      @count: How many paths we should look for.
119  *
120  *      This does a breadth first search on the key tree, starting with the
121  *      key we have. It returns as soon as a path is found or when we run out
122  *      of keys; whichever comes sooner.
123  */
124 void dofindpath(uint64_t have, uint64_t want, bool html, int count)
125 {
126         struct stats_key *keyinfoa, *keyinfob, *curkey;
127         uint64_t fullhave, fullwant;
128         int rec;
129         int pathnum;
130         char *uid;
131
132         fullhave = config.dbbackend->getfullkeyid(have);
133         fullwant = config.dbbackend->getfullkeyid(want);
134
135         /*
136          * Make sure the keys we have and want are in the cache.
137          */
138         (void) config.dbbackend->cached_getkeysigs(fullhave);
139         (void) config.dbbackend->cached_getkeysigs(fullwant);
140
141         if ((keyinfoa = findinhash(fullhave)) == NULL) {
142                 printf("Couldn't find key 0x%016" PRIX64 ".\n", have);
143                 return;
144         }
145         if ((keyinfob = findinhash(fullwant)) == NULL) {
146                 printf("Couldn't find key 0x%016" PRIX64 ".\n", want);
147                 return;
148         }
149
150         pathnum = 0;
151         
152         while ((!cleanup()) && (pathnum < count)) {
153                 /*
154                  * Fill the tree info up.
155                  */
156                 initcolour(true);
157                 rec = findpath(keyinfoa, keyinfob);
158                 keyinfob->parent = 0;
159
160                 printf("%s%d nodes examined. %ld elements in the hash%s\n",
161                         html ? "<HR>" : "",
162                         rec,
163                         hashelements(),
164                         html ? "<BR>" : "");
165                 if (keyinfoa->colour == 0) {
166                         if (pathnum == 0) {
167                                 printf("Can't find a link from 0x%08" PRIX64
168                                 " to 0x%08" PRIX64 "%s\n",
169                                 have,
170                                 want,
171                                 html ? "<BR>" : "");
172                         } else {
173                                 printf("Can't find any further paths%s\n",
174                                         html ? "<BR>" : "");
175                         }
176                         pathnum = count;
177                 } else {
178                         printf("%d steps from 0x%08" PRIX64 " to 0x%08" PRIX64
179                                 "%s\n",
180                                 keyinfoa->colour, have & 0xFFFFFFFF,
181                                 want & 0xFFFFFFFF,
182                                 html ? "<BR>" : "");
183                         curkey = keyinfoa;
184                         while (curkey != NULL && curkey->keyid != 0) {
185                                 uid = config.dbbackend->keyid2uid(
186                                                 curkey->keyid);
187                                 if (html && uid == NULL) {
188                                         printf("<a href=\"lookup?op=get&search="
189                                                 "0x%08" PRIX64 "\">0x%08" PRIX64
190                                                 "</a> (["
191                                                 "User id not found])%s<BR>\n",
192                                                 curkey->keyid & 0xFFFFFFFF,
193                                                 curkey->keyid & 0xFFFFFFFF,
194                                                 (curkey->keyid == fullwant) ?
195                                                         "" : " signs");
196                                 } else if (html && uid != NULL) {
197                                         printf("<a href=\"lookup?op=get&search="
198                                                 "0x%08" PRIX64 "\">0x%08"
199                                                 PRIX64 "</a>"
200                                                 " (<a href=\"lookup?op=vindex&"
201                                                 "search=0x%08" PRIX64 
202                                                 "\">%s</a>)%s"
203                                                 "<BR>\n",
204                                                 curkey->keyid & 0xFFFFFFFF,
205                                                 curkey->keyid & 0xFFFFFFFF,
206                                                 curkey->keyid & 0xFFFFFFFF,
207                                                 txt2html(uid),
208                                                 (curkey->keyid == fullwant) ?
209                                                 "" : " signs");
210                                 } else {
211                                         printf("0x%08" PRIX64 " (%s)%s\n",
212                                                 curkey->keyid & 0xFFFFFFFF,
213                                                 (uid == NULL) ?
214                                                         "[User id not found]" :
215                                                         uid,
216                                                 (curkey->keyid == fullwant) ?
217                                                 "" : " signs");
218                                 }
219                                 if (uid != NULL) {
220                                         free(uid);
221                                         uid = NULL;
222                                 }
223                                 if (curkey != keyinfoa && curkey != keyinfob) {
224                                         curkey->disabled = true;
225                                 }
226                                 curkey = findinhash(curkey->parent);
227                         }
228                         if (html) {
229                                 puts("<P>List of key ids in path:</P>");
230                         } else {
231                                 puts("List of key ids in path:");
232                         }
233                         curkey = keyinfoa;
234                         while (curkey != NULL && curkey->keyid != 0) {
235                                 printf("0x%08" PRIX64 " ",
236                                                 curkey->keyid & 0xFFFFFFFF);
237                                 curkey = findinhash(curkey->parent);
238                         }
239                         putchar('\n');
240                 }
241                 pathnum++;
242         }
243 }
244
245
246
247 struct stats_key *furthestkey(struct stats_key *have)
248 {
249         unsigned long count = 0;
250         unsigned long curdegree = 0;
251         struct ll *curll, *nextll, *tmp;
252         struct ll *sigs = NULL;
253         struct stats_key *max;
254
255         if (have == NULL) {
256                 return NULL;
257         }
258
259         ++curdegree;
260
261         nextll = NULL;
262         max = have;
263         curll = lladd(NULL, have);
264
265         while (curll != NULL) {
266                 sigs = config.dbbackend->cached_getkeysigs(((struct stats_key *)
267                                 curll->object)->keyid);
268                 while (sigs != NULL) {
269                         if (((struct stats_key *) sigs->object)->colour == 0) {
270                                 /*
271                                  * We've never seen it. Count it, mark it and
272                                  * explore its subtree.
273                                  */
274                                 count++;
275                                 max = (struct stats_key *)sigs->object;
276                                 ((struct stats_key *)sigs->object)->colour = 
277                                         curdegree;
278                                 ((struct stats_key *)sigs->object)->parent = 
279                                         ((struct stats_key *)
280                                          curll->object)->keyid;
281                                 
282                                 nextll=lladd(nextll, sigs->object);
283                         }
284                         sigs=sigs->next;
285                 }
286                 tmp = curll->next;
287                 free(curll);
288                 curll = tmp;
289                 if (curll == NULL) {
290                         curll = nextll;
291                         nextll = NULL;
292                         ++curdegree;
293                 };
294         }
295
296         return max;
297 }