]> git.ipfire.org Git - thirdparty/strongswan.git/blob - src/swanctl/commands/initiate.c
Update copyright headers after acquisition by secunet
[thirdparty/strongswan.git] / src / swanctl / commands / initiate.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 *
4 * Copyright (C) secunet Security Networks AG
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "command.h"
18
19 #include <errno.h>
20
21 CALLBACK(log_cb, void,
22 command_format_options_t *format, char *name, vici_res_t *msg)
23 {
24 if (*format & COMMAND_FORMAT_RAW)
25 {
26 vici_dump(msg, "log", *format & COMMAND_FORMAT_PRETTY, stdout);
27 }
28 else
29 {
30 printf("[%s] %s\n",
31 vici_find_str(msg, " ", "group"),
32 vici_find_str(msg, "", "msg"));
33 }
34 }
35
36 static int initiate(vici_conn_t *conn)
37 {
38 vici_req_t *req;
39 vici_res_t *res;
40 command_format_options_t format = COMMAND_FORMAT_NONE;
41 char *arg, *child = NULL, *ike = NULL;
42 int ret = 0, timeout = 0, level = 1;
43
44 while (TRUE)
45 {
46 switch (command_getopt(&arg))
47 {
48 case 'h':
49 return command_usage(NULL);
50 case 'P':
51 format |= COMMAND_FORMAT_PRETTY;
52 /* fall through to raw */
53 case 'r':
54 format |= COMMAND_FORMAT_RAW;
55 continue;
56 case 'c':
57 child = arg;
58 continue;
59 case 'i':
60 ike = arg;
61 continue;
62 case 't':
63 timeout = atoi(arg);
64 continue;
65 case 'l':
66 level = atoi(arg);
67 continue;
68 case EOF:
69 break;
70 default:
71 return command_usage("invalid --initiate option");
72 }
73 break;
74 }
75
76 if (vici_register(conn, "control-log", log_cb, &format) != 0)
77 {
78 ret = errno;
79 fprintf(stderr, "registering for log failed: %s\n", strerror(errno));
80 return ret;
81 }
82 req = vici_begin("initiate");
83 if (child)
84 {
85 vici_add_key_valuef(req, "child", "%s", child);
86 }
87 if (ike)
88 {
89 vici_add_key_valuef(req, "ike", "%s", ike);
90 }
91 if (timeout)
92 {
93 vici_add_key_valuef(req, "timeout", "%d", timeout * 1000);
94 }
95 vici_add_key_valuef(req, "loglevel", "%d", level);
96 res = vici_submit(req, conn);
97 if (!res)
98 {
99 ret = errno;
100 fprintf(stderr, "initiate request failed: %s\n", strerror(errno));
101 return ret;
102 }
103 if (format & COMMAND_FORMAT_RAW)
104 {
105 vici_dump(res, "initiate reply", format & COMMAND_FORMAT_PRETTY,
106 stdout);
107 }
108 else
109 {
110 if (streq(vici_find_str(res, "no", "success"), "yes"))
111 {
112 printf("initiate completed successfully\n");
113 }
114 else
115 {
116 fprintf(stderr, "initiate failed: %s\n",
117 vici_find_str(res, "", "errmsg"));
118 ret = 1;
119 }
120 }
121 vici_free_res(res);
122 return ret;
123 }
124
125 /**
126 * Register the command.
127 */
128 static void __attribute__ ((constructor))reg()
129 {
130 command_register((command_t) {
131 initiate, 'i', "initiate", "initiate a connection",
132 {"[--child <name>] [--ike <name>] [--timeout <s>] [--raw|--pretty]"},
133 {
134 {"help", 'h', 0, "show usage information"},
135 {"child", 'c', 1, "initiate a CHILD_SA configuration"},
136 {"ike", 'i', 1, "initiate an IKE_SA, or name of child's parent"},
137 {"timeout", 't', 1, "timeout in seconds before detaching"},
138 {"raw", 'r', 0, "dump raw response message"},
139 {"pretty", 'P', 0, "dump raw response message in pretty print"},
140 {"loglevel", 'l', 1, "verbosity of redirected log"},
141 }
142 });
143 }