]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/swanctl/commands/list_authorities.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / swanctl / commands / list_authorities.c
1 /*
2 * Copyright (C) 2015 Andreas Steffen
3 *
4 * Copyright (C) secunet Security Networks AG
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
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <errno.h>
20
21 #include "command.h"
22
23 #define LABELED_CRL_URI (1 << 0)
24 #define LABELED_OCSP_URI (1 << 1)
25
26 CALLBACK(authority_kv, int,
27 void *null, vici_res_t *res, char *name, void *value, int len)
28 {
29 chunk_t chunk;
30
31 chunk = chunk_create(value, len);
32 if (chunk_printable(chunk, NULL, ' '))
33 {
34 printf(" %s: %.*s\n", name, len, value);
35 }
36
37 return 0;
38 }
39
40
41 CALLBACK(authority_list, int,
42 int *labeled, vici_res_t *res, char *name, void *value, int len)
43 {
44 chunk_t chunk;
45
46 chunk = chunk_create(value, len);
47 if (chunk_printable(chunk, NULL, ' '))
48 {
49 if (streq(name, "crl_uris"))
50 {
51 printf(" %s %.*s\n",
52 (*labeled & LABELED_CRL_URI) ? " " : "crl_uris: ",
53 len, value);
54 *labeled |= LABELED_CRL_URI;
55 }
56 if (streq(name, "ocsp_uris"))
57 {
58 printf(" %s %.*s\n",
59 (*labeled & LABELED_OCSP_URI) ? " " : "ocsp_uris:",
60 len, value);
61 *labeled %= LABELED_OCSP_URI;
62 }
63 }
64 return 0;
65 }
66
67 CALLBACK(authorities, int,
68 void *null, vici_res_t *res, char *name)
69 {
70 int labeled = 0;
71
72 printf("%s:\n", name);
73
74 return vici_parse_cb(res, NULL, authority_kv, authority_list, &labeled);
75 }
76
77 CALLBACK(list_cb, void,
78 command_format_options_t *format, char *name, vici_res_t *res)
79 {
80 if (*format & COMMAND_FORMAT_RAW)
81 {
82 vici_dump(res, "list-authorities event", *format & COMMAND_FORMAT_PRETTY,
83 stdout);
84 }
85 else
86 {
87 if (vici_parse_cb(res, authorities, NULL, NULL, NULL) != 0)
88 {
89 fprintf(stderr, "parsing authority event failed: %s\n",
90 strerror(errno));
91 }
92 }
93 }
94
95 static int list_authorities(vici_conn_t *conn)
96 {
97 vici_req_t *req;
98 vici_res_t *res;
99 command_format_options_t format = COMMAND_FORMAT_NONE;
100 char *arg, *ca_name = NULL;;
101 int ret = 0;
102
103 while (TRUE)
104 {
105 switch (command_getopt(&arg))
106 {
107 case 'h':
108 return command_usage(NULL);
109 case 'n':
110 ca_name = arg;
111 continue;
112 case 'P':
113 format |= COMMAND_FORMAT_PRETTY;
114 /* fall through to raw */
115 case 'r':
116 format |= COMMAND_FORMAT_RAW;
117 continue;
118 case EOF:
119 break;
120 default:
121 return command_usage("invalid --list-authorities option");
122 }
123 break;
124 }
125 if (vici_register(conn, "list-authority", list_cb, &format) != 0)
126 {
127 ret = errno;
128 fprintf(stderr, "registering for authorities failed: %s\n",
129 strerror(errno));
130 return ret;
131 }
132
133 req = vici_begin("list-authorities");
134 if (ca_name)
135 {
136 vici_add_key_valuef(req, "name", "%s", ca_name);
137 }
138 res = vici_submit(req, conn);
139 if (!res)
140 {
141 ret = errno;
142 fprintf(stderr, "list-authorities request failed: %s\n", strerror(errno));
143 return ret;
144 }
145 if (format & COMMAND_FORMAT_RAW)
146 {
147 vici_dump(res, "list-authorities reply", format & COMMAND_FORMAT_PRETTY,
148 stdout);
149 }
150 vici_free_res(res);
151 return 0;
152 }
153
154 /**
155 * Register the command.
156 */
157 static void __attribute__ ((constructor))reg()
158 {
159 command_register((command_t) {
160 list_authorities, 'B', "list-authorities",
161 "list loaded authority configurations",
162 {"[--raw|--pretty]"},
163 {
164 {"help", 'h', 0, "show usage information"},
165 {"name", 'n', 1, "filter by authority name"},
166 {"raw", 'r', 0, "dump raw response message"},
167 {"pretty", 'P', 0, "dump raw response message in pretty print"},
168 }
169 });
170 }