vici: Match identity with wildcards against remote ID in redirect command
[strongswan.git] / src / swanctl / commands / initiate.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 initiate(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;
41 int ret = 0, timeout = 0, level = 1;
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 't':
59 timeout = atoi(arg);
60 continue;
61 case 'l':
62 level = atoi(arg);
63 continue;
64 case EOF:
65 break;
66 default:
67 return command_usage("invalid --initiate option");
68 }
69 break;
70 }
71
72 if (vici_register(conn, "control-log", log_cb, &format) != 0)
73 {
74 ret = errno;
75 fprintf(stderr, "registering for log failed: %s\n", strerror(errno));
76 return ret;
77 }
78 req = vici_begin("initiate");
79 if (child)
80 {
81 vici_add_key_valuef(req, "child", "%s", child);
82 }
83 if (timeout)
84 {
85 vici_add_key_valuef(req, "timeout", "%d", timeout * 1000);
86 }
87 vici_add_key_valuef(req, "loglevel", "%d", level);
88 res = vici_submit(req, conn);
89 if (!res)
90 {
91 ret = errno;
92 fprintf(stderr, "initiate request failed: %s\n", strerror(errno));
93 return ret;
94 }
95 if (format & COMMAND_FORMAT_RAW)
96 {
97 vici_dump(res, "initiate reply", format & COMMAND_FORMAT_PRETTY,
98 stdout);
99 }
100 else
101 {
102 if (streq(vici_find_str(res, "no", "success"), "yes"))
103 {
104 printf("initiate completed successfully\n");
105 }
106 else
107 {
108 fprintf(stderr, "initiate failed: %s\n",
109 vici_find_str(res, "", "errmsg"));
110 ret = 1;
111 }
112 }
113 vici_free_res(res);
114 return ret;
115 }
116
117 /**
118 * Register the command.
119 */
120 static void __attribute__ ((constructor))reg()
121 {
122 command_register((command_t) {
123 initiate, 'i', "initiate", "initiate a connection",
124 {"--child <name> [--timeout <s>] [--raw|--pretty]"},
125 {
126 {"help", 'h', 0, "show usage information"},
127 {"child", 'c', 1, "initate a CHILD_SA configuration"},
128 {"timeout", 't', 1, "timeout in seconds before detaching"},
129 {"raw", 'r', 0, "dump raw response message"},
130 {"pretty", 'P', 0, "dump raw response message in pretty print"},
131 {"loglevel", 'l', 1, "verbosity of redirected log"},
132 }
133 });
134 }