vici: Match identity with wildcards against remote ID in redirect command
[strongswan.git] / src / swanctl / commands / terminate.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 #include "command.h"
17
18 #include <errno.h>
19
20 CALLBACK(log_cb, void,
21 command_format_options_t *format, char *name, vici_res_t *msg)
22 {
23 if (*format & COMMAND_FORMAT_RAW)
24 {
25 vici_dump(msg, "log", *format & COMMAND_FORMAT_PRETTY, stdout);
26 }
27 else
28 {
29 printf("[%s] %s\n",
30 vici_find_str(msg, " ", "group"),
31 vici_find_str(msg, "", "msg"));
32 }
33 }
34
35 static int terminate(vici_conn_t *conn)
36 {
37 vici_req_t *req;
38 vici_res_t *res;
39 command_format_options_t format = COMMAND_FORMAT_NONE;
40 char *arg, *child = NULL, *ike = NULL;
41 int ret = 0, timeout = 0, level = 1, child_id = 0, ike_id = 0;
42
43 while (TRUE)
44 {
45 switch (command_getopt(&arg))
46 {
47 case 'h':
48 return command_usage(NULL);
49 case 'P':
50 format |= COMMAND_FORMAT_PRETTY;
51 /* fall through to raw */
52 case 'r':
53 format |= COMMAND_FORMAT_RAW;
54 continue;
55 case 'c':
56 child = arg;
57 continue;
58 case 'i':
59 ike = arg;
60 continue;
61 case 'C':
62 child_id = atoi(arg);
63 continue;
64 case 'I':
65 ike_id = atoi(arg);
66 continue;
67 case 't':
68 timeout = atoi(arg);
69 continue;
70 case 'l':
71 level = atoi(arg);
72 continue;
73 case EOF:
74 break;
75 default:
76 return command_usage("invalid --terminate option");
77 }
78 break;
79 }
80
81 if (vici_register(conn, "control-log", log_cb, &format) != 0)
82 {
83 ret = errno;
84 fprintf(stderr, "registering for log failed: %s\n", strerror(errno));
85 return ret;
86 }
87 req = vici_begin("terminate");
88 if (child)
89 {
90 vici_add_key_valuef(req, "child", "%s", child);
91 }
92 if (ike)
93 {
94 vici_add_key_valuef(req, "ike", "%s", ike);
95 }
96 if (child_id)
97 {
98 vici_add_key_valuef(req, "child-id", "%d", child_id);
99 }
100 if (ike_id)
101 {
102 vici_add_key_valuef(req, "ike-id", "%d", ike_id);
103 }
104 if (timeout)
105 {
106 vici_add_key_valuef(req, "timeout", "%d", timeout * 1000);
107 }
108 vici_add_key_valuef(req, "loglevel", "%d", level);
109 res = vici_submit(req, conn);
110 if (!res)
111 {
112 ret = errno;
113 fprintf(stderr, "terminate request failed: %s\n", strerror(errno));
114 return ret;
115 }
116 if (format & COMMAND_FORMAT_RAW)
117 {
118 vici_dump(res, "terminate reply", format & COMMAND_FORMAT_PRETTY,
119 stdout);
120 }
121 else
122 {
123 if (streq(vici_find_str(res, "no", "success"), "yes"))
124 {
125 printf("terminate completed successfully\n");
126 }
127 else
128 {
129 fprintf(stderr, "terminate failed: %s\n",
130 vici_find_str(res, "", "errmsg"));
131 ret = 1;
132 }
133 }
134 vici_free_res(res);
135 return ret;
136 }
137
138 /**
139 * Register the command.
140 */
141 static void __attribute__ ((constructor))reg()
142 {
143 command_register((command_t) {
144 terminate, 't', "terminate", "terminate a connection",
145 {"--child <name> | --ike <name | --child-id <id> | --ike-id <id>",
146 "[--timeout <s>] [--raw|--pretty]"},
147 {
148 {"help", 'h', 0, "show usage information"},
149 {"child", 'c', 1, "terminate by CHILD_SA name"},
150 {"ike", 'i', 1, "terminate by IKE_SA name"},
151 {"child-id", 'C', 1, "terminate by CHILD_SA reqid"},
152 {"ike-id", 'I', 1, "terminate by IKE_SA unique identifier"},
153 {"timeout", 't', 1, "timeout in seconds before detaching"},
154 {"raw", 'r', 0, "dump raw response message"},
155 {"pretty", 'P', 0, "dump raw response message in pretty print"},
156 {"loglevel", 'l', 1, "verbosity of redirected log"},
157 }
158 });
159 }