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