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