Annotation of embedaddon/strongswan/src/swanctl/commands/terminate.c, revision 1.1.1.1
1.1 misho 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: bool force = FALSE;
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 'f':
60: force = TRUE;
61: continue;
62: case 'i':
63: ike = arg;
64: continue;
65: case 'C':
66: child_id = atoi(arg);
67: continue;
68: case 'I':
69: ike_id = atoi(arg);
70: continue;
71: case 't':
72: timeout = atoi(arg);
73: continue;
74: case 'l':
75: level = atoi(arg);
76: continue;
77: case EOF:
78: break;
79: default:
80: return command_usage("invalid --terminate option");
81: }
82: break;
83: }
84:
85: if (vici_register(conn, "control-log", log_cb, &format) != 0)
86: {
87: ret = errno;
88: fprintf(stderr, "registering for log failed: %s\n", strerror(errno));
89: return ret;
90: }
91: req = vici_begin("terminate");
92: if (child)
93: {
94: vici_add_key_valuef(req, "child", "%s", child);
95: }
96: if (ike)
97: {
98: vici_add_key_valuef(req, "ike", "%s", ike);
99: }
100: if (child_id)
101: {
102: vici_add_key_valuef(req, "child-id", "%d", child_id);
103: }
104: if (ike_id)
105: {
106: vici_add_key_valuef(req, "ike-id", "%d", ike_id);
107: }
108: if (force)
109: {
110: vici_add_key_valuef(req, "force", "yes");
111: }
112: if (timeout)
113: {
114: vici_add_key_valuef(req, "timeout", "%d", timeout * 1000);
115: }
116: vici_add_key_valuef(req, "loglevel", "%d", level);
117: res = vici_submit(req, conn);
118: if (!res)
119: {
120: ret = errno;
121: fprintf(stderr, "terminate request failed: %s\n", strerror(errno));
122: return ret;
123: }
124: if (format & COMMAND_FORMAT_RAW)
125: {
126: vici_dump(res, "terminate reply", format & COMMAND_FORMAT_PRETTY,
127: stdout);
128: }
129: else
130: {
131: if (streq(vici_find_str(res, "no", "success"), "yes"))
132: {
133: printf("terminate completed successfully\n");
134: }
135: else
136: {
137: fprintf(stderr, "terminate failed: %s\n",
138: vici_find_str(res, "", "errmsg"));
139: ret = 1;
140: }
141: }
142: vici_free_res(res);
143: return ret;
144: }
145:
146: /**
147: * Register the command.
148: */
149: static void __attribute__ ((constructor))reg()
150: {
151: command_register((command_t) {
152: terminate, 't', "terminate", "terminate a connection",
153: {"--child <name> | --ike <name> | --child-id <id> | --ike-id <id>",
154: "[--timeout <s>] [--raw|--pretty]"},
155: {
156: {"help", 'h', 0, "show usage information"},
157: {"child", 'c', 1, "terminate by CHILD_SA name"},
158: {"ike", 'i', 1, "terminate by IKE_SA name"},
159: {"child-id", 'C', 1, "terminate by CHILD_SA reqid"},
160: {"ike-id", 'I', 1, "terminate by IKE_SA unique identifier"},
161: {"force", 'f', 0, "terminate IKE_SA without waiting, unless timeout is set"},
162: {"timeout", 't', 1, "timeout in seconds before detaching"},
163: {"raw", 'r', 0, "dump raw response message"},
164: {"pretty", 'P', 0, "dump raw response message in pretty print"},
165: {"loglevel", 'l', 1, "verbosity of redirected log"},
166: }
167: });
168: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>