2 * sixdegrees.c - List the size of the six degrees of trust away from a key.
4 * Copyright 2001-2002 Jonathan McDowell <noodles@earth.li>
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.
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
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/>.
26 #include "keystructs.h"
29 #include "onak-conf.h"
32 unsigned long countdegree(struct onak_dbctx *dbctx,
33 struct stats_key *have, bool sigs, int maxdegree)
35 unsigned long count = 0, curdegree = 0;
36 struct ll *curll, *nextll, *sigll, *tmp;
37 struct stats_key *key = NULL;
42 curll = lladd(NULL, have);
44 while (curll != NULL && curdegree <= maxdegree) {
46 sigll = dbctx->cached_getkeysigs(dbctx,
48 curll->object)->keyid);
51 key = findinhash(((struct stats_key *)
52 curll->object)->keyid);
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 */
62 ((struct stats_key *)sigll->object)->colour =
64 ((struct stats_key *)sigll->object)->parent =
66 curll->object)->keyid;
67 nextll=lladd(nextll, sigll->object);
92 void sixdegrees(struct onak_dbctx *dbctx, uint64_t keyid)
94 struct stats_key *keyinfo;
99 dbctx->cached_getkeysigs(dbctx, keyid);
101 if ((keyinfo = findinhash(keyid)) == NULL) {
102 printf("Couldn't find key 0x%016" PRIX64 ".\n", keyid);
106 uid = dbctx->keyid2uid(dbctx, keyinfo->keyid);
107 printf("Six degrees for 0x%016" PRIX64 " (%s):\n", keyinfo->keyid,
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.
121 countdegree(dbctx, keyinfo, true, 7);
123 puts("\t\tSigned by\t\tSigns");
124 for (loop = 1; loop < 7; loop++) {
126 degree = countdegree(dbctx, keyinfo, true, loop);
127 printf("Degree %d:\t%8ld", loop, degree);
130 degree = countdegree(dbctx, keyinfo, false, loop);
131 printf("\t\t%8ld\n", degree);
135 int main(int argc, char *argv[])
138 char *configfile = NULL;
139 uint64_t keyid = 0x94FA372B2DA8B985;
140 struct onak_dbctx *dbctx;
142 while ((optchar = getopt(argc, argv, "c:")) != -1 ) {
145 if (configfile != NULL) {
148 configfile = strdup(optarg);
154 keyid = strtoll(argv[optind], NULL, 16);
157 readconfig(configfile);
159 initlogthing("sixdegrees", config.logfile);
160 dbctx = config.dbinit(config.backend, true);
163 sixdegrees(dbctx, keyid);
165 dbctx->cleanupdb(dbctx);
167 fprintf(stderr, "Couldn't initialize key database.\n");