]> the.earth.li Git - onak.git/blob - stats.c
Bump debhelper compat level to 13
[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, see <https://www.gnu.org/licenses/>.
17  */
18
19 #include <inttypes.h>
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 "stats.h"
29
30 /**
31  *      initcolour - Clear the key graph ready for use.
32  *      @parent: Do we want to clear the parent pointers too?
33  *
34  *      Clears the parent and colour information on all elements in the key
35  *      graph.
36  */
37 void initcolour(bool parent)
38 {
39         unsigned int loop;
40         struct ll *curkey;
41
42         /*
43          * Init the colour/parent values. We get each entry list from the hash
44          * table and walk along it, zeroing the values.
45          */
46         for (loop = 0; loop < HASHSIZE; loop++) {
47                 curkey = gethashtableentry(loop);
48                 while (curkey != NULL) {
49                         ((struct stats_key *)curkey->object)->colour = 0;
50                         if (parent) {
51                                 ((struct stats_key *)curkey->object)->parent =
52                                         0;
53                         }
54                         curkey = curkey->next;
55                 }
56         }
57 }
58
59 /**
60  *      findpath - Given 2 keys finds a path between them.
61  *      @have: The key we have.
62  *      @want: The key we want to get to.
63  *
64  *      This does a breadth first search on the key tree, starting with the
65  *      key we have. It returns as soon as a path is found or when we run out
66  *      of keys; whichever comes sooner.
67  */
68 unsigned long findpath(struct onak_dbctx *dbctx,
69                 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 = dbctx->cached_getkeysigs(dbctx, ((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(struct onak_dbctx *dbctx,
136                 uint64_t have, uint64_t want, bool html, int count)
137 {
138         struct stats_key *keyinfoa, *keyinfob, *curkey;
139         int rec;
140         int pathnum;
141         char *uid;
142
143         /*
144          * Make sure the keys we have and want are in the cache.
145          */
146         (void) dbctx->cached_getkeysigs(dbctx, have);
147         (void) dbctx->cached_getkeysigs(dbctx, want);
148
149         if ((keyinfoa = findinhash(have)) == NULL) {
150                 printf("Couldn't find key 0x%016" PRIX64 ".\n", have);
151                 return;
152         }
153         if ((keyinfob = findinhash(want)) == NULL) {
154                 printf("Couldn't find key 0x%016" PRIX64 ".\n", want);
155                 return;
156         }
157
158         pathnum = 0;
159         
160         while ((!cleanup()) && (pathnum < count)) {
161                 /*
162                  * Fill the tree info up.
163                  */
164                 initcolour(true);
165                 rec = findpath(dbctx, keyinfoa, keyinfob);
166                 keyinfob->parent = 0;
167
168                 printf("%s%d nodes examined. %ld elements in the hash%s\n",
169                         html ? "<HR>" : "",
170                         rec,
171                         hashelements(),
172                         html ? "<BR>" : "");
173                 if (keyinfoa->colour == 0) {
174                         if (pathnum == 0) {
175                                 printf("Can't find a link from 0x%016" PRIX64
176                                 " to 0x%016" PRIX64 "%s\n",
177                                 have,
178                                 want,
179                                 html ? "<BR>" : "");
180                         } else {
181                                 printf("Can't find any further paths%s\n",
182                                         html ? "<BR>" : "");
183                         }
184                         pathnum = count;
185                 } else {
186                         printf("%d steps from 0x%016" PRIX64 " to 0x%016"
187                                 PRIX64 "%s\n",
188                                 keyinfoa->colour, have,
189                                 want,
190                                 html ? "<BR>" : "");
191                         curkey = keyinfoa;
192                         while (curkey != NULL && curkey->keyid != 0) {
193                                 uid = dbctx->keyid2uid(dbctx,
194                                                 curkey->keyid);
195                                 if (html && uid == NULL) {
196                                         printf("<a href=\"lookup?op=get&search="
197                                                 "0x%016" PRIX64 "\">0x%016"
198                                                 PRIX64 "</a> (["
199                                                 "User id not found])%s<BR>\n",
200                                                 curkey->keyid,
201                                                 curkey->keyid,
202                                                 (curkey->keyid == want) ?
203                                                         "" : " signs");
204                                 } else if (html && uid != NULL) {
205                                         printf("<a href=\"lookup?op=get&search="
206                                                 "0x%016" PRIX64 "\">0x%016"
207                                                 PRIX64 "</a>"
208                                                 " (<a href=\"lookup?op=vindex&"
209                                                 "search=0x%016" PRIX64
210                                                 "\">%s</a>)%s"
211                                                 "<BR>\n",
212                                                 curkey->keyid,
213                                                 curkey->keyid,
214                                                 curkey->keyid,
215                                                 txt2html(uid),
216                                                 (curkey->keyid == want) ?
217                                                 "" : " signs");
218                                 } else {
219                                         printf("0x%016" PRIX64 " (%s)%s\n",
220                                                 curkey->keyid,
221                                                 (uid == NULL) ?
222                                                         "[User id not found]" :
223                                                         uid,
224                                                 (curkey->keyid == want) ?
225                                                 "" : " signs");
226                                 }
227                                 if (uid != NULL) {
228                                         free(uid);
229                                         uid = NULL;
230                                 }
231                                 if (curkey != keyinfoa && curkey != keyinfob) {
232                                         curkey->disabled = true;
233                                 }
234                                 curkey = findinhash(curkey->parent);
235                         }
236                         if (html) {
237                                 puts("<P>List of key ids in path:</P>");
238                         } else {
239                                 puts("List of key ids in path:");
240                         }
241                         curkey = keyinfoa;
242                         while (curkey != NULL && curkey->keyid != 0) {
243                                 printf("0x%016" PRIX64 " ",
244                                                 curkey->keyid);
245                                 curkey = findinhash(curkey->parent);
246                         }
247                         putchar('\n');
248                 }
249                 pathnum++;
250         }
251 }
252
253
254
255 struct stats_key *furthestkey(struct onak_dbctx *dbctx, struct stats_key *have)
256 {
257         unsigned long count = 0;
258         unsigned long curdegree = 0;
259         struct ll *curll, *nextll, *tmp;
260         struct ll *sigs = NULL;
261         struct stats_key *max;
262
263         if (have == NULL) {
264                 return NULL;
265         }
266
267         ++curdegree;
268
269         nextll = NULL;
270         max = have;
271         curll = lladd(NULL, have);
272
273         while (curll != NULL) {
274                 sigs = dbctx->cached_getkeysigs(dbctx, ((struct stats_key *)
275                                 curll->object)->keyid);
276                 while (sigs != NULL) {
277                         if (((struct stats_key *) sigs->object)->colour == 0) {
278                                 /*
279                                  * We've never seen it. Count it, mark it and
280                                  * explore its subtree.
281                                  */
282                                 count++;
283                                 max = (struct stats_key *)sigs->object;
284                                 ((struct stats_key *)sigs->object)->colour = 
285                                         curdegree;
286                                 ((struct stats_key *)sigs->object)->parent = 
287                                         ((struct stats_key *)
288                                          curll->object)->keyid;
289                                 
290                                 nextll=lladd(nextll, sigs->object);
291                         }
292                         sigs=sigs->next;
293                 }
294                 tmp = curll->next;
295                 free(curll);
296                 curll = tmp;
297                 if (curll == NULL) {
298                         curll = nextll;
299                         nextll = NULL;
300                         ++curdegree;
301                 };
302         }
303
304         return max;
305 }