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