1 /*	$OpenBSD: output-json.c,v 1.12 2020/05/03 20:24:02 deraadt Exp $ */
2 /*
3  * Copyright (c) 2019 Claudio Jeker <claudio@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <time.h>
21 #include <netdb.h>
22 #include <openssl/ssl.h>
23 
24 #include "extern.h"
25 
26 static int
27 outputheader_json(FILE *out, struct stats *st)
28 {
29 	char		hn[NI_MAXHOST], tbuf[26];
30 	struct tm	*tp;
31 	time_t		t;
32 
33 	time(&t);
34 	setenv("TZ", "UTC", 1);
35 	tp = localtime(&t);
36 	strftime(tbuf, sizeof tbuf, "%FT%TZ", tp);
37 
38 	gethostname(hn, sizeof hn);
39 
40 	if (fprintf(out,
41 	    "{\n\t\"metadata\": {\n"
42 	    "\t\t\"buildmachine\": \"%s\",\n"
43 	    "\t\t\"buildtime\": \"%s\",\n"
44 	    "\t\t\"elapsedtime\": \"%lld\",\n"
45 	    "\t\t\"usertime\": \"%lld\",\n"
46 	    "\t\t\"systemtime\": \"%lld\",\n"
47 	    "\t\t\"roas\": %zu,\n"
48 	    "\t\t\"failedroas\": %zu,\n"
49 	    "\t\t\"invalidroas\": %zu,\n"
50 	    "\t\t\"certificates\": %zu,\n"
51 	    "\t\t\"failcertificates\": %zu,\n"
52 	    "\t\t\"invalidcertificates\": %zu,\n"
53 	    "\t\t\"tals\": %zu,\n"
54 	    "\t\t\"talfiles\": \"%s\",\n"
55 	    "\t\t\"manifests\": %zu,\n"
56 	    "\t\t\"failedmanifests\": %zu,\n"
57 	    "\t\t\"stalemanifests\": %zu,\n"
58 	    "\t\t\"crls\": %zu,\n"
59 	    "\t\t\"repositories\": %zu,\n"
60 	    "\t\t\"vrps\": %zu,\n"
61 	    "\t\t\"uniquevrps\": %zu\n"
62 	    "\t},\n\n",
63 	    hn, tbuf, (long long)st->elapsed_time.tv_sec,
64 	    (long long)st->user_time.tv_sec, (long long)st->system_time.tv_sec,
65 	    st->roas, st->roas_fail, st->roas_invalid,
66 	    st->certs, st->certs_fail, st->certs_invalid,
67 	    st->tals, st->talnames,
68 	    st->mfts, st->mfts_fail, st->mfts_stale,
69 	    st->crls,
70 	    st->repos,
71 	    st->vrps, st->uniqs) < 0)
72 		return -1;
73 	return 0;
74 }
75 
76 int
77 output_json(FILE *out, struct vrp_tree *vrps, struct stats *st)
78 {
79 	char		 buf[64];
80 	struct vrp	*v;
81 	int		 first = 1;
82 
83 	if (outputheader_json(out, st) < 0)
84 		return -1;
85 
86 	if (fprintf(out, "\t\"roas\": [\n") < 0)
87 		return -1;
88 
89 	RB_FOREACH(v, vrp_tree, vrps) {
90 		if (first)
91 			first = 0;
92 		else {
93 			if (fprintf(out, ",\n") < 0)
94 				return -1;
95 		}
96 
97 		ip_addr_print(&v->addr, v->afi, buf, sizeof(buf));
98 
99 		if (fprintf(out, "\t\t{ \"asn\": \"AS%u\", \"prefix\": \"%s\", "
100 		    "\"maxLength\": %u, \"ta\": \"%s\" }",
101 		    v->asid, buf, v->maxlength, v->tal) < 0)
102 			return -1;
103 	}
104 
105 	if (fprintf(out, "\n\t]\n}\n") < 0)
106 		return -1;
107 	return 0;
108 }
109