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