swanctl: Stop logging with Ctrl+C on Windows as well
[strongswan.git] / src / swanctl / commands / install.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 static int manage_policy(vici_conn_t *conn, char *label)
21 {
22 vici_req_t *req;
23 vici_res_t *res;
24 bool raw = FALSE;
25 char *arg, *child = NULL;
26 int ret = 0;
27
28 while (TRUE)
29 {
30 switch (command_getopt(&arg))
31 {
32 case 'h':
33 return command_usage(NULL);
34 case 'r':
35 raw = TRUE;
36 continue;
37 case 'c':
38 child = arg;
39 continue;
40 case EOF:
41 break;
42 default:
43 return command_usage("invalid --%s option", label);
44 }
45 break;
46 }
47 req = vici_begin(label);
48 if (child)
49 {
50 vici_add_key_valuef(req, "child", "%s", child);
51 }
52 res = vici_submit(req, conn);
53 if (!res)
54 {
55 fprintf(stderr, "%s request failed: %s\n", label, strerror(errno));
56 return errno;
57 }
58 if (raw)
59 {
60 puts(label);
61 vici_dump(res, " reply", stdout);
62 }
63 else
64 {
65 if (streq(vici_find_str(res, "no", "success"), "yes"))
66 {
67 printf("%s completed successfully\n", label);
68 }
69 else
70 {
71 fprintf(stderr, "%s failed: %s\n",
72 label, vici_find_str(res, "", "errmsg"));
73 ret = 1;
74 }
75 }
76 vici_free_res(res);
77 return ret;
78 }
79
80 static int uninstall(vici_conn_t *conn)
81 {
82 return manage_policy(conn, "uninstall");
83 }
84
85 static int install(vici_conn_t *conn)
86 {
87 return manage_policy(conn, "install");
88 }
89
90 /**
91 * Register the uninstall command.
92 */
93 static void __attribute__ ((constructor))reg_uninstall()
94 {
95 command_register((command_t) {
96 uninstall, 'u', "uninstall", "uninstall a trap or shunt policy",
97 {"--child <name> [--raw]"},
98 {
99 {"help", 'h', 0, "show usage information"},
100 {"child", 'c', 1, "CHILD_SA configuration to uninstall"},
101 {"raw", 'r', 0, "dump raw response message"},
102 }
103 });
104 }
105
106 /**
107 * Register install the command.
108 */
109 static void __attribute__ ((constructor))reg_install()
110 {
111 command_register((command_t) {
112 install, 'p', "install", "install a trap or shunt policy",
113 {"--child <name> [--raw]"},
114 {
115 {"help", 'h', 0, "show usage information"},
116 {"child", 'c', 1, "CHILD_SA configuration to install"},
117 {"raw", 'r', 0, "dump raw response message"},
118 }
119 });
120 }