Update Debian Vcs-* fields to point to git repository
[onak.git] / sixdegrees.c
1 /*
2  * sixdegrees.c - List the size of the six degrees of trust away from a key.
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 <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "hash.h"
26 #include "keydb.h"
27 #include "keystructs.h"
28 #include "ll.h"
29 #include "log.h"
30 #include "onak-conf.h"
31 #include "stats.h"
32
33 unsigned long countdegree(struct stats_key *have, bool sigs, int maxdegree)
34 {
35         unsigned long     count = 0, curdegree = 0;
36         struct ll        *curll, *nextll, *sigll, *tmp;
37         struct stats_key *key = NULL;
38
39         ++curdegree;
40
41         nextll = NULL;
42         curll = lladd(NULL, have);
43
44         while (curll != NULL && curdegree <= maxdegree) {
45                 if (sigs) {
46                         sigll = config.dbbackend->cached_getkeysigs(
47                                 ((struct stats_key *)
48                                 curll->object)->keyid);
49                 } else {
50                         sigll = NULL;
51                         key = findinhash(((struct stats_key *)
52                                 curll->object)->keyid);
53                         if (key != NULL) {
54                                 sigll = key->signs;
55                         }
56                 }
57                 while (sigll != NULL) {
58                         if (((struct stats_key *) sigll->object)->colour==0) {
59                                 /* We've never seen it. Count it, mark it and
60                                         explore its subtree */
61                                 count++;
62                                 ((struct stats_key *)sigll->object)->colour = 
63                                         curdegree;
64                                 ((struct stats_key *)sigll->object)->parent = 
65                                         ((struct stats_key *)
66                                          curll->object)->keyid;
67                                 nextll=lladd(nextll, sigll->object);
68                         }
69                         sigll = sigll->next;
70                 }
71                 tmp = curll->next;
72                 free(curll);
73                 curll = tmp;
74                 if (curll == NULL) {
75                         curll = nextll;
76                         nextll = NULL;
77                         ++curdegree;
78                 };
79         }
80         if (curll != NULL) {
81                 llfree(curll, NULL);
82                 curll = NULL;
83         }
84         if (nextll != NULL) {
85                 llfree(nextll, NULL);
86                 nextll = NULL;
87         }
88
89         return count;
90 }
91
92 void sixdegrees(uint64_t keyid)
93 {
94         struct stats_key *keyinfo;
95         int loop;
96         long degree;
97         char *uid;
98
99         config.dbbackend->cached_getkeysigs(keyid);
100
101         if ((keyinfo = findinhash(keyid)) == NULL) {
102                 printf("Couldn't find key 0x%016" PRIX64 ".\n", keyid);
103                 return;
104         }
105
106         uid = config.dbbackend->keyid2uid(keyinfo->keyid);
107         printf("Six degrees for 0x%016" PRIX64 " (%s):\n", keyinfo->keyid,
108                         uid);
109         free(uid);
110         uid = NULL;
111
112         /*
113          * Cheat. This prefills the ->sign part of all the keys we want to
114          * look at so that we can output that info at the same time as the
115          * signers. However we're assuming that the signers and signees are
116          * reasonably closely related otherwise the info is wildly off - the
117          * only way to get 100% accurate results is to examine every key to see
118          * if it's signed by the key we're looking at.
119          */
120         initcolour(false);
121         degree = countdegree(keyinfo, true, 7);
122
123         puts("\t\tSigned by\t\tSigns");
124         for (loop = 1; loop < 7; loop++) {
125                 initcolour(false);
126                 degree = countdegree(keyinfo, true, loop);
127                 printf("Degree %d:\t%8ld", loop, degree);
128
129                 initcolour(false);
130                 degree = countdegree(keyinfo, false, loop);
131                 printf("\t\t%8ld\n", degree);
132         }
133 }
134
135 int main(int argc, char *argv[])
136 {
137         int optchar;
138         char *configfile = NULL;
139         uint64_t keyid = 0x2DA8B985;
140
141         while ((optchar = getopt(argc, argv, "c:")) != -1 ) {
142                 switch (optchar) {
143                 case 'c':
144                         configfile = strdup(optarg);
145                         break;
146                 }
147         }
148
149         if (optind < argc) {
150                 keyid = strtoll(argv[optind], NULL, 16);
151         }
152
153         readconfig(configfile);
154         initlogthing("sixdegrees", config.logfile);
155         config.dbbackend->initdb(true);
156         inithash();
157         sixdegrees(config.dbbackend->getfullkeyid(keyid));
158         destroyhash();
159         config.dbbackend->cleanupdb();
160         cleanuplogthing();
161         cleanupconfig();
162
163         return 0;
164 }