]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/confile.c
confile: config_init_gid()
[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{
884a4580
CB
1252 if (config_value_empty(value))
1253 return 0;
1254
17919969
CB
1255 if (lxc_safe_uint(value, &lxc_conf->pts) < 0)
1256 return -1;
10db618d 1257
1258 return 0;
1259}
1260
ee1e7aa0
SG
1261static int config_start(const char *key, const char *value,
1262 struct lxc_conf *lxc_conf)
1263{
61ff8fc8
CB
1264 if (config_value_empty(value))
1265 return 0;
1266
ee1e7aa0 1267 if(strcmp(key, "lxc.start.auto") == 0) {
3590152f
CB
1268 if (lxc_safe_uint(value, &lxc_conf->start_auto) < 0)
1269 return -1;
1270 if (lxc_conf->start_auto > 1)
1271 return -1;
ee1e7aa0
SG
1272 return 0;
1273 }
1274 else if (strcmp(key, "lxc.start.delay") == 0) {
3590152f
CB
1275 if (lxc_safe_uint(value, &lxc_conf->start_delay) < 0)
1276 return -1;
ee1e7aa0
SG
1277 return 0;
1278 }
1279 else if (strcmp(key, "lxc.start.order") == 0) {
3590152f
CB
1280 if (lxc_safe_int(value, &lxc_conf->start_order) < 0)
1281 return -1;
ee1e7aa0
SG
1282 return 0;
1283 }
1284 SYSERROR("Unknown key: %s", key);
1285 return -1;
1286}
1287
a8dfe4e0
WB
1288static int config_monitor(const char *key, const char *value,
1289 struct lxc_conf *lxc_conf)
1290{
d0524eea
CB
1291 if (config_value_empty(value))
1292 return 0;
1293
a8dfe4e0 1294 if(strcmp(key, "lxc.monitor.unshare") == 0) {
226dc30e
CB
1295 if (lxc_safe_uint(value, &lxc_conf->monitor_unshare) < 0)
1296 return -1;
a8dfe4e0
WB
1297 return 0;
1298 }
1299 SYSERROR("Unknown key: %s", key);
1300 return -1;
1301}
1302
ee1e7aa0
SG
1303static int config_group(const char *key, const char *value,
1304 struct lxc_conf *lxc_conf)
1305{
1306 char *groups, *groupptr, *sptr, *token;
1307 struct lxc_list *grouplist;
1308 int ret = -1;
1309
7584546d 1310 if (config_value_empty(value))
ee1e7aa0
SG
1311 return lxc_clear_groups(lxc_conf);
1312
1313 groups = strdup(value);
1314 if (!groups) {
1315 SYSERROR("failed to dup '%s'", value);
1316 return -1;
1317 }
1318
1319 /* in case several groups are specified in a single line
1320 * split these groups in a single element for the list */
1321 for (groupptr = groups;;groupptr = NULL) {
d028235d
SG
1322 token = strtok_r(groupptr, " \t", &sptr);
1323 if (!token) {
ee1e7aa0 1324 ret = 0;
d028235d 1325 break;
ee1e7aa0
SG
1326 }
1327
1328 grouplist = malloc(sizeof(*grouplist));
1329 if (!grouplist) {
1330 SYSERROR("failed to allocate groups list");
1331 break;
1332 }
1333
1334 grouplist->elem = strdup(token);
1335 if (!grouplist->elem) {
1336 SYSERROR("failed to dup '%s'", token);
1337 free(grouplist);
1338 break;
1339 }
1340
1341 lxc_list_add_tail(&lxc_conf->groups, grouplist);
d028235d 1342 }
ee1e7aa0
SG
1343
1344 free(groups);
1345
1346 return ret;
1347}
1348
7c661726
MP
1349static int config_environment(const char *key, const char *value,
1350 struct lxc_conf *lxc_conf)
1351{
1352 struct lxc_list *list_item = NULL;
1353
cdee76e3 1354 if (config_value_empty(value))
ab799c0b
SG
1355 return lxc_clear_environment(lxc_conf);
1356
7c661726
MP
1357 list_item = malloc(sizeof(*list_item));
1358 if (!list_item)
1359 goto freak_out;
1360
1361 list_item->elem = strdup(value);
1362
1363 if (!list_item->elem)
1364 goto freak_out;
1365
1366 lxc_list_add_tail(&lxc_conf->environment, list_item);
1367
1368 return 0;
1369
1370freak_out:
f10fad2f 1371 free(list_item);
7c661726
MP
1372
1373 return -1;
1374}
1375
12a50cc6
DE
1376static int config_tty(const char *key, const char *value,
1377 struct lxc_conf *lxc_conf)
b0a33c1e 1378{
fb12b12a
CB
1379 if (config_value_empty(value))
1380 return 0;
1381
1c30b4ad
CB
1382 if (lxc_safe_uint(value, &lxc_conf->tty) < 0)
1383 return -1;
b0a33c1e 1384
1385 return 0;
1386}
1387
12a50cc6 1388static int config_ttydir(const char *key, const char *value,
7c6ef2a2
SH
1389 struct lxc_conf *lxc_conf)
1390{
6d03d92a 1391 return config_string_item_max(&lxc_conf->ttydir, value, NAME_MAX+1);
7c6ef2a2
SH
1392}
1393
7e0e1d94
AV
1394static int config_kmsg(const char *key, const char *value,
1395 struct lxc_conf *lxc_conf)
1396{
5767e9ba
CB
1397 if (config_value_empty(value))
1398 return 0;
1399
91863d36
CB
1400 if (lxc_safe_uint(value, &lxc_conf->kmsg) < 0)
1401 return -1;
7e0e1d94 1402
91863d36
CB
1403 if (lxc_conf->kmsg > 1)
1404 return -1;
7e0e1d94
AV
1405
1406 return 0;
1407}
1408
fe4de9a6
DE
1409static int config_lsm_aa_profile(const char *key, const char *value,
1410 struct lxc_conf *lxc_conf)
e075f5d9 1411{
6d03d92a 1412 return config_string_item(&lxc_conf->lsm_aa_profile, value);
fe4de9a6
DE
1413}
1414
7aff4f43
SH
1415static int config_lsm_aa_incomplete(const char *key, const char *value,
1416 struct lxc_conf *lxc_conf)
1417{
a678e9fa
CB
1418 if (config_value_empty(value))
1419 return 0;
1420
a56e2df9
CB
1421 if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete) < 0)
1422 return -1;
7aff4f43 1423
a56e2df9
CB
1424 if (lxc_conf->lsm_aa_allow_incomplete > 1) {
1425 ERROR("Wrong value for lxc.lsm_aa_allow_incomplete. Can only be set to 0 or 1");
1426 return -1;
1427 }
7aff4f43
SH
1428
1429 return 0;
1430}
1431
fe4de9a6
DE
1432static int config_lsm_se_context(const char *key, const char *value,
1433 struct lxc_conf *lxc_conf)
1434{
6d03d92a 1435 return config_string_item(&lxc_conf->lsm_se_context, value);
e075f5d9 1436}
e075f5d9 1437
4a85ce2a 1438static int config_logfile(const char *key, const char *value,
858377e4 1439 struct lxc_conf *c)
4a85ce2a 1440{
6d03d92a
DE
1441 int ret;
1442
b40a606e
SH
1443 // store these values in the lxc_conf, and then try to set for
1444 // actual current logging.
858377e4 1445 ret = config_path_item(&c->logfile, value);
6d03d92a 1446 if (ret == 0)
858377e4 1447 ret = lxc_log_set_file(&c->logfd, c->logfile);
6d03d92a 1448 return ret;
4a85ce2a
SH
1449}
1450
1451static int config_loglevel(const char *key, const char *value,
1452 struct lxc_conf *lxc_conf)
1453{
9ea87d5d
SH
1454 int newlevel;
1455
33501e96 1456 if (config_value_empty(value))
4a85ce2a
SH
1457 return 0;
1458
a56e2df9
CB
1459 if (value[0] >= '0' && value[0] <= '9') {
1460 if (lxc_safe_int(value, &newlevel) < 0)
1461 return -1;
1462 } else {
9ea87d5d 1463 newlevel = lxc_log_priority_to_int(value);
a56e2df9 1464 }
b40a606e
SH
1465 // store these values in the lxc_conf, and then try to set for
1466 // actual current logging.
1467 lxc_conf->loglevel = newlevel;
858377e4 1468 return lxc_log_set_level(&lxc_conf->loglevel, newlevel);
4a85ce2a
SH
1469}
1470
12a50cc6 1471static int config_autodev(const char *key, const char *value,
c6883f38
SH
1472 struct lxc_conf *lxc_conf)
1473{
180abbc0
CB
1474 if (config_value_empty(value))
1475 return 0;
1476
ff6cb4ed
CB
1477 if (lxc_safe_uint(value, &lxc_conf->autodev) < 0)
1478 return -1;
c6883f38 1479
ff6cb4ed
CB
1480 if (lxc_conf->autodev > 1) {
1481 ERROR("Wrong value for lxc.autodev. Can only be set to 0 or 1");
1482 return -1;
1483 }
c6883f38
SH
1484
1485 return 0;
1486}
1487
a84b9932
AV
1488static int sig_num(const char *sig)
1489{
f2e539b3 1490 unsigned int signum;
a84b9932 1491
f2e539b3 1492 if (lxc_safe_uint(sig, &signum) < 0)
a84b9932 1493 return -1;
f2e539b3
CB
1494
1495 return signum;
a84b9932
AV
1496}
1497
1498static int rt_sig_num(const char *signame)
1499{
1500 int sig_n = 0;
1501 int rtmax = 0;
1502
1503 if (strncasecmp(signame, "max-", 4) == 0) {
1504 rtmax = 1;
1505 }
1506 signame += 4;
1507 if (!isdigit(*signame))
1508 return -1;
1509 sig_n = sig_num(signame);
1510 sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n;
1511 if (sig_n > SIGRTMAX || sig_n < SIGRTMIN)
1512 return -1;
1513 return sig_n;
1514}
1515
1516static int sig_parse(const char *signame) {
84760c11 1517 size_t n;
a84b9932
AV
1518
1519 if (isdigit(*signame)) {
1520 return sig_num(signame);
1521 } else if (strncasecmp(signame, "sig", 3) == 0) {
1522 signame += 3;
1523 if (strncasecmp(signame, "rt", 2) == 0)
1524 return rt_sig_num(signame + 2);
1525 for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++) {
1526 if (strcasecmp (signames[n].name, signame) == 0)
1527 return signames[n].num;
1528 }
1529 }
1530 return -1;
1531}
1532
f0f1d8c0
DE
1533static int config_haltsignal(const char *key, const char *value,
1534 struct lxc_conf *lxc_conf)
1535{
62a085fb 1536 int sig_n;
f0f1d8c0 1537
955912f0
CB
1538 if (config_value_empty(value))
1539 return 0;
1540
62a085fb
CB
1541 sig_n = sig_parse(value);
1542
f0f1d8c0
DE
1543 if (sig_n < 0)
1544 return -1;
1545 lxc_conf->haltsignal = sig_n;
1546
1547 return 0;
1548}
1549
dd267776 1550static int config_rebootsignal(const char *key, const char *value,
9d7e7587 1551 struct lxc_conf *lxc_conf)
dd267776 1552{
9d7e7587 1553 int sig_n;
dd267776 1554
9d7e7587
CB
1555 if (config_value_empty(value))
1556 return 0;
1557
1558 sig_n = sig_parse(value);
dd267776
BP
1559 if (sig_n < 0)
1560 return -1;
1561 lxc_conf->rebootsignal = sig_n;
1562
1563 return 0;
1564}
1565
a84b9932 1566static int config_stopsignal(const char *key, const char *value,
6ca6aedd 1567 struct lxc_conf *lxc_conf)
a84b9932 1568{
6ca6aedd 1569 int sig_n;
a84b9932 1570
6ca6aedd
CB
1571 if (config_value_empty(value))
1572 return 0;
1573
1574 sig_n = sig_parse(value);
a84b9932
AV
1575 if (sig_n < 0)
1576 return -1;
1577 lxc_conf->stopsignal = sig_n;
1578
1579 return 0;
1580}
1581
12a50cc6
DE
1582static int config_cgroup(const char *key, const char *value,
1583 struct lxc_conf *lxc_conf)
576f946d 1584{
1585 char *token = "lxc.cgroup.";
1586 char *subkey;
bf83c5b9
MS
1587 struct lxc_list *cglist = NULL;
1588 struct lxc_cgroup *cgelem = NULL;
576f946d 1589
fb0752be 1590 if (config_value_empty(value))
7d0eb87e
SH
1591 return lxc_clear_cgroups(lxc_conf, key);
1592
576f946d 1593 subkey = strstr(key, token);
1594
1595 if (!subkey)
1596 return -1;
1597
1598 if (!strlen(subkey))
1599 return -1;
1600
1601 if (strlen(subkey) == strlen(token))
1602 return -1;
a871ff6b 1603
576f946d 1604 subkey += strlen(token);
1605
1606 cglist = malloc(sizeof(*cglist));
1607 if (!cglist)
bf83c5b9 1608 goto out;
576f946d 1609
1610 cgelem = malloc(sizeof(*cgelem));
bf83c5b9
MS
1611 if (!cgelem)
1612 goto out;
1613 memset(cgelem, 0, sizeof(*cgelem));
576f946d 1614
1615 cgelem->subsystem = strdup(subkey);
1616 cgelem->value = strdup(value);
bf83c5b9
MS
1617
1618 if (!cgelem->subsystem || !cgelem->value)
1619 goto out;
1620
576f946d 1621 cglist->elem = cgelem;
1622
94d12f0a 1623 lxc_list_add_tail(&lxc_conf->cgroup, cglist);
576f946d 1624
1625 return 0;
bf83c5b9
MS
1626
1627out:
f10fad2f 1628 free(cglist);
bf83c5b9
MS
1629
1630 if (cgelem) {
f10fad2f 1631 free(cgelem->subsystem);
bf83c5b9 1632
f10fad2f 1633 free(cgelem->value);
bf83c5b9
MS
1634
1635 free(cgelem);
1636 }
1637
1638 return -1;
576f946d 1639}
1640
c6d09e15
WB
1641static bool parse_limit_value(const char **value, unsigned long *res) {
1642 char *endptr = NULL;
1643
1644 if (strncmp(*value, "unlimited", sizeof("unlimited")-1) == 0) {
1645 *res = RLIM_INFINITY;
1646 *value += sizeof("unlimited")-1;
1647 return true;
1648 }
1649
1650 errno = 0;
1651 *res = strtoul(*value, &endptr, 10);
1652 if (errno || !endptr)
1653 return false;
1654 *value = endptr;
1655
1656 return true;
1657}
1658
1659static int config_limit(const char *key, const char *value,
1660 struct lxc_conf *lxc_conf)
1661{
1662 struct lxc_list *limlist = NULL;
1663 struct lxc_limit *limelem = NULL;
1664 struct lxc_list *iter;
1665 struct rlimit limit;
1666 unsigned long limit_value;
1667
7a18f483 1668 if (config_value_empty(value))
c6d09e15
WB
1669 return lxc_clear_limits(lxc_conf, key);
1670
1671 if (strncmp(key, "lxc.limit.", sizeof("lxc.limit.")-1) != 0)
1672 return -1;
1673
1674 key += sizeof("lxc.limit.")-1;
1675
1676 /* soft limit comes first in the value */
1677 if (!parse_limit_value(&value, &limit_value))
1678 return -1;
1679 limit.rlim_cur = limit_value;
1680
1681 /* skip spaces and a colon */
1682 while (isspace(*value))
1683 ++value;
1684 if (*value == ':')
1685 ++value;
1686 else if (*value) /* any other character is an error here */
1687 return -1;
1688 while (isspace(*value))
1689 ++value;
1690
1691 /* optional hard limit */
1692 if (*value) {
1693 if (!parse_limit_value(&value, &limit_value))
1694 return -1;
1695 limit.rlim_max = limit_value;
1696 /* check for trailing garbage */
1697 while (isspace(*value))
1698 ++value;
1699 if (*value)
1700 return -1;
1701 } else {
1702 /* a single value sets both hard and soft limit */
1703 limit.rlim_max = limit.rlim_cur;
1704 }
1705
1706 /* find existing list element */
1707 lxc_list_for_each(iter, &lxc_conf->limits) {
1708 limelem = iter->elem;
1709 if (!strcmp(key, limelem->resource)) {
1710 limelem->limit = limit;
1711 return 0;
1712 }
1713 }
1714
1715 /* allocate list element */
1716 limlist = malloc(sizeof(*limlist));
1717 if (!limlist)
1718 goto out;
2e6e3feb 1719
c6d09e15
WB
1720 limelem = malloc(sizeof(*limelem));
1721 if (!limelem)
1722 goto out;
1723 memset(limelem, 0, sizeof(*limelem));
1724
1725 limelem->resource = strdup(key);
1726 if (!limelem->resource)
1727 goto out;
1728 limelem->limit = limit;
1729
1730 limlist->elem = limelem;
1731
1732 lxc_list_add_tail(&lxc_conf->limits, limlist);
1733
1734 return 0;
1735
1736out:
1737 free(limlist);
1738 if (limelem) {
1739 free(limelem->resource);
1740 free(limelem);
1741 }
1742 return -1;
1743}
1744
f6d3e3e4
SH
1745static int config_idmap(const char *key, const char *value, struct lxc_conf *lxc_conf)
1746{
1747 char *token = "lxc.id_map";
1748 char *subkey;
1749 struct lxc_list *idmaplist = NULL;
1750 struct id_map *idmap = NULL;
251d0d2a 1751 unsigned long hostid, nsid, range;
f6d3e3e4
SH
1752 char type;
1753 int ret;
1754
ab8d8307 1755 if (config_value_empty(value))
7d0eb87e
SH
1756 return lxc_clear_idmaps(lxc_conf);
1757
f6d3e3e4
SH
1758 subkey = strstr(key, token);
1759
1760 if (!subkey)
1761 return -1;
1762
1763 if (!strlen(subkey))
1764 return -1;
1765
1766 idmaplist = malloc(sizeof(*idmaplist));
1767 if (!idmaplist)
1768 goto out;
1769
1770 idmap = malloc(sizeof(*idmap));
1771 if (!idmap)
1772 goto out;
1773 memset(idmap, 0, sizeof(*idmap));
1774
251d0d2a 1775 ret = sscanf(value, "%c %lu %lu %lu", &type, &nsid, &hostid, &range);
f6d3e3e4
SH
1776 if (ret != 4)
1777 goto out;
7e60c3f0 1778
251d0d2a 1779 INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range);
ac7725e7 1780 if (type == 'u')
f6d3e3e4 1781 idmap->idtype = ID_TYPE_UID;
ac7725e7 1782 else if (type == 'g')
f6d3e3e4
SH
1783 idmap->idtype = ID_TYPE_GID;
1784 else
1785 goto out;
7e60c3f0 1786
f6d3e3e4
SH
1787 idmap->hostid = hostid;
1788 idmap->nsid = nsid;
1789 idmap->range = range;
1790
7e60c3f0
SG
1791 idmaplist->elem = idmap;
1792 lxc_list_add_tail(&lxc_conf->id_map, idmaplist);
1793
f6d3e3e4
SH
1794 return 0;
1795
1796out:
f10fad2f 1797 free(idmaplist);
f6d3e3e4
SH
1798
1799 if (idmap) {
1800 free(idmap);
1801 }
1802
1803 return -1;
1804}
1805
d95db067
DE
1806static int config_fstab(const char *key, const char *value,
1807 struct lxc_conf *lxc_conf)
1808{
6f5685f0 1809 if (config_value_empty(value))
d9192f5d 1810 return -1;
6f5685f0 1811
6d03d92a 1812 return config_path_item(&lxc_conf->fstab, value);
d95db067
DE
1813}
1814
368bbc02
CS
1815static int config_mount_auto(const char *key, const char *value,
1816 struct lxc_conf *lxc_conf)
1817{
1818 char *autos, *autoptr, *sptr, *token;
b06b8511 1819 static struct { const char *token; int mask; int flag; } allowed_auto_mounts[] = {
0769b82a
CS
1820 { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
1821 { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
1822 { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW },
f24a52d5 1823 { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
0769b82a 1824 { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO },
f24a52d5 1825 { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
0769b82a
CS
1826 { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW },
1827 { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC },
1828 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED },
1829 { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO },
1830 { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW },
1831 { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC },
1832 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED },
1833 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO },
1834 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW },
70f55bfd 1835 /* NB: For adding anything that is just a single on/off, but has
b06b8511
CS
1836 * no options: keep mask and flag identical and just define the
1837 * enum value as an unused bit so far
1838 */
368bbc02
CS
1839 { NULL, 0 }
1840 };
1841 int i;
1842 int ret = -1;
1843
ede57f73 1844 if (config_value_empty(value)) {
d9192f5d
SH
1845 lxc_conf->auto_mounts = 0;
1846 return 0;
1847 }
368bbc02
CS
1848
1849 autos = strdup(value);
1850 if (!autos) {
1851 SYSERROR("failed to dup '%s'", value);
1852 return -1;
1853 }
1854
1855 for (autoptr = autos; ; autoptr = NULL) {
d028235d
SG
1856 token = strtok_r(autoptr, " \t", &sptr);
1857 if (!token) {
368bbc02 1858 ret = 0;
d028235d 1859 break;
368bbc02
CS
1860 }
1861
1862 for (i = 0; allowed_auto_mounts[i].token; i++) {
1863 if (!strcmp(allowed_auto_mounts[i].token, token))
1864 break;
1865 }
1866
1867 if (!allowed_auto_mounts[i].token) {
1868 ERROR("Invalid filesystem to automount: %s", token);
1869 break;
1870 }
1871
b06b8511 1872 lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask;
368bbc02 1873 lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag;
d028235d 1874 }
368bbc02
CS
1875
1876 free(autos);
1877
1878 return ret;
1879}
1880
12a50cc6
DE
1881static int config_mount(const char *key, const char *value,
1882 struct lxc_conf *lxc_conf)
e7938e9e 1883{
e7938e9e
MN
1884 char *mntelem;
1885 struct lxc_list *mntlist;
1886
67cbe21a 1887 if (config_value_empty(value))
d9192f5d 1888 return lxc_clear_mount_entries(lxc_conf);
e7938e9e
MN
1889
1890 mntlist = malloc(sizeof(*mntlist));
1891 if (!mntlist)
1892 return -1;
1893
1894 mntelem = strdup(value);
00b6be44
SH
1895 if (!mntelem) {
1896 free(mntlist);
bf83c5b9 1897 return -1;
00b6be44 1898 }
e7938e9e
MN
1899 mntlist->elem = mntelem;
1900
1901 lxc_list_add_tail(&lxc_conf->mount_list, mntlist);
1902
1903 return 0;
1904}
1905
1fb86a7c
SH
1906static int config_cap_keep(const char *key, const char *value,
1907 struct lxc_conf *lxc_conf)
1908{
1909 char *keepcaps, *keepptr, *sptr, *token;
1910 struct lxc_list *keeplist;
1911 int ret = -1;
1912
99feb7f1 1913 if (config_value_empty(value))
7d0eb87e 1914 return lxc_clear_config_keepcaps(lxc_conf);
1fb86a7c
SH
1915
1916 keepcaps = strdup(value);
1917 if (!keepcaps) {
1918 SYSERROR("failed to dup '%s'", value);
1919 return -1;
1920 }
1921
1922 /* in case several capability keep is specified in a single line
1923 * split these caps in a single element for the list */
1924 for (keepptr = keepcaps;;keepptr = NULL) {
d028235d
SG
1925 token = strtok_r(keepptr, " \t", &sptr);
1926 if (!token) {
1fb86a7c 1927 ret = 0;
d028235d 1928 break;
1fb86a7c
SH
1929 }
1930
7035407c
DE
1931 if (!strcmp(token, "none"))
1932 lxc_clear_config_keepcaps(lxc_conf);
1933
1fb86a7c
SH
1934 keeplist = malloc(sizeof(*keeplist));
1935 if (!keeplist) {
1936 SYSERROR("failed to allocate keepcap list");
1937 break;
1938 }
1939
1940 keeplist->elem = strdup(token);
1941 if (!keeplist->elem) {
1942 SYSERROR("failed to dup '%s'", token);
1943 free(keeplist);
1944 break;
1945 }
1946
1947 lxc_list_add_tail(&lxc_conf->keepcaps, keeplist);
d028235d 1948 }
1fb86a7c
SH
1949
1950 free(keepcaps);
1951
1952 return ret;
1953}
1954
12a50cc6 1955static int config_cap_drop(const char *key, const char *value,
81810dd1
DL
1956 struct lxc_conf *lxc_conf)
1957{
d95db067 1958 char *dropcaps, *dropptr, *sptr, *token;
81810dd1
DL
1959 struct lxc_list *droplist;
1960 int ret = -1;
1961
b2fae748 1962 if (config_value_empty(value))
7d0eb87e 1963 return lxc_clear_config_caps(lxc_conf);
81810dd1
DL
1964
1965 dropcaps = strdup(value);
1966 if (!dropcaps) {
1967 SYSERROR("failed to dup '%s'", value);
1968 return -1;
1969 }
1970
1971 /* in case several capability drop is specified in a single line
1972 * split these caps in a single element for the list */
d95db067 1973 for (dropptr = dropcaps;;dropptr = NULL) {
d028235d
SG
1974 token = strtok_r(dropptr, " \t", &sptr);
1975 if (!token) {
81810dd1 1976 ret = 0;
d028235d 1977 break;
81810dd1 1978 }
81810dd1
DL
1979
1980 droplist = malloc(sizeof(*droplist));
1981 if (!droplist) {
1982 SYSERROR("failed to allocate drop list");
1983 break;
1984 }
1985
1986 droplist->elem = strdup(token);
1987 if (!droplist->elem) {
1988 SYSERROR("failed to dup '%s'", token);
1989 free(droplist);
1990 break;
1991 }
1992
1993 lxc_list_add_tail(&lxc_conf->caps, droplist);
d028235d 1994 }
81810dd1
DL
1995
1996 free(dropcaps);
1997
1998 return ret;
1999}
2000
12a50cc6 2001static int config_console(const char *key, const char *value,
28a4b0e5
DL
2002 struct lxc_conf *lxc_conf)
2003{
6d03d92a 2004 return config_path_item(&lxc_conf->console.path, value);
28a4b0e5
DL
2005}
2006
96f15ca1
SH
2007static int config_console_logfile(const char *key, const char *value,
2008 struct lxc_conf *lxc_conf)
2009{
2010 return config_path_item(&lxc_conf->console.log_path, value);
2011}
2012
e6744e9b
SH
2013/*
2014 * If we find a lxc.network.hwaddr in the original config file,
2015 * we expand it in the unexpanded_config, so that after a save_config
2016 * we store the hwaddr for re-use.
2017 * This is only called when reading the config file, not when executing
2018 * a lxc.include.
2019 * 'x' and 'X' are substituted in-place.
2020 */
2021static void update_hwaddr(const char *line)
2022{
2023 char *p;
2024
2025 line += lxc_char_left_gc(line, strlen(line));
2026 if (line[0] == '#')
2027 return;
2028 if (strncmp(line, "lxc.network.hwaddr", 18) != 0)
2029 return;
2030 p = strchr(line, '=');
2031 if (!p)
2032 return; // let config_network_hwaddr raise the error
2033 p++;
2034 while (isblank(*p))
2035 p++;
2036 if (!*p)
2037 return;
2038
2039 rand_complete_hwaddr(p);
2040}
2041
6b0d5538 2042int append_unexp_config_line(const char *line, struct lxc_conf *conf)
f979ac15 2043{
6b0d5538 2044 size_t len = conf->unexpanded_len, linelen = strlen(line);
f979ac15 2045
e6744e9b
SH
2046 update_hwaddr(line);
2047
6b0d5538
SH
2048 while (conf->unexpanded_alloced <= len + linelen + 2) {
2049 char *tmp = realloc(conf->unexpanded_config, conf->unexpanded_alloced + 1024);
2050 if (!tmp)
2051 return -1;
2052 if (!conf->unexpanded_config)
2053 *tmp = '\0';
2054 conf->unexpanded_config = tmp;
2055 conf->unexpanded_alloced += 1024;
2056 }
2057 strcat(conf->unexpanded_config, line);
2058 conf->unexpanded_len += linelen;
2059 if (line[linelen-1] != '\n') {
2060 strcat(conf->unexpanded_config, "\n");
2061 conf->unexpanded_len++;
f979ac15 2062 }
f979ac15
SH
2063 return 0;
2064}
2065
e1daebd9
SH
2066static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf)
2067{
74f96976 2068 struct dirent *direntp;
e1daebd9
SH
2069 DIR *dir;
2070 char path[MAXPATHLEN];
2071 int ret = -1, len;
2072
2073 dir = opendir(dirp);
2074 if (!dir) {
2075 SYSERROR("failed to open '%s'", dirp);
2076 return -1;
2077 }
2078
74f96976 2079 while ((direntp = readdir(dir))) {
e1daebd9
SH
2080 const char *fnam;
2081 if (!direntp)
2082 break;
2083
2084 fnam = direntp->d_name;
2085 if (!strcmp(fnam, "."))
2086 continue;
2087
2088 if (!strcmp(fnam, ".."))
2089 continue;
2090
2091 len = strlen(fnam);
2092 if (len < 6 || strncmp(fnam+len-5, ".conf", 5) != 0)
2093 continue;
2094 len = snprintf(path, MAXPATHLEN, "%s/%s", dirp, fnam);
2095 if (len < 0 || len >= MAXPATHLEN) {
2096 ERROR("lxc.include filename too long under '%s'", dirp);
2097 ret = -1;
2098 goto out;
2099 }
2100
2101 ret = lxc_config_read(path, lxc_conf, true);
2102 if (ret < 0)
2103 goto out;
2104 }
2105 ret = 0;
2106
2107out:
2108 if (closedir(dir))
2109 WARN("lxc.include dir: failed to close directory");
2110
2111 return ret;
2112}
2113
12a50cc6 2114static int config_includefile(const char *key, const char *value,
09ad6246
SH
2115 struct lxc_conf *lxc_conf)
2116{
616422f1
CB
2117 if (config_value_empty(value))
2118 return 0;
2119
e1daebd9
SH
2120 if (is_dir(value))
2121 return do_includedir(value, lxc_conf);
2122
6b0d5538 2123 return lxc_config_read(value, lxc_conf, true);
09ad6246
SH
2124}
2125
12a50cc6
DE
2126static int config_rootfs(const char *key, const char *value,
2127 struct lxc_conf *lxc_conf)
c2cc9f0a 2128{
6d03d92a 2129 return config_path_item(&lxc_conf->rootfs.path, value);
c2cc9f0a 2130}
2131
12a50cc6
DE
2132static int config_rootfs_mount(const char *key, const char *value,
2133 struct lxc_conf *lxc_conf)
23b7ea69 2134{
6d03d92a 2135 return config_path_item(&lxc_conf->rootfs.mount, value);
23b7ea69
DL
2136}
2137
a17b1e65
SG
2138static int config_rootfs_options(const char *key, const char *value,
2139 struct lxc_conf *lxc_conf)
2140{
2141 return config_string_item(&lxc_conf->rootfs.options, value);
2142}
2143
327a1e78 2144static int config_rootfs_backend(const char *key, const char *value,
b8223439 2145 struct lxc_conf *lxc_conf)
bfd77214 2146{
b8223439 2147 if (config_value_empty(value)) {
6a21d4ae
SH
2148 free(lxc_conf->rootfs.bdev_type);
2149 lxc_conf->rootfs.bdev_type = NULL;
b8223439 2150 return 0;
6a21d4ae 2151 }
b8223439 2152
bfd77214 2153 if (!is_valid_bdev_type(value)) {
327a1e78 2154 ERROR("Bad rootfs.backend: '%s'", value);
bfd77214
SH
2155 return -1;
2156 }
2157
b3b8c97f 2158 return config_string_item(&lxc_conf->rootfs.bdev_type, value);
bfd77214
SH
2159}
2160
12a50cc6
DE
2161static int config_pivotdir(const char *key, const char *value,
2162 struct lxc_conf *lxc_conf)
bf601689 2163{
2d489f9e 2164 WARN("lxc.pivotdir is ignored. It will soon become an error.");
59bb8698 2165 return 0;
bf601689
MH
2166}
2167
12a50cc6
DE
2168static int config_utsname(const char *key, const char *value,
2169 struct lxc_conf *lxc_conf)
c2cc9f0a 2170{
2171 struct utsname *utsname;
2172
1939e73d
CB
2173 if (config_value_empty(value))
2174 return 0;
2175
c2cc9f0a 2176 utsname = malloc(sizeof(*utsname));
2177 if (!utsname) {
36eb9bde 2178 SYSERROR("failed to allocate memory");
c2cc9f0a 2179 return -1;
2180 }
2181
2182 if (strlen(value) >= sizeof(utsname->nodename)) {
36eb9bde 2183 ERROR("node name '%s' is too long",
16e29c91 2184 value);
b6f24d54 2185 free(utsname);
c2cc9f0a 2186 return -1;
2187 }
2188
2189 strcpy(utsname->nodename, value);
f10fad2f 2190 free(lxc_conf->utsname);
c2cc9f0a 2191 lxc_conf->utsname = utsname;
2192
2193 return 0;
2194}
2195
6b0d5538
SH
2196struct parse_line_conf {
2197 struct lxc_conf *conf;
2198 bool from_include;
2199};
4184c3e1 2200
7a7ff0c6 2201static int parse_line(char *buffer, void *data)
c2cc9f0a 2202{
72d0e1cb 2203 struct lxc_config_t *config;
81192358 2204 char *line, *linep;
c2cc9f0a 2205 char *dot;
2206 char *key;
2207 char *value;
476d4cf1 2208 int ret = 0;
6b0d5538 2209 struct parse_line_conf *plc = data;
c2cc9f0a 2210
91480a0f 2211 if (lxc_is_line_empty(buffer))
c2cc9f0a 2212 return 0;
2213
81192358
DL
2214 /* we have to dup the buffer otherwise, at the re-exec for
2215 * reboot we modified the original string on the stack by
2216 * replacing '=' by '\0' below
91480a0f 2217 */
81192358 2218 linep = line = strdup(buffer);
91480a0f
DL
2219 if (!line) {
2220 SYSERROR("failed to allocate memory for '%s'", buffer);
81192358 2221 return -1;
91480a0f
DL
2222 }
2223
6b0d5538
SH
2224 if (!plc->from_include)
2225 if ((ret = append_unexp_config_line(line, plc->conf)))
2226 goto out;
2227
b2718c72 2228 line += lxc_char_left_gc(line, strlen(line));
476d4cf1 2229
4184c3e1
SH
2230 /* ignore comments */
2231 if (line[0] == '#')
91480a0f 2232 goto out;
476d4cf1 2233
6b0d5538
SH
2234 /* martian option - don't add it to the config itself */
2235 if (strncmp(line, "lxc.", 4))
4184c3e1 2236 goto out;
4184c3e1 2237
476d4cf1 2238 ret = -1;
c2cc9f0a 2239
b2718c72 2240 dot = strstr(line, "=");
c2cc9f0a 2241 if (!dot) {
36eb9bde 2242 ERROR("invalid configuration line: %s", line);
91480a0f 2243 goto out;
c2cc9f0a 2244 }
a871ff6b 2245
c2cc9f0a 2246 *dot = '\0';
2247 value = dot + 1;
2248
b2718c72 2249 key = line;
2250 key[lxc_char_right_gc(key, strlen(key))] = '\0';
c2cc9f0a 2251
b2718c72 2252 value += lxc_char_left_gc(value, strlen(value));
2253 value[lxc_char_right_gc(value, strlen(value))] = '\0';
c2cc9f0a 2254
bd878dee
SB
2255 if (*value == '\'' || *value == '\"') {
2256 size_t len = strlen(value);
2257 if (len > 1 && value[len-1] == *value) {
2258 value[len-1] = '\0';
2259 value++;
2260 }
2261 }
2262
72d0e1cb 2263 config = lxc_getconfig(key);
c2cc9f0a 2264 if (!config) {
6e1d9b94 2265 ERROR("unknown key %s", key);
91480a0f 2266 goto out;
c2cc9f0a 2267 }
2268
6b0d5538 2269 ret = config->cb(key, value, plc->conf);
91480a0f
DL
2270
2271out:
81192358 2272 free(linep);
91480a0f 2273 return ret;
c2cc9f0a 2274}
2275
74a3920a 2276static int lxc_config_readline(char *buffer, struct lxc_conf *conf)
af5b0155 2277{
6b0d5538
SH
2278 struct parse_line_conf c;
2279
2280 c.conf = conf;
2281 c.from_include = false;
2282
2283 return parse_line(buffer, &c);
af5b0155
CLG
2284}
2285
6b0d5538 2286int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
c2cc9f0a 2287{
6b0d5538
SH
2288 struct parse_line_conf c;
2289
2290 c.conf = conf;
2291 c.from_include = from_include;
f979ac15 2292
f3ca99fd
SH
2293 if( access(file, R_OK) == -1 ) {
2294 return -1;
2295 }
6b0d5538 2296
f7bee6c6 2297 /* Catch only the top level config file name in the structure */
76d0127f
CB
2298 if(!conf->rcfile)
2299 conf->rcfile = strdup(file);
f979ac15 2300
6b0d5538 2301 return lxc_file_for_each_line(file, parse_line, &c);
c2cc9f0a 2302}
62e46035
CLG
2303
2304int lxc_config_define_add(struct lxc_list *defines, char* arg)
2305{
2306 struct lxc_list *dent;
2307
2308 dent = malloc(sizeof(struct lxc_list));
2309 if (!dent)
2310 return -1;
2311
2312 dent->elem = arg;
2313 lxc_list_add_tail(defines, dent);
2314 return 0;
2315}
2316
226a18d6 2317int lxc_config_define_load(struct lxc_list *defines, struct lxc_conf *conf)
62e46035 2318{
9ebb03ad 2319 struct lxc_list *it,*next;
62e46035
CLG
2320 int ret = 0;
2321
2322 lxc_list_for_each(it, defines) {
2323 ret = lxc_config_readline(it->elem, conf);
2324 if (ret)
2325 break;
2326 }
2327
9ebb03ad 2328 lxc_list_for_each_safe(it, defines, next) {
62e46035
CLG
2329 lxc_list_del(it);
2330 free(it);
2331 }
2332
2333 return ret;
2334}
525f0002
CS
2335
2336signed long lxc_config_parse_arch(const char *arch)
2337{
6ff05e18 2338 #if HAVE_SYS_PERSONALITY_H
525f0002
CS
2339 struct per_name {
2340 char *name;
2341 unsigned long per;
bb8d8207 2342 } pername[] = {
525f0002 2343 { "x86", PER_LINUX32 },
bb8d8207
DE
2344 { "linux32", PER_LINUX32 },
2345 { "i386", PER_LINUX32 },
2346 { "i486", PER_LINUX32 },
2347 { "i586", PER_LINUX32 },
525f0002 2348 { "i686", PER_LINUX32 },
bb8d8207 2349 { "athlon", PER_LINUX32 },
e319eb34
JC
2350 { "mips", PER_LINUX32 },
2351 { "mipsel", PER_LINUX32 },
08245e8c
CB
2352 { "ppc", PER_LINUX32 },
2353 { "arm", PER_LINUX32 },
2354 { "armv7l", PER_LINUX32 },
2355 { "armhf", PER_LINUX32 },
2356 { "armel", PER_LINUX32 },
2357 { "powerpc", PER_LINUX32 },
bb8d8207 2358 { "linux64", PER_LINUX },
525f0002
CS
2359 { "x86_64", PER_LINUX },
2360 { "amd64", PER_LINUX },
e319eb34
JC
2361 { "mips64", PER_LINUX },
2362 { "mips64el", PER_LINUX },
08245e8c
CB
2363 { "ppc64", PER_LINUX },
2364 { "ppc64le", PER_LINUX },
2365 { "ppc64el", PER_LINUX },
2366 { "powerpc64", PER_LINUX },
2367 { "s390x", PER_LINUX },
2368 { "aarch64", PER_LINUX },
2369 { "arm64", PER_LINUX },
525f0002
CS
2370 };
2371 size_t len = sizeof(pername) / sizeof(pername[0]);
2372
84760c11 2373 size_t i;
525f0002
CS
2374
2375 for (i = 0; i < len; i++) {
2376 if (!strcmp(pername[i].name, arch))
2377 return pername[i].per;
2378 }
6ff05e18 2379 #endif
525f0002
CS
2380
2381 return -1;
2382}
72d0e1cb 2383
4d69b293
NK
2384int lxc_fill_elevated_privileges(char *flaglist, int *flags)
2385{
2386 char *token, *saveptr = NULL;
2387 int i, aflag;
2388 struct { const char *token; int flag; } all_privs[] = {
2389 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP },
2390 { "CAP", LXC_ATTACH_DROP_CAPABILITIES },
2391 { "LSM", LXC_ATTACH_LSM_EXEC },
2392 { NULL, 0 }
2393 };
2394
2395 if (!flaglist) {
2396 /* for the sake of backward compatibility, drop all privileges
2397 if none is specified */
2398 for (i = 0; all_privs[i].token; i++) {
d028235d 2399 *flags |= all_privs[i].flag;
4d69b293
NK
2400 }
2401 return 0;
2402 }
2403
2404 token = strtok_r(flaglist, "|", &saveptr);
2405 while (token) {
2406 aflag = -1;
2407 for (i = 0; all_privs[i].token; i++) {
2408 if (!strcmp(all_privs[i].token, token))
2409 aflag = all_privs[i].flag;
2410 }
2411 if (aflag < 0)
2412 return -1;
2413
2414 *flags |= aflag;
2415
2416 token = strtok_r(NULL, "|", &saveptr);
2417 }
2418 return 0;
2419}
2420
72d0e1cb
SG
2421static int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v)
2422{
2423 if (!retv)
2424 inlen = 0;
2425 else
2426 memset(retv, 0, inlen);
2427 return snprintf(retv, inlen, "%d", v);
2428}
2429
2430static int lxc_get_arch_entry(struct lxc_conf *c, char *retv, int inlen)
2431{
6ff05e18 2432 int fulllen = 0;
72d0e1cb
SG
2433
2434 if (!retv)
2435 inlen = 0;
2436 else
2437 memset(retv, 0, inlen);
2438
6ff05e18
SG
2439 #if HAVE_SYS_PERSONALITY_H
2440 int len = 0;
2441
72d0e1cb 2442 switch(c->personality) {
14622799 2443 case PER_LINUX32: strprint(retv, inlen, "i686"); break;
72d0e1cb
SG
2444 case PER_LINUX: strprint(retv, inlen, "x86_64"); break;
2445 default: break;
2446 }
6ff05e18 2447 #endif
72d0e1cb
SG
2448
2449 return fulllen;
2450}
2451
2452/*
2453 * If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list,
2454 * then just the value(s) will be printed. Since there still could be
2455 * more than one, it is newline-separated.
2456 * (Maybe that's ambigous, since some values, i.e. devices.list, will
2457 * already have newlines?)
2458 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed,
2459 * in 'lxc.cgroup.subsystem.key = value' format.
2460 */
12a50cc6
DE
2461static int lxc_get_cgroup_entry(struct lxc_conf *c, char *retv, int inlen,
2462 const char *key)
72d0e1cb
SG
2463{
2464 int fulllen = 0, len;
2465 int all = 0;
2466 struct lxc_list *it;
2467
2468 if (!retv)
2469 inlen = 0;
2470 else
2471 memset(retv, 0, inlen);
2472
2473 if (strcmp(key, "all") == 0)
2474 all = 1;
2475
2476 lxc_list_for_each(it, &c->cgroup) {
2477 struct lxc_cgroup *cg = it->elem;
2478 if (all) {
2479 strprint(retv, inlen, "lxc.cgroup.%s = %s\n", cg->subsystem, cg->value);
2480 } else if (strcmp(cg->subsystem, key) == 0) {
2481 strprint(retv, inlen, "%s\n", cg->value);
2482 }
2483 }
2484 return fulllen;
2485}
2486
c6d09e15
WB
2487/*
2488 * If you ask for a specific value, i.e. lxc.limit.nofile, then just the value
2489 * will be printed. If you ask for 'lxc.limit', then all limit entries will be
2490 * printed, in 'lxc.limit.resource = value' format.
2491 */
2492static int lxc_get_limit_entry(struct lxc_conf *c, char *retv, int inlen,
2493 const char *key)
2494{
2495 int fulllen = 0, len;
2496 int all = 0;
2497 struct lxc_list *it;
2498
2499 if (!retv)
2500 inlen = 0;
2501 else
2502 memset(retv, 0, inlen);
2503
2504 if (strcmp(key, "all") == 0)
2505 all = 1;
2506
2507 lxc_list_for_each(it, &c->limits) {
2508 char buf[LXC_NUMSTRLEN64*2+2]; /* 2 colon separated 64 bit integers or the word 'unlimited' */
2509 int partlen;
2510 struct lxc_limit *lim = it->elem;
2511
2512 if (lim->limit.rlim_cur == RLIM_INFINITY) {
2513 memcpy(buf, "unlimited", sizeof("unlimited"));
2514 partlen = sizeof("unlimited")-1;
2515 } else {
2e6e3feb 2516 partlen = sprintf(buf, "%"PRIu64, (uint64_t)lim->limit.rlim_cur);
c6d09e15
WB
2517 }
2518 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
2519 if (lim->limit.rlim_max == RLIM_INFINITY) {
2520 memcpy(buf+partlen, ":unlimited", sizeof(":unlimited"));
2521 } else {
2e6e3feb 2522 sprintf(buf+partlen, ":%"PRIu64, (uint64_t)lim->limit.rlim_max);
c6d09e15
WB
2523 }
2524 }
2525
2526 if (all) {
2527 strprint(retv, inlen, "lxc.limit.%s = %s\n", lim->resource, buf);
2528 } else if (strcmp(lim->resource, key) == 0) {
2529 strprint(retv, inlen, "%s", buf);
2530 }
2531 }
2532
2533 return fulllen;
2534}
2535
12a50cc6
DE
2536static int lxc_get_item_hooks(struct lxc_conf *c, char *retv, int inlen,
2537 const char *key)
72d0e1cb
SG
2538{
2539 char *subkey;
2540 int len, fulllen = 0, found = -1;
2541 struct lxc_list *it;
2542 int i;
2543
2544 /* "lxc.hook.mount" */
46cd2845
PL
2545 subkey = strchr(key, '.');
2546 if (subkey) subkey = strchr(subkey+1, '.');
72d0e1cb
SG
2547 if (!subkey)
2548 return -1;
2549 subkey++;
2550 if (!*subkey)
2551 return -1;
2552 for (i=0; i<NUM_LXC_HOOKS; i++) {
2553 if (strcmp(lxchook_names[i], subkey) == 0) {
2554 found=i;
2555 break;
2556 }
2557 }
2558 if (found == -1)
2559 return -1;
2560
2561 if (!retv)
2562 inlen = 0;
2563 else
2564 memset(retv, 0, inlen);
2565
2566 lxc_list_for_each(it, &c->hooks[found]) {
2567 strprint(retv, inlen, "%s\n", (char *)it->elem);
2568 }
2569 return fulllen;
2570}
2571
ee1e7aa0
SG
2572static int lxc_get_item_groups(struct lxc_conf *c, char *retv, int inlen)
2573{
2574 int len, fulllen = 0;
2575 struct lxc_list *it;
2576
2577 if (!retv)
2578 inlen = 0;
2579 else
2580 memset(retv, 0, inlen);
2581
2582 lxc_list_for_each(it, &c->groups) {
2583 strprint(retv, inlen, "%s\n", (char *)it->elem);
2584 }
2585 return fulllen;
2586}
2587
ab799c0b
SG
2588static int lxc_get_item_environment(struct lxc_conf *c, char *retv, int inlen)
2589{
2590 int len, fulllen = 0;
2591 struct lxc_list *it;
2592
2593 if (!retv)
2594 inlen = 0;
2595 else
2596 memset(retv, 0, inlen);
2597
2598 lxc_list_for_each(it, &c->environment) {
2599 strprint(retv, inlen, "%s\n", (char *)it->elem);
2600 }
2601 return fulllen;
2602}
2603
72d0e1cb
SG
2604static int lxc_get_item_cap_drop(struct lxc_conf *c, char *retv, int inlen)
2605{
2606 int len, fulllen = 0;
2607 struct lxc_list *it;
2608
2609 if (!retv)
2610 inlen = 0;
2611 else
2612 memset(retv, 0, inlen);
2613
2614 lxc_list_for_each(it, &c->caps) {
2615 strprint(retv, inlen, "%s\n", (char *)it->elem);
2616 }
2617 return fulllen;
2618}
2619
1fb86a7c
SH
2620static int lxc_get_item_cap_keep(struct lxc_conf *c, char *retv, int inlen)
2621{
2622 int len, fulllen = 0;
2623 struct lxc_list *it;
2624
2625 if (!retv)
2626 inlen = 0;
2627 else
2628 memset(retv, 0, inlen);
2629
2630 lxc_list_for_each(it, &c->keepcaps) {
2631 strprint(retv, inlen, "%s\n", (char *)it->elem);
2632 }
2633 return fulllen;
2634}
2635
72d0e1cb
SG
2636static int lxc_get_mount_entries(struct lxc_conf *c, char *retv, int inlen)
2637{
2638 int len, fulllen = 0;
2639 struct lxc_list *it;
2640
2641 if (!retv)
2642 inlen = 0;
2643 else
2644 memset(retv, 0, inlen);
2645
2646 lxc_list_for_each(it, &c->mount_list) {
2647 strprint(retv, inlen, "%s\n", (char *)it->elem);
2648 }
2649 return fulllen;
2650}
2651
b099e9e9
SH
2652static int lxc_get_auto_mounts(struct lxc_conf *c, char *retv, int inlen)
2653{
2654 int len, fulllen = 0;
0769b82a 2655 const char *sep = "";
b099e9e9
SH
2656
2657 if (!retv)
2658 inlen = 0;
2659 else
2660 memset(retv, 0, inlen);
2661
2662 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
2663 return 0;
2664
2665 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
0769b82a
CS
2666 case LXC_AUTO_PROC_MIXED: strprint(retv, inlen, "%sproc:mixed", sep); sep = " "; break;
2667 case LXC_AUTO_PROC_RW: strprint(retv, inlen, "%sproc:rw", sep); sep = " "; break;
b099e9e9
SH
2668 default: break;
2669 }
2670 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
0769b82a
CS
2671 case LXC_AUTO_SYS_RO: strprint(retv, inlen, "%ssys:ro", sep); sep = " "; break;
2672 case LXC_AUTO_SYS_RW: strprint(retv, inlen, "%ssys:rw", sep); sep = " "; break;
10f27710 2673 case LXC_AUTO_SYS_MIXED: strprint(retv, inlen, "%ssys:mixed", sep); sep = " "; break;
b099e9e9
SH
2674 default: break;
2675 }
2676 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
0769b82a
CS
2677 case LXC_AUTO_CGROUP_NOSPEC: strprint(retv, inlen, "%scgroup", sep); sep = " "; break;
2678 case LXC_AUTO_CGROUP_MIXED: strprint(retv, inlen, "%scgroup:mixed", sep); sep = " "; break;
2679 case LXC_AUTO_CGROUP_RO: strprint(retv, inlen, "%scgroup:ro", sep); sep = " "; break;
2680 case LXC_AUTO_CGROUP_RW: strprint(retv, inlen, "%scgroup:rw", sep); sep = " "; break;
2681 case LXC_AUTO_CGROUP_FULL_NOSPEC: strprint(retv, inlen, "%scgroup-full", sep); sep = " "; break;
2682 case LXC_AUTO_CGROUP_FULL_MIXED: strprint(retv, inlen, "%scgroup-full:mixed", sep); sep = " "; break;
2683 case LXC_AUTO_CGROUP_FULL_RO: strprint(retv, inlen, "%scgroup-full:ro", sep); sep = " "; break;
2684 case LXC_AUTO_CGROUP_FULL_RW: strprint(retv, inlen, "%scgroup-full:rw", sep); sep = " "; break;
b099e9e9
SH
2685 default: break;
2686 }
0769b82a 2687
b099e9e9
SH
2688 return fulllen;
2689}
2690
72d0e1cb
SG
2691/*
2692 * lxc.network.0.XXX, where XXX can be: name, type, link, flags, type,
8fc8295a 2693 * macvlan.mode, veth.pair, vlan, ipv4, ipv6, script.up, hwaddr, mtu,
9eaf8a59 2694 * ipv4.gateway, ipv6.gateway. ipvX.gateway can return 'auto' instead
72d0e1cb
SG
2695 * of an address. ipv4 and ipv6 return lists (newline-separated).
2696 * things like veth.pair return '' if invalid (i.e. if called for vlan
2697 * type).
2698 */
12a50cc6
DE
2699static int lxc_get_item_nic(struct lxc_conf *c, char *retv, int inlen,
2700 const char *key)
72d0e1cb
SG
2701{
2702 char *p1;
fe88b9d2 2703 int len, fulllen = 0;
72d0e1cb
SG
2704 struct lxc_netdev *netdev;
2705
2706 if (!retv)
2707 inlen = 0;
2708 else
2709 memset(retv, 0, inlen);
2710
46cd2845 2711 p1 = strchr(key, '.');
72d0e1cb
SG
2712 if (!p1 || *(p1+1) == '\0') return -1;
2713 p1++;
2714
2715 netdev = get_netdev_from_key(key, &c->network);
2716 if (!netdev)
2717 return -1;
2718 if (strcmp(p1, "name") == 0) {
2719 if (netdev->name)
2720 strprint(retv, inlen, "%s", netdev->name);
2721 } else if (strcmp(p1, "type") == 0) {
2722 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
2723 } else if (strcmp(p1, "link") == 0) {
2724 if (netdev->link)
2725 strprint(retv, inlen, "%s", netdev->link);
2726 } else if (strcmp(p1, "flags") == 0) {
2727 if (netdev->flags & IFF_UP)
2728 strprint(retv, inlen, "up");
8fc8295a 2729 } else if (strcmp(p1, "script.up") == 0) {
72d0e1cb
SG
2730 if (netdev->upscript)
2731 strprint(retv, inlen, "%s", netdev->upscript);
8fc8295a
DE
2732 } else if (strcmp(p1, "script.down") == 0) {
2733 if (netdev->downscript)
2734 strprint(retv, inlen, "%s", netdev->downscript);
72d0e1cb
SG
2735 } else if (strcmp(p1, "hwaddr") == 0) {
2736 if (netdev->hwaddr)
2737 strprint(retv, inlen, "%s", netdev->hwaddr);
2738 } else if (strcmp(p1, "mtu") == 0) {
2739 if (netdev->mtu)
2740 strprint(retv, inlen, "%s", netdev->mtu);
2741 } else if (strcmp(p1, "macvlan.mode") == 0) {
2742 if (netdev->type == LXC_NET_MACVLAN) {
2743 const char *mode;
2744 switch (netdev->priv.macvlan_attr.mode) {
2745 case MACVLAN_MODE_PRIVATE: mode = "private"; break;
2746 case MACVLAN_MODE_VEPA: mode = "vepa"; break;
2747 case MACVLAN_MODE_BRIDGE: mode = "bridge"; break;
99854161 2748 case MACVLAN_MODE_PASSTHRU: mode = "passthru"; break;
72d0e1cb
SG
2749 default: mode = "(invalid)"; break;
2750 }
2751 strprint(retv, inlen, "%s", mode);
2752 }
2753 } else if (strcmp(p1, "veth.pair") == 0) {
11029c02
DE
2754 if (netdev->type == LXC_NET_VETH) {
2755 strprint(retv, inlen, "%s",
2756 netdev->priv.veth_attr.pair ?
2757 netdev->priv.veth_attr.pair :
2758 netdev->priv.veth_attr.veth1);
2759 }
72d0e1cb
SG
2760 } else if (strcmp(p1, "vlan") == 0) {
2761 if (netdev->type == LXC_NET_VLAN) {
2762 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
2763 }
9eaf8a59 2764 } else if (strcmp(p1, "ipv4.gateway") == 0) {
72d0e1cb
SG
2765 if (netdev->ipv4_gateway_auto) {
2766 strprint(retv, inlen, "auto");
2767 } else if (netdev->ipv4_gateway) {
2768 char buf[INET_ADDRSTRLEN];
2769 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
2770 strprint(retv, inlen, "%s", buf);
2771 }
2772 } else if (strcmp(p1, "ipv4") == 0) {
2773 struct lxc_list *it2;
2774 lxc_list_for_each(it2, &netdev->ipv4) {
2775 struct lxc_inetdev *i = it2->elem;
2776 char buf[INET_ADDRSTRLEN];
2777 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
28417b5a 2778 strprint(retv, inlen, "%s/%d\n", buf, i->prefix);
72d0e1cb 2779 }
9eaf8a59 2780 } else if (strcmp(p1, "ipv6.gateway") == 0) {
72d0e1cb
SG
2781 if (netdev->ipv6_gateway_auto) {
2782 strprint(retv, inlen, "auto");
2783 } else if (netdev->ipv6_gateway) {
6afb165d
AN
2784 char buf[INET6_ADDRSTRLEN];
2785 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
72d0e1cb
SG
2786 strprint(retv, inlen, "%s", buf);
2787 }
2788 } else if (strcmp(p1, "ipv6") == 0) {
2789 struct lxc_list *it2;
2790 lxc_list_for_each(it2, &netdev->ipv6) {
6afb165d
AN
2791 struct lxc_inet6dev *i = it2->elem;
2792 char buf[INET6_ADDRSTRLEN];
72d0e1cb 2793 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
28417b5a 2794 strprint(retv, inlen, "%s/%d\n", buf, i->prefix);
72d0e1cb
SG
2795 }
2796 }
2797 return fulllen;
2798}
2799
2800static int lxc_get_item_network(struct lxc_conf *c, char *retv, int inlen)
2801{
2802 int len, fulllen = 0;
2803 struct lxc_list *it;
2804
2805 if (!retv)
2806 inlen = 0;
2807 else
2808 memset(retv, 0, inlen);
2809
2810 lxc_list_for_each(it, &c->network) {
2811 struct lxc_netdev *n = it->elem;
2812 const char *t = lxc_net_type_to_str(n->type);
2813 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
2814 }
2815 return fulllen;
2816}
2817
12a50cc6
DE
2818int lxc_get_config_item(struct lxc_conf *c, const char *key, char *retv,
2819 int inlen)
72d0e1cb 2820{
4a85ce2a 2821 const char *v = NULL;
72d0e1cb
SG
2822
2823 if (strcmp(key, "lxc.mount.entry") == 0)
2824 return lxc_get_mount_entries(c, retv, inlen);
b099e9e9
SH
2825 else if (strcmp(key, "lxc.mount.auto") == 0)
2826 return lxc_get_auto_mounts(c, retv, inlen);
72d0e1cb
SG
2827 else if (strcmp(key, "lxc.mount") == 0)
2828 v = c->fstab;
2829 else if (strcmp(key, "lxc.tty") == 0)
2830 return lxc_get_conf_int(c, retv, inlen, c->tty);
2831 else if (strcmp(key, "lxc.pts") == 0)
2832 return lxc_get_conf_int(c, retv, inlen, c->pts);
2833 else if (strcmp(key, "lxc.devttydir") == 0)
2834 v = c->ttydir;
2835 else if (strcmp(key, "lxc.arch") == 0)
2836 return lxc_get_arch_entry(c, retv, inlen);
2837 else if (strcmp(key, "lxc.aa_profile") == 0)
fe4de9a6 2838 v = c->lsm_aa_profile;
7aff4f43
SH
2839 else if (strcmp(key, "lxc.aa_allow_incomplete") == 0)
2840 return lxc_get_conf_int(c, retv, inlen, c->lsm_aa_allow_incomplete);
fe4de9a6
DE
2841 else if (strcmp(key, "lxc.se_context") == 0)
2842 v = c->lsm_se_context;
4a85ce2a 2843 else if (strcmp(key, "lxc.logfile") == 0)
858377e4 2844 v = c->logfile;
4a85ce2a 2845 else if (strcmp(key, "lxc.loglevel") == 0)
858377e4 2846 v = lxc_log_priority_to_string(c->loglevel);
72d0e1cb
SG
2847 else if (strcmp(key, "lxc.cgroup") == 0) // all cgroup info
2848 return lxc_get_cgroup_entry(c, retv, inlen, "all");
2849 else if (strncmp(key, "lxc.cgroup.", 11) == 0) // specific cgroup info
2850 return lxc_get_cgroup_entry(c, retv, inlen, key + 11);
2851 else if (strcmp(key, "lxc.utsname") == 0)
64fca455 2852 v = c->utsname ? c->utsname->nodename : NULL;
96f15ca1
SH
2853 else if (strcmp(key, "lxc.console.logfile") == 0)
2854 v = c->console.log_path;
72d0e1cb
SG
2855 else if (strcmp(key, "lxc.console") == 0)
2856 v = c->console.path;
2857 else if (strcmp(key, "lxc.rootfs.mount") == 0)
2858 v = c->rootfs.mount;
327a1e78 2859 else if (strcmp(key, "lxc.rootfs.backend") == 0)
6a21d4ae 2860 v = c->rootfs.bdev_type;
a17b1e65
SG
2861 else if (strcmp(key, "lxc.rootfs.options") == 0)
2862 v = c->rootfs.options;
72d0e1cb
SG
2863 else if (strcmp(key, "lxc.rootfs") == 0)
2864 v = c->rootfs.path;
72d0e1cb
SG
2865 else if (strcmp(key, "lxc.cap.drop") == 0)
2866 return lxc_get_item_cap_drop(c, retv, inlen);
1fb86a7c
SH
2867 else if (strcmp(key, "lxc.cap.keep") == 0)
2868 return lxc_get_item_cap_keep(c, retv, inlen);
72d0e1cb
SG
2869 else if (strncmp(key, "lxc.hook", 8) == 0)
2870 return lxc_get_item_hooks(c, retv, inlen, key);
2871 else if (strcmp(key, "lxc.network") == 0)
2872 return lxc_get_item_network(c, retv, inlen);
2873 else if (strncmp(key, "lxc.network.", 12) == 0)
2874 return lxc_get_item_nic(c, retv, inlen, key + 12);
ee1e7aa0
SG
2875 else if (strcmp(key, "lxc.start.auto") == 0)
2876 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
2877 else if (strcmp(key, "lxc.start.delay") == 0)
2878 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
2879 else if (strcmp(key, "lxc.start.order") == 0)
2880 return lxc_get_conf_int(c, retv, inlen, c->start_order);
a8dfe4e0
WB
2881 else if (strcmp(key, "lxc.monitor.unshare") == 0)
2882 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
ee1e7aa0
SG
2883 else if (strcmp(key, "lxc.group") == 0)
2884 return lxc_get_item_groups(c, retv, inlen);
58e0f57d
SH
2885 else if (strcmp(key, "lxc.seccomp") == 0)
2886 v = c->seccomp;
ab799c0b
SG
2887 else if (strcmp(key, "lxc.environment") == 0)
2888 return lxc_get_item_environment(c, retv, inlen);
67c660d0
SG
2889 else if (strcmp(key, "lxc.init_cmd") == 0)
2890 v = c->init_cmd;
68d18db8
PT
2891 else if (strcmp(key, "lxc.init_uid") == 0)
2892 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
2893 else if (strcmp(key, "lxc.init_gid") == 0)
2894 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
8796becf
CB
2895 else if (strcmp(key, "lxc.ephemeral") == 0)
2896 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
76d0127f
CB
2897 else if (strcmp(key, "lxc.syslog") == 0)
2898 v = c->syslog;
5a46f283
CB
2899 else if (strcmp(key, "lxc.no_new_privs") == 0)
2900 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
c6d09e15
WB
2901 else if (strcmp(key, "lxc.limit") == 0) // all limits
2902 return lxc_get_limit_entry(c, retv, inlen, "all");
2903 else if (strncmp(key, "lxc.limit.", 10) == 0) // specific limit
2904 return lxc_get_limit_entry(c, retv, inlen, key + 10);
72d0e1cb
SG
2905 else return -1;
2906
2907 if (!v)
2908 return 0;
2909 if (retv && inlen >= strlen(v) + 1)
2910 strncpy(retv, v, strlen(v)+1);
2911 return strlen(v);
2912}
2913
12a50cc6 2914int lxc_clear_config_item(struct lxc_conf *c, const char *key)
72d0e1cb 2915{
c7b15d1e
CB
2916 int ret = 0;
2917
2918 if (strcmp(key, "lxc.network") == 0) {
2919 ret = lxc_clear_config_network(c);
2920
2921 } else if (strncmp(key, "lxc.network.", 12) == 0) {
2922 ret = lxc_clear_nic(c, key + 12);
2923
2924 } else if (strcmp(key, "lxc.cap.drop") == 0) {
2925 ret = lxc_clear_config_caps(c);
2926
2927 } else if (strcmp(key, "lxc.cap.keep") == 0) {
2928 ret = lxc_clear_config_keepcaps(c);
2929
2930 } else if (strncmp(key, "lxc.cgroup", 10) == 0) {
2931 ret = lxc_clear_cgroups(c, key);
2932
2933 } else if (strcmp(key, "lxc.mount.entry") == 0) {
2934 ret = lxc_clear_mount_entries(c);
2935
2936 } else if (strcmp(key, "lxc.mount.auto") == 0) {
2937 ret = lxc_clear_automounts(c);
2938
2939 } else if (strncmp(key, "lxc.hook", 8) == 0) {
2940 ret = lxc_clear_hooks(c, key);
2941
2942 } else if (strncmp(key, "lxc.group", 9) == 0) {
2943 ret = lxc_clear_groups(c);
2944
2945 } else if (strncmp(key, "lxc.environment", 15) == 0) {
2946 ret = lxc_clear_environment(c);
2947
2948 } else if (strncmp(key, "lxc.id_map", 10) == 0) {
2949 ret = lxc_clear_idmaps(c);
2950
2951 } else if (strncmp(key, "lxc.limit", 9) == 0) {
2952 ret = lxc_clear_limits(c, key);
2953
2954 } else if (strcmp(key, "lxc.utsname") == 0) {
2955 free(c->utsname);
2956 c->utsname = NULL;
2957
2958 } else if (strcmp(key, "lxc.arch") == 0) {
2959 c->personality = -1;
2960
2961 } else if (strcmp(key, "lxc.haltsignal") == 0) {
2962 c->haltsignal = 0;
2963
2964 } else if (strcmp(key, "lxc.rebootsignal") == 0) {
2965 c->rebootsignal = 0;
2966
2967 } else if (strcmp(key, "lxc.stopsignal") == 0) {
2968 c->stopsignal = 0;
2969
2970 } else if (strcmp(key, "lxc.init_cmd") == 0) {
2971 free(c->init_cmd);
2972 c->init_cmd = NULL;
2973
2974 } else if (strcmp(key, "lxc.init_uid") == 0) {
2975 c->init_uid = 0;
2976
2977 } else if (strcmp(key, "lxc.init_gid") == 0) {
2978 c->init_gid = 0;
2979
2980 } else if (strcmp(key, "lxc.ephemeral") == 0) {
2981 c->ephemeral = 0;
2982
2983 } else if (strcmp(key, "lxc.console.logfile") == 0) {
2984 free(c->console.log_path);
2985 c->console.log_path = NULL;
2986
2987 } else if (strcmp(key, "lxc.console") == 0) {
2988 free(c->console.path);
2989 c->console.path = NULL;
2990
2991 } else if (strcmp(key, "lxc.tty") == 0) {
2992 c->tty = 0;
2993
2994 } else if (strcmp(key, "lxc.devttydir") == 0) {
2995 free(c->ttydir);
2996 c->ttydir = NULL;
2997
2998 } else if (strcmp(key, "lxc.autodev") == 0) {
2999 c->autodev = 1;
3000
3001 } else if (strcmp(key, "lxc.kmsg") == 0) {
3002 c->kmsg = 0;
3003
3004 } else if (strcmp(key, "lxc.mount") == 0) {
3005 free(c->fstab);
3006 c->fstab = NULL;
3007
3008 } else if (strcmp(key, "lxc.rootfs") == 0) {
3009 free(c->rootfs.path);
3010 c->rootfs.path = NULL;
3011
3012 } else if (strcmp(key, "lxc.rootfs.mount") == 0) {
3013 free(c->rootfs.mount);
3014 c->rootfs.mount = NULL;
3015
3016 } else if (strcmp(key, "lxc.rootfs.options") == 0) {
3017 free(c->rootfs.options);
3018 c->rootfs.options = NULL;
3019
3020 } else if (strcmp(key, "lxc.rootfs.backend") == 0) {
3021 free(c->rootfs.bdev_type);
3022 c->rootfs.bdev_type = NULL;
3023
3024 } else if (strcmp(key, "lxc.aa_profile") == 0) {
3025 free(c->lsm_aa_profile);
3026 c->lsm_aa_profile = NULL;
3027
3028 } else if (strcmp(key, "lxc.aa_allow_incomplete") == 0) {
3029 c->lsm_aa_allow_incomplete = 0;
3030
3031 } else if (strcmp(key, "lxc.se_context") == 0) {
3032 free(c->lsm_se_context);
3033 c->lsm_se_context = NULL;
3034
3035 } else if (strcmp(key, "lxc.seccomp") == 0) {
3036 free(c->seccomp);
3037 c->seccomp = NULL;
3038
3039 } else if (strcmp(key, "lxc.loglevel") == 0) {
3040 c->loglevel = LXC_LOG_PRIORITY_NOTSET;
3041
3042 } else if (strcmp(key, "lxc.logfile") == 0) {
3043 free(c->logfile);
3044 c->logfile = NULL;
3045
3046 } else if (strcmp(key, "lxc.monitor.unshare") == 0) {
3047 c->monitor_unshare = 0;
3048
3049 } else if (strcmp(key, "lxc.pts") == 0) {
3050 c->pts = 0;
3051
3052 } else if (strcmp(key, "lxc.include") == 0) {
3053 lxc_clear_includes(c);
3054
3055 } else if (strcmp(key, "lxc.syslog") == 0) {
3056 free(c->syslog);
3057 c->syslog = NULL;
3058 } else {
3059 ret = -1;
3060 }
3061
3062 return ret;
72d0e1cb
SG
3063}
3064
3065/*
3066 * writing out a confile.
3067 */
3068void write_config(FILE *fout, struct lxc_conf *c)
3069{
6b0d5538
SH
3070 size_t len = c->unexpanded_len;
3071 int ret;
72d0e1cb 3072
6b0d5538
SH
3073 if (!len)
3074 return;
3075 ret = fwrite(c->unexpanded_config, 1, len, fout);
3076 if (ret != len)
3077 SYSERROR("Error writing configuration file");
3078}
f979ac15 3079
6b0d5538
SH
3080bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key, const char *v)
3081{
3082 int ret;
151d2da2
CB
3083 size_t len;
3084 char *tmp;
4184c3e1 3085
151d2da2
CB
3086 len = strlen(key) + strlen(v) + 4;
3087 tmp = alloca(len);
3088
3089 if (config_value_empty(v))
3090 ret = snprintf(tmp, len, "%s =", key);
3091 else
3092 ret = snprintf(tmp, len, "%s = %s", key, v);
6b0d5538
SH
3093 if (ret < 0 || ret >= len)
3094 return false;
3095
3096 /* Save the line verbatim into unexpanded_conf */
3097 if (append_unexp_config_line(tmp, conf))
3098 return false;
3099
3100 return true;
3101}
3102
3103void clear_unexp_config_line(struct lxc_conf *conf, const char *key, bool rm_subkeys)
3104{
3105 char *lstart = conf->unexpanded_config, *lend;
3106
3107 if (!conf->unexpanded_config)
3108 return;
3109 while (*lstart) {
3110 lend = strchr(lstart, '\n');
3111 char v;
3112 if (!lend)
3113 lend = lstart + strlen(lstart);
3114 else
3115 lend++;
3116 if (strncmp(lstart, key, strlen(key)) != 0) {
3117 lstart = lend;
3118 continue;
5f62730e 3119 }
6b0d5538
SH
3120 if (!rm_subkeys) {
3121 v = lstart[strlen(key)];
3122 if (!isspace(v) && v != '=') {
3123 lstart = lend;
3124 continue;
3125 }
5f62730e 3126 }
6b0d5538
SH
3127 conf->unexpanded_len -= (lend - lstart);
3128 if (*lend == '\0') {
3129 *lstart = '\0';
3130 return;
fd986e08 3131 }
6b0d5538 3132 memmove(lstart, lend, strlen(lend)+1);
fd986e08 3133 }
6b0d5538
SH
3134}
3135
329b3625
CB
3136bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
3137 const char *newpath, const char *oldname,
3138 const char *newname, const char *ovldir)
3139{
3140 const char *key = "lxc.mount.entry";
3141 int ret;
3142 char *lstart = conf->unexpanded_config;
3143 char *lend;
3144 char *p;
3145 char *q;
3146 size_t newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
3147 size_t olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
3148 char *olddir = alloca(olddirlen + 1);
3149 char *newdir = alloca(newdirlen + 1);
3150
3151 ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath, oldname);
3152 if (ret < 0 || ret >= olddirlen + 1) {
3153 ERROR("Bug in %s", __func__);
3154 return false;
3155 }
3156 ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath, newname);
3157 if (ret < 0 || ret >= newdirlen + 1) {
3158 ERROR("Bug in %s", __func__);
3159 return false;
3160 }
3161 if (!conf->unexpanded_config)
3162 return true;
3163 while (*lstart) {
3164 lend = strchr(lstart, '\n');
3165 if (!lend)
3166 lend = lstart + strlen(lstart);
3167 else
3168 lend++;
3169 if (strncmp(lstart, key, strlen(key)) != 0)
3170 goto next;
3171 p = strchr(lstart + strlen(key), '=');
3172 if (!p)
3173 goto next;
3174 p++;
3175 while (isblank(*p))
3176 p++;
3177 if (p >= lend)
3178 goto next;
3179 /* Whenever an lxc.mount.entry entry is found in a line we check
3180 * if the substring " overlay" or the substring " aufs" is
3181 * present before doing any further work. We check for "
3182 * overlay" and " aufs" since both substrings need to have at
3183 * least one space before them in a valid overlay
3184 * lxc.mount.entry (/A B overlay). When the space before is
3185 * missing it is very likely that these substrings are part of a
3186 * path or something else. (Checking q >= lend ensures that we
3187 * only count matches in the current line.) */
3188 if ((!(q = strstr(p, " overlay")) || q >= lend) && (!(q = strstr(p, " aufs")) || q >= lend))
3189 goto next;
3190 if (!(q = strstr(p, olddir)) || (q >= lend))
3191 goto next;
3192
3193 /* replace the olddir with newdir */
3194 if (olddirlen >= newdirlen) {
3195 size_t diff = olddirlen - newdirlen;
3196 memcpy(q, newdir, newdirlen);
3197 if (olddirlen != newdirlen) {
3198 memmove(q + newdirlen, q + newdirlen + diff,
3199 strlen(q) - newdirlen - diff + 1);
3200 lend -= diff;
3201 conf->unexpanded_len -= diff;
3202 }
3203 } else {
3204 char *new;
3205 size_t diff = newdirlen - olddirlen;
3206 size_t oldlen = conf->unexpanded_len;
3207 size_t newlen = oldlen + diff;
3208 size_t poffset = q - conf->unexpanded_config;
3209 new = realloc(conf->unexpanded_config, newlen + 1);
3210 if (!new) {
3211 ERROR("Out of memory");
3212 return false;
3213 }
3214 conf->unexpanded_len = newlen;
3215 conf->unexpanded_alloced = newlen + 1;
3216 new[newlen - 1] = '\0';
3217 lend = new + (lend - conf->unexpanded_config);
3218 /* move over the remainder to make room for the newdir */
3219 memmove(new + poffset + newdirlen,
3220 new + poffset + olddirlen,
3221 oldlen - poffset - olddirlen + 1);
3222 conf->unexpanded_config = new;
3223 memcpy(new + poffset, newdir, newdirlen);
3224 lend += diff;
3225 }
3226next:
3227 lstart = lend;
3228 }
3229 return true;
3230}
3231
67702c21 3232bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
d546aa0e
CB
3233 const char *newpath, const char *oldname,
3234 const char *newname)
6b0d5538 3235{
67702c21
SH
3236 const char *key = "lxc.hook";
3237 int ret;
3238 char *lstart = conf->unexpanded_config, *lend, *p;
3239 size_t newdirlen = strlen(newpath) + strlen(newname) + 1;
3240 size_t olddirlen = strlen(oldpath) + strlen(oldname) + 1;
3241 char *olddir = alloca(olddirlen + 1);
3242 char *newdir = alloca(newdirlen + 1);
3243
d546aa0e
CB
3244 ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
3245 if (ret < 0 || ret >= olddirlen + 1) {
67702c21
SH
3246 ERROR("Bug in %s", __func__);
3247 return false;
3248 }
d546aa0e
CB
3249 ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
3250 if (ret < 0 || ret >= newdirlen + 1) {
67702c21
SH
3251 ERROR("Bug in %s", __func__);
3252 return false;
3253 }
3254 if (!conf->unexpanded_config)
3255 return true;
3256 while (*lstart) {
3257 lend = strchr(lstart, '\n');
3258 if (!lend)
3259 lend = lstart + strlen(lstart);
3260 else
3261 lend++;
d546aa0e
CB
3262 if (strncmp(lstart, key, strlen(key)) != 0)
3263 goto next;
3264 p = strchr(lstart + strlen(key), '=');
3265 if (!p)
3266 goto next;
67702c21
SH
3267 p++;
3268 while (isblank(*p))
3269 p++;
d546aa0e
CB
3270 if (p >= lend)
3271 goto next;
3272 if (strncmp(p, olddir, strlen(olddir)) != 0)
3273 goto next;
67702c21
SH
3274 /* replace the olddir with newdir */
3275 if (olddirlen >= newdirlen) {
3276 size_t diff = olddirlen - newdirlen;
3277 memcpy(p, newdir, newdirlen);
3278 if (olddirlen != newdirlen) {
d546aa0e
CB
3279 memmove(p + newdirlen, p + newdirlen + diff,
3280 strlen(p) - newdirlen - diff + 1);
67702c21
SH
3281 lend -= diff;
3282 conf->unexpanded_len -= diff;
3283 }
67702c21
SH
3284 } else {
3285 char *new;
3286 size_t diff = newdirlen - olddirlen;
3287 size_t oldlen = conf->unexpanded_len;
3288 size_t newlen = oldlen + diff;
3289 size_t poffset = p - conf->unexpanded_config;
d546aa0e 3290 new = realloc(conf->unexpanded_config, newlen + 1);
67702c21
SH
3291 if (!new) {
3292 ERROR("Out of memory");
6b0d5538 3293 return false;
67702c21
SH
3294 }
3295 conf->unexpanded_len = newlen;
d546aa0e
CB
3296 conf->unexpanded_alloced = newlen + 1;
3297 new[newlen - 1] = '\0';
67702c21 3298 lend = new + (lend - conf->unexpanded_config);
d546aa0e
CB
3299 /* move over the remainder to make room for the newdir */
3300 memmove(new + poffset + newdirlen,
3301 new + poffset + olddirlen,
3302 oldlen - poffset - olddirlen + 1);
67702c21 3303 conf->unexpanded_config = new;
d546aa0e
CB
3304 memcpy(new + poffset, newdir, newdirlen);
3305 lend += diff;
fd986e08 3306 }
d546aa0e
CB
3307next:
3308 lstart = lend;
fd986e08 3309 }
6b0d5538
SH
3310 return true;
3311}
3312
3313#define DO(cmd) { \
3314 if (!(cmd)) { \
3315 ERROR("Error writing to new config"); \
3316 return false; \
3317 } \
3318}
3319
091045f8 3320static bool new_hwaddr(char *hwaddr)
67702c21 3321{
091045f8
CB
3322 int ret;
3323
3324 /* COMMENT(brauner): Initialize random number generator. */
3325 (void)randseed(true);
3326
3327 ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255,
3328 rand() % 255, rand() % 255);
3329 if (ret < 0 || ret >= 18) {
3330 SYSERROR("Failed to call snprintf().");
3331 return false;
3332 }
3333
3334 return true;
67702c21
SH
3335}
3336
3337/*
3338 * This is called only from clone.
3339 * We wish to update all hwaddrs in the unexpanded config file. We
3340 * can't/don't want to update any which come from lxc.includes (there
3341 * shouldn't be any).
3342 * We can't just walk the c->lxc-conf->network list because that includes
3343 * netifs from the include files. So we update the ones which we find in
3344 * the unexp config file, then find the original macaddr in the
3345 * conf->network, and update that to the same value.
3346 */
3347bool network_new_hwaddrs(struct lxc_conf *conf)
6b0d5538
SH
3348{
3349 struct lxc_list *it;
3350
67702c21
SH
3351 const char *key = "lxc.network.hwaddr";
3352 char *lstart = conf->unexpanded_config, *lend, *p, *p2;
6b0d5538 3353
67702c21
SH
3354 if (!conf->unexpanded_config)
3355 return true;
091045f8 3356
67702c21
SH
3357 while (*lstart) {
3358 char newhwaddr[18], oldhwaddr[17];
091045f8 3359
67702c21
SH
3360 lend = strchr(lstart, '\n');
3361 if (!lend)
3362 lend = lstart + strlen(lstart);
3363 else
3364 lend++;
091045f8 3365
67702c21
SH
3366 if (strncmp(lstart, key, strlen(key)) != 0) {
3367 lstart = lend;
3368 continue;
72d0e1cb 3369 }
091045f8 3370
67702c21
SH
3371 p = strchr(lstart+strlen(key), '=');
3372 if (!p) {
3373 lstart = lend;
3374 continue;
72d0e1cb 3375 }
091045f8 3376
67702c21
SH
3377 p++;
3378 while (isblank(*p))
3379 p++;
3380 if (!*p)
3381 return true;
091045f8 3382
67702c21
SH
3383 p2 = p;
3384 while (*p2 && !isblank(*p2) && *p2 != '\n')
3385 p2++;
3386 if (p2-p != 17) {
3387 WARN("Bad hwaddr entry");
3388 lstart = lend;
3389 continue;
72d0e1cb 3390 }
091045f8 3391
67702c21 3392 memcpy(oldhwaddr, p, 17);
091045f8
CB
3393
3394 if (!new_hwaddr(newhwaddr))
3395 return false;
3396
67702c21
SH
3397 memcpy(p, newhwaddr, 17);
3398 lxc_list_for_each(it, &conf->network) {
3399 struct lxc_netdev *n = it->elem;
3400 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
3401 memcpy(n->hwaddr, newhwaddr, 17);
72d0e1cb 3402 }
67702c21
SH
3403
3404 lstart = lend;
72d0e1cb 3405 }
091045f8 3406
6b0d5538 3407 return true;
72d0e1cb 3408}
8796becf
CB
3409
3410static int config_ephemeral(const char *key, const char *value,
3411 struct lxc_conf *lxc_conf)
3412{
78304622
CB
3413 if (config_value_empty(value))
3414 return 0;
3415
66ffdb1a
CB
3416 if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0)
3417 return -1;
8796becf 3418
66ffdb1a 3419 if (lxc_conf->ephemeral > 1) {
8796becf
CB
3420 ERROR("Wrong value for lxc.ephemeral. Can only be set to 0 or 1");
3421 return -1;
8796becf
CB
3422 }
3423
3424 return 0;
3425}
3426
64c57ea1 3427static int config_syslog(const char *key, const char *value,
76d0127f 3428 struct lxc_conf *lxc_conf)
64c57ea1 3429{
76d0127f 3430 int facility;
7ca56b84 3431
ee10a69c
CB
3432 /* Clear any previously set value. */
3433 if (lxc_conf->syslog) {
3434 free(lxc_conf->syslog);
3435 lxc_conf->syslog = NULL;
3436 }
3437
3438 /* Check if value is empty. */
7ca56b84
CB
3439 if (config_value_empty(value))
3440 return 0;
3441
ee10a69c 3442 /* Parse value. */
76d0127f
CB
3443 facility = lxc_syslog_priority_to_int(value);
3444 if (facility == -EINVAL) {
d47f1b43 3445 ERROR("Wrong value for lxc.syslog.");
76d0127f 3446 return -1;
64c57ea1
BD
3447 }
3448
76d0127f
CB
3449 lxc_log_syslog(facility);
3450 return config_string_item(&lxc_conf->syslog, value);
64c57ea1 3451}
5a46f283
CB
3452
3453static int config_no_new_privs(const char *key, const char *value,
3454 struct lxc_conf *lxc_conf)
3455{
e8ec7c9e 3456 unsigned int v;
5a46f283 3457
80926845
CB
3458 if (config_value_empty(value))
3459 return 0;
3460
e8ec7c9e
CB
3461 if (lxc_safe_uint(value, &v) < 0)
3462 return -1;
3463
3464 if (v > 1) {
5a46f283
CB
3465 ERROR("Wrong value for lxc.no_new_privs. Can only be set to 0 or 1");
3466 return -1;
3467 }
e8ec7c9e 3468
5a46f283
CB
3469 lxc_conf->no_new_privs = v ? true : false;
3470
3471 return 0;
3472}