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