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