]> the.earth.li Git - onak.git/blob - maxpath.c
d128e9c65e51583c75bed8e7e8c74336e4836c0c
[onak.git] / maxpath.c
1 /*
2  * maxpath.c - Find the longest trust path in the key database.
3  *
4  * Copyright 2001-2002 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 <getopt.h>
21 #include <inttypes.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "hash.h"
28 #include "keydb.h"
29 #include "ll.h"
30 #include "log.h"
31 #include "onak-conf.h"
32 #include "stats.h"
33
34 void findmaxpath(struct onak_dbctx *dbctx, unsigned long max)
35 {
36         struct stats_key *from, *to, *tmp;
37         struct ll *curkey;
38         unsigned long distance, loop;
39
40         distance = 0;
41         from = to = tmp = NULL;
42
43         /*
44          * My (noodles@earth.li, RSA) key is in the strongly connected set of
45          * keys, so we use it as a suitable starting seed.
46          */
47         dbctx->cached_getkeysigs(dbctx, 0x94FA372B2DA8B985);
48
49         /*
50          * Loop through the hash examining each key present and finding the
51          * furthest key from it. If it's further than our current max then
52          * store it as our new max and print out the fact we've found a new
53          * max.
54          */
55         for (loop = 0; (loop < HASHSIZE) && (distance < max); loop++) {
56                 curkey = gethashtableentry(loop);
57                 while (curkey != NULL && distance < max) {
58                         dbctx->cached_getkeysigs(dbctx,
59                                         ((struct stats_key *)
60                                         curkey->object)->keyid);
61                         initcolour(false);
62                         tmp = furthestkey(dbctx, (struct stats_key *)
63                                                 curkey->object);
64                         if (tmp->colour > distance) {
65                                 from = (struct stats_key *)curkey->object;
66                                 to = tmp;
67                                 distance = to->colour;
68                                 printf("Current max path (#%ld) is from %"
69                                                 PRIX64 " to %" PRIX64 
70                                                 " (%ld steps)\n", 
71                                                 loop,
72                                                 from->keyid,
73                                                 to->keyid,
74                                                 distance);
75                         }
76                         curkey=curkey->next;
77                 }
78         }
79         printf("Max path is from %" PRIX64 " to %" PRIX64 " (%ld steps)\n",
80                         from->keyid,
81                         to->keyid,
82                         distance);
83         dofindpath(dbctx, to->keyid, from->keyid, false, 1);
84 }
85
86 int main(int argc, char *argv[])
87 {
88         int optchar;
89         char *configfile = NULL;
90         struct onak_dbctx *dbctx;
91
92         while ((optchar = getopt(argc, argv, "c:")) != -1 ) {
93                 switch (optchar) {
94                 case 'c':
95                         if (configfile != NULL) {
96                                 free(configfile);
97                         }
98                         configfile = strdup(optarg);
99                         break;
100                 }
101         }
102
103         readconfig(configfile);
104         free(configfile);
105         initlogthing("maxpath", config.logfile);
106         dbctx = config.dbinit(config.backend, true);
107         if (dbctx != NULL) {
108                 inithash();
109                 findmaxpath(dbctx, 30);
110                 printf("--------\n");
111                 findmaxpath(dbctx, 30);
112                 destroyhash();
113                 dbctx->cleanupdb(dbctx);
114         } else {
115                 fprintf(stderr, "Couldn't initialize key database.\n");
116         }
117         cleanuplogthing();
118         cleanupconfig();
119         
120         return EXIT_SUCCESS;
121 }