swanctl: Add a --load-all command, performing --load-{creds,pools,conns}
[strongswan.git] / src / swanctl / commands / load_all.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 #define _GNU_SOURCE
17 #include <stdio.h>
18 #include <errno.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21
22 #include "command.h"
23 #include "swanctl.h"
24 #include "load_creds.h"
25 #include "load_pools.h"
26 #include "load_conns.h"
27
28 static int load_all(vici_conn_t *conn)
29 {
30 bool clear = FALSE, noprompt = FALSE;
31 command_format_options_t format = COMMAND_FORMAT_NONE;
32 settings_t *cfg;
33 int ret = 0;
34 char *arg;
35
36 while (TRUE)
37 {
38 switch (command_getopt(&arg))
39 {
40 case 'h':
41 return command_usage(NULL);
42 case 'c':
43 clear = TRUE;
44 continue;
45 case 'n':
46 noprompt = TRUE;
47 continue;
48 case 'P':
49 format |= COMMAND_FORMAT_PRETTY;
50 /* fall through to raw */
51 case 'r':
52 format |= COMMAND_FORMAT_RAW;
53 continue;
54 case EOF:
55 break;
56 default:
57 return command_usage("invalid --load-all option");
58 }
59 break;
60 }
61
62 cfg = settings_create(SWANCTL_CONF);
63 if (!cfg)
64 {
65 fprintf(stderr, "parsing '%s' failed\n", SWANCTL_CONF);
66 return EINVAL;
67 }
68
69 if (ret == 0)
70 {
71 ret = load_creds_cfg(conn, format, cfg, clear, noprompt);
72 }
73 if (ret == 0)
74 {
75 ret = load_pools_cfg(conn, format, cfg);
76 }
77 if (ret == 0)
78 {
79 ret = load_conns_cfg(conn, format, cfg);
80 }
81
82 cfg->destroy(cfg);
83
84 return ret;
85 }
86
87 /**
88 * Register the command.
89 */
90 static void __attribute__ ((constructor))reg()
91 {
92 command_register((command_t) {
93 load_all, 'q', "load-all", "load credentials, pools and connections",
94 {"[--raw|--pretty] [--clear] [--noprompt]"},
95 {
96 {"help", 'h', 0, "show usage information"},
97 {"clear", 'c', 0, "clear previously loaded credentials"},
98 {"noprompt", 'n', 0, "do not prompt for passwords"},
99 {"raw", 'r', 0, "dump raw response message"},
100 {"pretty", 'P', 0, "dump raw response message in pretty print"},
101 }
102 });
103 }