]>
Commit | Line | Data |
---|---|---|
c2cc9f0a | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
c2cc9f0a | 8 | * |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
c2cc9f0a | 22 | */ |
12a50cc6 | 23 | #define _GNU_SOURCE |
2e6e3feb | 24 | #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */ |
c2cc9f0a | 25 | #include <stdio.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <unistd.h> | |
29 | #include <errno.h> | |
63376d7d | 30 | #include <fcntl.h> |
a84b9932 | 31 | #include <ctype.h> |
2e6e3feb | 32 | #include <inttypes.h> /* Required for PRIu64 to work. */ |
a84b9932 | 33 | #include <signal.h> |
63376d7d | 34 | #include <sys/stat.h> |
c2cc9f0a | 35 | #include <sys/types.h> |
36 | #include <sys/param.h> | |
37 | #include <sys/utsname.h> | |
38 | #include <arpa/inet.h> | |
39 | #include <netinet/in.h> | |
40 | #include <net/if.h> | |
67702c21 | 41 | #include <time.h> |
e1daebd9 | 42 | #include <dirent.h> |
64c57ea1 | 43 | #include <syslog.h> |
c2cc9f0a | 44 | |
d8e48992 | 45 | #include "bdev.h" |
b2718c72 | 46 | #include "parse.h" |
6ff05e18 | 47 | #include "config.h" |
525f0002 | 48 | #include "confile.h" |
26c39028 | 49 | #include "utils.h" |
f2363e38 ÇO |
50 | #include "log.h" |
51 | #include "conf.h" | |
72d0e1cb | 52 | #include "network.h" |
58e0f57d | 53 | #include "lxcseccomp.h" |
36eb9bde | 54 | |
576400e5 | 55 | #if HAVE_IFADDRS_H |
56 | #include <ifaddrs.h> | |
57 | #else | |
58 | #include <../include/ifaddrs.h> | |
59 | #endif | |
60 | ||
6ff05e18 SG |
61 | #if HAVE_SYS_PERSONALITY_H |
62 | #include <sys/personality.h> | |
63 | #endif | |
64 | ||
36eb9bde | 65 | lxc_log_define(lxc_confile, lxc); |
576f946d | 66 | |
12a50cc6 DE |
67 | static int config_personality(const char *, const char *, struct lxc_conf *); |
68 | static int config_pts(const char *, const char *, struct lxc_conf *); | |
69 | static int config_tty(const char *, const char *, struct lxc_conf *); | |
70 | static int config_ttydir(const char *, const char *, struct lxc_conf *); | |
7e0e1d94 | 71 | static int config_kmsg(const char *, const char *, struct lxc_conf *); |
fe4de9a6 | 72 | static int config_lsm_aa_profile(const char *, const char *, struct lxc_conf *); |
7aff4f43 | 73 | static int config_lsm_aa_incomplete(const char *, const char *, struct lxc_conf *); |
fe4de9a6 | 74 | static int config_lsm_se_context(const char *, const char *, struct lxc_conf *); |
12a50cc6 | 75 | static int config_cgroup(const char *, const char *, struct lxc_conf *); |
f6d3e3e4 | 76 | static int config_idmap(const char *, const char *, struct lxc_conf *); |
4a85ce2a SH |
77 | static int config_loglevel(const char *, const char *, struct lxc_conf *); |
78 | static int config_logfile(const char *, const char *, struct lxc_conf *); | |
12a50cc6 | 79 | static int config_mount(const char *, const char *, struct lxc_conf *); |
70f55bfd NC |
80 | static int config_mount_auto(const char *, const char *, struct lxc_conf *); |
81 | static int config_fstab(const char *, const char *, struct lxc_conf *); | |
12a50cc6 DE |
82 | static int config_rootfs(const char *, const char *, struct lxc_conf *); |
83 | static int config_rootfs_mount(const char *, const char *, struct lxc_conf *); | |
a17b1e65 | 84 | static int config_rootfs_options(const char *, const char *, struct lxc_conf *); |
327a1e78 | 85 | static int config_rootfs_backend(const char *, const char *, struct lxc_conf *); |
12a50cc6 DE |
86 | static int config_pivotdir(const char *, const char *, struct lxc_conf *); |
87 | static int config_utsname(const char *, const char *, struct lxc_conf *); | |
88 | static int config_hook(const char *, const char *, struct lxc_conf *lxc_conf); | |
6b0d5538 | 89 | static int config_network(const char *, const char *, struct lxc_conf *); |
12a50cc6 DE |
90 | static int config_network_type(const char *, const char *, struct lxc_conf *); |
91 | static int config_network_flags(const char *, const char *, struct lxc_conf *); | |
92 | static int config_network_link(const char *, const char *, struct lxc_conf *); | |
93 | static int config_network_name(const char *, const char *, struct lxc_conf *); | |
94 | static int config_network_veth_pair(const char *, const char *, struct lxc_conf *); | |
95 | static int config_network_macvlan_mode(const char *, const char *, struct lxc_conf *); | |
96 | static int config_network_hwaddr(const char *, const char *, struct lxc_conf *); | |
97 | static int config_network_vlan_id(const char *, const char *, struct lxc_conf *); | |
98 | static int config_network_mtu(const char *, const char *, struct lxc_conf *); | |
99 | static int config_network_ipv4(const char *, const char *, struct lxc_conf *); | |
100 | static int config_network_ipv4_gateway(const char *, const char *, struct lxc_conf *); | |
8fc8295a DE |
101 | static int config_network_script_up(const char *, const char *, struct lxc_conf *); |
102 | static int config_network_script_down(const char *, const char *, struct lxc_conf *); | |
12a50cc6 DE |
103 | static int config_network_ipv6(const char *, const char *, struct lxc_conf *); |
104 | static int config_network_ipv6_gateway(const char *, const char *, struct lxc_conf *); | |
105 | static int config_cap_drop(const char *, const char *, struct lxc_conf *); | |
1fb86a7c | 106 | static int config_cap_keep(const char *, const char *, struct lxc_conf *); |
12a50cc6 | 107 | static int config_console(const char *, const char *, struct lxc_conf *); |
96f15ca1 | 108 | static int config_console_logfile(const char *, const char *, struct lxc_conf *); |
12a50cc6 DE |
109 | static int config_seccomp(const char *, const char *, struct lxc_conf *); |
110 | static int config_includefile(const char *, const char *, struct lxc_conf *); | |
111 | static int config_network_nic(const char *, const char *, struct lxc_conf *); | |
112 | static int config_autodev(const char *, const char *, struct lxc_conf *); | |
f0f1d8c0 | 113 | static int config_haltsignal(const char *, const char *, struct lxc_conf *); |
dd267776 | 114 | static int config_rebootsignal(const char *, const char *, struct lxc_conf *); |
a84b9932 | 115 | static int config_stopsignal(const char *, const char *, struct lxc_conf *); |
ee1e7aa0 | 116 | static int config_start(const char *, const char *, struct lxc_conf *); |
64c57ea1 | 117 | static int config_syslog(const char *, const char *, struct lxc_conf *); |
a8dfe4e0 | 118 | static int config_monitor(const char *, const char *, struct lxc_conf *); |
ee1e7aa0 | 119 | static int config_group(const char *, const char *, struct lxc_conf *); |
7c661726 | 120 | static int config_environment(const char *, const char *, struct lxc_conf *); |
67c660d0 | 121 | static int config_init_cmd(const char *, const char *, struct lxc_conf *); |
72bb04e4 PT |
122 | static int config_init_uid(const char *, const char *, struct lxc_conf *); |
123 | static int config_init_gid(const char *, const char *, struct lxc_conf *); | |
8796becf | 124 | static int config_ephemeral(const char *, const char *, struct lxc_conf *); |
5a46f283 | 125 | static int config_no_new_privs(const char *, const char *, struct lxc_conf *); |
c6d09e15 | 126 | static int config_limit(const char *, const char *, struct lxc_conf *); |
c2cc9f0a | 127 | |
72d0e1cb | 128 | static struct lxc_config_t config[] = { |
576f946d | 129 | |
cccc74b5 | 130 | { "lxc.arch", config_personality }, |
e892973e DL |
131 | { "lxc.pts", config_pts }, |
132 | { "lxc.tty", config_tty }, | |
7c6ef2a2 | 133 | { "lxc.devttydir", config_ttydir }, |
7e0e1d94 | 134 | { "lxc.kmsg", config_kmsg }, |
fe4de9a6 | 135 | { "lxc.aa_profile", config_lsm_aa_profile }, |
7aff4f43 | 136 | { "lxc.aa_allow_incomplete", config_lsm_aa_incomplete }, |
fe4de9a6 | 137 | { "lxc.se_context", config_lsm_se_context }, |
e892973e | 138 | { "lxc.cgroup", config_cgroup }, |
f6d3e3e4 | 139 | { "lxc.id_map", config_idmap }, |
4a85ce2a SH |
140 | { "lxc.loglevel", config_loglevel }, |
141 | { "lxc.logfile", config_logfile }, | |
70f55bfd NC |
142 | { "lxc.mount.entry", config_mount }, |
143 | { "lxc.mount.auto", config_mount_auto }, | |
144 | { "lxc.mount", config_fstab }, | |
23b7ea69 | 145 | { "lxc.rootfs.mount", config_rootfs_mount }, |
a17b1e65 | 146 | { "lxc.rootfs.options", config_rootfs_options }, |
327a1e78 | 147 | { "lxc.rootfs.backend", config_rootfs_backend }, |
e892973e | 148 | { "lxc.rootfs", config_rootfs }, |
bf601689 | 149 | { "lxc.pivotdir", config_pivotdir }, |
e892973e | 150 | { "lxc.utsname", config_utsname }, |
26ddeedd | 151 | { "lxc.hook.pre-start", config_hook }, |
12a50cc6 | 152 | { "lxc.hook.pre-mount", config_hook }, |
26ddeedd | 153 | { "lxc.hook.mount", config_hook }, |
f7bee6c6 | 154 | { "lxc.hook.autodev", config_hook }, |
26ddeedd | 155 | { "lxc.hook.start", config_hook }, |
52492063 | 156 | { "lxc.hook.stop", config_hook }, |
26ddeedd | 157 | { "lxc.hook.post-stop", config_hook }, |
148e91f5 | 158 | { "lxc.hook.clone", config_hook }, |
37cf711b | 159 | { "lxc.hook.destroy", config_hook }, |
6b0d5538 | 160 | { "lxc.hook", config_hook }, |
e892973e DL |
161 | { "lxc.network.type", config_network_type }, |
162 | { "lxc.network.flags", config_network_flags }, | |
163 | { "lxc.network.link", config_network_link }, | |
164 | { "lxc.network.name", config_network_name }, | |
165 | { "lxc.network.macvlan.mode", config_network_macvlan_mode }, | |
166 | { "lxc.network.veth.pair", config_network_veth_pair }, | |
8fc8295a DE |
167 | { "lxc.network.script.up", config_network_script_up }, |
168 | { "lxc.network.script.down", config_network_script_down }, | |
e892973e DL |
169 | { "lxc.network.hwaddr", config_network_hwaddr }, |
170 | { "lxc.network.mtu", config_network_mtu }, | |
171 | { "lxc.network.vlan.id", config_network_vlan_id }, | |
f8fee0e2 | 172 | { "lxc.network.ipv4.gateway", config_network_ipv4_gateway }, |
e892973e | 173 | { "lxc.network.ipv4", config_network_ipv4 }, |
f8fee0e2 | 174 | { "lxc.network.ipv6.gateway", config_network_ipv6_gateway }, |
e892973e | 175 | { "lxc.network.ipv6", config_network_ipv6 }, |
72d0e1cb SG |
176 | /* config_network_nic must come after all other 'lxc.network.*' entries */ |
177 | { "lxc.network.", config_network_nic }, | |
6b0d5538 | 178 | { "lxc.network", config_network }, |
81810dd1 | 179 | { "lxc.cap.drop", config_cap_drop }, |
1fb86a7c | 180 | { "lxc.cap.keep", config_cap_keep }, |
96f15ca1 | 181 | { "lxc.console.logfile", config_console_logfile }, |
63376d7d | 182 | { "lxc.console", config_console }, |
8f2c3a70 | 183 | { "lxc.seccomp", config_seccomp }, |
09ad6246 | 184 | { "lxc.include", config_includefile }, |
c6883f38 | 185 | { "lxc.autodev", config_autodev }, |
f0f1d8c0 | 186 | { "lxc.haltsignal", config_haltsignal }, |
dd267776 | 187 | { "lxc.rebootsignal", config_rebootsignal }, |
a84b9932 | 188 | { "lxc.stopsignal", config_stopsignal }, |
ee1e7aa0 SG |
189 | { "lxc.start.auto", config_start }, |
190 | { "lxc.start.delay", config_start }, | |
191 | { "lxc.start.order", config_start }, | |
a8dfe4e0 | 192 | { "lxc.monitor.unshare", config_monitor }, |
ee1e7aa0 | 193 | { "lxc.group", config_group }, |
7c661726 | 194 | { "lxc.environment", config_environment }, |
67c660d0 | 195 | { "lxc.init_cmd", config_init_cmd }, |
72bb04e4 PT |
196 | { "lxc.init_uid", config_init_uid }, |
197 | { "lxc.init_gid", config_init_gid }, | |
8796becf | 198 | { "lxc.ephemeral", config_ephemeral }, |
64c57ea1 | 199 | { "lxc.syslog", config_syslog }, |
5a46f283 | 200 | { "lxc.no_new_privs", config_no_new_privs }, |
c6d09e15 | 201 | { "lxc.limit", config_limit }, |
a84b9932 AV |
202 | }; |
203 | ||
204 | struct signame { | |
205 | int num; | |
74a3920a | 206 | const char *name; |
a84b9932 AV |
207 | }; |
208 | ||
74a3920a | 209 | static const struct signame signames[] = { |
a84b9932 AV |
210 | { SIGHUP, "HUP" }, |
211 | { SIGINT, "INT" }, | |
212 | { SIGQUIT, "QUIT" }, | |
213 | { SIGILL, "ILL" }, | |
214 | { SIGABRT, "ABRT" }, | |
215 | { SIGFPE, "FPE" }, | |
216 | { SIGKILL, "KILL" }, | |
217 | { SIGSEGV, "SEGV" }, | |
218 | { SIGPIPE, "PIPE" }, | |
219 | { SIGALRM, "ALRM" }, | |
220 | { SIGTERM, "TERM" }, | |
221 | { SIGUSR1, "USR1" }, | |
222 | { SIGUSR2, "USR2" }, | |
223 | { SIGCHLD, "CHLD" }, | |
224 | { SIGCONT, "CONT" }, | |
225 | { SIGSTOP, "STOP" }, | |
226 | { SIGTSTP, "TSTP" }, | |
227 | { SIGTTIN, "TTIN" }, | |
228 | { SIGTTOU, "TTOU" }, | |
89dfc302 SY |
229 | #ifdef SIGTRAP |
230 | { SIGTRAP, "TRAP" }, | |
231 | #endif | |
232 | #ifdef SIGIOT | |
233 | { SIGIOT, "IOT" }, | |
234 | #endif | |
235 | #ifdef SIGEMT | |
236 | { SIGEMT, "EMT" }, | |
237 | #endif | |
238 | #ifdef SIGBUS | |
239 | { SIGBUS, "BUS" }, | |
240 | #endif | |
241 | #ifdef SIGSTKFLT | |
242 | { SIGSTKFLT, "STKFLT" }, | |
243 | #endif | |
244 | #ifdef SIGCLD | |
245 | { SIGCLD, "CLD" }, | |
246 | #endif | |
247 | #ifdef SIGURG | |
248 | { SIGURG, "URG" }, | |
249 | #endif | |
250 | #ifdef SIGXCPU | |
251 | { SIGXCPU, "XCPU" }, | |
252 | #endif | |
253 | #ifdef SIGXFSZ | |
254 | { SIGXFSZ, "XFSZ" }, | |
255 | #endif | |
256 | #ifdef SIGVTALRM | |
257 | { SIGVTALRM, "VTALRM" }, | |
258 | #endif | |
259 | #ifdef SIGPROF | |
260 | { SIGPROF, "PROF" }, | |
261 | #endif | |
262 | #ifdef SIGWINCH | |
263 | { SIGWINCH, "WINCH" }, | |
264 | #endif | |
265 | #ifdef SIGIO | |
266 | { SIGIO, "IO" }, | |
267 | #endif | |
268 | #ifdef SIGPOLL | |
269 | { SIGPOLL, "POLL" }, | |
270 | #endif | |
271 | #ifdef SIGINFO | |
272 | { SIGINFO, "INFO" }, | |
273 | #endif | |
274 | #ifdef SIGLOST | |
275 | { SIGLOST, "LOST" }, | |
276 | #endif | |
277 | #ifdef SIGPWR | |
278 | { SIGPWR, "PWR" }, | |
279 | #endif | |
280 | #ifdef SIGUNUSED | |
281 | { SIGUNUSED, "UNUSED" }, | |
282 | #endif | |
283 | #ifdef SIGSYS | |
284 | { SIGSYS, "SYS" }, | |
285 | #endif | |
c2cc9f0a | 286 | }; |
287 | ||
72d0e1cb | 288 | static const size_t config_size = sizeof(config)/sizeof(struct lxc_config_t); |
c2cc9f0a | 289 | |
72d0e1cb | 290 | extern struct lxc_config_t *lxc_getconfig(const char *key) |
c2cc9f0a | 291 | { |
84760c11 | 292 | size_t i; |
c2cc9f0a | 293 | |
294 | for (i = 0; i < config_size; i++) | |
a871ff6b | 295 | if (!strncmp(config[i].name, key, |
c2cc9f0a | 296 | strlen(config[i].name))) |
297 | return &config[i]; | |
298 | return NULL; | |
299 | } | |
300 | ||
72d0e1cb SG |
301 | #define strprint(str, inlen, ...) \ |
302 | do { \ | |
303 | len = snprintf(str, inlen, ##__VA_ARGS__); \ | |
304 | if (len < 0) { SYSERROR("snprintf"); return -1; }; \ | |
305 | fulllen += len; \ | |
306 | if (inlen > 0) { \ | |
307 | if (str) str += len; \ | |
308 | inlen -= len; \ | |
309 | if (inlen < 0) inlen = 0; \ | |
310 | } \ | |
311 | } while (0); | |
312 | ||
313 | int lxc_listconfigs(char *retv, int inlen) | |
314 | { | |
84760c11 | 315 | size_t i; |
316 | int fulllen = 0, len; | |
72d0e1cb SG |
317 | |
318 | if (!retv) | |
319 | inlen = 0; | |
320 | else | |
321 | memset(retv, 0, inlen); | |
322 | for (i = 0; i < config_size; i++) { | |
323 | char *s = config[i].name; | |
324 | if (s[strlen(s)-1] == '.') | |
325 | continue; | |
326 | strprint(retv, inlen, "%s\n", s); | |
327 | } | |
328 | return fulllen; | |
329 | } | |
330 | ||
fe8d7be7 CB |
331 | static inline bool config_value_empty(const char *value) |
332 | { | |
333 | if (value && strlen(value) > 0) | |
334 | return false; | |
335 | ||
336 | return true; | |
337 | } | |
338 | ||
6d03d92a DE |
339 | static int config_string_item(char **conf_item, const char *value) |
340 | { | |
341 | char *new_value; | |
342 | ||
e70b9db5 | 343 | if (config_value_empty(value)) { |
f10fad2f | 344 | free(*conf_item); |
d6eca240 | 345 | *conf_item = NULL; |
6d03d92a | 346 | return 0; |
d6eca240 | 347 | } |
6d03d92a DE |
348 | |
349 | new_value = strdup(value); | |
350 | if (!new_value) { | |
e70b9db5 | 351 | SYSERROR("failed to duplicate string \"%s\"", value); |
6d03d92a DE |
352 | return -1; |
353 | } | |
354 | ||
f10fad2f | 355 | free(*conf_item); |
6d03d92a DE |
356 | *conf_item = new_value; |
357 | return 0; | |
358 | } | |
359 | ||
360 | static int config_string_item_max(char **conf_item, const char *value, | |
361 | size_t max) | |
362 | { | |
363 | if (strlen(value) >= max) { | |
a5a82508 | 364 | ERROR("%s is too long (>= %lu)", value, (unsigned long)max); |
6d03d92a DE |
365 | return -1; |
366 | } | |
367 | ||
368 | return config_string_item(conf_item, value); | |
369 | } | |
370 | ||
371 | static int config_path_item(char **conf_item, const char *value) | |
372 | { | |
373 | return config_string_item_max(conf_item, value, PATH_MAX); | |
374 | } | |
375 | ||
72d0e1cb SG |
376 | /* |
377 | * config entry is something like "lxc.network.0.ipv4" | |
378 | * the key 'lxc.network.' was found. So we make sure next | |
379 | * comes an integer, find the right callback (by rewriting | |
380 | * the key), and call it. | |
381 | */ | |
12a50cc6 | 382 | static int config_network_nic(const char *key, const char *value, |
72d0e1cb SG |
383 | struct lxc_conf *lxc_conf) |
384 | { | |
385 | char *copy = strdup(key), *p; | |
386 | int ret = -1; | |
387 | struct lxc_config_t *config; | |
388 | ||
389 | if (!copy) { | |
390 | SYSERROR("failed to allocate memory"); | |
391 | return -1; | |
392 | } | |
393 | /* | |
394 | * ok we know that to get here we've got "lxc.network." | |
395 | * and it isn't any of the other network entries. So | |
396 | * after the second . should come an integer (# of defined | |
397 | * nic) followed by a valid entry. | |
398 | */ | |
399 | if (*(key+12) < '0' || *(key+12) > '9') | |
400 | goto out; | |
46cd2845 | 401 | p = strchr(key+12, '.'); |
72d0e1cb SG |
402 | if (!p) |
403 | goto out; | |
404 | strcpy(copy+12, p+1); | |
405 | config = lxc_getconfig(copy); | |
406 | if (!config) { | |
407 | ERROR("unknown key %s", key); | |
408 | goto out; | |
409 | } | |
410 | ret = config->cb(key, value, lxc_conf); | |
411 | ||
412 | out: | |
413 | free(copy); | |
414 | return ret; | |
415 | } | |
416 | ||
6b0d5538 | 417 | static int config_network(const char *key, const char *value, |
61924323 | 418 | struct lxc_conf *lxc_conf) |
6b0d5538 | 419 | { |
61924323 | 420 | if (!config_value_empty(value)) { |
6b0d5538 SH |
421 | ERROR("lxc.network must not have a value"); |
422 | return -1; | |
423 | } | |
424 | ||
425 | return lxc_clear_config_network(lxc_conf); | |
426 | } | |
427 | ||
261658e8 BP |
428 | static int macvlan_mode(int *valuep, const char *value); |
429 | ||
12a50cc6 | 430 | static int config_network_type(const char *key, const char *value, |
e892973e | 431 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 432 | { |
5f4535a3 | 433 | struct lxc_list *network = &lxc_conf->network; |
c2cc9f0a | 434 | struct lxc_netdev *netdev; |
435 | struct lxc_list *list; | |
c2cc9f0a | 436 | |
d4ba45b3 | 437 | if (config_value_empty(value)) |
7d0eb87e SH |
438 | return lxc_clear_config_network(lxc_conf); |
439 | ||
c2cc9f0a | 440 | netdev = malloc(sizeof(*netdev)); |
441 | if (!netdev) { | |
36eb9bde | 442 | SYSERROR("failed to allocate memory"); |
c2cc9f0a | 443 | return -1; |
444 | } | |
445 | ||
82d5ae15 | 446 | memset(netdev, 0, sizeof(*netdev)); |
c2cc9f0a | 447 | lxc_list_init(&netdev->ipv4); |
448 | lxc_list_init(&netdev->ipv6); | |
c2cc9f0a | 449 | |
450 | list = malloc(sizeof(*list)); | |
451 | if (!list) { | |
36eb9bde | 452 | SYSERROR("failed to allocate memory"); |
022de5f3 | 453 | free(netdev); |
c2cc9f0a | 454 | return -1; |
455 | } | |
456 | ||
457 | lxc_list_init(list); | |
5f4535a3 | 458 | list->elem = netdev; |
c2cc9f0a | 459 | |
bac89583 | 460 | lxc_list_add_tail(network, list); |
a871ff6b | 461 | |
c2cc9f0a | 462 | if (!strcmp(value, "veth")) |
24654103 | 463 | netdev->type = LXC_NET_VETH; |
261658e8 | 464 | else if (!strcmp(value, "macvlan")) { |
24654103 | 465 | netdev->type = LXC_NET_MACVLAN; |
261658e8 BP |
466 | macvlan_mode(&netdev->priv.macvlan_attr.mode, "private"); |
467 | } | |
26c39028 | 468 | else if (!strcmp(value, "vlan")) |
24654103 | 469 | netdev->type = LXC_NET_VLAN; |
c2cc9f0a | 470 | else if (!strcmp(value, "phys")) |
24654103 | 471 | netdev->type = LXC_NET_PHYS; |
5f58350a | 472 | else if (!strcmp(value, "empty")) |
24654103 | 473 | netdev->type = LXC_NET_EMPTY; |
26b797f3 SH |
474 | else if (!strcmp(value, "none")) |
475 | netdev->type = LXC_NET_NONE; | |
c2cc9f0a | 476 | else { |
36eb9bde | 477 | ERROR("invalid network type %s", value); |
c2cc9f0a | 478 | return -1; |
479 | } | |
480 | return 0; | |
481 | } | |
482 | ||
a059591e DL |
483 | static int config_ip_prefix(struct in_addr *addr) |
484 | { | |
485 | if (IN_CLASSA(addr->s_addr)) | |
486 | return 32 - IN_CLASSA_NSHIFT; | |
487 | if (IN_CLASSB(addr->s_addr)) | |
488 | return 32 - IN_CLASSB_NSHIFT; | |
489 | if (IN_CLASSC(addr->s_addr)) | |
490 | return 32 - IN_CLASSC_NSHIFT; | |
491 | ||
492 | return 0; | |
493 | } | |
494 | ||
72d0e1cb SG |
495 | /* |
496 | * if you have p="lxc.network.0.link", pass it p+12 | |
497 | * to get back '0' (the index of the nic) | |
498 | */ | |
499 | static int get_network_netdev_idx(const char *key) | |
500 | { | |
501 | int ret, idx; | |
502 | ||
503 | if (*key < '0' || *key > '9') | |
504 | return -1; | |
505 | ret = sscanf(key, "%d", &idx); | |
506 | if (ret != 1) | |
507 | return -1; | |
508 | return idx; | |
509 | } | |
510 | ||
511 | /* | |
512 | * if you have p="lxc.network.0", pass this p+12 and it will return | |
513 | * the netdev of the first configured nic | |
514 | */ | |
515 | static struct lxc_netdev *get_netdev_from_key(const char *key, | |
516 | struct lxc_list *network) | |
517 | { | |
518 | int i = 0, idx = get_network_netdev_idx(key); | |
519 | struct lxc_netdev *netdev = NULL; | |
520 | struct lxc_list *it; | |
521 | if (idx == -1) | |
522 | return NULL; | |
523 | lxc_list_for_each(it, network) { | |
524 | if (idx == i++) { | |
525 | netdev = it->elem; | |
526 | break; | |
527 | } | |
528 | } | |
529 | return netdev; | |
530 | } | |
531 | ||
12a50cc6 DE |
532 | extern int lxc_list_nicconfigs(struct lxc_conf *c, const char *key, |
533 | char *retv, int inlen) | |
72d0e1cb SG |
534 | { |
535 | struct lxc_netdev *netdev; | |
536 | int fulllen = 0, len; | |
537 | ||
538 | netdev = get_netdev_from_key(key+12, &c->network); | |
539 | if (!netdev) | |
540 | return -1; | |
541 | ||
542 | if (!retv) | |
543 | inlen = 0; | |
544 | else | |
545 | memset(retv, 0, inlen); | |
546 | ||
74836992 | 547 | strprint(retv, inlen, "type\n"); |
72d0e1cb | 548 | strprint(retv, inlen, "script.up\n"); |
8fc8295a | 549 | strprint(retv, inlen, "script.down\n"); |
72d0e1cb SG |
550 | if (netdev->type != LXC_NET_EMPTY) { |
551 | strprint(retv, inlen, "flags\n"); | |
552 | strprint(retv, inlen, "link\n"); | |
553 | strprint(retv, inlen, "name\n"); | |
554 | strprint(retv, inlen, "hwaddr\n"); | |
555 | strprint(retv, inlen, "mtu\n"); | |
556 | strprint(retv, inlen, "ipv6\n"); | |
9eaf8a59 | 557 | strprint(retv, inlen, "ipv6.gateway\n"); |
72d0e1cb | 558 | strprint(retv, inlen, "ipv4\n"); |
9eaf8a59 | 559 | strprint(retv, inlen, "ipv4.gateway\n"); |
72d0e1cb SG |
560 | } |
561 | switch(netdev->type) { | |
562 | case LXC_NET_VETH: | |
563 | strprint(retv, inlen, "veth.pair\n"); | |
564 | break; | |
565 | case LXC_NET_MACVLAN: | |
566 | strprint(retv, inlen, "macvlan.mode\n"); | |
567 | break; | |
568 | case LXC_NET_VLAN: | |
569 | strprint(retv, inlen, "vlan.id\n"); | |
570 | break; | |
571 | case LXC_NET_PHYS: | |
572 | break; | |
573 | } | |
574 | return fulllen; | |
575 | } | |
576 | ||
16950ecb DL |
577 | static struct lxc_netdev *network_netdev(const char *key, const char *value, |
578 | struct lxc_list *network) | |
c2cc9f0a | 579 | { |
72d0e1cb | 580 | struct lxc_netdev *netdev = NULL; |
c2cc9f0a | 581 | |
5f4535a3 | 582 | if (lxc_list_empty(network)) { |
16950ecb DL |
583 | ERROR("network is not created for '%s' = '%s' option", |
584 | key, value); | |
33c945e0 | 585 | return NULL; |
c2cc9f0a | 586 | } |
587 | ||
72d0e1cb SG |
588 | if (get_network_netdev_idx(key+12) == -1) |
589 | netdev = lxc_list_last_elem(network); | |
590 | else | |
591 | netdev = get_netdev_from_key(key+12, network); | |
592 | ||
5f4535a3 | 593 | if (!netdev) { |
16950ecb DL |
594 | ERROR("no network device defined for '%s' = '%s' option", |
595 | key, value); | |
33c945e0 | 596 | return NULL; |
c2cc9f0a | 597 | } |
598 | ||
33c945e0 | 599 | return netdev; |
c2cc9f0a | 600 | } |
601 | ||
12a50cc6 | 602 | static int network_ifname(char **valuep, const char *value) |
c2cc9f0a | 603 | { |
c9bb9a85 | 604 | return config_string_item_max(valuep, value, IFNAMSIZ); |
c2cc9f0a | 605 | } |
606 | ||
e892973e DL |
607 | #ifndef MACVLAN_MODE_PRIVATE |
608 | # define MACVLAN_MODE_PRIVATE 1 | |
609 | #endif | |
610 | ||
611 | #ifndef MACVLAN_MODE_VEPA | |
612 | # define MACVLAN_MODE_VEPA 2 | |
613 | #endif | |
614 | ||
615 | #ifndef MACVLAN_MODE_BRIDGE | |
616 | # define MACVLAN_MODE_BRIDGE 4 | |
617 | #endif | |
618 | ||
99854161 EL |
619 | #ifndef MACVLAN_MODE_PASSTHRU |
620 | # define MACVLAN_MODE_PASSTHRU 8 | |
621 | #endif | |
622 | ||
12a50cc6 | 623 | static int macvlan_mode(int *valuep, const char *value) |
e892973e DL |
624 | { |
625 | struct mc_mode { | |
626 | char *name; | |
627 | int mode; | |
628 | } m[] = { | |
629 | { "private", MACVLAN_MODE_PRIVATE }, | |
630 | { "vepa", MACVLAN_MODE_VEPA }, | |
631 | { "bridge", MACVLAN_MODE_BRIDGE }, | |
99854161 | 632 | { "passthru", MACVLAN_MODE_PASSTHRU }, |
e892973e DL |
633 | }; |
634 | ||
84760c11 | 635 | size_t i; |
e892973e DL |
636 | |
637 | for (i = 0; i < sizeof(m)/sizeof(m[0]); i++) { | |
638 | if (strcmp(m[i].name, value)) | |
639 | continue; | |
640 | ||
641 | *valuep = m[i].mode; | |
642 | return 0; | |
643 | } | |
644 | ||
645 | return -1; | |
646 | } | |
647 | ||
508c263e SH |
648 | static int rand_complete_hwaddr(char *hwaddr) |
649 | { | |
650 | const char hex[] = "0123456789abcdef"; | |
651 | char *curs = hwaddr; | |
652 | ||
653 | #ifndef HAVE_RAND_R | |
654 | randseed(true); | |
655 | #else | |
656 | unsigned int seed=randseed(false); | |
657 | #endif | |
e6744e9b | 658 | while (*curs != '\0' && *curs != '\n') |
508c263e SH |
659 | { |
660 | if ( *curs == 'x' || *curs == 'X' ) { | |
661 | if (curs - hwaddr == 1) { | |
662 | //ensure address is unicast | |
663 | #ifdef HAVE_RAND_R | |
664 | *curs = hex[rand_r(&seed) & 0x0E]; | |
665 | } else { | |
666 | *curs = hex[rand_r(&seed) & 0x0F]; | |
667 | #else | |
668 | *curs = hex[rand() & 0x0E]; | |
669 | } else { | |
670 | *curs = hex[rand() & 0x0F]; | |
671 | #endif | |
672 | } | |
673 | } | |
674 | curs++; | |
675 | } | |
676 | return 0; | |
677 | } | |
678 | ||
12a50cc6 | 679 | static int config_network_flags(const char *key, const char *value, |
33c945e0 | 680 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 681 | { |
c2cc9f0a | 682 | struct lxc_netdev *netdev; |
683 | ||
16950ecb | 684 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 685 | if (!netdev) |
c2cc9f0a | 686 | return -1; |
c2cc9f0a | 687 | |
33c945e0 | 688 | netdev->flags |= IFF_UP; |
c2cc9f0a | 689 | |
33c945e0 MT |
690 | return 0; |
691 | } | |
692 | ||
576400e5 | 693 | static int set_network_link(const char *key, const char *value, struct lxc_conf *lxc_conf) |
694 | { | |
695 | struct lxc_netdev *netdev; | |
696 | ||
697 | netdev = network_netdev(key, value, &lxc_conf->network); | |
698 | if (!netdev) | |
699 | return -1; | |
700 | ||
701 | return network_ifname(&netdev->link, value); | |
702 | } | |
703 | ||
704 | static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf) | |
705 | { | |
706 | struct ifaddrs *ifaddr, *ifa; | |
707 | const char *type_key = "lxc.network.type"; | |
708 | const char *link_key = "lxc.network.link"; | |
709 | const char *tmpvalue = "phys"; | |
710 | int n, ret = 0; | |
711 | ||
712 | if (getifaddrs(&ifaddr) == -1) { | |
713 | SYSERROR("Get network interfaces failed"); | |
714 | return -1; | |
715 | } | |
716 | ||
717 | for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { | |
718 | if (!ifa->ifa_addr) | |
719 | continue; | |
720 | if (ifa->ifa_addr->sa_family != AF_PACKET) | |
721 | continue; | |
722 | ||
723 | if (!strncmp(value, ifa->ifa_name, strlen(value)-1)) { | |
724 | ret = config_network_type(type_key, tmpvalue, lxc_conf); | |
725 | if (!ret) { | |
726 | ret = set_network_link(link_key, ifa->ifa_name, lxc_conf); | |
727 | if (ret) { | |
728 | ERROR("failed to create matched ifnames"); | |
729 | break; | |
730 | } | |
731 | } else { | |
732 | ERROR("failed to create matched ifnames"); | |
733 | break; | |
734 | } | |
735 | } | |
736 | } | |
737 | ||
738 | freeifaddrs(ifaddr); /* free the dynamic memory */ | |
739 | ifaddr = NULL; /* prevent use after free */ | |
091045f8 | 740 | |
576400e5 | 741 | return ret; |
742 | } | |
743 | ||
12a50cc6 | 744 | static int config_network_link(const char *key, const char *value, |
33c945e0 MT |
745 | struct lxc_conf *lxc_conf) |
746 | { | |
747 | struct lxc_netdev *netdev; | |
576400e5 | 748 | struct lxc_list * it; |
749 | int ret = 0; | |
33c945e0 | 750 | |
16950ecb | 751 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 752 | if (!netdev) |
c2cc9f0a | 753 | return -1; |
c2cc9f0a | 754 | |
576400e5 | 755 | if (value[strlen(value) - 1] == '+' && netdev->type == LXC_NET_PHYS) { |
756 | //get the last network list and remove it. | |
757 | it = lxc_conf->network.prev; | |
758 | if (((struct lxc_netdev *)(it->elem))->type != LXC_NET_PHYS) { | |
759 | ERROR("lxc config cannot support string pattern matching for this link type"); | |
760 | return -1; | |
761 | } | |
762 | ||
763 | lxc_list_del(it); | |
764 | free(it); | |
765 | ret = create_matched_ifnames(value, lxc_conf); | |
766 | ||
767 | } else { | |
768 | ret = network_ifname(&netdev->link, value); | |
769 | } | |
770 | ||
771 | return ret; | |
c2cc9f0a | 772 | } |
773 | ||
12a50cc6 | 774 | static int config_network_name(const char *key, const char *value, |
33c945e0 | 775 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 776 | { |
c2cc9f0a | 777 | struct lxc_netdev *netdev; |
778 | ||
16950ecb | 779 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 780 | if (!netdev) |
c2cc9f0a | 781 | return -1; |
c2cc9f0a | 782 | |
33c945e0 MT |
783 | return network_ifname(&netdev->name, value); |
784 | } | |
785 | ||
12a50cc6 | 786 | static int config_network_veth_pair(const char *key, const char *value, |
e892973e DL |
787 | struct lxc_conf *lxc_conf) |
788 | { | |
789 | struct lxc_netdev *netdev; | |
790 | ||
791 | netdev = network_netdev(key, value, &lxc_conf->network); | |
792 | if (!netdev) | |
793 | return -1; | |
794 | ||
c5316d60 | 795 | if (netdev->type != LXC_NET_VETH) { |
128d327a | 796 | ERROR("Invalid veth pair for a non-veth netdev"); |
c5316d60 SH |
797 | return -1; |
798 | } | |
e892973e DL |
799 | return network_ifname(&netdev->priv.veth_attr.pair, value); |
800 | } | |
801 | ||
12a50cc6 | 802 | static int config_network_macvlan_mode(const char *key, const char *value, |
e892973e | 803 | struct lxc_conf *lxc_conf) |
8634bc19 MT |
804 | { |
805 | struct lxc_netdev *netdev; | |
806 | ||
807 | netdev = network_netdev(key, value, &lxc_conf->network); | |
808 | if (!netdev) | |
809 | return -1; | |
810 | ||
c5316d60 | 811 | if (netdev->type != LXC_NET_MACVLAN) { |
128d327a | 812 | ERROR("Invalid macvlan.mode for a non-macvlan netdev"); |
c5316d60 SH |
813 | return -1; |
814 | } | |
e892973e | 815 | return macvlan_mode(&netdev->priv.macvlan_attr.mode, value); |
8634bc19 MT |
816 | } |
817 | ||
12a50cc6 | 818 | static int config_network_hwaddr(const char *key, const char *value, |
33c945e0 MT |
819 | struct lxc_conf *lxc_conf) |
820 | { | |
821 | struct lxc_netdev *netdev; | |
822 | ||
508c263e SH |
823 | char *new_value = strdup(value); |
824 | if (!new_value) { | |
825 | SYSERROR("failed to strdup '%s': %m", value); | |
c2cc9f0a | 826 | return -1; |
508c263e SH |
827 | } |
828 | rand_complete_hwaddr(new_value); | |
c2cc9f0a | 829 | |
508c263e SH |
830 | netdev = network_netdev(key, new_value, &lxc_conf->network); |
831 | if (!netdev) { | |
832 | free(new_value); | |
833 | return -1; | |
834 | }; | |
835 | ||
9763f881 | 836 | if (config_value_empty(new_value)) { |
508c263e SH |
837 | free(new_value); |
838 | netdev->hwaddr = NULL; | |
839 | return 0; | |
840 | } | |
841 | ||
842 | netdev->hwaddr = new_value; | |
843 | return 0; | |
c2cc9f0a | 844 | } |
845 | ||
12a50cc6 | 846 | static int config_network_vlan_id(const char *key, const char *value, |
26c39028 JHS |
847 | struct lxc_conf *lxc_conf) |
848 | { | |
849 | struct lxc_netdev *netdev; | |
850 | ||
851 | netdev = network_netdev(key, value, &lxc_conf->network); | |
852 | if (!netdev) | |
853 | return -1; | |
854 | ||
c5316d60 | 855 | if (netdev->type != LXC_NET_VLAN) { |
128d327a | 856 | ERROR("Invalid vlan.id for a non-macvlan netdev"); |
c5316d60 SH |
857 | return -1; |
858 | } | |
f6cc1de1 | 859 | if (get_u16(&netdev->priv.vlan_attr.vid, value, 0)) |
26c39028 JHS |
860 | return -1; |
861 | ||
862 | return 0; | |
863 | } | |
864 | ||
12a50cc6 | 865 | static int config_network_mtu(const char *key, const char *value, |
33c945e0 | 866 | struct lxc_conf *lxc_conf) |
442cbbe6 | 867 | { |
442cbbe6 TR |
868 | struct lxc_netdev *netdev; |
869 | ||
16950ecb | 870 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 871 | if (!netdev) |
442cbbe6 | 872 | return -1; |
442cbbe6 | 873 | |
6d03d92a | 874 | return config_string_item(&netdev->mtu, value); |
442cbbe6 TR |
875 | } |
876 | ||
12a50cc6 | 877 | static int config_network_ipv4(const char *key, const char *value, |
33c945e0 | 878 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 879 | { |
c2cc9f0a | 880 | struct lxc_netdev *netdev; |
33c945e0 | 881 | struct lxc_inetdev *inetdev; |
c2cc9f0a | 882 | struct lxc_list *list; |
883 | char *cursor, *slash, *addr = NULL, *bcast = NULL, *prefix = NULL; | |
884 | ||
c3760156 | 885 | if (config_value_empty(value)) |
0797e123 CB |
886 | return lxc_clear_config_item(lxc_conf, key); |
887 | ||
16950ecb | 888 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 889 | if (!netdev) |
c2cc9f0a | 890 | return -1; |
c2cc9f0a | 891 | |
892 | inetdev = malloc(sizeof(*inetdev)); | |
893 | if (!inetdev) { | |
36eb9bde | 894 | SYSERROR("failed to allocate ipv4 address"); |
c2cc9f0a | 895 | return -1; |
896 | } | |
897 | memset(inetdev, 0, sizeof(*inetdev)); | |
898 | ||
899 | list = malloc(sizeof(*list)); | |
900 | if (!list) { | |
36eb9bde | 901 | SYSERROR("failed to allocate memory"); |
53719062 | 902 | free(inetdev); |
c2cc9f0a | 903 | return -1; |
904 | } | |
905 | ||
906 | lxc_list_init(list); | |
907 | list->elem = inetdev; | |
908 | ||
956edc54 SG |
909 | addr = strdup(value); |
910 | if (!addr) { | |
911 | ERROR("no address specified"); | |
53719062 SH |
912 | free(inetdev); |
913 | free(list); | |
956edc54 SG |
914 | return -1; |
915 | } | |
c2cc9f0a | 916 | |
917 | cursor = strstr(addr, " "); | |
918 | if (cursor) { | |
919 | *cursor = '\0'; | |
920 | bcast = cursor + 1; | |
921 | } | |
922 | ||
923 | slash = strstr(addr, "/"); | |
924 | if (slash) { | |
925 | *slash = '\0'; | |
926 | prefix = slash + 1; | |
927 | } | |
928 | ||
c2cc9f0a | 929 | if (!inet_pton(AF_INET, addr, &inetdev->addr)) { |
36eb9bde | 930 | SYSERROR("invalid ipv4 address: %s", value); |
53719062 | 931 | free(inetdev); |
956edc54 | 932 | free(addr); |
53719062 | 933 | free(list); |
c2cc9f0a | 934 | return -1; |
935 | } | |
936 | ||
0093bb8c DL |
937 | if (bcast && !inet_pton(AF_INET, bcast, &inetdev->bcast)) { |
938 | SYSERROR("invalid ipv4 broadcast address: %s", value); | |
53719062 SH |
939 | free(inetdev); |
940 | free(list); | |
956edc54 | 941 | free(addr); |
0093bb8c DL |
942 | return -1; |
943 | } | |
c2cc9f0a | 944 | |
a059591e | 945 | /* no prefix specified, determine it from the network class */ |
1c633398 CB |
946 | if (prefix) { |
947 | if (lxc_safe_uint(prefix, &inetdev->prefix) < 0) | |
948 | return -1; | |
949 | } else { | |
950 | inetdev->prefix = config_ip_prefix(&inetdev->addr); | |
951 | } | |
a059591e | 952 | |
b3df193c | 953 | /* if no broadcast address, let compute one from the |
0093bb8c DL |
954 | * prefix and address |
955 | */ | |
956 | if (!bcast) { | |
1b7d4743 DL |
957 | inetdev->bcast.s_addr = inetdev->addr.s_addr; |
958 | inetdev->bcast.s_addr |= | |
959 | htonl(INADDR_BROADCAST >> inetdev->prefix); | |
0093bb8c | 960 | } |
c2cc9f0a | 961 | |
8538f388 | 962 | lxc_list_add_tail(&netdev->ipv4, list); |
c2cc9f0a | 963 | |
956edc54 | 964 | free(addr); |
c2cc9f0a | 965 | return 0; |
966 | } | |
967 | ||
12a50cc6 | 968 | static int config_network_ipv4_gateway(const char *key, const char *value, |
f8fee0e2 MK |
969 | struct lxc_conf *lxc_conf) |
970 | { | |
971 | struct lxc_netdev *netdev; | |
f8fee0e2 MK |
972 | |
973 | netdev = network_netdev(key, value, &lxc_conf->network); | |
974 | if (!netdev) | |
975 | return -1; | |
976 | ||
e088e926 | 977 | free(netdev->ipv4_gateway); |
f8fee0e2 | 978 | |
10da55e4 | 979 | if (config_value_empty(value)) { |
e088e926 SG |
980 | netdev->ipv4_gateway = NULL; |
981 | } else if (!strcmp(value, "auto")) { | |
19a26f82 MK |
982 | netdev->ipv4_gateway = NULL; |
983 | netdev->ipv4_gateway_auto = true; | |
984 | } else { | |
e088e926 SG |
985 | struct in_addr *gw; |
986 | ||
987 | gw = malloc(sizeof(*gw)); | |
988 | if (!gw) { | |
989 | SYSERROR("failed to allocate ipv4 gateway address"); | |
990 | return -1; | |
991 | } | |
992 | ||
19a26f82 MK |
993 | if (!inet_pton(AF_INET, value, gw)) { |
994 | SYSERROR("invalid ipv4 gateway address: %s", value); | |
53719062 | 995 | free(gw); |
19a26f82 MK |
996 | return -1; |
997 | } | |
998 | ||
999 | netdev->ipv4_gateway = gw; | |
1000 | netdev->ipv4_gateway_auto = false; | |
f8fee0e2 MK |
1001 | } |
1002 | ||
f8fee0e2 MK |
1003 | return 0; |
1004 | } | |
1005 | ||
12a50cc6 | 1006 | static int config_network_ipv6(const char *key, const char *value, |
e892973e | 1007 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 1008 | { |
c2cc9f0a | 1009 | struct lxc_netdev *netdev; |
1010 | struct lxc_inet6dev *inet6dev; | |
1011 | struct lxc_list *list; | |
12a50cc6 | 1012 | char *slash,*valdup; |
c2cc9f0a | 1013 | char *netmask; |
1014 | ||
cae8a0f0 | 1015 | if (config_value_empty(value)) |
0797e123 CB |
1016 | return lxc_clear_config_item(lxc_conf, key); |
1017 | ||
16950ecb | 1018 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 1019 | if (!netdev) |
c2cc9f0a | 1020 | return -1; |
c2cc9f0a | 1021 | |
1022 | inet6dev = malloc(sizeof(*inet6dev)); | |
1023 | if (!inet6dev) { | |
36eb9bde | 1024 | SYSERROR("failed to allocate ipv6 address"); |
c2cc9f0a | 1025 | return -1; |
1026 | } | |
1027 | memset(inet6dev, 0, sizeof(*inet6dev)); | |
1028 | ||
1029 | list = malloc(sizeof(*list)); | |
1030 | if (!list) { | |
36eb9bde | 1031 | SYSERROR("failed to allocate memory"); |
28027320 | 1032 | free(inet6dev); |
c2cc9f0a | 1033 | return -1; |
1034 | } | |
1035 | ||
1036 | lxc_list_init(list); | |
1037 | list->elem = inet6dev; | |
1038 | ||
956edc54 SG |
1039 | valdup = strdup(value); |
1040 | if (!valdup) { | |
1041 | ERROR("no address specified"); | |
28027320 SH |
1042 | free(list); |
1043 | free(inet6dev); | |
956edc54 SG |
1044 | return -1; |
1045 | } | |
1046 | ||
a059591e | 1047 | inet6dev->prefix = 64; |
12a50cc6 | 1048 | slash = strstr(valdup, "/"); |
c2cc9f0a | 1049 | if (slash) { |
1050 | *slash = '\0'; | |
1051 | netmask = slash + 1; | |
1c633398 CB |
1052 | if (lxc_safe_uint(netmask, &inet6dev->prefix) < 0) |
1053 | return -1; | |
c2cc9f0a | 1054 | } |
1055 | ||
5acccf95 SH |
1056 | if (!inet_pton(AF_INET6, valdup, &inet6dev->addr)) { |
1057 | SYSERROR("invalid ipv6 address: %s", valdup); | |
28027320 SH |
1058 | free(list); |
1059 | free(inet6dev); | |
956edc54 | 1060 | free(valdup); |
c2cc9f0a | 1061 | return -1; |
1062 | } | |
1063 | ||
8538f388 | 1064 | lxc_list_add_tail(&netdev->ipv6, list); |
c2cc9f0a | 1065 | |
956edc54 | 1066 | free(valdup); |
c2cc9f0a | 1067 | return 0; |
1068 | } | |
1069 | ||
12a50cc6 | 1070 | static int config_network_ipv6_gateway(const char *key, const char *value, |
f8fee0e2 MK |
1071 | struct lxc_conf *lxc_conf) |
1072 | { | |
1073 | struct lxc_netdev *netdev; | |
f8fee0e2 MK |
1074 | |
1075 | netdev = network_netdev(key, value, &lxc_conf->network); | |
1076 | if (!netdev) | |
1077 | return -1; | |
1078 | ||
e088e926 | 1079 | free(netdev->ipv6_gateway); |
f8fee0e2 | 1080 | |
114df19e | 1081 | if (config_value_empty(value)) { |
ffe34437 | 1082 | netdev->ipv6_gateway = NULL; |
e088e926 | 1083 | } else if (!strcmp(value, "auto")) { |
19a26f82 MK |
1084 | netdev->ipv6_gateway = NULL; |
1085 | netdev->ipv6_gateway_auto = true; | |
1086 | } else { | |
8fb86a37 SH |
1087 | struct in6_addr *gw; |
1088 | ||
bec695f3 DE |
1089 | gw = malloc(sizeof(*gw)); |
1090 | if (!gw) { | |
1091 | SYSERROR("failed to allocate ipv6 gateway address"); | |
1092 | return -1; | |
1093 | } | |
1094 | ||
19a26f82 MK |
1095 | if (!inet_pton(AF_INET6, value, gw)) { |
1096 | SYSERROR("invalid ipv6 gateway address: %s", value); | |
28027320 | 1097 | free(gw); |
19a26f82 MK |
1098 | return -1; |
1099 | } | |
1100 | ||
1101 | netdev->ipv6_gateway = gw; | |
1102 | netdev->ipv6_gateway_auto = false; | |
f8fee0e2 MK |
1103 | } |
1104 | ||
f8fee0e2 MK |
1105 | return 0; |
1106 | } | |
1107 | ||
8fc8295a DE |
1108 | static int config_network_script_up(const char *key, const char *value, |
1109 | struct lxc_conf *lxc_conf) | |
e3b4c4c4 ST |
1110 | { |
1111 | struct lxc_netdev *netdev; | |
1112 | ||
1113 | netdev = network_netdev(key, value, &lxc_conf->network); | |
1114 | if (!netdev) | |
8fc8295a | 1115 | return -1; |
e3b4c4c4 | 1116 | |
8fc8295a DE |
1117 | return config_string_item(&netdev->upscript, value); |
1118 | } | |
1119 | ||
1120 | static int config_network_script_down(const char *key, const char *value, | |
1121 | struct lxc_conf *lxc_conf) | |
1122 | { | |
1123 | struct lxc_netdev *netdev; | |
1124 | ||
1125 | netdev = network_netdev(key, value, &lxc_conf->network); | |
1126 | if (!netdev) | |
1127 | return -1; | |
1128 | ||
1129 | return config_string_item(&netdev->downscript, value); | |
e3b4c4c4 ST |
1130 | } |
1131 | ||
26ddeedd SH |
1132 | static int add_hook(struct lxc_conf *lxc_conf, int which, char *hook) |
1133 | { | |
1134 | struct lxc_list *hooklist; | |
1135 | ||
1136 | hooklist = malloc(sizeof(*hooklist)); | |
1137 | if (!hooklist) { | |
1138 | free(hook); | |
1139 | return -1; | |
1140 | } | |
1141 | hooklist->elem = hook; | |
1142 | lxc_list_add_tail(&lxc_conf->hooks[which], hooklist); | |
1143 | return 0; | |
1144 | } | |
1145 | ||
12a50cc6 | 1146 | static int config_seccomp(const char *key, const char *value, |
8f2c3a70 SH |
1147 | struct lxc_conf *lxc_conf) |
1148 | { | |
6d03d92a | 1149 | return config_path_item(&lxc_conf->seccomp, value); |
8f2c3a70 SH |
1150 | } |
1151 | ||
67c660d0 SG |
1152 | static int config_init_cmd(const char *key, const char *value, |
1153 | struct lxc_conf *lxc_conf) | |
1154 | { | |
1155 | return config_path_item(&lxc_conf->init_cmd, value); | |
1156 | } | |
1157 | ||
72bb04e4 | 1158 | static int config_init_uid(const char *key, const char *value, |
2e7cde40 | 1159 | struct lxc_conf *lxc_conf) |
72bb04e4 | 1160 | { |
d1e5d636 CB |
1161 | unsigned int init_uid; |
1162 | ||
2e7cde40 CB |
1163 | /* Set config value to default. */ |
1164 | if (config_value_empty(value)) { | |
1165 | lxc_conf->init_uid = 0; | |
fee80911 | 1166 | return 0; |
2e7cde40 | 1167 | } |
fee80911 | 1168 | |
2e7cde40 | 1169 | /* Parse new config value. */ |
d1e5d636 CB |
1170 | if (lxc_safe_uint(value, &init_uid) < 0) |
1171 | return -1; | |
1172 | lxc_conf->init_uid = init_uid; | |
1173 | ||
72bb04e4 PT |
1174 | return 0; |
1175 | } | |
1176 | ||
1177 | static int config_init_gid(const char *key, const char *value, | |
2debb6e6 | 1178 | struct lxc_conf *lxc_conf) |
72bb04e4 | 1179 | { |
d1e5d636 CB |
1180 | unsigned int init_gid; |
1181 | ||
2debb6e6 CB |
1182 | /* Set config value to default. */ |
1183 | if (config_value_empty(value)) { | |
1184 | lxc_conf->init_gid = 0; | |
a757cc7d | 1185 | return 0; |
2debb6e6 | 1186 | } |
a757cc7d | 1187 | |
2debb6e6 | 1188 | /* Parse new config value. */ |
d1e5d636 CB |
1189 | if (lxc_safe_uint(value, &init_gid) < 0) |
1190 | return -1; | |
1191 | lxc_conf->init_gid = init_gid; | |
1192 | ||
72bb04e4 PT |
1193 | return 0; |
1194 | } | |
1195 | ||
12a50cc6 | 1196 | static int config_hook(const char *key, const char *value, |
26ddeedd SH |
1197 | struct lxc_conf *lxc_conf) |
1198 | { | |
7d0eb87e | 1199 | char *copy; |
72bb04e4 | 1200 | |
03d88f7c | 1201 | if (config_value_empty(value)) |
7d0eb87e SH |
1202 | return lxc_clear_hooks(lxc_conf, key); |
1203 | ||
6b0d5538 SH |
1204 | if (strcmp(key, "lxc.hook") == 0) { |
1205 | ERROR("lxc.hook cannot take a value"); | |
1206 | return -1; | |
1207 | } | |
7d0eb87e | 1208 | copy = strdup(value); |
26ddeedd SH |
1209 | if (!copy) { |
1210 | SYSERROR("failed to dup string '%s'", value); | |
1211 | return -1; | |
1212 | } | |
1213 | if (strcmp(key, "lxc.hook.pre-start") == 0) | |
1214 | return add_hook(lxc_conf, LXCHOOK_PRESTART, copy); | |
5ea6163a SH |
1215 | else if (strcmp(key, "lxc.hook.pre-mount") == 0) |
1216 | return add_hook(lxc_conf, LXCHOOK_PREMOUNT, copy); | |
f7bee6c6 MW |
1217 | else if (strcmp(key, "lxc.hook.autodev") == 0) |
1218 | return add_hook(lxc_conf, LXCHOOK_AUTODEV, copy); | |
26ddeedd SH |
1219 | else if (strcmp(key, "lxc.hook.mount") == 0) |
1220 | return add_hook(lxc_conf, LXCHOOK_MOUNT, copy); | |
1221 | else if (strcmp(key, "lxc.hook.start") == 0) | |
1222 | return add_hook(lxc_conf, LXCHOOK_START, copy); | |
52492063 WB |
1223 | else if (strcmp(key, "lxc.hook.stop") == 0) |
1224 | return add_hook(lxc_conf, LXCHOOK_STOP, copy); | |
26ddeedd SH |
1225 | else if (strcmp(key, "lxc.hook.post-stop") == 0) |
1226 | return add_hook(lxc_conf, LXCHOOK_POSTSTOP, copy); | |
148e91f5 SH |
1227 | else if (strcmp(key, "lxc.hook.clone") == 0) |
1228 | return add_hook(lxc_conf, LXCHOOK_CLONE, copy); | |
37cf711b SY |
1229 | else if (strcmp(key, "lxc.hook.destroy") == 0) |
1230 | return add_hook(lxc_conf, LXCHOOK_DESTROY, copy); | |
26ddeedd SH |
1231 | SYSERROR("Unknown key: %s", key); |
1232 | free(copy); | |
1233 | return -1; | |
1234 | } | |
1235 | ||
8f47bc3f | 1236 | static int config_personality(const char *key, const char *value, |
cccc74b5 DL |
1237 | struct lxc_conf *lxc_conf) |
1238 | { | |
525f0002 | 1239 | signed long personality = lxc_config_parse_arch(value); |
cccc74b5 | 1240 | |
525f0002 CS |
1241 | if (personality >= 0) |
1242 | lxc_conf->personality = personality; | |
1243 | else | |
1244 | WARN("unsupported personality '%s'", value); | |
970ab589 DL |
1245 | |
1246 | return 0; | |
cccc74b5 DL |
1247 | } |
1248 | ||
12a50cc6 DE |
1249 | static int config_pts(const char *key, const char *value, |
1250 | struct lxc_conf *lxc_conf) | |
10db618d | 1251 | { |
ec200ce9 CB |
1252 | /* Set config value to default. */ |
1253 | if (config_value_empty(value)) { | |
1254 | lxc_conf->pts = 0; | |
884a4580 | 1255 | return 0; |
ec200ce9 | 1256 | } |
884a4580 | 1257 | |
ec200ce9 | 1258 | /* Parse new config value. */ |
17919969 CB |
1259 | if (lxc_safe_uint(value, &lxc_conf->pts) < 0) |
1260 | return -1; | |
10db618d | 1261 | |
1262 | return 0; | |
1263 | } | |
1264 | ||
ee1e7aa0 | 1265 | static int config_start(const char *key, const char *value, |
ebb80f95 | 1266 | struct lxc_conf *lxc_conf) |
ee1e7aa0 | 1267 | { |
ebb80f95 CB |
1268 | bool is_empty; |
1269 | ||
1270 | is_empty = config_value_empty(value); | |
1271 | ||
1272 | if (strcmp(key, "lxc.start.auto") == 0) { | |
1273 | /* Set config value to default. */ | |
1274 | if (is_empty) { | |
1275 | lxc_conf->start_auto = 0; | |
1276 | return 0; | |
1277 | } | |
61ff8fc8 | 1278 | |
ebb80f95 | 1279 | /* Parse new config value. */ |
3590152f CB |
1280 | if (lxc_safe_uint(value, &lxc_conf->start_auto) < 0) |
1281 | return -1; | |
ebb80f95 | 1282 | |
3590152f CB |
1283 | if (lxc_conf->start_auto > 1) |
1284 | return -1; | |
ebb80f95 | 1285 | |
ee1e7aa0 | 1286 | return 0; |
ebb80f95 CB |
1287 | } else if (strcmp(key, "lxc.start.delay") == 0) { |
1288 | /* Set config value to default. */ | |
1289 | if (is_empty) { | |
1290 | lxc_conf->start_delay = 0; | |
1291 | return 0; | |
1292 | } | |
1293 | ||
1294 | /* Parse new config value. */ | |
1295 | return lxc_safe_uint(value, &lxc_conf->start_delay); | |
1296 | } else if (strcmp(key, "lxc.start.order") == 0) { | |
1297 | /* Set config value to default. */ | |
1298 | if (is_empty) { | |
1299 | lxc_conf->start_order = 0; | |
1300 | return 0; | |
1301 | } | |
1302 | ||
1303 | /* Parse new config value. */ | |
1304 | return lxc_safe_int(value, &lxc_conf->start_order); | |
ee1e7aa0 | 1305 | } |
ebb80f95 | 1306 | |
ee1e7aa0 SG |
1307 | SYSERROR("Unknown key: %s", key); |
1308 | return -1; | |
1309 | } | |
1310 | ||
a8dfe4e0 WB |
1311 | static int config_monitor(const char *key, const char *value, |
1312 | struct lxc_conf *lxc_conf) | |
1313 | { | |
4ad9cd26 CB |
1314 | /* Set config value to default. */ |
1315 | if (config_value_empty(value)) { | |
1316 | lxc_conf->monitor_unshare = 0; | |
a8dfe4e0 WB |
1317 | return 0; |
1318 | } | |
4ad9cd26 CB |
1319 | |
1320 | /* Parse new config value. */ | |
1321 | if (strcmp(key, "lxc.monitor.unshare") == 0) | |
1322 | return lxc_safe_uint(value, &lxc_conf->monitor_unshare); | |
1323 | ||
a8dfe4e0 WB |
1324 | SYSERROR("Unknown key: %s", key); |
1325 | return -1; | |
1326 | } | |
1327 | ||
ee1e7aa0 SG |
1328 | static int config_group(const char *key, const char *value, |
1329 | struct lxc_conf *lxc_conf) | |
1330 | { | |
1331 | char *groups, *groupptr, *sptr, *token; | |
1332 | struct lxc_list *grouplist; | |
1333 | int ret = -1; | |
1334 | ||
7584546d | 1335 | if (config_value_empty(value)) |
ee1e7aa0 SG |
1336 | return lxc_clear_groups(lxc_conf); |
1337 | ||
1338 | groups = strdup(value); | |
1339 | if (!groups) { | |
1340 | SYSERROR("failed to dup '%s'", value); | |
1341 | return -1; | |
1342 | } | |
1343 | ||
1344 | /* in case several groups are specified in a single line | |
1345 | * split these groups in a single element for the list */ | |
1346 | for (groupptr = groups;;groupptr = NULL) { | |
d028235d SG |
1347 | token = strtok_r(groupptr, " \t", &sptr); |
1348 | if (!token) { | |
ee1e7aa0 | 1349 | ret = 0; |
d028235d | 1350 | break; |
ee1e7aa0 SG |
1351 | } |
1352 | ||
1353 | grouplist = malloc(sizeof(*grouplist)); | |
1354 | if (!grouplist) { | |
1355 | SYSERROR("failed to allocate groups list"); | |
1356 | break; | |
1357 | } | |
1358 | ||
1359 | grouplist->elem = strdup(token); | |
1360 | if (!grouplist->elem) { | |
1361 | SYSERROR("failed to dup '%s'", token); | |
1362 | free(grouplist); | |
1363 | break; | |
1364 | } | |
1365 | ||
1366 | lxc_list_add_tail(&lxc_conf->groups, grouplist); | |
d028235d | 1367 | } |
ee1e7aa0 SG |
1368 | |
1369 | free(groups); | |
1370 | ||
1371 | return ret; | |
1372 | } | |
1373 | ||
7c661726 MP |
1374 | static int config_environment(const char *key, const char *value, |
1375 | struct lxc_conf *lxc_conf) | |
1376 | { | |
1377 | struct lxc_list *list_item = NULL; | |
1378 | ||
cdee76e3 | 1379 | if (config_value_empty(value)) |
ab799c0b SG |
1380 | return lxc_clear_environment(lxc_conf); |
1381 | ||
7c661726 MP |
1382 | list_item = malloc(sizeof(*list_item)); |
1383 | if (!list_item) | |
1384 | goto freak_out; | |
1385 | ||
1386 | list_item->elem = strdup(value); | |
1387 | ||
1388 | if (!list_item->elem) | |
1389 | goto freak_out; | |
1390 | ||
1391 | lxc_list_add_tail(&lxc_conf->environment, list_item); | |
1392 | ||
1393 | return 0; | |
1394 | ||
1395 | freak_out: | |
f10fad2f | 1396 | free(list_item); |
7c661726 MP |
1397 | |
1398 | return -1; | |
1399 | } | |
1400 | ||
12a50cc6 DE |
1401 | static int config_tty(const char *key, const char *value, |
1402 | struct lxc_conf *lxc_conf) | |
b0a33c1e | 1403 | { |
cb508ee8 CB |
1404 | /* Set config value to default. */ |
1405 | if (config_value_empty(value)) { | |
1406 | lxc_conf->tty = 0; | |
fb12b12a | 1407 | return 0; |
cb508ee8 | 1408 | } |
fb12b12a | 1409 | |
cb508ee8 CB |
1410 | /* Parse new config value. */ |
1411 | return lxc_safe_uint(value, &lxc_conf->tty); | |
b0a33c1e | 1412 | } |
1413 | ||
12a50cc6 | 1414 | static int config_ttydir(const char *key, const char *value, |
7c6ef2a2 SH |
1415 | struct lxc_conf *lxc_conf) |
1416 | { | |
6d03d92a | 1417 | return config_string_item_max(&lxc_conf->ttydir, value, NAME_MAX+1); |
7c6ef2a2 SH |
1418 | } |
1419 | ||
7e0e1d94 AV |
1420 | static int config_kmsg(const char *key, const char *value, |
1421 | struct lxc_conf *lxc_conf) | |
1422 | { | |
5767e9ba CB |
1423 | if (config_value_empty(value)) |
1424 | return 0; | |
1425 | ||
91863d36 CB |
1426 | if (lxc_safe_uint(value, &lxc_conf->kmsg) < 0) |
1427 | return -1; | |
7e0e1d94 | 1428 | |
91863d36 CB |
1429 | if (lxc_conf->kmsg > 1) |
1430 | return -1; | |
7e0e1d94 AV |
1431 | |
1432 | return 0; | |
1433 | } | |
1434 | ||
fe4de9a6 DE |
1435 | static int config_lsm_aa_profile(const char *key, const char *value, |
1436 | struct lxc_conf *lxc_conf) | |
e075f5d9 | 1437 | { |
6d03d92a | 1438 | return config_string_item(&lxc_conf->lsm_aa_profile, value); |
fe4de9a6 DE |
1439 | } |
1440 | ||
7aff4f43 SH |
1441 | static int config_lsm_aa_incomplete(const char *key, const char *value, |
1442 | struct lxc_conf *lxc_conf) | |
1443 | { | |
a678e9fa CB |
1444 | if (config_value_empty(value)) |
1445 | return 0; | |
1446 | ||
a56e2df9 CB |
1447 | if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete) < 0) |
1448 | return -1; | |
7aff4f43 | 1449 | |
a56e2df9 CB |
1450 | if (lxc_conf->lsm_aa_allow_incomplete > 1) { |
1451 | ERROR("Wrong value for lxc.lsm_aa_allow_incomplete. Can only be set to 0 or 1"); | |
1452 | return -1; | |
1453 | } | |
7aff4f43 SH |
1454 | |
1455 | return 0; | |
1456 | } | |
1457 | ||
fe4de9a6 DE |
1458 | static int config_lsm_se_context(const char *key, const char *value, |
1459 | struct lxc_conf *lxc_conf) | |
1460 | { | |
6d03d92a | 1461 | return config_string_item(&lxc_conf->lsm_se_context, value); |
e075f5d9 | 1462 | } |
e075f5d9 | 1463 | |
4a85ce2a | 1464 | static int config_logfile(const char *key, const char *value, |
858377e4 | 1465 | struct lxc_conf *c) |
4a85ce2a | 1466 | { |
6d03d92a DE |
1467 | int ret; |
1468 | ||
b40a606e SH |
1469 | // store these values in the lxc_conf, and then try to set for |
1470 | // actual current logging. | |
858377e4 | 1471 | ret = config_path_item(&c->logfile, value); |
6d03d92a | 1472 | if (ret == 0) |
858377e4 | 1473 | ret = lxc_log_set_file(&c->logfd, c->logfile); |
6d03d92a | 1474 | return ret; |
4a85ce2a SH |
1475 | } |
1476 | ||
1477 | static int config_loglevel(const char *key, const char *value, | |
1478 | struct lxc_conf *lxc_conf) | |
1479 | { | |
9ea87d5d SH |
1480 | int newlevel; |
1481 | ||
33501e96 | 1482 | if (config_value_empty(value)) |
4a85ce2a SH |
1483 | return 0; |
1484 | ||
a56e2df9 CB |
1485 | if (value[0] >= '0' && value[0] <= '9') { |
1486 | if (lxc_safe_int(value, &newlevel) < 0) | |
1487 | return -1; | |
1488 | } else { | |
9ea87d5d | 1489 | newlevel = lxc_log_priority_to_int(value); |
a56e2df9 | 1490 | } |
b40a606e SH |
1491 | // store these values in the lxc_conf, and then try to set for |
1492 | // actual current logging. | |
1493 | lxc_conf->loglevel = newlevel; | |
858377e4 | 1494 | return lxc_log_set_level(&lxc_conf->loglevel, newlevel); |
4a85ce2a SH |
1495 | } |
1496 | ||
12a50cc6 | 1497 | static int config_autodev(const char *key, const char *value, |
c6883f38 SH |
1498 | struct lxc_conf *lxc_conf) |
1499 | { | |
180abbc0 CB |
1500 | if (config_value_empty(value)) |
1501 | return 0; | |
1502 | ||
ff6cb4ed CB |
1503 | if (lxc_safe_uint(value, &lxc_conf->autodev) < 0) |
1504 | return -1; | |
c6883f38 | 1505 | |
ff6cb4ed CB |
1506 | if (lxc_conf->autodev > 1) { |
1507 | ERROR("Wrong value for lxc.autodev. Can only be set to 0 or 1"); | |
1508 | return -1; | |
1509 | } | |
c6883f38 SH |
1510 | |
1511 | return 0; | |
1512 | } | |
1513 | ||
a84b9932 AV |
1514 | static int sig_num(const char *sig) |
1515 | { | |
f2e539b3 | 1516 | unsigned int signum; |
a84b9932 | 1517 | |
f2e539b3 | 1518 | if (lxc_safe_uint(sig, &signum) < 0) |
a84b9932 | 1519 | return -1; |
f2e539b3 CB |
1520 | |
1521 | return signum; | |
a84b9932 AV |
1522 | } |
1523 | ||
1524 | static int rt_sig_num(const char *signame) | |
1525 | { | |
1526 | int sig_n = 0; | |
1527 | int rtmax = 0; | |
1528 | ||
1529 | if (strncasecmp(signame, "max-", 4) == 0) { | |
1530 | rtmax = 1; | |
1531 | } | |
1532 | signame += 4; | |
1533 | if (!isdigit(*signame)) | |
1534 | return -1; | |
1535 | sig_n = sig_num(signame); | |
1536 | sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n; | |
1537 | if (sig_n > SIGRTMAX || sig_n < SIGRTMIN) | |
1538 | return -1; | |
1539 | return sig_n; | |
1540 | } | |
1541 | ||
1542 | static int sig_parse(const char *signame) { | |
84760c11 | 1543 | size_t n; |
a84b9932 AV |
1544 | |
1545 | if (isdigit(*signame)) { | |
1546 | return sig_num(signame); | |
1547 | } else if (strncasecmp(signame, "sig", 3) == 0) { | |
1548 | signame += 3; | |
1549 | if (strncasecmp(signame, "rt", 2) == 0) | |
1550 | return rt_sig_num(signame + 2); | |
1551 | for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++) { | |
1552 | if (strcasecmp (signames[n].name, signame) == 0) | |
1553 | return signames[n].num; | |
1554 | } | |
1555 | } | |
1556 | return -1; | |
1557 | } | |
1558 | ||
f0f1d8c0 DE |
1559 | static int config_haltsignal(const char *key, const char *value, |
1560 | struct lxc_conf *lxc_conf) | |
1561 | { | |
62a085fb | 1562 | int sig_n; |
f0f1d8c0 | 1563 | |
955912f0 CB |
1564 | if (config_value_empty(value)) |
1565 | return 0; | |
1566 | ||
62a085fb CB |
1567 | sig_n = sig_parse(value); |
1568 | ||
f0f1d8c0 DE |
1569 | if (sig_n < 0) |
1570 | return -1; | |
1571 | lxc_conf->haltsignal = sig_n; | |
1572 | ||
1573 | return 0; | |
1574 | } | |
1575 | ||
dd267776 | 1576 | static int config_rebootsignal(const char *key, const char *value, |
9d7e7587 | 1577 | struct lxc_conf *lxc_conf) |
dd267776 | 1578 | { |
9d7e7587 | 1579 | int sig_n; |
dd267776 | 1580 | |
9d7e7587 CB |
1581 | if (config_value_empty(value)) |
1582 | return 0; | |
1583 | ||
1584 | sig_n = sig_parse(value); | |
dd267776 BP |
1585 | if (sig_n < 0) |
1586 | return -1; | |
1587 | lxc_conf->rebootsignal = sig_n; | |
1588 | ||
1589 | return 0; | |
1590 | } | |
1591 | ||
a84b9932 | 1592 | static int config_stopsignal(const char *key, const char *value, |
6ca6aedd | 1593 | struct lxc_conf *lxc_conf) |
a84b9932 | 1594 | { |
6ca6aedd | 1595 | int sig_n; |
a84b9932 | 1596 | |
6ca6aedd CB |
1597 | if (config_value_empty(value)) |
1598 | return 0; | |
1599 | ||
1600 | sig_n = sig_parse(value); | |
a84b9932 AV |
1601 | if (sig_n < 0) |
1602 | return -1; | |
1603 | lxc_conf->stopsignal = sig_n; | |
1604 | ||
1605 | return 0; | |
1606 | } | |
1607 | ||
12a50cc6 DE |
1608 | static int config_cgroup(const char *key, const char *value, |
1609 | struct lxc_conf *lxc_conf) | |
576f946d | 1610 | { |
1611 | char *token = "lxc.cgroup."; | |
1612 | char *subkey; | |
bf83c5b9 MS |
1613 | struct lxc_list *cglist = NULL; |
1614 | struct lxc_cgroup *cgelem = NULL; | |
576f946d | 1615 | |
fb0752be | 1616 | if (config_value_empty(value)) |
7d0eb87e SH |
1617 | return lxc_clear_cgroups(lxc_conf, key); |
1618 | ||
576f946d | 1619 | subkey = strstr(key, token); |
1620 | ||
1621 | if (!subkey) | |
1622 | return -1; | |
1623 | ||
1624 | if (!strlen(subkey)) | |
1625 | return -1; | |
1626 | ||
1627 | if (strlen(subkey) == strlen(token)) | |
1628 | return -1; | |
a871ff6b | 1629 | |
576f946d | 1630 | subkey += strlen(token); |
1631 | ||
1632 | cglist = malloc(sizeof(*cglist)); | |
1633 | if (!cglist) | |
bf83c5b9 | 1634 | goto out; |
576f946d | 1635 | |
1636 | cgelem = malloc(sizeof(*cgelem)); | |
bf83c5b9 MS |
1637 | if (!cgelem) |
1638 | goto out; | |
1639 | memset(cgelem, 0, sizeof(*cgelem)); | |
576f946d | 1640 | |
1641 | cgelem->subsystem = strdup(subkey); | |
1642 | cgelem->value = strdup(value); | |
bf83c5b9 MS |
1643 | |
1644 | if (!cgelem->subsystem || !cgelem->value) | |
1645 | goto out; | |
1646 | ||
576f946d | 1647 | cglist->elem = cgelem; |
1648 | ||
94d12f0a | 1649 | lxc_list_add_tail(&lxc_conf->cgroup, cglist); |
576f946d | 1650 | |
1651 | return 0; | |
bf83c5b9 MS |
1652 | |
1653 | out: | |
f10fad2f | 1654 | free(cglist); |
bf83c5b9 MS |
1655 | |
1656 | if (cgelem) { | |
f10fad2f | 1657 | free(cgelem->subsystem); |
bf83c5b9 | 1658 | |
f10fad2f | 1659 | free(cgelem->value); |
bf83c5b9 MS |
1660 | |
1661 | free(cgelem); | |
1662 | } | |
1663 | ||
1664 | return -1; | |
576f946d | 1665 | } |
1666 | ||
c6d09e15 WB |
1667 | static bool parse_limit_value(const char **value, unsigned long *res) { |
1668 | char *endptr = NULL; | |
1669 | ||
1670 | if (strncmp(*value, "unlimited", sizeof("unlimited")-1) == 0) { | |
1671 | *res = RLIM_INFINITY; | |
1672 | *value += sizeof("unlimited")-1; | |
1673 | return true; | |
1674 | } | |
1675 | ||
1676 | errno = 0; | |
1677 | *res = strtoul(*value, &endptr, 10); | |
1678 | if (errno || !endptr) | |
1679 | return false; | |
1680 | *value = endptr; | |
1681 | ||
1682 | return true; | |
1683 | } | |
1684 | ||
1685 | static int config_limit(const char *key, const char *value, | |
1686 | struct lxc_conf *lxc_conf) | |
1687 | { | |
1688 | struct lxc_list *limlist = NULL; | |
1689 | struct lxc_limit *limelem = NULL; | |
1690 | struct lxc_list *iter; | |
1691 | struct rlimit limit; | |
1692 | unsigned long limit_value; | |
1693 | ||
7a18f483 | 1694 | if (config_value_empty(value)) |
c6d09e15 WB |
1695 | return lxc_clear_limits(lxc_conf, key); |
1696 | ||
1697 | if (strncmp(key, "lxc.limit.", sizeof("lxc.limit.")-1) != 0) | |
1698 | return -1; | |
1699 | ||
1700 | key += sizeof("lxc.limit.")-1; | |
1701 | ||
1702 | /* soft limit comes first in the value */ | |
1703 | if (!parse_limit_value(&value, &limit_value)) | |
1704 | return -1; | |
1705 | limit.rlim_cur = limit_value; | |
1706 | ||
1707 | /* skip spaces and a colon */ | |
1708 | while (isspace(*value)) | |
1709 | ++value; | |
1710 | if (*value == ':') | |
1711 | ++value; | |
1712 | else if (*value) /* any other character is an error here */ | |
1713 | return -1; | |
1714 | while (isspace(*value)) | |
1715 | ++value; | |
1716 | ||
1717 | /* optional hard limit */ | |
1718 | if (*value) { | |
1719 | if (!parse_limit_value(&value, &limit_value)) | |
1720 | return -1; | |
1721 | limit.rlim_max = limit_value; | |
1722 | /* check for trailing garbage */ | |
1723 | while (isspace(*value)) | |
1724 | ++value; | |
1725 | if (*value) | |
1726 | return -1; | |
1727 | } else { | |
1728 | /* a single value sets both hard and soft limit */ | |
1729 | limit.rlim_max = limit.rlim_cur; | |
1730 | } | |
1731 | ||
1732 | /* find existing list element */ | |
1733 | lxc_list_for_each(iter, &lxc_conf->limits) { | |
1734 | limelem = iter->elem; | |
1735 | if (!strcmp(key, limelem->resource)) { | |
1736 | limelem->limit = limit; | |
1737 | return 0; | |
1738 | } | |
1739 | } | |
1740 | ||
1741 | /* allocate list element */ | |
1742 | limlist = malloc(sizeof(*limlist)); | |
1743 | if (!limlist) | |
1744 | goto out; | |
2e6e3feb | 1745 | |
c6d09e15 WB |
1746 | limelem = malloc(sizeof(*limelem)); |
1747 | if (!limelem) | |
1748 | goto out; | |
1749 | memset(limelem, 0, sizeof(*limelem)); | |
1750 | ||
1751 | limelem->resource = strdup(key); | |
1752 | if (!limelem->resource) | |
1753 | goto out; | |
1754 | limelem->limit = limit; | |
1755 | ||
1756 | limlist->elem = limelem; | |
1757 | ||
1758 | lxc_list_add_tail(&lxc_conf->limits, limlist); | |
1759 | ||
1760 | return 0; | |
1761 | ||
1762 | out: | |
1763 | free(limlist); | |
1764 | if (limelem) { | |
1765 | free(limelem->resource); | |
1766 | free(limelem); | |
1767 | } | |
1768 | return -1; | |
1769 | } | |
1770 | ||
f6d3e3e4 SH |
1771 | static int config_idmap(const char *key, const char *value, struct lxc_conf *lxc_conf) |
1772 | { | |
1773 | char *token = "lxc.id_map"; | |
1774 | char *subkey; | |
1775 | struct lxc_list *idmaplist = NULL; | |
1776 | struct id_map *idmap = NULL; | |
251d0d2a | 1777 | unsigned long hostid, nsid, range; |
f6d3e3e4 SH |
1778 | char type; |
1779 | int ret; | |
1780 | ||
ab8d8307 | 1781 | if (config_value_empty(value)) |
7d0eb87e SH |
1782 | return lxc_clear_idmaps(lxc_conf); |
1783 | ||
f6d3e3e4 SH |
1784 | subkey = strstr(key, token); |
1785 | ||
1786 | if (!subkey) | |
1787 | return -1; | |
1788 | ||
1789 | if (!strlen(subkey)) | |
1790 | return -1; | |
1791 | ||
1792 | idmaplist = malloc(sizeof(*idmaplist)); | |
1793 | if (!idmaplist) | |
1794 | goto out; | |
1795 | ||
1796 | idmap = malloc(sizeof(*idmap)); | |
1797 | if (!idmap) | |
1798 | goto out; | |
1799 | memset(idmap, 0, sizeof(*idmap)); | |
1800 | ||
251d0d2a | 1801 | ret = sscanf(value, "%c %lu %lu %lu", &type, &nsid, &hostid, &range); |
f6d3e3e4 SH |
1802 | if (ret != 4) |
1803 | goto out; | |
7e60c3f0 | 1804 | |
251d0d2a | 1805 | INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range); |
ac7725e7 | 1806 | if (type == 'u') |
f6d3e3e4 | 1807 | idmap->idtype = ID_TYPE_UID; |
ac7725e7 | 1808 | else if (type == 'g') |
f6d3e3e4 SH |
1809 | idmap->idtype = ID_TYPE_GID; |
1810 | else | |
1811 | goto out; | |
7e60c3f0 | 1812 | |
f6d3e3e4 SH |
1813 | idmap->hostid = hostid; |
1814 | idmap->nsid = nsid; | |
1815 | idmap->range = range; | |
1816 | ||
7e60c3f0 SG |
1817 | idmaplist->elem = idmap; |
1818 | lxc_list_add_tail(&lxc_conf->id_map, idmaplist); | |
1819 | ||
f6d3e3e4 SH |
1820 | return 0; |
1821 | ||
1822 | out: | |
f10fad2f | 1823 | free(idmaplist); |
f6d3e3e4 SH |
1824 | |
1825 | if (idmap) { | |
1826 | free(idmap); | |
1827 | } | |
1828 | ||
1829 | return -1; | |
1830 | } | |
1831 | ||
d95db067 DE |
1832 | static int config_fstab(const char *key, const char *value, |
1833 | struct lxc_conf *lxc_conf) | |
1834 | { | |
6f5685f0 | 1835 | if (config_value_empty(value)) |
d9192f5d | 1836 | return -1; |
6f5685f0 | 1837 | |
6d03d92a | 1838 | return config_path_item(&lxc_conf->fstab, value); |
d95db067 DE |
1839 | } |
1840 | ||
368bbc02 CS |
1841 | static int config_mount_auto(const char *key, const char *value, |
1842 | struct lxc_conf *lxc_conf) | |
1843 | { | |
1844 | char *autos, *autoptr, *sptr, *token; | |
b06b8511 | 1845 | static struct { const char *token; int mask; int flag; } allowed_auto_mounts[] = { |
0769b82a CS |
1846 | { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED }, |
1847 | { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED }, | |
1848 | { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW }, | |
f24a52d5 | 1849 | { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED }, |
0769b82a | 1850 | { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO }, |
f24a52d5 | 1851 | { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED }, |
0769b82a CS |
1852 | { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW }, |
1853 | { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC }, | |
1854 | { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED }, | |
1855 | { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO }, | |
1856 | { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW }, | |
1857 | { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC }, | |
1858 | { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED }, | |
1859 | { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO }, | |
1860 | { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW }, | |
70f55bfd | 1861 | /* NB: For adding anything that is just a single on/off, but has |
b06b8511 CS |
1862 | * no options: keep mask and flag identical and just define the |
1863 | * enum value as an unused bit so far | |
1864 | */ | |
368bbc02 CS |
1865 | { NULL, 0 } |
1866 | }; | |
1867 | int i; | |
1868 | int ret = -1; | |
1869 | ||
ede57f73 | 1870 | if (config_value_empty(value)) { |
d9192f5d SH |
1871 | lxc_conf->auto_mounts = 0; |
1872 | return 0; | |
1873 | } | |
368bbc02 CS |
1874 | |
1875 | autos = strdup(value); | |
1876 | if (!autos) { | |
1877 | SYSERROR("failed to dup '%s'", value); | |
1878 | return -1; | |
1879 | } | |
1880 | ||
1881 | for (autoptr = autos; ; autoptr = NULL) { | |
d028235d SG |
1882 | token = strtok_r(autoptr, " \t", &sptr); |
1883 | if (!token) { | |
368bbc02 | 1884 | ret = 0; |
d028235d | 1885 | break; |
368bbc02 CS |
1886 | } |
1887 | ||
1888 | for (i = 0; allowed_auto_mounts[i].token; i++) { | |
1889 | if (!strcmp(allowed_auto_mounts[i].token, token)) | |
1890 | break; | |
1891 | } | |
1892 | ||
1893 | if (!allowed_auto_mounts[i].token) { | |
1894 | ERROR("Invalid filesystem to automount: %s", token); | |
1895 | break; | |
1896 | } | |
1897 | ||
b06b8511 | 1898 | lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask; |
368bbc02 | 1899 | lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag; |
d028235d | 1900 | } |
368bbc02 CS |
1901 | |
1902 | free(autos); | |
1903 | ||
1904 | return ret; | |
1905 | } | |
1906 | ||
12a50cc6 DE |
1907 | static int config_mount(const char *key, const char *value, |
1908 | struct lxc_conf *lxc_conf) | |
e7938e9e | 1909 | { |
e7938e9e MN |
1910 | char *mntelem; |
1911 | struct lxc_list *mntlist; | |
1912 | ||
67cbe21a | 1913 | if (config_value_empty(value)) |
d9192f5d | 1914 | return lxc_clear_mount_entries(lxc_conf); |
e7938e9e MN |
1915 | |
1916 | mntlist = malloc(sizeof(*mntlist)); | |
1917 | if (!mntlist) | |
1918 | return -1; | |
1919 | ||
1920 | mntelem = strdup(value); | |
00b6be44 SH |
1921 | if (!mntelem) { |
1922 | free(mntlist); | |
bf83c5b9 | 1923 | return -1; |
00b6be44 | 1924 | } |
e7938e9e MN |
1925 | mntlist->elem = mntelem; |
1926 | ||
1927 | lxc_list_add_tail(&lxc_conf->mount_list, mntlist); | |
1928 | ||
1929 | return 0; | |
1930 | } | |
1931 | ||
1fb86a7c SH |
1932 | static int config_cap_keep(const char *key, const char *value, |
1933 | struct lxc_conf *lxc_conf) | |
1934 | { | |
1935 | char *keepcaps, *keepptr, *sptr, *token; | |
1936 | struct lxc_list *keeplist; | |
1937 | int ret = -1; | |
1938 | ||
99feb7f1 | 1939 | if (config_value_empty(value)) |
7d0eb87e | 1940 | return lxc_clear_config_keepcaps(lxc_conf); |
1fb86a7c SH |
1941 | |
1942 | keepcaps = strdup(value); | |
1943 | if (!keepcaps) { | |
1944 | SYSERROR("failed to dup '%s'", value); | |
1945 | return -1; | |
1946 | } | |
1947 | ||
1948 | /* in case several capability keep is specified in a single line | |
1949 | * split these caps in a single element for the list */ | |
1950 | for (keepptr = keepcaps;;keepptr = NULL) { | |
d028235d SG |
1951 | token = strtok_r(keepptr, " \t", &sptr); |
1952 | if (!token) { | |
1fb86a7c | 1953 | ret = 0; |
d028235d | 1954 | break; |
1fb86a7c SH |
1955 | } |
1956 | ||
7035407c DE |
1957 | if (!strcmp(token, "none")) |
1958 | lxc_clear_config_keepcaps(lxc_conf); | |
1959 | ||
1fb86a7c SH |
1960 | keeplist = malloc(sizeof(*keeplist)); |
1961 | if (!keeplist) { | |
1962 | SYSERROR("failed to allocate keepcap list"); | |
1963 | break; | |
1964 | } | |
1965 | ||
1966 | keeplist->elem = strdup(token); | |
1967 | if (!keeplist->elem) { | |
1968 | SYSERROR("failed to dup '%s'", token); | |
1969 | free(keeplist); | |
1970 | break; | |
1971 | } | |
1972 | ||
1973 | lxc_list_add_tail(&lxc_conf->keepcaps, keeplist); | |
d028235d | 1974 | } |
1fb86a7c SH |
1975 | |
1976 | free(keepcaps); | |
1977 | ||
1978 | return ret; | |
1979 | } | |
1980 | ||
12a50cc6 | 1981 | static int config_cap_drop(const char *key, const char *value, |
81810dd1 DL |
1982 | struct lxc_conf *lxc_conf) |
1983 | { | |
d95db067 | 1984 | char *dropcaps, *dropptr, *sptr, *token; |
81810dd1 DL |
1985 | struct lxc_list *droplist; |
1986 | int ret = -1; | |
1987 | ||
b2fae748 | 1988 | if (config_value_empty(value)) |
7d0eb87e | 1989 | return lxc_clear_config_caps(lxc_conf); |
81810dd1 DL |
1990 | |
1991 | dropcaps = strdup(value); | |
1992 | if (!dropcaps) { | |
1993 | SYSERROR("failed to dup '%s'", value); | |
1994 | return -1; | |
1995 | } | |
1996 | ||
1997 | /* in case several capability drop is specified in a single line | |
1998 | * split these caps in a single element for the list */ | |
d95db067 | 1999 | for (dropptr = dropcaps;;dropptr = NULL) { |
d028235d SG |
2000 | token = strtok_r(dropptr, " \t", &sptr); |
2001 | if (!token) { | |
81810dd1 | 2002 | ret = 0; |
d028235d | 2003 | break; |
81810dd1 | 2004 | } |
81810dd1 DL |
2005 | |
2006 | droplist = malloc(sizeof(*droplist)); | |
2007 | if (!droplist) { | |
2008 | SYSERROR("failed to allocate drop list"); | |
2009 | break; | |
2010 | } | |
2011 | ||
2012 | droplist->elem = strdup(token); | |
2013 | if (!droplist->elem) { | |
2014 | SYSERROR("failed to dup '%s'", token); | |
2015 | free(droplist); | |
2016 | break; | |
2017 | } | |
2018 | ||
2019 | lxc_list_add_tail(&lxc_conf->caps, droplist); | |
d028235d | 2020 | } |
81810dd1 DL |
2021 | |
2022 | free(dropcaps); | |
2023 | ||
2024 | return ret; | |
2025 | } | |
2026 | ||
12a50cc6 | 2027 | static int config_console(const char *key, const char *value, |
28a4b0e5 DL |
2028 | struct lxc_conf *lxc_conf) |
2029 | { | |
6d03d92a | 2030 | return config_path_item(&lxc_conf->console.path, value); |
28a4b0e5 DL |
2031 | } |
2032 | ||
96f15ca1 SH |
2033 | static int config_console_logfile(const char *key, const char *value, |
2034 | struct lxc_conf *lxc_conf) | |
2035 | { | |
2036 | return config_path_item(&lxc_conf->console.log_path, value); | |
2037 | } | |
2038 | ||
e6744e9b SH |
2039 | /* |
2040 | * If we find a lxc.network.hwaddr in the original config file, | |
2041 | * we expand it in the unexpanded_config, so that after a save_config | |
2042 | * we store the hwaddr for re-use. | |
2043 | * This is only called when reading the config file, not when executing | |
2044 | * a lxc.include. | |
2045 | * 'x' and 'X' are substituted in-place. | |
2046 | */ | |
2047 | static void update_hwaddr(const char *line) | |
2048 | { | |
2049 | char *p; | |
2050 | ||
2051 | line += lxc_char_left_gc(line, strlen(line)); | |
2052 | if (line[0] == '#') | |
2053 | return; | |
2054 | if (strncmp(line, "lxc.network.hwaddr", 18) != 0) | |
2055 | return; | |
2056 | p = strchr(line, '='); | |
2057 | if (!p) | |
2058 | return; // let config_network_hwaddr raise the error | |
2059 | p++; | |
2060 | while (isblank(*p)) | |
2061 | p++; | |
2062 | if (!*p) | |
2063 | return; | |
2064 | ||
2065 | rand_complete_hwaddr(p); | |
2066 | } | |
2067 | ||
6b0d5538 | 2068 | int append_unexp_config_line(const char *line, struct lxc_conf *conf) |
f979ac15 | 2069 | { |
6b0d5538 | 2070 | size_t len = conf->unexpanded_len, linelen = strlen(line); |
f979ac15 | 2071 | |
e6744e9b SH |
2072 | update_hwaddr(line); |
2073 | ||
6b0d5538 SH |
2074 | while (conf->unexpanded_alloced <= len + linelen + 2) { |
2075 | char *tmp = realloc(conf->unexpanded_config, conf->unexpanded_alloced + 1024); | |
2076 | if (!tmp) | |
2077 | return -1; | |
2078 | if (!conf->unexpanded_config) | |
2079 | *tmp = '\0'; | |
2080 | conf->unexpanded_config = tmp; | |
2081 | conf->unexpanded_alloced += 1024; | |
2082 | } | |
2083 | strcat(conf->unexpanded_config, line); | |
2084 | conf->unexpanded_len += linelen; | |
2085 | if (line[linelen-1] != '\n') { | |
2086 | strcat(conf->unexpanded_config, "\n"); | |
2087 | conf->unexpanded_len++; | |
f979ac15 | 2088 | } |
f979ac15 SH |
2089 | return 0; |
2090 | } | |
2091 | ||
e1daebd9 SH |
2092 | static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf) |
2093 | { | |
74f96976 | 2094 | struct dirent *direntp; |
e1daebd9 SH |
2095 | DIR *dir; |
2096 | char path[MAXPATHLEN]; | |
2097 | int ret = -1, len; | |
2098 | ||
2099 | dir = opendir(dirp); | |
2100 | if (!dir) { | |
2101 | SYSERROR("failed to open '%s'", dirp); | |
2102 | return -1; | |
2103 | } | |
2104 | ||
74f96976 | 2105 | while ((direntp = readdir(dir))) { |
e1daebd9 SH |
2106 | const char *fnam; |
2107 | if (!direntp) | |
2108 | break; | |
2109 | ||
2110 | fnam = direntp->d_name; | |
2111 | if (!strcmp(fnam, ".")) | |
2112 | continue; | |
2113 | ||
2114 | if (!strcmp(fnam, "..")) | |
2115 | continue; | |
2116 | ||
2117 | len = strlen(fnam); | |
2118 | if (len < 6 || strncmp(fnam+len-5, ".conf", 5) != 0) | |
2119 | continue; | |
2120 | len = snprintf(path, MAXPATHLEN, "%s/%s", dirp, fnam); | |
2121 | if (len < 0 || len >= MAXPATHLEN) { | |
2122 | ERROR("lxc.include filename too long under '%s'", dirp); | |
2123 | ret = -1; | |
2124 | goto out; | |
2125 | } | |
2126 | ||
2127 | ret = lxc_config_read(path, lxc_conf, true); | |
2128 | if (ret < 0) | |
2129 | goto out; | |
2130 | } | |
2131 | ret = 0; | |
2132 | ||
2133 | out: | |
2134 | if (closedir(dir)) | |
2135 | WARN("lxc.include dir: failed to close directory"); | |
2136 | ||
2137 | return ret; | |
2138 | } | |
2139 | ||
12a50cc6 | 2140 | static int config_includefile(const char *key, const char *value, |
09ad6246 SH |
2141 | struct lxc_conf *lxc_conf) |
2142 | { | |
616422f1 CB |
2143 | if (config_value_empty(value)) |
2144 | return 0; | |
2145 | ||
e1daebd9 SH |
2146 | if (is_dir(value)) |
2147 | return do_includedir(value, lxc_conf); | |
2148 | ||
6b0d5538 | 2149 | return lxc_config_read(value, lxc_conf, true); |
09ad6246 SH |
2150 | } |
2151 | ||
12a50cc6 DE |
2152 | static int config_rootfs(const char *key, const char *value, |
2153 | struct lxc_conf *lxc_conf) | |
c2cc9f0a | 2154 | { |
6d03d92a | 2155 | return config_path_item(&lxc_conf->rootfs.path, value); |
c2cc9f0a | 2156 | } |
2157 | ||
12a50cc6 DE |
2158 | static int config_rootfs_mount(const char *key, const char *value, |
2159 | struct lxc_conf *lxc_conf) | |
23b7ea69 | 2160 | { |
6d03d92a | 2161 | return config_path_item(&lxc_conf->rootfs.mount, value); |
23b7ea69 DL |
2162 | } |
2163 | ||
a17b1e65 SG |
2164 | static int config_rootfs_options(const char *key, const char *value, |
2165 | struct lxc_conf *lxc_conf) | |
2166 | { | |
2167 | return config_string_item(&lxc_conf->rootfs.options, value); | |
2168 | } | |
2169 | ||
327a1e78 | 2170 | static int config_rootfs_backend(const char *key, const char *value, |
b8223439 | 2171 | struct lxc_conf *lxc_conf) |
bfd77214 | 2172 | { |
b8223439 | 2173 | if (config_value_empty(value)) { |
6a21d4ae SH |
2174 | free(lxc_conf->rootfs.bdev_type); |
2175 | lxc_conf->rootfs.bdev_type = NULL; | |
b8223439 | 2176 | return 0; |
6a21d4ae | 2177 | } |
b8223439 | 2178 | |
bfd77214 | 2179 | if (!is_valid_bdev_type(value)) { |
327a1e78 | 2180 | ERROR("Bad rootfs.backend: '%s'", value); |
bfd77214 SH |
2181 | return -1; |
2182 | } | |
2183 | ||
b3b8c97f | 2184 | return config_string_item(&lxc_conf->rootfs.bdev_type, value); |
bfd77214 SH |
2185 | } |
2186 | ||
12a50cc6 DE |
2187 | static int config_pivotdir(const char *key, const char *value, |
2188 | struct lxc_conf *lxc_conf) | |
bf601689 | 2189 | { |
2d489f9e | 2190 | WARN("lxc.pivotdir is ignored. It will soon become an error."); |
59bb8698 | 2191 | return 0; |
bf601689 MH |
2192 | } |
2193 | ||
12a50cc6 DE |
2194 | static int config_utsname(const char *key, const char *value, |
2195 | struct lxc_conf *lxc_conf) | |
c2cc9f0a | 2196 | { |
2197 | struct utsname *utsname; | |
2198 | ||
1939e73d CB |
2199 | if (config_value_empty(value)) |
2200 | return 0; | |
2201 | ||
c2cc9f0a | 2202 | utsname = malloc(sizeof(*utsname)); |
2203 | if (!utsname) { | |
36eb9bde | 2204 | SYSERROR("failed to allocate memory"); |
c2cc9f0a | 2205 | return -1; |
2206 | } | |
2207 | ||
2208 | if (strlen(value) >= sizeof(utsname->nodename)) { | |
36eb9bde | 2209 | ERROR("node name '%s' is too long", |
16e29c91 | 2210 | value); |
b6f24d54 | 2211 | free(utsname); |
c2cc9f0a | 2212 | return -1; |
2213 | } | |
2214 | ||
2215 | strcpy(utsname->nodename, value); | |
f10fad2f | 2216 | free(lxc_conf->utsname); |
c2cc9f0a | 2217 | lxc_conf->utsname = utsname; |
2218 | ||
2219 | return 0; | |
2220 | } | |
2221 | ||
6b0d5538 SH |
2222 | struct parse_line_conf { |
2223 | struct lxc_conf *conf; | |
2224 | bool from_include; | |
2225 | }; | |
4184c3e1 | 2226 | |
7a7ff0c6 | 2227 | static int parse_line(char *buffer, void *data) |
c2cc9f0a | 2228 | { |
72d0e1cb | 2229 | struct lxc_config_t *config; |
81192358 | 2230 | char *line, *linep; |
c2cc9f0a | 2231 | char *dot; |
2232 | char *key; | |
2233 | char *value; | |
476d4cf1 | 2234 | int ret = 0; |
6b0d5538 | 2235 | struct parse_line_conf *plc = data; |
c2cc9f0a | 2236 | |
91480a0f | 2237 | if (lxc_is_line_empty(buffer)) |
c2cc9f0a | 2238 | return 0; |
2239 | ||
81192358 DL |
2240 | /* we have to dup the buffer otherwise, at the re-exec for |
2241 | * reboot we modified the original string on the stack by | |
2242 | * replacing '=' by '\0' below | |
91480a0f | 2243 | */ |
81192358 | 2244 | linep = line = strdup(buffer); |
91480a0f DL |
2245 | if (!line) { |
2246 | SYSERROR("failed to allocate memory for '%s'", buffer); | |
81192358 | 2247 | return -1; |
91480a0f DL |
2248 | } |
2249 | ||
6b0d5538 SH |
2250 | if (!plc->from_include) |
2251 | if ((ret = append_unexp_config_line(line, plc->conf))) | |
2252 | goto out; | |
2253 | ||
b2718c72 | 2254 | line += lxc_char_left_gc(line, strlen(line)); |
476d4cf1 | 2255 | |
4184c3e1 SH |
2256 | /* ignore comments */ |
2257 | if (line[0] == '#') | |
91480a0f | 2258 | goto out; |
476d4cf1 | 2259 | |
6b0d5538 SH |
2260 | /* martian option - don't add it to the config itself */ |
2261 | if (strncmp(line, "lxc.", 4)) | |
4184c3e1 | 2262 | goto out; |
4184c3e1 | 2263 | |
476d4cf1 | 2264 | ret = -1; |
c2cc9f0a | 2265 | |
b2718c72 | 2266 | dot = strstr(line, "="); |
c2cc9f0a | 2267 | if (!dot) { |
36eb9bde | 2268 | ERROR("invalid configuration line: %s", line); |
91480a0f | 2269 | goto out; |
c2cc9f0a | 2270 | } |
a871ff6b | 2271 | |
c2cc9f0a | 2272 | *dot = '\0'; |
2273 | value = dot + 1; | |
2274 | ||
b2718c72 | 2275 | key = line; |
2276 | key[lxc_char_right_gc(key, strlen(key))] = '\0'; | |
c2cc9f0a | 2277 | |
b2718c72 | 2278 | value += lxc_char_left_gc(value, strlen(value)); |
2279 | value[lxc_char_right_gc(value, strlen(value))] = '\0'; | |
c2cc9f0a | 2280 | |
bd878dee SB |
2281 | if (*value == '\'' || *value == '\"') { |
2282 | size_t len = strlen(value); | |
2283 | if (len > 1 && value[len-1] == *value) { | |
2284 | value[len-1] = '\0'; | |
2285 | value++; | |
2286 | } | |
2287 | } | |
2288 | ||
72d0e1cb | 2289 | config = lxc_getconfig(key); |
c2cc9f0a | 2290 | if (!config) { |
6e1d9b94 | 2291 | ERROR("unknown key %s", key); |
91480a0f | 2292 | goto out; |
c2cc9f0a | 2293 | } |
2294 | ||
6b0d5538 | 2295 | ret = config->cb(key, value, plc->conf); |
91480a0f DL |
2296 | |
2297 | out: | |
81192358 | 2298 | free(linep); |
91480a0f | 2299 | return ret; |
c2cc9f0a | 2300 | } |
2301 | ||
74a3920a | 2302 | static int lxc_config_readline(char *buffer, struct lxc_conf *conf) |
af5b0155 | 2303 | { |
6b0d5538 SH |
2304 | struct parse_line_conf c; |
2305 | ||
2306 | c.conf = conf; | |
2307 | c.from_include = false; | |
2308 | ||
2309 | return parse_line(buffer, &c); | |
af5b0155 CLG |
2310 | } |
2311 | ||
6b0d5538 | 2312 | int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include) |
c2cc9f0a | 2313 | { |
6b0d5538 SH |
2314 | struct parse_line_conf c; |
2315 | ||
2316 | c.conf = conf; | |
2317 | c.from_include = from_include; | |
f979ac15 | 2318 | |
f3ca99fd SH |
2319 | if( access(file, R_OK) == -1 ) { |
2320 | return -1; | |
2321 | } | |
6b0d5538 | 2322 | |
f7bee6c6 | 2323 | /* Catch only the top level config file name in the structure */ |
76d0127f CB |
2324 | if(!conf->rcfile) |
2325 | conf->rcfile = strdup(file); | |
f979ac15 | 2326 | |
6b0d5538 | 2327 | return lxc_file_for_each_line(file, parse_line, &c); |
c2cc9f0a | 2328 | } |
62e46035 CLG |
2329 | |
2330 | int lxc_config_define_add(struct lxc_list *defines, char* arg) | |
2331 | { | |
2332 | struct lxc_list *dent; | |
2333 | ||
2334 | dent = malloc(sizeof(struct lxc_list)); | |
2335 | if (!dent) | |
2336 | return -1; | |
2337 | ||
2338 | dent->elem = arg; | |
2339 | lxc_list_add_tail(defines, dent); | |
2340 | return 0; | |
2341 | } | |
2342 | ||
226a18d6 | 2343 | int lxc_config_define_load(struct lxc_list *defines, struct lxc_conf *conf) |
62e46035 | 2344 | { |
9ebb03ad | 2345 | struct lxc_list *it,*next; |
62e46035 CLG |
2346 | int ret = 0; |
2347 | ||
2348 | lxc_list_for_each(it, defines) { | |
2349 | ret = lxc_config_readline(it->elem, conf); | |
2350 | if (ret) | |
2351 | break; | |
2352 | } | |
2353 | ||
9ebb03ad | 2354 | lxc_list_for_each_safe(it, defines, next) { |
62e46035 CLG |
2355 | lxc_list_del(it); |
2356 | free(it); | |
2357 | } | |
2358 | ||
2359 | return ret; | |
2360 | } | |
525f0002 CS |
2361 | |
2362 | signed long lxc_config_parse_arch(const char *arch) | |
2363 | { | |
6ff05e18 | 2364 | #if HAVE_SYS_PERSONALITY_H |
525f0002 CS |
2365 | struct per_name { |
2366 | char *name; | |
2367 | unsigned long per; | |
bb8d8207 | 2368 | } pername[] = { |
525f0002 | 2369 | { "x86", PER_LINUX32 }, |
bb8d8207 DE |
2370 | { "linux32", PER_LINUX32 }, |
2371 | { "i386", PER_LINUX32 }, | |
2372 | { "i486", PER_LINUX32 }, | |
2373 | { "i586", PER_LINUX32 }, | |
525f0002 | 2374 | { "i686", PER_LINUX32 }, |
bb8d8207 | 2375 | { "athlon", PER_LINUX32 }, |
e319eb34 JC |
2376 | { "mips", PER_LINUX32 }, |
2377 | { "mipsel", PER_LINUX32 }, | |
08245e8c CB |
2378 | { "ppc", PER_LINUX32 }, |
2379 | { "arm", PER_LINUX32 }, | |
2380 | { "armv7l", PER_LINUX32 }, | |
2381 | { "armhf", PER_LINUX32 }, | |
2382 | { "armel", PER_LINUX32 }, | |
2383 | { "powerpc", PER_LINUX32 }, | |
bb8d8207 | 2384 | { "linux64", PER_LINUX }, |
525f0002 CS |
2385 | { "x86_64", PER_LINUX }, |
2386 | { "amd64", PER_LINUX }, | |
e319eb34 JC |
2387 | { "mips64", PER_LINUX }, |
2388 | { "mips64el", PER_LINUX }, | |
08245e8c CB |
2389 | { "ppc64", PER_LINUX }, |
2390 | { "ppc64le", PER_LINUX }, | |
2391 | { "ppc64el", PER_LINUX }, | |
2392 | { "powerpc64", PER_LINUX }, | |
2393 | { "s390x", PER_LINUX }, | |
2394 | { "aarch64", PER_LINUX }, | |
2395 | { "arm64", PER_LINUX }, | |
525f0002 CS |
2396 | }; |
2397 | size_t len = sizeof(pername) / sizeof(pername[0]); | |
2398 | ||
84760c11 | 2399 | size_t i; |
525f0002 CS |
2400 | |
2401 | for (i = 0; i < len; i++) { | |
2402 | if (!strcmp(pername[i].name, arch)) | |
2403 | return pername[i].per; | |
2404 | } | |
6ff05e18 | 2405 | #endif |
525f0002 CS |
2406 | |
2407 | return -1; | |
2408 | } | |
72d0e1cb | 2409 | |
4d69b293 NK |
2410 | int lxc_fill_elevated_privileges(char *flaglist, int *flags) |
2411 | { | |
2412 | char *token, *saveptr = NULL; | |
2413 | int i, aflag; | |
2414 | struct { const char *token; int flag; } all_privs[] = { | |
2415 | { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP }, | |
2416 | { "CAP", LXC_ATTACH_DROP_CAPABILITIES }, | |
2417 | { "LSM", LXC_ATTACH_LSM_EXEC }, | |
2418 | { NULL, 0 } | |
2419 | }; | |
2420 | ||
2421 | if (!flaglist) { | |
2422 | /* for the sake of backward compatibility, drop all privileges | |
2423 | if none is specified */ | |
2424 | for (i = 0; all_privs[i].token; i++) { | |
d028235d | 2425 | *flags |= all_privs[i].flag; |
4d69b293 NK |
2426 | } |
2427 | return 0; | |
2428 | } | |
2429 | ||
2430 | token = strtok_r(flaglist, "|", &saveptr); | |
2431 | while (token) { | |
2432 | aflag = -1; | |
2433 | for (i = 0; all_privs[i].token; i++) { | |
2434 | if (!strcmp(all_privs[i].token, token)) | |
2435 | aflag = all_privs[i].flag; | |
2436 | } | |
2437 | if (aflag < 0) | |
2438 | return -1; | |
2439 | ||
2440 | *flags |= aflag; | |
2441 | ||
2442 | token = strtok_r(NULL, "|", &saveptr); | |
2443 | } | |
2444 | return 0; | |
2445 | } | |
2446 | ||
72d0e1cb SG |
2447 | static int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v) |
2448 | { | |
2449 | if (!retv) | |
2450 | inlen = 0; | |
2451 | else | |
2452 | memset(retv, 0, inlen); | |
2453 | return snprintf(retv, inlen, "%d", v); | |
2454 | } | |
2455 | ||
2456 | static int lxc_get_arch_entry(struct lxc_conf *c, char *retv, int inlen) | |
2457 | { | |
6ff05e18 | 2458 | int fulllen = 0; |
72d0e1cb SG |
2459 | |
2460 | if (!retv) | |
2461 | inlen = 0; | |
2462 | else | |
2463 | memset(retv, 0, inlen); | |
2464 | ||
6ff05e18 SG |
2465 | #if HAVE_SYS_PERSONALITY_H |
2466 | int len = 0; | |
2467 | ||
72d0e1cb | 2468 | switch(c->personality) { |
14622799 | 2469 | case PER_LINUX32: strprint(retv, inlen, "i686"); break; |
72d0e1cb SG |
2470 | case PER_LINUX: strprint(retv, inlen, "x86_64"); break; |
2471 | default: break; | |
2472 | } | |
6ff05e18 | 2473 | #endif |
72d0e1cb SG |
2474 | |
2475 | return fulllen; | |
2476 | } | |
2477 | ||
2478 | /* | |
2479 | * If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, | |
2480 | * then just the value(s) will be printed. Since there still could be | |
2481 | * more than one, it is newline-separated. | |
2482 | * (Maybe that's ambigous, since some values, i.e. devices.list, will | |
2483 | * already have newlines?) | |
2484 | * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, | |
2485 | * in 'lxc.cgroup.subsystem.key = value' format. | |
2486 | */ | |
12a50cc6 DE |
2487 | static int lxc_get_cgroup_entry(struct lxc_conf *c, char *retv, int inlen, |
2488 | const char *key) | |
72d0e1cb SG |
2489 | { |
2490 | int fulllen = 0, len; | |
2491 | int all = 0; | |
2492 | struct lxc_list *it; | |
2493 | ||
2494 | if (!retv) | |
2495 | inlen = 0; | |
2496 | else | |
2497 | memset(retv, 0, inlen); | |
2498 | ||
2499 | if (strcmp(key, "all") == 0) | |
2500 | all = 1; | |
2501 | ||
2502 | lxc_list_for_each(it, &c->cgroup) { | |
2503 | struct lxc_cgroup *cg = it->elem; | |
2504 | if (all) { | |
2505 | strprint(retv, inlen, "lxc.cgroup.%s = %s\n", cg->subsystem, cg->value); | |
2506 | } else if (strcmp(cg->subsystem, key) == 0) { | |
2507 | strprint(retv, inlen, "%s\n", cg->value); | |
2508 | } | |
2509 | } | |
2510 | return fulllen; | |
2511 | } | |
2512 | ||
c6d09e15 WB |
2513 | /* |
2514 | * If you ask for a specific value, i.e. lxc.limit.nofile, then just the value | |
2515 | * will be printed. If you ask for 'lxc.limit', then all limit entries will be | |
2516 | * printed, in 'lxc.limit.resource = value' format. | |
2517 | */ | |
2518 | static int lxc_get_limit_entry(struct lxc_conf *c, char *retv, int inlen, | |
2519 | const char *key) | |
2520 | { | |
2521 | int fulllen = 0, len; | |
2522 | int all = 0; | |
2523 | struct lxc_list *it; | |
2524 | ||
2525 | if (!retv) | |
2526 | inlen = 0; | |
2527 | else | |
2528 | memset(retv, 0, inlen); | |
2529 | ||
2530 | if (strcmp(key, "all") == 0) | |
2531 | all = 1; | |
2532 | ||
2533 | lxc_list_for_each(it, &c->limits) { | |
2534 | char buf[LXC_NUMSTRLEN64*2+2]; /* 2 colon separated 64 bit integers or the word 'unlimited' */ | |
2535 | int partlen; | |
2536 | struct lxc_limit *lim = it->elem; | |
2537 | ||
2538 | if (lim->limit.rlim_cur == RLIM_INFINITY) { | |
2539 | memcpy(buf, "unlimited", sizeof("unlimited")); | |
2540 | partlen = sizeof("unlimited")-1; | |
2541 | } else { | |
2e6e3feb | 2542 | partlen = sprintf(buf, "%"PRIu64, (uint64_t)lim->limit.rlim_cur); |
c6d09e15 WB |
2543 | } |
2544 | if (lim->limit.rlim_cur != lim->limit.rlim_max) { | |
2545 | if (lim->limit.rlim_max == RLIM_INFINITY) { | |
2546 | memcpy(buf+partlen, ":unlimited", sizeof(":unlimited")); | |
2547 | } else { | |
2e6e3feb | 2548 | sprintf(buf+partlen, ":%"PRIu64, (uint64_t)lim->limit.rlim_max); |
c6d09e15 WB |
2549 | } |
2550 | } | |
2551 | ||
2552 | if (all) { | |
2553 | strprint(retv, inlen, "lxc.limit.%s = %s\n", lim->resource, buf); | |
2554 | } else if (strcmp(lim->resource, key) == 0) { | |
2555 | strprint(retv, inlen, "%s", buf); | |
2556 | } | |
2557 | } | |
2558 | ||
2559 | return fulllen; | |
2560 | } | |
2561 | ||
12a50cc6 DE |
2562 | static int lxc_get_item_hooks(struct lxc_conf *c, char *retv, int inlen, |
2563 | const char *key) | |
72d0e1cb SG |
2564 | { |
2565 | char *subkey; | |
2566 | int len, fulllen = 0, found = -1; | |
2567 | struct lxc_list *it; | |
2568 | int i; | |
2569 | ||
2570 | /* "lxc.hook.mount" */ | |
46cd2845 PL |
2571 | subkey = strchr(key, '.'); |
2572 | if (subkey) subkey = strchr(subkey+1, '.'); | |
72d0e1cb SG |
2573 | if (!subkey) |
2574 | return -1; | |
2575 | subkey++; | |
2576 | if (!*subkey) | |
2577 | return -1; | |
2578 | for (i=0; i<NUM_LXC_HOOKS; i++) { | |
2579 | if (strcmp(lxchook_names[i], subkey) == 0) { | |
2580 | found=i; | |
2581 | break; | |
2582 | } | |
2583 | } | |
2584 | if (found == -1) | |
2585 | return -1; | |
2586 | ||
2587 | if (!retv) | |
2588 | inlen = 0; | |
2589 | else | |
2590 | memset(retv, 0, inlen); | |
2591 | ||
2592 | lxc_list_for_each(it, &c->hooks[found]) { | |
2593 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2594 | } | |
2595 | return fulllen; | |
2596 | } | |
2597 | ||
ee1e7aa0 SG |
2598 | static int lxc_get_item_groups(struct lxc_conf *c, char *retv, int inlen) |
2599 | { | |
2600 | int len, fulllen = 0; | |
2601 | struct lxc_list *it; | |
2602 | ||
2603 | if (!retv) | |
2604 | inlen = 0; | |
2605 | else | |
2606 | memset(retv, 0, inlen); | |
2607 | ||
2608 | lxc_list_for_each(it, &c->groups) { | |
2609 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2610 | } | |
2611 | return fulllen; | |
2612 | } | |
2613 | ||
ab799c0b SG |
2614 | static int lxc_get_item_environment(struct lxc_conf *c, char *retv, int inlen) |
2615 | { | |
2616 | int len, fulllen = 0; | |
2617 | struct lxc_list *it; | |
2618 | ||
2619 | if (!retv) | |
2620 | inlen = 0; | |
2621 | else | |
2622 | memset(retv, 0, inlen); | |
2623 | ||
2624 | lxc_list_for_each(it, &c->environment) { | |
2625 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2626 | } | |
2627 | return fulllen; | |
2628 | } | |
2629 | ||
72d0e1cb SG |
2630 | static int lxc_get_item_cap_drop(struct lxc_conf *c, char *retv, int inlen) |
2631 | { | |
2632 | int len, fulllen = 0; | |
2633 | struct lxc_list *it; | |
2634 | ||
2635 | if (!retv) | |
2636 | inlen = 0; | |
2637 | else | |
2638 | memset(retv, 0, inlen); | |
2639 | ||
2640 | lxc_list_for_each(it, &c->caps) { | |
2641 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2642 | } | |
2643 | return fulllen; | |
2644 | } | |
2645 | ||
1fb86a7c SH |
2646 | static int lxc_get_item_cap_keep(struct lxc_conf *c, char *retv, int inlen) |
2647 | { | |
2648 | int len, fulllen = 0; | |
2649 | struct lxc_list *it; | |
2650 | ||
2651 | if (!retv) | |
2652 | inlen = 0; | |
2653 | else | |
2654 | memset(retv, 0, inlen); | |
2655 | ||
2656 | lxc_list_for_each(it, &c->keepcaps) { | |
2657 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2658 | } | |
2659 | return fulllen; | |
2660 | } | |
2661 | ||
72d0e1cb SG |
2662 | static int lxc_get_mount_entries(struct lxc_conf *c, char *retv, int inlen) |
2663 | { | |
2664 | int len, fulllen = 0; | |
2665 | struct lxc_list *it; | |
2666 | ||
2667 | if (!retv) | |
2668 | inlen = 0; | |
2669 | else | |
2670 | memset(retv, 0, inlen); | |
2671 | ||
2672 | lxc_list_for_each(it, &c->mount_list) { | |
2673 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2674 | } | |
2675 | return fulllen; | |
2676 | } | |
2677 | ||
b099e9e9 SH |
2678 | static int lxc_get_auto_mounts(struct lxc_conf *c, char *retv, int inlen) |
2679 | { | |
2680 | int len, fulllen = 0; | |
0769b82a | 2681 | const char *sep = ""; |
b099e9e9 SH |
2682 | |
2683 | if (!retv) | |
2684 | inlen = 0; | |
2685 | else | |
2686 | memset(retv, 0, inlen); | |
2687 | ||
2688 | if (!(c->auto_mounts & LXC_AUTO_ALL_MASK)) | |
2689 | return 0; | |
2690 | ||
2691 | switch (c->auto_mounts & LXC_AUTO_PROC_MASK) { | |
0769b82a CS |
2692 | case LXC_AUTO_PROC_MIXED: strprint(retv, inlen, "%sproc:mixed", sep); sep = " "; break; |
2693 | case LXC_AUTO_PROC_RW: strprint(retv, inlen, "%sproc:rw", sep); sep = " "; break; | |
b099e9e9 SH |
2694 | default: break; |
2695 | } | |
2696 | switch (c->auto_mounts & LXC_AUTO_SYS_MASK) { | |
0769b82a CS |
2697 | case LXC_AUTO_SYS_RO: strprint(retv, inlen, "%ssys:ro", sep); sep = " "; break; |
2698 | case LXC_AUTO_SYS_RW: strprint(retv, inlen, "%ssys:rw", sep); sep = " "; break; | |
10f27710 | 2699 | case LXC_AUTO_SYS_MIXED: strprint(retv, inlen, "%ssys:mixed", sep); sep = " "; break; |
b099e9e9 SH |
2700 | default: break; |
2701 | } | |
2702 | switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) { | |
0769b82a CS |
2703 | case LXC_AUTO_CGROUP_NOSPEC: strprint(retv, inlen, "%scgroup", sep); sep = " "; break; |
2704 | case LXC_AUTO_CGROUP_MIXED: strprint(retv, inlen, "%scgroup:mixed", sep); sep = " "; break; | |
2705 | case LXC_AUTO_CGROUP_RO: strprint(retv, inlen, "%scgroup:ro", sep); sep = " "; break; | |
2706 | case LXC_AUTO_CGROUP_RW: strprint(retv, inlen, "%scgroup:rw", sep); sep = " "; break; | |
2707 | case LXC_AUTO_CGROUP_FULL_NOSPEC: strprint(retv, inlen, "%scgroup-full", sep); sep = " "; break; | |
2708 | case LXC_AUTO_CGROUP_FULL_MIXED: strprint(retv, inlen, "%scgroup-full:mixed", sep); sep = " "; break; | |
2709 | case LXC_AUTO_CGROUP_FULL_RO: strprint(retv, inlen, "%scgroup-full:ro", sep); sep = " "; break; | |
2710 | case LXC_AUTO_CGROUP_FULL_RW: strprint(retv, inlen, "%scgroup-full:rw", sep); sep = " "; break; | |
b099e9e9 SH |
2711 | default: break; |
2712 | } | |
0769b82a | 2713 | |
b099e9e9 SH |
2714 | return fulllen; |
2715 | } | |
2716 | ||
72d0e1cb SG |
2717 | /* |
2718 | * lxc.network.0.XXX, where XXX can be: name, type, link, flags, type, | |
8fc8295a | 2719 | * macvlan.mode, veth.pair, vlan, ipv4, ipv6, script.up, hwaddr, mtu, |
9eaf8a59 | 2720 | * ipv4.gateway, ipv6.gateway. ipvX.gateway can return 'auto' instead |
72d0e1cb SG |
2721 | * of an address. ipv4 and ipv6 return lists (newline-separated). |
2722 | * things like veth.pair return '' if invalid (i.e. if called for vlan | |
2723 | * type). | |
2724 | */ | |
12a50cc6 DE |
2725 | static int lxc_get_item_nic(struct lxc_conf *c, char *retv, int inlen, |
2726 | const char *key) | |
72d0e1cb SG |
2727 | { |
2728 | char *p1; | |
fe88b9d2 | 2729 | int len, fulllen = 0; |
72d0e1cb SG |
2730 | struct lxc_netdev *netdev; |
2731 | ||
2732 | if (!retv) | |
2733 | inlen = 0; | |
2734 | else | |
2735 | memset(retv, 0, inlen); | |
2736 | ||
46cd2845 | 2737 | p1 = strchr(key, '.'); |
72d0e1cb SG |
2738 | if (!p1 || *(p1+1) == '\0') return -1; |
2739 | p1++; | |
2740 | ||
2741 | netdev = get_netdev_from_key(key, &c->network); | |
2742 | if (!netdev) | |
2743 | return -1; | |
2744 | if (strcmp(p1, "name") == 0) { | |
2745 | if (netdev->name) | |
2746 | strprint(retv, inlen, "%s", netdev->name); | |
2747 | } else if (strcmp(p1, "type") == 0) { | |
2748 | strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type)); | |
2749 | } else if (strcmp(p1, "link") == 0) { | |
2750 | if (netdev->link) | |
2751 | strprint(retv, inlen, "%s", netdev->link); | |
2752 | } else if (strcmp(p1, "flags") == 0) { | |
2753 | if (netdev->flags & IFF_UP) | |
2754 | strprint(retv, inlen, "up"); | |
8fc8295a | 2755 | } else if (strcmp(p1, "script.up") == 0) { |
72d0e1cb SG |
2756 | if (netdev->upscript) |
2757 | strprint(retv, inlen, "%s", netdev->upscript); | |
8fc8295a DE |
2758 | } else if (strcmp(p1, "script.down") == 0) { |
2759 | if (netdev->downscript) | |
2760 | strprint(retv, inlen, "%s", netdev->downscript); | |
72d0e1cb SG |
2761 | } else if (strcmp(p1, "hwaddr") == 0) { |
2762 | if (netdev->hwaddr) | |
2763 | strprint(retv, inlen, "%s", netdev->hwaddr); | |
2764 | } else if (strcmp(p1, "mtu") == 0) { | |
2765 | if (netdev->mtu) | |
2766 | strprint(retv, inlen, "%s", netdev->mtu); | |
2767 | } else if (strcmp(p1, "macvlan.mode") == 0) { | |
2768 | if (netdev->type == LXC_NET_MACVLAN) { | |
2769 | const char *mode; | |
2770 | switch (netdev->priv.macvlan_attr.mode) { | |
2771 | case MACVLAN_MODE_PRIVATE: mode = "private"; break; | |
2772 | case MACVLAN_MODE_VEPA: mode = "vepa"; break; | |
2773 | case MACVLAN_MODE_BRIDGE: mode = "bridge"; break; | |
99854161 | 2774 | case MACVLAN_MODE_PASSTHRU: mode = "passthru"; break; |
72d0e1cb SG |
2775 | default: mode = "(invalid)"; break; |
2776 | } | |
2777 | strprint(retv, inlen, "%s", mode); | |
2778 | } | |
2779 | } else if (strcmp(p1, "veth.pair") == 0) { | |
11029c02 DE |
2780 | if (netdev->type == LXC_NET_VETH) { |
2781 | strprint(retv, inlen, "%s", | |
2782 | netdev->priv.veth_attr.pair ? | |
2783 | netdev->priv.veth_attr.pair : | |
2784 | netdev->priv.veth_attr.veth1); | |
2785 | } | |
72d0e1cb SG |
2786 | } else if (strcmp(p1, "vlan") == 0) { |
2787 | if (netdev->type == LXC_NET_VLAN) { | |
2788 | strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid); | |
2789 | } | |
9eaf8a59 | 2790 | } else if (strcmp(p1, "ipv4.gateway") == 0) { |
72d0e1cb SG |
2791 | if (netdev->ipv4_gateway_auto) { |
2792 | strprint(retv, inlen, "auto"); | |
2793 | } else if (netdev->ipv4_gateway) { | |
2794 | char buf[INET_ADDRSTRLEN]; | |
2795 | inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf)); | |
2796 | strprint(retv, inlen, "%s", buf); | |
2797 | } | |
2798 | } else if (strcmp(p1, "ipv4") == 0) { | |
2799 | struct lxc_list *it2; | |
2800 | lxc_list_for_each(it2, &netdev->ipv4) { | |
2801 | struct lxc_inetdev *i = it2->elem; | |
2802 | char buf[INET_ADDRSTRLEN]; | |
2803 | inet_ntop(AF_INET, &i->addr, buf, sizeof(buf)); | |
28417b5a | 2804 | strprint(retv, inlen, "%s/%d\n", buf, i->prefix); |
72d0e1cb | 2805 | } |
9eaf8a59 | 2806 | } else if (strcmp(p1, "ipv6.gateway") == 0) { |
72d0e1cb SG |
2807 | if (netdev->ipv6_gateway_auto) { |
2808 | strprint(retv, inlen, "auto"); | |
2809 | } else if (netdev->ipv6_gateway) { | |
6afb165d AN |
2810 | char buf[INET6_ADDRSTRLEN]; |
2811 | inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf)); | |
72d0e1cb SG |
2812 | strprint(retv, inlen, "%s", buf); |
2813 | } | |
2814 | } else if (strcmp(p1, "ipv6") == 0) { | |
2815 | struct lxc_list *it2; | |
2816 | lxc_list_for_each(it2, &netdev->ipv6) { | |
6afb165d AN |
2817 | struct lxc_inet6dev *i = it2->elem; |
2818 | char buf[INET6_ADDRSTRLEN]; | |
72d0e1cb | 2819 | inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf)); |
28417b5a | 2820 | strprint(retv, inlen, "%s/%d\n", buf, i->prefix); |
72d0e1cb SG |
2821 | } |
2822 | } | |
2823 | return fulllen; | |
2824 | } | |
2825 | ||
2826 | static int lxc_get_item_network(struct lxc_conf *c, char *retv, int inlen) | |
2827 | { | |
2828 | int len, fulllen = 0; | |
2829 | struct lxc_list *it; | |
2830 | ||
2831 | if (!retv) | |
2832 | inlen = 0; | |
2833 | else | |
2834 | memset(retv, 0, inlen); | |
2835 | ||
2836 | lxc_list_for_each(it, &c->network) { | |
2837 | struct lxc_netdev *n = it->elem; | |
2838 | const char *t = lxc_net_type_to_str(n->type); | |
2839 | strprint(retv, inlen, "%s\n", t ? t : "(invalid)"); | |
2840 | } | |
2841 | return fulllen; | |
2842 | } | |
2843 | ||
12a50cc6 DE |
2844 | int lxc_get_config_item(struct lxc_conf *c, const char *key, char *retv, |
2845 | int inlen) | |
72d0e1cb | 2846 | { |
4a85ce2a | 2847 | const char *v = NULL; |
72d0e1cb SG |
2848 | |
2849 | if (strcmp(key, "lxc.mount.entry") == 0) | |
2850 | return lxc_get_mount_entries(c, retv, inlen); | |
b099e9e9 SH |
2851 | else if (strcmp(key, "lxc.mount.auto") == 0) |
2852 | return lxc_get_auto_mounts(c, retv, inlen); | |
72d0e1cb SG |
2853 | else if (strcmp(key, "lxc.mount") == 0) |
2854 | v = c->fstab; | |
2855 | else if (strcmp(key, "lxc.tty") == 0) | |
2856 | return lxc_get_conf_int(c, retv, inlen, c->tty); | |
2857 | else if (strcmp(key, "lxc.pts") == 0) | |
2858 | return lxc_get_conf_int(c, retv, inlen, c->pts); | |
2859 | else if (strcmp(key, "lxc.devttydir") == 0) | |
2860 | v = c->ttydir; | |
2861 | else if (strcmp(key, "lxc.arch") == 0) | |
2862 | return lxc_get_arch_entry(c, retv, inlen); | |
2863 | else if (strcmp(key, "lxc.aa_profile") == 0) | |
fe4de9a6 | 2864 | v = c->lsm_aa_profile; |
7aff4f43 SH |
2865 | else if (strcmp(key, "lxc.aa_allow_incomplete") == 0) |
2866 | return lxc_get_conf_int(c, retv, inlen, c->lsm_aa_allow_incomplete); | |
fe4de9a6 DE |
2867 | else if (strcmp(key, "lxc.se_context") == 0) |
2868 | v = c->lsm_se_context; | |
4a85ce2a | 2869 | else if (strcmp(key, "lxc.logfile") == 0) |
858377e4 | 2870 | v = c->logfile; |
4a85ce2a | 2871 | else if (strcmp(key, "lxc.loglevel") == 0) |
858377e4 | 2872 | v = lxc_log_priority_to_string(c->loglevel); |
72d0e1cb SG |
2873 | else if (strcmp(key, "lxc.cgroup") == 0) // all cgroup info |
2874 | return lxc_get_cgroup_entry(c, retv, inlen, "all"); | |
2875 | else if (strncmp(key, "lxc.cgroup.", 11) == 0) // specific cgroup info | |
2876 | return lxc_get_cgroup_entry(c, retv, inlen, key + 11); | |
2877 | else if (strcmp(key, "lxc.utsname") == 0) | |
64fca455 | 2878 | v = c->utsname ? c->utsname->nodename : NULL; |
96f15ca1 SH |
2879 | else if (strcmp(key, "lxc.console.logfile") == 0) |
2880 | v = c->console.log_path; | |
72d0e1cb SG |
2881 | else if (strcmp(key, "lxc.console") == 0) |
2882 | v = c->console.path; | |
2883 | else if (strcmp(key, "lxc.rootfs.mount") == 0) | |
2884 | v = c->rootfs.mount; | |
327a1e78 | 2885 | else if (strcmp(key, "lxc.rootfs.backend") == 0) |
6a21d4ae | 2886 | v = c->rootfs.bdev_type; |
a17b1e65 SG |
2887 | else if (strcmp(key, "lxc.rootfs.options") == 0) |
2888 | v = c->rootfs.options; | |
72d0e1cb SG |
2889 | else if (strcmp(key, "lxc.rootfs") == 0) |
2890 | v = c->rootfs.path; | |
72d0e1cb SG |
2891 | else if (strcmp(key, "lxc.cap.drop") == 0) |
2892 | return lxc_get_item_cap_drop(c, retv, inlen); | |
1fb86a7c SH |
2893 | else if (strcmp(key, "lxc.cap.keep") == 0) |
2894 | return lxc_get_item_cap_keep(c, retv, inlen); | |
72d0e1cb SG |
2895 | else if (strncmp(key, "lxc.hook", 8) == 0) |
2896 | return lxc_get_item_hooks(c, retv, inlen, key); | |
2897 | else if (strcmp(key, "lxc.network") == 0) | |
2898 | return lxc_get_item_network(c, retv, inlen); | |
2899 | else if (strncmp(key, "lxc.network.", 12) == 0) | |
2900 | return lxc_get_item_nic(c, retv, inlen, key + 12); | |
ee1e7aa0 SG |
2901 | else if (strcmp(key, "lxc.start.auto") == 0) |
2902 | return lxc_get_conf_int(c, retv, inlen, c->start_auto); | |
2903 | else if (strcmp(key, "lxc.start.delay") == 0) | |
2904 | return lxc_get_conf_int(c, retv, inlen, c->start_delay); | |
2905 | else if (strcmp(key, "lxc.start.order") == 0) | |
2906 | return lxc_get_conf_int(c, retv, inlen, c->start_order); | |
a8dfe4e0 WB |
2907 | else if (strcmp(key, "lxc.monitor.unshare") == 0) |
2908 | return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare); | |
ee1e7aa0 SG |
2909 | else if (strcmp(key, "lxc.group") == 0) |
2910 | return lxc_get_item_groups(c, retv, inlen); | |
58e0f57d SH |
2911 | else if (strcmp(key, "lxc.seccomp") == 0) |
2912 | v = c->seccomp; | |
ab799c0b SG |
2913 | else if (strcmp(key, "lxc.environment") == 0) |
2914 | return lxc_get_item_environment(c, retv, inlen); | |
67c660d0 SG |
2915 | else if (strcmp(key, "lxc.init_cmd") == 0) |
2916 | v = c->init_cmd; | |
68d18db8 PT |
2917 | else if (strcmp(key, "lxc.init_uid") == 0) |
2918 | return lxc_get_conf_int(c, retv, inlen, c->init_uid); | |
2919 | else if (strcmp(key, "lxc.init_gid") == 0) | |
2920 | return lxc_get_conf_int(c, retv, inlen, c->init_gid); | |
8796becf CB |
2921 | else if (strcmp(key, "lxc.ephemeral") == 0) |
2922 | return lxc_get_conf_int(c, retv, inlen, c->ephemeral); | |
76d0127f CB |
2923 | else if (strcmp(key, "lxc.syslog") == 0) |
2924 | v = c->syslog; | |
5a46f283 CB |
2925 | else if (strcmp(key, "lxc.no_new_privs") == 0) |
2926 | return lxc_get_conf_int(c, retv, inlen, c->no_new_privs); | |
c6d09e15 WB |
2927 | else if (strcmp(key, "lxc.limit") == 0) // all limits |
2928 | return lxc_get_limit_entry(c, retv, inlen, "all"); | |
2929 | else if (strncmp(key, "lxc.limit.", 10) == 0) // specific limit | |
2930 | return lxc_get_limit_entry(c, retv, inlen, key + 10); | |
72d0e1cb SG |
2931 | else return -1; |
2932 | ||
2933 | if (!v) | |
2934 | return 0; | |
2935 | if (retv && inlen >= strlen(v) + 1) | |
2936 | strncpy(retv, v, strlen(v)+1); | |
2937 | return strlen(v); | |
2938 | } | |
2939 | ||
12a50cc6 | 2940 | int lxc_clear_config_item(struct lxc_conf *c, const char *key) |
72d0e1cb | 2941 | { |
c7b15d1e CB |
2942 | int ret = 0; |
2943 | ||
2944 | if (strcmp(key, "lxc.network") == 0) { | |
2945 | ret = lxc_clear_config_network(c); | |
2946 | ||
2947 | } else if (strncmp(key, "lxc.network.", 12) == 0) { | |
2948 | ret = lxc_clear_nic(c, key + 12); | |
2949 | ||
2950 | } else if (strcmp(key, "lxc.cap.drop") == 0) { | |
2951 | ret = lxc_clear_config_caps(c); | |
2952 | ||
2953 | } else if (strcmp(key, "lxc.cap.keep") == 0) { | |
2954 | ret = lxc_clear_config_keepcaps(c); | |
2955 | ||
2956 | } else if (strncmp(key, "lxc.cgroup", 10) == 0) { | |
2957 | ret = lxc_clear_cgroups(c, key); | |
2958 | ||
2959 | } else if (strcmp(key, "lxc.mount.entry") == 0) { | |
2960 | ret = lxc_clear_mount_entries(c); | |
2961 | ||
2962 | } else if (strcmp(key, "lxc.mount.auto") == 0) { | |
2963 | ret = lxc_clear_automounts(c); | |
2964 | ||
2965 | } else if (strncmp(key, "lxc.hook", 8) == 0) { | |
2966 | ret = lxc_clear_hooks(c, key); | |
2967 | ||
2968 | } else if (strncmp(key, "lxc.group", 9) == 0) { | |
2969 | ret = lxc_clear_groups(c); | |
2970 | ||
2971 | } else if (strncmp(key, "lxc.environment", 15) == 0) { | |
2972 | ret = lxc_clear_environment(c); | |
2973 | ||
2974 | } else if (strncmp(key, "lxc.id_map", 10) == 0) { | |
2975 | ret = lxc_clear_idmaps(c); | |
2976 | ||
2977 | } else if (strncmp(key, "lxc.limit", 9) == 0) { | |
2978 | ret = lxc_clear_limits(c, key); | |
2979 | ||
2980 | } else if (strcmp(key, "lxc.utsname") == 0) { | |
2981 | free(c->utsname); | |
2982 | c->utsname = NULL; | |
2983 | ||
2984 | } else if (strcmp(key, "lxc.arch") == 0) { | |
2985 | c->personality = -1; | |
2986 | ||
2987 | } else if (strcmp(key, "lxc.haltsignal") == 0) { | |
2988 | c->haltsignal = 0; | |
2989 | ||
2990 | } else if (strcmp(key, "lxc.rebootsignal") == 0) { | |
2991 | c->rebootsignal = 0; | |
2992 | ||
2993 | } else if (strcmp(key, "lxc.stopsignal") == 0) { | |
2994 | c->stopsignal = 0; | |
2995 | ||
2996 | } else if (strcmp(key, "lxc.init_cmd") == 0) { | |
2997 | free(c->init_cmd); | |
2998 | c->init_cmd = NULL; | |
2999 | ||
3000 | } else if (strcmp(key, "lxc.init_uid") == 0) { | |
3001 | c->init_uid = 0; | |
3002 | ||
3003 | } else if (strcmp(key, "lxc.init_gid") == 0) { | |
3004 | c->init_gid = 0; | |
3005 | ||
3006 | } else if (strcmp(key, "lxc.ephemeral") == 0) { | |
3007 | c->ephemeral = 0; | |
3008 | ||
3009 | } else if (strcmp(key, "lxc.console.logfile") == 0) { | |
3010 | free(c->console.log_path); | |
3011 | c->console.log_path = NULL; | |
3012 | ||
3013 | } else if (strcmp(key, "lxc.console") == 0) { | |
3014 | free(c->console.path); | |
3015 | c->console.path = NULL; | |
3016 | ||
3017 | } else if (strcmp(key, "lxc.tty") == 0) { | |
3018 | c->tty = 0; | |
3019 | ||
3020 | } else if (strcmp(key, "lxc.devttydir") == 0) { | |
3021 | free(c->ttydir); | |
3022 | c->ttydir = NULL; | |
3023 | ||
3024 | } else if (strcmp(key, "lxc.autodev") == 0) { | |
3025 | c->autodev = 1; | |
3026 | ||
3027 | } else if (strcmp(key, "lxc.kmsg") == 0) { | |
3028 | c->kmsg = 0; | |
3029 | ||
3030 | } else if (strcmp(key, "lxc.mount") == 0) { | |
3031 | free(c->fstab); | |
3032 | c->fstab = NULL; | |
3033 | ||
3034 | } else if (strcmp(key, "lxc.rootfs") == 0) { | |
3035 | free(c->rootfs.path); | |
3036 | c->rootfs.path = NULL; | |
3037 | ||
3038 | } else if (strcmp(key, "lxc.rootfs.mount") == 0) { | |
3039 | free(c->rootfs.mount); | |
3040 | c->rootfs.mount = NULL; | |
3041 | ||
3042 | } else if (strcmp(key, "lxc.rootfs.options") == 0) { | |
3043 | free(c->rootfs.options); | |
3044 | c->rootfs.options = NULL; | |
3045 | ||
3046 | } else if (strcmp(key, "lxc.rootfs.backend") == 0) { | |
3047 | free(c->rootfs.bdev_type); | |
3048 | c->rootfs.bdev_type = NULL; | |
3049 | ||
3050 | } else if (strcmp(key, "lxc.aa_profile") == 0) { | |
3051 | free(c->lsm_aa_profile); | |
3052 | c->lsm_aa_profile = NULL; | |
3053 | ||
3054 | } else if (strcmp(key, "lxc.aa_allow_incomplete") == 0) { | |
3055 | c->lsm_aa_allow_incomplete = 0; | |
3056 | ||
3057 | } else if (strcmp(key, "lxc.se_context") == 0) { | |
3058 | free(c->lsm_se_context); | |
3059 | c->lsm_se_context = NULL; | |
3060 | ||
3061 | } else if (strcmp(key, "lxc.seccomp") == 0) { | |
3062 | free(c->seccomp); | |
3063 | c->seccomp = NULL; | |
3064 | ||
3065 | } else if (strcmp(key, "lxc.loglevel") == 0) { | |
3066 | c->loglevel = LXC_LOG_PRIORITY_NOTSET; | |
3067 | ||
3068 | } else if (strcmp(key, "lxc.logfile") == 0) { | |
3069 | free(c->logfile); | |
3070 | c->logfile = NULL; | |
3071 | ||
3072 | } else if (strcmp(key, "lxc.monitor.unshare") == 0) { | |
3073 | c->monitor_unshare = 0; | |
3074 | ||
3075 | } else if (strcmp(key, "lxc.pts") == 0) { | |
3076 | c->pts = 0; | |
3077 | ||
3078 | } else if (strcmp(key, "lxc.include") == 0) { | |
3079 | lxc_clear_includes(c); | |
3080 | ||
3081 | } else if (strcmp(key, "lxc.syslog") == 0) { | |
3082 | free(c->syslog); | |
3083 | c->syslog = NULL; | |
3084 | } else { | |
3085 | ret = -1; | |
3086 | } | |
3087 | ||
3088 | return ret; | |
72d0e1cb SG |
3089 | } |
3090 | ||
3091 | /* | |
3092 | * writing out a confile. | |
3093 | */ | |
3094 | void write_config(FILE *fout, struct lxc_conf *c) | |
3095 | { | |
6b0d5538 SH |
3096 | size_t len = c->unexpanded_len; |
3097 | int ret; | |
72d0e1cb | 3098 | |
6b0d5538 SH |
3099 | if (!len) |
3100 | return; | |
3101 | ret = fwrite(c->unexpanded_config, 1, len, fout); | |
3102 | if (ret != len) | |
3103 | SYSERROR("Error writing configuration file"); | |
3104 | } | |
f979ac15 | 3105 | |
6b0d5538 SH |
3106 | bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key, const char *v) |
3107 | { | |
3108 | int ret; | |
151d2da2 CB |
3109 | size_t len; |
3110 | char *tmp; | |
4184c3e1 | 3111 | |
151d2da2 CB |
3112 | len = strlen(key) + strlen(v) + 4; |
3113 | tmp = alloca(len); | |
3114 | ||
3115 | if (config_value_empty(v)) | |
3116 | ret = snprintf(tmp, len, "%s =", key); | |
3117 | else | |
3118 | ret = snprintf(tmp, len, "%s = %s", key, v); | |
6b0d5538 SH |
3119 | if (ret < 0 || ret >= len) |
3120 | return false; | |
3121 | ||
3122 | /* Save the line verbatim into unexpanded_conf */ | |
3123 | if (append_unexp_config_line(tmp, conf)) | |
3124 | return false; | |
3125 | ||
3126 | return true; | |
3127 | } | |
3128 | ||
3129 | void clear_unexp_config_line(struct lxc_conf *conf, const char *key, bool rm_subkeys) | |
3130 | { | |
3131 | char *lstart = conf->unexpanded_config, *lend; | |
3132 | ||
3133 | if (!conf->unexpanded_config) | |
3134 | return; | |
3135 | while (*lstart) { | |
3136 | lend = strchr(lstart, '\n'); | |
3137 | char v; | |
3138 | if (!lend) | |
3139 | lend = lstart + strlen(lstart); | |
3140 | else | |
3141 | lend++; | |
3142 | if (strncmp(lstart, key, strlen(key)) != 0) { | |
3143 | lstart = lend; | |
3144 | continue; | |
5f62730e | 3145 | } |
6b0d5538 SH |
3146 | if (!rm_subkeys) { |
3147 | v = lstart[strlen(key)]; | |
3148 | if (!isspace(v) && v != '=') { | |
3149 | lstart = lend; | |
3150 | continue; | |
3151 | } | |
5f62730e | 3152 | } |
6b0d5538 SH |
3153 | conf->unexpanded_len -= (lend - lstart); |
3154 | if (*lend == '\0') { | |
3155 | *lstart = '\0'; | |
3156 | return; | |
fd986e08 | 3157 | } |
6b0d5538 | 3158 | memmove(lstart, lend, strlen(lend)+1); |
fd986e08 | 3159 | } |
6b0d5538 SH |
3160 | } |
3161 | ||
329b3625 CB |
3162 | bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath, |
3163 | const char *newpath, const char *oldname, | |
3164 | const char *newname, const char *ovldir) | |
3165 | { | |
3166 | const char *key = "lxc.mount.entry"; | |
3167 | int ret; | |
3168 | char *lstart = conf->unexpanded_config; | |
3169 | char *lend; | |
3170 | char *p; | |
3171 | char *q; | |
3172 | size_t newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2; | |
3173 | size_t olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2; | |
3174 | char *olddir = alloca(olddirlen + 1); | |
3175 | char *newdir = alloca(newdirlen + 1); | |
3176 | ||
3177 | ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath, oldname); | |
3178 | if (ret < 0 || ret >= olddirlen + 1) { | |
3179 | ERROR("Bug in %s", __func__); | |
3180 | return false; | |
3181 | } | |
3182 | ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath, newname); | |
3183 | if (ret < 0 || ret >= newdirlen + 1) { | |
3184 | ERROR("Bug in %s", __func__); | |
3185 | return false; | |
3186 | } | |
3187 | if (!conf->unexpanded_config) | |
3188 | return true; | |
3189 | while (*lstart) { | |
3190 | lend = strchr(lstart, '\n'); | |
3191 | if (!lend) | |
3192 | lend = lstart + strlen(lstart); | |
3193 | else | |
3194 | lend++; | |
3195 | if (strncmp(lstart, key, strlen(key)) != 0) | |
3196 | goto next; | |
3197 | p = strchr(lstart + strlen(key), '='); | |
3198 | if (!p) | |
3199 | goto next; | |
3200 | p++; | |
3201 | while (isblank(*p)) | |
3202 | p++; | |
3203 | if (p >= lend) | |
3204 | goto next; | |
3205 | /* Whenever an lxc.mount.entry entry is found in a line we check | |
3206 | * if the substring " overlay" or the substring " aufs" is | |
3207 | * present before doing any further work. We check for " | |
3208 | * overlay" and " aufs" since both substrings need to have at | |
3209 | * least one space before them in a valid overlay | |
3210 | * lxc.mount.entry (/A B overlay). When the space before is | |
3211 | * missing it is very likely that these substrings are part of a | |
3212 | * path or something else. (Checking q >= lend ensures that we | |
3213 | * only count matches in the current line.) */ | |
3214 | if ((!(q = strstr(p, " overlay")) || q >= lend) && (!(q = strstr(p, " aufs")) || q >= lend)) | |
3215 | goto next; | |
3216 | if (!(q = strstr(p, olddir)) || (q >= lend)) | |
3217 | goto next; | |
3218 | ||
3219 | /* replace the olddir with newdir */ | |
3220 | if (olddirlen >= newdirlen) { | |
3221 | size_t diff = olddirlen - newdirlen; | |
3222 | memcpy(q, newdir, newdirlen); | |
3223 | if (olddirlen != newdirlen) { | |
3224 | memmove(q + newdirlen, q + newdirlen + diff, | |
3225 | strlen(q) - newdirlen - diff + 1); | |
3226 | lend -= diff; | |
3227 | conf->unexpanded_len -= diff; | |
3228 | } | |
3229 | } else { | |
3230 | char *new; | |
3231 | size_t diff = newdirlen - olddirlen; | |
3232 | size_t oldlen = conf->unexpanded_len; | |
3233 | size_t newlen = oldlen + diff; | |
3234 | size_t poffset = q - conf->unexpanded_config; | |
3235 | new = realloc(conf->unexpanded_config, newlen + 1); | |
3236 | if (!new) { | |
3237 | ERROR("Out of memory"); | |
3238 | return false; | |
3239 | } | |
3240 | conf->unexpanded_len = newlen; | |
3241 | conf->unexpanded_alloced = newlen + 1; | |
3242 | new[newlen - 1] = '\0'; | |
3243 | lend = new + (lend - conf->unexpanded_config); | |
3244 | /* move over the remainder to make room for the newdir */ | |
3245 | memmove(new + poffset + newdirlen, | |
3246 | new + poffset + olddirlen, | |
3247 | oldlen - poffset - olddirlen + 1); | |
3248 | conf->unexpanded_config = new; | |
3249 | memcpy(new + poffset, newdir, newdirlen); | |
3250 | lend += diff; | |
3251 | } | |
3252 | next: | |
3253 | lstart = lend; | |
3254 | } | |
3255 | return true; | |
3256 | } | |
3257 | ||
67702c21 | 3258 | bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath, |
d546aa0e CB |
3259 | const char *newpath, const char *oldname, |
3260 | const char *newname) | |
6b0d5538 | 3261 | { |
67702c21 SH |
3262 | const char *key = "lxc.hook"; |
3263 | int ret; | |
3264 | char *lstart = conf->unexpanded_config, *lend, *p; | |
3265 | size_t newdirlen = strlen(newpath) + strlen(newname) + 1; | |
3266 | size_t olddirlen = strlen(oldpath) + strlen(oldname) + 1; | |
3267 | char *olddir = alloca(olddirlen + 1); | |
3268 | char *newdir = alloca(newdirlen + 1); | |
3269 | ||
d546aa0e CB |
3270 | ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname); |
3271 | if (ret < 0 || ret >= olddirlen + 1) { | |
67702c21 SH |
3272 | ERROR("Bug in %s", __func__); |
3273 | return false; | |
3274 | } | |
d546aa0e CB |
3275 | ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname); |
3276 | if (ret < 0 || ret >= newdirlen + 1) { | |
67702c21 SH |
3277 | ERROR("Bug in %s", __func__); |
3278 | return false; | |
3279 | } | |
3280 | if (!conf->unexpanded_config) | |
3281 | return true; | |
3282 | while (*lstart) { | |
3283 | lend = strchr(lstart, '\n'); | |
3284 | if (!lend) | |
3285 | lend = lstart + strlen(lstart); | |
3286 | else | |
3287 | lend++; | |
d546aa0e CB |
3288 | if (strncmp(lstart, key, strlen(key)) != 0) |
3289 | goto next; | |
3290 | p = strchr(lstart + strlen(key), '='); | |
3291 | if (!p) | |
3292 | goto next; | |
67702c21 SH |
3293 | p++; |
3294 | while (isblank(*p)) | |
3295 | p++; | |
d546aa0e CB |
3296 | if (p >= lend) |
3297 | goto next; | |
3298 | if (strncmp(p, olddir, strlen(olddir)) != 0) | |
3299 | goto next; | |
67702c21 SH |
3300 | /* replace the olddir with newdir */ |
3301 | if (olddirlen >= newdirlen) { | |
3302 | size_t diff = olddirlen - newdirlen; | |
3303 | memcpy(p, newdir, newdirlen); | |
3304 | if (olddirlen != newdirlen) { | |
d546aa0e CB |
3305 | memmove(p + newdirlen, p + newdirlen + diff, |
3306 | strlen(p) - newdirlen - diff + 1); | |
67702c21 SH |
3307 | lend -= diff; |
3308 | conf->unexpanded_len -= diff; | |
3309 | } | |
67702c21 SH |
3310 | } else { |
3311 | char *new; | |
3312 | size_t diff = newdirlen - olddirlen; | |
3313 | size_t oldlen = conf->unexpanded_len; | |
3314 | size_t newlen = oldlen + diff; | |
3315 | size_t poffset = p - conf->unexpanded_config; | |
d546aa0e | 3316 | new = realloc(conf->unexpanded_config, newlen + 1); |
67702c21 SH |
3317 | if (!new) { |
3318 | ERROR("Out of memory"); | |
6b0d5538 | 3319 | return false; |
67702c21 SH |
3320 | } |
3321 | conf->unexpanded_len = newlen; | |
d546aa0e CB |
3322 | conf->unexpanded_alloced = newlen + 1; |
3323 | new[newlen - 1] = '\0'; | |
67702c21 | 3324 | lend = new + (lend - conf->unexpanded_config); |
d546aa0e CB |
3325 | /* move over the remainder to make room for the newdir */ |
3326 | memmove(new + poffset + newdirlen, | |
3327 | new + poffset + olddirlen, | |
3328 | oldlen - poffset - olddirlen + 1); | |
67702c21 | 3329 | conf->unexpanded_config = new; |
d546aa0e CB |
3330 | memcpy(new + poffset, newdir, newdirlen); |
3331 | lend += diff; | |
fd986e08 | 3332 | } |
d546aa0e CB |
3333 | next: |
3334 | lstart = lend; | |
fd986e08 | 3335 | } |
6b0d5538 SH |
3336 | return true; |
3337 | } | |
3338 | ||
3339 | #define DO(cmd) { \ | |
3340 | if (!(cmd)) { \ | |
3341 | ERROR("Error writing to new config"); \ | |
3342 | return false; \ | |
3343 | } \ | |
3344 | } | |
3345 | ||
091045f8 | 3346 | static bool new_hwaddr(char *hwaddr) |
67702c21 | 3347 | { |
091045f8 CB |
3348 | int ret; |
3349 | ||
3350 | /* COMMENT(brauner): Initialize random number generator. */ | |
3351 | (void)randseed(true); | |
3352 | ||
3353 | ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255, | |
3354 | rand() % 255, rand() % 255); | |
3355 | if (ret < 0 || ret >= 18) { | |
3356 | SYSERROR("Failed to call snprintf()."); | |
3357 | return false; | |
3358 | } | |
3359 | ||
3360 | return true; | |
67702c21 SH |
3361 | } |
3362 | ||
3363 | /* | |
3364 | * This is called only from clone. | |
3365 | * We wish to update all hwaddrs in the unexpanded config file. We | |
3366 | * can't/don't want to update any which come from lxc.includes (there | |
3367 | * shouldn't be any). | |
3368 | * We can't just walk the c->lxc-conf->network list because that includes | |
3369 | * netifs from the include files. So we update the ones which we find in | |
3370 | * the unexp config file, then find the original macaddr in the | |
3371 | * conf->network, and update that to the same value. | |
3372 | */ | |
3373 | bool network_new_hwaddrs(struct lxc_conf *conf) | |
6b0d5538 SH |
3374 | { |
3375 | struct lxc_list *it; | |
3376 | ||
67702c21 SH |
3377 | const char *key = "lxc.network.hwaddr"; |
3378 | char *lstart = conf->unexpanded_config, *lend, *p, *p2; | |
6b0d5538 | 3379 | |
67702c21 SH |
3380 | if (!conf->unexpanded_config) |
3381 | return true; | |
091045f8 | 3382 | |
67702c21 SH |
3383 | while (*lstart) { |
3384 | char newhwaddr[18], oldhwaddr[17]; | |
091045f8 | 3385 | |
67702c21 SH |
3386 | lend = strchr(lstart, '\n'); |
3387 | if (!lend) | |
3388 | lend = lstart + strlen(lstart); | |
3389 | else | |
3390 | lend++; | |
091045f8 | 3391 | |
67702c21 SH |
3392 | if (strncmp(lstart, key, strlen(key)) != 0) { |
3393 | lstart = lend; | |
3394 | continue; | |
72d0e1cb | 3395 | } |
091045f8 | 3396 | |
67702c21 SH |
3397 | p = strchr(lstart+strlen(key), '='); |
3398 | if (!p) { | |
3399 | lstart = lend; | |
3400 | continue; | |
72d0e1cb | 3401 | } |
091045f8 | 3402 | |
67702c21 SH |
3403 | p++; |
3404 | while (isblank(*p)) | |
3405 | p++; | |
3406 | if (!*p) | |
3407 | return true; | |
091045f8 | 3408 | |
67702c21 SH |
3409 | p2 = p; |
3410 | while (*p2 && !isblank(*p2) && *p2 != '\n') | |
3411 | p2++; | |
3412 | if (p2-p != 17) { | |
3413 | WARN("Bad hwaddr entry"); | |
3414 | lstart = lend; | |
3415 | continue; | |
72d0e1cb | 3416 | } |
091045f8 | 3417 | |
67702c21 | 3418 | memcpy(oldhwaddr, p, 17); |
091045f8 CB |
3419 | |
3420 | if (!new_hwaddr(newhwaddr)) | |
3421 | return false; | |
3422 | ||
67702c21 SH |
3423 | memcpy(p, newhwaddr, 17); |
3424 | lxc_list_for_each(it, &conf->network) { | |
3425 | struct lxc_netdev *n = it->elem; | |
3426 | if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0) | |
3427 | memcpy(n->hwaddr, newhwaddr, 17); | |
72d0e1cb | 3428 | } |
67702c21 SH |
3429 | |
3430 | lstart = lend; | |
72d0e1cb | 3431 | } |
091045f8 | 3432 | |
6b0d5538 | 3433 | return true; |
72d0e1cb | 3434 | } |
8796becf CB |
3435 | |
3436 | static int config_ephemeral(const char *key, const char *value, | |
3437 | struct lxc_conf *lxc_conf) | |
3438 | { | |
78304622 CB |
3439 | if (config_value_empty(value)) |
3440 | return 0; | |
3441 | ||
66ffdb1a CB |
3442 | if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0) |
3443 | return -1; | |
8796becf | 3444 | |
66ffdb1a | 3445 | if (lxc_conf->ephemeral > 1) { |
8796becf CB |
3446 | ERROR("Wrong value for lxc.ephemeral. Can only be set to 0 or 1"); |
3447 | return -1; | |
8796becf CB |
3448 | } |
3449 | ||
3450 | return 0; | |
3451 | } | |
3452 | ||
64c57ea1 | 3453 | static int config_syslog(const char *key, const char *value, |
76d0127f | 3454 | struct lxc_conf *lxc_conf) |
64c57ea1 | 3455 | { |
76d0127f | 3456 | int facility; |
7ca56b84 | 3457 | |
ee10a69c CB |
3458 | /* Clear any previously set value. */ |
3459 | if (lxc_conf->syslog) { | |
3460 | free(lxc_conf->syslog); | |
3461 | lxc_conf->syslog = NULL; | |
3462 | } | |
3463 | ||
3464 | /* Check if value is empty. */ | |
7ca56b84 CB |
3465 | if (config_value_empty(value)) |
3466 | return 0; | |
3467 | ||
ee10a69c | 3468 | /* Parse value. */ |
76d0127f CB |
3469 | facility = lxc_syslog_priority_to_int(value); |
3470 | if (facility == -EINVAL) { | |
d47f1b43 | 3471 | ERROR("Wrong value for lxc.syslog."); |
76d0127f | 3472 | return -1; |
64c57ea1 BD |
3473 | } |
3474 | ||
76d0127f CB |
3475 | lxc_log_syslog(facility); |
3476 | return config_string_item(&lxc_conf->syslog, value); | |
64c57ea1 | 3477 | } |
5a46f283 CB |
3478 | |
3479 | static int config_no_new_privs(const char *key, const char *value, | |
3480 | struct lxc_conf *lxc_conf) | |
3481 | { | |
e8ec7c9e | 3482 | unsigned int v; |
5a46f283 | 3483 | |
80926845 CB |
3484 | if (config_value_empty(value)) |
3485 | return 0; | |
3486 | ||
e8ec7c9e CB |
3487 | if (lxc_safe_uint(value, &v) < 0) |
3488 | return -1; | |
3489 | ||
3490 | if (v > 1) { | |
5a46f283 CB |
3491 | ERROR("Wrong value for lxc.no_new_privs. Can only be set to 0 or 1"); |
3492 | return -1; | |
3493 | } | |
e8ec7c9e | 3494 | |
5a46f283 CB |
3495 | lxc_conf->no_new_privs = v ? true : false; |
3496 | ||
3497 | return 0; | |
3498 | } |