]>
Commit | Line | Data |
---|---|---|
51bdc1f3 MW |
1 | /* |
2 | * Copyright (C) 2014 Martin Willi | |
3 | * Copyright (C) 2014 revosec AG | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation; either version 2 of the License, or (at your | |
8 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
12 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 | * for more details. | |
14 | */ | |
15 | ||
16 | #define _GNU_SOURCE | |
17 | #include <stdio.h> | |
18 | #include <errno.h> | |
19 | ||
20 | #include "command.h" | |
21 | ||
22 | #include <collections/hashtable.h> | |
23 | ||
24 | /** | |
25 | * Free hashtable with contained strings | |
26 | */ | |
27 | static void free_hashtable(hashtable_t *hashtable) | |
28 | { | |
29 | enumerator_t *enumerator; | |
30 | char *str; | |
31 | ||
32 | enumerator = hashtable->create_enumerator(hashtable); | |
33 | while (enumerator->enumerate(enumerator, NULL, &str)) | |
34 | { | |
35 | free(str); | |
36 | } | |
37 | enumerator->destroy(enumerator); | |
38 | ||
39 | hashtable->destroy(hashtable); | |
40 | } | |
41 | ||
42 | CALLBACK(values, int, | |
43 | hashtable_t *sa, vici_res_t *res, char *name, void *value, int len) | |
44 | { | |
45 | chunk_t chunk; | |
46 | char *str; | |
47 | ||
48 | chunk = chunk_create(value, len); | |
49 | if (chunk_printable(chunk, NULL, ' ')) | |
50 | { | |
51 | if (asprintf(&str, "%.*s", len, value) >= 0) | |
52 | { | |
53 | free(sa->put(sa, name, str)); | |
54 | } | |
55 | } | |
56 | return 0; | |
57 | } | |
58 | ||
59 | ||
60 | CALLBACK(list, int, | |
61 | hashtable_t *sa, vici_res_t *res, char *name, void *value, int len) | |
62 | { | |
63 | chunk_t chunk; | |
64 | char *str; | |
65 | ||
66 | chunk = chunk_create(value, len); | |
67 | if (chunk_printable(chunk, NULL, ' ')) | |
68 | { | |
69 | str = sa->get(sa, name); | |
70 | if (asprintf(&str, "%s%s%.*s", | |
71 | str ?: "", str ? " " : "", len, value) >= 0) | |
72 | { | |
73 | free(sa->put(sa, name, str)); | |
74 | } | |
75 | } | |
76 | return 0; | |
77 | } | |
78 | ||
79 | CALLBACK(children_sn, int, | |
80 | hashtable_t *ike, vici_res_t *res, char *name) | |
81 | { | |
82 | hashtable_t *child; | |
83 | int ret; | |
84 | ||
85 | child = hashtable_create(hashtable_hash_str, hashtable_equals_str, 1); | |
86 | ret = vici_parse_cb(res, NULL, values, list, child); | |
87 | if (ret == 0) | |
88 | { | |
89 | printf(" %s: %s\n", name, child->get(child, "mode")); | |
90 | printf(" local: %s\n", child->get(child, "local-ts")); | |
91 | printf(" remote: %s\n", child->get(child, "remote-ts")); | |
92 | } | |
93 | free_hashtable(child); | |
94 | return ret; | |
95 | } | |
96 | ||
97 | CALLBACK(conn_sn, int, | |
98 | hashtable_t *ike, vici_res_t *res, char *name) | |
99 | { | |
100 | int ret = 0; | |
101 | ||
102 | if (streq(name, "children")) | |
103 | { | |
104 | return vici_parse_cb(res, children_sn, NULL, NULL, NULL); | |
105 | } | |
94bb26fa | 106 | if (strpfx(name, "local") || strpfx(name, "remote")) |
51bdc1f3 MW |
107 | { |
108 | hashtable_t *auth; | |
109 | ||
110 | auth = hashtable_create(hashtable_hash_str, hashtable_equals_str, 1); | |
111 | ret = vici_parse_cb(res, NULL, values, list, auth); | |
112 | if (ret == 0) | |
113 | { | |
114 | printf(" %s %s authentication:\n", | |
94bb26fa MW |
115 | strpfx(name, "local") ? "local" : "remote", |
116 | auth->get(auth, "class") ?: "unspecified"); | |
51bdc1f3 MW |
117 | if (auth->get(auth, "id")) |
118 | { | |
119 | printf(" id: %s\n", auth->get(auth, "id")); | |
120 | } | |
121 | if (auth->get(auth, "groups")) | |
122 | { | |
123 | printf(" groups: %s\n", auth->get(auth, "groups")); | |
124 | } | |
125 | if (auth->get(auth, "certs")) | |
126 | { | |
127 | printf(" certs: %s\n", auth->get(auth, "certs")); | |
128 | } | |
129 | if (auth->get(auth, "cacerts")) | |
130 | { | |
131 | printf(" cacerts: %s\n", auth->get(auth, "cacerts")); | |
132 | } | |
133 | } | |
134 | free_hashtable(auth); | |
135 | } | |
136 | return ret; | |
137 | } | |
138 | ||
a2875525 MW |
139 | CALLBACK(conn_list, int, |
140 | hashtable_t *sa, vici_res_t *res, char *name, void *value, int len) | |
141 | { | |
142 | if (chunk_printable(chunk_create(value, len), NULL, ' ')) | |
143 | { | |
144 | if (streq(name, "local_addrs")) | |
145 | { | |
146 | printf(" local: %.*s\n", len, value); | |
147 | } | |
148 | if (streq(name, "remote_addrs")) | |
149 | { | |
150 | printf(" remote: %.*s\n", len, value); | |
151 | } | |
152 | } | |
153 | return 0; | |
154 | } | |
155 | ||
51bdc1f3 MW |
156 | CALLBACK(conns, int, |
157 | void *null, vici_res_t *res, char *name) | |
158 | { | |
159 | printf("%s: %s\n", name, vici_find_str(res, "", "%s.version", name)); | |
160 | ||
a2875525 | 161 | return vici_parse_cb(res, conn_sn, NULL, conn_list, NULL); |
51bdc1f3 MW |
162 | } |
163 | ||
164 | CALLBACK(list_cb, void, | |
dacb75f5 | 165 | command_format_options_t *format, char *name, vici_res_t *res) |
51bdc1f3 | 166 | { |
dacb75f5 | 167 | if (*format & COMMAND_FORMAT_RAW) |
51bdc1f3 | 168 | { |
dacb75f5 AS |
169 | vici_dump(res, "list-conn event", *format & COMMAND_FORMAT_PRETTY, |
170 | stdout); | |
51bdc1f3 MW |
171 | } |
172 | else | |
173 | { | |
174 | if (vici_parse_cb(res, conns, NULL, NULL, NULL) != 0) | |
175 | { | |
176 | fprintf(stderr, "parsing conn event failed: %s\n", strerror(errno)); | |
177 | } | |
178 | } | |
179 | } | |
180 | ||
181 | static int list_conns(vici_conn_t *conn) | |
182 | { | |
183 | vici_req_t *req; | |
184 | vici_res_t *res; | |
dacb75f5 | 185 | command_format_options_t format = COMMAND_FORMAT_NONE; |
51bdc1f3 | 186 | char *arg; |
67f9f09d | 187 | int ret; |
51bdc1f3 MW |
188 | |
189 | while (TRUE) | |
190 | { | |
191 | switch (command_getopt(&arg)) | |
192 | { | |
193 | case 'h': | |
194 | return command_usage(NULL); | |
dacb75f5 AS |
195 | case 'P': |
196 | format |= COMMAND_FORMAT_PRETTY; | |
197 | /* fall through to raw */ | |
51bdc1f3 | 198 | case 'r': |
dacb75f5 | 199 | format |= COMMAND_FORMAT_RAW; |
51bdc1f3 MW |
200 | continue; |
201 | case EOF: | |
202 | break; | |
203 | default: | |
204 | return command_usage("invalid --list-conns option"); | |
205 | } | |
206 | break; | |
207 | } | |
dacb75f5 | 208 | if (vici_register(conn, "list-conn", list_cb, &format) != 0) |
51bdc1f3 | 209 | { |
67f9f09d | 210 | ret = errno; |
51bdc1f3 MW |
211 | fprintf(stderr, "registering for connections failed: %s\n", |
212 | strerror(errno)); | |
67f9f09d | 213 | return ret; |
51bdc1f3 MW |
214 | } |
215 | req = vici_begin("list-conns"); | |
216 | res = vici_submit(req, conn); | |
217 | if (!res) | |
218 | { | |
67f9f09d | 219 | ret = errno; |
51bdc1f3 | 220 | fprintf(stderr, "list-conns request failed: %s\n", strerror(errno)); |
67f9f09d | 221 | return ret; |
51bdc1f3 | 222 | } |
dacb75f5 | 223 | if (format & COMMAND_FORMAT_RAW) |
51bdc1f3 | 224 | { |
dacb75f5 AS |
225 | vici_dump(res, "list-conns reply", format & COMMAND_FORMAT_PRETTY, |
226 | stdout); | |
51bdc1f3 MW |
227 | } |
228 | vici_free_res(res); | |
229 | return 0; | |
230 | } | |
231 | ||
232 | /** | |
233 | * Register the command. | |
234 | */ | |
235 | static void __attribute__ ((constructor))reg() | |
236 | { | |
237 | command_register((command_t) { | |
238 | list_conns, 'L', "list-conns", "list loaded configurations", | |
dacb75f5 | 239 | {"[--raw|--pretty]"}, |
51bdc1f3 MW |
240 | { |
241 | {"help", 'h', 0, "show usage information"}, | |
242 | {"raw", 'r', 0, "dump raw response message"}, | |
dacb75f5 | 243 | {"pretty", 'P', 0, "dump raw response message in pretty print"}, |
51bdc1f3 MW |
244 | } |
245 | }); | |
246 | } |