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