]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/swanctl/commands/list_conns.c
vici: Rebuild ruby gem on source file changes
[thirdparty/strongswan.git] / src / swanctl / commands / list_conns.c
CommitLineData
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 */
27static 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
42CALLBACK(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
60CALLBACK(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
79CALLBACK(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
97CALLBACK(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
a2875525
MW
138CALLBACK(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
51bdc1f3
MW
155CALLBACK(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
a2875525 160 return vici_parse_cb(res, conn_sn, NULL, conn_list, NULL);
51bdc1f3
MW
161}
162
163CALLBACK(list_cb, void,
dacb75f5 164 command_format_options_t *format, char *name, vici_res_t *res)
51bdc1f3 165{
dacb75f5 166 if (*format & COMMAND_FORMAT_RAW)
51bdc1f3 167 {
dacb75f5
AS
168 vici_dump(res, "list-conn event", *format & COMMAND_FORMAT_PRETTY,
169 stdout);
51bdc1f3
MW
170 }
171 else
172 {
173 if (vici_parse_cb(res, conns, NULL, NULL, NULL) != 0)
174 {
175 fprintf(stderr, "parsing conn event failed: %s\n", strerror(errno));
176 }
177 }
178}
179
180static int list_conns(vici_conn_t *conn)
181{
182 vici_req_t *req;
183 vici_res_t *res;
dacb75f5 184 command_format_options_t format = COMMAND_FORMAT_NONE;
51bdc1f3 185 char *arg;
67f9f09d 186 int ret;
51bdc1f3
MW
187
188 while (TRUE)
189 {
190 switch (command_getopt(&arg))
191 {
192 case 'h':
193 return command_usage(NULL);
dacb75f5
AS
194 case 'P':
195 format |= COMMAND_FORMAT_PRETTY;
196 /* fall through to raw */
51bdc1f3 197 case 'r':
dacb75f5 198 format |= COMMAND_FORMAT_RAW;
51bdc1f3
MW
199 continue;
200 case EOF:
201 break;
202 default:
203 return command_usage("invalid --list-conns option");
204 }
205 break;
206 }
dacb75f5 207 if (vici_register(conn, "list-conn", list_cb, &format) != 0)
51bdc1f3 208 {
67f9f09d 209 ret = errno;
51bdc1f3
MW
210 fprintf(stderr, "registering for connections failed: %s\n",
211 strerror(errno));
67f9f09d 212 return ret;
51bdc1f3
MW
213 }
214 req = vici_begin("list-conns");
215 res = vici_submit(req, conn);
216 if (!res)
217 {
67f9f09d 218 ret = errno;
51bdc1f3 219 fprintf(stderr, "list-conns request failed: %s\n", strerror(errno));
67f9f09d 220 return ret;
51bdc1f3 221 }
dacb75f5 222 if (format & COMMAND_FORMAT_RAW)
51bdc1f3 223 {
dacb75f5
AS
224 vici_dump(res, "list-conns reply", format & COMMAND_FORMAT_PRETTY,
225 stdout);
51bdc1f3
MW
226 }
227 vici_free_res(res);
228 return 0;
229}
230
231/**
232 * Register the command.
233 */
234static void __attribute__ ((constructor))reg()
235{
236 command_register((command_t) {
237 list_conns, 'L', "list-conns", "list loaded configurations",
dacb75f5 238 {"[--raw|--pretty]"},
51bdc1f3
MW
239 {
240 {"help", 'h', 0, "show usage information"},
241 {"raw", 'r', 0, "dump raw response message"},
dacb75f5 242 {"pretty", 'P', 0, "dump raw response message in pretty print"},
51bdc1f3
MW
243 }
244 });
245}