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