swanctl: Be more verbose while loading connections and credentials
[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(conns, int,
139 void *null, vici_res_t *res, char *name)
140 {
141 printf("%s: %s\n", name, vici_find_str(res, "", "%s.version", name));
142
143 return vici_parse_cb(res, conn_sn, NULL, NULL, NULL);
144 }
145
146 CALLBACK(list_cb, void,
147 bool *raw, char *name, vici_res_t *res)
148 {
149 if (*raw)
150 {
151 vici_dump(res, "list-conn event", stdout);
152 }
153 else
154 {
155 if (vici_parse_cb(res, conns, NULL, NULL, NULL) != 0)
156 {
157 fprintf(stderr, "parsing conn event failed: %s\n", strerror(errno));
158 }
159 }
160 }
161
162 static int list_conns(vici_conn_t *conn)
163 {
164 vici_req_t *req;
165 vici_res_t *res;
166 bool raw = FALSE;
167 char *arg;
168
169 while (TRUE)
170 {
171 switch (command_getopt(&arg))
172 {
173 case 'h':
174 return command_usage(NULL);
175 case 'r':
176 raw = TRUE;
177 continue;
178 case EOF:
179 break;
180 default:
181 return command_usage("invalid --list-conns option");
182 }
183 break;
184 }
185 if (vici_register(conn, "list-conn", list_cb, &raw) != 0)
186 {
187 fprintf(stderr, "registering for connections failed: %s\n",
188 strerror(errno));
189 return errno;
190 }
191 req = vici_begin("list-conns");
192 res = vici_submit(req, conn);
193 if (!res)
194 {
195 fprintf(stderr, "list-conns request failed: %s\n", strerror(errno));
196 return errno;
197 }
198 if (raw)
199 {
200 vici_dump(res, "list-conns reply", stdout);
201 }
202 vici_free_res(res);
203 return 0;
204 }
205
206 /**
207 * Register the command.
208 */
209 static void __attribute__ ((constructor))reg()
210 {
211 command_register((command_t) {
212 list_conns, 'L', "list-conns", "list loaded configurations",
213 {"[--raw]"},
214 {
215 {"help", 'h', 0, "show usage information"},
216 {"raw", 'r', 0, "dump raw response message"},
217 }
218 });
219 }