2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
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>.
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
21 CALLBACK(log_cb
, void,
22 bool *raw
, char *name
, vici_res_t
*msg
)
26 vici_dump(msg
, "log", stdout
);
31 vici_find_str(msg
, " ", "group"),
32 vici_find_str(msg
, "", "msg"));
36 static int terminate(vici_conn_t
*conn
)
41 char *arg
, *child
= NULL
, *ike
= NULL
;
42 int ret
= 0, timeout
= 0, level
= 1, child_id
= 0, ike_id
= 0;
46 switch (command_getopt(&arg
))
49 return command_usage(NULL
);
74 return command_usage("invalid --terminate option");
79 if (vici_register(conn
, "control-log", log_cb
, &raw
) != 0)
81 fprintf(stderr
, "registering for log failed: %s\n", strerror(errno
));
84 req
= vici_begin("terminate");
87 vici_add_key_valuef(req
, "child", "%s", child
);
91 vici_add_key_valuef(req
, "ike", "%s", ike
);
95 vici_add_key_valuef(req
, "child-id", "%d", child_id
);
99 vici_add_key_valuef(req
, "ike-id", "%d", ike_id
);
103 vici_add_key_valuef(req
, "timeout", "%d", timeout
* 1000);
105 vici_add_key_valuef(req
, "loglevel", "%d", level
);
106 res
= vici_submit(req
, conn
);
109 fprintf(stderr
, "terminate request failed: %s\n", strerror(errno
));
114 vici_dump(res
, "terminate reply", stdout
);
118 if (streq(vici_find_str(res
, "no", "success"), "yes"))
120 printf("terminate completed successfully\n");
124 fprintf(stderr
, "terminate failed: %s\n",
125 vici_find_str(res
, "", "errmsg"));
134 * Register the command.
136 static void __attribute__ ((constructor
))reg()
138 command_register((command_t
) {
139 terminate
, 't', "terminate", "terminate a connection",
140 {"--child <name> | --ike <name | --child-id <id> | --ike-id <id>",
141 "[--timeout <s>] [--raw]"},
143 {"help", 'h', 0, "show usage information"},
144 {"child", 'c', 1, "terminate by CHILD_SA name"},
145 {"ike", 'i', 1, "terminate by IKE_SA name"},
146 {"child-id", 'C', 1, "terminate by CHILD_SA reqid"},
147 {"ike-id", 'I', 1, "terminate by IKE_SA unique identifier"},
148 {"timeout", 't', 1, "timeout in seconds before detaching"},
149 {"raw", 'r', 0, "dump raw response message"},
150 {"loglevel", 'l', 1, "verbosity of redirected log"},