b6842e00df5c4c9aa2ebe86eed560b4c11321445
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 * Check if we should handle a key as a list of comma separated values
23 static bool is_list_key(char *key
)
37 for (i
= 0; i
< countof(keys
); i
++)
39 if (strcaseeq(keys
[i
], key
))
48 * Add a vici list from a comma separated string value
50 static void add_list_key(vici_req_t
*req
, char *key
, char *value
)
52 enumerator_t
*enumerator
;
55 vici_begin_list(req
, key
);
56 enumerator
= enumerator_create_token(value
, ",", " ");
57 while (enumerator
->enumerate(enumerator
, &token
))
59 vici_add_list_itemf(req
, "%s", token
);
61 enumerator
->destroy(enumerator
);
66 * Translate setting key/values from a section into vici key-values/lists
68 static void add_key_values(vici_req_t
*req
, settings_t
*cfg
, char *section
)
70 enumerator_t
*enumerator
;
73 enumerator
= cfg
->create_key_value_enumerator(cfg
, section
);
74 while (enumerator
->enumerate(enumerator
, &key
, &value
))
78 add_list_key(req
, key
, value
);
82 vici_add_key_valuef(req
, key
, "%s", value
);
85 enumerator
->destroy(enumerator
);
89 * Translate a settings section to a vici section
91 static void add_sections(vici_req_t
*req
, settings_t
*cfg
, char *section
)
93 enumerator_t
*enumerator
;
96 enumerator
= cfg
->create_section_enumerator(cfg
, section
);
97 while (enumerator
->enumerate(enumerator
, &name
))
99 vici_begin_section(req
, name
);
100 snprintf(buf
, sizeof(buf
), "%s.%s", section
, name
);
101 add_key_values(req
, cfg
, buf
);
102 add_sections(req
, cfg
, buf
);
103 vici_end_section(req
);
105 enumerator
->destroy(enumerator
);
109 * Load an IKE_SA config with CHILD_SA configs from a section
111 static bool load_conn(vici_conn_t
*conn
, settings_t
*cfg
,
112 char *section
, bool raw
)
119 snprintf(buf
, sizeof(buf
), "%s.%s", "connections", section
);
121 req
= vici_begin("load-conn");
123 vici_begin_section(req
, section
);
124 add_key_values(req
, cfg
, buf
);
125 add_sections(req
, cfg
, buf
);
126 vici_end_section(req
);
128 res
= vici_submit(req
, conn
);
131 fprintf(stderr
, "load-conn request failed: %s\n", strerror(errno
));
136 vici_dump(res
, "load-conn reply", stdout
);
138 else if (!streq(vici_find_str(res
, "no", "success"), "yes"))
140 fprintf(stderr
, "loading connection '%s' failed: %s\n",
141 section
, vici_find_str(res
, "", "errmsg"));
148 static int load_conns(vici_conn_t
*conn
)
151 u_int found
= 0, loaded
= 0;
153 enumerator_t
*enumerator
;
158 switch (command_getopt(&arg
))
161 return command_usage(NULL
);
168 return command_usage("invalid --load-conns option");
173 cfg
= settings_create(CONF_FILE
);
176 fprintf(stderr
, "parsing '%s' failed\n", CONF_FILE
);
180 enumerator
= cfg
->create_section_enumerator(cfg
, "connections");
181 while (enumerator
->enumerate(enumerator
, §ion
))
184 if (load_conn(conn
, cfg
, section
, raw
))
189 enumerator
->destroy(enumerator
);
199 printf("no connections found\n");
204 printf("successfully loaded %u connections\n", loaded
);
207 fprintf(stderr
, "loaded %u of %u connections, %u failed to load\n",
208 loaded
, found
, found
- loaded
);
213 * Register the command.
215 static void __attribute__ ((constructor
))reg()
217 command_register((command_t
) {
218 load_conns
, 'c', "load-conns", "(re-)load connection configuration",
221 {"help", 'h', 0, "show usage information"},
222 {"raw", 'r', 0, "dump raw response message"},