swanctl: Stop logging with Ctrl+C on Windows as well
[strongswan.git] / src / swanctl / commands / list_conns.c
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 }
106 if (streq(name, "local") || streq(name, "remote"))
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",
115 name, auth->get(auth, "class") ?: "unspecified");
116 if (auth->get(auth, "id"))
117 {
118 printf(" id: %s\n", auth->get(auth, "id"));
119 }
120 if (auth->get(auth, "groups"))
121 {
122 printf(" groups: %s\n", auth->get(auth, "groups"));
123 }
124 if (auth->get(auth, "certs"))
125 {
126 printf(" certs: %s\n", auth->get(auth, "certs"));
127 }
128 if (auth->get(auth, "cacerts"))
129 {
130 printf(" cacerts: %s\n", auth->get(auth, "cacerts"));
131 }
132 }
133 free_hashtable(auth);
134 }
135 return ret;
136 }
137
138 CALLBACK(conn_list, int,
139 hashtable_t *sa, vici_res_t *res, char *name, void *value, int len)
140 {
141 if (chunk_printable(chunk_create(value, len), NULL, ' '))
142 {
143 if (streq(name, "local_addrs"))
144 {
145 printf(" local: %.*s\n", len, value);
146 }
147 if (streq(name, "remote_addrs"))
148 {
149 printf(" remote: %.*s\n", len, value);
150 }
151 }
152 return 0;
153 }
154
155 CALLBACK(conns, int,
156 void *null, vici_res_t *res, char *name)
157 {
158 printf("%s: %s\n", name, vici_find_str(res, "", "%s.version", name));
159
160 return vici_parse_cb(res, conn_sn, NULL, conn_list, NULL);
161 }
162
163 CALLBACK(list_cb, void,
164 bool *raw, char *name, vici_res_t *res)
165 {
166 if (*raw)
167 {
168 vici_dump(res, "list-conn event", stdout);
169 }
170 else
171 {
172 if (vici_parse_cb(res, conns, NULL, NULL, NULL) != 0)
173 {
174 fprintf(stderr, "parsing conn event failed: %s\n", strerror(errno));
175 }
176 }
177 }
178
179 static int list_conns(vici_conn_t *conn)
180 {
181 vici_req_t *req;
182 vici_res_t *res;
183 bool raw = FALSE;
184 char *arg;
185
186 while (TRUE)
187 {
188 switch (command_getopt(&arg))
189 {
190 case 'h':
191 return command_usage(NULL);
192 case 'r':
193 raw = TRUE;
194 continue;
195 case EOF:
196 break;
197 default:
198 return command_usage("invalid --list-conns option");
199 }
200 break;
201 }
202 if (vici_register(conn, "list-conn", list_cb, &raw) != 0)
203 {
204 fprintf(stderr, "registering for connections failed: %s\n",
205 strerror(errno));
206 return errno;
207 }
208 req = vici_begin("list-conns");
209 res = vici_submit(req, conn);
210 if (!res)
211 {
212 fprintf(stderr, "list-conns request failed: %s\n", strerror(errno));
213 return errno;
214 }
215 if (raw)
216 {
217 vici_dump(res, "list-conns reply", stdout);
218 }
219 vici_free_res(res);
220 return 0;
221 }
222
223 /**
224 * Register the command.
225 */
226 static void __attribute__ ((constructor))reg()
227 {
228 command_register((command_t) {
229 list_conns, 'L', "list-conns", "list loaded configurations",
230 {"[--raw]"},
231 {
232 {"help", 'h', 0, "show usage information"},
233 {"raw", 'r', 0, "dump raw response message"},
234 }
235 });
236 }