]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/confile.c
log: change ERROR macro using sterror to SYSERROR
[mirror_lxc.git] / src / lxc / confile.c
CommitLineData
c2cc9f0a 1/*
2 * lxc: linux Container library
c2cc9f0a 3 * (C) Copyright IBM Corp. 2007, 2008
4 *
5 * Authors:
9afe19d6 6 * Daniel Lezcano <daniel.lezcano at free.fr>
70c1e708
CB
7 * Serge Hallyn <serge@hallyn.com>
8 * Christian Brauner <christian.brauner@ubuntu.com>
c2cc9f0a 9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
250b1eec 22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
c2cc9f0a 23 */
70c1e708 24
12a50cc6 25#define _GNU_SOURCE
70c1e708
CB
26#define __STDC_FORMAT_MACROS
27#include <ctype.h>
28#include <dirent.h>
29#include <errno.h>
30#include <fcntl.h>
31#include <inttypes.h>
32#include <signal.h>
c2cc9f0a 33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
70c1e708
CB
36#include <syslog.h>
37#include <time.h>
c2cc9f0a 38#include <unistd.h>
70c1e708
CB
39#include <arpa/inet.h>
40#include <net/if.h>
41#include <netinet/in.h>
42#include <sys/param.h>
63376d7d 43#include <sys/stat.h>
c2cc9f0a 44#include <sys/types.h>
c2cc9f0a 45#include <sys/utsname.h>
c2cc9f0a 46
70c1e708 47#include "conf.h"
6ff05e18 48#include "config.h"
525f0002 49#include "confile.h"
0b843d35 50#include "confile_utils.h"
f2363e38 51#include "log.h"
58e0f57d 52#include "lxcseccomp.h"
70c1e708
CB
53#include "network.h"
54#include "parse.h"
28d832c4 55#include "storage.h"
70c1e708 56#include "utils.h"
36eb9bde 57
576400e5 58#if HAVE_IFADDRS_H
59#include <ifaddrs.h>
60#else
61#include <../include/ifaddrs.h>
62#endif
63
6ff05e18
SG
64#if HAVE_SYS_PERSONALITY_H
65#include <sys/personality.h>
66#endif
67
43f984ea
DJ
68#ifndef HAVE_STRLCPY
69#include "include/strlcpy.h"
70#endif
71
ebcd5140
DJ
72#ifndef HAVE_STRLCAT
73#include "include/strlcat.h"
74#endif
75
36eb9bde 76lxc_log_define(lxc_confile, lxc);
576f946d 77
861d1ada
CB
78#define lxc_config_define(name) \
79 static int set_config_##name(const char *, const char *, \
80 struct lxc_conf *, void *); \
81 static int get_config_##name(const char *, char *, int, \
82 struct lxc_conf *, void *); \
83 static int clr_config_##name(const char *, struct lxc_conf *, void *);
71e287ca 84
63bab717 85lxc_config_define(autodev);
71e287ca 86lxc_config_define(apparmor_allow_incomplete);
63bab717
CB
87lxc_config_define(apparmor_profile);
88lxc_config_define(cap_drop);
89lxc_config_define(cap_keep);
43654d34 90lxc_config_define(cgroup_controller);
54860ed0 91lxc_config_define(cgroup2_controller);
43654d34 92lxc_config_define(cgroup_dir);
28f3b1cd 93lxc_config_define(console_buffer_size);
861813e5 94lxc_config_define(console_logfile);
63bab717 95lxc_config_define(console_path);
861813e5
CB
96lxc_config_define(console_rotate);
97lxc_config_define(console_size);
63bab717
CB
98lxc_config_define(environment);
99lxc_config_define(ephemeral);
100lxc_config_define(execute_cmd);
101lxc_config_define(group);
102lxc_config_define(hooks);
44ae0fb6 103lxc_config_define(hooks_version);
71e287ca 104lxc_config_define(idmaps);
63bab717
CB
105lxc_config_define(includefiles);
106lxc_config_define(init_cmd);
3c491553 107lxc_config_define(init_cwd);
63bab717
CB
108lxc_config_define(init_gid);
109lxc_config_define(init_uid);
46cc906d 110lxc_config_define(log_file);
63bab717
CB
111lxc_config_define(log_level);
112lxc_config_define(log_syslog);
113lxc_config_define(monitor);
71e287ca 114lxc_config_define(mount);
115lxc_config_define(mount_auto);
47148e96 116lxc_config_define(mount_fstab);
1d8d3676 117lxc_config_define(namespace_clone);
abeb5bba 118lxc_config_define(namespace_keep);
b074bbf1 119lxc_config_define(namespace_share);
63bab717 120lxc_config_define(net);
71e287ca 121lxc_config_define(net_flags);
71e287ca 122lxc_config_define(net_hwaddr);
9ff60df2 123lxc_config_define(net_ipv4_address);
71e287ca 124lxc_config_define(net_ipv4_gateway);
2e44ae28 125lxc_config_define(net_ipv6_address);
71e287ca 126lxc_config_define(net_ipv6_gateway);
63bab717
CB
127lxc_config_define(net_link);
128lxc_config_define(net_macvlan_mode);
129lxc_config_define(net_mtu);
130lxc_config_define(net_name);
71e287ca 131lxc_config_define(net_nic);
63bab717
CB
132lxc_config_define(net_script_down);
133lxc_config_define(net_script_up);
134lxc_config_define(net_type);
135lxc_config_define(net_veth_pair);
136lxc_config_define(net_vlan_id);
137lxc_config_define(no_new_privs);
63bab717
CB
138lxc_config_define(personality);
139lxc_config_define(prlimit);
140lxc_config_define(pty_max);
63bab717
CB
141lxc_config_define(rootfs_mount);
142lxc_config_define(rootfs_options);
143lxc_config_define(rootfs_path);
0b427da0 144lxc_config_define(seccomp_profile);
63bab717 145lxc_config_define(selinux_context);
55c84efc 146lxc_config_define(signal_halt);
147lxc_config_define(signal_reboot);
148lxc_config_define(signal_stop);
71e287ca 149lxc_config_define(start);
63bab717
CB
150lxc_config_define(tty_max);
151lxc_config_define(tty_dir);
152lxc_config_define(uts_name);
7edd0540 153lxc_config_define(sysctl);
61d7a733 154lxc_config_define(proc);
c2cc9f0a 155
72d0e1cb 156static struct lxc_config_t config[] = {
6fba98b5
CB
157 { "lxc.arch", set_config_personality, get_config_personality, clr_config_personality, },
158 { "lxc.apparmor.profile", set_config_apparmor_profile, get_config_apparmor_profile, clr_config_apparmor_profile, },
159 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete, get_config_apparmor_allow_incomplete, clr_config_apparmor_allow_incomplete, },
160 { "lxc.autodev", set_config_autodev, get_config_autodev, clr_config_autodev, },
161 { "lxc.cap.drop", set_config_cap_drop, get_config_cap_drop, clr_config_cap_drop, },
162 { "lxc.cap.keep", set_config_cap_keep, get_config_cap_keep, clr_config_cap_keep, },
163 { "lxc.cgroup2", set_config_cgroup2_controller, get_config_cgroup2_controller, clr_config_cgroup2_controller, },
164 { "lxc.cgroup.dir", set_config_cgroup_dir, get_config_cgroup_dir, clr_config_cgroup_dir, },
165 { "lxc.cgroup", set_config_cgroup_controller, get_config_cgroup_controller, clr_config_cgroup_controller, },
6fba98b5
CB
166 { "lxc.console.buffer.size", set_config_console_buffer_size, get_config_console_buffer_size, clr_config_console_buffer_size, },
167 { "lxc.console.logfile", set_config_console_logfile, get_config_console_logfile, clr_config_console_logfile, },
168 { "lxc.console.path", set_config_console_path, get_config_console_path, clr_config_console_path, },
169 { "lxc.console.rotate", set_config_console_rotate, get_config_console_rotate, clr_config_console_rotate, },
861813e5 170 { "lxc.console.size", set_config_console_size, get_config_console_size, clr_config_console_size, },
6fba98b5
CB
171 { "lxc.environment", set_config_environment, get_config_environment, clr_config_environment, },
172 { "lxc.ephemeral", set_config_ephemeral, get_config_ephemeral, clr_config_ephemeral, },
173 { "lxc.execute.cmd", set_config_execute_cmd, get_config_execute_cmd, clr_config_execute_cmd, },
174 { "lxc.group", set_config_group, get_config_group, clr_config_group, },
175 { "lxc.hook.autodev", set_config_hooks, get_config_hooks, clr_config_hooks, },
176 { "lxc.hook.clone", set_config_hooks, get_config_hooks, clr_config_hooks, },
177 { "lxc.hook.destroy", set_config_hooks, get_config_hooks, clr_config_hooks, },
178 { "lxc.hook.mount", set_config_hooks, get_config_hooks, clr_config_hooks, },
179 { "lxc.hook.post-stop", set_config_hooks, get_config_hooks, clr_config_hooks, },
180 { "lxc.hook.pre-mount", set_config_hooks, get_config_hooks, clr_config_hooks, },
181 { "lxc.hook.pre-start", set_config_hooks, get_config_hooks, clr_config_hooks, },
182 { "lxc.hook.start", set_config_hooks, get_config_hooks, clr_config_hooks, },
183 { "lxc.hook.start-host", set_config_hooks, get_config_hooks, clr_config_hooks, },
184 { "lxc.hook.stop", set_config_hooks, get_config_hooks, clr_config_hooks, },
185 { "lxc.hook.version", set_config_hooks_version, get_config_hooks_version, clr_config_hooks_version, },
186 { "lxc.hook", set_config_hooks, get_config_hooks, clr_config_hooks, },
187 { "lxc.idmap", set_config_idmaps, get_config_idmaps, clr_config_idmaps, },
188 { "lxc.include", set_config_includefiles, get_config_includefiles, clr_config_includefiles, },
189 { "lxc.init.cmd", set_config_init_cmd, get_config_init_cmd, clr_config_init_cmd, },
190 { "lxc.init.gid", set_config_init_gid, get_config_init_gid, clr_config_init_gid, },
191 { "lxc.init.uid", set_config_init_uid, get_config_init_uid, clr_config_init_uid, },
192 { "lxc.init.cwd", set_config_init_cwd, get_config_init_cwd, clr_config_init_cwd, },
193 { "lxc.log.file", set_config_log_file, get_config_log_file, clr_config_log_file, },
194 { "lxc.log.level", set_config_log_level, get_config_log_level, clr_config_log_level, },
195 { "lxc.log.syslog", set_config_log_syslog, get_config_log_syslog, clr_config_log_syslog, },
196 { "lxc.monitor.unshare", set_config_monitor, get_config_monitor, clr_config_monitor, },
197 { "lxc.mount.auto", set_config_mount_auto, get_config_mount_auto, clr_config_mount_auto, },
198 { "lxc.mount.entry", set_config_mount, get_config_mount, clr_config_mount, },
199 { "lxc.mount.fstab", set_config_mount_fstab, get_config_mount_fstab, clr_config_mount_fstab, },
200 { "lxc.namespace.clone", set_config_namespace_clone, get_config_namespace_clone, clr_config_namespace_clone, },
201 { "lxc.namespace.keep", set_config_namespace_keep, get_config_namespace_keep, clr_config_namespace_keep, },
202 { "lxc.namespace.share", set_config_namespace_share, get_config_namespace_share, clr_config_namespace_share, },
203 { "lxc.net.flags", set_config_net_flags, get_config_net_flags, clr_config_net_flags, },
204 { "lxc.net.hwaddr", set_config_net_hwaddr, get_config_net_hwaddr, clr_config_net_hwaddr, },
205 { "lxc.net.ipv4.address", set_config_net_ipv4_address, get_config_net_ipv4_address, clr_config_net_ipv4_address, },
206 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway, get_config_net_ipv4_gateway, clr_config_net_ipv4_gateway, },
207 { "lxc.net.ipv6.address", set_config_net_ipv6_address, get_config_net_ipv6_address, clr_config_net_ipv6_address, },
208 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway, get_config_net_ipv6_gateway, clr_config_net_ipv6_gateway, },
209 { "lxc.net.link", set_config_net_link, get_config_net_link, clr_config_net_link, },
210 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode, get_config_net_macvlan_mode, clr_config_net_macvlan_mode, },
211 { "lxc.net.mtu", set_config_net_mtu, get_config_net_mtu, clr_config_net_mtu, },
212 { "lxc.net.name", set_config_net_name, get_config_net_name, clr_config_net_name, },
213 { "lxc.net.script.down", set_config_net_script_down, get_config_net_script_down, clr_config_net_script_down, },
214 { "lxc.net.script.up", set_config_net_script_up, get_config_net_script_up, clr_config_net_script_up, },
215 { "lxc.net.type", set_config_net_type, get_config_net_type, clr_config_net_type, },
216 { "lxc.net.vlan.id", set_config_net_vlan_id, get_config_net_vlan_id, clr_config_net_vlan_id, },
217 { "lxc.net.veth.pair", set_config_net_veth_pair, get_config_net_veth_pair, clr_config_net_veth_pair, },
218 { "lxc.net.", set_config_net_nic, get_config_net_nic, clr_config_net_nic, },
219 { "lxc.net", set_config_net, get_config_net, clr_config_net, },
220 { "lxc.no_new_privs", set_config_no_new_privs, get_config_no_new_privs, clr_config_no_new_privs, },
221 { "lxc.prlimit", set_config_prlimit, get_config_prlimit, clr_config_prlimit, },
222 { "lxc.pty.max", set_config_pty_max, get_config_pty_max, clr_config_pty_max, },
223 { "lxc.rootfs.mount", set_config_rootfs_mount, get_config_rootfs_mount, clr_config_rootfs_mount, },
224 { "lxc.rootfs.options", set_config_rootfs_options, get_config_rootfs_options, clr_config_rootfs_options, },
225 { "lxc.rootfs.path", set_config_rootfs_path, get_config_rootfs_path, clr_config_rootfs_path, },
226 { "lxc.seccomp.profile", set_config_seccomp_profile, get_config_seccomp_profile, clr_config_seccomp_profile, },
227 { "lxc.selinux.context", set_config_selinux_context, get_config_selinux_context, clr_config_selinux_context, },
228 { "lxc.signal.halt", set_config_signal_halt, get_config_signal_halt, clr_config_signal_halt, },
229 { "lxc.signal.reboot", set_config_signal_reboot, get_config_signal_reboot, clr_config_signal_reboot, },
230 { "lxc.signal.stop", set_config_signal_stop, get_config_signal_stop, clr_config_signal_stop, },
231 { "lxc.start.auto", set_config_start, get_config_start, clr_config_start, },
232 { "lxc.start.delay", set_config_start, get_config_start, clr_config_start, },
233 { "lxc.start.order", set_config_start, get_config_start, clr_config_start, },
234 { "lxc.tty.dir", set_config_tty_dir, get_config_tty_dir, clr_config_tty_dir, },
235 { "lxc.tty.max", set_config_tty_max, get_config_tty_max, clr_config_tty_max, },
236 { "lxc.uts.name", set_config_uts_name, get_config_uts_name, clr_config_uts_name, },
237 { "lxc.sysctl", set_config_sysctl, get_config_sysctl, clr_config_sysctl, },
238 { "lxc.proc", set_config_proc, get_config_proc, clr_config_proc, },
a84b9932
AV
239};
240
241struct signame {
242 int num;
74a3920a 243 const char *name;
a84b9932
AV
244};
245
74a3920a 246static const struct signame signames[] = {
504a2217
CB
247 { SIGHUP, "HUP" },
248 { SIGINT, "INT" },
249 { SIGQUIT, "QUIT" },
250 { SIGILL, "ILL" },
251 { SIGABRT, "ABRT" },
252 { SIGFPE, "FPE" },
253 { SIGKILL, "KILL" },
254 { SIGSEGV, "SEGV" },
255 { SIGPIPE, "PIPE" },
256 { SIGALRM, "ALRM" },
257 { SIGTERM, "TERM" },
258 { SIGUSR1, "USR1" },
259 { SIGUSR2, "USR2" },
260 { SIGCHLD, "CHLD" },
261 { SIGCONT, "CONT" },
262 { SIGSTOP, "STOP" },
263 { SIGTSTP, "TSTP" },
264 { SIGTTIN, "TTIN" },
265 { SIGTTOU, "TTOU" },
89dfc302 266#ifdef SIGTRAP
504a2217 267 { SIGTRAP, "TRAP" },
89dfc302
SY
268#endif
269#ifdef SIGIOT
504a2217 270 { SIGIOT, "IOT" },
89dfc302
SY
271#endif
272#ifdef SIGEMT
504a2217 273 { SIGEMT, "EMT" },
89dfc302
SY
274#endif
275#ifdef SIGBUS
504a2217 276 { SIGBUS, "BUS" },
89dfc302
SY
277#endif
278#ifdef SIGSTKFLT
279 { SIGSTKFLT, "STKFLT" },
280#endif
281#ifdef SIGCLD
504a2217 282 { SIGCLD, "CLD" },
89dfc302
SY
283#endif
284#ifdef SIGURG
504a2217 285 { SIGURG, "URG" },
89dfc302
SY
286#endif
287#ifdef SIGXCPU
504a2217 288 { SIGXCPU, "XCPU" },
89dfc302
SY
289#endif
290#ifdef SIGXFSZ
504a2217 291 { SIGXFSZ, "XFSZ" },
89dfc302
SY
292#endif
293#ifdef SIGVTALRM
294 { SIGVTALRM, "VTALRM" },
295#endif
296#ifdef SIGPROF
504a2217 297 { SIGPROF, "PROF" },
89dfc302
SY
298#endif
299#ifdef SIGWINCH
504a2217 300 { SIGWINCH, "WINCH" },
89dfc302
SY
301#endif
302#ifdef SIGIO
504a2217 303 { SIGIO, "IO" },
89dfc302
SY
304#endif
305#ifdef SIGPOLL
504a2217 306 { SIGPOLL, "POLL" },
89dfc302
SY
307#endif
308#ifdef SIGINFO
504a2217 309 { SIGINFO, "INFO" },
89dfc302
SY
310#endif
311#ifdef SIGLOST
504a2217 312 { SIGLOST, "LOST" },
89dfc302
SY
313#endif
314#ifdef SIGPWR
504a2217 315 { SIGPWR, "PWR" },
89dfc302
SY
316#endif
317#ifdef SIGUNUSED
318 { SIGUNUSED, "UNUSED" },
319#endif
320#ifdef SIGSYS
504a2217 321 { SIGSYS, "SYS" },
89dfc302 322#endif
c2cc9f0a 323};
324
504a2217 325static const size_t config_size = sizeof(config) / sizeof(struct lxc_config_t);
c2cc9f0a 326
300df83e 327struct lxc_config_t *lxc_get_config(const char *key)
c2cc9f0a 328{
84760c11 329 size_t i;
c2cc9f0a 330
331 for (i = 0; i < config_size; i++)
504a2217 332 if (!strncmp(config[i].name, key, strlen(config[i].name)))
c2cc9f0a 333 return &config[i];
300df83e 334
c2cc9f0a 335 return NULL;
336}
337
f9373e40
CB
338static int set_config_net(const char *key, const char *value,
339 struct lxc_conf *lxc_conf, void *data)
6b0d5538 340{
663e9916 341 if (!lxc_config_value_empty(value)) {
f9373e40 342 ERROR("lxc.net must not have a value");
6b0d5538
SH
343 return -1;
344 }
345
f9373e40 346 return clr_config_net(key, lxc_conf, data);
6b0d5538
SH
347}
348
f9373e40
CB
349static int set_config_net_type(const char *key, const char *value,
350 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 351{
070a05af 352 struct lxc_netdev *netdev = data;
c2cc9f0a 353
663e9916 354 if (lxc_config_value_empty(value))
f9373e40 355 return clr_config_net_type(key, lxc_conf, data);
c302b476 356
bbc079cf 357 if (!netdev)
c2cc9f0a 358 return -1;
c2cc9f0a 359
bbc079cf 360 if (!strcmp(value, "veth")) {
24654103 361 netdev->type = LXC_NET_VETH;
bbc079cf 362 } else if (!strcmp(value, "macvlan")) {
24654103 363 netdev->type = LXC_NET_MACVLAN;
9b0df30f
CB
364 lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode,
365 "private");
bbc079cf 366 } else if (!strcmp(value, "vlan")) {
24654103 367 netdev->type = LXC_NET_VLAN;
bbc079cf 368 } else if (!strcmp(value, "phys")) {
24654103 369 netdev->type = LXC_NET_PHYS;
bbc079cf 370 } else if (!strcmp(value, "empty")) {
24654103 371 netdev->type = LXC_NET_EMPTY;
bbc079cf 372 } else if (!strcmp(value, "none")) {
26b797f3 373 netdev->type = LXC_NET_NONE;
bbc079cf 374 } else {
36eb9bde 375 ERROR("invalid network type %s", value);
c2cc9f0a 376 return -1;
377 }
bbc079cf 378
c2cc9f0a 379 return 0;
380}
381
f9373e40
CB
382static int set_config_net_flags(const char *key, const char *value,
383 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 384{
070a05af 385 struct lxc_netdev *netdev = data;
c2cc9f0a 386
9d4bf22d 387 if (lxc_config_value_empty(value))
f9373e40 388 return clr_config_net_flags(key, lxc_conf, data);
9d4bf22d 389
33c945e0 390 if (!netdev)
c2cc9f0a 391 return -1;
c2cc9f0a 392
33c945e0 393 netdev->flags |= IFF_UP;
c2cc9f0a 394
33c945e0
MT
395 return 0;
396}
397
b45e32f9 398static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf,
d5aba460 399 struct lxc_netdev *netdev)
576400e5 400{
401 struct ifaddrs *ifaddr, *ifa;
504a2217
CB
402 int n;
403 int ret = 0;
f9373e40
CB
404 const char *type_key = "lxc.net.type";
405 const char *link_key = "lxc.net.link";
576400e5 406 const char *tmpvalue = "phys";
576400e5 407
408 if (getifaddrs(&ifaddr) == -1) {
409 SYSERROR("Get network interfaces failed");
410 return -1;
411 }
412
413 for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
414 if (!ifa->ifa_addr)
415 continue;
416 if (ifa->ifa_addr->sa_family != AF_PACKET)
417 continue;
418
504a2217 419 if (!strncmp(value, ifa->ifa_name, strlen(value) - 1)) {
f9373e40
CB
420 ret = set_config_net_type(type_key, tmpvalue, lxc_conf,
421 netdev);
576400e5 422 if (!ret) {
f9373e40 423 ret = set_config_net_link(
c302b476 424 link_key, ifa->ifa_name, lxc_conf, netdev);
576400e5 425 if (ret) {
426 ERROR("failed to create matched ifnames");
427 break;
428 }
429 } else {
430 ERROR("failed to create matched ifnames");
431 break;
432 }
433 }
434 }
435
504a2217
CB
436 freeifaddrs(ifaddr);
437 ifaddr = NULL;
091045f8 438
576400e5 439 return ret;
440}
441
f9373e40
CB
442static int set_config_net_link(const char *key, const char *value,
443 struct lxc_conf *lxc_conf, void *data)
33c945e0 444{
070a05af 445 struct lxc_netdev *netdev = data;
576400e5 446 int ret = 0;
33c945e0 447
6bed0fb6 448 if (lxc_config_value_empty(value))
f9373e40 449 return clr_config_net_link(key, lxc_conf, data);
6bed0fb6 450
33c945e0 451 if (!netdev)
c2cc9f0a 452 return -1;
c2cc9f0a 453
b45e32f9
CB
454 if (value[strlen(value) - 1] == '+' && netdev->type == LXC_NET_PHYS)
455 ret = create_matched_ifnames(value, lxc_conf, netdev);
456 else
18cd4b54 457 ret = network_ifname(netdev->link, value, sizeof(netdev->link));
576400e5 458
459 return ret;
c2cc9f0a 460}
461
f9373e40
CB
462static int set_config_net_name(const char *key, const char *value,
463 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 464{
070a05af 465 struct lxc_netdev *netdev = data;
c2cc9f0a 466
6bed0fb6 467 if (lxc_config_value_empty(value))
f9373e40 468 return clr_config_net_name(key, lxc_conf, data);
6bed0fb6 469
33c945e0 470 if (!netdev)
c2cc9f0a 471 return -1;
c2cc9f0a 472
18cd4b54 473 return network_ifname(netdev->name, value, sizeof(netdev->name));
33c945e0
MT
474}
475
f9373e40
CB
476static int set_config_net_veth_pair(const char *key, const char *value,
477 struct lxc_conf *lxc_conf, void *data)
e892973e 478{
070a05af 479 struct lxc_netdev *netdev = data;
e892973e 480
6bed0fb6 481 if (lxc_config_value_empty(value))
f9373e40 482 return clr_config_net_veth_pair(key, lxc_conf, data);
6bed0fb6 483
e892973e
DL
484 if (!netdev)
485 return -1;
486
18cd4b54 487 return network_ifname(netdev->priv.veth_attr.pair, value, sizeof(netdev->priv.veth_attr.pair));
e892973e
DL
488}
489
f9373e40
CB
490static int set_config_net_macvlan_mode(const char *key, const char *value,
491 struct lxc_conf *lxc_conf, void *data)
8634bc19 492{
070a05af 493 struct lxc_netdev *netdev = data;
8634bc19 494
6bed0fb6 495 if (lxc_config_value_empty(value))
f9373e40 496 return clr_config_net_macvlan_mode(key, lxc_conf, data);
6bed0fb6 497
8634bc19
MT
498 if (!netdev)
499 return -1;
500
9b0df30f 501 return lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, value);
8634bc19
MT
502}
503
f9373e40
CB
504static int set_config_net_hwaddr(const char *key, const char *value,
505 struct lxc_conf *lxc_conf, void *data)
33c945e0 506{
070a05af 507 struct lxc_netdev *netdev = data;
504a2217 508 char *new_value;
33c945e0 509
6bed0fb6 510 if (lxc_config_value_empty(value))
f9373e40 511 return clr_config_net_hwaddr(key, lxc_conf, data);
6bed0fb6 512
ecbb3790
CB
513 if (!netdev)
514 return -1;
515
504a2217 516 new_value = strdup(value);
d5aba460 517 if (!new_value)
c2cc9f0a 518 return -1;
d5aba460 519
508c263e 520 rand_complete_hwaddr(new_value);
c2cc9f0a 521
663e9916 522 if (lxc_config_value_empty(new_value)) {
508c263e
SH
523 free(new_value);
524 netdev->hwaddr = NULL;
525 return 0;
526 }
527
528 netdev->hwaddr = new_value;
529 return 0;
c2cc9f0a 530}
531
f9373e40
CB
532static int set_config_net_vlan_id(const char *key, const char *value,
533 struct lxc_conf *lxc_conf, void *data)
26c39028 534{
d5aba460 535 int ret;
070a05af 536 struct lxc_netdev *netdev = data;
26c39028 537
6bed0fb6 538 if (lxc_config_value_empty(value))
f9373e40 539 return clr_config_net_vlan_id(key, lxc_conf, data);
6bed0fb6 540
26c39028
JHS
541 if (!netdev)
542 return -1;
543
d5aba460
CB
544 ret = get_u16(&netdev->priv.vlan_attr.vid, value, 0);
545 if (ret < 0)
26c39028
JHS
546 return -1;
547
548 return 0;
549}
550
f9373e40
CB
551static int set_config_net_mtu(const char *key, const char *value,
552 struct lxc_conf *lxc_conf, void *data)
442cbbe6 553{
070a05af 554 struct lxc_netdev *netdev = data;
442cbbe6 555
6bed0fb6 556 if (lxc_config_value_empty(value))
f9373e40 557 return clr_config_net_mtu(key, lxc_conf, data);
6bed0fb6 558
33c945e0 559 if (!netdev)
442cbbe6 560 return -1;
442cbbe6 561
713046e3 562 return set_config_string_item(&netdev->mtu, value);
442cbbe6
TR
563}
564
9ff60df2
CB
565static int set_config_net_ipv4_address(const char *key, const char *value,
566 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 567{
d5aba460 568 int ret;
070a05af 569 struct lxc_netdev *netdev = data;
33c945e0 570 struct lxc_inetdev *inetdev;
c2cc9f0a 571 struct lxc_list *list;
504a2217
CB
572 char *cursor, *slash;
573 char *addr = NULL, *bcast = NULL, *prefix = NULL;
c2cc9f0a 574
663e9916 575 if (lxc_config_value_empty(value))
9ff60df2 576 return clr_config_net_ipv4_address(key, lxc_conf, data);
0797e123 577
33c945e0 578 if (!netdev)
c2cc9f0a 579 return -1;
c2cc9f0a 580
581 inetdev = malloc(sizeof(*inetdev));
d5aba460 582 if (!inetdev)
c2cc9f0a 583 return -1;
d5aba460 584
c2cc9f0a 585 memset(inetdev, 0, sizeof(*inetdev));
586
587 list = malloc(sizeof(*list));
588 if (!list) {
53719062 589 free(inetdev);
c2cc9f0a 590 return -1;
591 }
592
593 lxc_list_init(list);
594 list->elem = inetdev;
595
956edc54
SG
596 addr = strdup(value);
597 if (!addr) {
53719062
SH
598 free(inetdev);
599 free(list);
956edc54
SG
600 return -1;
601 }
c2cc9f0a 602
603 cursor = strstr(addr, " ");
604 if (cursor) {
605 *cursor = '\0';
606 bcast = cursor + 1;
607 }
608
609 slash = strstr(addr, "/");
610 if (slash) {
611 *slash = '\0';
612 prefix = slash + 1;
613 }
614
d5aba460
CB
615 ret = inet_pton(AF_INET, addr, &inetdev->addr);
616 if (!ret || ret < 0) {
617 SYSERROR("Invalid ipv4 address \"%s\"", value);
53719062 618 free(inetdev);
956edc54 619 free(addr);
53719062 620 free(list);
c2cc9f0a 621 return -1;
622 }
623
d5aba460
CB
624 if (bcast) {
625 ret = inet_pton(AF_INET, bcast, &inetdev->bcast);
626 if (!ret || ret < 0) {
627 SYSERROR("Invalid ipv4 broadcast address \"%s\"", value);
628 free(inetdev);
629 free(list);
630 free(addr);
631 return -1;
632 }
633
0093bb8c 634 }
c2cc9f0a 635
504a2217 636 /* No prefix specified, determine it from the network class. */
1c633398 637 if (prefix) {
d5aba460 638 ret = lxc_safe_uint(prefix, &inetdev->prefix);
aa7c5841
DJ
639 if (ret < 0) {
640 free(inetdev);
641 free(list);
642 free(addr);
1c633398 643 return -1;
aa7c5841 644 }
1c633398
CB
645 } else {
646 inetdev->prefix = config_ip_prefix(&inetdev->addr);
647 }
a059591e 648
504a2217
CB
649 /* If no broadcast address, let compute one from the
650 * prefix and address.
0093bb8c
DL
651 */
652 if (!bcast) {
1b7d4743 653 inetdev->bcast.s_addr = inetdev->addr.s_addr;
d5aba460 654 inetdev->bcast.s_addr |= htonl(INADDR_BROADCAST >> inetdev->prefix);
0093bb8c 655 }
c2cc9f0a 656
8538f388 657 lxc_list_add_tail(&netdev->ipv4, list);
c2cc9f0a 658
956edc54 659 free(addr);
c2cc9f0a 660 return 0;
661}
662
f9373e40
CB
663static int set_config_net_ipv4_gateway(const char *key, const char *value,
664 struct lxc_conf *lxc_conf, void *data)
f8fee0e2 665{
070a05af 666 struct lxc_netdev *netdev = data;
f8fee0e2 667
6bed0fb6 668 if (lxc_config_value_empty(value))
f9373e40 669 return clr_config_net_ipv4_gateway(key, lxc_conf, data);
6bed0fb6 670
f8fee0e2
MK
671 if (!netdev)
672 return -1;
673
e088e926 674 free(netdev->ipv4_gateway);
f8fee0e2 675
ab6faf2e 676 if (!strcmp(value, "auto")) {
19a26f82
MK
677 netdev->ipv4_gateway = NULL;
678 netdev->ipv4_gateway_auto = true;
679 } else {
25a908b8 680 int ret;
e088e926
SG
681 struct in_addr *gw;
682
683 gw = malloc(sizeof(*gw));
25a908b8 684 if (!gw)
e088e926 685 return -1;
e088e926 686
25a908b8
CB
687 ret = inet_pton(AF_INET, value, gw);
688 if (!ret || ret < 0) {
689 SYSERROR("Invalid ipv4 gateway address \"%s\"", value);
53719062 690 free(gw);
19a26f82
MK
691 return -1;
692 }
693
694 netdev->ipv4_gateway = gw;
695 netdev->ipv4_gateway_auto = false;
f8fee0e2
MK
696 }
697
f8fee0e2
MK
698 return 0;
699}
700
2e44ae28
CB
701static int set_config_net_ipv6_address(const char *key, const char *value,
702 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 703{
25a908b8 704 int ret;
070a05af 705 struct lxc_netdev *netdev = data;
c2cc9f0a 706 struct lxc_inet6dev *inet6dev;
707 struct lxc_list *list;
504a2217 708 char *slash, *valdup, *netmask;
c2cc9f0a 709
6bed0fb6 710 if (lxc_config_value_empty(value))
2e44ae28 711 return clr_config_net_ipv6_address(key, lxc_conf, data);
6bed0fb6 712
33c945e0 713 if (!netdev)
c2cc9f0a 714 return -1;
c2cc9f0a 715
716 inet6dev = malloc(sizeof(*inet6dev));
25a908b8 717 if (!inet6dev)
c2cc9f0a 718 return -1;
25a908b8 719
c2cc9f0a 720 memset(inet6dev, 0, sizeof(*inet6dev));
721
722 list = malloc(sizeof(*list));
723 if (!list) {
28027320 724 free(inet6dev);
c2cc9f0a 725 return -1;
726 }
727
728 lxc_list_init(list);
729 list->elem = inet6dev;
730
956edc54
SG
731 valdup = strdup(value);
732 if (!valdup) {
28027320
SH
733 free(list);
734 free(inet6dev);
956edc54
SG
735 return -1;
736 }
737
a059591e 738 inet6dev->prefix = 64;
12a50cc6 739 slash = strstr(valdup, "/");
c2cc9f0a 740 if (slash) {
741 *slash = '\0';
742 netmask = slash + 1;
f54f8d0b
CB
743 ret = lxc_safe_uint(netmask, &inet6dev->prefix);
744 if (ret < 0) {
745 free(list);
746 free(inet6dev);
747 free(valdup);
1c633398 748 return -1;
f54f8d0b 749 }
c2cc9f0a 750 }
751
25a908b8
CB
752 ret = inet_pton(AF_INET6, valdup, &inet6dev->addr);
753 if (!ret || ret < 0) {
754 SYSERROR("Invalid ipv6 address \"%s\"", valdup);
28027320
SH
755 free(list);
756 free(inet6dev);
956edc54 757 free(valdup);
c2cc9f0a 758 return -1;
759 }
760
8538f388 761 lxc_list_add_tail(&netdev->ipv6, list);
c2cc9f0a 762
956edc54 763 free(valdup);
c2cc9f0a 764 return 0;
765}
766
f9373e40
CB
767static int set_config_net_ipv6_gateway(const char *key, const char *value,
768 struct lxc_conf *lxc_conf, void *data)
f8fee0e2 769{
070a05af 770 struct lxc_netdev *netdev = data;
f8fee0e2 771
6bed0fb6 772 if (lxc_config_value_empty(value))
f9373e40 773 return clr_config_net_ipv6_gateway(key, lxc_conf, data);
6bed0fb6 774
f8fee0e2
MK
775 if (!netdev)
776 return -1;
777
e088e926 778 free(netdev->ipv6_gateway);
f8fee0e2 779
ab6faf2e 780 if (!strcmp(value, "auto")) {
19a26f82
MK
781 netdev->ipv6_gateway = NULL;
782 netdev->ipv6_gateway_auto = true;
783 } else {
25a908b8 784 int ret;
8fb86a37
SH
785 struct in6_addr *gw;
786
bec695f3 787 gw = malloc(sizeof(*gw));
25a908b8 788 if (!gw)
bec695f3 789 return -1;
bec695f3 790
25a908b8
CB
791 ret = inet_pton(AF_INET6, value, gw);
792 if (!ret || ret < 0) {
793 SYSERROR("Invalid ipv6 gateway address \"%s\"", value);
28027320 794 free(gw);
19a26f82
MK
795 return -1;
796 }
797
798 netdev->ipv6_gateway = gw;
799 netdev->ipv6_gateway_auto = false;
f8fee0e2
MK
800 }
801
f8fee0e2
MK
802 return 0;
803}
804
f9373e40
CB
805static int set_config_net_script_up(const char *key, const char *value,
806 struct lxc_conf *lxc_conf, void *data)
e3b4c4c4 807{
070a05af 808 struct lxc_netdev *netdev = data;
e3b4c4c4 809
6bed0fb6 810 if (lxc_config_value_empty(value))
f9373e40 811 return clr_config_net_script_up(key, lxc_conf, data);
6bed0fb6 812
e3b4c4c4 813 if (!netdev)
504a2217 814 return -1;
e3b4c4c4 815
713046e3 816 return set_config_string_item(&netdev->upscript, value);
8fc8295a
DE
817}
818
f9373e40
CB
819static int set_config_net_script_down(const char *key, const char *value,
820 struct lxc_conf *lxc_conf, void *data)
8fc8295a 821{
070a05af 822 struct lxc_netdev *netdev = data;
8fc8295a 823
6bed0fb6 824 if (lxc_config_value_empty(value))
f9373e40 825 return clr_config_net_script_down(key, lxc_conf, data);
6bed0fb6 826
8fc8295a 827 if (!netdev)
504a2217 828 return -1;
8fc8295a 829
713046e3 830 return set_config_string_item(&netdev->downscript, value);
e3b4c4c4
ST
831}
832
26ddeedd
SH
833static int add_hook(struct lxc_conf *lxc_conf, int which, char *hook)
834{
835 struct lxc_list *hooklist;
836
837 hooklist = malloc(sizeof(*hooklist));
838 if (!hooklist) {
839 free(hook);
840 return -1;
841 }
504a2217 842
26ddeedd
SH
843 hooklist->elem = hook;
844 lxc_list_add_tail(&lxc_conf->hooks[which], hooklist);
845 return 0;
846}
847
0b427da0
CB
848static int set_config_seccomp_profile(const char *key, const char *value,
849 struct lxc_conf *lxc_conf, void *data)
8f2c3a70 850{
713046e3 851 return set_config_path_item(&lxc_conf->seccomp, value);
8f2c3a70
SH
852}
853
5cda27c1
SH
854static int set_config_execute_cmd(const char *key, const char *value,
855 struct lxc_conf *lxc_conf, void *data)
856{
857 return set_config_path_item(&lxc_conf->execute_cmd, value);
858}
859
713046e3 860static int set_config_init_cmd(const char *key, const char *value,
c7e27aaf 861 struct lxc_conf *lxc_conf, void *data)
67c660d0 862{
713046e3 863 return set_config_path_item(&lxc_conf->init_cmd, value);
67c660d0
SG
864}
865
3c491553
L
866static int set_config_init_cwd(const char *key, const char *value,
867 struct lxc_conf *lxc_conf, void *data)
868{
869 return set_config_path_item(&lxc_conf->init_cwd, value);
870}
871
713046e3 872static int set_config_init_uid(const char *key, const char *value,
c7e27aaf 873 struct lxc_conf *lxc_conf, void *data)
72bb04e4 874{
d1e5d636
CB
875 unsigned int init_uid;
876
663e9916 877 if (lxc_config_value_empty(value)) {
2e7cde40 878 lxc_conf->init_uid = 0;
fee80911 879 return 0;
2e7cde40 880 }
fee80911 881
d1e5d636
CB
882 if (lxc_safe_uint(value, &init_uid) < 0)
883 return -1;
25a908b8 884
d1e5d636
CB
885 lxc_conf->init_uid = init_uid;
886
72bb04e4
PT
887 return 0;
888}
889
713046e3 890static int set_config_init_gid(const char *key, const char *value,
c7e27aaf 891 struct lxc_conf *lxc_conf, void *data)
72bb04e4 892{
d1e5d636
CB
893 unsigned int init_gid;
894
663e9916 895 if (lxc_config_value_empty(value)) {
2debb6e6 896 lxc_conf->init_gid = 0;
a757cc7d 897 return 0;
2debb6e6 898 }
a757cc7d 899
d1e5d636
CB
900 if (lxc_safe_uint(value, &init_gid) < 0)
901 return -1;
25a908b8 902
d1e5d636
CB
903 lxc_conf->init_gid = init_gid;
904
72bb04e4
PT
905 return 0;
906}
907
466c2e93 908static int set_config_hooks(const char *key, const char *value,
c7e27aaf 909 struct lxc_conf *lxc_conf, void *data)
26ddeedd 910{
7d0eb87e 911 char *copy;
72bb04e4 912
663e9916 913 if (lxc_config_value_empty(value))
7d0eb87e
SH
914 return lxc_clear_hooks(lxc_conf, key);
915
a182feae 916 if (strcmp(key + 4, "hook") == 0) {
25a908b8 917 ERROR("lxc.hook must not have a value");
6b0d5538
SH
918 return -1;
919 }
25a908b8 920
7d0eb87e 921 copy = strdup(value);
25a908b8 922 if (!copy)
26ddeedd 923 return -1;
504a2217 924
a182feae 925 if (strcmp(key + 9, "pre-start") == 0)
26ddeedd 926 return add_hook(lxc_conf, LXCHOOK_PRESTART, copy);
08dd2805
SH
927 else if (strcmp(key + 9, "start-host") == 0)
928 return add_hook(lxc_conf, LXCHOOK_START_HOST, copy);
a182feae 929 else if (strcmp(key + 9, "pre-mount") == 0)
5ea6163a 930 return add_hook(lxc_conf, LXCHOOK_PREMOUNT, copy);
a182feae 931 else if (strcmp(key + 9, "autodev") == 0)
f7bee6c6 932 return add_hook(lxc_conf, LXCHOOK_AUTODEV, copy);
a182feae 933 else if (strcmp(key + 9, "mount") == 0)
26ddeedd 934 return add_hook(lxc_conf, LXCHOOK_MOUNT, copy);
a182feae 935 else if (strcmp(key + 9, "start") == 0)
26ddeedd 936 return add_hook(lxc_conf, LXCHOOK_START, copy);
a182feae 937 else if (strcmp(key + 9, "stop") == 0)
52492063 938 return add_hook(lxc_conf, LXCHOOK_STOP, copy);
a182feae 939 else if (strcmp(key + 9, "post-stop") == 0)
26ddeedd 940 return add_hook(lxc_conf, LXCHOOK_POSTSTOP, copy);
a182feae 941 else if (strcmp(key + 9, "clone") == 0)
148e91f5 942 return add_hook(lxc_conf, LXCHOOK_CLONE, copy);
a182feae 943 else if (strcmp(key + 9, "destroy") == 0)
37cf711b 944 return add_hook(lxc_conf, LXCHOOK_DESTROY, copy);
504a2217 945
26ddeedd
SH
946 free(copy);
947 return -1;
948}
949
44ae0fb6
CB
950static int set_config_hooks_version(const char *key, const char *value,
951 struct lxc_conf *lxc_conf, void *data)
952{
953 int ret;
954 unsigned int tmp;
955
956 if (lxc_config_value_empty(value))
957 return clr_config_hooks_version(key, lxc_conf, NULL);
958
959 ret = lxc_safe_uint(value, &tmp);
960 if (ret < 0)
961 return -1;
962
963 if (tmp > 1) {
964 ERROR("Invalid hook version specified. Currently only 0 "
965 "(legacy) and 1 are supported");
966 return -1;
967 }
968
969 lxc_conf->hooks_version = tmp;
970 return 0;
971}
972
713046e3 973static int set_config_personality(const char *key, const char *value,
c7e27aaf 974 struct lxc_conf *lxc_conf, void *data)
cccc74b5 975{
525f0002 976 signed long personality = lxc_config_parse_arch(value);
cccc74b5 977
525f0002
CS
978 if (personality >= 0)
979 lxc_conf->personality = personality;
980 else
25a908b8 981 WARN("Unsupported personality \"%s\"", value);
970ab589
DL
982
983 return 0;
cccc74b5
DL
984}
985
232763d6
CB
986static int set_config_pty_max(const char *key, const char *value,
987 struct lxc_conf *lxc_conf, void *data)
10db618d 988{
e528c735
CB
989 int ret;
990 unsigned int max = 0;
991
663e9916 992 if (lxc_config_value_empty(value)) {
e528c735 993 lxc_conf->pty_max = 0;
884a4580 994 return 0;
ec200ce9 995 }
884a4580 996
e528c735
CB
997 ret = lxc_safe_uint(value, &max);
998 if (ret < 0)
17919969 999 return -1;
10db618d 1000
e528c735 1001 lxc_conf->pty_max = max;
10db618d 1002 return 0;
1003}
1004
a182feae
CB
1005/* We only need to check whether the first byte of the key after the lxc.start.
1006 * prefix matches our expectations since they fortunately all start with a
1007 * different letter. If anything was wrong with the key we would have already
1008 * noticed when the callback was called.
1009 */
713046e3 1010static int set_config_start(const char *key, const char *value,
c7e27aaf 1011 struct lxc_conf *lxc_conf, void *data)
ee1e7aa0 1012{
ebb80f95
CB
1013 bool is_empty;
1014
663e9916 1015 is_empty = lxc_config_value_empty(value);
ebb80f95 1016
a182feae 1017 if (*(key + 10) == 'a') { /* lxc.start.auto */
ebb80f95
CB
1018 if (is_empty) {
1019 lxc_conf->start_auto = 0;
1020 return 0;
1021 }
61ff8fc8 1022
3590152f
CB
1023 if (lxc_safe_uint(value, &lxc_conf->start_auto) < 0)
1024 return -1;
ebb80f95 1025
3590152f
CB
1026 if (lxc_conf->start_auto > 1)
1027 return -1;
ebb80f95 1028
ee1e7aa0 1029 return 0;
a182feae 1030 } else if (*(key + 10) == 'd') { /* lxc.start.delay */
ebb80f95
CB
1031 if (is_empty) {
1032 lxc_conf->start_delay = 0;
1033 return 0;
1034 }
1035
ebb80f95 1036 return lxc_safe_uint(value, &lxc_conf->start_delay);
a182feae 1037 } else if (*(key + 10) == 'o') { /* lxc.start.order */
ebb80f95
CB
1038 if (is_empty) {
1039 lxc_conf->start_order = 0;
1040 return 0;
1041 }
1042
ebb80f95 1043 return lxc_safe_int(value, &lxc_conf->start_order);
ee1e7aa0 1044 }
ebb80f95 1045
ee1e7aa0
SG
1046 return -1;
1047}
1048
713046e3 1049static int set_config_monitor(const char *key, const char *value,
c7e27aaf 1050 struct lxc_conf *lxc_conf, void *data)
a8dfe4e0 1051{
663e9916 1052 if (lxc_config_value_empty(value)) {
4ad9cd26 1053 lxc_conf->monitor_unshare = 0;
a8dfe4e0
WB
1054 return 0;
1055 }
4ad9cd26 1056
a182feae 1057 if (strcmp(key + 12, "unshare") == 0)
4ad9cd26
CB
1058 return lxc_safe_uint(value, &lxc_conf->monitor_unshare);
1059
a8dfe4e0
WB
1060 return -1;
1061}
1062
713046e3 1063static int set_config_group(const char *key, const char *value,
c7e27aaf 1064 struct lxc_conf *lxc_conf, void *data)
ee1e7aa0
SG
1065{
1066 char *groups, *groupptr, *sptr, *token;
1067 struct lxc_list *grouplist;
1068 int ret = -1;
1069
663e9916 1070 if (lxc_config_value_empty(value))
ee1e7aa0
SG
1071 return lxc_clear_groups(lxc_conf);
1072
1073 groups = strdup(value);
25a908b8 1074 if (!groups)
ee1e7aa0 1075 return -1;
ee1e7aa0 1076
25a908b8
CB
1077 /* In case several groups are specified in a single line split these
1078 * groups in a single element for the list.
504a2217
CB
1079 */
1080 for (groupptr = groups;; groupptr = NULL) {
d028235d
SG
1081 token = strtok_r(groupptr, " \t", &sptr);
1082 if (!token) {
ee1e7aa0 1083 ret = 0;
d028235d 1084 break;
ee1e7aa0
SG
1085 }
1086
1087 grouplist = malloc(sizeof(*grouplist));
25a908b8 1088 if (!grouplist)
ee1e7aa0 1089 break;
ee1e7aa0
SG
1090
1091 grouplist->elem = strdup(token);
1092 if (!grouplist->elem) {
ee1e7aa0
SG
1093 free(grouplist);
1094 break;
1095 }
1096
1097 lxc_list_add_tail(&lxc_conf->groups, grouplist);
d028235d 1098 }
ee1e7aa0
SG
1099
1100 free(groups);
ee1e7aa0
SG
1101 return ret;
1102}
1103
713046e3 1104static int set_config_environment(const char *key, const char *value,
c7e27aaf 1105 struct lxc_conf *lxc_conf, void *data)
7c661726
MP
1106{
1107 struct lxc_list *list_item = NULL;
1108
663e9916 1109 if (lxc_config_value_empty(value))
ab799c0b
SG
1110 return lxc_clear_environment(lxc_conf);
1111
7c661726
MP
1112 list_item = malloc(sizeof(*list_item));
1113 if (!list_item)
504a2217 1114 goto on_error;
7c661726 1115
5eab47bc
CB
1116 if (!strchr(value, '=')) {
1117 const char *env_val;
1118 const char *env_key = value;
1119 const char *env_var[3] = {0};
1120
1121 env_val = getenv(env_key);
1122 if (!env_val)
1123 goto on_error;
1124
1125 env_var[0] = env_key;
1126 env_var[1] = env_val;
1127 list_item->elem = lxc_string_join("=", env_var, false);
1128 } else {
1129 list_item->elem = strdup(value);
1130 }
7c661726
MP
1131
1132 if (!list_item->elem)
504a2217 1133 goto on_error;
7c661726
MP
1134
1135 lxc_list_add_tail(&lxc_conf->environment, list_item);
1136
1137 return 0;
1138
504a2217 1139on_error:
f10fad2f 1140 free(list_item);
7c661726
MP
1141 return -1;
1142}
1143
fe1c5887
CB
1144static int set_config_tty_max(const char *key, const char *value,
1145 struct lxc_conf *lxc_conf, void *data)
b0a33c1e 1146{
885766f5
CB
1147 int ret;
1148 unsigned int nbtty = 0;
1149
663e9916 1150 if (lxc_config_value_empty(value)) {
885766f5 1151 lxc_conf->ttys.max = 0;
fb12b12a 1152 return 0;
cb508ee8 1153 }
fb12b12a 1154
885766f5
CB
1155 ret = lxc_safe_uint(value, &nbtty);
1156 if (ret < 0)
1157 return -1;
1158
1159 lxc_conf->ttys.max = nbtty;
1160 return 0;
b0a33c1e 1161}
1162
42e53c29 1163static int set_config_tty_dir(const char *key, const char *value,
c7e27aaf 1164 struct lxc_conf *lxc_conf, void *data)
7c6ef2a2 1165{
885766f5 1166 return set_config_string_item_max(&lxc_conf->ttys.dir, value,
504a2217 1167 NAME_MAX + 1);
7c6ef2a2
SH
1168}
1169
953fe44f
CB
1170static int set_config_apparmor_profile(const char *key, const char *value,
1171 struct lxc_conf *lxc_conf, void *data)
e075f5d9 1172{
713046e3 1173 return set_config_string_item(&lxc_conf->lsm_aa_profile, value);
fe4de9a6
DE
1174}
1175
953fe44f
CB
1176static int set_config_apparmor_allow_incomplete(const char *key,
1177 const char *value,
1178 struct lxc_conf *lxc_conf,
1179 void *data)
7aff4f43 1180{
663e9916 1181 if (lxc_config_value_empty(value)) {
cccfa758 1182 lxc_conf->lsm_aa_allow_incomplete = 0;
a678e9fa 1183 return 0;
cccfa758 1184 }
a678e9fa 1185
a56e2df9
CB
1186 if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete) < 0)
1187 return -1;
7aff4f43 1188
25a908b8 1189 if (lxc_conf->lsm_aa_allow_incomplete > 1)
a56e2df9 1190 return -1;
7aff4f43
SH
1191
1192 return 0;
1193}
1194
953fe44f
CB
1195static int set_config_selinux_context(const char *key, const char *value,
1196 struct lxc_conf *lxc_conf, void *data)
fe4de9a6 1197{
713046e3 1198 return set_config_string_item(&lxc_conf->lsm_se_context, value);
e075f5d9 1199}
e075f5d9 1200
46cc906d 1201static int set_config_log_file(const char *key, const char *value,
c7e27aaf 1202 struct lxc_conf *c, void *data)
4a85ce2a 1203{
6d03d92a
DE
1204 int ret;
1205
663e9916 1206 if (lxc_config_value_empty(value)) {
0d601acb
CB
1207 free(c->logfile);
1208 c->logfile = NULL;
1209 return 0;
1210 }
1211
1212 /* Store these values in the lxc_conf, and then try to set for actual
504a2217
CB
1213 * current logging.
1214 */
713046e3 1215 ret = set_config_path_item(&c->logfile, value);
6d03d92a 1216 if (ret == 0)
858377e4 1217 ret = lxc_log_set_file(&c->logfd, c->logfile);
25a908b8 1218
6d03d92a 1219 return ret;
4a85ce2a
SH
1220}
1221
46cc906d 1222static int set_config_log_level(const char *key, const char *value,
c7e27aaf 1223 struct lxc_conf *lxc_conf, void *data)
4a85ce2a 1224{
9ea87d5d
SH
1225 int newlevel;
1226
663e9916 1227 if (lxc_config_value_empty(value)) {
4b73005c 1228 lxc_conf->loglevel = LXC_LOG_LEVEL_NOTSET;
4a85ce2a 1229 return 0;
575b9745 1230 }
4a85ce2a 1231
a56e2df9
CB
1232 if (value[0] >= '0' && value[0] <= '9') {
1233 if (lxc_safe_int(value, &newlevel) < 0)
1234 return -1;
1235 } else {
9ea87d5d 1236 newlevel = lxc_log_priority_to_int(value);
a56e2df9 1237 }
575b9745 1238
504a2217 1239 /* Store these values in the lxc_conf, and then try to set for actual
575b9745
CB
1240 * current logging.
1241 */
b40a606e 1242 lxc_conf->loglevel = newlevel;
858377e4 1243 return lxc_log_set_level(&lxc_conf->loglevel, newlevel);
4a85ce2a
SH
1244}
1245
713046e3 1246static int set_config_autodev(const char *key, const char *value,
c7e27aaf 1247 struct lxc_conf *lxc_conf, void *data)
c6883f38 1248{
663e9916 1249 if (lxc_config_value_empty(value)) {
1045031e 1250 lxc_conf->autodev = 0;
180abbc0 1251 return 0;
1045031e 1252 }
180abbc0 1253
ff6cb4ed
CB
1254 if (lxc_safe_uint(value, &lxc_conf->autodev) < 0)
1255 return -1;
c6883f38 1256
25a908b8 1257 if (lxc_conf->autodev > 1)
ff6cb4ed 1258 return -1;
c6883f38
SH
1259
1260 return 0;
1261}
1262
a84b9932
AV
1263static int sig_num(const char *sig)
1264{
f2e539b3 1265 unsigned int signum;
a84b9932 1266
f2e539b3 1267 if (lxc_safe_uint(sig, &signum) < 0)
a84b9932 1268 return -1;
f2e539b3
CB
1269
1270 return signum;
a84b9932
AV
1271}
1272
1273static int rt_sig_num(const char *signame)
1274{
504a2217 1275 int rtmax = 0, sig_n = 0;
a84b9932
AV
1276
1277 if (strncasecmp(signame, "max-", 4) == 0) {
1278 rtmax = 1;
1279 }
504a2217 1280
a84b9932
AV
1281 signame += 4;
1282 if (!isdigit(*signame))
1283 return -1;
504a2217 1284
a84b9932
AV
1285 sig_n = sig_num(signame);
1286 sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n;
1287 if (sig_n > SIGRTMAX || sig_n < SIGRTMIN)
1288 return -1;
504a2217 1289
a84b9932
AV
1290 return sig_n;
1291}
1292
504a2217
CB
1293static int sig_parse(const char *signame)
1294{
84760c11 1295 size_t n;
a84b9932
AV
1296
1297 if (isdigit(*signame)) {
1298 return sig_num(signame);
1299 } else if (strncasecmp(signame, "sig", 3) == 0) {
1300 signame += 3;
1301 if (strncasecmp(signame, "rt", 2) == 0)
1302 return rt_sig_num(signame + 2);
1303 for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++) {
504a2217 1304 if (strcasecmp(signames[n].name, signame) == 0)
a84b9932
AV
1305 return signames[n].num;
1306 }
1307 }
504a2217 1308
a84b9932
AV
1309 return -1;
1310}
1311
55c84efc 1312static int set_config_signal_halt(const char *key, const char *value,
c7e27aaf 1313 struct lxc_conf *lxc_conf, void *data)
f0f1d8c0 1314{
62a085fb 1315 int sig_n;
f0f1d8c0 1316
663e9916 1317 if (lxc_config_value_empty(value)) {
c1a64603 1318 lxc_conf->haltsignal = 0;
955912f0 1319 return 0;
c1a64603 1320 }
955912f0 1321
62a085fb 1322 sig_n = sig_parse(value);
f0f1d8c0
DE
1323 if (sig_n < 0)
1324 return -1;
25a908b8 1325
f0f1d8c0
DE
1326 lxc_conf->haltsignal = sig_n;
1327
1328 return 0;
1329}
1330
55c84efc 1331static int set_config_signal_reboot(const char *key, const char *value,
c7e27aaf 1332 struct lxc_conf *lxc_conf, void *data)
dd267776 1333{
9d7e7587 1334 int sig_n;
dd267776 1335
663e9916 1336 if (lxc_config_value_empty(value)) {
18fcee44 1337 lxc_conf->rebootsignal = 0;
9d7e7587 1338 return 0;
18fcee44 1339 }
9d7e7587
CB
1340
1341 sig_n = sig_parse(value);
dd267776
BP
1342 if (sig_n < 0)
1343 return -1;
25a908b8 1344
dd267776
BP
1345 lxc_conf->rebootsignal = sig_n;
1346
1347 return 0;
1348}
1349
55c84efc 1350static int set_config_signal_stop(const char *key, const char *value,
c7e27aaf 1351 struct lxc_conf *lxc_conf, void *data)
a84b9932 1352{
6ca6aedd 1353 int sig_n;
a84b9932 1354
663e9916 1355 if (lxc_config_value_empty(value)) {
4100d1a7 1356 lxc_conf->stopsignal = 0;
6ca6aedd 1357 return 0;
4100d1a7 1358 }
6ca6aedd
CB
1359
1360 sig_n = sig_parse(value);
a84b9932
AV
1361 if (sig_n < 0)
1362 return -1;
25a908b8 1363
a84b9932
AV
1364 lxc_conf->stopsignal = sig_n;
1365
1366 return 0;
1367}
1368
54860ed0
CB
1369static int __set_config_cgroup_controller(const char *key, const char *value,
1370 struct lxc_conf *lxc_conf, int version)
576f946d 1371{
54860ed0
CB
1372 const char *subkey, *token;
1373 size_t token_len;
bf83c5b9
MS
1374 struct lxc_list *cglist = NULL;
1375 struct lxc_cgroup *cgelem = NULL;
576f946d 1376
663e9916 1377 if (lxc_config_value_empty(value))
54860ed0
CB
1378 return lxc_clear_cgroups(lxc_conf, key, version);
1379
1380 if (version == CGROUP2_SUPER_MAGIC) {
1381 token = "lxc.cgroup2.";
1382 token_len = 12;
1383 } else if (version == CGROUP_SUPER_MAGIC) {
1384 token = "lxc.cgroup.";
1385 token_len = 11;
1386 } else {
1387 return -EINVAL;
1388 }
576f946d 1389
54860ed0
CB
1390 if (strncmp(key, token, token_len) != 0)
1391 return -EINVAL;
a871ff6b 1392
54860ed0
CB
1393 subkey = key + token_len;
1394 if (*subkey == '\0')
1395 return -EINVAL;
576f946d 1396
1397 cglist = malloc(sizeof(*cglist));
1398 if (!cglist)
bf83c5b9 1399 goto out;
576f946d 1400
1401 cgelem = malloc(sizeof(*cgelem));
bf83c5b9
MS
1402 if (!cgelem)
1403 goto out;
1404 memset(cgelem, 0, sizeof(*cgelem));
576f946d 1405
1406 cgelem->subsystem = strdup(subkey);
54860ed0
CB
1407 if (!cgelem->subsystem)
1408 goto out;
bf83c5b9 1409
54860ed0
CB
1410 cgelem->value = strdup(value);
1411 if (!cgelem->value)
bf83c5b9
MS
1412 goto out;
1413
54860ed0
CB
1414 cgelem->version = version;
1415
1416 lxc_list_add_elem(cglist, cgelem);
576f946d 1417
54860ed0
CB
1418 if (version == CGROUP2_SUPER_MAGIC)
1419 lxc_list_add_tail(&lxc_conf->cgroup2, cglist);
1420 else
1421 lxc_list_add_tail(&lxc_conf->cgroup, cglist);
576f946d 1422
1423 return 0;
bf83c5b9
MS
1424
1425out:
f10fad2f 1426 free(cglist);
bf83c5b9 1427 if (cgelem) {
f10fad2f 1428 free(cgelem->subsystem);
f10fad2f 1429 free(cgelem->value);
bf83c5b9
MS
1430 free(cgelem);
1431 }
1432
1433 return -1;
576f946d 1434}
1435
54860ed0
CB
1436static int set_config_cgroup_controller(const char *key, const char *value,
1437 struct lxc_conf *lxc_conf, void *data)
1438{
1439 return __set_config_cgroup_controller(key, value, lxc_conf,
1440 CGROUP_SUPER_MAGIC);
1441}
1442
1443static int set_config_cgroup2_controller(const char *key, const char *value,
1444 struct lxc_conf *lxc_conf, void *data)
1445{
1446 return __set_config_cgroup_controller(key, value, lxc_conf,
1447 CGROUP2_SUPER_MAGIC);
1448}
1449
1450
43654d34
CB
1451static int set_config_cgroup_dir(const char *key, const char *value,
1452 struct lxc_conf *lxc_conf, void *data)
1453{
1454 if (lxc_config_value_empty(value))
1455 return clr_config_cgroup_dir(key, lxc_conf, NULL);
1456
43654d34
CB
1457 return set_config_string_item(&lxc_conf->cgroup_meta.dir, value);
1458}
1459
240d4b74 1460static int set_config_prlimit(const char *key, const char *value,
c7e27aaf 1461 struct lxc_conf *lxc_conf, void *data)
c6d09e15 1462{
c6d09e15
WB
1463 struct lxc_list *iter;
1464 struct rlimit limit;
71460831 1465 rlim_t limit_value;
504a2217
CB
1466 struct lxc_list *limlist = NULL;
1467 struct lxc_limit *limelem = NULL;
c6d09e15 1468
663e9916 1469 if (lxc_config_value_empty(value))
c6d09e15
WB
1470 return lxc_clear_limits(lxc_conf, key);
1471
240d4b74 1472 if (strncmp(key, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
c6d09e15
WB
1473 return -1;
1474
240d4b74 1475 key += sizeof("lxc.prlimit.") - 1;
c6d09e15
WB
1476
1477 /* soft limit comes first in the value */
1478 if (!parse_limit_value(&value, &limit_value))
1479 return -1;
1480 limit.rlim_cur = limit_value;
1481
1482 /* skip spaces and a colon */
1483 while (isspace(*value))
1484 ++value;
504a2217 1485
c6d09e15
WB
1486 if (*value == ':')
1487 ++value;
1488 else if (*value) /* any other character is an error here */
1489 return -1;
504a2217 1490
c6d09e15
WB
1491 while (isspace(*value))
1492 ++value;
1493
1494 /* optional hard limit */
1495 if (*value) {
1496 if (!parse_limit_value(&value, &limit_value))
1497 return -1;
1498 limit.rlim_max = limit_value;
504a2217 1499
c6d09e15
WB
1500 /* check for trailing garbage */
1501 while (isspace(*value))
1502 ++value;
504a2217 1503
c6d09e15
WB
1504 if (*value)
1505 return -1;
1506 } else {
1507 /* a single value sets both hard and soft limit */
1508 limit.rlim_max = limit.rlim_cur;
1509 }
1510
1511 /* find existing list element */
7edd0540 1512 lxc_list_for_each(iter, &lxc_conf->limits) {
c6d09e15
WB
1513 limelem = iter->elem;
1514 if (!strcmp(key, limelem->resource)) {
1515 limelem->limit = limit;
1516 return 0;
1517 }
1518 }
1519
1520 /* allocate list element */
1521 limlist = malloc(sizeof(*limlist));
1522 if (!limlist)
7edd0540 1523 goto on_error;
2e6e3feb 1524
c6d09e15
WB
1525 limelem = malloc(sizeof(*limelem));
1526 if (!limelem)
7edd0540 1527 goto on_error;
c6d09e15
WB
1528 memset(limelem, 0, sizeof(*limelem));
1529
1530 limelem->resource = strdup(key);
1531 if (!limelem->resource)
7edd0540 1532 goto on_error;
c6d09e15
WB
1533 limelem->limit = limit;
1534
7edd0540 1535 lxc_list_add_elem(limlist, limelem);;
c6d09e15
WB
1536
1537 lxc_list_add_tail(&lxc_conf->limits, limlist);
1538
1539 return 0;
1540
7edd0540 1541on_error:
c6d09e15
WB
1542 free(limlist);
1543 if (limelem) {
1544 free(limelem->resource);
1545 free(limelem);
1546 }
1547 return -1;
1548}
1549
7edd0540
L
1550static int set_config_sysctl(const char *key, const char *value,
1551 struct lxc_conf *lxc_conf, void *data)
1552{
1553 struct lxc_list *iter;
e409b214 1554 char *replace_value = NULL;
7edd0540
L
1555 struct lxc_list *sysctl_list = NULL;
1556 struct lxc_sysctl *sysctl_elem = NULL;
7edd0540
L
1557
1558 if (lxc_config_value_empty(value))
e409b214 1559 return clr_config_sysctl(key, lxc_conf, NULL);
7edd0540
L
1560
1561 if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1562 return -1;
1563
1564 key += sizeof("lxc.sysctl.") - 1;
1565
1566 /* find existing list element */
1567 lxc_list_for_each(iter, &lxc_conf->sysctls) {
1568 sysctl_elem = iter->elem;
e409b214
CB
1569
1570 if (strcmp(key, sysctl_elem->key) != 0)
1571 continue;
1572
1573 replace_value = strdup(value);
1574 if (!replace_value)
1575 return -1;
1576
1577 free(sysctl_elem->value);
1578 sysctl_elem->value = replace_value;
1579 return 0;
7edd0540
L
1580 }
1581
1582 /* allocate list element */
1583 sysctl_list = malloc(sizeof(*sysctl_list));
1584 if (!sysctl_list)
1585 goto on_error;
1586
1587 sysctl_elem = malloc(sizeof(*sysctl_elem));
1588 if (!sysctl_elem)
1589 goto on_error;
1590 memset(sysctl_elem, 0, sizeof(*sysctl_elem));
1591
1592 sysctl_elem->key = strdup(key);
1593 if (!sysctl_elem->key)
1594 goto on_error;
1595
1596 sysctl_elem->value = strdup(value);
1597 if (!sysctl_elem->value)
1598 goto on_error;
1599
1600 lxc_list_add_elem(sysctl_list, sysctl_elem);
1601
1602 lxc_list_add_tail(&lxc_conf->sysctls, sysctl_list);
1603
1604 return 0;
1605
1606on_error:
1607 free(sysctl_list);
1608 if (sysctl_elem) {
1609 free(sysctl_elem->key);
1610 free(sysctl_elem->value);
1611 free(sysctl_elem);
1612 }
1613 return -1;
7edd0540
L
1614}
1615
61d7a733
YT
1616static int set_config_proc(const char *key, const char *value,
1617 struct lxc_conf *lxc_conf, void *data)
1618{
1619 const char *subkey;
1620 struct lxc_list *proclist = NULL;
1621 struct lxc_proc *procelem = NULL;
1622
1623 if (lxc_config_value_empty(value))
1624 return clr_config_proc(key, lxc_conf, NULL);
1625
1626 if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1627 return -1;
1628
1629 subkey = key + sizeof("lxc.proc.") - 1;
1630 if (*subkey == '\0')
1631 return -EINVAL;
1632
1633 proclist = malloc(sizeof(*proclist));
1634 if (!proclist)
1635 goto on_error;
1636
1637 procelem = malloc(sizeof(*procelem));
1638 if (!procelem)
1639 goto on_error;
1640 memset(procelem, 0, sizeof(*procelem));
1641
1642 procelem->filename = strdup(subkey);
1643 procelem->value = strdup(value);
1644
1645 if (!procelem->filename || !procelem->value)
1646 goto on_error;
1647
1648 proclist->elem = procelem;
1649
1650 lxc_list_add_tail(&lxc_conf->procs, proclist);
1651
1652 return 0;
1653
1654on_error:
1655 free(proclist);
1656 if (procelem) {
1657 free(procelem->filename);
1658 free(procelem->value);
1659 free(procelem);
1660 }
1661
1662 return -1;
1663}
1664
5014ff2e 1665static int set_config_idmaps(const char *key, const char *value,
c7e27aaf 1666 struct lxc_conf *lxc_conf, void *data)
f6d3e3e4 1667{
251d0d2a 1668 unsigned long hostid, nsid, range;
f6d3e3e4 1669 char type;
0b843d35 1670 int ret;
34a7a4c6
CB
1671 struct lxc_list *idmaplist = NULL;
1672 struct id_map *idmap = NULL;
f6d3e3e4 1673
663e9916 1674 if (lxc_config_value_empty(value))
7d0eb87e
SH
1675 return lxc_clear_idmaps(lxc_conf);
1676
f6d3e3e4
SH
1677 idmaplist = malloc(sizeof(*idmaplist));
1678 if (!idmaplist)
34a7a4c6 1679 goto on_error;
f6d3e3e4
SH
1680
1681 idmap = malloc(sizeof(*idmap));
1682 if (!idmap)
34a7a4c6 1683 goto on_error;
f6d3e3e4
SH
1684 memset(idmap, 0, sizeof(*idmap));
1685
0b843d35 1686 ret = parse_idmaps(value, &type, &nsid, &hostid, &range);
a8b1ac78 1687 if (ret < 0) {
f37d1c22 1688 ERROR("Failed to parse id mappings");
34a7a4c6 1689 goto on_error;
a8b1ac78 1690 }
34a7a4c6 1691
25a908b8 1692 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range);
ac7725e7 1693 if (type == 'u')
f6d3e3e4 1694 idmap->idtype = ID_TYPE_UID;
ac7725e7 1695 else if (type == 'g')
f6d3e3e4
SH
1696 idmap->idtype = ID_TYPE_GID;
1697 else
34a7a4c6 1698 goto on_error;
7e60c3f0 1699
f6d3e3e4
SH
1700 idmap->hostid = hostid;
1701 idmap->nsid = nsid;
1702 idmap->range = range;
7e60c3f0
SG
1703 idmaplist->elem = idmap;
1704 lxc_list_add_tail(&lxc_conf->id_map, idmaplist);
46ad64ab
CB
1705
1706 if (!lxc_conf->root_nsuid_map && idmap->idtype == ID_TYPE_UID)
1707 if (idmap->nsid == 0)
1708 lxc_conf->root_nsuid_map = idmap;
1709
1710
4160c3a0 1711 if (!lxc_conf->root_nsgid_map && idmap->idtype == ID_TYPE_GID)
46ad64ab
CB
1712 if (idmap->nsid == 0)
1713 lxc_conf->root_nsgid_map = idmap;
1714
34a7a4c6 1715 idmap = NULL;
7e60c3f0 1716
f6d3e3e4
SH
1717 return 0;
1718
34a7a4c6 1719on_error:
f10fad2f 1720 free(idmaplist);
34a7a4c6 1721 free(idmap);
f6d3e3e4
SH
1722
1723 return -1;
1724}
1725
47148e96
CB
1726static int set_config_mount_fstab(const char *key, const char *value,
1727 struct lxc_conf *lxc_conf, void *data)
d95db067 1728{
663e9916 1729 if (lxc_config_value_empty(value)) {
47148e96 1730 clr_config_mount_fstab(key, lxc_conf, NULL);
d9192f5d 1731 return -1;
46f3de30 1732 }
6f5685f0 1733
713046e3 1734 return set_config_path_item(&lxc_conf->fstab, value);
d95db067
DE
1735}
1736
713046e3 1737static int set_config_mount_auto(const char *key, const char *value,
c7e27aaf 1738 struct lxc_conf *lxc_conf, void *data)
368bbc02
CS
1739{
1740 char *autos, *autoptr, *sptr, *token;
368bbc02
CS
1741 int i;
1742 int ret = -1;
504a2217
CB
1743 static struct {
1744 const char *token;
1745 int mask;
1746 int flag;
1747 } allowed_auto_mounts[] = {
6812d833
CB
1748 { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
1749 { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
1750 { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW },
1751 { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
1752 { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO },
1753 { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
1754 { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW },
1755 { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC },
1756 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED },
1757 { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO },
1758 { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW },
1759 { "cgroup:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC | LXC_AUTO_CGROUP_FORCE },
1760 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED | LXC_AUTO_CGROUP_FORCE },
1761 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO | LXC_AUTO_CGROUP_FORCE },
1762 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW | LXC_AUTO_CGROUP_FORCE },
1763 { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC },
1764 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED },
1765 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO },
1766 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW },
1767 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC | LXC_AUTO_CGROUP_FORCE },
1768 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED | LXC_AUTO_CGROUP_FORCE },
1769 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO | LXC_AUTO_CGROUP_FORCE },
1770 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW | LXC_AUTO_CGROUP_FORCE },
25a908b8 1771 /* For adding anything that is just a single on/off, but has no
3f69fb12
SY
1772 * options: keep mask and flag identical and just define the enum
1773 * value as an unused bit so far
504a2217 1774 */
3f69fb12 1775 { NULL, 0, 0 }
504a2217 1776 };
368bbc02 1777
663e9916 1778 if (lxc_config_value_empty(value)) {
d9192f5d
SH
1779 lxc_conf->auto_mounts = 0;
1780 return 0;
1781 }
368bbc02
CS
1782
1783 autos = strdup(value);
25a908b8 1784 if (!autos)
368bbc02 1785 return -1;
368bbc02 1786
504a2217 1787 for (autoptr = autos;; autoptr = NULL) {
d028235d
SG
1788 token = strtok_r(autoptr, " \t", &sptr);
1789 if (!token) {
368bbc02 1790 ret = 0;
d028235d 1791 break;
368bbc02
CS
1792 }
1793
1794 for (i = 0; allowed_auto_mounts[i].token; i++) {
1795 if (!strcmp(allowed_auto_mounts[i].token, token))
1796 break;
1797 }
1798
1799 if (!allowed_auto_mounts[i].token) {
25a908b8 1800 ERROR("Invalid filesystem to automount \"%s\"", token);
368bbc02
CS
1801 break;
1802 }
1803
b06b8511 1804 lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask;
368bbc02 1805 lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag;
d028235d 1806 }
368bbc02
CS
1807
1808 free(autos);
368bbc02
CS
1809 return ret;
1810}
1811
713046e3 1812static int set_config_mount(const char *key, const char *value,
c7e27aaf 1813 struct lxc_conf *lxc_conf, void *data)
e7938e9e 1814{
e7938e9e
MN
1815 char *mntelem;
1816 struct lxc_list *mntlist;
1817
663e9916 1818 if (lxc_config_value_empty(value))
d9192f5d 1819 return lxc_clear_mount_entries(lxc_conf);
e7938e9e
MN
1820
1821 mntlist = malloc(sizeof(*mntlist));
1822 if (!mntlist)
1823 return -1;
1824
1825 mntelem = strdup(value);
00b6be44
SH
1826 if (!mntelem) {
1827 free(mntlist);
bf83c5b9 1828 return -1;
00b6be44 1829 }
e7938e9e
MN
1830 mntlist->elem = mntelem;
1831
1832 lxc_list_add_tail(&lxc_conf->mount_list, mntlist);
1833
1834 return 0;
1835}
1836
713046e3 1837static int set_config_cap_keep(const char *key, const char *value,
c7e27aaf 1838 struct lxc_conf *lxc_conf, void *data)
1fb86a7c
SH
1839{
1840 char *keepcaps, *keepptr, *sptr, *token;
1841 struct lxc_list *keeplist;
1842 int ret = -1;
1843
663e9916 1844 if (lxc_config_value_empty(value))
7d0eb87e 1845 return lxc_clear_config_keepcaps(lxc_conf);
1fb86a7c
SH
1846
1847 keepcaps = strdup(value);
25a908b8 1848 if (!keepcaps)
1fb86a7c 1849 return -1;
1fb86a7c 1850
504a2217
CB
1851 /* In case several capability keep is specified in a single line
1852 * split these caps in a single element for the list.
1853 */
1854 for (keepptr = keepcaps;; keepptr = NULL) {
d028235d
SG
1855 token = strtok_r(keepptr, " \t", &sptr);
1856 if (!token) {
1fb86a7c 1857 ret = 0;
d028235d 1858 break;
1fb86a7c
SH
1859 }
1860
7035407c
DE
1861 if (!strcmp(token, "none"))
1862 lxc_clear_config_keepcaps(lxc_conf);
1863
1fb86a7c 1864 keeplist = malloc(sizeof(*keeplist));
25a908b8 1865 if (!keeplist)
1fb86a7c 1866 break;
1fb86a7c
SH
1867
1868 keeplist->elem = strdup(token);
1869 if (!keeplist->elem) {
1fb86a7c
SH
1870 free(keeplist);
1871 break;
1872 }
1873
1874 lxc_list_add_tail(&lxc_conf->keepcaps, keeplist);
d028235d 1875 }
1fb86a7c
SH
1876
1877 free(keepcaps);
1878
1879 return ret;
1880}
1881
713046e3 1882static int set_config_cap_drop(const char *key, const char *value,
c7e27aaf 1883 struct lxc_conf *lxc_conf, void *data)
81810dd1 1884{
d95db067 1885 char *dropcaps, *dropptr, *sptr, *token;
81810dd1
DL
1886 struct lxc_list *droplist;
1887 int ret = -1;
1888
663e9916 1889 if (lxc_config_value_empty(value))
7d0eb87e 1890 return lxc_clear_config_caps(lxc_conf);
81810dd1
DL
1891
1892 dropcaps = strdup(value);
25a908b8 1893 if (!dropcaps)
81810dd1 1894 return -1;
81810dd1 1895
504a2217
CB
1896 /* In case several capability drop is specified in a single line
1897 * split these caps in a single element for the list.
1898 */
1899 for (dropptr = dropcaps;; dropptr = NULL) {
d028235d
SG
1900 token = strtok_r(dropptr, " \t", &sptr);
1901 if (!token) {
81810dd1 1902 ret = 0;
d028235d 1903 break;
81810dd1 1904 }
81810dd1
DL
1905
1906 droplist = malloc(sizeof(*droplist));
25a908b8 1907 if (!droplist)
81810dd1 1908 break;
81810dd1
DL
1909
1910 droplist->elem = strdup(token);
1911 if (!droplist->elem) {
81810dd1
DL
1912 free(droplist);
1913 break;
1914 }
1915
1916 lxc_list_add_tail(&lxc_conf->caps, droplist);
d028235d 1917 }
81810dd1
DL
1918
1919 free(dropcaps);
1920
1921 return ret;
1922}
1923
3aed4934
CB
1924static int set_config_console_path(const char *key, const char *value,
1925 struct lxc_conf *lxc_conf, void *data)
28a4b0e5 1926{
713046e3 1927 return set_config_path_item(&lxc_conf->console.path, value);
28a4b0e5
DL
1928}
1929
d91adfa6
CB
1930static int set_config_console_rotate(const char *key, const char *value,
1931 struct lxc_conf *lxc_conf, void *data)
1932{
1933 if (lxc_config_value_empty(value)) {
1934 lxc_conf->console.log_rotate = 0;
1935 return 0;
1936 }
1937
1938 if (lxc_safe_uint(value, &lxc_conf->console.log_rotate) < 0)
1939 return -1;
1940
966b9ecd
CB
1941 if (lxc_conf->console.log_rotate > 1) {
1942 ERROR("The \"lxc.console.rotate\" config key can only be set "
1943 "to 0 or 1");
d91adfa6 1944 return -1;
966b9ecd 1945 }
d91adfa6
CB
1946
1947 return 0;
1948}
1949
713046e3 1950static int set_config_console_logfile(const char *key, const char *value,
c7e27aaf 1951 struct lxc_conf *lxc_conf, void *data)
96f15ca1 1952{
713046e3 1953 return set_config_path_item(&lxc_conf->console.log_path, value);
96f15ca1
SH
1954}
1955
28f3b1cd
CB
1956static int set_config_console_buffer_size(const char *key, const char *value,
1957 struct lxc_conf *lxc_conf, void *data)
a04220de
CB
1958{
1959 int ret;
1960 int64_t size;
28f3b1cd 1961 uint64_t buffer_size, pgsz;
a04220de
CB
1962
1963 if (lxc_config_value_empty(value)) {
28f3b1cd 1964 lxc_conf->console.buffer_size = 0;
a04220de
CB
1965 return 0;
1966 }
1967
1968 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1969 if (!strcmp(value, "auto")) {
28f3b1cd 1970 lxc_conf->console.buffer_size = 1 << 17;
a04220de
CB
1971 return 0;
1972 }
1973
1974 ret = parse_byte_size_string(value, &size);
1975 if (ret < 0)
1976 return -1;
1977
1978 if (size < 0)
1979 return -EINVAL;
1980
1981 /* must be at least a page size */
1982 pgsz = lxc_getpagesize();
1983 if ((uint64_t)size < pgsz) {
1984 NOTICE("Requested ringbuffer size for the console is %" PRId64
1985 " but must be at least %" PRId64
1986 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
1987 size, pgsz, pgsz);
1988 size = pgsz;
1989 }
1990
28f3b1cd
CB
1991 buffer_size = lxc_find_next_power2((uint64_t)size);
1992 if (buffer_size == 0)
a04220de
CB
1993 return -EINVAL;
1994
28f3b1cd 1995 if (buffer_size != size)
a04220de 1996 NOTICE("Passed size was not a power of 2. Rounding log size to "
28f3b1cd 1997 "next power of two: %" PRIu64 " bytes", buffer_size);
a04220de 1998
28f3b1cd 1999 lxc_conf->console.buffer_size = buffer_size;
a04220de
CB
2000 return 0;
2001}
2002
861813e5
CB
2003static int set_config_console_size(const char *key, const char *value,
2004 struct lxc_conf *lxc_conf, void *data)
2005{
2006 int ret;
2007 int64_t size;
2008 uint64_t log_size, pgsz;
2009
2010 if (lxc_config_value_empty(value)) {
2011 lxc_conf->console.log_size = 0;
2012 return 0;
2013 }
2014
2015 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2016 if (!strcmp(value, "auto")) {
2017 lxc_conf->console.log_size = 1 << 17;
2018 return 0;
2019 }
2020
2021 ret = parse_byte_size_string(value, &size);
2022 if (ret < 0)
2023 return -1;
2024
2025 if (size < 0)
2026 return -EINVAL;
2027
2028 /* must be at least a page size */
2029 pgsz = lxc_getpagesize();
2030 if ((uint64_t)size < pgsz) {
2031 NOTICE("Requested ringbuffer size for the console is %" PRId64
2032 " but must be at least %" PRId64
2033 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
2034 size, pgsz, pgsz);
2035 size = pgsz;
2036 }
2037
2038 log_size = lxc_find_next_power2((uint64_t)size);
2039 if (log_size == 0)
2040 return -EINVAL;
2041
2042 if (log_size != size)
2043 NOTICE("Passed size was not a power of 2. Rounding log size to "
2044 "next power of two: %" PRIu64 " bytes", log_size);
2045
2046 lxc_conf->console.log_size = log_size;
2047 return 0;
2048}
2049
6b0d5538 2050int append_unexp_config_line(const char *line, struct lxc_conf *conf)
f979ac15 2051{
6b0d5538 2052 size_t len = conf->unexpanded_len, linelen = strlen(line);
f979ac15 2053
e6744e9b
SH
2054 update_hwaddr(line);
2055
6b0d5538 2056 while (conf->unexpanded_alloced <= len + linelen + 2) {
504a2217
CB
2057 char *tmp = realloc(conf->unexpanded_config,
2058 conf->unexpanded_alloced + 1024);
6b0d5538
SH
2059 if (!tmp)
2060 return -1;
504a2217 2061
6b0d5538
SH
2062 if (!conf->unexpanded_config)
2063 *tmp = '\0';
2064 conf->unexpanded_config = tmp;
2065 conf->unexpanded_alloced += 1024;
2066 }
efed99a4 2067
ebcd5140 2068 (void)strlcat(conf->unexpanded_config, line, conf->unexpanded_alloced);
6b0d5538 2069 conf->unexpanded_len += linelen;
504a2217 2070 if (line[linelen - 1] != '\n') {
ebcd5140 2071 (void)strlcat(conf->unexpanded_config, "\n", conf->unexpanded_alloced);
6b0d5538 2072 conf->unexpanded_len++;
f979ac15 2073 }
25a908b8 2074
f979ac15
SH
2075 return 0;
2076}
2077
e1daebd9
SH
2078static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf)
2079{
74f96976 2080 struct dirent *direntp;
e1daebd9
SH
2081 DIR *dir;
2082 char path[MAXPATHLEN];
504a2217
CB
2083 int len;
2084 int ret = -1;
e1daebd9
SH
2085
2086 dir = opendir(dirp);
25a908b8 2087 if (!dir)
e1daebd9 2088 return -1;
e1daebd9 2089
74f96976 2090 while ((direntp = readdir(dir))) {
e1daebd9 2091 const char *fnam;
e1daebd9
SH
2092
2093 fnam = direntp->d_name;
2094 if (!strcmp(fnam, "."))
2095 continue;
2096
2097 if (!strcmp(fnam, ".."))
2098 continue;
2099
2100 len = strlen(fnam);
504a2217 2101 if (len < 6 || strncmp(fnam + len - 5, ".conf", 5) != 0)
e1daebd9 2102 continue;
25a908b8 2103
e1daebd9
SH
2104 len = snprintf(path, MAXPATHLEN, "%s/%s", dirp, fnam);
2105 if (len < 0 || len >= MAXPATHLEN) {
e1daebd9
SH
2106 ret = -1;
2107 goto out;
2108 }
2109
2110 ret = lxc_config_read(path, lxc_conf, true);
2111 if (ret < 0)
2112 goto out;
2113 }
2114 ret = 0;
2115
2116out:
25a908b8 2117 closedir(dir);
e1daebd9
SH
2118
2119 return ret;
2120}
2121
973082f5 2122static int set_config_includefiles(const char *key, const char *value,
c7e27aaf 2123 struct lxc_conf *lxc_conf, void *data)
09ad6246 2124{
663e9916 2125 if (lxc_config_value_empty(value)) {
26471403 2126 clr_config_includefiles(key, lxc_conf, NULL);
616422f1 2127 return 0;
355c5701 2128 }
616422f1 2129
e1daebd9
SH
2130 if (is_dir(value))
2131 return do_includedir(value, lxc_conf);
2132
6b0d5538 2133 return lxc_config_read(value, lxc_conf, true);
09ad6246
SH
2134}
2135
7a96a068
CB
2136static int set_config_rootfs_path(const char *key, const char *value,
2137 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 2138{
693dbdb9
CB
2139 int ret;
2140 char *dup, *tmp;
2141 const char *container_path;
2142
2143 if (lxc_config_value_empty(value)) {
2144 free(lxc_conf->rootfs.path);
2145 lxc_conf->rootfs.path = NULL;
2146 return 0;
2147 }
2148
2149 dup = strdup(value);
2150 if (!dup)
2151 return -1;
2152
2153 /* Split <storage type>:<container path> into <storage type> and
2154 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2155 * "rootfs.path" to <container path>.
2156 */
2157 tmp = strchr(dup, ':');
2158 if (tmp) {
2159 *tmp = '\0';
2160 ret = set_config_path_item(&lxc_conf->rootfs.bdev_type, dup);
2161 if (ret < 0) {
2162 free(dup);
2163 return -1;
2164 }
2165 tmp++;
2166 container_path = tmp;
2167 } else {
2168 container_path = value;
2169 }
2170
2171 ret = set_config_path_item(&lxc_conf->rootfs.path, container_path);
2172 free(dup);
2173 return ret;
c2cc9f0a 2174}
2175
713046e3 2176static int set_config_rootfs_mount(const char *key, const char *value,
c7e27aaf 2177 struct lxc_conf *lxc_conf, void *data)
23b7ea69 2178{
713046e3 2179 return set_config_path_item(&lxc_conf->rootfs.mount, value);
23b7ea69
DL
2180}
2181
713046e3 2182static int set_config_rootfs_options(const char *key, const char *value,
c7e27aaf 2183 struct lxc_conf *lxc_conf, void *data)
a17b1e65 2184{
713046e3 2185 return set_config_string_item(&lxc_conf->rootfs.options, value);
a17b1e65
SG
2186}
2187
b67771bc 2188static int set_config_uts_name(const char *key, const char *value,
c7e27aaf 2189 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 2190{
2191 struct utsname *utsname;
2192
663e9916 2193 if (lxc_config_value_empty(value)) {
b67771bc 2194 clr_config_uts_name(key, lxc_conf, NULL);
1939e73d 2195 return 0;
00cd8039 2196 }
1939e73d 2197
c2cc9f0a 2198 utsname = malloc(sizeof(*utsname));
25a908b8 2199 if (!utsname)
c2cc9f0a 2200 return -1;
c2cc9f0a 2201
2202 if (strlen(value) >= sizeof(utsname->nodename)) {
b6f24d54 2203 free(utsname);
c2cc9f0a 2204 return -1;
2205 }
2206
43f984ea 2207 (void)strlcpy(utsname->nodename, value, sizeof(utsname->nodename));
f10fad2f 2208 free(lxc_conf->utsname);
c2cc9f0a 2209 lxc_conf->utsname = utsname;
2210
2211 return 0;
2212}
2213
1d8d3676
CB
2214static int set_config_namespace_clone(const char *key, const char *value,
2215 struct lxc_conf *lxc_conf, void *data)
2216{
2217 char *ns, *nsptr, *token;
2218 int cloneflag = 0;
2219 char *saveptr = NULL;
2220
2221 if (lxc_config_value_empty(value))
2222 return clr_config_namespace_clone(key, lxc_conf, data);
2223
8bc8c715 2224 if (lxc_conf->ns_keep != 0) {
6d1400b5 2225 errno = EINVAL;
2226 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2227 "\"lxc.namespace.keep\"");
8bc8c715
CB
2228 return -EINVAL;
2229 }
2230
1d8d3676
CB
2231 ns = strdup(value);
2232 if (!ns)
2233 return -1;
2234 nsptr = ns;
2235
2236 for (; (token = strtok_r(nsptr, " \t", &saveptr)); nsptr = NULL) {
2237 token += lxc_char_left_gc(token, strlen(token));
2238 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2239 cloneflag = lxc_namespace_2_cloneflag(token);
2240 if (cloneflag < 0) {
2241 free(ns);
2242 return -EINVAL;
2243 }
2244 lxc_conf->ns_clone |= cloneflag;
2245 }
2246 free(ns);
2247
2248 return 0;
2249}
2250
abeb5bba
CB
2251static int set_config_namespace_keep(const char *key, const char *value,
2252 struct lxc_conf *lxc_conf, void *data)
2253{
2254 char *ns, *nsptr, *token;
2255 int cloneflag = 0;
2256 char *saveptr = NULL;
2257
2258 if (lxc_config_value_empty(value))
2259 return clr_config_namespace_keep(key, lxc_conf, data);
2260
8bc8c715 2261 if (lxc_conf->ns_clone != 0) {
6d1400b5 2262 errno = EINVAL;
2263 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2264 "\"lxc.namespace.keep\"");
8bc8c715
CB
2265 return -EINVAL;
2266 }
2267
abeb5bba
CB
2268 ns = strdup(value);
2269 if (!ns)
2270 return -1;
2271 nsptr = ns;
2272
2273 for (; (token = strtok_r(nsptr, " \t", &saveptr)); nsptr = NULL) {
2274 token += lxc_char_left_gc(token, strlen(token));
2275 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2276 cloneflag = lxc_namespace_2_cloneflag(token);
2277 if (cloneflag < 0) {
2278 free(ns);
2279 return -EINVAL;
2280 }
2281 lxc_conf->ns_keep |= cloneflag;
2282 }
2283 free(ns);
2284
2285 return 0;
2286}
2287
b074bbf1
CB
2288static int set_config_namespace_share(const char *key, const char *value,
2289 struct lxc_conf *lxc_conf, void *data)
28d9e29e
CB
2290{
2291 int ns_idx;
2292 const char *namespace;
2293
2294 if (lxc_config_value_empty(value))
b074bbf1 2295 return clr_config_namespace_share(key, lxc_conf, data);
28d9e29e 2296
b074bbf1 2297 namespace = key + sizeof("lxc.namespace.share.") - 1;
28d9e29e
CB
2298 ns_idx = lxc_namespace_2_ns_idx(namespace);
2299 if (ns_idx < 0)
2300 return ns_idx;
2301
b074bbf1 2302 return set_config_string_item(&lxc_conf->ns_share[ns_idx], value);
28d9e29e
CB
2303}
2304
6b0d5538
SH
2305struct parse_line_conf {
2306 struct lxc_conf *conf;
2307 bool from_include;
2308};
4184c3e1 2309
7a7ff0c6 2310static int parse_line(char *buffer, void *data)
c2cc9f0a 2311{
504a2217 2312 char *dot, *key, *line, *linep, *value;
3b13691d
CB
2313 bool empty_line;
2314 struct lxc_config_t *config;
504a2217 2315 int ret = 0;
3b13691d
CB
2316 char *dup = buffer;
2317 struct parse_line_conf *plc = data;
c2cc9f0a 2318
3b13691d
CB
2319 /* If there are newlines in the config file we should keep them. */
2320 empty_line = lxc_is_line_empty(dup);
2321 if (empty_line)
2322 dup = "\n";
c2cc9f0a 2323
25a908b8
CB
2324 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2325 * modified the original string on the stack by replacing '=' by '\0'
2326 * below.
91480a0f 2327 */
3b13691d 2328 linep = line = strdup(dup);
25a908b8 2329 if (!line)
81192358 2330 return -1;
91480a0f 2331
3b13691d
CB
2332 if (!plc->from_include) {
2333 ret = append_unexp_config_line(line, plc->conf);
2334 if (ret < 0)
2335 goto on_error;
2336 }
2337
2338 if (empty_line)
327500d4 2339 goto on_error;
6b0d5538 2340
b2718c72 2341 line += lxc_char_left_gc(line, strlen(line));
476d4cf1 2342
4184c3e1
SH
2343 /* ignore comments */
2344 if (line[0] == '#')
3b13691d 2345 goto on_error;
476d4cf1 2346
6b0d5538
SH
2347 /* martian option - don't add it to the config itself */
2348 if (strncmp(line, "lxc.", 4))
3b13691d 2349 goto on_error;
4184c3e1 2350
476d4cf1 2351 ret = -1;
c2cc9f0a 2352
3b13691d 2353 dot = strchr(line, '=');
c2cc9f0a 2354 if (!dot) {
3b13691d
CB
2355 ERROR("Invalid configuration line: %s", line);
2356 goto on_error;
c2cc9f0a 2357 }
a871ff6b 2358
c2cc9f0a 2359 *dot = '\0';
2360 value = dot + 1;
2361
b2718c72 2362 key = line;
2363 key[lxc_char_right_gc(key, strlen(key))] = '\0';
c2cc9f0a 2364
b2718c72 2365 value += lxc_char_left_gc(value, strlen(value));
2366 value[lxc_char_right_gc(value, strlen(value))] = '\0';
c2cc9f0a 2367
bd878dee 2368 if (*value == '\'' || *value == '\"') {
25a908b8
CB
2369 size_t len;
2370
2371 len = strlen(value);
504a2217
CB
2372 if (len > 1 && value[len - 1] == *value) {
2373 value[len - 1] = '\0';
bd878dee
SB
2374 value++;
2375 }
2376 }
2377
300df83e 2378 config = lxc_get_config(key);
c2cc9f0a 2379 if (!config) {
25a908b8 2380 ERROR("Unknown configuration key \"%s\"", key);
3b13691d 2381 goto on_error;
c2cc9f0a 2382 }
2383
bbc079cf 2384 ret = config->set(key, value, plc->conf, NULL);
91480a0f 2385
3b13691d 2386on_error:
81192358 2387 free(linep);
91480a0f 2388 return ret;
c2cc9f0a 2389}
2390
74a3920a 2391static int lxc_config_readline(char *buffer, struct lxc_conf *conf)
af5b0155 2392{
6b0d5538
SH
2393 struct parse_line_conf c;
2394
2395 c.conf = conf;
2396 c.from_include = false;
2397
2398 return parse_line(buffer, &c);
af5b0155
CLG
2399}
2400
6b0d5538 2401int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
c2cc9f0a 2402{
25a908b8 2403 int ret;
6b0d5538
SH
2404 struct parse_line_conf c;
2405
2406 c.conf = conf;
2407 c.from_include = from_include;
f979ac15 2408
25a908b8
CB
2409 ret = access(file, R_OK);
2410 if (ret < 0)
f3ca99fd 2411 return -1;
6b0d5538 2412
25a908b8 2413 /* Catch only the top level config file name in the structure. */
504a2217 2414 if (!conf->rcfile)
76d0127f 2415 conf->rcfile = strdup(file);
f979ac15 2416
9994d140 2417 return lxc_file_for_each_line_mmap(file, parse_line, &c);
c2cc9f0a 2418}
62e46035 2419
504a2217 2420int lxc_config_define_add(struct lxc_list *defines, char *arg)
62e46035
CLG
2421{
2422 struct lxc_list *dent;
2423
2424 dent = malloc(sizeof(struct lxc_list));
2425 if (!dent)
2426 return -1;
2427
2428 dent->elem = arg;
2429 lxc_list_add_tail(defines, dent);
2430 return 0;
2431}
2432
226a18d6 2433int lxc_config_define_load(struct lxc_list *defines, struct lxc_conf *conf)
62e46035 2434{
504a2217 2435 struct lxc_list *it, *next;
62e46035
CLG
2436 int ret = 0;
2437
2438 lxc_list_for_each(it, defines) {
2439 ret = lxc_config_readline(it->elem, conf);
2440 if (ret)
2441 break;
2442 }
2443
9ebb03ad 2444 lxc_list_for_each_safe(it, defines, next) {
62e46035
CLG
2445 lxc_list_del(it);
2446 free(it);
2447 }
2448
2449 return ret;
2450}
525f0002
CS
2451
2452signed long lxc_config_parse_arch(const char *arch)
2453{
504a2217
CB
2454#if HAVE_SYS_PERSONALITY_H
2455 size_t i;
525f0002
CS
2456 struct per_name {
2457 char *name;
2458 unsigned long per;
bb8d8207 2459 } pername[] = {
8f3e3c13
CB
2460 { "arm", PER_LINUX32 },
2461 { "armel", PER_LINUX32 },
2462 { "armhf", PER_LINUX32 },
2463 { "armv7l", PER_LINUX32 },
2464 { "athlon", PER_LINUX32 },
504a2217
CB
2465 { "i386", PER_LINUX32 },
2466 { "i486", PER_LINUX32 },
2467 { "i586", PER_LINUX32 },
2468 { "i686", PER_LINUX32 },
8f3e3c13 2469 { "linux32", PER_LINUX32 },
504a2217
CB
2470 { "mips", PER_LINUX32 },
2471 { "mipsel", PER_LINUX32 },
2472 { "ppc", PER_LINUX32 },
504a2217 2473 { "powerpc", PER_LINUX32 },
8f3e3c13 2474 { "x86", PER_LINUX32 },
504a2217 2475 { "amd64", PER_LINUX },
8f3e3c13
CB
2476 { "arm64", PER_LINUX },
2477 { "linux64", PER_LINUX },
504a2217
CB
2478 { "mips64", PER_LINUX },
2479 { "mips64el", PER_LINUX },
2480 { "ppc64", PER_LINUX },
504a2217 2481 { "ppc64el", PER_LINUX },
8f3e3c13 2482 { "ppc64le", PER_LINUX },
504a2217
CB
2483 { "powerpc64", PER_LINUX },
2484 { "s390x", PER_LINUX },
8f3e3c13 2485 { "x86_64", PER_LINUX },
525f0002
CS
2486 };
2487 size_t len = sizeof(pername) / sizeof(pername[0]);
2488
8f3e3c13 2489 for (i = 0; i < len; i++)
525f0002 2490 if (!strcmp(pername[i].name, arch))
504a2217 2491 return pername[i].per;
504a2217 2492#endif
525f0002
CS
2493
2494 return -1;
2495}
72d0e1cb 2496
504a2217 2497/* Write out a configuration file. */
0e1a60b0 2498int write_config(int fd, const struct lxc_conf *conf)
72d0e1cb 2499{
6b0d5538 2500 int ret;
0e1a60b0 2501 size_t len = conf->unexpanded_len;
72d0e1cb 2502
0e1a60b0
CB
2503 if (len == 0)
2504 return 0;
504a2217 2505
0e1a60b0
CB
2506 ret = lxc_write_nointr(fd, conf->unexpanded_config, len);
2507 if (ret < 0) {
25a908b8 2508 SYSERROR("Failed to write configuration file");
0e1a60b0
CB
2509 return -1;
2510 }
2511
2512 return 0;
6b0d5538 2513}
f979ac15 2514
504a2217
CB
2515bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key,
2516 const char *v)
6b0d5538
SH
2517{
2518 int ret;
151d2da2
CB
2519 size_t len;
2520 char *tmp;
4184c3e1 2521
151d2da2
CB
2522 len = strlen(key) + strlen(v) + 4;
2523 tmp = alloca(len);
2524
663e9916 2525 if (lxc_config_value_empty(v))
151d2da2
CB
2526 ret = snprintf(tmp, len, "%s =", key);
2527 else
2528 ret = snprintf(tmp, len, "%s = %s", key, v);
6b0d5538
SH
2529 if (ret < 0 || ret >= len)
2530 return false;
2531
2532 /* Save the line verbatim into unexpanded_conf */
2533 if (append_unexp_config_line(tmp, conf))
2534 return false;
2535
2536 return true;
2537}
2538
504a2217
CB
2539void clear_unexp_config_line(struct lxc_conf *conf, const char *key,
2540 bool rm_subkeys)
6b0d5538 2541{
504a2217
CB
2542 char *lend;
2543 char *lstart = conf->unexpanded_config;
6b0d5538
SH
2544
2545 if (!conf->unexpanded_config)
2546 return;
504a2217 2547
6b0d5538
SH
2548 while (*lstart) {
2549 lend = strchr(lstart, '\n');
2550 char v;
2551 if (!lend)
2552 lend = lstart + strlen(lstart);
2553 else
2554 lend++;
2555 if (strncmp(lstart, key, strlen(key)) != 0) {
2556 lstart = lend;
2557 continue;
5f62730e 2558 }
6b0d5538
SH
2559 if (!rm_subkeys) {
2560 v = lstart[strlen(key)];
2561 if (!isspace(v) && v != '=') {
2562 lstart = lend;
2563 continue;
2564 }
5f62730e 2565 }
6b0d5538
SH
2566 conf->unexpanded_len -= (lend - lstart);
2567 if (*lend == '\0') {
2568 *lstart = '\0';
2569 return;
fd986e08 2570 }
504a2217 2571 memmove(lstart, lend, strlen(lend) + 1);
fd986e08 2572 }
6b0d5538
SH
2573}
2574
329b3625
CB
2575bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
2576 const char *newpath, const char *oldname,
2577 const char *newname, const char *ovldir)
2578{
329b3625 2579 int ret;
504a2217
CB
2580 char *lend, *newdir, *olddir, *p, *q;
2581 size_t newdirlen, olddirlen;
329b3625 2582 char *lstart = conf->unexpanded_config;
504a2217 2583 const char *key = "lxc.mount.entry";
329b3625 2584
504a2217
CB
2585 olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
2586 olddir = alloca(olddirlen + 1);
2587 ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath,
2588 oldname);
25a908b8 2589 if (ret < 0 || ret >= olddirlen + 1)
329b3625 2590 return false;
504a2217
CB
2591
2592 newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
2593 newdir = alloca(newdirlen + 1);
2594 ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath,
2595 newname);
25a908b8 2596 if (ret < 0 || ret >= newdirlen + 1)
329b3625 2597 return false;
504a2217 2598
329b3625
CB
2599 if (!conf->unexpanded_config)
2600 return true;
504a2217 2601
329b3625
CB
2602 while (*lstart) {
2603 lend = strchr(lstart, '\n');
2604 if (!lend)
2605 lend = lstart + strlen(lstart);
2606 else
2607 lend++;
504a2217 2608
329b3625 2609 if (strncmp(lstart, key, strlen(key)) != 0)
504a2217
CB
2610 goto next;
2611
329b3625
CB
2612 p = strchr(lstart + strlen(key), '=');
2613 if (!p)
504a2217 2614 goto next;
329b3625 2615 p++;
504a2217 2616
329b3625
CB
2617 while (isblank(*p))
2618 p++;
504a2217 2619
329b3625 2620 if (p >= lend)
504a2217
CB
2621 goto next;
2622
12e6ab5d
CB
2623 /* Whenever a lxc.mount.entry entry is found in a line we check
2624 * if the substring "overlay" is present before doing any
2625 * further work. We check for "overlay" because substrings need
2626 * to have at least one space before them in a valid overlay
504a2217
CB
2627 * lxc.mount.entry (/A B overlay). When the space before is
2628 * missing it is very likely that these substrings are part of a
2629 * path or something else. (Checking q >= lend ensures that we
2630 * only count matches in the current line.) */
12e6ab5d
CB
2631 q = strstr(p, " overlay");
2632 if (!q || q >= lend)
504a2217
CB
2633 goto next;
2634
329b3625 2635 if (!(q = strstr(p, olddir)) || (q >= lend))
504a2217 2636 goto next;
329b3625
CB
2637
2638 /* replace the olddir with newdir */
2639 if (olddirlen >= newdirlen) {
2640 size_t diff = olddirlen - newdirlen;
2641 memcpy(q, newdir, newdirlen);
2642 if (olddirlen != newdirlen) {
2643 memmove(q + newdirlen, q + newdirlen + diff,
2644 strlen(q) - newdirlen - diff + 1);
2645 lend -= diff;
2646 conf->unexpanded_len -= diff;
2647 }
2648 } else {
2649 char *new;
2650 size_t diff = newdirlen - olddirlen;
2651 size_t oldlen = conf->unexpanded_len;
2652 size_t newlen = oldlen + diff;
2653 size_t poffset = q - conf->unexpanded_config;
504a2217 2654
329b3625 2655 new = realloc(conf->unexpanded_config, newlen + 1);
25a908b8 2656 if (!new)
329b3625 2657 return false;
25a908b8 2658
329b3625
CB
2659 conf->unexpanded_len = newlen;
2660 conf->unexpanded_alloced = newlen + 1;
2661 new[newlen - 1] = '\0';
2662 lend = new + (lend - conf->unexpanded_config);
25a908b8 2663 /* Move over the remainder to make room for the newdir.
504a2217 2664 */
329b3625
CB
2665 memmove(new + poffset + newdirlen,
2666 new + poffset + olddirlen,
2667 oldlen - poffset - olddirlen + 1);
2668 conf->unexpanded_config = new;
2669 memcpy(new + poffset, newdir, newdirlen);
2670 lend += diff;
2671 }
504a2217
CB
2672 next:
2673 lstart = lend;
329b3625 2674 }
504a2217 2675
329b3625
CB
2676 return true;
2677}
2678
67702c21 2679bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
d546aa0e
CB
2680 const char *newpath, const char *oldname,
2681 const char *newname)
6b0d5538 2682{
67702c21 2683 int ret;
504a2217
CB
2684 char *lend, *newdir, *olddir, *p;
2685 char *lstart = conf->unexpanded_config;
2686 size_t newdirlen, olddirlen;
2687 const char *key = "lxc.hook";
67702c21 2688
504a2217
CB
2689 olddirlen = strlen(oldpath) + strlen(oldname) + 1;
2690 olddir = alloca(olddirlen + 1);
d546aa0e 2691 ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
25a908b8 2692 if (ret < 0 || ret >= olddirlen + 1)
67702c21 2693 return false;
504a2217
CB
2694
2695 newdirlen = strlen(newpath) + strlen(newname) + 1;
2696 newdir = alloca(newdirlen + 1);
d546aa0e 2697 ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
25a908b8 2698 if (ret < 0 || ret >= newdirlen + 1)
67702c21 2699 return false;
25a908b8 2700
67702c21
SH
2701 if (!conf->unexpanded_config)
2702 return true;
25a908b8 2703
67702c21
SH
2704 while (*lstart) {
2705 lend = strchr(lstart, '\n');
2706 if (!lend)
2707 lend = lstart + strlen(lstart);
2708 else
2709 lend++;
504a2217 2710
d546aa0e 2711 if (strncmp(lstart, key, strlen(key)) != 0)
504a2217
CB
2712 goto next;
2713
d546aa0e
CB
2714 p = strchr(lstart + strlen(key), '=');
2715 if (!p)
504a2217 2716 goto next;
67702c21 2717 p++;
504a2217 2718
67702c21
SH
2719 while (isblank(*p))
2720 p++;
504a2217
CB
2721
2722 if (p >= lend)
2723 goto next;
2724
d546aa0e 2725 if (strncmp(p, olddir, strlen(olddir)) != 0)
504a2217
CB
2726 goto next;
2727
67702c21
SH
2728 /* replace the olddir with newdir */
2729 if (olddirlen >= newdirlen) {
2730 size_t diff = olddirlen - newdirlen;
2731 memcpy(p, newdir, newdirlen);
2732 if (olddirlen != newdirlen) {
d546aa0e
CB
2733 memmove(p + newdirlen, p + newdirlen + diff,
2734 strlen(p) - newdirlen - diff + 1);
67702c21
SH
2735 lend -= diff;
2736 conf->unexpanded_len -= diff;
2737 }
67702c21
SH
2738 } else {
2739 char *new;
2740 size_t diff = newdirlen - olddirlen;
2741 size_t oldlen = conf->unexpanded_len;
2742 size_t newlen = oldlen + diff;
2743 size_t poffset = p - conf->unexpanded_config;
504a2217 2744
d546aa0e 2745 new = realloc(conf->unexpanded_config, newlen + 1);
25a908b8 2746 if (!new)
6b0d5538 2747 return false;
25a908b8 2748
67702c21 2749 conf->unexpanded_len = newlen;
d546aa0e
CB
2750 conf->unexpanded_alloced = newlen + 1;
2751 new[newlen - 1] = '\0';
67702c21 2752 lend = new + (lend - conf->unexpanded_config);
25a908b8 2753 /* Move over the remainder to make room for the newdir.
504a2217 2754 */
d546aa0e
CB
2755 memmove(new + poffset + newdirlen,
2756 new + poffset + olddirlen,
2757 oldlen - poffset - olddirlen + 1);
67702c21 2758 conf->unexpanded_config = new;
d546aa0e
CB
2759 memcpy(new + poffset, newdir, newdirlen);
2760 lend += diff;
fd986e08 2761 }
504a2217
CB
2762 next:
2763 lstart = lend;
fd986e08 2764 }
504a2217 2765
6b0d5538
SH
2766 return true;
2767}
2768
504a2217
CB
2769#define DO(cmd) \
2770 { \
2771 if (!(cmd)) { \
2772 ERROR("Error writing to new config"); \
2773 return false; \
2774 } \
2775 }
6b0d5538 2776
25a908b8
CB
2777/* This is called only from clone. We wish to update all hwaddrs in the
2778 * unexpanded config file. We can't/don't want to update any which come from
504a2217
CB
2779 * lxc.includes (there shouldn't be any).
2780 * We can't just walk the c->lxc-conf->network list because that includes netifs
2781 * from the include files. So we update the ones which we find in the unexp
2782 * config file, then find the original macaddr in the conf->network, and update
2783 * that to the same value.
67702c21
SH
2784 */
2785bool network_new_hwaddrs(struct lxc_conf *conf)
6b0d5538 2786{
504a2217 2787 char *lend, *p, *p2;
6b0d5538 2788 struct lxc_list *it;
504a2217 2789 char *lstart = conf->unexpanded_config;
6b0d5538 2790
67702c21
SH
2791 if (!conf->unexpanded_config)
2792 return true;
091045f8 2793
67702c21
SH
2794 while (*lstart) {
2795 char newhwaddr[18], oldhwaddr[17];
091045f8 2796
67702c21
SH
2797 lend = strchr(lstart, '\n');
2798 if (!lend)
2799 lend = lstart + strlen(lstart);
2800 else
2801 lend++;
091045f8 2802
4a787c27 2803 if (!lxc_config_net_hwaddr(lstart)) {
67702c21
SH
2804 lstart = lend;
2805 continue;
72d0e1cb 2806 }
091045f8 2807
4a787c27 2808 p = strchr(lstart, '=');
67702c21
SH
2809 if (!p) {
2810 lstart = lend;
2811 continue;
72d0e1cb 2812 }
091045f8 2813
67702c21
SH
2814 p++;
2815 while (isblank(*p))
2816 p++;
2817 if (!*p)
2818 return true;
091045f8 2819
67702c21
SH
2820 p2 = p;
2821 while (*p2 && !isblank(*p2) && *p2 != '\n')
2822 p2++;
504a2217
CB
2823
2824 if ((p2 - p) != 17) {
67702c21
SH
2825 WARN("Bad hwaddr entry");
2826 lstart = lend;
2827 continue;
72d0e1cb 2828 }
091045f8 2829
67702c21 2830 memcpy(oldhwaddr, p, 17);
091045f8
CB
2831
2832 if (!new_hwaddr(newhwaddr))
2833 return false;
2834
67702c21 2835 memcpy(p, newhwaddr, 17);
25a908b8 2836 lxc_list_for_each(it, &conf->network) {
67702c21 2837 struct lxc_netdev *n = it->elem;
25a908b8 2838
67702c21
SH
2839 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
2840 memcpy(n->hwaddr, newhwaddr, 17);
72d0e1cb 2841 }
67702c21
SH
2842
2843 lstart = lend;
72d0e1cb 2844 }
091045f8 2845
6b0d5538 2846 return true;
72d0e1cb 2847}
8796becf 2848
713046e3 2849static int set_config_ephemeral(const char *key, const char *value,
c7e27aaf 2850 struct lxc_conf *lxc_conf, void *data)
8796becf 2851{
663e9916 2852 if (lxc_config_value_empty(value)) {
3c6cf53a 2853 lxc_conf->ephemeral = 0;
78304622 2854 return 0;
3c6cf53a 2855 }
78304622 2856
66ffdb1a
CB
2857 if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0)
2858 return -1;
8796becf 2859
25a908b8 2860 if (lxc_conf->ephemeral > 1)
8796becf 2861 return -1;
8796becf
CB
2862
2863 return 0;
2864}
2865
46cc906d 2866static int set_config_log_syslog(const char *key, const char *value,
c7e27aaf 2867 struct lxc_conf *lxc_conf, void *data)
64c57ea1 2868{
76d0127f 2869 int facility;
7ca56b84 2870
ee10a69c
CB
2871 if (lxc_conf->syslog) {
2872 free(lxc_conf->syslog);
2873 lxc_conf->syslog = NULL;
2874 }
2875
663e9916 2876 if (lxc_config_value_empty(value))
7ca56b84
CB
2877 return 0;
2878
76d0127f 2879 facility = lxc_syslog_priority_to_int(value);
25a908b8 2880 if (facility == -EINVAL)
76d0127f 2881 return -1;
64c57ea1 2882
76d0127f 2883 lxc_log_syslog(facility);
713046e3 2884 return set_config_string_item(&lxc_conf->syslog, value);
64c57ea1 2885}
5a46f283 2886
713046e3 2887static int set_config_no_new_privs(const char *key, const char *value,
c7e27aaf 2888 struct lxc_conf *lxc_conf, void *data)
5a46f283 2889{
e8ec7c9e 2890 unsigned int v;
5a46f283 2891
663e9916 2892 if (lxc_config_value_empty(value)) {
cf3f8bf6 2893 lxc_conf->no_new_privs = false;
80926845 2894 return 0;
cf3f8bf6 2895 }
80926845 2896
e8ec7c9e
CB
2897 if (lxc_safe_uint(value, &v) < 0)
2898 return -1;
2899
25a908b8 2900 if (v > 1)
5a46f283 2901 return -1;
e8ec7c9e 2902
5a46f283
CB
2903 lxc_conf->no_new_privs = v ? true : false;
2904
2905 return 0;
2906}
7b992a3e
CB
2907
2908/* Callbacks to get configuration items. */
6bede808 2909static int get_config_personality(const char *key, char *retv, int inlen,
cccd2219 2910 struct lxc_conf *c, void *data)
7b992a3e
CB
2911{
2912 int fulllen = 0;
2913
2914 if (!retv)
2915 inlen = 0;
2916 else
2917 memset(retv, 0, inlen);
2918
2919#if HAVE_SYS_PERSONALITY_H
2920 int len = 0;
2921
6bede808 2922 switch (c->personality) {
7b992a3e
CB
2923 case PER_LINUX32:
2924 strprint(retv, inlen, "i686");
2925 break;
2926 case PER_LINUX:
2927 strprint(retv, inlen, "x86_64");
2928 break;
2929 default:
2930 break;
2931 }
2932#endif
2933
2934 return fulllen;
2935}
bdf91ab4 2936
232763d6
CB
2937static int get_config_pty_max(const char *key, char *retv, int inlen,
2938 struct lxc_conf *c, void *data)
bdf91ab4 2939{
e528c735 2940 return lxc_get_conf_size_t(c, retv, inlen, c->pty_max);
bdf91ab4 2941}
5485782f 2942
fe1c5887
CB
2943static int get_config_tty_max(const char *key, char *retv, int inlen,
2944 struct lxc_conf *c, void *data)
5485782f 2945{
885766f5 2946 return lxc_get_conf_size_t(c, retv, inlen, c->ttys.max);
5485782f 2947}
8015e018 2948
42e53c29 2949static int get_config_tty_dir(const char *key, char *retv, int inlen,
cccd2219 2950 struct lxc_conf *c, void *data)
8015e018 2951{
885766f5 2952 return lxc_get_conf_str(retv, inlen, c->ttys.dir);
8015e018 2953}
de1ede69 2954
953fe44f
CB
2955static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
2956 struct lxc_conf *c, void *data)
104c8e6c 2957{
6bede808 2958 return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile);
104c8e6c 2959}
d60d18c6 2960
953fe44f
CB
2961static int get_config_apparmor_allow_incomplete(const char *key, char *retv,
2962 int inlen, struct lxc_conf *c,
2963 void *data)
d60d18c6 2964{
6bede808
CB
2965 return lxc_get_conf_int(c, retv, inlen,
2966 c->lsm_aa_allow_incomplete);
d60d18c6 2967}
4203a0b5 2968
953fe44f
CB
2969static int get_config_selinux_context(const char *key, char *retv, int inlen,
2970 struct lxc_conf *c, void *data)
4203a0b5 2971{
6bede808 2972 return lxc_get_conf_str(retv, inlen, c->lsm_se_context);
4203a0b5 2973}
b863bf92 2974
25a908b8
CB
2975/* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
2976 * just the value(s) will be printed. Since there still could be more than one,
2977 * it is newline-separated.
2978 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
2979 * have newlines?)
2980 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
2981 * 'lxc.cgroup.subsystem.key = value' format.
b863bf92 2982 */
54860ed0
CB
2983static int __get_config_cgroup_controller(const char *key, char *retv,
2984 int inlen, struct lxc_conf *c,
2985 int version)
b863bf92 2986{
b863bf92 2987 int len;
54860ed0
CB
2988 size_t namespaced_token_len;
2989 char *global_token, *namespaced_token;
2990 struct lxc_list *it;
b863bf92
CB
2991 int fulllen = 0;
2992 bool get_all = false;
2993
2994 if (!retv)
2995 inlen = 0;
2996 else
2997 memset(retv, 0, inlen);
2998
54860ed0
CB
2999 if (version == CGROUP2_SUPER_MAGIC) {
3000 global_token = "lxc.cgroup2";
3001 namespaced_token = "lxc.cgroup2.";
3002 namespaced_token_len = sizeof("lxc.cgroup2.") - 1;;
3003 } else if (version == CGROUP_SUPER_MAGIC) {
3004 global_token = "lxc.cgroup";
3005 namespaced_token = "lxc.cgroup.";
3006 namespaced_token_len = sizeof("lxc.cgroup.") - 1;;
3007 } else {
3008 return -1;
3009 }
3010
3011 if (strcmp(key, global_token) == 0)
b863bf92 3012 get_all = true;
54860ed0
CB
3013 else if (strncmp(key, namespaced_token, namespaced_token_len) == 0)
3014 key += namespaced_token_len;
b863bf92
CB
3015 else
3016 return -1;
3017
6bede808 3018 lxc_list_for_each(it, &c->cgroup) {
b863bf92 3019 struct lxc_cgroup *cg = it->elem;
25a908b8 3020
b863bf92 3021 if (get_all) {
54860ed0
CB
3022 if (version != cg->version)
3023 continue;
3024
3025 strprint(retv, inlen, "%s.%s = %s\n",
3026 global_token, cg->subsystem, cg->value);
b863bf92
CB
3027 } else if (!strcmp(cg->subsystem, key)) {
3028 strprint(retv, inlen, "%s\n", cg->value);
3029 }
3030 }
3031
3032 return fulllen;
3033}
5014ff2e 3034
54860ed0
CB
3035static int get_config_cgroup_controller(const char *key, char *retv, int inlen,
3036 struct lxc_conf *c, void *data)
3037{
3038 return __get_config_cgroup_controller(key, retv, inlen, c,
3039 CGROUP_SUPER_MAGIC);
3040}
3041
3042static int get_config_cgroup2_controller(const char *key, char *retv, int inlen,
3043 struct lxc_conf *c, void *data)
3044{
3045 return __get_config_cgroup_controller(key, retv, inlen, c,
3046 CGROUP2_SUPER_MAGIC);
3047}
3048
43654d34
CB
3049static int get_config_cgroup_dir(const char *key, char *retv, int inlen,
3050 struct lxc_conf *lxc_conf, void *data)
3051{
3052 int len;
3053 int fulllen = 0;
3054
3055 if (!retv)
3056 inlen = 0;
3057 else
3058 memset(retv, 0, inlen);
3059
3060 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir);
3061
3062 return fulllen;
3063}
3064
6bede808 3065static int get_config_idmaps(const char *key, char *retv, int inlen,
cccd2219 3066 struct lxc_conf *c, void *data)
5014ff2e
CB
3067{
3068 struct lxc_list *it;
3069 int len, listlen, ret;
3070 int fulllen = 0;
3071/* "u 1000 1000000 65536"
3072 *
3073 * let's render this as
3074 *
3075 * sizeof(char)
3076 * +
3077 * sizeof(" ")
3078 * +
3079 * sizeof(uint64_t)
3080 * +
3081 * sizeof(" ")
3082 * +
3083 * sizeof(uint64_t)
3084 * +
3085 * sizeof(" ")
3086 * +
3087 * sizeof(uint64_t)
3088 * +
3089 * \0
3090 */
3091#define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
3092 char buf[__LXC_IDMAP_STR_BUF];
3093
3094 if (!retv)
3095 inlen = 0;
3096 else
3097 memset(retv, 0, inlen);
3098
6bede808
CB
3099 listlen = lxc_list_len(&c->id_map);
3100 lxc_list_for_each(it, &c->id_map)
5014ff2e
CB
3101 {
3102 struct id_map *map = it->elem;
3103 ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu",
3104 (map->idtype == ID_TYPE_UID) ? 'u' : 'g',
3105 map->nsid, map->hostid, map->range);
3106 if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF)
3107 return -1;
3108
3109 strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : "");
3110 }
3111 return fulllen;
3112}
b29b29be 3113
46cc906d 3114static int get_config_log_level(const char *key, char *retv, int inlen,
cccd2219 3115 struct lxc_conf *c, void *data)
b29b29be
CB
3116{
3117 const char *v;
6bede808 3118 v = lxc_log_priority_to_string(c->loglevel);
b29b29be
CB
3119 return lxc_get_conf_str(retv, inlen, v);
3120}
3d4630ab 3121
46cc906d 3122static int get_config_log_file(const char *key, char *retv, int inlen,
cccd2219 3123 struct lxc_conf *c, void *data)
3d4630ab 3124{
6bede808 3125 return lxc_get_conf_str(retv, inlen, c->logfile);
3d4630ab 3126}
0d601acb 3127
47148e96
CB
3128static int get_config_mount_fstab(const char *key, char *retv, int inlen,
3129 struct lxc_conf *c, void *data)
0d601acb 3130{
6bede808 3131 return lxc_get_conf_str(retv, inlen, c->fstab);
0d601acb 3132}
43fbf8d9 3133
6bede808 3134static int get_config_mount_auto(const char *key, char *retv, int inlen,
cccd2219 3135 struct lxc_conf *c, void *data)
43fbf8d9
CB
3136{
3137 int len, fulllen = 0;
3138 const char *sep = "";
3139
3140 if (!retv)
3141 inlen = 0;
3142 else
3143 memset(retv, 0, inlen);
3144
6bede808 3145 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
43fbf8d9
CB
3146 return 0;
3147
6bede808 3148 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
43fbf8d9
CB
3149 case LXC_AUTO_PROC_MIXED:
3150 strprint(retv, inlen, "%sproc:mixed", sep);
3151 sep = " ";
3152 break;
3153 case LXC_AUTO_PROC_RW:
3154 strprint(retv, inlen, "%sproc:rw", sep);
3155 sep = " ";
3156 break;
3157 default:
3158 break;
3159 }
3160
6bede808 3161 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
43fbf8d9
CB
3162 case LXC_AUTO_SYS_RO:
3163 strprint(retv, inlen, "%ssys:ro", sep);
3164 sep = " ";
3165 break;
3166 case LXC_AUTO_SYS_RW:
3167 strprint(retv, inlen, "%ssys:rw", sep);
3168 sep = " ";
3169 break;
3170 case LXC_AUTO_SYS_MIXED:
3171 strprint(retv, inlen, "%ssys:mixed", sep);
3172 sep = " ";
3173 break;
3174 default:
3175 break;
3176 }
3177
6bede808 3178 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
43fbf8d9
CB
3179 case LXC_AUTO_CGROUP_NOSPEC:
3180 strprint(retv, inlen, "%scgroup", sep);
43fbf8d9
CB
3181 break;
3182 case LXC_AUTO_CGROUP_MIXED:
3183 strprint(retv, inlen, "%scgroup:mixed", sep);
43fbf8d9
CB
3184 break;
3185 case LXC_AUTO_CGROUP_RO:
3186 strprint(retv, inlen, "%scgroup:ro", sep);
43fbf8d9
CB
3187 break;
3188 case LXC_AUTO_CGROUP_RW:
3189 strprint(retv, inlen, "%scgroup:rw", sep);
43fbf8d9
CB
3190 break;
3191 case LXC_AUTO_CGROUP_FULL_NOSPEC:
3192 strprint(retv, inlen, "%scgroup-full", sep);
43fbf8d9
CB
3193 break;
3194 case LXC_AUTO_CGROUP_FULL_MIXED:
3195 strprint(retv, inlen, "%scgroup-full:mixed", sep);
43fbf8d9
CB
3196 break;
3197 case LXC_AUTO_CGROUP_FULL_RO:
3198 strprint(retv, inlen, "%scgroup-full:ro", sep);
43fbf8d9
CB
3199 break;
3200 case LXC_AUTO_CGROUP_FULL_RW:
3201 strprint(retv, inlen, "%scgroup-full:rw", sep);
43fbf8d9
CB
3202 break;
3203 default:
3204 break;
3205 }
3206
3207 return fulllen;
3208}
cc921848 3209
6bede808 3210static int get_config_mount(const char *key, char *retv, int inlen,
cccd2219 3211 struct lxc_conf *c, void *data)
cc921848
CB
3212{
3213 int len, fulllen = 0;
3214 struct lxc_list *it;
3215
3216 if (!retv)
3217 inlen = 0;
3218 else
3219 memset(retv, 0, inlen);
3220
25a908b8 3221 lxc_list_for_each(it, &c->mount_list) {
cc921848
CB
3222 strprint(retv, inlen, "%s\n", (char *)it->elem);
3223 }
3224
3225 return fulllen;
3226}
819114b6 3227
7a96a068
CB
3228static int get_config_rootfs_path(const char *key, char *retv, int inlen,
3229 struct lxc_conf *c, void *data)
819114b6 3230{
6bede808 3231 return lxc_get_conf_str(retv, inlen, c->rootfs.path);
819114b6 3232}
3af60359 3233
6bede808 3234static int get_config_rootfs_mount(const char *key, char *retv, int inlen,
cccd2219 3235 struct lxc_conf *c, void *data)
3af60359 3236{
6bede808 3237 return lxc_get_conf_str(retv, inlen, c->rootfs.mount);
3af60359 3238}
0e9db631 3239
6bede808 3240static int get_config_rootfs_options(const char *key, char *retv, int inlen,
cccd2219 3241 struct lxc_conf *c, void *data)
0e9db631 3242{
6bede808 3243 return lxc_get_conf_str(retv, inlen, c->rootfs.options);
0e9db631 3244}
8f183f38 3245
b67771bc 3246static int get_config_uts_name(const char *key, char *retv, int inlen,
cccd2219 3247 struct lxc_conf *c, void *data)
e274f8b0
CB
3248{
3249 return lxc_get_conf_str(
3250 retv, inlen,
6bede808 3251 c->utsname ? c->utsname->nodename : NULL);
e274f8b0 3252}
466c2e93 3253
6bede808 3254static int get_config_hooks(const char *key, char *retv, int inlen,
cccd2219 3255 struct lxc_conf *c, void *data)
466c2e93
CB
3256{
3257 char *subkey;
3258 int len, fulllen = 0, found = -1;
3259 struct lxc_list *it;
3260 int i;
3261
466c2e93
CB
3262 subkey = strchr(key, '.');
3263 if (subkey)
3264 subkey = strchr(subkey + 1, '.');
3265 if (!subkey)
3266 return -1;
3267 subkey++;
3268 if (!*subkey)
3269 return -1;
3270 for (i = 0; i < NUM_LXC_HOOKS; i++) {
3271 if (strcmp(lxchook_names[i], subkey) == 0) {
3272 found = i;
3273 break;
3274 }
3275 }
3276 if (found == -1)
3277 return -1;
3278
3279 if (!retv)
3280 inlen = 0;
3281 else
3282 memset(retv, 0, inlen);
3283
6bede808 3284 lxc_list_for_each(it, &c->hooks[found]) {
466c2e93
CB
3285 strprint(retv, inlen, "%s\n", (char *)it->elem);
3286 }
3287 return fulllen;
3288}
d2ceff53 3289
44ae0fb6
CB
3290static int get_config_hooks_version(const char *key, char *retv, int inlen,
3291 struct lxc_conf *c, void *data)
3292{
3293 return lxc_get_conf_int(c, retv, inlen, c->hooks_version);
3294}
3295
f9373e40
CB
3296static int get_config_net(const char *key, char *retv, int inlen,
3297 struct lxc_conf *c, void *data)
d2ceff53
CB
3298{
3299 int len, fulllen = 0;
3300 struct lxc_list *it;
3301
3302 if (!retv)
3303 inlen = 0;
3304 else
3305 memset(retv, 0, inlen);
3306
6bede808 3307 lxc_list_for_each(it, &c->network) {
d2ceff53
CB
3308 struct lxc_netdev *n = it->elem;
3309 const char *t = lxc_net_type_to_str(n->type);
3310 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
3311 }
3312
3313 return fulllen;
3314}
bdccff60 3315
6bede808 3316static int get_config_cap_drop(const char *key, char *retv, int inlen,
cccd2219 3317 struct lxc_conf *c, void *data)
1c96d6d8
CB
3318{
3319 int len, fulllen = 0;
3320 struct lxc_list *it;
3321
3322 if (!retv)
3323 inlen = 0;
3324 else
3325 memset(retv, 0, inlen);
3326
6bede808 3327 lxc_list_for_each(it, &c->caps) {
b80927f2
CB
3328 strprint(retv, inlen, "%s\n", (char *)it->elem);
3329 }
25a908b8 3330
b80927f2
CB
3331 return fulllen;
3332}
3333
6bede808 3334static int get_config_cap_keep(const char *key, char *retv, int inlen,
cccd2219 3335 struct lxc_conf *c, void *data)
b80927f2
CB
3336{
3337 int len, fulllen = 0;
3338 struct lxc_list *it;
3339
3340 if (!retv)
3341 inlen = 0;
3342 else
3343 memset(retv, 0, inlen);
3344
6bede808 3345 lxc_list_for_each(it, &c->keepcaps) {
1c96d6d8
CB
3346 strprint(retv, inlen, "%s\n", (char *)it->elem);
3347 }
25a908b8 3348
1c96d6d8
CB
3349 return fulllen;
3350}
0692663a 3351
3aed4934
CB
3352static int get_config_console_path(const char *key, char *retv, int inlen,
3353 struct lxc_conf *c, void *data)
0692663a 3354{
6bede808 3355 return lxc_get_conf_str(retv, inlen, c->console.path);
0692663a 3356}
794d1c06 3357
6bede808 3358static int get_config_console_logfile(const char *key, char *retv, int inlen,
cccd2219 3359 struct lxc_conf *c, void *data)
794d1c06 3360{
6bede808 3361 return lxc_get_conf_str(retv, inlen, c->console.log_path);
794d1c06 3362}
75f55b1f 3363
d91adfa6
CB
3364static int get_config_console_rotate(const char *key, char *retv, int inlen,
3365 struct lxc_conf *c, void *data)
3366{
3367 return lxc_get_conf_int(c, retv, inlen, c->console.log_rotate);
3368}
3369
3370
28f3b1cd
CB
3371static int get_config_console_buffer_size(const char *key, char *retv,
3372 int inlen, struct lxc_conf *c,
3373 void *data)
a04220de 3374{
dcea90a0 3375 return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size);
a04220de
CB
3376}
3377
861813e5
CB
3378static int get_config_console_size(const char *key, char *retv, int inlen,
3379 struct lxc_conf *c, void *data)
3380{
3381 return lxc_get_conf_uint64(c, retv, inlen, c->console.log_size);
3382}
3383
3384
0b427da0
CB
3385static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
3386 struct lxc_conf *c, void *data)
75f55b1f 3387{
6bede808 3388 return lxc_get_conf_str(retv, inlen, c->seccomp);
75f55b1f 3389}
97f6dad0 3390
6bede808 3391static int get_config_autodev(const char *key, char *retv, int inlen,
cccd2219 3392 struct lxc_conf *c, void *data)
97f6dad0 3393{
6bede808 3394 return lxc_get_conf_int(c, retv, inlen, c->autodev);
97f6dad0 3395}
afee4324 3396
55c84efc 3397static int get_config_signal_halt(const char *key, char *retv, int inlen,
25a908b8 3398 struct lxc_conf *c, void *data)
afee4324 3399{
6bede808 3400 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
afee4324 3401}
3aa8f359 3402
55c84efc 3403static int get_config_signal_reboot(const char *key, char *retv, int inlen,
25a908b8 3404 struct lxc_conf *c, void *data)
3aa8f359 3405{
6bede808 3406 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
3aa8f359 3407}
2e16269f 3408
55c84efc 3409static int get_config_signal_stop(const char *key, char *retv, int inlen,
25a908b8 3410 struct lxc_conf *c, void *data)
2e16269f 3411{
6bede808 3412 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
2e16269f 3413}
54345299 3414
6bede808 3415static int get_config_start(const char *key, char *retv, int inlen,
cccd2219 3416 struct lxc_conf *c, void *data)
54345299 3417{
c6182222 3418 if (strcmp(key + 10, "auto") == 0)
6bede808 3419 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
c6182222 3420 else if (strcmp(key + 10, "delay") == 0)
6bede808 3421 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
c6182222 3422 else if (strcmp(key + 10, "order") == 0)
6bede808 3423 return lxc_get_conf_int(c, retv, inlen, c->start_order);
54345299
CB
3424
3425 return -1;
3426}
de9df15e 3427
46cc906d 3428static int get_config_log_syslog(const char *key, char *retv, int inlen,
25a908b8 3429 struct lxc_conf *c, void *data)
de9df15e 3430{
6bede808 3431 return lxc_get_conf_str(retv, inlen, c->syslog);
de9df15e 3432}
ac0f949c 3433
6bede808 3434static int get_config_monitor(const char *key, char *retv, int inlen,
cccd2219 3435 struct lxc_conf *c, void *data)
ac0f949c 3436{
6bede808 3437 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
ac0f949c 3438}
90ec7b6e 3439
6bede808 3440static int get_config_group(const char *key, char *retv, int inlen,
cccd2219 3441 struct lxc_conf *c, void *data)
90ec7b6e
CB
3442{
3443 int len, fulllen = 0;
3444 struct lxc_list *it;
3445
3446 if (!retv)
3447 inlen = 0;
3448 else
3449 memset(retv, 0, inlen);
3450
6bede808 3451 lxc_list_for_each(it, &c->groups) {
90ec7b6e
CB
3452 strprint(retv, inlen, "%s\n", (char *)it->elem);
3453 }
25a908b8 3454
90ec7b6e
CB
3455 return fulllen;
3456}
aa0db7d3 3457
6bede808 3458static int get_config_environment(const char *key, char *retv, int inlen,
cccd2219 3459 struct lxc_conf *c, void *data)
aa0db7d3
CB
3460{
3461 int len, fulllen = 0;
3462 struct lxc_list *it;
3463
3464 if (!retv)
3465 inlen = 0;
3466 else
3467 memset(retv, 0, inlen);
3468
6bede808 3469 lxc_list_for_each(it, &c->environment) {
aa0db7d3
CB
3470 strprint(retv, inlen, "%s\n", (char *)it->elem);
3471 }
25a908b8 3472
aa0db7d3
CB
3473 return fulllen;
3474}
96dfcb7d 3475
5cda27c1
SH
3476static int get_config_execute_cmd(const char *key, char *retv, int inlen,
3477 struct lxc_conf *c, void *data)
3478{
3479 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
3480}
3481
6bede808 3482static int get_config_init_cmd(const char *key, char *retv, int inlen,
cccd2219 3483 struct lxc_conf *c, void *data)
96dfcb7d 3484{
6bede808 3485 return lxc_get_conf_str(retv, inlen, c->init_cmd);
96dfcb7d 3486}
1398e9b1 3487
3c491553
L
3488static int get_config_init_cwd(const char *key, char *retv, int inlen,
3489 struct lxc_conf *c, void *data)
3490{
3491 return lxc_get_conf_str(retv, inlen, c->init_cwd);
3492}
3493
6bede808 3494static int get_config_init_uid(const char *key, char *retv, int inlen,
cccd2219 3495 struct lxc_conf *c, void *data)
1398e9b1 3496{
6bede808 3497 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
1398e9b1 3498}
dfeb7e42 3499
6bede808 3500static int get_config_init_gid(const char *key, char *retv, int inlen,
cccd2219 3501 struct lxc_conf *c, void *data)
dfeb7e42 3502{
6bede808 3503 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
dfeb7e42 3504}
62048afe 3505
6bede808 3506static int get_config_ephemeral(const char *key, char *retv, int inlen,
cccd2219 3507 struct lxc_conf *c, void *data)
62048afe 3508{
6bede808 3509 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
62048afe 3510}
b09521ac 3511
6bede808 3512static int get_config_no_new_privs(const char *key, char *retv, int inlen,
cccd2219 3513 struct lxc_conf *c, void *data)
b09521ac 3514{
6bede808 3515 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
b09521ac 3516}
389f6466 3517
25a908b8 3518/* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
240d4b74 3519 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3520 * printed, in 'lxc.prlimit.resource = value' format.
389f6466 3521 */
240d4b74 3522static int get_config_prlimit(const char *key, char *retv, int inlen,
cccd2219 3523 struct lxc_conf *c, void *data)
389f6466
CB
3524{
3525 int fulllen = 0, len;
3526 bool get_all = false;
3527 struct lxc_list *it;
3528
3529 if (!retv)
3530 inlen = 0;
3531 else
3532 memset(retv, 0, inlen);
3533
240d4b74 3534 if (!strcmp(key, "lxc.prlimit"))
389f6466 3535 get_all = true;
240d4b74 3536 else if (strncmp(key, "lxc.prlimit.", 12) == 0)
3537 key += 12;
389f6466
CB
3538 else
3539 return -1;
3540
6bede808 3541 lxc_list_for_each(it, &c->limits) {
389f6466
CB
3542 char buf[LXC_NUMSTRLEN64 * 2 + 2]; /* 2 colon separated 64 bit
3543 integers or the word
3544 'unlimited' */
3545 int partlen;
3546 struct lxc_limit *lim = it->elem;
3547
3548 if (lim->limit.rlim_cur == RLIM_INFINITY) {
3549 memcpy(buf, "unlimited", sizeof("unlimited"));
3550 partlen = sizeof("unlimited") - 1;
3551 } else {
3552 partlen = sprintf(buf, "%" PRIu64,
3553 (uint64_t)lim->limit.rlim_cur);
3554 }
3555 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
25a908b8 3556 if (lim->limit.rlim_max == RLIM_INFINITY)
389f6466
CB
3557 memcpy(buf + partlen, ":unlimited",
3558 sizeof(":unlimited"));
25a908b8 3559 else
389f6466
CB
3560 sprintf(buf + partlen, ":%" PRIu64,
3561 (uint64_t)lim->limit.rlim_max);
389f6466
CB
3562 }
3563
3564 if (get_all) {
240d4b74 3565 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
389f6466 3566 lim->resource, buf);
25a908b8 3567 } else if (!strcmp(lim->resource, key)) {
389f6466
CB
3568 strprint(retv, inlen, "%s", buf);
3569 }
3570 }
3571
3572 return fulllen;
3573}
e08cb901 3574
e409b214
CB
3575/* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3576 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3577 * entries will be printed, in 'lxc.sysctl.key = value' format.
7edd0540
L
3578 */
3579static int get_config_sysctl(const char *key, char *retv, int inlen,
3580 struct lxc_conf *c, void *data)
3581{
3582 int len;
3583 struct lxc_list *it;
3584 int fulllen = 0;
3585 bool get_all = false;
3586
3587 if (!retv)
3588 inlen = 0;
3589 else
3590 memset(retv, 0, inlen);
3591
3592 if (strcmp(key, "lxc.sysctl") == 0)
3593 get_all = true;
3594 else if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3595 key += sizeof("lxc.sysctl.") - 1;
3596 else
3597 return -1;
3598
3599 lxc_list_for_each(it, &c->sysctls) {
3600 struct lxc_sysctl *elem = it->elem;
3601 if (get_all) {
3602 strprint(retv, inlen, "lxc.sysctl.%s = %s\n",
3603 elem->key, elem->value);
3604 } else if (strcmp(elem->key, key) == 0) {
3605 strprint(retv, inlen, "%s", elem->value);
3606 }
3607 }
3608
3609 return fulllen;
3610}
3611
61d7a733
YT
3612static int get_config_proc(const char *key, char *retv, int inlen,
3613 struct lxc_conf *c, void *data)
3614{
3615 struct lxc_list *it;
3616 int len;
3617 int fulllen = 0;
3618 bool get_all = false;
3619
3620 if (!retv)
3621 inlen = 0;
3622 else
3623 memset(retv, 0, inlen);
3624
3625 if (strcmp(key, "lxc.proc") == 0)
3626 get_all = true;
3627 else if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3628 key += sizeof("lxc.proc.") - 1;
3629 else
3630 return -1;
3631
3632 lxc_list_for_each(it, &c->procs) {
3633 struct lxc_proc *proc = it->elem;
3634
3635 if (get_all) {
3636 strprint(retv, inlen, "lxc.proc.%s = %s\n",
3637 proc->filename, proc->value);
3638 } else if (strcmp(proc->filename, key) == 0) {
3639 strprint(retv, inlen, "%s", proc->value);
3640 }
3641 }
3642
3643 return fulllen;
3644}
3645
1d8d3676
CB
3646static int get_config_namespace_clone(const char *key, char *retv, int inlen,
3647 struct lxc_conf *c, void *data)
3648{
3649 int i, len;
3650 int fulllen = 0;
3651
3652 if (!retv)
3653 inlen = 0;
3654 else
3655 memset(retv, 0, inlen);
3656
3657 for (i = 0; i < LXC_NS_MAX; i++) {
3658 if (c->ns_clone & ns_info[i].clone_flag)
3659 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
3660 }
3661
3662 return fulllen;
3663}
3664
abeb5bba
CB
3665static int get_config_namespace_keep(const char *key, char *retv, int inlen,
3666 struct lxc_conf *c, void *data)
3667{
3668 int i, len;
3669 int fulllen = 0;
3670
3671 if (!retv)
3672 inlen = 0;
3673 else
3674 memset(retv, 0, inlen);
3675
3676 for (i = 0; i < LXC_NS_MAX; i++) {
3677 if (c->ns_keep & ns_info[i].clone_flag)
3678 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
3679 }
3680
3681 return fulllen;
3682}
3683
b074bbf1
CB
3684static int get_config_namespace_share(const char *key, char *retv, int inlen,
3685 struct lxc_conf *c, void *data)
28d9e29e
CB
3686{
3687 int len, ns_idx;
3688 const char *namespace;
3689 int fulllen = 0;
3690
3691 if (!retv)
3692 inlen = 0;
3693 else
3694 memset(retv, 0, inlen);
3695
b074bbf1 3696 namespace = key + sizeof("lxc.namespace.share.") - 1;
28d9e29e
CB
3697 ns_idx = lxc_namespace_2_ns_idx(namespace);
3698 if (ns_idx < 0)
3699 return ns_idx;
3700
b074bbf1 3701 strprint(retv, inlen, "%s", c->ns_share[ns_idx]);
28d9e29e
CB
3702
3703 return fulllen;
3704}
3705
e08cb901 3706/* Callbacks to clear config items. */
26471403
CB
3707static inline int clr_config_personality(const char *key, struct lxc_conf *c,
3708 void *data)
e08cb901
CB
3709{
3710 c->personality = -1;
3711 return 0;
3712}
03818ae3 3713
232763d6
CB
3714static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
3715 void *data)
03818ae3 3716{
e528c735 3717 c->pty_max = 0;
03818ae3
CB
3718 return 0;
3719}
e7a4b096 3720
fe1c5887
CB
3721static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
3722 void *data)
e7a4b096 3723{
885766f5 3724 c->ttys.tty = 0;
e7a4b096
CB
3725 return 0;
3726}
eaf8c0c7 3727
42e53c29 3728static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
26471403 3729 void *data)
eaf8c0c7 3730{
885766f5
CB
3731 free(c->ttys.dir);
3732 c->ttys.dir = NULL;
eaf8c0c7
CB
3733 return 0;
3734}
6bd86308 3735
953fe44f
CB
3736static inline int clr_config_apparmor_profile(const char *key,
3737 struct lxc_conf *c, void *data)
025718fb
CB
3738{
3739 free(c->lsm_aa_profile);
3740 c->lsm_aa_profile = NULL;
3741 return 0;
3742}
3061e04e 3743
953fe44f
CB
3744static inline int clr_config_apparmor_allow_incomplete(const char *key,
3745 struct lxc_conf *c,
3746 void *data)
3061e04e
CB
3747{
3748 c->lsm_aa_allow_incomplete = 0;
3749 return 0;
3750}
31fc3494 3751
953fe44f
CB
3752static inline int clr_config_selinux_context(const char *key,
3753 struct lxc_conf *c, void *data)
31fc3494
CB
3754{
3755 free(c->lsm_se_context);
3756 c->lsm_se_context = NULL;
3757 return 0;
3758}
754d01ce 3759
43654d34
CB
3760static inline int clr_config_cgroup_controller(const char *key,
3761 struct lxc_conf *c, void *data)
754d01ce 3762{
54860ed0
CB
3763 return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC);
3764}
3765
3766static inline int clr_config_cgroup2_controller(const char *key,
3767 struct lxc_conf *c, void *data)
3768{
3769 return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC);
754d01ce 3770}
d3a178de 3771
43654d34
CB
3772static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
3773 void *data)
3774{
3775 if (lxc_conf->cgroup_meta.dir) {
3776 free(lxc_conf->cgroup_meta.dir);
3777 lxc_conf->cgroup_meta.dir = NULL;
3778 }
3779
3780 return 0;
3781}
3782
26471403
CB
3783static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
3784 void *data)
d3a178de
CB
3785{
3786 return lxc_clear_idmaps(c);
3787}
97d3338d 3788
46cc906d 3789static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
26471403 3790 void *data)
97d3338d 3791{
4b73005c 3792 c->loglevel = LXC_LOG_LEVEL_NOTSET;
97d3338d
CB
3793 return 0;
3794}
de46099c 3795
46cc906d 3796static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
26471403 3797 void *data)
de46099c
CB
3798{
3799 free(c->logfile);
3800 c->logfile = NULL;
3801 return 0;
3802}
b4fa13cd 3803
26471403
CB
3804static inline int clr_config_mount(const char *key, struct lxc_conf *c,
3805 void *data)
b4fa13cd
CB
3806{
3807 return lxc_clear_mount_entries(c);
3808}
4be81021 3809
26471403
CB
3810static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
3811 void *data)
4be81021
CB
3812{
3813 return lxc_clear_automounts(c);
3814}
350d4b15 3815
47148e96
CB
3816static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
3817 void *data)
350d4b15
CB
3818{
3819 free(c->fstab);
3820 c->fstab = NULL;
3821 return 0;
3822}
faca124d 3823
7a96a068
CB
3824static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
3825 void *data)
faca124d
CB
3826{
3827 free(c->rootfs.path);
3828 c->rootfs.path = NULL;
3829 return 0;
3830}
3831
26471403
CB
3832static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
3833 void *data)
fddefc2d
CB
3834{
3835 free(c->rootfs.mount);
3836 c->rootfs.mount = NULL;
3837 return 0;
3838}
7b1eb67d 3839
26471403
CB
3840static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
3841 void *data)
7b1eb67d
CB
3842{
3843 free(c->rootfs.options);
3844 c->rootfs.options = NULL;
3845 return 0;
3846}
02becb8d 3847
b67771bc 3848static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
26471403 3849 void *data)
d31d0103
CB
3850{
3851 free(c->utsname);
3852 c->utsname = NULL;
3853 return 0;
3854}
c9eeb90c 3855
26471403
CB
3856static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
3857 void *data)
c9eeb90c
CB
3858{
3859 return lxc_clear_hooks(c, key);
3860}
3861
44ae0fb6
CB
3862static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c,
3863 void *data)
3864{
3865 /* default to legacy hooks version */
3866 c->hooks_version = 0;
3867 return 0;
3868}
3869
f9373e40
CB
3870static inline int clr_config_net(const char *key, struct lxc_conf *c,
3871 void *data)
f4488271 3872{
c302b476
CB
3873 lxc_free_networks(&c->network);
3874
e5d2fd7c 3875 return 0;
f4488271
CB
3876}
3877
26471403
CB
3878static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
3879 void *data)
244cb55b
CB
3880{
3881 return lxc_clear_config_caps(c);
3882}
c74cc490 3883
26471403
CB
3884static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
3885 void *data)
c74cc490
CB
3886{
3887 return lxc_clear_config_keepcaps(c);
3888}
4e5b633f 3889
3aed4934
CB
3890static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
3891 void *data)
4e5b633f
CB
3892{
3893 free(c->console.path);
3894 c->console.path = NULL;
3895 return 0;
3896}
3897
7c2ec23a 3898static inline int clr_config_console_logfile(const char *key,
26471403 3899 struct lxc_conf *c, void *data)
7c2ec23a
CB
3900{
3901 free(c->console.log_path);
3902 c->console.log_path = NULL;
3903 return 0;
3904}
bbca37d8 3905
d91adfa6
CB
3906static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c,
3907 void *data)
3908{
3909 c->console.log_rotate = 0;
3910 return 0;
3911}
3912
28f3b1cd
CB
3913static inline int clr_config_console_buffer_size(const char *key,
3914 struct lxc_conf *c, void *data)
a04220de 3915{
28f3b1cd 3916 c->console.buffer_size = 0;
a04220de
CB
3917 return 0;
3918}
3919
861813e5
CB
3920static inline int clr_config_console_size(const char *key, struct lxc_conf *c,
3921 void *data)
3922{
3923 c->console.log_size = 0;
3924 return 0;
3925}
3926
0b427da0
CB
3927static inline int clr_config_seccomp_profile(const char *key,
3928 struct lxc_conf *c, void *data)
bbca37d8
CB
3929{
3930 free(c->seccomp);
3931 c->seccomp = NULL;
3932 return 0;
3933}
c721e86c 3934
26471403
CB
3935static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
3936 void *data)
c721e86c
CB
3937{
3938 c->autodev = 1;
3939 return 0;
3940}
87b288d1 3941
55c84efc 3942static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
26471403 3943 void *data)
87b288d1
CB
3944{
3945 c->haltsignal = 0;
3946 return 0;
3947}
cae63cfa 3948
55c84efc 3949static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
26471403 3950 void *data)
cae63cfa
CB
3951{
3952 c->rebootsignal = 0;
3953 return 0;
3954}
de45f3a8 3955
55c84efc 3956static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
26471403 3957 void *data)
de45f3a8
CB
3958{
3959 c->stopsignal = 0;
3960 return 0;
3961}
c6182222 3962
26471403
CB
3963static inline int clr_config_start(const char *key, struct lxc_conf *c,
3964 void *data)
c6182222
CB
3965{
3966 if (strcmp(key + 10, "auto") == 0)
3967 c->start_auto = 0;
3968 else if (strcmp(key + 10, "delay") == 0)
3969 c->start_delay = 0;
3970 else if (strcmp(key + 10, "order") == 0)
3971 c->start_order = 0;
3972
3973 return 0;
3974}
998ca94f 3975
46cc906d 3976static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
26471403 3977 void *data)
998ca94f
CB
3978{
3979 free(c->syslog);
3980 c->syslog = NULL;
3981 return 0;
3982}
adad12ca 3983
26471403
CB
3984static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
3985 void *data)
adad12ca
CB
3986{
3987 c->monitor_unshare = 0;
3988 return 0;
3989}
4850d223 3990
26471403
CB
3991static inline int clr_config_group(const char *key, struct lxc_conf *c,
3992 void *data)
4850d223
CB
3993{
3994 return lxc_clear_groups(c);
3995}
832fb63a 3996
26471403
CB
3997static inline int clr_config_environment(const char *key, struct lxc_conf *c,
3998 void *data)
832fb63a
CB
3999{
4000 return lxc_clear_environment(c);
4850d223 4001}
8e90af3e 4002
5cda27c1
SH
4003static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
4004 void *data)
4005{
4006 free(c->execute_cmd);
4007 c->execute_cmd = NULL;
4008 return 0;
4009}
4010
26471403
CB
4011static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
4012 void *data)
8e90af3e
CB
4013{
4014 free(c->init_cmd);
4015 c->init_cmd = NULL;
4016 return 0;
4017}
ec76dcfb 4018
3c491553
L
4019static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
4020 void *data)
4021{
4022 free(c->init_cwd);
4023 c->init_cwd = NULL;
4024 return 0;
4025}
4026
26471403
CB
4027static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
4028 void *data)
ec76dcfb
CB
4029{
4030 c->init_uid = 0;
4031 return 0;
4032}
1044b247 4033
26471403
CB
4034static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
4035 void *data)
1044b247
CB
4036{
4037 c->init_gid = 0;
4038 return 0;
4039}
59e370db 4040
26471403
CB
4041static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
4042 void *data)
59e370db
CB
4043{
4044 c->ephemeral = 0;
4045 return 0;
4046}
b98c5ab0 4047
26471403
CB
4048static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
4049 void *data)
b98c5ab0
CB
4050{
4051 c->no_new_privs = false;
4052 return 0;
4053}
715ccc96 4054
240d4b74 4055static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
26471403 4056 void *data)
715ccc96
CB
4057{
4058 return lxc_clear_limits(c, key);
4059}
fdf3c589 4060
7edd0540
L
4061static inline int clr_config_sysctl(const char *key, struct lxc_conf *c,
4062 void *data)
4063{
4064 return lxc_clear_sysctls(c, key);
4065}
4066
61d7a733
YT
4067static inline int clr_config_proc(const char *key, struct lxc_conf *c,
4068 void *data)
4069{
4070 return lxc_clear_procs(c, key);
4071}
4072
26471403
CB
4073static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
4074 void *data)
fdf3c589
CB
4075{
4076 lxc_clear_includes(c);
4077 return 0;
4078}
a3c8e600 4079
1d8d3676
CB
4080static int clr_config_namespace_clone(const char *key,
4081 struct lxc_conf *lxc_conf, void *data)
4082{
4083 lxc_conf->ns_clone = 0;
4084 return 0;
4085}
4086
abeb5bba
CB
4087static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf,
4088 void *data)
4089{
4090 lxc_conf->ns_keep = 0;
4091 return 0;
4092}
4093
b074bbf1
CB
4094static int clr_config_namespace_share(const char *key,
4095 struct lxc_conf *lxc_conf, void *data)
28d9e29e
CB
4096{
4097 int ns_idx;
4098 const char *namespace;
4099
b074bbf1 4100 namespace = key + sizeof("lxc.namespace.share.") - 1;
28d9e29e
CB
4101 ns_idx = lxc_namespace_2_ns_idx(namespace);
4102 if (ns_idx < 0)
4103 return ns_idx;
4104
b074bbf1
CB
4105 free(lxc_conf->ns_share[ns_idx]);
4106 lxc_conf->ns_share[ns_idx] = NULL;
28d9e29e
CB
4107
4108 return 0;
4109}
4110
a3c8e600 4111static int get_config_includefiles(const char *key, char *retv, int inlen,
cccd2219 4112 struct lxc_conf *c, void *data)
a3c8e600
CB
4113{
4114 return -ENOSYS;
4115}
40db5d2f 4116
31ee747b
CB
4117static struct lxc_config_t *get_network_config_ops(const char *key,
4118 struct lxc_conf *lxc_conf,
4119 ssize_t *idx,
4120 char **deindexed_key)
40db5d2f 4121{
31ee747b
CB
4122 int ret;
4123 unsigned int tmpidx;
4124 size_t numstrlen;
40db5d2f
CB
4125 char *copy, *idx_start, *idx_end;
4126 struct lxc_config_t *config = NULL;
4127
4128 /* check that this is a sensible network key */
31ee747b
CB
4129 if (strncmp("lxc.net.", key, 8)) {
4130 ERROR("Invalid network configuration key \"%s\"", key);
40db5d2f 4131 return NULL;
31ee747b 4132 }
40db5d2f
CB
4133
4134 copy = strdup(key);
31ee747b
CB
4135 if (!copy) {
4136 ERROR("Failed to duplicate string \"%s\"", key);
40db5d2f 4137 return NULL;
31ee747b 4138 }
40db5d2f 4139
f9373e40 4140 /* lxc.net.<n> */
31ee747b
CB
4141 if (!isdigit(*(key + 8))) {
4142 ERROR("Failed to detect digit in string \"%s\"", key + 8);
4143 goto on_error;
4144 }
40db5d2f 4145
31ee747b
CB
4146 /* beginning of index string */
4147 idx_start = (copy + 7);
4148 *idx_start = '\0';
40db5d2f 4149
31ee747b
CB
4150 /* end of index string */
4151 idx_end = strchr((copy + 8), '.');
7451daf8
CB
4152 if (idx_end)
4153 *idx_end = '\0';
31ee747b
CB
4154
4155 /* parse current index */
4156 ret = lxc_safe_uint((idx_start + 1), &tmpidx);
4157 if (ret < 0) {
6d1400b5 4158 errno = -ret;
4159 SYSERROR("Failed to parse usigned integer from string \"%s\"",
4160 idx_start + 1);
31ee747b
CB
4161 *idx = ret;
4162 goto on_error;
4163 }
40db5d2f 4164
31ee747b
CB
4165 /* This, of course is utterly nonsensical on so many levels, but
4166 * better safe than sorry.
4167 * (Checking for INT_MAX here is intentional.)
4168 */
4169 if (tmpidx == INT_MAX) {
25a908b8
CB
4170 SYSERROR("Number of configured networks would overflow the "
4171 "counter");
31ee747b 4172 goto on_error;
40db5d2f 4173 }
31ee747b
CB
4174 *idx = tmpidx;
4175
4176 numstrlen = strlen((idx_start + 1));
4177
4178 /* repair configuration key */
4179 *idx_start = '.';
31ee747b 4180
7451daf8
CB
4181 /* lxc.net.<idx>.<subkey> */
4182 if (idx_end) {
4183 *idx_end = '.';
24fa7df6
FA
4184 if (strlen(idx_end + 1) == 0) {
4185 ERROR("No subkey in network configuration key \"%s\"", key);
4186 goto on_error;
4187 }
40db5d2f 4188
7451daf8
CB
4189 memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
4190 copy[strlen(key) - numstrlen + 1] = '\0';
4191
300df83e 4192 config = lxc_get_config(copy);
7451daf8 4193 if (!config) {
25a908b8 4194 ERROR("Unknown network configuration key \"%s\"", key);
7451daf8
CB
4195 goto on_error;
4196 }
31ee747b
CB
4197 }
4198
7451daf8
CB
4199 if (deindexed_key)
4200 *deindexed_key = copy;
4201
31ee747b 4202 return config;
40db5d2f
CB
4203
4204on_error:
4205 free(copy);
31ee747b 4206 return NULL;
40db5d2f
CB
4207}
4208
25a908b8
CB
4209/* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4210 * found. So we make sure next comes an integer, find the right callback (by
4211 * rewriting the key), and call it.
40db5d2f 4212 */
f9373e40
CB
4213static int set_config_net_nic(const char *key, const char *value,
4214 struct lxc_conf *lxc_conf, void *data)
40db5d2f 4215{
31ee747b
CB
4216 int ret;
4217 const char *idxstring;
40db5d2f
CB
4218 struct lxc_config_t *config;
4219 struct lxc_netdev *netdev;
4220 ssize_t idx = -1;
31ee747b
CB
4221 char *deindexed_key = NULL;
4222
4223 idxstring = key + 8;
4224 if (!isdigit(*idxstring))
4225 return -1;
40db5d2f 4226
6bed0fb6 4227 if (lxc_config_value_empty(value))
f9373e40 4228 return clr_config_net_nic(key, lxc_conf, data);
6bed0fb6 4229
31ee747b 4230 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
40db5d2f
CB
4231 if (!config || idx < 0)
4232 return -1;
4233
0070b1c4 4234 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
31ee747b
CB
4235 if (!netdev) {
4236 free(deindexed_key);
40db5d2f 4237 return -1;
31ee747b 4238 }
40db5d2f 4239
31ee747b
CB
4240 ret = config->set(deindexed_key, value, lxc_conf, netdev);
4241 free(deindexed_key);
4242 return ret;
40db5d2f 4243}
ff6da295 4244
f9373e40
CB
4245static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
4246 void *data)
ff6da295 4247{
31ee747b 4248 int ret;
519df1c1 4249 const char *idxstring;
ff6da295
CB
4250 struct lxc_config_t *config;
4251 struct lxc_netdev *netdev;
31ee747b
CB
4252 ssize_t idx = -1;
4253 char *deindexed_key = NULL;
519df1c1 4254
31ee747b
CB
4255 idxstring = key + 8;
4256 if (!isdigit(*idxstring))
519df1c1
CB
4257 return -1;
4258
519df1c1 4259 /* The left conjunct is pretty self-explanatory. The right conjunct
31ee747b 4260 * checks whether the two pointers are equal. If they are we know that
519df1c1
CB
4261 * this is not a key that is namespaced any further and so we are
4262 * supposed to clear the whole network.
4263 */
4264 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
4265 unsigned int rmnetdevidx;
4266
4267 if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0)
4268 return -1;
4269
4270 /* Remove network from network list. */
4271 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
4272 return 0;
4273 }
ff6da295 4274
31ee747b 4275 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
ff6da295
CB
4276 if (!config || idx < 0)
4277 return -1;
4278
0070b1c4 4279 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false);
31ee747b
CB
4280 if (!netdev) {
4281 free(deindexed_key);
ff6da295 4282 return -1;
31ee747b 4283 }
ff6da295 4284
31ee747b
CB
4285 ret = config->clr(deindexed_key, lxc_conf, netdev);
4286 free(deindexed_key);
4287 return ret;
ff6da295
CB
4288}
4289
f9373e40
CB
4290static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
4291 void *data)
ff6da295 4292{
070a05af 4293 struct lxc_netdev *netdev = data;
ff6da295 4294
ff6da295
CB
4295 if (!netdev)
4296 return -1;
4297
4298 netdev->type = -1;
4299
4300 return 0;
4301}
4302
f9373e40
CB
4303static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
4304 void *data)
ff6da295 4305{
070a05af 4306 struct lxc_netdev *netdev = data;
ff6da295 4307
ff6da295
CB
4308 if (!netdev)
4309 return -1;
4310
de4855a8 4311 netdev->name[0] = '\0';
ff6da295
CB
4312
4313 return 0;
4314}
4315
f9373e40
CB
4316static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
4317 void *data)
ff6da295 4318{
070a05af 4319 struct lxc_netdev *netdev = data;
ff6da295 4320
ff6da295
CB
4321 if (!netdev)
4322 return -1;
4323
4324 netdev->flags = 0;
4325
4326 return 0;
4327}
4328
f9373e40
CB
4329static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
4330 void *data)
ff6da295 4331{
070a05af 4332 struct lxc_netdev *netdev = data;
ff6da295 4333
ff6da295
CB
4334 if (!netdev)
4335 return -1;
4336
de4855a8 4337 netdev->link[0] = '\0';
ff6da295
CB
4338
4339 return 0;
4340}
4341
f9373e40
CB
4342static int clr_config_net_macvlan_mode(const char *key,
4343 struct lxc_conf *lxc_conf, void *data)
ff6da295 4344{
070a05af 4345 struct lxc_netdev *netdev = data;
ff6da295 4346
ff6da295
CB
4347 if (!netdev)
4348 return -1;
4349
6bed0fb6
CB
4350 if (netdev->type != LXC_NET_MACVLAN)
4351 return 0;
4352
ff6da295
CB
4353 netdev->priv.macvlan_attr.mode = -1;
4354
4355 return 0;
4356}
4357
f9373e40
CB
4358static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
4359 void *data)
ff6da295 4360{
070a05af 4361 struct lxc_netdev *netdev = data;
ff6da295 4362
ff6da295
CB
4363 if (!netdev)
4364 return -1;
4365
de4855a8 4366 netdev->priv.veth_attr.pair[0] = '\0';
ff6da295
CB
4367
4368 return 0;
4369}
4370
f9373e40
CB
4371static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
4372 void *data)
ff6da295 4373{
070a05af 4374 struct lxc_netdev *netdev = data;
ff6da295 4375
ff6da295
CB
4376 if (!netdev)
4377 return -1;
4378
4379 free(netdev->upscript);
4380 netdev->upscript = NULL;
4381
4382 return 0;
4383}
4384
f9373e40
CB
4385static int clr_config_net_script_down(const char *key,
4386 struct lxc_conf *lxc_conf, void *data)
ff6da295 4387{
070a05af 4388 struct lxc_netdev *netdev = data;
ff6da295 4389
ff6da295
CB
4390 if (!netdev)
4391 return -1;
4392
4393 free(netdev->downscript);
4394 netdev->downscript = NULL;
4395
4396 return 0;
4397}
4398
f9373e40
CB
4399static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
4400 void *data)
ff6da295 4401{
070a05af 4402 struct lxc_netdev *netdev = data;
ff6da295 4403
ff6da295
CB
4404 if (!netdev)
4405 return -1;
4406
4407 free(netdev->hwaddr);
4408 netdev->hwaddr = NULL;
4409
4410 return 0;
4411}
4412
f9373e40
CB
4413static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
4414 void *data)
ff6da295 4415{
070a05af 4416 struct lxc_netdev *netdev = data;
ff6da295 4417
ff6da295
CB
4418 if (!netdev)
4419 return -1;
4420
4421 free(netdev->mtu);
4422 netdev->mtu = NULL;
4423
4424 return 0;
4425}
4426
f9373e40
CB
4427static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
4428 void *data)
ff6da295 4429{
070a05af 4430 struct lxc_netdev *netdev = data;
ff6da295 4431
ff6da295
CB
4432 if (!netdev)
4433 return -1;
4434
4435 netdev->priv.vlan_attr.vid = 0;
4436
4437 return 0;
4438}
4439
f9373e40
CB
4440static int clr_config_net_ipv4_gateway(const char *key,
4441 struct lxc_conf *lxc_conf, void *data)
ff6da295 4442{
070a05af 4443 struct lxc_netdev *netdev = data;
ff6da295 4444
ff6da295
CB
4445 if (!netdev)
4446 return -1;
4447
4448 free(netdev->ipv4_gateway);
4449 netdev->ipv4_gateway = NULL;
4450
4451 return 0;
4452}
4453
9ff60df2
CB
4454static int clr_config_net_ipv4_address(const char *key,
4455 struct lxc_conf *lxc_conf, void *data)
ff6da295 4456{
070a05af 4457 struct lxc_netdev *netdev = data;
ff6da295
CB
4458 struct lxc_list *cur, *next;
4459
ecbb3790
CB
4460 if (!netdev)
4461 return -1;
ff6da295
CB
4462
4463 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
4464 lxc_list_del(cur);
4465 free(cur->elem);
4466 free(cur);
4467 }
4468
4469 return 0;
4470}
4471
f9373e40
CB
4472static int clr_config_net_ipv6_gateway(const char *key,
4473 struct lxc_conf *lxc_conf, void *data)
ff6da295 4474{
070a05af 4475 struct lxc_netdev *netdev = data;
ff6da295 4476
ff6da295
CB
4477 if (!netdev)
4478 return -1;
4479
4480 free(netdev->ipv6_gateway);
4481 netdev->ipv6_gateway = NULL;
4482
4483 return 0;
4484}
4485
2e44ae28
CB
4486static int clr_config_net_ipv6_address(const char *key,
4487 struct lxc_conf *lxc_conf, void *data)
ff6da295 4488{
070a05af 4489 struct lxc_netdev *netdev = data;
ff6da295
CB
4490 struct lxc_list *cur, *next;
4491
ecbb3790
CB
4492 if (!netdev)
4493 return -1;
ff6da295
CB
4494
4495 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
4496 lxc_list_del(cur);
4497 free(cur->elem);
4498 free(cur);
4499 }
4500
4501 return 0;
4502}
9d4bf22d 4503
f9373e40
CB
4504static int get_config_net_nic(const char *key, char *retv, int inlen,
4505 struct lxc_conf *c, void *data)
9d4bf22d 4506{
31ee747b
CB
4507 int ret;
4508 const char *idxstring;
9d4bf22d
CB
4509 struct lxc_config_t *config;
4510 struct lxc_netdev *netdev;
4511 ssize_t idx = -1;
31ee747b 4512 char *deindexed_key = NULL;
9d4bf22d 4513
31ee747b
CB
4514 idxstring = key + 8;
4515 if (!isdigit(*idxstring))
4516 return -1;
4517
4518 config = get_network_config_ops(key, c, &idx, &deindexed_key);
9d4bf22d
CB
4519 if (!config || idx < 0)
4520 return -1;
4521
0070b1c4 4522 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
31ee747b
CB
4523 if (!netdev) {
4524 free(deindexed_key);
9d4bf22d 4525 return -1;
31ee747b 4526 }
9d4bf22d 4527
31ee747b
CB
4528 ret = config->get(deindexed_key, retv, inlen, c, netdev);
4529 free(deindexed_key);
4530 return ret;
9d4bf22d
CB
4531}
4532
f9373e40
CB
4533static int get_config_net_type(const char *key, char *retv, int inlen,
4534 struct lxc_conf *c, void *data)
9d4bf22d 4535{
070a05af
CB
4536 int len;
4537 int fulllen = 0;
4538 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4539
4540 if (!retv)
4541 inlen = 0;
4542 else
4543 memset(retv, 0, inlen);
4544
9d4bf22d
CB
4545 if (!netdev)
4546 return -1;
4547
4548 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
4549
4550 return fulllen;
4551}
4552
f9373e40
CB
4553static int get_config_net_flags(const char *key, char *retv, int inlen,
4554 struct lxc_conf *c, void *data)
9d4bf22d 4555{
070a05af
CB
4556 int len;
4557 int fulllen = 0;
4558 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4559
4560 if (!retv)
4561 inlen = 0;
4562 else
4563 memset(retv, 0, inlen);
4564
9d4bf22d
CB
4565 if (!netdev)
4566 return -1;
4567
4568 if (netdev->flags & IFF_UP)
4569 strprint(retv, inlen, "up");
4570
4571 return fulllen;
4572}
4573
f9373e40
CB
4574static int get_config_net_link(const char *key, char *retv, int inlen,
4575 struct lxc_conf *c, void *data)
9d4bf22d 4576{
070a05af
CB
4577 int len;
4578 int fulllen = 0;
4579 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4580
4581 if (!retv)
4582 inlen = 0;
4583 else
4584 memset(retv, 0, inlen);
4585
9d4bf22d
CB
4586 if (!netdev)
4587 return -1;
4588
de4855a8 4589 if (netdev->link[0] != '\0')
9d4bf22d
CB
4590 strprint(retv, inlen, "%s", netdev->link);
4591
4592 return fulllen;
4593}
4594
f9373e40
CB
4595static int get_config_net_name(const char *key, char *retv, int inlen,
4596 struct lxc_conf *c, void *data)
9d4bf22d 4597{
070a05af
CB
4598 int len;
4599 int fulllen = 0;
4600 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4601
4602 if (!retv)
4603 inlen = 0;
4604 else
4605 memset(retv, 0, inlen);
4606
9d4bf22d
CB
4607 if (!netdev)
4608 return -1;
4609
de4855a8 4610 if (netdev->name[0] != '\0')
9d4bf22d
CB
4611 strprint(retv, inlen, "%s", netdev->name);
4612
4613 return fulllen;
4614}
4615
f9373e40
CB
4616static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
4617 struct lxc_conf *c, void *data)
9d4bf22d 4618{
070a05af
CB
4619 int len;
4620 int fulllen = 0;
9d4bf22d 4621 const char *mode;
070a05af 4622 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4623
4624 if (!retv)
4625 inlen = 0;
4626 else
4627 memset(retv, 0, inlen);
4628
9d4bf22d
CB
4629 if (!netdev)
4630 return -1;
4631
4632 if (netdev->type != LXC_NET_MACVLAN)
4633 return 0;
4634
4635 switch (netdev->priv.macvlan_attr.mode) {
4636 case MACVLAN_MODE_PRIVATE:
4637 mode = "private";
4638 break;
4639 case MACVLAN_MODE_VEPA:
4640 mode = "vepa";
4641 break;
4642 case MACVLAN_MODE_BRIDGE:
4643 mode = "bridge";
4644 break;
4645 case MACVLAN_MODE_PASSTHRU:
4646 mode = "passthru";
4647 break;
4648 default:
4649 mode = "(invalid)";
4650 break;
4651 }
4652
4653 strprint(retv, inlen, "%s", mode);
4654
4655 return fulllen;
4656}
4657
f9373e40
CB
4658static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
4659 struct lxc_conf *c, void *data)
9d4bf22d 4660{
070a05af
CB
4661 int len;
4662 int fulllen = 0;
4663 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4664
4665 if (!retv)
4666 inlen = 0;
4667 else
4668 memset(retv, 0, inlen);
4669
9d4bf22d
CB
4670 if (!netdev)
4671 return -1;
4672
4673 if (netdev->type != LXC_NET_VETH)
4674 return 0;
4675
4676 strprint(retv, inlen, "%s",
de4855a8
CB
4677 netdev->priv.veth_attr.pair[0] != '\0'
4678 ? netdev->priv.veth_attr.pair
4679 : netdev->priv.veth_attr.veth1);
9d4bf22d
CB
4680
4681 return fulllen;
4682}
4683
f9373e40
CB
4684static int get_config_net_script_up(const char *key, char *retv, int inlen,
4685 struct lxc_conf *c, void *data)
9d4bf22d 4686{
070a05af
CB
4687 int len;
4688 int fulllen = 0;
4689 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4690
4691 if (!retv)
4692 inlen = 0;
4693 else
4694 memset(retv, 0, inlen);
4695
9d4bf22d
CB
4696 if (!netdev)
4697 return -1;
4698
4699 if (netdev->upscript)
4700 strprint(retv, inlen, "%s", netdev->upscript);
4701
4702 return fulllen;
4703}
4704
f9373e40
CB
4705static int get_config_net_script_down(const char *key, char *retv, int inlen,
4706 struct lxc_conf *c, void *data)
9d4bf22d 4707{
070a05af
CB
4708 int len;
4709 int fulllen = 0;
4710 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4711
4712 if (!retv)
4713 inlen = 0;
4714 else
4715 memset(retv, 0, inlen);
4716
9d4bf22d
CB
4717 if (!netdev)
4718 return -1;
4719
4720 if (netdev->downscript)
4721 strprint(retv, inlen, "%s", netdev->downscript);
4722
4723 return fulllen;
4724}
4725
f9373e40
CB
4726static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
4727 struct lxc_conf *c, void *data)
9d4bf22d 4728{
070a05af
CB
4729 int len;
4730 int fulllen = 0;
4731 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4732
4733 if (!retv)
4734 inlen = 0;
4735 else
4736 memset(retv, 0, inlen);
4737
9d4bf22d
CB
4738 if (!netdev)
4739 return -1;
4740
4741 if (netdev->hwaddr)
4742 strprint(retv, inlen, "%s", netdev->hwaddr);
4743
4744 return fulllen;
4745}
4746
f9373e40
CB
4747static int get_config_net_mtu(const char *key, char *retv, int inlen,
4748 struct lxc_conf *c, void *data)
9d4bf22d 4749{
070a05af
CB
4750 int len;
4751 int fulllen = 0;
4752 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4753
4754 if (!retv)
4755 inlen = 0;
4756 else
4757 memset(retv, 0, inlen);
4758
9d4bf22d
CB
4759 if (!netdev)
4760 return -1;
4761
4762 if (netdev->mtu)
4763 strprint(retv, inlen, "%s", netdev->mtu);
4764
4765 return fulllen;
4766}
4767
f9373e40
CB
4768static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
4769 struct lxc_conf *c, void *data)
9d4bf22d 4770{
070a05af
CB
4771 int len;
4772 int fulllen = 0;
4773 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4774
4775 if (!retv)
4776 inlen = 0;
4777 else
4778 memset(retv, 0, inlen);
4779
9d4bf22d
CB
4780 if (!netdev)
4781 return -1;
4782
4783 if (netdev->type != LXC_NET_VLAN)
4784 return 0;
4785
4786 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
4787
4788 return fulllen;
4789}
4790
f9373e40
CB
4791static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
4792 struct lxc_conf *c, void *data)
9d4bf22d 4793{
070a05af 4794 int len;
9d4bf22d 4795 char buf[INET_ADDRSTRLEN];
070a05af
CB
4796 int fulllen = 0;
4797 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4798
4799 if (!retv)
4800 inlen = 0;
4801 else
4802 memset(retv, 0, inlen);
4803
9d4bf22d
CB
4804 if (!netdev)
4805 return -1;
4806
4807 if (netdev->ipv4_gateway_auto) {
4808 strprint(retv, inlen, "auto");
4809 } else if (netdev->ipv4_gateway) {
4810 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
4811 strprint(retv, inlen, "%s", buf);
4812 }
4813
4814 return fulllen;
4815}
4816
9ff60df2
CB
4817static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
4818 struct lxc_conf *c, void *data)
9d4bf22d 4819{
070a05af 4820 int len;
9d4bf22d
CB
4821 size_t listlen;
4822 char buf[INET_ADDRSTRLEN];
9d4bf22d 4823 struct lxc_list *it;
070a05af
CB
4824 int fulllen = 0;
4825 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4826
4827 if (!retv)
4828 inlen = 0;
4829 else
4830 memset(retv, 0, inlen);
4831
9d4bf22d
CB
4832 if (!netdev)
4833 return -1;
4834
4835 listlen = lxc_list_len(&netdev->ipv4);
4836 lxc_list_for_each(it, &netdev->ipv4) {
4837 struct lxc_inetdev *i = it->elem;
4838 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6fc7d8b6 4839 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
4840 (listlen-- > 1) ? "\n" : "");
4841 }
4842
4843 return fulllen;
4844}
4845
f9373e40
CB
4846static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
4847 struct lxc_conf *c, void *data)
9d4bf22d 4848{
070a05af 4849 int len;
9d4bf22d 4850 char buf[INET6_ADDRSTRLEN];
070a05af
CB
4851 int fulllen = 0;
4852 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4853
4854 if (!retv)
4855 inlen = 0;
4856 else
4857 memset(retv, 0, inlen);
4858
9d4bf22d
CB
4859 if (!netdev)
4860 return -1;
4861
4862 if (netdev->ipv6_gateway_auto) {
4863 strprint(retv, inlen, "auto");
4864 } else if (netdev->ipv6_gateway) {
4865 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
4866 strprint(retv, inlen, "%s", buf);
4867 }
4868
4869 return fulllen;
4870}
4871
2e44ae28
CB
4872static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
4873 struct lxc_conf *c, void *data)
9d4bf22d 4874{
070a05af 4875 int len;
9d4bf22d
CB
4876 size_t listlen;
4877 char buf[INET6_ADDRSTRLEN];
9d4bf22d 4878 struct lxc_list *it;
070a05af
CB
4879 int fulllen = 0;
4880 struct lxc_netdev *netdev = data;
9d4bf22d
CB
4881
4882 if (!retv)
4883 inlen = 0;
4884 else
4885 memset(retv, 0, inlen);
4886
9d4bf22d
CB
4887 if (!netdev)
4888 return -1;
4889
4890 listlen = lxc_list_len(&netdev->ipv6);
4891 lxc_list_for_each(it, &netdev->ipv6) {
4892 struct lxc_inet6dev *i = it->elem;
4893 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6fc7d8b6 4894 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
4895 (listlen-- > 1) ? "\n" : "");
4896 }
4897
4898 return fulllen;
4899}
cfc67626
CB
4900
4901int lxc_list_config_items(char *retv, int inlen)
4902{
4903 size_t i;
4904 int len;
4905 int fulllen = 0;
4906
4907 if (!retv)
4908 inlen = 0;
4909 else
4910 memset(retv, 0, inlen);
4911
4912 for (i = 0; i < config_size; i++) {
4913 char *s = config[i].name;
300df83e 4914
cfc67626
CB
4915 if (s[strlen(s) - 1] == '.')
4916 continue;
300df83e 4917
cfc67626
CB
4918 strprint(retv, inlen, "%s\n", s);
4919 }
4920
4921 return fulllen;
4922}
a5448c15 4923
fe9b7349
CB
4924int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
4925 int inlen)
4926{
4927 int len;
4928 int fulllen = 0;
4929
4930 if (!retv)
4931 inlen = 0;
4932 else
4933 memset(retv, 0, inlen);
4934
4935 if (!strcmp(key, "lxc.apparmor")) {
4936 strprint(retv, inlen, "allow_incomplete\n");
4937 strprint(retv, inlen, "profile\n");
dae8c253
CB
4938 } else if (!strcmp(key, "lxc.cgroup")) {
4939 strprint(retv, inlen, "dir\n");
fe9b7349
CB
4940 } else if (!strcmp(key, "lxc.selinux")) {
4941 strprint(retv, inlen, "context\n");
4942 } else if (!strcmp(key, "lxc.mount")) {
4943 strprint(retv, inlen, "auto\n");
4944 strprint(retv, inlen, "entry\n");
4945 strprint(retv, inlen, "fstab\n");
4946 } else if (!strcmp(key, "lxc.rootfs")) {
4947 strprint(retv, inlen, "mount\n");
4948 strprint(retv, inlen, "options\n");
4949 strprint(retv, inlen, "path\n");
4950 } else if (!strcmp(key, "lxc.uts")) {
4951 strprint(retv, inlen, "name\n");
4952 } else if (!strcmp(key, "lxc.hook")) {
4953 strprint(retv, inlen, "autodev\n");
4954 strprint(retv, inlen, "clone\n");
4955 strprint(retv, inlen, "destroy\n");
4956 strprint(retv, inlen, "mount\n");
4957 strprint(retv, inlen, "post-stop\n");
4958 strprint(retv, inlen, "pre-mount\n");
4959 strprint(retv, inlen, "pre-start\n");
08dd2805 4960 strprint(retv, inlen, "start-host\n");
fe9b7349
CB
4961 strprint(retv, inlen, "start\n");
4962 strprint(retv, inlen, "stop\n");
4963 } else if (!strcmp(key, "lxc.cap")) {
4964 strprint(retv, inlen, "drop\n");
4965 strprint(retv, inlen, "keep\n");
4966 } else if (!strcmp(key, "lxc.console")) {
4967 strprint(retv, inlen, "logfile\n");
4968 strprint(retv, inlen, "path\n");
4969 } else if (!strcmp(key, "lxc.seccomp")) {
4970 strprint(retv, inlen, "profile\n");
4971 } else if (!strcmp(key, "lxc.signal")) {
4972 strprint(retv, inlen, "halt\n");
4973 strprint(retv, inlen, "reboot\n");
4974 strprint(retv, inlen, "stop\n");
4975 } else if (!strcmp(key, "lxc.start")) {
4976 strprint(retv, inlen, "auto\n");
4977 strprint(retv, inlen, "delay\n");
4978 strprint(retv, inlen, "order\n");
4979 } else if (!strcmp(key, "lxc.monitor")) {
4980 strprint(retv, inlen, "unshare\n");
4981 } else {
4982 fulllen = -1;
4983 }
4984
4985 return fulllen;
4986}
4987
a5448c15
CB
4988int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
4989{
4990 int len;
4991 const char *idxstring;
a5448c15
CB
4992 struct lxc_netdev *netdev;
4993 int fulllen = 0;
4994 ssize_t idx = -1;
a5448c15
CB
4995
4996 idxstring = key + 8;
4997 if (!isdigit(*idxstring))
4998 return -1;
4999
7451daf8
CB
5000 (void)get_network_config_ops(key, c, &idx, NULL);
5001 if (idx < 0)
a5448c15
CB
5002 return -1;
5003
5004 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
a5448c15
CB
5005 if (!netdev)
5006 return -1;
5007
5008 if (!retv)
5009 inlen = 0;
5010 else
5011 memset(retv, 0, inlen);
5012
5013 strprint(retv, inlen, "type\n");
5014 strprint(retv, inlen, "script.up\n");
5015 strprint(retv, inlen, "script.down\n");
5016 if (netdev->type != LXC_NET_EMPTY) {
5017 strprint(retv, inlen, "flags\n");
5018 strprint(retv, inlen, "link\n");
5019 strprint(retv, inlen, "name\n");
5020 strprint(retv, inlen, "hwaddr\n");
5021 strprint(retv, inlen, "mtu\n");
5022 strprint(retv, inlen, "ipv6.address\n");
5023 strprint(retv, inlen, "ipv6.gateway\n");
5024 strprint(retv, inlen, "ipv4.address\n");
5025 strprint(retv, inlen, "ipv4.gateway\n");
5026 }
5027
5028 switch (netdev->type) {
5029 case LXC_NET_VETH:
5030 strprint(retv, inlen, "veth.pair\n");
5031 break;
5032 case LXC_NET_MACVLAN:
5033 strprint(retv, inlen, "macvlan.mode\n");
5034 break;
5035 case LXC_NET_VLAN:
5036 strprint(retv, inlen, "vlan.id\n");
5037 break;
5038 case LXC_NET_PHYS:
5039 break;
5040 }
5041
5042 return fulllen;
5043}