]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/confile.c
confile: cleanup set_config_environment()
[mirror_lxc.git] / src / lxc / confile.c
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
70c1e708 2
d38dd64a
CB
3#ifndef _GNU_SOURCE
4#define _GNU_SOURCE 1
5#endif
70c1e708 6#define __STDC_FORMAT_MACROS
d38dd64a 7#include <arpa/inet.h>
70c1e708
CB
8#include <ctype.h>
9#include <dirent.h>
10#include <errno.h>
11#include <fcntl.h>
12#include <inttypes.h>
d38dd64a
CB
13#include <net/if.h>
14#include <netinet/in.h>
70c1e708 15#include <signal.h>
c2cc9f0a 16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
70c1e708 19#include <sys/param.h>
63376d7d 20#include <sys/stat.h>
c2cc9f0a 21#include <sys/types.h>
c2cc9f0a 22#include <sys/utsname.h>
d38dd64a
CB
23#include <syslog.h>
24#include <time.h>
25#include <unistd.h>
c2cc9f0a 26
86ce1da1 27#include "af_unix.h"
70c1e708 28#include "conf.h"
6ff05e18 29#include "config.h"
525f0002 30#include "confile.h"
0b843d35 31#include "confile_utils.h"
d38dd64a 32#include "../include/netns_ifaddrs.h"
f2363e38 33#include "log.h"
58e0f57d 34#include "lxcseccomp.h"
fdf76c6d 35#include "macro.h"
f01d0358 36#include "memory_utils.h"
70c1e708
CB
37#include "network.h"
38#include "parse.h"
28d832c4 39#include "storage.h"
70c1e708 40#include "utils.h"
36eb9bde 41
6ff05e18
SG
42#if HAVE_SYS_PERSONALITY_H
43#include <sys/personality.h>
44#endif
45
43f984ea
DJ
46#ifndef HAVE_STRLCPY
47#include "include/strlcpy.h"
48#endif
49
ebcd5140
DJ
50#ifndef HAVE_STRLCAT
51#include "include/strlcat.h"
52#endif
53
ac2cecc4 54lxc_log_define(confile, lxc);
576f946d 55
afeec9b7
CB
56#define lxc_config_define(name) \
57 __hot static int set_config_##name(const char *, const char *, \
58 struct lxc_conf *, void *); \
59 __hot static int get_config_##name(const char *, char *, int, \
60 struct lxc_conf *, void *); \
61 __hot static int clr_config_##name(const char *, struct lxc_conf *, \
62 void *);
71e287ca 63
63bab717 64lxc_config_define(autodev);
63012bdd 65lxc_config_define(autodev_tmpfs_size);
71e287ca 66lxc_config_define(apparmor_allow_incomplete);
1800f924 67lxc_config_define(apparmor_allow_nesting);
63bab717 68lxc_config_define(apparmor_profile);
1800f924 69lxc_config_define(apparmor_raw);
63bab717
CB
70lxc_config_define(cap_drop);
71lxc_config_define(cap_keep);
43654d34 72lxc_config_define(cgroup_controller);
54860ed0 73lxc_config_define(cgroup2_controller);
43654d34 74lxc_config_define(cgroup_dir);
a900cbaf 75lxc_config_define(cgroup_monitor_dir);
7696c1f9 76lxc_config_define(cgroup_monitor_pivot_dir);
a900cbaf
WB
77lxc_config_define(cgroup_container_dir);
78lxc_config_define(cgroup_container_inner_dir);
9caee129 79lxc_config_define(cgroup_relative);
28f3b1cd 80lxc_config_define(console_buffer_size);
861813e5 81lxc_config_define(console_logfile);
63bab717 82lxc_config_define(console_path);
861813e5
CB
83lxc_config_define(console_rotate);
84lxc_config_define(console_size);
63bab717
CB
85lxc_config_define(environment);
86lxc_config_define(ephemeral);
87lxc_config_define(execute_cmd);
88lxc_config_define(group);
89lxc_config_define(hooks);
44ae0fb6 90lxc_config_define(hooks_version);
71e287ca 91lxc_config_define(idmaps);
63bab717
CB
92lxc_config_define(includefiles);
93lxc_config_define(init_cmd);
3c491553 94lxc_config_define(init_cwd);
63bab717
CB
95lxc_config_define(init_gid);
96lxc_config_define(init_uid);
8f818a84 97lxc_config_define(keyring_session);
46cc906d 98lxc_config_define(log_file);
63bab717
CB
99lxc_config_define(log_level);
100lxc_config_define(log_syslog);
101lxc_config_define(monitor);
258f8051 102lxc_config_define(monitor_signal_pdeath);
71e287ca 103lxc_config_define(mount);
104lxc_config_define(mount_auto);
47148e96 105lxc_config_define(mount_fstab);
1d8d3676 106lxc_config_define(namespace_clone);
abeb5bba 107lxc_config_define(namespace_keep);
70fd7fc9
CB
108lxc_config_define(time_offset_boot);
109lxc_config_define(time_offset_monotonic);
b074bbf1 110lxc_config_define(namespace_share);
63bab717 111lxc_config_define(net);
71e287ca 112lxc_config_define(net_flags);
71e287ca 113lxc_config_define(net_hwaddr);
9ff60df2 114lxc_config_define(net_ipv4_address);
71e287ca 115lxc_config_define(net_ipv4_gateway);
2e44ae28 116lxc_config_define(net_ipv6_address);
71e287ca 117lxc_config_define(net_ipv6_gateway);
63bab717 118lxc_config_define(net_link);
6509154d 119lxc_config_define(net_l2proxy);
63bab717 120lxc_config_define(net_macvlan_mode);
c9f52382 121lxc_config_define(net_ipvlan_mode);
122lxc_config_define(net_ipvlan_isolation);
63bab717
CB
123lxc_config_define(net_mtu);
124lxc_config_define(net_name);
71e287ca 125lxc_config_define(net_nic);
63bab717
CB
126lxc_config_define(net_script_down);
127lxc_config_define(net_script_up);
128lxc_config_define(net_type);
3f0ed090 129lxc_config_define(net_veth_mode);
63bab717 130lxc_config_define(net_veth_pair);
d4a7da46 131lxc_config_define(net_veth_ipv4_route);
132lxc_config_define(net_veth_ipv6_route);
fdf76c6d 133lxc_config_define(net_veth_vlan_id);
b8e06d33 134lxc_config_define(net_veth_vlan_tagged_id);
63bab717
CB
135lxc_config_define(net_vlan_id);
136lxc_config_define(no_new_privs);
63bab717
CB
137lxc_config_define(personality);
138lxc_config_define(prlimit);
139lxc_config_define(pty_max);
6e54330c 140lxc_config_define(rootfs_managed);
63bab717
CB
141lxc_config_define(rootfs_mount);
142lxc_config_define(rootfs_options);
143lxc_config_define(rootfs_path);
0b427da0 144lxc_config_define(seccomp_profile);
50d86993 145lxc_config_define(seccomp_allow_nesting);
84cf6d25 146lxc_config_define(seccomp_notify_cookie);
86ce1da1 147lxc_config_define(seccomp_notify_proxy);
63bab717 148lxc_config_define(selinux_context);
4fef78bc 149lxc_config_define(selinux_context_keyring);
55c84efc 150lxc_config_define(signal_halt);
151lxc_config_define(signal_reboot);
152lxc_config_define(signal_stop);
71e287ca 153lxc_config_define(start);
63bab717
CB
154lxc_config_define(tty_max);
155lxc_config_define(tty_dir);
156lxc_config_define(uts_name);
7edd0540 157lxc_config_define(sysctl);
61d7a733 158lxc_config_define(proc);
c2cc9f0a 159
20c3318a
MB
160/*
161 * Important Note:
4110345b 162 * If a new config option is added to this table, be aware that
20c3318a
MB
163 * the order in which the options are places into the table matters.
164 * That means that more specific options of a namespace have to be
165 * placed above more generic ones.
166 *
167 * For instance: If lxc.ab is placed before lxc.ab.c, the config option
168 * lxc.ab.c will always be matched to lxc.ab. That is, the lxc.ab.c option
169 * has to be placed above lxc.ab.
170 */
33eb2ec1 171static struct lxc_config_t config_jump_table[] = {
70fd7fc9
CB
172 { "lxc.arch", set_config_personality, get_config_personality, clr_config_personality, },
173 { "lxc.apparmor.profile", set_config_apparmor_profile, get_config_apparmor_profile, clr_config_apparmor_profile, },
174 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete, get_config_apparmor_allow_incomplete, clr_config_apparmor_allow_incomplete, },
175 { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting, get_config_apparmor_allow_nesting, clr_config_apparmor_allow_nesting, },
176 { "lxc.apparmor.raw", set_config_apparmor_raw, get_config_apparmor_raw, clr_config_apparmor_raw, },
177 { "lxc.autodev.tmpfs.size", set_config_autodev_tmpfs_size, get_config_autodev_tmpfs_size, clr_config_autodev_tmpfs_size, },
178 { "lxc.autodev", set_config_autodev, get_config_autodev, clr_config_autodev, },
179 { "lxc.cap.drop", set_config_cap_drop, get_config_cap_drop, clr_config_cap_drop, },
180 { "lxc.cap.keep", set_config_cap_keep, get_config_cap_keep, clr_config_cap_keep, },
181 { "lxc.cgroup2", set_config_cgroup2_controller, get_config_cgroup2_controller, clr_config_cgroup2_controller, },
7696c1f9 182 { "lxc.cgroup.dir.monitor.pivot", set_config_cgroup_monitor_pivot_dir, get_config_cgroup_monitor_pivot_dir, clr_config_cgroup_monitor_pivot_dir, },
70fd7fc9
CB
183 { "lxc.cgroup.dir.monitor", set_config_cgroup_monitor_dir, get_config_cgroup_monitor_dir, clr_config_cgroup_monitor_dir, },
184 { "lxc.cgroup.dir.container.inner", set_config_cgroup_container_inner_dir, get_config_cgroup_container_inner_dir, clr_config_cgroup_container_inner_dir, },
185 { "lxc.cgroup.dir.container", set_config_cgroup_container_dir, get_config_cgroup_container_dir, clr_config_cgroup_container_dir, },
186 { "lxc.cgroup.dir", set_config_cgroup_dir, get_config_cgroup_dir, clr_config_cgroup_dir, },
187 { "lxc.cgroup.relative", set_config_cgroup_relative, get_config_cgroup_relative, clr_config_cgroup_relative, },
188 { "lxc.cgroup", set_config_cgroup_controller, get_config_cgroup_controller, clr_config_cgroup_controller, },
189 { "lxc.console.buffer.size", set_config_console_buffer_size, get_config_console_buffer_size, clr_config_console_buffer_size, },
190 { "lxc.console.logfile", set_config_console_logfile, get_config_console_logfile, clr_config_console_logfile, },
191 { "lxc.console.path", set_config_console_path, get_config_console_path, clr_config_console_path, },
192 { "lxc.console.rotate", set_config_console_rotate, get_config_console_rotate, clr_config_console_rotate, },
193 { "lxc.console.size", set_config_console_size, get_config_console_size, clr_config_console_size, },
194 { "lxc.environment", set_config_environment, get_config_environment, clr_config_environment, },
195 { "lxc.ephemeral", set_config_ephemeral, get_config_ephemeral, clr_config_ephemeral, },
196 { "lxc.execute.cmd", set_config_execute_cmd, get_config_execute_cmd, clr_config_execute_cmd, },
197 { "lxc.group", set_config_group, get_config_group, clr_config_group, },
198 { "lxc.hook.autodev", set_config_hooks, get_config_hooks, clr_config_hooks, },
199 { "lxc.hook.clone", set_config_hooks, get_config_hooks, clr_config_hooks, },
200 { "lxc.hook.destroy", set_config_hooks, get_config_hooks, clr_config_hooks, },
201 { "lxc.hook.mount", set_config_hooks, get_config_hooks, clr_config_hooks, },
202 { "lxc.hook.post-stop", set_config_hooks, get_config_hooks, clr_config_hooks, },
203 { "lxc.hook.pre-mount", set_config_hooks, get_config_hooks, clr_config_hooks, },
204 { "lxc.hook.pre-start", set_config_hooks, get_config_hooks, clr_config_hooks, },
205 { "lxc.hook.start", set_config_hooks, get_config_hooks, clr_config_hooks, },
206 { "lxc.hook.start-host", set_config_hooks, get_config_hooks, clr_config_hooks, },
207 { "lxc.hook.stop", set_config_hooks, get_config_hooks, clr_config_hooks, },
208 { "lxc.hook.version", set_config_hooks_version, get_config_hooks_version, clr_config_hooks_version, },
209 { "lxc.hook", set_config_hooks, get_config_hooks, clr_config_hooks, },
210 { "lxc.idmap", set_config_idmaps, get_config_idmaps, clr_config_idmaps, },
211 { "lxc.include", set_config_includefiles, get_config_includefiles, clr_config_includefiles, },
212 { "lxc.init.cmd", set_config_init_cmd, get_config_init_cmd, clr_config_init_cmd, },
213 { "lxc.init.gid", set_config_init_gid, get_config_init_gid, clr_config_init_gid, },
214 { "lxc.init.uid", set_config_init_uid, get_config_init_uid, clr_config_init_uid, },
215 { "lxc.init.cwd", set_config_init_cwd, get_config_init_cwd, clr_config_init_cwd, },
216 { "lxc.keyring.session", set_config_keyring_session, get_config_keyring_session, clr_config_keyring_session },
217 { "lxc.log.file", set_config_log_file, get_config_log_file, clr_config_log_file, },
218 { "lxc.log.level", set_config_log_level, get_config_log_level, clr_config_log_level, },
219 { "lxc.log.syslog", set_config_log_syslog, get_config_log_syslog, clr_config_log_syslog, },
220 { "lxc.monitor.unshare", set_config_monitor, get_config_monitor, clr_config_monitor, },
221 { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath, get_config_monitor_signal_pdeath, clr_config_monitor_signal_pdeath, },
222 { "lxc.mount.auto", set_config_mount_auto, get_config_mount_auto, clr_config_mount_auto, },
223 { "lxc.mount.entry", set_config_mount, get_config_mount, clr_config_mount, },
224 { "lxc.mount.fstab", set_config_mount_fstab, get_config_mount_fstab, clr_config_mount_fstab, },
225 { "lxc.namespace.clone", set_config_namespace_clone, get_config_namespace_clone, clr_config_namespace_clone, },
226 { "lxc.namespace.keep", set_config_namespace_keep, get_config_namespace_keep, clr_config_namespace_keep, },
227 { "lxc.namespace.share", set_config_namespace_share, get_config_namespace_share, clr_config_namespace_share, },
228 { "lxc.time.offset.boot", set_config_time_offset_boot, get_config_time_offset_boot, clr_config_time_offset_boot, },
229 { "lxc.time.offset.monotonic", set_config_time_offset_monotonic, get_config_time_offset_monotonic, clr_config_time_offset_monotonic, },
230 { "lxc.net.flags", set_config_net_flags, get_config_net_flags, clr_config_net_flags, },
231 { "lxc.net.hwaddr", set_config_net_hwaddr, get_config_net_hwaddr, clr_config_net_hwaddr, },
232 { "lxc.net.ipv4.address", set_config_net_ipv4_address, get_config_net_ipv4_address, clr_config_net_ipv4_address, },
233 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway, get_config_net_ipv4_gateway, clr_config_net_ipv4_gateway, },
234 { "lxc.net.ipv6.address", set_config_net_ipv6_address, get_config_net_ipv6_address, clr_config_net_ipv6_address, },
235 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway, get_config_net_ipv6_gateway, clr_config_net_ipv6_gateway, },
236 { "lxc.net.link", set_config_net_link, get_config_net_link, clr_config_net_link, },
237 { "lxc.net.l2proxy", set_config_net_l2proxy, get_config_net_l2proxy, clr_config_net_l2proxy, },
238 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode, get_config_net_macvlan_mode, clr_config_net_macvlan_mode, },
239 { "lxc.net.ipvlan.mode", set_config_net_ipvlan_mode, get_config_net_ipvlan_mode, clr_config_net_ipvlan_mode, },
240 { "lxc.net.ipvlan.isolation", set_config_net_ipvlan_isolation, get_config_net_ipvlan_isolation, clr_config_net_ipvlan_isolation, },
241 { "lxc.net.mtu", set_config_net_mtu, get_config_net_mtu, clr_config_net_mtu, },
242 { "lxc.net.name", set_config_net_name, get_config_net_name, clr_config_net_name, },
243 { "lxc.net.script.down", set_config_net_script_down, get_config_net_script_down, clr_config_net_script_down, },
244 { "lxc.net.script.up", set_config_net_script_up, get_config_net_script_up, clr_config_net_script_up, },
245 { "lxc.net.type", set_config_net_type, get_config_net_type, clr_config_net_type, },
246 { "lxc.net.vlan.id", set_config_net_vlan_id, get_config_net_vlan_id, clr_config_net_vlan_id, },
247 { "lxc.net.veth.mode", set_config_net_veth_mode, get_config_net_veth_mode, clr_config_net_veth_mode, },
248 { "lxc.net.veth.pair", set_config_net_veth_pair, get_config_net_veth_pair, clr_config_net_veth_pair, },
249 { "lxc.net.veth.ipv4.route", set_config_net_veth_ipv4_route, get_config_net_veth_ipv4_route, clr_config_net_veth_ipv4_route, },
250 { "lxc.net.veth.ipv6.route", set_config_net_veth_ipv6_route, get_config_net_veth_ipv6_route, clr_config_net_veth_ipv6_route, },
251 { "lxc.net.veth.vlan.id", set_config_net_veth_vlan_id, get_config_net_veth_vlan_id, clr_config_net_veth_vlan_id, },
252 { "lxc.net.veth.vlan.tagged.id", set_config_net_veth_vlan_tagged_id, get_config_net_veth_vlan_tagged_id, clr_config_net_veth_vlan_tagged_id, },
253 { "lxc.net.", set_config_net_nic, get_config_net_nic, clr_config_net_nic, },
254 { "lxc.net", set_config_net, get_config_net, clr_config_net, },
255 { "lxc.no_new_privs", set_config_no_new_privs, get_config_no_new_privs, clr_config_no_new_privs, },
256 { "lxc.prlimit", set_config_prlimit, get_config_prlimit, clr_config_prlimit, },
257 { "lxc.pty.max", set_config_pty_max, get_config_pty_max, clr_config_pty_max, },
258 { "lxc.rootfs.managed", set_config_rootfs_managed, get_config_rootfs_managed, clr_config_rootfs_managed, },
259 { "lxc.rootfs.mount", set_config_rootfs_mount, get_config_rootfs_mount, clr_config_rootfs_mount, },
260 { "lxc.rootfs.options", set_config_rootfs_options, get_config_rootfs_options, clr_config_rootfs_options, },
261 { "lxc.rootfs.path", set_config_rootfs_path, get_config_rootfs_path, clr_config_rootfs_path, },
262 { "lxc.seccomp.allow_nesting", set_config_seccomp_allow_nesting, get_config_seccomp_allow_nesting, clr_config_seccomp_allow_nesting, },
263 { "lxc.seccomp.notify.cookie", set_config_seccomp_notify_cookie, get_config_seccomp_notify_cookie, clr_config_seccomp_notify_cookie, },
264 { "lxc.seccomp.notify.proxy", set_config_seccomp_notify_proxy, get_config_seccomp_notify_proxy, clr_config_seccomp_notify_proxy, },
265 { "lxc.seccomp.profile", set_config_seccomp_profile, get_config_seccomp_profile, clr_config_seccomp_profile, },
266 { "lxc.selinux.context.keyring", set_config_selinux_context_keyring, get_config_selinux_context_keyring, clr_config_selinux_context_keyring },
267 { "lxc.selinux.context", set_config_selinux_context, get_config_selinux_context, clr_config_selinux_context, },
268 { "lxc.signal.halt", set_config_signal_halt, get_config_signal_halt, clr_config_signal_halt, },
269 { "lxc.signal.reboot", set_config_signal_reboot, get_config_signal_reboot, clr_config_signal_reboot, },
270 { "lxc.signal.stop", set_config_signal_stop, get_config_signal_stop, clr_config_signal_stop, },
271 { "lxc.start.auto", set_config_start, get_config_start, clr_config_start, },
272 { "lxc.start.delay", set_config_start, get_config_start, clr_config_start, },
273 { "lxc.start.order", set_config_start, get_config_start, clr_config_start, },
274 { "lxc.tty.dir", set_config_tty_dir, get_config_tty_dir, clr_config_tty_dir, },
275 { "lxc.tty.max", set_config_tty_max, get_config_tty_max, clr_config_tty_max, },
276 { "lxc.uts.name", set_config_uts_name, get_config_uts_name, clr_config_uts_name, },
277 { "lxc.sysctl", set_config_sysctl, get_config_sysctl, clr_config_sysctl, },
278 { "lxc.proc", set_config_proc, get_config_proc, clr_config_proc, },
a84b9932
AV
279};
280
33eb2ec1 281static const size_t config_jump_table_size = sizeof(config_jump_table) / sizeof(struct lxc_config_t);
c2cc9f0a 282
6eb516a7
RJ
283struct lxc_config_t *lxc_get_config_exact(const char *key)
284{
285 size_t i;
286
287 for (i = 0; i < config_jump_table_size; i++)
288 if (!strcmp(config_jump_table[i].name, key))
289 return &config_jump_table[i];
290
291 return NULL;
292}
293
294
300df83e 295struct lxc_config_t *lxc_get_config(const char *key)
c2cc9f0a 296{
84760c11 297 size_t i;
c2cc9f0a 298
33eb2ec1
CB
299 for (i = 0; i < config_jump_table_size; i++)
300 if (!strncmp(config_jump_table[i].name, key, strlen(config_jump_table[i].name)))
301 return &config_jump_table[i];
300df83e 302
c2cc9f0a 303 return NULL;
304}
305
f9373e40
CB
306static int set_config_net(const char *key, const char *value,
307 struct lxc_conf *lxc_conf, void *data)
6b0d5538 308{
663e9916 309 if (!lxc_config_value_empty(value)) {
f9373e40 310 ERROR("lxc.net must not have a value");
6b0d5538
SH
311 return -1;
312 }
313
f9373e40 314 return clr_config_net(key, lxc_conf, data);
6b0d5538
SH
315}
316
f9373e40
CB
317static int set_config_net_type(const char *key, const char *value,
318 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 319{
070a05af 320 struct lxc_netdev *netdev = data;
c2cc9f0a 321
663e9916 322 if (lxc_config_value_empty(value))
f9373e40 323 return clr_config_net_type(key, lxc_conf, data);
c302b476 324
bbc079cf 325 if (!netdev)
059a1ec3 326 return ret_errno(EINVAL);
c2cc9f0a 327
c9f52382 328 if (strcmp(value, "veth") == 0) {
24654103 329 netdev->type = LXC_NET_VETH;
d4a7da46 330 lxc_list_init(&netdev->priv.veth_attr.ipv4_routes);
331 lxc_list_init(&netdev->priv.veth_attr.ipv6_routes);
b8e06d33 332 lxc_list_init(&netdev->priv.veth_attr.vlan_tagged_ids);
756cadb6
CB
333 if (!lxc_veth_flag_to_mode(netdev->priv.veth_attr.mode))
334 lxc_veth_mode_to_flag(&netdev->priv.veth_attr.mode, "bridge");
c9f52382 335 } else if (strcmp(value, "macvlan") == 0) {
24654103 336 netdev->type = LXC_NET_MACVLAN;
756cadb6
CB
337 if (!lxc_macvlan_flag_to_mode(netdev->priv.veth_attr.mode))
338 lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, "private");
c9f52382 339 } else if (strcmp(value, "ipvlan") == 0) {
340 netdev->type = LXC_NET_IPVLAN;
756cadb6
CB
341 if (!lxc_ipvlan_flag_to_mode(netdev->priv.ipvlan_attr.mode))
342 lxc_ipvlan_mode_to_flag(&netdev->priv.ipvlan_attr.mode, "l3");
343 if (!lxc_ipvlan_flag_to_isolation(netdev->priv.ipvlan_attr.isolation))
344 lxc_ipvlan_isolation_to_flag(&netdev->priv.ipvlan_attr.isolation, "bridge");
c9f52382 345 } else if (strcmp(value, "vlan") == 0) {
24654103 346 netdev->type = LXC_NET_VLAN;
c9f52382 347 } else if (strcmp(value, "phys") == 0) {
24654103 348 netdev->type = LXC_NET_PHYS;
c9f52382 349 } else if (strcmp(value, "empty") == 0) {
24654103 350 netdev->type = LXC_NET_EMPTY;
c9f52382 351 } else if (strcmp(value, "none") == 0) {
26b797f3 352 netdev->type = LXC_NET_NONE;
bbc079cf 353 } else {
059a1ec3 354 return log_error(-1, "Invalid network type %s", value);
c2cc9f0a 355 }
bbc079cf 356
c2cc9f0a 357 return 0;
358}
359
f9373e40
CB
360static int set_config_net_flags(const char *key, const char *value,
361 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 362{
070a05af 363 struct lxc_netdev *netdev = data;
c2cc9f0a 364
9d4bf22d 365 if (lxc_config_value_empty(value))
f9373e40 366 return clr_config_net_flags(key, lxc_conf, data);
9d4bf22d 367
33c945e0 368 if (!netdev)
059a1ec3 369 return ret_errno(EINVAL);
c2cc9f0a 370
33c945e0 371 netdev->flags |= IFF_UP;
c2cc9f0a 372
33c945e0
MT
373 return 0;
374}
375
b45e32f9 376static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf,
d5aba460 377 struct lxc_netdev *netdev)
576400e5 378{
c4ef8f4c
CB
379 call_cleaner(netns_freeifaddrs) struct netns_ifaddrs *ifaddr = NULL;
380 struct netns_ifaddrs *ifa;
504a2217
CB
381 int n;
382 int ret = 0;
f9373e40
CB
383 const char *type_key = "lxc.net.type";
384 const char *link_key = "lxc.net.link";
576400e5 385 const char *tmpvalue = "phys";
576400e5 386
c4ef8f4c
CB
387 if (netns_getifaddrs(&ifaddr, -1, &(bool){false}) < 0)
388 return log_error_errno(-1, errno, "Failed to get network interfaces");
576400e5 389
390 for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
391 if (!ifa->ifa_addr)
392 continue;
47903908 393
576400e5 394 if (ifa->ifa_addr->sa_family != AF_PACKET)
395 continue;
396
504a2217 397 if (!strncmp(value, ifa->ifa_name, strlen(value) - 1)) {
f9373e40
CB
398 ret = set_config_net_type(type_key, tmpvalue, lxc_conf,
399 netdev);
576400e5 400 if (!ret) {
f9373e40 401 ret = set_config_net_link(
c302b476 402 link_key, ifa->ifa_name, lxc_conf, netdev);
576400e5 403 if (ret) {
47903908 404 ERROR("Failed to create matched ifnames");
576400e5 405 break;
406 }
407 } else {
47903908 408 ERROR("Failed to create matched ifnames");
576400e5 409 break;
410 }
411 }
412 }
413
576400e5 414 return ret;
415}
416
f9373e40
CB
417static int set_config_net_link(const char *key, const char *value,
418 struct lxc_conf *lxc_conf, void *data)
33c945e0 419{
070a05af 420 struct lxc_netdev *netdev = data;
576400e5 421 int ret = 0;
33c945e0 422
6bed0fb6 423 if (lxc_config_value_empty(value))
f9373e40 424 return clr_config_net_link(key, lxc_conf, data);
6bed0fb6 425
33c945e0 426 if (!netdev)
059a1ec3 427 return ret_errno(EINVAL);
c2cc9f0a 428
b45e32f9
CB
429 if (value[strlen(value) - 1] == '+' && netdev->type == LXC_NET_PHYS)
430 ret = create_matched_ifnames(value, lxc_conf, netdev);
431 else
18cd4b54 432 ret = network_ifname(netdev->link, value, sizeof(netdev->link));
576400e5 433
434 return ret;
c2cc9f0a 435}
436
6509154d 437static int set_config_net_l2proxy(const char *key, const char *value,
438 struct lxc_conf *lxc_conf, void *data)
439{
440 struct lxc_netdev *netdev = data;
441 unsigned int val = 0;
442 int ret;
443
444 if (lxc_config_value_empty(value))
445 return clr_config_net_l2proxy(key, lxc_conf, data);
446
447 if (!netdev)
059a1ec3 448 return ret_errno(EINVAL);
6509154d 449
450 ret = lxc_safe_uint(value, &val);
451 if (ret < 0)
059a1ec3 452 return ret_errno(-ret);
6509154d 453
454 switch (val) {
455 case 0:
456 netdev->l2proxy = false;
457 return 0;
458 case 1:
459 netdev->l2proxy = true;
460 return 0;
461 }
462
21fce08c 463 return ret_set_errno(-1, EINVAL);
6509154d 464}
465
f9373e40
CB
466static int set_config_net_name(const char *key, const char *value,
467 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 468{
070a05af 469 struct lxc_netdev *netdev = data;
c2cc9f0a 470
6bed0fb6 471 if (lxc_config_value_empty(value))
f9373e40 472 return clr_config_net_name(key, lxc_conf, data);
6bed0fb6 473
33c945e0 474 if (!netdev)
059a1ec3 475 return ret_errno(EINVAL);
c2cc9f0a 476
18cd4b54 477 return network_ifname(netdev->name, value, sizeof(netdev->name));
33c945e0
MT
478}
479
3f0ed090
TP
480
481static int set_config_net_veth_mode(const char *key, const char *value,
482 struct lxc_conf *lxc_conf, void *data)
483{
484 struct lxc_netdev *netdev = data;
485
486 if (lxc_config_value_empty(value))
487 return clr_config_net_veth_mode(key, lxc_conf, data);
488
489 if (!netdev)
059a1ec3 490 return ret_errno(EINVAL);
3f0ed090
TP
491
492 return lxc_veth_mode_to_flag(&netdev->priv.veth_attr.mode, value);
493}
494
f9373e40
CB
495static int set_config_net_veth_pair(const char *key, const char *value,
496 struct lxc_conf *lxc_conf, void *data)
e892973e 497{
070a05af 498 struct lxc_netdev *netdev = data;
e892973e 499
6bed0fb6 500 if (lxc_config_value_empty(value))
f9373e40 501 return clr_config_net_veth_pair(key, lxc_conf, data);
6bed0fb6 502
e892973e 503 if (!netdev)
059a1ec3 504 return ret_errno(EINVAL);
e892973e 505
059a1ec3
CB
506 return network_ifname(netdev->priv.veth_attr.pair, value,
507 sizeof(netdev->priv.veth_attr.pair));
e892973e
DL
508}
509
fdf76c6d
TP
510static int set_config_net_veth_vlan_id(const char *key, const char *value,
511 struct lxc_conf *lxc_conf, void *data)
512{
513 int ret;
514 struct lxc_netdev *netdev = data;
515
516 if (!netdev)
517 return ret_errno(EINVAL);
518
519 if (lxc_config_value_empty(value))
520 return clr_config_net_veth_vlan_id(key, lxc_conf, data);
521
522 if (strcmp(value, "none") == 0) {
523 netdev->priv.veth_attr.vlan_id = BRIDGE_VLAN_NONE;
524 } else {
525 unsigned short vlan_id;
526 ret = get_u16(&vlan_id, value, 0);
527 if (ret < 0)
528 return ret_errno(EINVAL);
529
530 if (vlan_id > BRIDGE_VLAN_ID_MAX)
531 return ret_errno(EINVAL);
532
533 netdev->priv.veth_attr.vlan_id = vlan_id;
534 }
535
536 netdev->priv.veth_attr.vlan_id_set = true;
537 return 0;
538}
539
b8e06d33
TP
540static int set_config_net_veth_vlan_tagged_id(const char *key, const char *value,
541 struct lxc_conf *lxc_conf, void *data)
542{
543 __do_free struct lxc_list *list = NULL;
544 int ret;
545 unsigned short vlan_id;
546 struct lxc_netdev *netdev = data;
547
548 if (!netdev)
549 return ret_errno(EINVAL);
550
551 if (lxc_config_value_empty(value))
552 return clr_config_net_veth_vlan_tagged_id(key, lxc_conf, data);
553
554 ret = get_u16(&vlan_id, value, 0);
555 if (ret < 0)
556 ret_errno(EINVAL);
557
558 if (vlan_id > BRIDGE_VLAN_ID_MAX)
559 ret_errno(EINVAL);
560
561 list = malloc(sizeof(*list));
562 if (!list)
563 return ret_errno(ENOMEM);
564
565 lxc_list_init(list);
566 list->elem = UINT_TO_PTR(vlan_id);
567
568 lxc_list_add_tail(&netdev->priv.veth_attr.vlan_tagged_ids, move_ptr(list));
569
570 return 0;
571}
572
f9373e40
CB
573static int set_config_net_macvlan_mode(const char *key, const char *value,
574 struct lxc_conf *lxc_conf, void *data)
8634bc19 575{
070a05af 576 struct lxc_netdev *netdev = data;
8634bc19 577
6bed0fb6 578 if (lxc_config_value_empty(value))
f9373e40 579 return clr_config_net_macvlan_mode(key, lxc_conf, data);
6bed0fb6 580
8634bc19 581 if (!netdev)
059a1ec3 582 return ret_errno(EINVAL);
8634bc19 583
9b0df30f 584 return lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, value);
8634bc19
MT
585}
586
c9f52382 587static int set_config_net_ipvlan_mode(const char *key, const char *value,
588 struct lxc_conf *lxc_conf, void *data)
589{
590 struct lxc_netdev *netdev = data;
591
592 if (lxc_config_value_empty(value))
593 return clr_config_net_ipvlan_mode(key, lxc_conf, data);
594
595 if (!netdev)
059a1ec3 596 return ret_errno(EINVAL);
c9f52382 597
059a1ec3
CB
598 if (netdev->type != LXC_NET_IPVLAN)
599 return log_error_errno(-EINVAL,
600 EINVAL, "Invalid ipvlan mode \"%s\", can only be used with ipvlan network",
601 value);
c9f52382 602
603 return lxc_ipvlan_mode_to_flag(&netdev->priv.ipvlan_attr.mode, value);
604}
605
606static int set_config_net_ipvlan_isolation(const char *key, const char *value,
607 struct lxc_conf *lxc_conf, void *data)
608{
609 struct lxc_netdev *netdev = data;
610
611 if (lxc_config_value_empty(value))
612 return clr_config_net_ipvlan_isolation(key, lxc_conf, data);
613
614 if (!netdev)
059a1ec3 615 return ret_errno(EINVAL);
c9f52382 616
059a1ec3
CB
617 if (netdev->type != LXC_NET_IPVLAN)
618 return log_error_errno(-EINVAL,
619 EINVAL, "Invalid ipvlan isolation \"%s\", can only be used with ipvlan network",
620 value);
c9f52382 621
622 return lxc_ipvlan_isolation_to_flag(&netdev->priv.ipvlan_attr.isolation, value);
623}
624
f9373e40
CB
625static int set_config_net_hwaddr(const char *key, const char *value,
626 struct lxc_conf *lxc_conf, void *data)
33c945e0 627{
070a05af 628 struct lxc_netdev *netdev = data;
504a2217 629 char *new_value;
33c945e0 630
6bed0fb6 631 if (lxc_config_value_empty(value))
f9373e40 632 return clr_config_net_hwaddr(key, lxc_conf, data);
6bed0fb6 633
ecbb3790 634 if (!netdev)
059a1ec3 635 return ret_errno(EINVAL);
ecbb3790 636
504a2217 637 new_value = strdup(value);
d5aba460 638 if (!new_value)
059a1ec3 639 return ret_errno(ENOMEM);
d5aba460 640
508c263e 641 rand_complete_hwaddr(new_value);
c2cc9f0a 642
663e9916 643 if (lxc_config_value_empty(new_value)) {
508c263e
SH
644 free(new_value);
645 netdev->hwaddr = NULL;
646 return 0;
647 }
648
649 netdev->hwaddr = new_value;
47903908 650
508c263e 651 return 0;
c2cc9f0a 652}
653
f9373e40
CB
654static int set_config_net_vlan_id(const char *key, const char *value,
655 struct lxc_conf *lxc_conf, void *data)
26c39028 656{
d5aba460 657 int ret;
070a05af 658 struct lxc_netdev *netdev = data;
26c39028 659
6bed0fb6 660 if (lxc_config_value_empty(value))
f9373e40 661 return clr_config_net_vlan_id(key, lxc_conf, data);
6bed0fb6 662
26c39028 663 if (!netdev)
059a1ec3 664 return ret_errno(EINVAL);
26c39028 665
d5aba460
CB
666 ret = get_u16(&netdev->priv.vlan_attr.vid, value, 0);
667 if (ret < 0)
059a1ec3 668 return ret;
26c39028
JHS
669
670 return 0;
671}
672
f9373e40
CB
673static int set_config_net_mtu(const char *key, const char *value,
674 struct lxc_conf *lxc_conf, void *data)
442cbbe6 675{
070a05af 676 struct lxc_netdev *netdev = data;
442cbbe6 677
6bed0fb6 678 if (lxc_config_value_empty(value))
f9373e40 679 return clr_config_net_mtu(key, lxc_conf, data);
6bed0fb6 680
33c945e0 681 if (!netdev)
059a1ec3 682 return ret_errno(EINVAL);
442cbbe6 683
713046e3 684 return set_config_string_item(&netdev->mtu, value);
442cbbe6
TR
685}
686
9ff60df2
CB
687static int set_config_net_ipv4_address(const char *key, const char *value,
688 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 689{
059a1ec3
CB
690 __do_free char *addr = NULL;
691 __do_free struct lxc_inetdev *inetdev = NULL;
692 __do_free struct lxc_list *list = NULL;
d5aba460 693 int ret;
070a05af 694 struct lxc_netdev *netdev = data;
504a2217 695 char *cursor, *slash;
059a1ec3 696 char *bcast = NULL, *prefix = NULL;
c2cc9f0a 697
663e9916 698 if (lxc_config_value_empty(value))
9ff60df2 699 return clr_config_net_ipv4_address(key, lxc_conf, data);
0797e123 700
33c945e0 701 if (!netdev)
059a1ec3 702 return ret_errno(EINVAL);
c2cc9f0a 703
704 inetdev = malloc(sizeof(*inetdev));
d5aba460 705 if (!inetdev)
059a1ec3 706 return ret_errno(ENOMEM);
c2cc9f0a 707 memset(inetdev, 0, sizeof(*inetdev));
708
709 list = malloc(sizeof(*list));
059a1ec3
CB
710 if (!list)
711 return ret_errno(ENOMEM);
c2cc9f0a 712
713 lxc_list_init(list);
c2cc9f0a 714
956edc54 715 addr = strdup(value);
059a1ec3
CB
716 if (!addr)
717 return ret_errno(ENOMEM);
c2cc9f0a 718
719 cursor = strstr(addr, " ");
720 if (cursor) {
721 *cursor = '\0';
722 bcast = cursor + 1;
723 }
724
725 slash = strstr(addr, "/");
726 if (slash) {
727 *slash = '\0';
728 prefix = slash + 1;
729 }
730
d5aba460 731 ret = inet_pton(AF_INET, addr, &inetdev->addr);
059a1ec3
CB
732 if (!ret || ret < 0)
733 return log_error_errno(-1, errno, "Invalid ipv4 address \"%s\"", value);
c2cc9f0a 734
d5aba460
CB
735 if (bcast) {
736 ret = inet_pton(AF_INET, bcast, &inetdev->bcast);
059a1ec3
CB
737 if (!ret || ret < 0)
738 return log_error_errno(-1, errno, "Invalid ipv4 broadcast address \"%s\"", value);
d5aba460 739
0093bb8c 740 }
c2cc9f0a 741
504a2217 742 /* No prefix specified, determine it from the network class. */
1c633398 743 if (prefix) {
d5aba460 744 ret = lxc_safe_uint(prefix, &inetdev->prefix);
059a1ec3
CB
745 if (ret < 0)
746 return ret;
1c633398
CB
747 } else {
748 inetdev->prefix = config_ip_prefix(&inetdev->addr);
749 }
a059591e 750
504a2217
CB
751 /* If no broadcast address, let compute one from the
752 * prefix and address.
0093bb8c
DL
753 */
754 if (!bcast) {
1b7d4743 755 inetdev->bcast.s_addr = inetdev->addr.s_addr;
d5aba460 756 inetdev->bcast.s_addr |= htonl(INADDR_BROADCAST >> inetdev->prefix);
0093bb8c 757 }
c2cc9f0a 758
059a1ec3 759 list->elem = inetdev;
8538f388 760 lxc_list_add_tail(&netdev->ipv4, list);
059a1ec3
CB
761 move_ptr(inetdev);
762 move_ptr(list);
47903908 763
c2cc9f0a 764 return 0;
765}
766
f9373e40
CB
767static int set_config_net_ipv4_gateway(const char *key, const char *value,
768 struct lxc_conf *lxc_conf, void *data)
f8fee0e2 769{
070a05af 770 struct lxc_netdev *netdev = data;
f8fee0e2 771
6bed0fb6 772 if (lxc_config_value_empty(value))
f9373e40 773 return clr_config_net_ipv4_gateway(key, lxc_conf, data);
6bed0fb6 774
f8fee0e2
MK
775 if (!netdev)
776 return -1;
777
e088e926 778 free(netdev->ipv4_gateway);
f8fee0e2 779
a2f9a670 780 if (strcmp(value, "auto") == 0) {
19a26f82
MK
781 netdev->ipv4_gateway = NULL;
782 netdev->ipv4_gateway_auto = true;
a2f9a670 783 } else if (strcmp(value, "dev") == 0) {
784 netdev->ipv4_gateway = NULL;
785 netdev->ipv4_gateway_auto = false;
786 netdev->ipv4_gateway_dev = true;
19a26f82 787 } else {
059a1ec3 788 __do_free struct in_addr *gw = NULL;
25a908b8 789 int ret;
e088e926
SG
790
791 gw = malloc(sizeof(*gw));
25a908b8 792 if (!gw)
059a1ec3 793 return ret_errno(ENOMEM);
e088e926 794
25a908b8 795 ret = inet_pton(AF_INET, value, gw);
059a1ec3
CB
796 if (!ret || ret < 0)
797 return log_error_errno(-1, errno, "Invalid ipv4 gateway address \"%s\"", value);
19a26f82 798
059a1ec3 799 netdev->ipv4_gateway = move_ptr(gw);
19a26f82 800 netdev->ipv4_gateway_auto = false;
f8fee0e2
MK
801 }
802
f8fee0e2
MK
803 return 0;
804}
805
d4a7da46 806static int set_config_net_veth_ipv4_route(const char *key, const char *value,
807 struct lxc_conf *lxc_conf, void *data)
808{
809 __do_free char *valdup = NULL;
810 __do_free struct lxc_inetdev *inetdev = NULL;
811 __do_free struct lxc_list *list = NULL;
812 int ret;
813 char *netmask, *slash;
814 struct lxc_netdev *netdev = data;
815
816 if (lxc_config_value_empty(value))
817 return clr_config_net_veth_ipv4_route(key, lxc_conf, data);
818
819 if (!netdev)
059a1ec3 820 return ret_errno(EINVAL);
d4a7da46 821
059a1ec3
CB
822 if (netdev->type != LXC_NET_VETH)
823 return log_error_errno(-EINVAL,
824 EINVAL, "Invalid ipv4 route \"%s\", can only be used with veth network",
825 value);
d4a7da46 826
827 inetdev = malloc(sizeof(*inetdev));
828 if (!inetdev)
059a1ec3 829 return ret_errno(ENOMEM);
d4a7da46 830 memset(inetdev, 0, sizeof(*inetdev));
831
832 list = malloc(sizeof(*list));
833 if (!list)
059a1ec3 834 return ret_errno(ENOMEM);
d4a7da46 835
836 lxc_list_init(list);
837 list->elem = inetdev;
838
839 valdup = strdup(value);
840 if (!valdup)
059a1ec3 841 return ret_errno(ENOMEM);
d4a7da46 842
843 slash = strchr(valdup, '/');
844 if (!slash)
059a1ec3 845 return ret_errno(EINVAL);
d4a7da46 846
847 *slash = '\0';
848 slash++;
849 if (*slash == '\0')
059a1ec3 850 return ret_errno(EINVAL);
d4a7da46 851
852 netmask = slash;
853
854 ret = lxc_safe_uint(netmask, &inetdev->prefix);
855 if (ret < 0 || inetdev->prefix > 32)
059a1ec3 856 return ret_errno(EINVAL);
d4a7da46 857
858 ret = inet_pton(AF_INET, valdup, &inetdev->addr);
859 if (!ret || ret < 0)
059a1ec3 860 return ret_errno(EINVAL);
d4a7da46 861
862 lxc_list_add_tail(&netdev->priv.veth_attr.ipv4_routes, list);
863 move_ptr(inetdev);
864 move_ptr(list);
865
866 return 0;
867}
868
2e44ae28
CB
869static int set_config_net_ipv6_address(const char *key, const char *value,
870 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 871{
059a1ec3
CB
872 __do_free char *valdup = NULL;
873 __do_free struct lxc_inet6dev *inet6dev = NULL;
874 __do_free struct lxc_list *list = NULL;
25a908b8 875 int ret;
070a05af 876 struct lxc_netdev *netdev = data;
059a1ec3 877 char *slash, *netmask;
c2cc9f0a 878
6bed0fb6 879 if (lxc_config_value_empty(value))
2e44ae28 880 return clr_config_net_ipv6_address(key, lxc_conf, data);
6bed0fb6 881
33c945e0 882 if (!netdev)
059a1ec3 883 return ret_errno(EINVAL);
c2cc9f0a 884
885 inet6dev = malloc(sizeof(*inet6dev));
25a908b8 886 if (!inet6dev)
059a1ec3 887 return ret_errno(ENOMEM);
c2cc9f0a 888 memset(inet6dev, 0, sizeof(*inet6dev));
889
890 list = malloc(sizeof(*list));
059a1ec3
CB
891 if (!list)
892 return ret_errno(ENOMEM);
c2cc9f0a 893
894 lxc_list_init(list);
c2cc9f0a 895
956edc54 896 valdup = strdup(value);
059a1ec3
CB
897 if (!valdup)
898 return ret_errno(ENOMEM);
956edc54 899
a059591e 900 inet6dev->prefix = 64;
12a50cc6 901 slash = strstr(valdup, "/");
c2cc9f0a 902 if (slash) {
903 *slash = '\0';
904 netmask = slash + 1;
47903908 905
f54f8d0b 906 ret = lxc_safe_uint(netmask, &inet6dev->prefix);
059a1ec3
CB
907 if (ret < 0)
908 return ret;
c2cc9f0a 909 }
910
25a908b8 911 ret = inet_pton(AF_INET6, valdup, &inet6dev->addr);
059a1ec3
CB
912 if (!ret || ret < 0)
913 return log_error_errno(-EINVAL, EINVAL, "Invalid ipv6 address \"%s\"", valdup);
c2cc9f0a 914
059a1ec3 915 list->elem = inet6dev;
8538f388 916 lxc_list_add_tail(&netdev->ipv6, list);
059a1ec3
CB
917 move_ptr(inet6dev);
918 move_ptr(list);
47903908 919
c2cc9f0a 920 return 0;
921}
922
f9373e40
CB
923static int set_config_net_ipv6_gateway(const char *key, const char *value,
924 struct lxc_conf *lxc_conf, void *data)
f8fee0e2 925{
070a05af 926 struct lxc_netdev *netdev = data;
f8fee0e2 927
6bed0fb6 928 if (lxc_config_value_empty(value))
f9373e40 929 return clr_config_net_ipv6_gateway(key, lxc_conf, data);
6bed0fb6 930
f8fee0e2 931 if (!netdev)
059a1ec3 932 return ret_errno(EINVAL);
f8fee0e2 933
e088e926 934 free(netdev->ipv6_gateway);
f8fee0e2 935
a2f9a670 936 if (strcmp(value, "auto") == 0) {
19a26f82
MK
937 netdev->ipv6_gateway = NULL;
938 netdev->ipv6_gateway_auto = true;
a2f9a670 939 } else if (strcmp(value, "dev") == 0) {
940 netdev->ipv6_gateway = NULL;
941 netdev->ipv6_gateway_auto = false;
942 netdev->ipv6_gateway_dev = true;
19a26f82 943 } else {
25a908b8 944 int ret;
059a1ec3 945 __do_free struct in6_addr *gw = NULL;
8fb86a37 946
bec695f3 947 gw = malloc(sizeof(*gw));
25a908b8 948 if (!gw)
059a1ec3 949 return ret_errno(ENOMEM);
bec695f3 950
25a908b8 951 ret = inet_pton(AF_INET6, value, gw);
059a1ec3
CB
952 if (!ret || ret < 0)
953 return log_error_errno(-EINVAL, EINVAL,
954 "Invalid ipv6 gateway address \"%s\"", value);
19a26f82 955
059a1ec3 956 netdev->ipv6_gateway = move_ptr(gw);
19a26f82 957 netdev->ipv6_gateway_auto = false;
f8fee0e2
MK
958 }
959
f8fee0e2
MK
960 return 0;
961}
962
d4a7da46 963static int set_config_net_veth_ipv6_route(const char *key, const char *value,
964 struct lxc_conf *lxc_conf, void *data)
965{
6453ba56
CB
966 __do_free char *valdup = NULL;
967 __do_free struct lxc_inet6dev *inet6dev = NULL;
968 __do_free struct lxc_list *list = NULL;
d4a7da46 969 int ret;
970 char *netmask, *slash;
971 struct lxc_netdev *netdev = data;
972
973 if (lxc_config_value_empty(value))
974 return clr_config_net_veth_ipv6_route(key, lxc_conf, data);
975
976 if (!netdev)
059a1ec3 977 return ret_errno(EINVAL);
d4a7da46 978
059a1ec3
CB
979 if (netdev->type != LXC_NET_VETH)
980 return log_error_errno(-EINVAL,
981 EINVAL, "Invalid ipv6 route \"%s\", can only be used with veth network",
982 value);
d4a7da46 983
984 inet6dev = malloc(sizeof(*inet6dev));
985 if (!inet6dev)
059a1ec3 986 return ret_errno(ENOMEM);
d4a7da46 987 memset(inet6dev, 0, sizeof(*inet6dev));
988
989 list = malloc(sizeof(*list));
990 if (!list)
059a1ec3 991 return ret_errno(ENOMEM);
d4a7da46 992
993 lxc_list_init(list);
d4a7da46 994
995 valdup = strdup(value);
996 if (!valdup)
997 return -1;
998
999 slash = strchr(valdup, '/');
1000 if (!slash)
059a1ec3 1001 return ret_errno(EINVAL);
d4a7da46 1002
1003 *slash = '\0';
1004 slash++;
1005 if (*slash == '\0')
059a1ec3 1006 return ret_errno(EINVAL);
d4a7da46 1007
1008 netmask = slash;
1009
1010 ret = lxc_safe_uint(netmask, &inet6dev->prefix);
1011 if (ret < 0 || inet6dev->prefix > 128)
059a1ec3 1012 return ret_errno(EINVAL);
d4a7da46 1013
1014 ret = inet_pton(AF_INET6, valdup, &inet6dev->addr);
1015 if (!ret || ret < 0)
059a1ec3 1016 return ret_errno(EINVAL);
d4a7da46 1017
059a1ec3 1018 list->elem = inet6dev;
d4a7da46 1019 lxc_list_add_tail(&netdev->priv.veth_attr.ipv6_routes, list);
1020 move_ptr(inet6dev);
1021 move_ptr(list);
1022
1023 return 0;
1024}
1025
f9373e40
CB
1026static int set_config_net_script_up(const char *key, const char *value,
1027 struct lxc_conf *lxc_conf, void *data)
e3b4c4c4 1028{
070a05af 1029 struct lxc_netdev *netdev = data;
e3b4c4c4 1030
6bed0fb6 1031 if (lxc_config_value_empty(value))
f9373e40 1032 return clr_config_net_script_up(key, lxc_conf, data);
6bed0fb6 1033
e3b4c4c4 1034 if (!netdev)
059a1ec3 1035 return ret_errno(EINVAL);
e3b4c4c4 1036
713046e3 1037 return set_config_string_item(&netdev->upscript, value);
8fc8295a
DE
1038}
1039
f9373e40
CB
1040static int set_config_net_script_down(const char *key, const char *value,
1041 struct lxc_conf *lxc_conf, void *data)
8fc8295a 1042{
070a05af 1043 struct lxc_netdev *netdev = data;
8fc8295a 1044
6bed0fb6 1045 if (lxc_config_value_empty(value))
f9373e40 1046 return clr_config_net_script_down(key, lxc_conf, data);
6bed0fb6 1047
8fc8295a 1048 if (!netdev)
059a1ec3 1049 return ret_errno(EINVAL);
8fc8295a 1050
713046e3 1051 return set_config_string_item(&netdev->downscript, value);
e3b4c4c4
ST
1052}
1053
059a1ec3 1054static int add_hook(struct lxc_conf *lxc_conf, int which, __owns char *hook)
26ddeedd 1055{
059a1ec3 1056 __do_free char *val = hook;
26ddeedd
SH
1057 struct lxc_list *hooklist;
1058
1059 hooklist = malloc(sizeof(*hooklist));
059a1ec3
CB
1060 if (!hooklist)
1061 return ret_errno(ENOMEM);
504a2217 1062
059a1ec3 1063 hooklist->elem = move_ptr(val);
26ddeedd 1064 lxc_list_add_tail(&lxc_conf->hooks[which], hooklist);
47903908 1065
26ddeedd
SH
1066 return 0;
1067}
1068
50d86993
CB
1069static int set_config_seccomp_allow_nesting(const char *key, const char *value,
1070 struct lxc_conf *lxc_conf, void *data)
1071{
c3e3c21a 1072#ifdef HAVE_SECCOMP
50d86993
CB
1073 if (lxc_config_value_empty(value))
1074 return clr_config_seccomp_allow_nesting(key, lxc_conf, NULL);
1075
c3e3c21a 1076 if (lxc_safe_uint(value, &lxc_conf->seccomp.allow_nesting) < 0)
50d86993
CB
1077 return -1;
1078
c3e3c21a 1079 if (lxc_conf->seccomp.allow_nesting > 1)
21fce08c 1080 return ret_set_errno(-1, EINVAL);
86ce1da1
CB
1081
1082 return 0;
c3e3c21a
CB
1083#else
1084 errno = ENOSYS;
1085 return -1;
1086#endif
86ce1da1
CB
1087}
1088
84cf6d25
WB
1089static int set_config_seccomp_notify_cookie(const char *key, const char *value,
1090 struct lxc_conf *lxc_conf, void *data)
1091{
1092#ifdef HAVE_SECCOMP_NOTIFY
1093 return set_config_string_item(&lxc_conf->seccomp.notifier.cookie, value);
1094#else
21fce08c 1095 return ret_set_errno(-1, ENOSYS);
84cf6d25
WB
1096#endif
1097}
1098
86ce1da1
CB
1099static int set_config_seccomp_notify_proxy(const char *key, const char *value,
1100 struct lxc_conf *lxc_conf, void *data)
1101{
c3e3c21a 1102#ifdef HAVE_SECCOMP_NOTIFY
86ce1da1
CB
1103 const char *offset;
1104
1105 if (lxc_config_value_empty(value))
1106 return clr_config_seccomp_notify_proxy(key, lxc_conf, NULL);
1107
1108 if (strncmp(value, "unix:", 5) != 0)
21fce08c 1109 return ret_set_errno(-1, EINVAL);
86ce1da1
CB
1110
1111 offset = value + 5;
c3e3c21a 1112 if (lxc_unix_sockaddr(&lxc_conf->seccomp.notifier.proxy_addr, offset) < 0)
50d86993
CB
1113 return -1;
1114
1115 return 0;
86ce1da1 1116#else
21fce08c 1117 return ret_set_errno(-1, ENOSYS);
86ce1da1 1118#endif
50d86993
CB
1119}
1120
0b427da0
CB
1121static int set_config_seccomp_profile(const char *key, const char *value,
1122 struct lxc_conf *lxc_conf, void *data)
8f2c3a70 1123{
c3e3c21a 1124 return set_config_path_item(&lxc_conf->seccomp.seccomp, value);
8f2c3a70
SH
1125}
1126
5cda27c1
SH
1127static int set_config_execute_cmd(const char *key, const char *value,
1128 struct lxc_conf *lxc_conf, void *data)
1129{
1130 return set_config_path_item(&lxc_conf->execute_cmd, value);
1131}
1132
713046e3 1133static int set_config_init_cmd(const char *key, const char *value,
c7e27aaf 1134 struct lxc_conf *lxc_conf, void *data)
67c660d0 1135{
713046e3 1136 return set_config_path_item(&lxc_conf->init_cmd, value);
67c660d0
SG
1137}
1138
3c491553
L
1139static int set_config_init_cwd(const char *key, const char *value,
1140 struct lxc_conf *lxc_conf, void *data)
1141{
1142 return set_config_path_item(&lxc_conf->init_cwd, value);
1143}
1144
713046e3 1145static int set_config_init_uid(const char *key, const char *value,
c7e27aaf 1146 struct lxc_conf *lxc_conf, void *data)
72bb04e4 1147{
d1e5d636
CB
1148 unsigned int init_uid;
1149
663e9916 1150 if (lxc_config_value_empty(value)) {
2e7cde40 1151 lxc_conf->init_uid = 0;
fee80911 1152 return 0;
2e7cde40 1153 }
fee80911 1154
d1e5d636
CB
1155 if (lxc_safe_uint(value, &init_uid) < 0)
1156 return -1;
25a908b8 1157
d1e5d636
CB
1158 lxc_conf->init_uid = init_uid;
1159
72bb04e4
PT
1160 return 0;
1161}
1162
713046e3 1163static int set_config_init_gid(const char *key, const char *value,
c7e27aaf 1164 struct lxc_conf *lxc_conf, void *data)
72bb04e4 1165{
d1e5d636
CB
1166 unsigned int init_gid;
1167
663e9916 1168 if (lxc_config_value_empty(value)) {
2debb6e6 1169 lxc_conf->init_gid = 0;
a757cc7d 1170 return 0;
2debb6e6 1171 }
a757cc7d 1172
d1e5d636
CB
1173 if (lxc_safe_uint(value, &init_gid) < 0)
1174 return -1;
25a908b8 1175
d1e5d636
CB
1176 lxc_conf->init_gid = init_gid;
1177
72bb04e4
PT
1178 return 0;
1179}
1180
466c2e93 1181static int set_config_hooks(const char *key, const char *value,
c7e27aaf 1182 struct lxc_conf *lxc_conf, void *data)
26ddeedd 1183{
059a1ec3 1184 __do_free char *copy = NULL;
72bb04e4 1185
663e9916 1186 if (lxc_config_value_empty(value))
7d0eb87e
SH
1187 return lxc_clear_hooks(lxc_conf, key);
1188
ed1454e8
CB
1189 if (strcmp(key + 4, "hook") == 0)
1190 return log_error_errno(-EINVAL, EINVAL, "lxc.hook must not have a value");
25a908b8 1191
7d0eb87e 1192 copy = strdup(value);
25a908b8 1193 if (!copy)
059a1ec3 1194 return ret_errno(ENOMEM);
504a2217 1195
a182feae 1196 if (strcmp(key + 9, "pre-start") == 0)
059a1ec3 1197 return add_hook(lxc_conf, LXCHOOK_PRESTART, move_ptr(copy));
08dd2805 1198 else if (strcmp(key + 9, "start-host") == 0)
059a1ec3 1199 return add_hook(lxc_conf, LXCHOOK_START_HOST, move_ptr(copy));
a182feae 1200 else if (strcmp(key + 9, "pre-mount") == 0)
059a1ec3 1201 return add_hook(lxc_conf, LXCHOOK_PREMOUNT, move_ptr(copy));
a182feae 1202 else if (strcmp(key + 9, "autodev") == 0)
059a1ec3 1203 return add_hook(lxc_conf, LXCHOOK_AUTODEV, move_ptr(copy));
a182feae 1204 else if (strcmp(key + 9, "mount") == 0)
059a1ec3 1205 return add_hook(lxc_conf, LXCHOOK_MOUNT, move_ptr(copy));
a182feae 1206 else if (strcmp(key + 9, "start") == 0)
059a1ec3 1207 return add_hook(lxc_conf, LXCHOOK_START, move_ptr(copy));
a182feae 1208 else if (strcmp(key + 9, "stop") == 0)
059a1ec3 1209 return add_hook(lxc_conf, LXCHOOK_STOP, move_ptr(copy));
a182feae 1210 else if (strcmp(key + 9, "post-stop") == 0)
059a1ec3 1211 return add_hook(lxc_conf, LXCHOOK_POSTSTOP, move_ptr(copy));
a182feae 1212 else if (strcmp(key + 9, "clone") == 0)
059a1ec3 1213 return add_hook(lxc_conf, LXCHOOK_CLONE, move_ptr(copy));
a182feae 1214 else if (strcmp(key + 9, "destroy") == 0)
059a1ec3 1215 return add_hook(lxc_conf, LXCHOOK_DESTROY, move_ptr(copy));
47903908 1216
26ddeedd
SH
1217 return -1;
1218}
1219
44ae0fb6
CB
1220static int set_config_hooks_version(const char *key, const char *value,
1221 struct lxc_conf *lxc_conf, void *data)
1222{
1223 int ret;
1224 unsigned int tmp;
1225
1226 if (lxc_config_value_empty(value))
1227 return clr_config_hooks_version(key, lxc_conf, NULL);
1228
1229 ret = lxc_safe_uint(value, &tmp);
1230 if (ret < 0)
1231 return -1;
1232
ed1454e8
CB
1233 if (tmp > 1)
1234 return log_error_errno(-EINVAL,
1235 EINVAL, "Invalid hook version specified. Currently only 0 (legacy) and 1 are supported");
44ae0fb6
CB
1236
1237 lxc_conf->hooks_version = tmp;
47903908 1238
44ae0fb6
CB
1239 return 0;
1240}
1241
713046e3 1242static int set_config_personality(const char *key, const char *value,
c7e27aaf 1243 struct lxc_conf *lxc_conf, void *data)
cccc74b5 1244{
62af653c 1245 signed long personality;
cccc74b5 1246
62af653c 1247 personality = lxc_config_parse_arch(value);
525f0002
CS
1248 if (personality >= 0)
1249 lxc_conf->personality = personality;
1250 else
25a908b8 1251 WARN("Unsupported personality \"%s\"", value);
970ab589
DL
1252
1253 return 0;
cccc74b5
DL
1254}
1255
232763d6
CB
1256static int set_config_pty_max(const char *key, const char *value,
1257 struct lxc_conf *lxc_conf, void *data)
10db618d 1258{
e528c735
CB
1259 int ret;
1260 unsigned int max = 0;
1261
663e9916 1262 if (lxc_config_value_empty(value)) {
e528c735 1263 lxc_conf->pty_max = 0;
884a4580 1264 return 0;
ec200ce9 1265 }
884a4580 1266
e528c735
CB
1267 ret = lxc_safe_uint(value, &max);
1268 if (ret < 0)
49aabd9d 1269 return ret_errno(EINVAL);
10db618d 1270
e528c735 1271 lxc_conf->pty_max = max;
47903908 1272
10db618d 1273 return 0;
1274}
1275
a182feae
CB
1276/* We only need to check whether the first byte of the key after the lxc.start.
1277 * prefix matches our expectations since they fortunately all start with a
1278 * different letter. If anything was wrong with the key we would have already
1279 * noticed when the callback was called.
1280 */
713046e3 1281static int set_config_start(const char *key, const char *value,
c7e27aaf 1282 struct lxc_conf *lxc_conf, void *data)
ee1e7aa0 1283{
572f6a14 1284 int ret;
ebb80f95
CB
1285 bool is_empty;
1286
663e9916 1287 is_empty = lxc_config_value_empty(value);
ebb80f95 1288
a182feae 1289 if (*(key + 10) == 'a') { /* lxc.start.auto */
ebb80f95
CB
1290 if (is_empty) {
1291 lxc_conf->start_auto = 0;
1292 return 0;
1293 }
61ff8fc8 1294
572f6a14
CB
1295 ret = lxc_safe_uint(value, &lxc_conf->start_auto);
1296 if (ret)
1297 return ret;
ebb80f95 1298
3590152f 1299 if (lxc_conf->start_auto > 1)
572f6a14 1300 return ret_errno(EINVAL);
ebb80f95 1301
ee1e7aa0 1302 return 0;
a182feae 1303 } else if (*(key + 10) == 'd') { /* lxc.start.delay */
ebb80f95
CB
1304 if (is_empty) {
1305 lxc_conf->start_delay = 0;
1306 return 0;
1307 }
1308
ebb80f95 1309 return lxc_safe_uint(value, &lxc_conf->start_delay);
a182feae 1310 } else if (*(key + 10) == 'o') { /* lxc.start.order */
ebb80f95
CB
1311 if (is_empty) {
1312 lxc_conf->start_order = 0;
1313 return 0;
1314 }
1315
ebb80f95 1316 return lxc_safe_int(value, &lxc_conf->start_order);
ee1e7aa0 1317 }
ebb80f95 1318
572f6a14 1319 return ret_errno(EINVAL);
ee1e7aa0
SG
1320}
1321
713046e3 1322static int set_config_monitor(const char *key, const char *value,
c7e27aaf 1323 struct lxc_conf *lxc_conf, void *data)
a8dfe4e0 1324{
663e9916 1325 if (lxc_config_value_empty(value)) {
4ad9cd26 1326 lxc_conf->monitor_unshare = 0;
a8dfe4e0
WB
1327 return 0;
1328 }
4ad9cd26 1329
a182feae 1330 if (strcmp(key + 12, "unshare") == 0)
4ad9cd26
CB
1331 return lxc_safe_uint(value, &lxc_conf->monitor_unshare);
1332
e1665973 1333 return ret_errno(EINVAL);
a8dfe4e0
WB
1334}
1335
258f8051
CB
1336static int set_config_monitor_signal_pdeath(const char *key, const char *value,
1337 struct lxc_conf *lxc_conf, void *data)
1338{
1339 if (lxc_config_value_empty(value)) {
1340 lxc_conf->monitor_signal_pdeath = 0;
1341 return 0;
1342 }
1343
1344 if (strcmp(key + 12, "signal.pdeath") == 0) {
1345 int sig_n;
1346
1347 sig_n = sig_parse(value);
1348 if (sig_n < 0)
cb5f3df2 1349 return ret_errno(EINVAL);
258f8051
CB
1350
1351 lxc_conf->monitor_signal_pdeath = sig_n;
1352 return 0;
1353 }
1354
cb5f3df2 1355 return ret_errno(EINVAL);
258f8051
CB
1356}
1357
713046e3 1358static int set_config_group(const char *key, const char *value,
c7e27aaf 1359 struct lxc_conf *lxc_conf, void *data)
ee1e7aa0 1360{
15436995
CB
1361 __do_free char *groups = NULL;
1362 char *token;
ee1e7aa0 1363
663e9916 1364 if (lxc_config_value_empty(value))
ee1e7aa0
SG
1365 return lxc_clear_groups(lxc_conf);
1366
1367 groups = strdup(value);
25a908b8 1368 if (!groups)
15436995 1369 return ret_errno(ENOMEM);
ee1e7aa0 1370
25a908b8
CB
1371 /* In case several groups are specified in a single line split these
1372 * groups in a single element for the list.
504a2217 1373 */
62dd965e 1374 lxc_iterate_parts(token, groups, " \t") {
15436995
CB
1375 __do_free struct lxc_list *grouplist = NULL;
1376
ee1e7aa0 1377 grouplist = malloc(sizeof(*grouplist));
15436995
CB
1378 if (!grouplist)
1379 return ret_errno(ENOMEM);
ee1e7aa0
SG
1380
1381 grouplist->elem = strdup(token);
15436995
CB
1382 if (!grouplist->elem)
1383 return ret_errno(ENOMEM);
ee1e7aa0 1384
15436995 1385 lxc_list_add_tail(&lxc_conf->groups, move_ptr(grouplist));
d028235d 1386 }
ee1e7aa0 1387
15436995 1388 return 0;
ee1e7aa0
SG
1389}
1390
713046e3 1391static int set_config_environment(const char *key, const char *value,
c7e27aaf 1392 struct lxc_conf *lxc_conf, void *data)
7c661726 1393{
a6bf1128 1394 __do_free struct lxc_list *list_item = NULL;
7c661726 1395
663e9916 1396 if (lxc_config_value_empty(value))
ab799c0b
SG
1397 return lxc_clear_environment(lxc_conf);
1398
7c661726
MP
1399 list_item = malloc(sizeof(*list_item));
1400 if (!list_item)
a6bf1128 1401 return ret_errno(ENOMEM);
7c661726 1402
5eab47bc
CB
1403 if (!strchr(value, '=')) {
1404 const char *env_val;
1405 const char *env_key = value;
1406 const char *env_var[3] = {0};
1407
1408 env_val = getenv(env_key);
1409 if (!env_val)
a6bf1128 1410 return ret_errno(ENOENT);
5eab47bc
CB
1411
1412 env_var[0] = env_key;
1413 env_var[1] = env_val;
1414 list_item->elem = lxc_string_join("=", env_var, false);
1415 } else {
1416 list_item->elem = strdup(value);
1417 }
7c661726
MP
1418
1419 if (!list_item->elem)
a6bf1128 1420 return ret_errno(ENOMEM);
7c661726 1421
a6bf1128 1422 lxc_list_add_tail(&lxc_conf->environment, move_ptr(list_item));
7c661726
MP
1423
1424 return 0;
7c661726
MP
1425}
1426
fe1c5887
CB
1427static int set_config_tty_max(const char *key, const char *value,
1428 struct lxc_conf *lxc_conf, void *data)
b0a33c1e 1429{
885766f5
CB
1430 int ret;
1431 unsigned int nbtty = 0;
1432
663e9916 1433 if (lxc_config_value_empty(value)) {
885766f5 1434 lxc_conf->ttys.max = 0;
fb12b12a 1435 return 0;
cb508ee8 1436 }
fb12b12a 1437
885766f5
CB
1438 ret = lxc_safe_uint(value, &nbtty);
1439 if (ret < 0)
1440 return -1;
1441
1442 lxc_conf->ttys.max = nbtty;
47903908 1443
885766f5 1444 return 0;
b0a33c1e 1445}
1446
42e53c29 1447static int set_config_tty_dir(const char *key, const char *value,
c7e27aaf 1448 struct lxc_conf *lxc_conf, void *data)
7c6ef2a2 1449{
885766f5 1450 return set_config_string_item_max(&lxc_conf->ttys.dir, value,
504a2217 1451 NAME_MAX + 1);
7c6ef2a2
SH
1452}
1453
953fe44f
CB
1454static int set_config_apparmor_profile(const char *key, const char *value,
1455 struct lxc_conf *lxc_conf, void *data)
e075f5d9 1456{
713046e3 1457 return set_config_string_item(&lxc_conf->lsm_aa_profile, value);
fe4de9a6
DE
1458}
1459
953fe44f
CB
1460static int set_config_apparmor_allow_incomplete(const char *key,
1461 const char *value,
1462 struct lxc_conf *lxc_conf,
1463 void *data)
7aff4f43 1464{
663e9916 1465 if (lxc_config_value_empty(value)) {
cccfa758 1466 lxc_conf->lsm_aa_allow_incomplete = 0;
a678e9fa 1467 return 0;
cccfa758 1468 }
a678e9fa 1469
a56e2df9
CB
1470 if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete) < 0)
1471 return -1;
7aff4f43 1472
25a908b8 1473 if (lxc_conf->lsm_aa_allow_incomplete > 1)
a56e2df9 1474 return -1;
7aff4f43
SH
1475
1476 return 0;
1477}
1478
1800f924
WB
1479static int set_config_apparmor_allow_nesting(const char *key,
1480 const char *value,
1481 struct lxc_conf *lxc_conf,
1482 void *data)
1483{
1484 if (lxc_config_value_empty(value))
1485 return clr_config_apparmor_allow_nesting(key, lxc_conf, NULL);
1486
1487 if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_nesting) < 0)
1488 return -1;
1489
1490 if (lxc_conf->lsm_aa_allow_nesting > 1)
1491 return -1;
1492
1493 return 0;
1494}
1495
1496static int set_config_apparmor_raw(const char *key,
1497 const char *value,
1498 struct lxc_conf *lxc_conf,
1499 void *data)
1500{
1501 char *elem;
1502 struct lxc_list *list;
1503
1504 if (lxc_config_value_empty(value))
1505 return lxc_clear_apparmor_raw(lxc_conf);
1506
1507 list = malloc(sizeof(*list));
1508 if (!list) {
1509 errno = ENOMEM;
1510 return -1;
1511 }
1512
1513 elem = strdup(value);
1514 if (!elem) {
1515 free(list);
1516 return -1;
1517 }
1518 list->elem = elem;
1519
1520 lxc_list_add_tail(&lxc_conf->lsm_aa_raw, list);
1521
1522 return 0;
1523}
1524
953fe44f
CB
1525static int set_config_selinux_context(const char *key, const char *value,
1526 struct lxc_conf *lxc_conf, void *data)
fe4de9a6 1527{
713046e3 1528 return set_config_string_item(&lxc_conf->lsm_se_context, value);
e075f5d9 1529}
e075f5d9 1530
4fef78bc
MB
1531static int set_config_selinux_context_keyring(const char *key, const char *value,
1532 struct lxc_conf *lxc_conf, void *data)
1533{
1534 return set_config_string_item(&lxc_conf->lsm_se_keyring_context, value);
1535}
1536
8f818a84
MB
1537static int set_config_keyring_session(const char *key, const char *value,
1538 struct lxc_conf *lxc_conf, void *data)
1539{
1540 return set_config_bool_item(&lxc_conf->keyring_disable_session, value, false);
1541}
1542
46cc906d 1543static int set_config_log_file(const char *key, const char *value,
c7e27aaf 1544 struct lxc_conf *c, void *data)
4a85ce2a 1545{
6d03d92a
DE
1546 int ret;
1547
663e9916 1548 if (lxc_config_value_empty(value)) {
0d601acb
CB
1549 free(c->logfile);
1550 c->logfile = NULL;
1551 return 0;
1552 }
1553
1554 /* Store these values in the lxc_conf, and then try to set for actual
504a2217
CB
1555 * current logging.
1556 */
713046e3 1557 ret = set_config_path_item(&c->logfile, value);
6d03d92a 1558 if (ret == 0)
858377e4 1559 ret = lxc_log_set_file(&c->logfd, c->logfile);
25a908b8 1560
6d03d92a 1561 return ret;
4a85ce2a
SH
1562}
1563
46cc906d 1564static int set_config_log_level(const char *key, const char *value,
c7e27aaf 1565 struct lxc_conf *lxc_conf, void *data)
4a85ce2a 1566{
9ea87d5d
SH
1567 int newlevel;
1568
663e9916 1569 if (lxc_config_value_empty(value)) {
4b73005c 1570 lxc_conf->loglevel = LXC_LOG_LEVEL_NOTSET;
4a85ce2a 1571 return 0;
575b9745 1572 }
4a85ce2a 1573
a56e2df9
CB
1574 if (value[0] >= '0' && value[0] <= '9') {
1575 if (lxc_safe_int(value, &newlevel) < 0)
1576 return -1;
1577 } else {
9ea87d5d 1578 newlevel = lxc_log_priority_to_int(value);
a56e2df9 1579 }
575b9745 1580
504a2217 1581 /* Store these values in the lxc_conf, and then try to set for actual
575b9745
CB
1582 * current logging.
1583 */
b40a606e 1584 lxc_conf->loglevel = newlevel;
47903908 1585
858377e4 1586 return lxc_log_set_level(&lxc_conf->loglevel, newlevel);
4a85ce2a
SH
1587}
1588
713046e3 1589static int set_config_autodev(const char *key, const char *value,
c7e27aaf 1590 struct lxc_conf *lxc_conf, void *data)
c6883f38 1591{
663e9916 1592 if (lxc_config_value_empty(value)) {
1045031e 1593 lxc_conf->autodev = 0;
180abbc0 1594 return 0;
1045031e 1595 }
180abbc0 1596
ff6cb4ed
CB
1597 if (lxc_safe_uint(value, &lxc_conf->autodev) < 0)
1598 return -1;
c6883f38 1599
25a908b8 1600 if (lxc_conf->autodev > 1)
ff6cb4ed 1601 return -1;
c6883f38
SH
1602
1603 return 0;
1604}
1605
63012bdd
CK
1606static int set_config_autodev_tmpfs_size(const char *key, const char *value,
1607 struct lxc_conf *lxc_conf, void *data)
1608{
1609 if (lxc_config_value_empty(value)) {
1610 lxc_conf->autodevtmpfssize = 500000;
1611 return 0;
1612 }
1613
1614 if (lxc_safe_int(value, &lxc_conf->autodevtmpfssize) < 0)
1615 lxc_conf->autodevtmpfssize = 500000;
1616
1617 return 0;
1618}
1619
55c84efc 1620static int set_config_signal_halt(const char *key, const char *value,
c7e27aaf 1621 struct lxc_conf *lxc_conf, void *data)
f0f1d8c0 1622{
62a085fb 1623 int sig_n;
f0f1d8c0 1624
663e9916 1625 if (lxc_config_value_empty(value)) {
c1a64603 1626 lxc_conf->haltsignal = 0;
955912f0 1627 return 0;
c1a64603 1628 }
955912f0 1629
62a085fb 1630 sig_n = sig_parse(value);
f0f1d8c0
DE
1631 if (sig_n < 0)
1632 return -1;
25a908b8 1633
f0f1d8c0
DE
1634 lxc_conf->haltsignal = sig_n;
1635
1636 return 0;
1637}
1638
55c84efc 1639static int set_config_signal_reboot(const char *key, const char *value,
c7e27aaf 1640 struct lxc_conf *lxc_conf, void *data)
dd267776 1641{
9d7e7587 1642 int sig_n;
dd267776 1643
663e9916 1644 if (lxc_config_value_empty(value)) {
18fcee44 1645 lxc_conf->rebootsignal = 0;
9d7e7587 1646 return 0;
18fcee44 1647 }
9d7e7587
CB
1648
1649 sig_n = sig_parse(value);
dd267776
BP
1650 if (sig_n < 0)
1651 return -1;
25a908b8 1652
dd267776
BP
1653 lxc_conf->rebootsignal = sig_n;
1654
1655 return 0;
1656}
1657
55c84efc 1658static int set_config_signal_stop(const char *key, const char *value,
c7e27aaf 1659 struct lxc_conf *lxc_conf, void *data)
a84b9932 1660{
6ca6aedd 1661 int sig_n;
a84b9932 1662
663e9916 1663 if (lxc_config_value_empty(value)) {
4100d1a7 1664 lxc_conf->stopsignal = 0;
6ca6aedd 1665 return 0;
4100d1a7 1666 }
6ca6aedd
CB
1667
1668 sig_n = sig_parse(value);
a84b9932
AV
1669 if (sig_n < 0)
1670 return -1;
25a908b8 1671
a84b9932
AV
1672 lxc_conf->stopsignal = sig_n;
1673
1674 return 0;
1675}
1676
54860ed0
CB
1677static int __set_config_cgroup_controller(const char *key, const char *value,
1678 struct lxc_conf *lxc_conf, int version)
576f946d 1679{
54860ed0
CB
1680 const char *subkey, *token;
1681 size_t token_len;
bf83c5b9
MS
1682 struct lxc_list *cglist = NULL;
1683 struct lxc_cgroup *cgelem = NULL;
576f946d 1684
663e9916 1685 if (lxc_config_value_empty(value))
54860ed0
CB
1686 return lxc_clear_cgroups(lxc_conf, key, version);
1687
1688 if (version == CGROUP2_SUPER_MAGIC) {
1689 token = "lxc.cgroup2.";
1690 token_len = 12;
1691 } else if (version == CGROUP_SUPER_MAGIC) {
1692 token = "lxc.cgroup.";
1693 token_len = 11;
1694 } else {
1695 return -EINVAL;
1696 }
576f946d 1697
54860ed0
CB
1698 if (strncmp(key, token, token_len) != 0)
1699 return -EINVAL;
a871ff6b 1700
54860ed0
CB
1701 subkey = key + token_len;
1702 if (*subkey == '\0')
1703 return -EINVAL;
576f946d 1704
1705 cglist = malloc(sizeof(*cglist));
1706 if (!cglist)
bf83c5b9 1707 goto out;
576f946d 1708
1709 cgelem = malloc(sizeof(*cgelem));
bf83c5b9
MS
1710 if (!cgelem)
1711 goto out;
1712 memset(cgelem, 0, sizeof(*cgelem));
576f946d 1713
1714 cgelem->subsystem = strdup(subkey);
54860ed0
CB
1715 if (!cgelem->subsystem)
1716 goto out;
bf83c5b9 1717
54860ed0
CB
1718 cgelem->value = strdup(value);
1719 if (!cgelem->value)
bf83c5b9
MS
1720 goto out;
1721
54860ed0
CB
1722 cgelem->version = version;
1723
1724 lxc_list_add_elem(cglist, cgelem);
576f946d 1725
54860ed0
CB
1726 if (version == CGROUP2_SUPER_MAGIC)
1727 lxc_list_add_tail(&lxc_conf->cgroup2, cglist);
1728 else
1729 lxc_list_add_tail(&lxc_conf->cgroup, cglist);
576f946d 1730
1731 return 0;
bf83c5b9
MS
1732
1733out:
f10fad2f 1734 free(cglist);
bf83c5b9 1735 if (cgelem) {
f10fad2f 1736 free(cgelem->subsystem);
f10fad2f 1737 free(cgelem->value);
bf83c5b9
MS
1738 free(cgelem);
1739 }
1740
1741 return -1;
576f946d 1742}
1743
54860ed0
CB
1744static int set_config_cgroup_controller(const char *key, const char *value,
1745 struct lxc_conf *lxc_conf, void *data)
1746{
1747 return __set_config_cgroup_controller(key, value, lxc_conf,
1748 CGROUP_SUPER_MAGIC);
1749}
1750
1751static int set_config_cgroup2_controller(const char *key, const char *value,
1752 struct lxc_conf *lxc_conf, void *data)
1753{
1754 return __set_config_cgroup_controller(key, value, lxc_conf,
1755 CGROUP2_SUPER_MAGIC);
1756}
1757
1758
43654d34
CB
1759static int set_config_cgroup_dir(const char *key, const char *value,
1760 struct lxc_conf *lxc_conf, void *data)
1761{
1762 if (lxc_config_value_empty(value))
1763 return clr_config_cgroup_dir(key, lxc_conf, NULL);
1764
43654d34
CB
1765 return set_config_string_item(&lxc_conf->cgroup_meta.dir, value);
1766}
1767
a900cbaf
WB
1768static int set_config_cgroup_monitor_dir(const char *key, const char *value,
1769 struct lxc_conf *lxc_conf, void *data)
1770{
1771 if (lxc_config_value_empty(value))
1772 return clr_config_cgroup_monitor_dir(key, lxc_conf, NULL);
1773
1774 return set_config_string_item(&lxc_conf->cgroup_meta.monitor_dir,
1775 value);
1776}
1777
7696c1f9
RJ
1778static int set_config_cgroup_monitor_pivot_dir(const char *key, const char *value,
1779 struct lxc_conf *lxc_conf, void *data)
1780{
1781 if (lxc_config_value_empty(value))
1782 return clr_config_cgroup_monitor_pivot_dir(key, lxc_conf, NULL);
1783
1784 return set_config_string_item(&lxc_conf->cgroup_meta.monitor_pivot_dir,
1785 value);
1786}
1787
a900cbaf
WB
1788static int set_config_cgroup_container_dir(const char *key, const char *value,
1789 struct lxc_conf *lxc_conf,
1790 void *data)
1791{
1792 if (lxc_config_value_empty(value))
1793 return clr_config_cgroup_container_dir(key, lxc_conf, NULL);
1794
1795 return set_config_string_item(&lxc_conf->cgroup_meta.container_dir,
1796 value);
1797}
1798
1799static int set_config_cgroup_container_inner_dir(const char *key,
1800 const char *value,
1801 struct lxc_conf *lxc_conf,
1802 void *data)
1803{
1804 if (lxc_config_value_empty(value))
e93197e7 1805 return clr_config_cgroup_container_inner_dir(key, lxc_conf, NULL);
a900cbaf
WB
1806
1807 if (strchr(value, '/') ||
1808 strcmp(value, ".") == 0 ||
1809 strcmp(value, "..") == 0)
e93197e7 1810 return log_error_errno(-EINVAL, EINVAL, "lxc.cgroup.dir.container.inner must be a single directory name");
a900cbaf 1811
e93197e7 1812 return set_config_string_item(&lxc_conf->cgroup_meta.namespace_dir, value);
a900cbaf
WB
1813}
1814
9caee129
CB
1815static int set_config_cgroup_relative(const char *key, const char *value,
1816 struct lxc_conf *lxc_conf, void *data)
76f0e2e7
CB
1817{
1818 unsigned int converted;
1819 int ret;
1820
1821 if (lxc_config_value_empty(value))
9caee129 1822 return clr_config_cgroup_relative(key, lxc_conf, NULL);
76f0e2e7
CB
1823
1824 ret = lxc_safe_uint(value, &converted);
1825 if (ret < 0)
1826 return -ret;
1827
1828 if (converted == 1) {
9caee129 1829 lxc_conf->cgroup_meta.relative = true;
76f0e2e7
CB
1830 return 0;
1831 }
1832
1833 if (converted == 0) {
9caee129 1834 lxc_conf->cgroup_meta.relative = false;
76f0e2e7
CB
1835 return 0;
1836 }
1837
1838 return -EINVAL;
1839}
1840
f7662514
CB
1841static bool parse_limit_value(const char **value, rlim_t *res)
1842{
1843 char *endptr = NULL;
1844
1845 if (strncmp(*value, "unlimited", STRLITERALLEN("unlimited")) == 0) {
1846 *res = RLIM_INFINITY;
1847 *value += STRLITERALLEN("unlimited");
1848 return true;
1849 }
1850
1851 errno = 0;
1852 *res = strtoull(*value, &endptr, 10);
1853 if (errno || !endptr)
1854 return false;
1855
1856 *value = endptr;
1857
1858 return true;
1859}
1860
240d4b74 1861static int set_config_prlimit(const char *key, const char *value,
c7e27aaf 1862 struct lxc_conf *lxc_conf, void *data)
c6d09e15 1863{
c6d09e15
WB
1864 struct lxc_list *iter;
1865 struct rlimit limit;
71460831 1866 rlim_t limit_value;
504a2217
CB
1867 struct lxc_list *limlist = NULL;
1868 struct lxc_limit *limelem = NULL;
c6d09e15 1869
663e9916 1870 if (lxc_config_value_empty(value))
c6d09e15
WB
1871 return lxc_clear_limits(lxc_conf, key);
1872
6333c915 1873 if (strncmp(key, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0)
c6d09e15
WB
1874 return -1;
1875
6333c915 1876 key += STRLITERALLEN("lxc.prlimit.");
c6d09e15
WB
1877
1878 /* soft limit comes first in the value */
1879 if (!parse_limit_value(&value, &limit_value))
1880 return -1;
47903908 1881
c6d09e15
WB
1882 limit.rlim_cur = limit_value;
1883
1884 /* skip spaces and a colon */
1885 while (isspace(*value))
1886 ++value;
504a2217 1887
c6d09e15
WB
1888 if (*value == ':')
1889 ++value;
1890 else if (*value) /* any other character is an error here */
1891 return -1;
504a2217 1892
c6d09e15
WB
1893 while (isspace(*value))
1894 ++value;
1895
1896 /* optional hard limit */
1897 if (*value) {
1898 if (!parse_limit_value(&value, &limit_value))
1899 return -1;
47903908 1900
c6d09e15 1901 limit.rlim_max = limit_value;
504a2217 1902
c6d09e15
WB
1903 /* check for trailing garbage */
1904 while (isspace(*value))
1905 ++value;
504a2217 1906
c6d09e15
WB
1907 if (*value)
1908 return -1;
1909 } else {
1910 /* a single value sets both hard and soft limit */
1911 limit.rlim_max = limit.rlim_cur;
1912 }
1913
1914 /* find existing list element */
7edd0540 1915 lxc_list_for_each(iter, &lxc_conf->limits) {
c6d09e15
WB
1916 limelem = iter->elem;
1917 if (!strcmp(key, limelem->resource)) {
1918 limelem->limit = limit;
1919 return 0;
1920 }
1921 }
1922
1923 /* allocate list element */
1924 limlist = malloc(sizeof(*limlist));
1925 if (!limlist)
7edd0540 1926 goto on_error;
2e6e3feb 1927
c6d09e15
WB
1928 limelem = malloc(sizeof(*limelem));
1929 if (!limelem)
7edd0540 1930 goto on_error;
c6d09e15
WB
1931 memset(limelem, 0, sizeof(*limelem));
1932
1933 limelem->resource = strdup(key);
1934 if (!limelem->resource)
7edd0540 1935 goto on_error;
c6d09e15 1936
47903908 1937 limelem->limit = limit;
7edd0540 1938 lxc_list_add_elem(limlist, limelem);;
c6d09e15
WB
1939 lxc_list_add_tail(&lxc_conf->limits, limlist);
1940
1941 return 0;
1942
7edd0540 1943on_error:
c6d09e15 1944 free(limlist);
47903908 1945
c6d09e15
WB
1946 if (limelem) {
1947 free(limelem->resource);
1948 free(limelem);
1949 }
47903908 1950
c6d09e15
WB
1951 return -1;
1952}
1953
7edd0540
L
1954static int set_config_sysctl(const char *key, const char *value,
1955 struct lxc_conf *lxc_conf, void *data)
1956{
1957 struct lxc_list *iter;
e409b214 1958 char *replace_value = NULL;
7edd0540
L
1959 struct lxc_list *sysctl_list = NULL;
1960 struct lxc_sysctl *sysctl_elem = NULL;
7edd0540
L
1961
1962 if (lxc_config_value_empty(value))
e409b214 1963 return clr_config_sysctl(key, lxc_conf, NULL);
7edd0540 1964
6333c915 1965 if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) != 0)
7edd0540
L
1966 return -1;
1967
6333c915 1968 key += STRLITERALLEN("lxc.sysctl.");
7edd0540
L
1969
1970 /* find existing list element */
1971 lxc_list_for_each(iter, &lxc_conf->sysctls) {
1972 sysctl_elem = iter->elem;
e409b214
CB
1973
1974 if (strcmp(key, sysctl_elem->key) != 0)
1975 continue;
1976
1977 replace_value = strdup(value);
1978 if (!replace_value)
1979 return -1;
1980
1981 free(sysctl_elem->value);
1982 sysctl_elem->value = replace_value;
47903908 1983
e409b214 1984 return 0;
7edd0540
L
1985 }
1986
1987 /* allocate list element */
1988 sysctl_list = malloc(sizeof(*sysctl_list));
1989 if (!sysctl_list)
1990 goto on_error;
1991
1992 sysctl_elem = malloc(sizeof(*sysctl_elem));
1993 if (!sysctl_elem)
1994 goto on_error;
1995 memset(sysctl_elem, 0, sizeof(*sysctl_elem));
1996
1997 sysctl_elem->key = strdup(key);
1998 if (!sysctl_elem->key)
1999 goto on_error;
2000
2001 sysctl_elem->value = strdup(value);
2002 if (!sysctl_elem->value)
2003 goto on_error;
2004
2005 lxc_list_add_elem(sysctl_list, sysctl_elem);
7edd0540
L
2006 lxc_list_add_tail(&lxc_conf->sysctls, sysctl_list);
2007
2008 return 0;
2009
2010on_error:
2011 free(sysctl_list);
47903908 2012
7edd0540
L
2013 if (sysctl_elem) {
2014 free(sysctl_elem->key);
2015 free(sysctl_elem->value);
2016 free(sysctl_elem);
2017 }
47903908 2018
7edd0540 2019 return -1;
7edd0540
L
2020}
2021
61d7a733
YT
2022static int set_config_proc(const char *key, const char *value,
2023 struct lxc_conf *lxc_conf, void *data)
2024{
2025 const char *subkey;
2026 struct lxc_list *proclist = NULL;
2027 struct lxc_proc *procelem = NULL;
2028
2029 if (lxc_config_value_empty(value))
2030 return clr_config_proc(key, lxc_conf, NULL);
2031
6333c915 2032 if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) != 0)
61d7a733
YT
2033 return -1;
2034
6333c915 2035 subkey = key + STRLITERALLEN("lxc.proc.");
61d7a733
YT
2036 if (*subkey == '\0')
2037 return -EINVAL;
2038
2039 proclist = malloc(sizeof(*proclist));
2040 if (!proclist)
2041 goto on_error;
2042
2043 procelem = malloc(sizeof(*procelem));
2044 if (!procelem)
2045 goto on_error;
2046 memset(procelem, 0, sizeof(*procelem));
2047
2048 procelem->filename = strdup(subkey);
2049 procelem->value = strdup(value);
2050
2051 if (!procelem->filename || !procelem->value)
2052 goto on_error;
2053
2054 proclist->elem = procelem;
2055
2056 lxc_list_add_tail(&lxc_conf->procs, proclist);
2057
2058 return 0;
2059
2060on_error:
2061 free(proclist);
47903908 2062
61d7a733
YT
2063 if (procelem) {
2064 free(procelem->filename);
2065 free(procelem->value);
2066 free(procelem);
2067 }
2068
2069 return -1;
2070}
2071
5014ff2e 2072static int set_config_idmaps(const char *key, const char *value,
c7e27aaf 2073 struct lxc_conf *lxc_conf, void *data)
f6d3e3e4 2074{
251d0d2a 2075 unsigned long hostid, nsid, range;
f6d3e3e4 2076 char type;
0b843d35 2077 int ret;
34a7a4c6
CB
2078 struct lxc_list *idmaplist = NULL;
2079 struct id_map *idmap = NULL;
f6d3e3e4 2080
663e9916 2081 if (lxc_config_value_empty(value))
7d0eb87e
SH
2082 return lxc_clear_idmaps(lxc_conf);
2083
f6d3e3e4
SH
2084 idmaplist = malloc(sizeof(*idmaplist));
2085 if (!idmaplist)
34a7a4c6 2086 goto on_error;
f6d3e3e4
SH
2087
2088 idmap = malloc(sizeof(*idmap));
2089 if (!idmap)
34a7a4c6 2090 goto on_error;
f6d3e3e4
SH
2091 memset(idmap, 0, sizeof(*idmap));
2092
0b843d35 2093 ret = parse_idmaps(value, &type, &nsid, &hostid, &range);
a8b1ac78 2094 if (ret < 0) {
f37d1c22 2095 ERROR("Failed to parse id mappings");
34a7a4c6 2096 goto on_error;
a8b1ac78 2097 }
34a7a4c6 2098
25a908b8 2099 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range);
ac7725e7 2100 if (type == 'u')
f6d3e3e4 2101 idmap->idtype = ID_TYPE_UID;
ac7725e7 2102 else if (type == 'g')
f6d3e3e4
SH
2103 idmap->idtype = ID_TYPE_GID;
2104 else
34a7a4c6 2105 goto on_error;
7e60c3f0 2106
f6d3e3e4
SH
2107 idmap->hostid = hostid;
2108 idmap->nsid = nsid;
2109 idmap->range = range;
7e60c3f0
SG
2110 idmaplist->elem = idmap;
2111 lxc_list_add_tail(&lxc_conf->id_map, idmaplist);
46ad64ab
CB
2112
2113 if (!lxc_conf->root_nsuid_map && idmap->idtype == ID_TYPE_UID)
2114 if (idmap->nsid == 0)
2115 lxc_conf->root_nsuid_map = idmap;
2116
4160c3a0 2117 if (!lxc_conf->root_nsgid_map && idmap->idtype == ID_TYPE_GID)
46ad64ab
CB
2118 if (idmap->nsid == 0)
2119 lxc_conf->root_nsgid_map = idmap;
2120
34a7a4c6 2121 idmap = NULL;
7e60c3f0 2122
f6d3e3e4
SH
2123 return 0;
2124
34a7a4c6 2125on_error:
f10fad2f 2126 free(idmaplist);
34a7a4c6 2127 free(idmap);
f6d3e3e4
SH
2128
2129 return -1;
2130}
2131
47148e96
CB
2132static int set_config_mount_fstab(const char *key, const char *value,
2133 struct lxc_conf *lxc_conf, void *data)
d95db067 2134{
663e9916 2135 if (lxc_config_value_empty(value)) {
47148e96 2136 clr_config_mount_fstab(key, lxc_conf, NULL);
d9192f5d 2137 return -1;
46f3de30 2138 }
6f5685f0 2139
713046e3 2140 return set_config_path_item(&lxc_conf->fstab, value);
d95db067
DE
2141}
2142
713046e3 2143static int set_config_mount_auto(const char *key, const char *value,
c7e27aaf 2144 struct lxc_conf *lxc_conf, void *data)
368bbc02 2145{
62dd965e 2146 char *autos, *token;
368bbc02
CS
2147 int i;
2148 int ret = -1;
504a2217
CB
2149 static struct {
2150 const char *token;
2151 int mask;
2152 int flag;
2153 } allowed_auto_mounts[] = {
6812d833
CB
2154 { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
2155 { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
2156 { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW },
2157 { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
2158 { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO },
2159 { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
2160 { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW },
2161 { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC },
2162 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED },
2163 { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO },
2164 { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW },
2165 { "cgroup:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC | LXC_AUTO_CGROUP_FORCE },
2166 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED | LXC_AUTO_CGROUP_FORCE },
2167 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO | LXC_AUTO_CGROUP_FORCE },
2168 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW | LXC_AUTO_CGROUP_FORCE },
2169 { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC },
2170 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED },
2171 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO },
2172 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW },
2173 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC | LXC_AUTO_CGROUP_FORCE },
2174 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED | LXC_AUTO_CGROUP_FORCE },
2175 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO | LXC_AUTO_CGROUP_FORCE },
2176 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW | LXC_AUTO_CGROUP_FORCE },
adf0ba1f 2177 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK, LXC_AUTO_SHMOUNTS },
25a908b8 2178 /* For adding anything that is just a single on/off, but has no
3f69fb12
SY
2179 * options: keep mask and flag identical and just define the enum
2180 * value as an unused bit so far
504a2217 2181 */
3f69fb12 2182 { NULL, 0, 0 }
504a2217 2183 };
368bbc02 2184
663e9916 2185 if (lxc_config_value_empty(value)) {
d9192f5d
SH
2186 lxc_conf->auto_mounts = 0;
2187 return 0;
2188 }
368bbc02
CS
2189
2190 autos = strdup(value);
25a908b8 2191 if (!autos)
368bbc02 2192 return -1;
368bbc02 2193
62dd965e 2194 lxc_iterate_parts(token, autos, " \t") {
0d190408
LT
2195 bool is_shmounts = false;
2196
368bbc02
CS
2197 for (i = 0; allowed_auto_mounts[i].token; i++) {
2198 if (!strcmp(allowed_auto_mounts[i].token, token))
2199 break;
0d190408 2200
fd14fdb8 2201 if (strcmp("shmounts:", allowed_auto_mounts[i].token) == 0 &&
6333c915 2202 strncmp("shmounts:", token, STRLITERALLEN("shmounts:")) == 0) {
0d190408
LT
2203 is_shmounts = true;
2204 break;
2205 }
368bbc02
CS
2206 }
2207
2208 if (!allowed_auto_mounts[i].token) {
25a908b8 2209 ERROR("Invalid filesystem to automount \"%s\"", token);
62dd965e 2210 goto on_error;
368bbc02
CS
2211 }
2212
b06b8511 2213 lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask;
368bbc02 2214 lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag;
47903908 2215
0d190408 2216 if (is_shmounts) {
6437f1c1
CB
2217 char *container_path;
2218 char *host_path;
594426ca 2219
6437f1c1
CB
2220 host_path = token + STRLITERALLEN("shmounts:");
2221 if (*host_path == '\0') {
fd14fdb8 2222 SYSERROR("Failed to copy shmounts host path");
62dd965e 2223 goto on_error;
fd14fdb8
LT
2224 }
2225
6437f1c1
CB
2226 container_path = strchr(host_path, ':');
2227 if (!container_path || *(container_path + 1) == '\0')
2228 container_path = "/dev/.lxc-mounts";
2229 else
2230 *container_path++ = '\0';
2231
6437f1c1 2232 lxc_conf->shmount.path_host = strdup(host_path);
594426ca
CB
2233 if (!lxc_conf->shmount.path_host) {
2234 SYSERROR("Failed to copy shmounts host path");
62dd965e 2235 goto on_error;
0d190408 2236 }
fd14fdb8 2237
6437f1c1 2238 lxc_conf->shmount.path_cont = strdup(container_path);
fd14fdb8
LT
2239 if(!lxc_conf->shmount.path_cont) {
2240 SYSERROR("Failed to copy shmounts container path");
62dd965e 2241 goto on_error;
fd14fdb8 2242 }
0d190408 2243 }
d028235d 2244 }
368bbc02 2245
62dd965e
CB
2246 ret = 0;
2247
2248on_error:
368bbc02 2249 free(autos);
62dd965e 2250
368bbc02
CS
2251 return ret;
2252}
2253
713046e3 2254static int set_config_mount(const char *key, const char *value,
c7e27aaf 2255 struct lxc_conf *lxc_conf, void *data)
e7938e9e 2256{
e7938e9e
MN
2257 char *mntelem;
2258 struct lxc_list *mntlist;
2259
663e9916 2260 if (lxc_config_value_empty(value))
d9192f5d 2261 return lxc_clear_mount_entries(lxc_conf);
e7938e9e
MN
2262
2263 mntlist = malloc(sizeof(*mntlist));
2264 if (!mntlist)
2265 return -1;
2266
2267 mntelem = strdup(value);
00b6be44
SH
2268 if (!mntelem) {
2269 free(mntlist);
bf83c5b9 2270 return -1;
00b6be44 2271 }
e7938e9e
MN
2272 mntlist->elem = mntelem;
2273
2274 lxc_list_add_tail(&lxc_conf->mount_list, mntlist);
2275
2276 return 0;
2277}
2278
0d190408
LT
2279int add_elem_to_mount_list(const char *value, struct lxc_conf *lxc_conf) {
2280 return set_config_mount(NULL, value, lxc_conf, NULL);
2281}
2282
713046e3 2283static int set_config_cap_keep(const char *key, const char *value,
c7e27aaf 2284 struct lxc_conf *lxc_conf, void *data)
1fb86a7c 2285{
62dd965e 2286 char *keepcaps, *token;
1fb86a7c
SH
2287 struct lxc_list *keeplist;
2288 int ret = -1;
2289
663e9916 2290 if (lxc_config_value_empty(value))
7d0eb87e 2291 return lxc_clear_config_keepcaps(lxc_conf);
1fb86a7c
SH
2292
2293 keepcaps = strdup(value);
25a908b8 2294 if (!keepcaps)
1fb86a7c 2295 return -1;
1fb86a7c 2296
504a2217
CB
2297 /* In case several capability keep is specified in a single line
2298 * split these caps in a single element for the list.
2299 */
62dd965e 2300 lxc_iterate_parts(token, keepcaps, " \t") {
7035407c
DE
2301 if (!strcmp(token, "none"))
2302 lxc_clear_config_keepcaps(lxc_conf);
2303
1fb86a7c 2304 keeplist = malloc(sizeof(*keeplist));
25a908b8 2305 if (!keeplist)
62dd965e 2306 goto on_error;
1fb86a7c
SH
2307
2308 keeplist->elem = strdup(token);
2309 if (!keeplist->elem) {
1fb86a7c 2310 free(keeplist);
62dd965e 2311 goto on_error;
1fb86a7c
SH
2312 }
2313
2314 lxc_list_add_tail(&lxc_conf->keepcaps, keeplist);
d028235d 2315 }
1fb86a7c 2316
62dd965e
CB
2317 ret = 0;
2318
2319on_error:
1fb86a7c
SH
2320 free(keepcaps);
2321
2322 return ret;
2323}
2324
713046e3 2325static int set_config_cap_drop(const char *key, const char *value,
c7e27aaf 2326 struct lxc_conf *lxc_conf, void *data)
81810dd1 2327{
62dd965e 2328 char *dropcaps, *token;
81810dd1
DL
2329 struct lxc_list *droplist;
2330 int ret = -1;
2331
663e9916 2332 if (lxc_config_value_empty(value))
7d0eb87e 2333 return lxc_clear_config_caps(lxc_conf);
81810dd1
DL
2334
2335 dropcaps = strdup(value);
25a908b8 2336 if (!dropcaps)
81810dd1 2337 return -1;
81810dd1 2338
504a2217
CB
2339 /* In case several capability drop is specified in a single line
2340 * split these caps in a single element for the list.
2341 */
62dd965e 2342 lxc_iterate_parts(token, dropcaps, " \t") {
81810dd1 2343 droplist = malloc(sizeof(*droplist));
25a908b8 2344 if (!droplist)
62dd965e 2345 goto on_error;
81810dd1
DL
2346
2347 droplist->elem = strdup(token);
2348 if (!droplist->elem) {
81810dd1 2349 free(droplist);
62dd965e 2350 goto on_error;
81810dd1
DL
2351 }
2352
2353 lxc_list_add_tail(&lxc_conf->caps, droplist);
d028235d 2354 }
81810dd1 2355
62dd965e
CB
2356 ret = 0;
2357
2358on_error:
81810dd1
DL
2359 free(dropcaps);
2360
2361 return ret;
2362}
2363
3aed4934
CB
2364static int set_config_console_path(const char *key, const char *value,
2365 struct lxc_conf *lxc_conf, void *data)
28a4b0e5 2366{
713046e3 2367 return set_config_path_item(&lxc_conf->console.path, value);
28a4b0e5
DL
2368}
2369
d91adfa6
CB
2370static int set_config_console_rotate(const char *key, const char *value,
2371 struct lxc_conf *lxc_conf, void *data)
2372{
2373 if (lxc_config_value_empty(value)) {
2374 lxc_conf->console.log_rotate = 0;
2375 return 0;
2376 }
2377
2378 if (lxc_safe_uint(value, &lxc_conf->console.log_rotate) < 0)
2379 return -1;
2380
966b9ecd
CB
2381 if (lxc_conf->console.log_rotate > 1) {
2382 ERROR("The \"lxc.console.rotate\" config key can only be set "
2383 "to 0 or 1");
d91adfa6 2384 return -1;
966b9ecd 2385 }
d91adfa6
CB
2386
2387 return 0;
2388}
2389
713046e3 2390static int set_config_console_logfile(const char *key, const char *value,
c7e27aaf 2391 struct lxc_conf *lxc_conf, void *data)
96f15ca1 2392{
713046e3 2393 return set_config_path_item(&lxc_conf->console.log_path, value);
96f15ca1
SH
2394}
2395
28f3b1cd
CB
2396static int set_config_console_buffer_size(const char *key, const char *value,
2397 struct lxc_conf *lxc_conf, void *data)
a04220de
CB
2398{
2399 int ret;
2400 int64_t size;
28f3b1cd 2401 uint64_t buffer_size, pgsz;
a04220de
CB
2402
2403 if (lxc_config_value_empty(value)) {
28f3b1cd 2404 lxc_conf->console.buffer_size = 0;
a04220de
CB
2405 return 0;
2406 }
2407
2408 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2409 if (!strcmp(value, "auto")) {
28f3b1cd 2410 lxc_conf->console.buffer_size = 1 << 17;
a04220de
CB
2411 return 0;
2412 }
2413
2414 ret = parse_byte_size_string(value, &size);
2415 if (ret < 0)
2416 return -1;
2417
2418 if (size < 0)
2419 return -EINVAL;
2420
2421 /* must be at least a page size */
2422 pgsz = lxc_getpagesize();
2423 if ((uint64_t)size < pgsz) {
2424 NOTICE("Requested ringbuffer size for the console is %" PRId64
2425 " but must be at least %" PRId64
2426 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
2427 size, pgsz, pgsz);
2428 size = pgsz;
2429 }
2430
28f3b1cd
CB
2431 buffer_size = lxc_find_next_power2((uint64_t)size);
2432 if (buffer_size == 0)
a04220de
CB
2433 return -EINVAL;
2434
28f3b1cd 2435 if (buffer_size != size)
a04220de 2436 NOTICE("Passed size was not a power of 2. Rounding log size to "
28f3b1cd 2437 "next power of two: %" PRIu64 " bytes", buffer_size);
a04220de 2438
28f3b1cd 2439 lxc_conf->console.buffer_size = buffer_size;
47903908 2440
a04220de
CB
2441 return 0;
2442}
2443
861813e5
CB
2444static int set_config_console_size(const char *key, const char *value,
2445 struct lxc_conf *lxc_conf, void *data)
2446{
2447 int ret;
2448 int64_t size;
2449 uint64_t log_size, pgsz;
2450
2451 if (lxc_config_value_empty(value)) {
2452 lxc_conf->console.log_size = 0;
2453 return 0;
2454 }
2455
2456 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2457 if (!strcmp(value, "auto")) {
2458 lxc_conf->console.log_size = 1 << 17;
2459 return 0;
2460 }
2461
2462 ret = parse_byte_size_string(value, &size);
2463 if (ret < 0)
2464 return -1;
2465
2466 if (size < 0)
2467 return -EINVAL;
2468
2469 /* must be at least a page size */
2470 pgsz = lxc_getpagesize();
2471 if ((uint64_t)size < pgsz) {
2472 NOTICE("Requested ringbuffer size for the console is %" PRId64
2473 " but must be at least %" PRId64
2474 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
2475 size, pgsz, pgsz);
2476 size = pgsz;
2477 }
2478
2479 log_size = lxc_find_next_power2((uint64_t)size);
2480 if (log_size == 0)
2481 return -EINVAL;
2482
2483 if (log_size != size)
2484 NOTICE("Passed size was not a power of 2. Rounding log size to "
2485 "next power of two: %" PRIu64 " bytes", log_size);
2486
2487 lxc_conf->console.log_size = log_size;
47903908 2488
861813e5
CB
2489 return 0;
2490}
2491
5648fc19
CB
2492/*
2493 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2494 * file, we expand it in the unexpanded_config, so that after a save_config we
2495 * store the hwaddr for re-use.
2496 * This is only called when reading the config file, not when executing a
2497 * lxc.include.
2498 * 'x' and 'X' are substituted in-place.
2499 */
2500static void update_hwaddr(const char *line)
2501{
2502 char *p;
2503
2504 line += lxc_char_left_gc(line, strlen(line));
2505 if (line[0] == '#')
2506 return;
2507
2508 if (!lxc_config_net_is_hwaddr(line))
2509 return;
2510
2511 /* Let config_net_hwaddr raise the error. */
2512 p = strchr(line, '=');
2513 if (!p)
2514 return;
2515 p++;
2516
2517 while (isblank(*p))
2518 p++;
2519
2520 if (!*p)
2521 return;
2522
2523 rand_complete_hwaddr(p);
2524}
2525
6b0d5538 2526int append_unexp_config_line(const char *line, struct lxc_conf *conf)
f979ac15 2527{
62a821f1
CB
2528 size_t linelen;
2529 size_t len = conf->unexpanded_len;
f979ac15 2530
e6744e9b
SH
2531 update_hwaddr(line);
2532
62a821f1 2533 linelen = strlen(line);
6b0d5538 2534 while (conf->unexpanded_alloced <= len + linelen + 2) {
504a2217
CB
2535 char *tmp = realloc(conf->unexpanded_config,
2536 conf->unexpanded_alloced + 1024);
6b0d5538
SH
2537 if (!tmp)
2538 return -1;
504a2217 2539
6b0d5538
SH
2540 if (!conf->unexpanded_config)
2541 *tmp = '\0';
62a821f1 2542
6b0d5538
SH
2543 conf->unexpanded_config = tmp;
2544 conf->unexpanded_alloced += 1024;
2545 }
efed99a4 2546
62a821f1 2547 memcpy(conf->unexpanded_config + conf->unexpanded_len, line, linelen);
6b0d5538 2548 conf->unexpanded_len += linelen;
62a821f1
CB
2549 if (line[linelen - 1] != '\n')
2550 conf->unexpanded_config[conf->unexpanded_len++] = '\n';
2551 conf->unexpanded_config[conf->unexpanded_len] = '\0';
25a908b8 2552
f979ac15
SH
2553 return 0;
2554}
2555
e1daebd9
SH
2556static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf)
2557{
4110345b 2558 __do_closedir DIR *dir = NULL;
74f96976 2559 struct dirent *direntp;
18740311 2560 char path[PATH_MAX];
504a2217
CB
2561 int len;
2562 int ret = -1;
e1daebd9
SH
2563
2564 dir = opendir(dirp);
25a908b8 2565 if (!dir)
e1daebd9 2566 return -1;
e1daebd9 2567
74f96976 2568 while ((direntp = readdir(dir))) {
e1daebd9 2569 const char *fnam;
e1daebd9
SH
2570
2571 fnam = direntp->d_name;
2572 if (!strcmp(fnam, "."))
2573 continue;
2574
2575 if (!strcmp(fnam, ".."))
2576 continue;
2577
2578 len = strlen(fnam);
504a2217 2579 if (len < 6 || strncmp(fnam + len - 5, ".conf", 5) != 0)
e1daebd9 2580 continue;
25a908b8 2581
18740311 2582 len = snprintf(path, PATH_MAX, "%s/%s", dirp, fnam);
4110345b
CB
2583 if (len < 0 || len >= PATH_MAX)
2584 return -1;
e1daebd9
SH
2585
2586 ret = lxc_config_read(path, lxc_conf, true);
2587 if (ret < 0)
4110345b 2588 return -1;
e1daebd9 2589 }
e1daebd9 2590
4110345b 2591 return 0;
e1daebd9
SH
2592}
2593
973082f5 2594static int set_config_includefiles(const char *key, const char *value,
c7e27aaf 2595 struct lxc_conf *lxc_conf, void *data)
09ad6246 2596{
663e9916 2597 if (lxc_config_value_empty(value)) {
26471403 2598 clr_config_includefiles(key, lxc_conf, NULL);
616422f1 2599 return 0;
355c5701 2600 }
616422f1 2601
e1daebd9
SH
2602 if (is_dir(value))
2603 return do_includedir(value, lxc_conf);
2604
6b0d5538 2605 return lxc_config_read(value, lxc_conf, true);
09ad6246
SH
2606}
2607
7a96a068
CB
2608static int set_config_rootfs_path(const char *key, const char *value,
2609 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 2610{
693dbdb9
CB
2611 int ret;
2612 char *dup, *tmp;
2613 const char *container_path;
2614
2615 if (lxc_config_value_empty(value)) {
2616 free(lxc_conf->rootfs.path);
2617 lxc_conf->rootfs.path = NULL;
2618 return 0;
2619 }
2620
2621 dup = strdup(value);
2622 if (!dup)
2623 return -1;
2624
2625 /* Split <storage type>:<container path> into <storage type> and
2626 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2627 * "rootfs.path" to <container path>.
2628 */
2629 tmp = strchr(dup, ':');
2630 if (tmp) {
2631 *tmp = '\0';
47903908 2632
693dbdb9
CB
2633 ret = set_config_path_item(&lxc_conf->rootfs.bdev_type, dup);
2634 if (ret < 0) {
2635 free(dup);
2636 return -1;
2637 }
47903908 2638
693dbdb9
CB
2639 tmp++;
2640 container_path = tmp;
2641 } else {
2642 container_path = value;
2643 }
2644
2645 ret = set_config_path_item(&lxc_conf->rootfs.path, container_path);
2646 free(dup);
47903908 2647
693dbdb9 2648 return ret;
c2cc9f0a 2649}
2650
6e54330c
CB
2651static int set_config_rootfs_managed(const char *key, const char *value,
2652 struct lxc_conf *lxc_conf, void *data)
2653{
8f818a84 2654 return set_config_bool_item(&lxc_conf->rootfs.managed, value, true);
6e54330c
CB
2655}
2656
713046e3 2657static int set_config_rootfs_mount(const char *key, const char *value,
c7e27aaf 2658 struct lxc_conf *lxc_conf, void *data)
23b7ea69 2659{
713046e3 2660 return set_config_path_item(&lxc_conf->rootfs.mount, value);
23b7ea69
DL
2661}
2662
713046e3 2663static int set_config_rootfs_options(const char *key, const char *value,
c7e27aaf 2664 struct lxc_conf *lxc_conf, void *data)
a17b1e65 2665{
3437f95c
CB
2666 unsigned long mflags = 0, pflags = 0;
2667 char *mdata = NULL, *opts = NULL;
a08bfbe3 2668 int ret;
3437f95c
CB
2669 struct lxc_rootfs *rootfs = &lxc_conf->rootfs;
2670
2671 ret = parse_mntopts(value, &mflags, &mdata);
2672 if (ret < 0)
2673 return -EINVAL;
2674
2675 ret = parse_propagationopts(value, &pflags);
2676 if (ret < 0) {
2677 free(mdata);
2678 return -EINVAL;
2679 }
2680
2681 ret = set_config_string_item(&opts, value);
2682 if (ret < 0) {
2683 free(mdata);
2684 return -ENOMEM;
2685 }
2686
2687 rootfs->mountflags = mflags | pflags;
2688 rootfs->options = opts;
2689 rootfs->data = mdata;
2690
2691 return 0;
a17b1e65
SG
2692}
2693
b67771bc 2694static int set_config_uts_name(const char *key, const char *value,
c7e27aaf 2695 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 2696{
2697 struct utsname *utsname;
2698
663e9916 2699 if (lxc_config_value_empty(value)) {
b67771bc 2700 clr_config_uts_name(key, lxc_conf, NULL);
1939e73d 2701 return 0;
00cd8039 2702 }
1939e73d 2703
c2cc9f0a 2704 utsname = malloc(sizeof(*utsname));
25a908b8 2705 if (!utsname)
c2cc9f0a 2706 return -1;
c2cc9f0a 2707
2708 if (strlen(value) >= sizeof(utsname->nodename)) {
b6f24d54 2709 free(utsname);
c2cc9f0a 2710 return -1;
2711 }
2712
43f984ea 2713 (void)strlcpy(utsname->nodename, value, sizeof(utsname->nodename));
f10fad2f 2714 free(lxc_conf->utsname);
c2cc9f0a 2715 lxc_conf->utsname = utsname;
2716
2717 return 0;
2718}
2719
1d8d3676
CB
2720static int set_config_namespace_clone(const char *key, const char *value,
2721 struct lxc_conf *lxc_conf, void *data)
2722{
62dd965e 2723 char *ns, *token;
1d8d3676 2724 int cloneflag = 0;
1d8d3676
CB
2725
2726 if (lxc_config_value_empty(value))
2727 return clr_config_namespace_clone(key, lxc_conf, data);
2728
8bc8c715 2729 if (lxc_conf->ns_keep != 0) {
6d1400b5 2730 errno = EINVAL;
2731 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2732 "\"lxc.namespace.keep\"");
8bc8c715
CB
2733 return -EINVAL;
2734 }
2735
1d8d3676
CB
2736 ns = strdup(value);
2737 if (!ns)
2738 return -1;
1d8d3676 2739
62dd965e 2740 lxc_iterate_parts(token, ns, " \t") {
1d8d3676
CB
2741 token += lxc_char_left_gc(token, strlen(token));
2742 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2743 cloneflag = lxc_namespace_2_cloneflag(token);
2744 if (cloneflag < 0) {
2745 free(ns);
2746 return -EINVAL;
2747 }
2748 lxc_conf->ns_clone |= cloneflag;
2749 }
2750 free(ns);
2751
2752 return 0;
2753}
2754
abeb5bba
CB
2755static int set_config_namespace_keep(const char *key, const char *value,
2756 struct lxc_conf *lxc_conf, void *data)
2757{
62dd965e 2758 char *ns, *token;
abeb5bba 2759 int cloneflag = 0;
abeb5bba
CB
2760
2761 if (lxc_config_value_empty(value))
2762 return clr_config_namespace_keep(key, lxc_conf, data);
2763
8bc8c715 2764 if (lxc_conf->ns_clone != 0) {
6d1400b5 2765 errno = EINVAL;
2766 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2767 "\"lxc.namespace.keep\"");
8bc8c715
CB
2768 return -EINVAL;
2769 }
2770
abeb5bba
CB
2771 ns = strdup(value);
2772 if (!ns)
2773 return -1;
abeb5bba 2774
62dd965e 2775 lxc_iterate_parts(token, ns, " \t") {
abeb5bba
CB
2776 token += lxc_char_left_gc(token, strlen(token));
2777 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2778 cloneflag = lxc_namespace_2_cloneflag(token);
2779 if (cloneflag < 0) {
2780 free(ns);
2781 return -EINVAL;
2782 }
2783 lxc_conf->ns_keep |= cloneflag;
2784 }
2785 free(ns);
2786
2787 return 0;
2788}
2789
70fd7fc9
CB
2790static int set_config_time_offset_boot(const char *key, const char *value,
2791 struct lxc_conf *lxc_conf, void *data)
2792{
2793 int ret;
2794 char *unit;
2795 int64_t offset = 0;
2796 char buf[STRLITERALLEN("ms") + 1];
2797
2798 if (lxc_config_value_empty(value))
2799 return clr_config_time_offset_boot(key, lxc_conf, data);
2800
2801 ret = lxc_safe_int64_residual(value, &offset, 10, buf, sizeof(buf));
2802 if (ret)
2803 return ret;
2804
70fd7fc9 2805 unit = lxc_trim_whitespace_in_place(buf);
07f89c1e
CB
2806 if (strcmp(unit, "h") == 0) {
2807 if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_boot))
2808 return -EOVERFLOW;
2809 } else if (strcmp(unit, "m") == 0) {
2810 if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_boot))
2811 return -EOVERFLOW;
2812 } else if (strcmp(unit, "s") == 0) {
70fd7fc9 2813 lxc_conf->timens.s_boot = offset;
07f89c1e
CB
2814 } else if (strcmp(unit, "ms") == 0) {
2815 if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_boot))
2816 return -EOVERFLOW;
2817 } else if (strcmp(unit, "us") == 0) {
2818 if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_boot))
2819 return -EOVERFLOW;
2820 } else if (strcmp(unit, "ns") == 0) {
70fd7fc9 2821 lxc_conf->timens.ns_boot = offset;
07f89c1e 2822 } else {
70fd7fc9 2823 return ret_errno(EINVAL);
07f89c1e 2824 }
70fd7fc9
CB
2825
2826 return 0;
2827}
2828
2829static int set_config_time_offset_monotonic(const char *key, const char *value,
2830 struct lxc_conf *lxc_conf, void *data)
2831{
2832 int ret;
2833 char *unit;
2834 int64_t offset = 0;
2835 char buf[STRLITERALLEN("ms") + 1];
2836
2837 if (lxc_config_value_empty(value))
2838 return clr_config_time_offset_monotonic(key, lxc_conf, data);
2839
2840 ret = lxc_safe_int64_residual(value, &offset, 10, buf, sizeof(buf));
2841 if (ret)
2842 return ret;
2843
70fd7fc9 2844 unit = lxc_trim_whitespace_in_place(buf);
07f89c1e
CB
2845 if (strcmp(unit, "h") == 0) {
2846 if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_monotonic))
2847 return -EOVERFLOW;
2848 } else if (strcmp(unit, "m") == 0) {
2849 if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_monotonic))
2850 return -EOVERFLOW;
2851 } else if (strcmp(unit, "s") == 0) {
70fd7fc9 2852 lxc_conf->timens.s_monotonic = offset;
07f89c1e
CB
2853 } else if (strcmp(unit, "ms") == 0) {
2854 if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_monotonic))
2855 return -EOVERFLOW;
2856 } else if (strcmp(unit, "us") == 0) {
2857 if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_monotonic))
2858 return -EOVERFLOW;
2859 } else if (strcmp(unit, "ns") == 0) {
70fd7fc9 2860 lxc_conf->timens.ns_monotonic = offset;
07f89c1e 2861 } else {
70fd7fc9 2862 return ret_errno(EINVAL);
07f89c1e 2863 }
70fd7fc9
CB
2864
2865 return 0;
2866}
2867
b074bbf1
CB
2868static int set_config_namespace_share(const char *key, const char *value,
2869 struct lxc_conf *lxc_conf, void *data)
28d9e29e
CB
2870{
2871 int ns_idx;
2872 const char *namespace;
2873
2874 if (lxc_config_value_empty(value))
b074bbf1 2875 return clr_config_namespace_share(key, lxc_conf, data);
28d9e29e 2876
6333c915 2877 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
2878 ns_idx = lxc_namespace_2_ns_idx(namespace);
2879 if (ns_idx < 0)
2880 return ns_idx;
2881
b074bbf1 2882 return set_config_string_item(&lxc_conf->ns_share[ns_idx], value);
28d9e29e
CB
2883}
2884
6b0d5538
SH
2885struct parse_line_conf {
2886 struct lxc_conf *conf;
2887 bool from_include;
2888};
4184c3e1 2889
7a7ff0c6 2890static int parse_line(char *buffer, void *data)
c2cc9f0a 2891{
504a2217 2892 char *dot, *key, *line, *linep, *value;
3b13691d
CB
2893 bool empty_line;
2894 struct lxc_config_t *config;
504a2217 2895 int ret = 0;
3b13691d
CB
2896 char *dup = buffer;
2897 struct parse_line_conf *plc = data;
c2cc9f0a 2898
3b13691d
CB
2899 /* If there are newlines in the config file we should keep them. */
2900 empty_line = lxc_is_line_empty(dup);
2901 if (empty_line)
2902 dup = "\n";
c2cc9f0a 2903
25a908b8
CB
2904 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2905 * modified the original string on the stack by replacing '=' by '\0'
2906 * below.
91480a0f 2907 */
3b13691d 2908 linep = line = strdup(dup);
25a908b8 2909 if (!line)
81192358 2910 return -1;
91480a0f 2911
3b13691d
CB
2912 if (!plc->from_include) {
2913 ret = append_unexp_config_line(line, plc->conf);
2914 if (ret < 0)
2915 goto on_error;
2916 }
2917
2918 if (empty_line)
327500d4 2919 goto on_error;
6b0d5538 2920
b2718c72 2921 line += lxc_char_left_gc(line, strlen(line));
476d4cf1 2922
4184c3e1
SH
2923 /* ignore comments */
2924 if (line[0] == '#')
3b13691d 2925 goto on_error;
476d4cf1 2926
6b0d5538
SH
2927 /* martian option - don't add it to the config itself */
2928 if (strncmp(line, "lxc.", 4))
3b13691d 2929 goto on_error;
4184c3e1 2930
476d4cf1 2931 ret = -1;
c2cc9f0a 2932
3b13691d 2933 dot = strchr(line, '=');
c2cc9f0a 2934 if (!dot) {
3b13691d
CB
2935 ERROR("Invalid configuration line: %s", line);
2936 goto on_error;
c2cc9f0a 2937 }
a871ff6b 2938
c2cc9f0a 2939 *dot = '\0';
2940 value = dot + 1;
2941
b2718c72 2942 key = line;
2943 key[lxc_char_right_gc(key, strlen(key))] = '\0';
c2cc9f0a 2944
b2718c72 2945 value += lxc_char_left_gc(value, strlen(value));
2946 value[lxc_char_right_gc(value, strlen(value))] = '\0';
c2cc9f0a 2947
bd878dee 2948 if (*value == '\'' || *value == '\"') {
25a908b8
CB
2949 size_t len;
2950
2951 len = strlen(value);
504a2217
CB
2952 if (len > 1 && value[len - 1] == *value) {
2953 value[len - 1] = '\0';
bd878dee
SB
2954 value++;
2955 }
2956 }
2957
300df83e 2958 config = lxc_get_config(key);
c2cc9f0a 2959 if (!config) {
25a908b8 2960 ERROR("Unknown configuration key \"%s\"", key);
3b13691d 2961 goto on_error;
c2cc9f0a 2962 }
2963
bbc079cf 2964 ret = config->set(key, value, plc->conf, NULL);
91480a0f 2965
3b13691d 2966on_error:
81192358 2967 free(linep);
47903908 2968
91480a0f 2969 return ret;
c2cc9f0a 2970}
2971
d899f11b 2972static struct new_config_item *parse_new_conf_line(char *buffer)
2973{
2974 char *dot, *key, *line, *linep, *value;
2975 int ret = 0;
2976 char *dup = buffer;
2977 struct new_config_item *new = NULL;
2978
2979 linep = line = strdup(dup);
2980 if (!line)
2981 return NULL;
2982
2983 line += lxc_char_left_gc(line, strlen(line));
2984
2985 /* martian option - don't add it to the config itself */
85d49873 2986 if (strncmp(line, "lxc.", 4))
d899f11b 2987 goto on_error;
2988
2989 ret = -1;
2990 dot = strchr(line, '=');
2991 if (!dot) {
2992 ERROR("Invalid configuration item: %s", line);
2993 goto on_error;
2994 }
2995
2996 *dot = '\0';
2997 value = dot + 1;
2998
2999 key = line;
3000 key[lxc_char_right_gc(key, strlen(key))] = '\0';
3001
3002 value += lxc_char_left_gc(value, strlen(value));
3003 value[lxc_char_right_gc(value, strlen(value))] = '\0';
3004
3005 if (*value == '\'' || *value == '\"') {
3006 size_t len;
3007
3008 len = strlen(value);
3009 if (len > 1 && value[len - 1] == *value) {
3010 value[len - 1] = '\0';
3011 value++;
3012 }
3013 }
3014
3015 ret = -1;
3016 new = malloc(sizeof(struct new_config_item));
3017 if (!new)
3018 goto on_error;
3019
3020 new->key = strdup(key);
3021 new->val = strdup(value);
3022 if (!new->val || !new->key)
3023 goto on_error;
3024
3025 ret = 0;
3026
3027on_error:
3028 free(linep);
3029
3030 if (ret < 0 && new) {
3031 free(new->key);
3032 free(new->val);
3033 free(new);
3034 new = NULL;
3035 }
3036
3037 return new;
3038}
3039
6b0d5538 3040int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
c2cc9f0a 3041{
6b0d5538
SH
3042 struct parse_line_conf c;
3043
3044 c.conf = conf;
3045 c.from_include = from_include;
f979ac15 3046
25a908b8 3047 /* Catch only the top level config file name in the structure. */
504a2217 3048 if (!conf->rcfile)
76d0127f 3049 conf->rcfile = strdup(file);
f979ac15 3050
9994d140 3051 return lxc_file_for_each_line_mmap(file, parse_line, &c);
c2cc9f0a 3052}
62e46035 3053
504a2217 3054int lxc_config_define_add(struct lxc_list *defines, char *arg)
62e46035
CLG
3055{
3056 struct lxc_list *dent;
3057
3058 dent = malloc(sizeof(struct lxc_list));
3059 if (!dent)
3060 return -1;
3061
d899f11b 3062 dent->elem = parse_new_conf_line(arg);
3063 if (!dent->elem) {
3064 free(dent);
3065 return -1;
3066 }
3067
62e46035 3068 lxc_list_add_tail(defines, dent);
47903908 3069
62e46035
CLG
3070 return 0;
3071}
3072
eb0c9382 3073bool lxc_config_define_load(struct lxc_list *defines, struct lxc_container *c)
62e46035 3074{
eb0c9382 3075 struct lxc_list *it;
3076 bool bret = true;
62e46035
CLG
3077
3078 lxc_list_for_each(it, defines) {
eb0c9382 3079 struct new_config_item *new_item = it->elem;
3080 bret = c->set_config_item(c, new_item->key, new_item->val);
3081 if (!bret)
62e46035
CLG
3082 break;
3083 }
3084
eb0c9382 3085 lxc_config_define_free(defines);
47903908 3086
eb0c9382 3087 return bret;
3088}
3089
3090void lxc_config_define_free(struct lxc_list *defines)
3091{
3092 struct lxc_list *it, *next;
3093
9ebb03ad 3094 lxc_list_for_each_safe(it, defines, next) {
eb0c9382 3095 struct new_config_item *new_item = it->elem;
3096 free(new_item->key);
3097 free(new_item->val);
62e46035
CLG
3098 lxc_list_del(it);
3099 free(it);
3100 }
62e46035 3101}
525f0002
CS
3102
3103signed long lxc_config_parse_arch(const char *arch)
3104{
504a2217
CB
3105#if HAVE_SYS_PERSONALITY_H
3106 size_t i;
525f0002
CS
3107 struct per_name {
3108 char *name;
3109 unsigned long per;
bb8d8207 3110 } pername[] = {
8f3e3c13
CB
3111 { "arm", PER_LINUX32 },
3112 { "armel", PER_LINUX32 },
3113 { "armhf", PER_LINUX32 },
3114 { "armv7l", PER_LINUX32 },
3115 { "athlon", PER_LINUX32 },
504a2217
CB
3116 { "i386", PER_LINUX32 },
3117 { "i486", PER_LINUX32 },
3118 { "i586", PER_LINUX32 },
3119 { "i686", PER_LINUX32 },
8f3e3c13 3120 { "linux32", PER_LINUX32 },
504a2217
CB
3121 { "mips", PER_LINUX32 },
3122 { "mipsel", PER_LINUX32 },
3123 { "ppc", PER_LINUX32 },
504a2217 3124 { "powerpc", PER_LINUX32 },
8f3e3c13 3125 { "x86", PER_LINUX32 },
504a2217 3126 { "amd64", PER_LINUX },
8f3e3c13
CB
3127 { "arm64", PER_LINUX },
3128 { "linux64", PER_LINUX },
504a2217
CB
3129 { "mips64", PER_LINUX },
3130 { "mips64el", PER_LINUX },
3131 { "ppc64", PER_LINUX },
504a2217 3132 { "ppc64el", PER_LINUX },
8f3e3c13 3133 { "ppc64le", PER_LINUX },
504a2217
CB
3134 { "powerpc64", PER_LINUX },
3135 { "s390x", PER_LINUX },
8f3e3c13 3136 { "x86_64", PER_LINUX },
525f0002
CS
3137 };
3138 size_t len = sizeof(pername) / sizeof(pername[0]);
3139
8f3e3c13 3140 for (i = 0; i < len; i++)
525f0002 3141 if (!strcmp(pername[i].name, arch))
504a2217 3142 return pername[i].per;
504a2217 3143#endif
525f0002
CS
3144
3145 return -1;
3146}
72d0e1cb 3147
eb0c9382 3148int lxc_fill_elevated_privileges(char *flaglist, int *flags)
3149{
62dd965e 3150 char *token;
eb0c9382 3151 int i, aflag;
3152 struct {
3153 const char *token;
3154 int flag;
3155 } all_privs[] = {
3156 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP },
3157 { "CAP", LXC_ATTACH_DROP_CAPABILITIES },
3158 { "LSM", LXC_ATTACH_LSM_EXEC },
3159 { NULL, 0 }
3160 };
3161
3162 if (!flaglist) {
3163 /* For the sake of backward compatibility, drop all privileges
3164 * if none is specified.
3165 */
3166 for (i = 0; all_privs[i].token; i++)
3167 *flags |= all_privs[i].flag;
3168
3169 return 0;
3170 }
3171
62dd965e 3172 lxc_iterate_parts(token, flaglist, "|") {
eb0c9382 3173 aflag = -1;
3174
3175 for (i = 0; all_privs[i].token; i++)
3176 if (!strcmp(all_privs[i].token, token))
3177 aflag = all_privs[i].flag;
3178
3179 if (aflag < 0)
3180 return -1;
3181
3182 *flags |= aflag;
eb0c9382 3183 }
3184
3185 return 0;
3186}
3187
504a2217 3188/* Write out a configuration file. */
0e1a60b0 3189int write_config(int fd, const struct lxc_conf *conf)
72d0e1cb 3190{
6b0d5538 3191 int ret;
0e1a60b0 3192 size_t len = conf->unexpanded_len;
72d0e1cb 3193
0e1a60b0
CB
3194 if (len == 0)
3195 return 0;
504a2217 3196
0e1a60b0
CB
3197 ret = lxc_write_nointr(fd, conf->unexpanded_config, len);
3198 if (ret < 0) {
25a908b8 3199 SYSERROR("Failed to write configuration file");
0e1a60b0
CB
3200 return -1;
3201 }
3202
3203 return 0;
6b0d5538 3204}
f979ac15 3205
504a2217
CB
3206bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key,
3207 const char *v)
6b0d5538 3208{
0cacddda 3209 __do_free char *tmp = NULL;
6b0d5538 3210 int ret;
151d2da2 3211 size_t len;
4184c3e1 3212
151d2da2 3213 len = strlen(key) + strlen(v) + 4;
f01d0358 3214 tmp = must_realloc(NULL, len);
151d2da2 3215
663e9916 3216 if (lxc_config_value_empty(v))
151d2da2
CB
3217 ret = snprintf(tmp, len, "%s =", key);
3218 else
3219 ret = snprintf(tmp, len, "%s = %s", key, v);
6b0d5538
SH
3220 if (ret < 0 || ret >= len)
3221 return false;
3222
3223 /* Save the line verbatim into unexpanded_conf */
3224 if (append_unexp_config_line(tmp, conf))
3225 return false;
3226
3227 return true;
3228}
3229
504a2217
CB
3230void clear_unexp_config_line(struct lxc_conf *conf, const char *key,
3231 bool rm_subkeys)
6b0d5538 3232{
504a2217
CB
3233 char *lend;
3234 char *lstart = conf->unexpanded_config;
6b0d5538
SH
3235
3236 if (!conf->unexpanded_config)
3237 return;
504a2217 3238
6b0d5538
SH
3239 while (*lstart) {
3240 lend = strchr(lstart, '\n');
3241 char v;
47903908 3242
6b0d5538
SH
3243 if (!lend)
3244 lend = lstart + strlen(lstart);
3245 else
3246 lend++;
47903908 3247
6b0d5538
SH
3248 if (strncmp(lstart, key, strlen(key)) != 0) {
3249 lstart = lend;
3250 continue;
5f62730e 3251 }
47903908 3252
6b0d5538
SH
3253 if (!rm_subkeys) {
3254 v = lstart[strlen(key)];
3255 if (!isspace(v) && v != '=') {
3256 lstart = lend;
3257 continue;
3258 }
5f62730e 3259 }
47903908 3260
6b0d5538 3261 conf->unexpanded_len -= (lend - lstart);
47903908 3262
6b0d5538
SH
3263 if (*lend == '\0') {
3264 *lstart = '\0';
3265 return;
fd986e08 3266 }
47903908 3267
504a2217 3268 memmove(lstart, lend, strlen(lend) + 1);
fd986e08 3269 }
6b0d5538
SH
3270}
3271
329b3625
CB
3272bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
3273 const char *newpath, const char *oldname,
3274 const char *newname, const char *ovldir)
3275{
f01d0358
CB
3276 __do_free char *newdir = NULL,
3277 *olddir = NULL;
329b3625 3278 int ret;
f01d0358 3279 char *lend, *p, *q;
504a2217 3280 size_t newdirlen, olddirlen;
329b3625 3281 char *lstart = conf->unexpanded_config;
504a2217 3282 const char *key = "lxc.mount.entry";
329b3625 3283
504a2217 3284 olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
f01d0358 3285 olddir = must_realloc(NULL, olddirlen + 1);
504a2217
CB
3286 ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath,
3287 oldname);
25a908b8 3288 if (ret < 0 || ret >= olddirlen + 1)
329b3625 3289 return false;
504a2217
CB
3290
3291 newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
f01d0358 3292 newdir = must_realloc(NULL, newdirlen + 1);
504a2217
CB
3293 ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath,
3294 newname);
25a908b8 3295 if (ret < 0 || ret >= newdirlen + 1)
329b3625 3296 return false;
504a2217 3297
329b3625
CB
3298 if (!conf->unexpanded_config)
3299 return true;
504a2217 3300
329b3625
CB
3301 while (*lstart) {
3302 lend = strchr(lstart, '\n');
3303 if (!lend)
3304 lend = lstart + strlen(lstart);
3305 else
3306 lend++;
504a2217 3307
329b3625 3308 if (strncmp(lstart, key, strlen(key)) != 0)
504a2217
CB
3309 goto next;
3310
329b3625
CB
3311 p = strchr(lstart + strlen(key), '=');
3312 if (!p)
504a2217 3313 goto next;
329b3625 3314 p++;
504a2217 3315
329b3625
CB
3316 while (isblank(*p))
3317 p++;
504a2217 3318
329b3625 3319 if (p >= lend)
504a2217
CB
3320 goto next;
3321
12e6ab5d
CB
3322 /* Whenever a lxc.mount.entry entry is found in a line we check
3323 * if the substring "overlay" is present before doing any
3324 * further work. We check for "overlay" because substrings need
3325 * to have at least one space before them in a valid overlay
504a2217
CB
3326 * lxc.mount.entry (/A B overlay). When the space before is
3327 * missing it is very likely that these substrings are part of a
3328 * path or something else. (Checking q >= lend ensures that we
3329 * only count matches in the current line.) */
12e6ab5d
CB
3330 q = strstr(p, " overlay");
3331 if (!q || q >= lend)
504a2217
CB
3332 goto next;
3333
329b3625 3334 if (!(q = strstr(p, olddir)) || (q >= lend))
504a2217 3335 goto next;
329b3625
CB
3336
3337 /* replace the olddir with newdir */
3338 if (olddirlen >= newdirlen) {
3339 size_t diff = olddirlen - newdirlen;
3340 memcpy(q, newdir, newdirlen);
47903908 3341
329b3625
CB
3342 if (olddirlen != newdirlen) {
3343 memmove(q + newdirlen, q + newdirlen + diff,
47903908 3344 strlen(q) - newdirlen - diff + 1);
329b3625
CB
3345 lend -= diff;
3346 conf->unexpanded_len -= diff;
3347 }
3348 } else {
3349 char *new;
3350 size_t diff = newdirlen - olddirlen;
3351 size_t oldlen = conf->unexpanded_len;
3352 size_t newlen = oldlen + diff;
3353 size_t poffset = q - conf->unexpanded_config;
504a2217 3354
329b3625 3355 new = realloc(conf->unexpanded_config, newlen + 1);
25a908b8 3356 if (!new)
329b3625 3357 return false;
25a908b8 3358
329b3625
CB
3359 conf->unexpanded_len = newlen;
3360 conf->unexpanded_alloced = newlen + 1;
3361 new[newlen - 1] = '\0';
3362 lend = new + (lend - conf->unexpanded_config);
47903908 3363
25a908b8 3364 /* Move over the remainder to make room for the newdir.
504a2217 3365 */
329b3625 3366 memmove(new + poffset + newdirlen,
47903908 3367 new + poffset + olddirlen,
3368 oldlen - poffset - olddirlen + 1);
329b3625 3369 conf->unexpanded_config = new;
47903908 3370
329b3625
CB
3371 memcpy(new + poffset, newdir, newdirlen);
3372 lend += diff;
3373 }
47903908 3374
504a2217
CB
3375 next:
3376 lstart = lend;
329b3625 3377 }
504a2217 3378
329b3625
CB
3379 return true;
3380}
3381
67702c21 3382bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
d546aa0e
CB
3383 const char *newpath, const char *oldname,
3384 const char *newname)
6b0d5538 3385{
f01d0358
CB
3386 __do_free char *newdir = NULL,
3387 *olddir = NULL;
67702c21 3388 int ret;
f01d0358 3389 char *lend, *p;
504a2217
CB
3390 char *lstart = conf->unexpanded_config;
3391 size_t newdirlen, olddirlen;
3392 const char *key = "lxc.hook";
67702c21 3393
504a2217 3394 olddirlen = strlen(oldpath) + strlen(oldname) + 1;
f01d0358 3395 olddir = must_realloc(NULL, olddirlen + 1);
d546aa0e 3396 ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
25a908b8 3397 if (ret < 0 || ret >= olddirlen + 1)
67702c21 3398 return false;
504a2217
CB
3399
3400 newdirlen = strlen(newpath) + strlen(newname) + 1;
f01d0358 3401 newdir = must_realloc(NULL, newdirlen + 1);
d546aa0e 3402 ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
25a908b8 3403 if (ret < 0 || ret >= newdirlen + 1)
67702c21 3404 return false;
25a908b8 3405
67702c21
SH
3406 if (!conf->unexpanded_config)
3407 return true;
25a908b8 3408
67702c21
SH
3409 while (*lstart) {
3410 lend = strchr(lstart, '\n');
3411 if (!lend)
3412 lend = lstart + strlen(lstart);
3413 else
3414 lend++;
504a2217 3415
d546aa0e 3416 if (strncmp(lstart, key, strlen(key)) != 0)
504a2217
CB
3417 goto next;
3418
d546aa0e
CB
3419 p = strchr(lstart + strlen(key), '=');
3420 if (!p)
504a2217 3421 goto next;
67702c21 3422 p++;
504a2217 3423
67702c21
SH
3424 while (isblank(*p))
3425 p++;
504a2217
CB
3426
3427 if (p >= lend)
3428 goto next;
3429
d546aa0e 3430 if (strncmp(p, olddir, strlen(olddir)) != 0)
504a2217
CB
3431 goto next;
3432
67702c21
SH
3433 /* replace the olddir with newdir */
3434 if (olddirlen >= newdirlen) {
3435 size_t diff = olddirlen - newdirlen;
3436 memcpy(p, newdir, newdirlen);
47903908 3437
67702c21 3438 if (olddirlen != newdirlen) {
d546aa0e 3439 memmove(p + newdirlen, p + newdirlen + diff,
47903908 3440 strlen(p) - newdirlen - diff + 1);
67702c21
SH
3441 lend -= diff;
3442 conf->unexpanded_len -= diff;
3443 }
67702c21
SH
3444 } else {
3445 char *new;
3446 size_t diff = newdirlen - olddirlen;
3447 size_t oldlen = conf->unexpanded_len;
3448 size_t newlen = oldlen + diff;
3449 size_t poffset = p - conf->unexpanded_config;
504a2217 3450
d546aa0e 3451 new = realloc(conf->unexpanded_config, newlen + 1);
25a908b8 3452 if (!new)
6b0d5538 3453 return false;
25a908b8 3454
67702c21 3455 conf->unexpanded_len = newlen;
d546aa0e
CB
3456 conf->unexpanded_alloced = newlen + 1;
3457 new[newlen - 1] = '\0';
67702c21 3458 lend = new + (lend - conf->unexpanded_config);
47903908 3459
25a908b8 3460 /* Move over the remainder to make room for the newdir.
504a2217 3461 */
d546aa0e 3462 memmove(new + poffset + newdirlen,
47903908 3463 new + poffset + olddirlen,
3464 oldlen - poffset - olddirlen + 1);
67702c21 3465 conf->unexpanded_config = new;
47903908 3466
d546aa0e
CB
3467 memcpy(new + poffset, newdir, newdirlen);
3468 lend += diff;
fd986e08 3469 }
47903908 3470
504a2217
CB
3471 next:
3472 lstart = lend;
fd986e08 3473 }
504a2217 3474
6b0d5538
SH
3475 return true;
3476}
3477
504a2217
CB
3478#define DO(cmd) \
3479 { \
3480 if (!(cmd)) { \
3481 ERROR("Error writing to new config"); \
3482 return false; \
3483 } \
3484 }
6b0d5538 3485
25a908b8
CB
3486/* This is called only from clone. We wish to update all hwaddrs in the
3487 * unexpanded config file. We can't/don't want to update any which come from
504a2217
CB
3488 * lxc.includes (there shouldn't be any).
3489 * We can't just walk the c->lxc-conf->network list because that includes netifs
3490 * from the include files. So we update the ones which we find in the unexp
3491 * config file, then find the original macaddr in the conf->network, and update
3492 * that to the same value.
67702c21
SH
3493 */
3494bool network_new_hwaddrs(struct lxc_conf *conf)
6b0d5538 3495{
504a2217 3496 char *lend, *p, *p2;
6b0d5538 3497 struct lxc_list *it;
504a2217 3498 char *lstart = conf->unexpanded_config;
6b0d5538 3499
67702c21
SH
3500 if (!conf->unexpanded_config)
3501 return true;
091045f8 3502
67702c21
SH
3503 while (*lstart) {
3504 char newhwaddr[18], oldhwaddr[17];
091045f8 3505
67702c21
SH
3506 lend = strchr(lstart, '\n');
3507 if (!lend)
3508 lend = lstart + strlen(lstart);
3509 else
3510 lend++;
091045f8 3511
3db41a6c 3512 if (!lxc_config_net_is_hwaddr(lstart)) {
67702c21
SH
3513 lstart = lend;
3514 continue;
72d0e1cb 3515 }
091045f8 3516
4a787c27 3517 p = strchr(lstart, '=');
67702c21
SH
3518 if (!p) {
3519 lstart = lend;
3520 continue;
72d0e1cb 3521 }
091045f8 3522
67702c21
SH
3523 p++;
3524 while (isblank(*p))
3525 p++;
3526 if (!*p)
3527 return true;
091045f8 3528
67702c21
SH
3529 p2 = p;
3530 while (*p2 && !isblank(*p2) && *p2 != '\n')
3531 p2++;
504a2217
CB
3532
3533 if ((p2 - p) != 17) {
67702c21
SH
3534 WARN("Bad hwaddr entry");
3535 lstart = lend;
3536 continue;
72d0e1cb 3537 }
091045f8 3538
67702c21 3539 memcpy(oldhwaddr, p, 17);
091045f8
CB
3540
3541 if (!new_hwaddr(newhwaddr))
3542 return false;
3543
67702c21 3544 memcpy(p, newhwaddr, 17);
25a908b8 3545 lxc_list_for_each(it, &conf->network) {
67702c21 3546 struct lxc_netdev *n = it->elem;
25a908b8 3547
67702c21
SH
3548 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
3549 memcpy(n->hwaddr, newhwaddr, 17);
72d0e1cb 3550 }
67702c21
SH
3551
3552 lstart = lend;
72d0e1cb 3553 }
091045f8 3554
6b0d5538 3555 return true;
72d0e1cb 3556}
8796becf 3557
713046e3 3558static int set_config_ephemeral(const char *key, const char *value,
c7e27aaf 3559 struct lxc_conf *lxc_conf, void *data)
8796becf 3560{
663e9916 3561 if (lxc_config_value_empty(value)) {
3c6cf53a 3562 lxc_conf->ephemeral = 0;
78304622 3563 return 0;
3c6cf53a 3564 }
78304622 3565
66ffdb1a
CB
3566 if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0)
3567 return -1;
8796becf 3568
25a908b8 3569 if (lxc_conf->ephemeral > 1)
8796becf 3570 return -1;
8796becf
CB
3571
3572 return 0;
3573}
3574
46cc906d 3575static int set_config_log_syslog(const char *key, const char *value,
c7e27aaf 3576 struct lxc_conf *lxc_conf, void *data)
64c57ea1 3577{
76d0127f 3578 int facility;
7ca56b84 3579
ee10a69c
CB
3580 if (lxc_conf->syslog) {
3581 free(lxc_conf->syslog);
3582 lxc_conf->syslog = NULL;
3583 }
3584
663e9916 3585 if (lxc_config_value_empty(value))
7ca56b84
CB
3586 return 0;
3587
76d0127f 3588 facility = lxc_syslog_priority_to_int(value);
25a908b8 3589 if (facility == -EINVAL)
76d0127f 3590 return -1;
64c57ea1 3591
76d0127f 3592 lxc_log_syslog(facility);
47903908 3593
713046e3 3594 return set_config_string_item(&lxc_conf->syslog, value);
64c57ea1 3595}
5a46f283 3596
713046e3 3597static int set_config_no_new_privs(const char *key, const char *value,
c7e27aaf 3598 struct lxc_conf *lxc_conf, void *data)
5a46f283 3599{
e8ec7c9e 3600 unsigned int v;
5a46f283 3601
663e9916 3602 if (lxc_config_value_empty(value)) {
cf3f8bf6 3603 lxc_conf->no_new_privs = false;
80926845 3604 return 0;
cf3f8bf6 3605 }
80926845 3606
e8ec7c9e
CB
3607 if (lxc_safe_uint(value, &v) < 0)
3608 return -1;
3609
25a908b8 3610 if (v > 1)
5a46f283 3611 return -1;
e8ec7c9e 3612
5a46f283
CB
3613 lxc_conf->no_new_privs = v ? true : false;
3614
3615 return 0;
3616}
7b992a3e
CB
3617
3618/* Callbacks to get configuration items. */
6bede808 3619static int get_config_personality(const char *key, char *retv, int inlen,
cccd2219 3620 struct lxc_conf *c, void *data)
7b992a3e
CB
3621{
3622 int fulllen = 0;
3623
3624 if (!retv)
3625 inlen = 0;
3626 else
3627 memset(retv, 0, inlen);
3628
3629#if HAVE_SYS_PERSONALITY_H
3630 int len = 0;
3631
6bede808 3632 switch (c->personality) {
7b992a3e
CB
3633 case PER_LINUX32:
3634 strprint(retv, inlen, "i686");
3635 break;
3636 case PER_LINUX:
3637 strprint(retv, inlen, "x86_64");
3638 break;
3639 default:
3640 break;
3641 }
3642#endif
3643
3644 return fulllen;
3645}
bdf91ab4 3646
232763d6
CB
3647static int get_config_pty_max(const char *key, char *retv, int inlen,
3648 struct lxc_conf *c, void *data)
bdf91ab4 3649{
e528c735 3650 return lxc_get_conf_size_t(c, retv, inlen, c->pty_max);
bdf91ab4 3651}
5485782f 3652
fe1c5887
CB
3653static int get_config_tty_max(const char *key, char *retv, int inlen,
3654 struct lxc_conf *c, void *data)
5485782f 3655{
885766f5 3656 return lxc_get_conf_size_t(c, retv, inlen, c->ttys.max);
5485782f 3657}
8015e018 3658
42e53c29 3659static int get_config_tty_dir(const char *key, char *retv, int inlen,
cccd2219 3660 struct lxc_conf *c, void *data)
8015e018 3661{
885766f5 3662 return lxc_get_conf_str(retv, inlen, c->ttys.dir);
8015e018 3663}
de1ede69 3664
953fe44f
CB
3665static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
3666 struct lxc_conf *c, void *data)
104c8e6c 3667{
6bede808 3668 return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile);
104c8e6c 3669}
d60d18c6 3670
953fe44f
CB
3671static int get_config_apparmor_allow_incomplete(const char *key, char *retv,
3672 int inlen, struct lxc_conf *c,
3673 void *data)
d60d18c6 3674{
6bede808
CB
3675 return lxc_get_conf_int(c, retv, inlen,
3676 c->lsm_aa_allow_incomplete);
d60d18c6 3677}
4203a0b5 3678
1800f924
WB
3679static int get_config_apparmor_allow_nesting(const char *key, char *retv,
3680 int inlen, struct lxc_conf *c,
3681 void *data)
3682{
3683 return lxc_get_conf_int(c, retv, inlen,
3684 c->lsm_aa_allow_nesting);
3685}
3686
3687static int get_config_apparmor_raw(const char *key, char *retv,
3688 int inlen, struct lxc_conf *c,
3689 void *data)
3690{
3691 int len;
3692 struct lxc_list *it;
3693 int fulllen = 0;
3694
3695 if (!retv)
3696 inlen = 0;
3697 else
3698 memset(retv, 0, inlen);
3699
3700 lxc_list_for_each(it, &c->lsm_aa_raw) {
3701 strprint(retv, inlen, "%s\n", (char *)it->elem);
3702 }
3703
3704 return fulllen;
3705}
3706
953fe44f
CB
3707static int get_config_selinux_context(const char *key, char *retv, int inlen,
3708 struct lxc_conf *c, void *data)
4203a0b5 3709{
6bede808 3710 return lxc_get_conf_str(retv, inlen, c->lsm_se_context);
4203a0b5 3711}
b863bf92 3712
4fef78bc
MB
3713static int get_config_selinux_context_keyring(const char *key, char *retv, int inlen,
3714 struct lxc_conf *c, void *data)
3715{
3716 return lxc_get_conf_str(retv, inlen, c->lsm_se_keyring_context);
3717}
3718
8f818a84
MB
3719static int get_config_keyring_session(const char *key, char *retv, int inlen,
3720 struct lxc_conf *c, void *data)
3721{
3722 return lxc_get_conf_bool(c, retv, inlen, c->keyring_disable_session);
3723}
3724
4fef78bc 3725
25a908b8
CB
3726/* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3727 * just the value(s) will be printed. Since there still could be more than one,
3728 * it is newline-separated.
8d47350b 3729 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
25a908b8
CB
3730 * have newlines?)
3731 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3732 * 'lxc.cgroup.subsystem.key = value' format.
b863bf92 3733 */
54860ed0
CB
3734static int __get_config_cgroup_controller(const char *key, char *retv,
3735 int inlen, struct lxc_conf *c,
3736 int version)
b863bf92 3737{
b863bf92 3738 int len;
54860ed0
CB
3739 size_t namespaced_token_len;
3740 char *global_token, *namespaced_token;
3741 struct lxc_list *it;
b863bf92
CB
3742 int fulllen = 0;
3743 bool get_all = false;
3744
3745 if (!retv)
3746 inlen = 0;
3747 else
3748 memset(retv, 0, inlen);
3749
54860ed0
CB
3750 if (version == CGROUP2_SUPER_MAGIC) {
3751 global_token = "lxc.cgroup2";
3752 namespaced_token = "lxc.cgroup2.";
6333c915 3753 namespaced_token_len = STRLITERALLEN("lxc.cgroup2.");
54860ed0
CB
3754 } else if (version == CGROUP_SUPER_MAGIC) {
3755 global_token = "lxc.cgroup";
3756 namespaced_token = "lxc.cgroup.";
6333c915 3757 namespaced_token_len = STRLITERALLEN("lxc.cgroup.");
54860ed0
CB
3758 } else {
3759 return -1;
3760 }
3761
3762 if (strcmp(key, global_token) == 0)
b863bf92 3763 get_all = true;
54860ed0
CB
3764 else if (strncmp(key, namespaced_token, namespaced_token_len) == 0)
3765 key += namespaced_token_len;
b863bf92
CB
3766 else
3767 return -1;
3768
6bede808 3769 lxc_list_for_each(it, &c->cgroup) {
b863bf92 3770 struct lxc_cgroup *cg = it->elem;
25a908b8 3771
b863bf92 3772 if (get_all) {
54860ed0
CB
3773 if (version != cg->version)
3774 continue;
3775
47903908 3776 strprint(retv, inlen, "%s.%s = %s\n", global_token,
3777 cg->subsystem, cg->value);
3778 } else if (strcmp(cg->subsystem, key) == 0) {
b863bf92
CB
3779 strprint(retv, inlen, "%s\n", cg->value);
3780 }
3781 }
3782
3783 return fulllen;
3784}
5014ff2e 3785
54860ed0
CB
3786static int get_config_cgroup_controller(const char *key, char *retv, int inlen,
3787 struct lxc_conf *c, void *data)
3788{
3789 return __get_config_cgroup_controller(key, retv, inlen, c,
3790 CGROUP_SUPER_MAGIC);
3791}
3792
3793static int get_config_cgroup2_controller(const char *key, char *retv, int inlen,
3794 struct lxc_conf *c, void *data)
3795{
3796 return __get_config_cgroup_controller(key, retv, inlen, c,
3797 CGROUP2_SUPER_MAGIC);
3798}
3799
43654d34
CB
3800static int get_config_cgroup_dir(const char *key, char *retv, int inlen,
3801 struct lxc_conf *lxc_conf, void *data)
3802{
3803 int len;
3804 int fulllen = 0;
3805
3806 if (!retv)
3807 inlen = 0;
3808 else
3809 memset(retv, 0, inlen);
3810
3811 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir);
3812
3813 return fulllen;
3814}
3815
a900cbaf
WB
3816static int get_config_cgroup_monitor_dir(const char *key, char *retv, int inlen,
3817 struct lxc_conf *lxc_conf, void *data)
3818{
3819 int len;
3820 int fulllen = 0;
3821
3822 if (!retv)
3823 inlen = 0;
3824 else
3825 memset(retv, 0, inlen);
3826
3827 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.monitor_dir);
3828
3829 return fulllen;
3830}
3831
7696c1f9
RJ
3832static int get_config_cgroup_monitor_pivot_dir(const char *key, char *retv, int inlen,
3833 struct lxc_conf *lxc_conf, void *data)
3834{
3835 int len;
3836 int fulllen = 0;
3837
3838 if (!retv)
3839 inlen = 0;
3840 else
3841 memset(retv, 0, inlen);
3842
3843 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.monitor_pivot_dir);
3844
3845 return fulllen;
3846}
3847
a900cbaf
WB
3848static int get_config_cgroup_container_dir(const char *key, char *retv,
3849 int inlen,
3850 struct lxc_conf *lxc_conf,
3851 void *data)
3852{
3853 int len;
3854 int fulllen = 0;
3855
3856 if (!retv)
3857 inlen = 0;
3858 else
3859 memset(retv, 0, inlen);
3860
3861 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.container_dir);
3862
3863 return fulllen;
3864}
3865
3866static int get_config_cgroup_container_inner_dir(const char *key, char *retv,
3867 int inlen,
3868 struct lxc_conf *lxc_conf,
3869 void *data)
3870{
3871 int len;
3872 int fulllen = 0;
3873
3874 if (!retv)
3875 inlen = 0;
3876 else
3877 memset(retv, 0, inlen);
3878
3879 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.namespace_dir);
3880
3881 return fulllen;
3882}
3883
9caee129
CB
3884static inline int get_config_cgroup_relative(const char *key, char *retv,
3885 int inlen, struct lxc_conf *lxc_conf,
3886 void *data)
76f0e2e7
CB
3887{
3888 return lxc_get_conf_int(lxc_conf, retv, inlen,
9caee129 3889 lxc_conf->cgroup_meta.relative);
76f0e2e7
CB
3890}
3891
6bede808 3892static int get_config_idmaps(const char *key, char *retv, int inlen,
cccd2219 3893 struct lxc_conf *c, void *data)
5014ff2e
CB
3894{
3895 struct lxc_list *it;
3896 int len, listlen, ret;
3897 int fulllen = 0;
3898/* "u 1000 1000000 65536"
3899 *
3900 * let's render this as
3901 *
3902 * sizeof(char)
3903 * +
3904 * sizeof(" ")
3905 * +
c77aee64 3906 * sizeof(uint32_t)
5014ff2e
CB
3907 * +
3908 * sizeof(" ")
3909 * +
c77aee64 3910 * sizeof(uint32_t)
5014ff2e
CB
3911 * +
3912 * sizeof(" ")
3913 * +
c77aee64 3914 * sizeof(uint32_t)
5014ff2e
CB
3915 * +
3916 * \0
3917 */
c77aee64 3918#define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
5014ff2e
CB
3919 char buf[__LXC_IDMAP_STR_BUF];
3920
3921 if (!retv)
3922 inlen = 0;
3923 else
3924 memset(retv, 0, inlen);
3925
6bede808 3926 listlen = lxc_list_len(&c->id_map);
c77aee64 3927 lxc_list_for_each(it, &c->id_map) {
5014ff2e
CB
3928 struct id_map *map = it->elem;
3929 ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu",
3930 (map->idtype == ID_TYPE_UID) ? 'u' : 'g',
3931 map->nsid, map->hostid, map->range);
3932 if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF)
3933 return -1;
3934
3935 strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : "");
3936 }
47903908 3937
5014ff2e
CB
3938 return fulllen;
3939}
b29b29be 3940
46cc906d 3941static int get_config_log_level(const char *key, char *retv, int inlen,
cccd2219 3942 struct lxc_conf *c, void *data)
b29b29be
CB
3943{
3944 const char *v;
6bede808 3945 v = lxc_log_priority_to_string(c->loglevel);
b29b29be
CB
3946 return lxc_get_conf_str(retv, inlen, v);
3947}
3d4630ab 3948
46cc906d 3949static int get_config_log_file(const char *key, char *retv, int inlen,
cccd2219 3950 struct lxc_conf *c, void *data)
3d4630ab 3951{
6bede808 3952 return lxc_get_conf_str(retv, inlen, c->logfile);
3d4630ab 3953}
0d601acb 3954
47148e96
CB
3955static int get_config_mount_fstab(const char *key, char *retv, int inlen,
3956 struct lxc_conf *c, void *data)
0d601acb 3957{
6bede808 3958 return lxc_get_conf_str(retv, inlen, c->fstab);
0d601acb 3959}
43fbf8d9 3960
6bede808 3961static int get_config_mount_auto(const char *key, char *retv, int inlen,
cccd2219 3962 struct lxc_conf *c, void *data)
43fbf8d9
CB
3963{
3964 int len, fulllen = 0;
3965 const char *sep = "";
3966
3967 if (!retv)
3968 inlen = 0;
3969 else
3970 memset(retv, 0, inlen);
3971
6bede808 3972 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
43fbf8d9
CB
3973 return 0;
3974
6bede808 3975 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
43fbf8d9
CB
3976 case LXC_AUTO_PROC_MIXED:
3977 strprint(retv, inlen, "%sproc:mixed", sep);
3978 sep = " ";
3979 break;
3980 case LXC_AUTO_PROC_RW:
3981 strprint(retv, inlen, "%sproc:rw", sep);
3982 sep = " ";
3983 break;
3984 default:
3985 break;
3986 }
3987
6bede808 3988 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
43fbf8d9
CB
3989 case LXC_AUTO_SYS_RO:
3990 strprint(retv, inlen, "%ssys:ro", sep);
3991 sep = " ";
3992 break;
3993 case LXC_AUTO_SYS_RW:
3994 strprint(retv, inlen, "%ssys:rw", sep);
3995 sep = " ";
3996 break;
3997 case LXC_AUTO_SYS_MIXED:
3998 strprint(retv, inlen, "%ssys:mixed", sep);
3999 sep = " ";
4000 break;
4001 default:
4002 break;
4003 }
4004
6bede808 4005 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
43fbf8d9
CB
4006 case LXC_AUTO_CGROUP_NOSPEC:
4007 strprint(retv, inlen, "%scgroup", sep);
43fbf8d9
CB
4008 break;
4009 case LXC_AUTO_CGROUP_MIXED:
4010 strprint(retv, inlen, "%scgroup:mixed", sep);
43fbf8d9
CB
4011 break;
4012 case LXC_AUTO_CGROUP_RO:
4013 strprint(retv, inlen, "%scgroup:ro", sep);
43fbf8d9
CB
4014 break;
4015 case LXC_AUTO_CGROUP_RW:
4016 strprint(retv, inlen, "%scgroup:rw", sep);
43fbf8d9
CB
4017 break;
4018 case LXC_AUTO_CGROUP_FULL_NOSPEC:
4019 strprint(retv, inlen, "%scgroup-full", sep);
43fbf8d9
CB
4020 break;
4021 case LXC_AUTO_CGROUP_FULL_MIXED:
4022 strprint(retv, inlen, "%scgroup-full:mixed", sep);
43fbf8d9
CB
4023 break;
4024 case LXC_AUTO_CGROUP_FULL_RO:
4025 strprint(retv, inlen, "%scgroup-full:ro", sep);
43fbf8d9
CB
4026 break;
4027 case LXC_AUTO_CGROUP_FULL_RW:
4028 strprint(retv, inlen, "%scgroup-full:rw", sep);
43fbf8d9
CB
4029 break;
4030 default:
4031 break;
4032 }
4033
4034 return fulllen;
4035}
cc921848 4036
6bede808 4037static int get_config_mount(const char *key, char *retv, int inlen,
cccd2219 4038 struct lxc_conf *c, void *data)
cc921848
CB
4039{
4040 int len, fulllen = 0;
4041 struct lxc_list *it;
4042
4043 if (!retv)
4044 inlen = 0;
4045 else
4046 memset(retv, 0, inlen);
4047
25a908b8 4048 lxc_list_for_each(it, &c->mount_list) {
cc921848
CB
4049 strprint(retv, inlen, "%s\n", (char *)it->elem);
4050 }
4051
4052 return fulllen;
4053}
819114b6 4054
7a96a068
CB
4055static int get_config_rootfs_path(const char *key, char *retv, int inlen,
4056 struct lxc_conf *c, void *data)
819114b6 4057{
6bede808 4058 return lxc_get_conf_str(retv, inlen, c->rootfs.path);
819114b6 4059}
3af60359 4060
6e54330c
CB
4061static int get_config_rootfs_managed(const char *key, char *retv, int inlen,
4062 struct lxc_conf *c, void *data)
4063{
4064 return lxc_get_conf_bool(c, retv, inlen, c->rootfs.managed);
4065}
4066
6bede808 4067static int get_config_rootfs_mount(const char *key, char *retv, int inlen,
cccd2219 4068 struct lxc_conf *c, void *data)
3af60359 4069{
6bede808 4070 return lxc_get_conf_str(retv, inlen, c->rootfs.mount);
3af60359 4071}
0e9db631 4072
6bede808 4073static int get_config_rootfs_options(const char *key, char *retv, int inlen,
cccd2219 4074 struct lxc_conf *c, void *data)
0e9db631 4075{
6bede808 4076 return lxc_get_conf_str(retv, inlen, c->rootfs.options);
0e9db631 4077}
8f183f38 4078
b67771bc 4079static int get_config_uts_name(const char *key, char *retv, int inlen,
cccd2219 4080 struct lxc_conf *c, void *data)
e274f8b0
CB
4081{
4082 return lxc_get_conf_str(
4083 retv, inlen,
6bede808 4084 c->utsname ? c->utsname->nodename : NULL);
e274f8b0 4085}
466c2e93 4086
6bede808 4087static int get_config_hooks(const char *key, char *retv, int inlen,
cccd2219 4088 struct lxc_conf *c, void *data)
466c2e93
CB
4089{
4090 char *subkey;
4091 int len, fulllen = 0, found = -1;
4092 struct lxc_list *it;
4093 int i;
4094
466c2e93 4095 subkey = strchr(key, '.');
466c2e93
CB
4096 if (!subkey)
4097 return -1;
47903908 4098
4099 subkey = strchr(subkey + 1, '.');
637d38f2
CB
4100 if (!subkey)
4101 return -1;
466c2e93 4102 subkey++;
637d38f2 4103 if (*subkey == '\0')
466c2e93 4104 return -1;
47903908 4105
466c2e93
CB
4106 for (i = 0; i < NUM_LXC_HOOKS; i++) {
4107 if (strcmp(lxchook_names[i], subkey) == 0) {
4108 found = i;
4109 break;
4110 }
4111 }
47903908 4112
466c2e93
CB
4113 if (found == -1)
4114 return -1;
4115
4116 if (!retv)
4117 inlen = 0;
4118 else
4119 memset(retv, 0, inlen);
4120
6bede808 4121 lxc_list_for_each(it, &c->hooks[found]) {
466c2e93
CB
4122 strprint(retv, inlen, "%s\n", (char *)it->elem);
4123 }
47903908 4124
466c2e93
CB
4125 return fulllen;
4126}
d2ceff53 4127
44ae0fb6
CB
4128static int get_config_hooks_version(const char *key, char *retv, int inlen,
4129 struct lxc_conf *c, void *data)
4130{
4131 return lxc_get_conf_int(c, retv, inlen, c->hooks_version);
4132}
4133
f9373e40
CB
4134static int get_config_net(const char *key, char *retv, int inlen,
4135 struct lxc_conf *c, void *data)
d2ceff53
CB
4136{
4137 int len, fulllen = 0;
4138 struct lxc_list *it;
4139
4140 if (!retv)
4141 inlen = 0;
4142 else
4143 memset(retv, 0, inlen);
4144
6bede808 4145 lxc_list_for_each(it, &c->network) {
d2ceff53
CB
4146 struct lxc_netdev *n = it->elem;
4147 const char *t = lxc_net_type_to_str(n->type);
4148 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
4149 }
4150
4151 return fulllen;
4152}
bdccff60 4153
6bede808 4154static int get_config_cap_drop(const char *key, char *retv, int inlen,
cccd2219 4155 struct lxc_conf *c, void *data)
1c96d6d8
CB
4156{
4157 int len, fulllen = 0;
4158 struct lxc_list *it;
4159
4160 if (!retv)
4161 inlen = 0;
4162 else
4163 memset(retv, 0, inlen);
4164
6bede808 4165 lxc_list_for_each(it, &c->caps) {
b80927f2
CB
4166 strprint(retv, inlen, "%s\n", (char *)it->elem);
4167 }
25a908b8 4168
b80927f2
CB
4169 return fulllen;
4170}
4171
6bede808 4172static int get_config_cap_keep(const char *key, char *retv, int inlen,
cccd2219 4173 struct lxc_conf *c, void *data)
b80927f2
CB
4174{
4175 int len, fulllen = 0;
4176 struct lxc_list *it;
4177
4178 if (!retv)
4179 inlen = 0;
4180 else
4181 memset(retv, 0, inlen);
4182
6bede808 4183 lxc_list_for_each(it, &c->keepcaps) {
1c96d6d8
CB
4184 strprint(retv, inlen, "%s\n", (char *)it->elem);
4185 }
25a908b8 4186
1c96d6d8
CB
4187 return fulllen;
4188}
0692663a 4189
3aed4934
CB
4190static int get_config_console_path(const char *key, char *retv, int inlen,
4191 struct lxc_conf *c, void *data)
0692663a 4192{
6bede808 4193 return lxc_get_conf_str(retv, inlen, c->console.path);
0692663a 4194}
794d1c06 4195
6bede808 4196static int get_config_console_logfile(const char *key, char *retv, int inlen,
cccd2219 4197 struct lxc_conf *c, void *data)
794d1c06 4198{
6bede808 4199 return lxc_get_conf_str(retv, inlen, c->console.log_path);
794d1c06 4200}
75f55b1f 4201
d91adfa6
CB
4202static int get_config_console_rotate(const char *key, char *retv, int inlen,
4203 struct lxc_conf *c, void *data)
4204{
4205 return lxc_get_conf_int(c, retv, inlen, c->console.log_rotate);
4206}
4207
4208
28f3b1cd
CB
4209static int get_config_console_buffer_size(const char *key, char *retv,
4210 int inlen, struct lxc_conf *c,
4211 void *data)
a04220de 4212{
dcea90a0 4213 return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size);
a04220de
CB
4214}
4215
861813e5
CB
4216static int get_config_console_size(const char *key, char *retv, int inlen,
4217 struct lxc_conf *c, void *data)
4218{
4219 return lxc_get_conf_uint64(c, retv, inlen, c->console.log_size);
4220}
4221
50d86993
CB
4222static int get_config_seccomp_allow_nesting(const char *key, char *retv,
4223 int inlen, struct lxc_conf *c,
4224 void *data)
4225{
c3e3c21a
CB
4226#ifdef HAVE_SECCOMP
4227 return lxc_get_conf_int(c, retv, inlen, c->seccomp.allow_nesting);
4228#else
4229 errno = ENOSYS;
4230 return -1;
4231#endif
50d86993 4232}
861813e5 4233
84cf6d25
WB
4234static int get_config_seccomp_notify_cookie(const char *key, char *retv, int inlen,
4235 struct lxc_conf *c, void *data)
4236{
4237#ifdef HAVE_SECCOMP_NOTIFY
4238 return lxc_get_conf_str(retv, inlen, c->seccomp.notifier.cookie);
4239#else
21fce08c 4240 return ret_set_errno(-1, ENOSYS);
84cf6d25
WB
4241#endif
4242}
4243
86ce1da1
CB
4244static int get_config_seccomp_notify_proxy(const char *key, char *retv, int inlen,
4245 struct lxc_conf *c, void *data)
4246{
c3e3c21a 4247#ifdef HAVE_SECCOMP_NOTIFY
86ce1da1 4248 return lxc_get_conf_str(retv, inlen,
c3e3c21a
CB
4249 (c->seccomp.notifier.proxy_addr.sun_path[0]) == '/'
4250 ? &c->seccomp.notifier.proxy_addr.sun_path[0]
4251 : &c->seccomp.notifier.proxy_addr.sun_path[1]);
86ce1da1 4252#else
21fce08c 4253 return ret_set_errno(-1, ENOSYS);
86ce1da1
CB
4254#endif
4255}
4256
0b427da0
CB
4257static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
4258 struct lxc_conf *c, void *data)
75f55b1f 4259{
c3e3c21a 4260 return lxc_get_conf_str(retv, inlen, c->seccomp.seccomp);
75f55b1f 4261}
97f6dad0 4262
6bede808 4263static int get_config_autodev(const char *key, char *retv, int inlen,
cccd2219 4264 struct lxc_conf *c, void *data)
97f6dad0 4265{
6bede808 4266 return lxc_get_conf_int(c, retv, inlen, c->autodev);
97f6dad0 4267}
afee4324 4268
63012bdd
CK
4269static int get_config_autodev_tmpfs_size(const char *key, char *retv, int inlen,
4270 struct lxc_conf *c, void *data)
4271{
4272 return lxc_get_conf_int(c, retv, inlen, c->autodevtmpfssize);
4273}
4274
55c84efc 4275static int get_config_signal_halt(const char *key, char *retv, int inlen,
25a908b8 4276 struct lxc_conf *c, void *data)
afee4324 4277{
6bede808 4278 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
afee4324 4279}
3aa8f359 4280
55c84efc 4281static int get_config_signal_reboot(const char *key, char *retv, int inlen,
25a908b8 4282 struct lxc_conf *c, void *data)
3aa8f359 4283{
6bede808 4284 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
3aa8f359 4285}
2e16269f 4286
55c84efc 4287static int get_config_signal_stop(const char *key, char *retv, int inlen,
25a908b8 4288 struct lxc_conf *c, void *data)
2e16269f 4289{
6bede808 4290 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
2e16269f 4291}
54345299 4292
6bede808 4293static int get_config_start(const char *key, char *retv, int inlen,
cccd2219 4294 struct lxc_conf *c, void *data)
54345299 4295{
c6182222 4296 if (strcmp(key + 10, "auto") == 0)
6bede808 4297 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
c6182222 4298 else if (strcmp(key + 10, "delay") == 0)
6bede808 4299 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
c6182222 4300 else if (strcmp(key + 10, "order") == 0)
6bede808 4301 return lxc_get_conf_int(c, retv, inlen, c->start_order);
54345299
CB
4302
4303 return -1;
4304}
de9df15e 4305
46cc906d 4306static int get_config_log_syslog(const char *key, char *retv, int inlen,
25a908b8 4307 struct lxc_conf *c, void *data)
de9df15e 4308{
6bede808 4309 return lxc_get_conf_str(retv, inlen, c->syslog);
de9df15e 4310}
ac0f949c 4311
6bede808 4312static int get_config_monitor(const char *key, char *retv, int inlen,
cccd2219 4313 struct lxc_conf *c, void *data)
ac0f949c 4314{
6bede808 4315 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
ac0f949c 4316}
90ec7b6e 4317
258f8051
CB
4318static int get_config_monitor_signal_pdeath(const char *key, char *retv,
4319 int inlen, struct lxc_conf *c,
4320 void *data)
4321{
4322 return lxc_get_conf_int(c, retv, inlen, c->monitor_signal_pdeath);
4323}
4324
6bede808 4325static int get_config_group(const char *key, char *retv, int inlen,
cccd2219 4326 struct lxc_conf *c, void *data)
90ec7b6e
CB
4327{
4328 int len, fulllen = 0;
4329 struct lxc_list *it;
4330
4331 if (!retv)
4332 inlen = 0;
4333 else
4334 memset(retv, 0, inlen);
4335
6bede808 4336 lxc_list_for_each(it, &c->groups) {
90ec7b6e
CB
4337 strprint(retv, inlen, "%s\n", (char *)it->elem);
4338 }
25a908b8 4339
90ec7b6e
CB
4340 return fulllen;
4341}
aa0db7d3 4342
6bede808 4343static int get_config_environment(const char *key, char *retv, int inlen,
cccd2219 4344 struct lxc_conf *c, void *data)
aa0db7d3
CB
4345{
4346 int len, fulllen = 0;
4347 struct lxc_list *it;
4348
4349 if (!retv)
4350 inlen = 0;
4351 else
4352 memset(retv, 0, inlen);
4353
6bede808 4354 lxc_list_for_each(it, &c->environment) {
aa0db7d3
CB
4355 strprint(retv, inlen, "%s\n", (char *)it->elem);
4356 }
25a908b8 4357
aa0db7d3
CB
4358 return fulllen;
4359}
96dfcb7d 4360
5cda27c1
SH
4361static int get_config_execute_cmd(const char *key, char *retv, int inlen,
4362 struct lxc_conf *c, void *data)
4363{
4364 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
4365}
4366
6bede808 4367static int get_config_init_cmd(const char *key, char *retv, int inlen,
cccd2219 4368 struct lxc_conf *c, void *data)
96dfcb7d 4369{
6bede808 4370 return lxc_get_conf_str(retv, inlen, c->init_cmd);
96dfcb7d 4371}
1398e9b1 4372
3c491553
L
4373static int get_config_init_cwd(const char *key, char *retv, int inlen,
4374 struct lxc_conf *c, void *data)
4375{
4376 return lxc_get_conf_str(retv, inlen, c->init_cwd);
4377}
4378
6bede808 4379static int get_config_init_uid(const char *key, char *retv, int inlen,
cccd2219 4380 struct lxc_conf *c, void *data)
1398e9b1 4381{
6bede808 4382 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
1398e9b1 4383}
dfeb7e42 4384
6bede808 4385static int get_config_init_gid(const char *key, char *retv, int inlen,
cccd2219 4386 struct lxc_conf *c, void *data)
dfeb7e42 4387{
6bede808 4388 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
dfeb7e42 4389}
62048afe 4390
6bede808 4391static int get_config_ephemeral(const char *key, char *retv, int inlen,
cccd2219 4392 struct lxc_conf *c, void *data)
62048afe 4393{
6bede808 4394 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
62048afe 4395}
b09521ac 4396
6bede808 4397static int get_config_no_new_privs(const char *key, char *retv, int inlen,
cccd2219 4398 struct lxc_conf *c, void *data)
b09521ac 4399{
6bede808 4400 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
b09521ac 4401}
389f6466 4402
25a908b8 4403/* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
240d4b74 4404 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4405 * printed, in 'lxc.prlimit.resource = value' format.
389f6466 4406 */
240d4b74 4407static int get_config_prlimit(const char *key, char *retv, int inlen,
47903908 4408 struct lxc_conf *c, void *data)
389f6466
CB
4409{
4410 int fulllen = 0, len;
4411 bool get_all = false;
4412 struct lxc_list *it;
4413
4414 if (!retv)
4415 inlen = 0;
4416 else
4417 memset(retv, 0, inlen);
4418
240d4b74 4419 if (!strcmp(key, "lxc.prlimit"))
389f6466 4420 get_all = true;
240d4b74 4421 else if (strncmp(key, "lxc.prlimit.", 12) == 0)
4422 key += 12;
389f6466
CB
4423 else
4424 return -1;
4425
6bede808 4426 lxc_list_for_each(it, &c->limits) {
c77aee64
CB
4427 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4428 char buf[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
389f6466
CB
4429 int partlen;
4430 struct lxc_limit *lim = it->elem;
4431
4432 if (lim->limit.rlim_cur == RLIM_INFINITY) {
6333c915
CB
4433 memcpy(buf, "unlimited", STRLITERALLEN("unlimited") + 1);
4434 partlen = STRLITERALLEN("unlimited");
389f6466
CB
4435 } else {
4436 partlen = sprintf(buf, "%" PRIu64,
4437 (uint64_t)lim->limit.rlim_cur);
4438 }
47903908 4439
389f6466 4440 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
25a908b8 4441 if (lim->limit.rlim_max == RLIM_INFINITY)
389f6466 4442 memcpy(buf + partlen, ":unlimited",
6333c915 4443 STRLITERALLEN(":unlimited") + 1);
25a908b8 4444 else
389f6466
CB
4445 sprintf(buf + partlen, ":%" PRIu64,
4446 (uint64_t)lim->limit.rlim_max);
389f6466
CB
4447 }
4448
4449 if (get_all) {
240d4b74 4450 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
389f6466 4451 lim->resource, buf);
47903908 4452 } else if (strcmp(lim->resource, key) == 0) {
389f6466
CB
4453 strprint(retv, inlen, "%s", buf);
4454 }
4455 }
4456
4457 return fulllen;
4458}
e08cb901 4459
e409b214
CB
4460/* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4461 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4462 * entries will be printed, in 'lxc.sysctl.key = value' format.
7edd0540
L
4463 */
4464static int get_config_sysctl(const char *key, char *retv, int inlen,
47903908 4465 struct lxc_conf *c, void *data)
7edd0540
L
4466{
4467 int len;
4468 struct lxc_list *it;
4469 int fulllen = 0;
4470 bool get_all = false;
4471
4472 if (!retv)
4473 inlen = 0;
4474 else
4475 memset(retv, 0, inlen);
4476
4477 if (strcmp(key, "lxc.sysctl") == 0)
4478 get_all = true;
6333c915
CB
4479 else if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
4480 key += STRLITERALLEN("lxc.sysctl.");
7edd0540
L
4481 else
4482 return -1;
4483
4484 lxc_list_for_each(it, &c->sysctls) {
4485 struct lxc_sysctl *elem = it->elem;
4486 if (get_all) {
47903908 4487 strprint(retv, inlen, "lxc.sysctl.%s = %s\n", elem->key,
4488 elem->value);
7edd0540
L
4489 } else if (strcmp(elem->key, key) == 0) {
4490 strprint(retv, inlen, "%s", elem->value);
4491 }
4492 }
4493
4494 return fulllen;
4495}
4496
61d7a733 4497static int get_config_proc(const char *key, char *retv, int inlen,
47903908 4498 struct lxc_conf *c, void *data)
61d7a733
YT
4499{
4500 struct lxc_list *it;
4501 int len;
4502 int fulllen = 0;
4503 bool get_all = false;
4504
4505 if (!retv)
4506 inlen = 0;
4507 else
4508 memset(retv, 0, inlen);
4509
4510 if (strcmp(key, "lxc.proc") == 0)
4511 get_all = true;
6333c915
CB
4512 else if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
4513 key += STRLITERALLEN("lxc.proc.");
61d7a733
YT
4514 else
4515 return -1;
4516
4517 lxc_list_for_each(it, &c->procs) {
4518 struct lxc_proc *proc = it->elem;
4519
4520 if (get_all) {
4521 strprint(retv, inlen, "lxc.proc.%s = %s\n",
47903908 4522 proc->filename, proc->value);
61d7a733
YT
4523 } else if (strcmp(proc->filename, key) == 0) {
4524 strprint(retv, inlen, "%s", proc->value);
4525 }
4526 }
4527
4528 return fulllen;
4529}
4530
1d8d3676
CB
4531static int get_config_namespace_clone(const char *key, char *retv, int inlen,
4532 struct lxc_conf *c, void *data)
4533{
4534 int i, len;
4535 int fulllen = 0;
4536
4537 if (!retv)
4538 inlen = 0;
4539 else
4540 memset(retv, 0, inlen);
4541
4542 for (i = 0; i < LXC_NS_MAX; i++) {
4543 if (c->ns_clone & ns_info[i].clone_flag)
4544 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4545 }
4546
4547 return fulllen;
4548}
4549
abeb5bba
CB
4550static int get_config_namespace_keep(const char *key, char *retv, int inlen,
4551 struct lxc_conf *c, void *data)
4552{
4553 int i, len;
4554 int fulllen = 0;
4555
4556 if (!retv)
4557 inlen = 0;
4558 else
4559 memset(retv, 0, inlen);
4560
4561 for (i = 0; i < LXC_NS_MAX; i++) {
4562 if (c->ns_keep & ns_info[i].clone_flag)
4563 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4564 }
4565
4566 return fulllen;
4567}
4568
70fd7fc9
CB
4569static int get_config_time_offset_boot(const char *key, char *retv, int inlen, struct lxc_conf *c,
4570 void *data)
4571{
4572 int len;
4573 int fulllen = 0;
4574
4575 if (!retv)
4576 inlen = 0;
4577 else
4578 memset(retv, 0, inlen);
4579
4580 if (c->timens.s_boot) {
4581 strprint(retv, inlen, "%" PRId64 " s\n", c->timens.s_boot);
4582 } else {
4583 strprint(retv, inlen, "%" PRId64 " ns\n", c->timens.ns_boot);
4584 }
4585
4586 return fulllen;
4587}
4588
4589static int get_config_time_offset_monotonic(const char *key, char *retv, int inlen,
4590 struct lxc_conf *c, void *data)
4591{
4592 int len;
4593 int fulllen = 0;
4594
4595 if (!retv)
4596 inlen = 0;
4597 else
4598 memset(retv, 0, inlen);
4599
4600 if (c->timens.s_monotonic) {
4601 strprint(retv, inlen, "%" PRId64 "s\n", c->timens.s_monotonic);
4602 } else {
4603 strprint(retv, inlen, "%" PRId64 "ns\n", c->timens.ns_monotonic);
4604 }
4605
4606 return fulllen;
4607}
4608
b074bbf1
CB
4609static int get_config_namespace_share(const char *key, char *retv, int inlen,
4610 struct lxc_conf *c, void *data)
28d9e29e
CB
4611{
4612 int len, ns_idx;
4613 const char *namespace;
4614 int fulllen = 0;
4615
4616 if (!retv)
4617 inlen = 0;
4618 else
4619 memset(retv, 0, inlen);
4620
6333c915 4621 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
4622 ns_idx = lxc_namespace_2_ns_idx(namespace);
4623 if (ns_idx < 0)
4624 return ns_idx;
4625
b074bbf1 4626 strprint(retv, inlen, "%s", c->ns_share[ns_idx]);
28d9e29e
CB
4627
4628 return fulllen;
4629}
4630
e08cb901 4631/* Callbacks to clear config items. */
26471403
CB
4632static inline int clr_config_personality(const char *key, struct lxc_conf *c,
4633 void *data)
e08cb901
CB
4634{
4635 c->personality = -1;
4636 return 0;
4637}
03818ae3 4638
232763d6
CB
4639static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
4640 void *data)
03818ae3 4641{
e528c735 4642 c->pty_max = 0;
03818ae3
CB
4643 return 0;
4644}
e7a4b096 4645
fe1c5887
CB
4646static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
4647 void *data)
e7a4b096 4648{
885766f5 4649 c->ttys.tty = 0;
e7a4b096
CB
4650 return 0;
4651}
eaf8c0c7 4652
42e53c29 4653static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
26471403 4654 void *data)
eaf8c0c7 4655{
885766f5
CB
4656 free(c->ttys.dir);
4657 c->ttys.dir = NULL;
eaf8c0c7
CB
4658 return 0;
4659}
6bd86308 4660
953fe44f
CB
4661static inline int clr_config_apparmor_profile(const char *key,
4662 struct lxc_conf *c, void *data)
025718fb
CB
4663{
4664 free(c->lsm_aa_profile);
4665 c->lsm_aa_profile = NULL;
4666 return 0;
4667}
3061e04e 4668
953fe44f
CB
4669static inline int clr_config_apparmor_allow_incomplete(const char *key,
4670 struct lxc_conf *c,
4671 void *data)
3061e04e
CB
4672{
4673 c->lsm_aa_allow_incomplete = 0;
4674 return 0;
4675}
31fc3494 4676
1800f924
WB
4677static inline int clr_config_apparmor_allow_nesting(const char *key,
4678 struct lxc_conf *c,
4679 void *data)
4680{
4681 c->lsm_aa_allow_nesting = 0;
4682 return 0;
4683}
4684
4685static inline int clr_config_apparmor_raw(const char *key,
4686 struct lxc_conf *c,
4687 void *data)
4688{
4689 return lxc_clear_apparmor_raw(c);
4690}
4691
953fe44f
CB
4692static inline int clr_config_selinux_context(const char *key,
4693 struct lxc_conf *c, void *data)
31fc3494
CB
4694{
4695 free(c->lsm_se_context);
4696 c->lsm_se_context = NULL;
4697 return 0;
4698}
754d01ce 4699
4fef78bc
MB
4700static inline int clr_config_selinux_context_keyring(const char *key,
4701 struct lxc_conf *c, void *data)
4702{
4703 free(c->lsm_se_keyring_context);
4704 c->lsm_se_keyring_context = NULL;
4705 return 0;
4706}
4707
8f818a84
MB
4708static inline int clr_config_keyring_session(const char *key,
4709 struct lxc_conf *c, void *data)
4710{
4711 c->keyring_disable_session = false;
4712 return 0;
4713}
4714
43654d34
CB
4715static inline int clr_config_cgroup_controller(const char *key,
4716 struct lxc_conf *c, void *data)
754d01ce 4717{
54860ed0
CB
4718 return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC);
4719}
4720
4721static inline int clr_config_cgroup2_controller(const char *key,
4722 struct lxc_conf *c, void *data)
4723{
4724 return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC);
754d01ce 4725}
d3a178de 4726
43654d34
CB
4727static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
4728 void *data)
4729{
4730 if (lxc_conf->cgroup_meta.dir) {
4731 free(lxc_conf->cgroup_meta.dir);
4732 lxc_conf->cgroup_meta.dir = NULL;
4733 }
4734
4735 return 0;
4736}
4737
a900cbaf
WB
4738static int clr_config_cgroup_monitor_dir(const char *key,
4739 struct lxc_conf *lxc_conf,
4740 void *data)
4741{
4742 free_disarm(lxc_conf->cgroup_meta.monitor_dir);
4743 return 0;
4744}
4745
7696c1f9
RJ
4746static int clr_config_cgroup_monitor_pivot_dir(const char *key,
4747 struct lxc_conf *lxc_conf,
4748 void *data)
4749{
4750 free_disarm(lxc_conf->cgroup_meta.monitor_pivot_dir);
4751 return 0;
4752}
4753
a900cbaf
WB
4754static int clr_config_cgroup_container_dir(const char *key,
4755 struct lxc_conf *lxc_conf,
4756 void *data)
4757{
4758 free_disarm(lxc_conf->cgroup_meta.container_dir);
4759 return 0;
4760}
4761
4762static int clr_config_cgroup_container_inner_dir(const char *key,
4763 struct lxc_conf *lxc_conf,
4764 void *data)
4765{
4766 free_disarm(lxc_conf->cgroup_meta.namespace_dir);
4767 return 0;
4768}
4769
9caee129
CB
4770static inline int clr_config_cgroup_relative(const char *key,
4771 struct lxc_conf *lxc_conf,
4772 void *data)
76f0e2e7 4773{
9caee129 4774 lxc_conf->cgroup_meta.relative = false;
76f0e2e7
CB
4775 return 0;
4776}
4777
26471403
CB
4778static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
4779 void *data)
d3a178de
CB
4780{
4781 return lxc_clear_idmaps(c);
4782}
97d3338d 4783
46cc906d 4784static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
26471403 4785 void *data)
97d3338d 4786{
4b73005c 4787 c->loglevel = LXC_LOG_LEVEL_NOTSET;
97d3338d
CB
4788 return 0;
4789}
de46099c 4790
46cc906d 4791static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
26471403 4792 void *data)
de46099c
CB
4793{
4794 free(c->logfile);
4795 c->logfile = NULL;
4796 return 0;
4797}
b4fa13cd 4798
26471403
CB
4799static inline int clr_config_mount(const char *key, struct lxc_conf *c,
4800 void *data)
b4fa13cd
CB
4801{
4802 return lxc_clear_mount_entries(c);
4803}
4be81021 4804
26471403
CB
4805static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
4806 void *data)
4be81021
CB
4807{
4808 return lxc_clear_automounts(c);
4809}
350d4b15 4810
47148e96
CB
4811static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
4812 void *data)
350d4b15
CB
4813{
4814 free(c->fstab);
4815 c->fstab = NULL;
4816 return 0;
4817}
faca124d 4818
7a96a068
CB
4819static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
4820 void *data)
faca124d
CB
4821{
4822 free(c->rootfs.path);
4823 c->rootfs.path = NULL;
4824 return 0;
4825}
4826
6e54330c
CB
4827static inline int clr_config_rootfs_managed(const char *key, struct lxc_conf *c,
4828 void *data)
4829{
4830 c->rootfs.managed = true;
4831 return 0;
4832}
4833
26471403
CB
4834static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
4835 void *data)
fddefc2d
CB
4836{
4837 free(c->rootfs.mount);
4838 c->rootfs.mount = NULL;
4839 return 0;
4840}
7b1eb67d 4841
26471403
CB
4842static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
4843 void *data)
7b1eb67d
CB
4844{
4845 free(c->rootfs.options);
4846 c->rootfs.options = NULL;
3437f95c
CB
4847
4848 free(c->rootfs.data);
4849 c->rootfs.data = NULL;
4850
7b1eb67d
CB
4851 return 0;
4852}
02becb8d 4853
b67771bc 4854static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
26471403 4855 void *data)
d31d0103
CB
4856{
4857 free(c->utsname);
4858 c->utsname = NULL;
4859 return 0;
4860}
c9eeb90c 4861
26471403
CB
4862static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
4863 void *data)
c9eeb90c
CB
4864{
4865 return lxc_clear_hooks(c, key);
4866}
4867
44ae0fb6
CB
4868static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c,
4869 void *data)
4870{
4871 /* default to legacy hooks version */
4872 c->hooks_version = 0;
4873 return 0;
4874}
4875
f9373e40
CB
4876static inline int clr_config_net(const char *key, struct lxc_conf *c,
4877 void *data)
f4488271 4878{
c302b476
CB
4879 lxc_free_networks(&c->network);
4880
e5d2fd7c 4881 return 0;
f4488271
CB
4882}
4883
26471403
CB
4884static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
4885 void *data)
244cb55b
CB
4886{
4887 return lxc_clear_config_caps(c);
4888}
c74cc490 4889
26471403
CB
4890static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
4891 void *data)
c74cc490
CB
4892{
4893 return lxc_clear_config_keepcaps(c);
4894}
4e5b633f 4895
3aed4934
CB
4896static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
4897 void *data)
4e5b633f
CB
4898{
4899 free(c->console.path);
4900 c->console.path = NULL;
4901 return 0;
4902}
4903
7c2ec23a 4904static inline int clr_config_console_logfile(const char *key,
26471403 4905 struct lxc_conf *c, void *data)
7c2ec23a
CB
4906{
4907 free(c->console.log_path);
4908 c->console.log_path = NULL;
4909 return 0;
4910}
bbca37d8 4911
d91adfa6
CB
4912static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c,
4913 void *data)
4914{
4915 c->console.log_rotate = 0;
4916 return 0;
4917}
4918
28f3b1cd
CB
4919static inline int clr_config_console_buffer_size(const char *key,
4920 struct lxc_conf *c, void *data)
a04220de 4921{
28f3b1cd 4922 c->console.buffer_size = 0;
a04220de
CB
4923 return 0;
4924}
4925
861813e5
CB
4926static inline int clr_config_console_size(const char *key, struct lxc_conf *c,
4927 void *data)
4928{
4929 c->console.log_size = 0;
4930 return 0;
4931}
4932
50d86993
CB
4933static inline int clr_config_seccomp_allow_nesting(const char *key,
4934 struct lxc_conf *c, void *data)
4935{
c3e3c21a
CB
4936#ifdef HAVE_SECCOMP
4937 c->seccomp.allow_nesting = 0;
50d86993 4938 return 0;
c3e3c21a
CB
4939#else
4940 errno = ENOSYS;
4941 return -1;
4942#endif
50d86993
CB
4943}
4944
84cf6d25
WB
4945static inline int clr_config_seccomp_notify_cookie(const char *key,
4946 struct lxc_conf *c, void *data)
4947{
4948#ifdef HAVE_SECCOMP_NOTIFY
4949 free(c->seccomp.notifier.cookie);
4950 c->seccomp.notifier.cookie = NULL;
4951 return 0;
4952#else
21fce08c 4953 return ret_set_errno(-1, ENOSYS);
84cf6d25
WB
4954#endif
4955}
4956
86ce1da1
CB
4957static inline int clr_config_seccomp_notify_proxy(const char *key,
4958 struct lxc_conf *c, void *data)
4959{
c3e3c21a
CB
4960#ifdef HAVE_SECCOMP_NOTIFY
4961 memset(&c->seccomp.notifier.proxy_addr, 0,
4962 sizeof(c->seccomp.notifier.proxy_addr));
86ce1da1
CB
4963 return 0;
4964#else
21fce08c 4965 return ret_set_errno(-1, ENOSYS);
86ce1da1
CB
4966#endif
4967}
4968
0b427da0
CB
4969static inline int clr_config_seccomp_profile(const char *key,
4970 struct lxc_conf *c, void *data)
bbca37d8 4971{
c3e3c21a
CB
4972 free(c->seccomp.seccomp);
4973 c->seccomp.seccomp = NULL;
bbca37d8
CB
4974 return 0;
4975}
c721e86c 4976
26471403
CB
4977static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
4978 void *data)
c721e86c
CB
4979{
4980 c->autodev = 1;
4981 return 0;
4982}
87b288d1 4983
63012bdd
CK
4984static inline int clr_config_autodev_tmpfs_size(const char *key, struct lxc_conf *c,
4985 void *data)
4986{
4987 c->autodevtmpfssize = 500000;
4988 return 0;
4989}
4990
55c84efc 4991static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
26471403 4992 void *data)
87b288d1
CB
4993{
4994 c->haltsignal = 0;
4995 return 0;
4996}
cae63cfa 4997
55c84efc 4998static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
26471403 4999 void *data)
cae63cfa
CB
5000{
5001 c->rebootsignal = 0;
5002 return 0;
5003}
de45f3a8 5004
55c84efc 5005static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
26471403 5006 void *data)
de45f3a8
CB
5007{
5008 c->stopsignal = 0;
5009 return 0;
5010}
c6182222 5011
26471403
CB
5012static inline int clr_config_start(const char *key, struct lxc_conf *c,
5013 void *data)
c6182222
CB
5014{
5015 if (strcmp(key + 10, "auto") == 0)
5016 c->start_auto = 0;
5017 else if (strcmp(key + 10, "delay") == 0)
5018 c->start_delay = 0;
5019 else if (strcmp(key + 10, "order") == 0)
5020 c->start_order = 0;
5021
5022 return 0;
5023}
998ca94f 5024
46cc906d 5025static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
26471403 5026 void *data)
998ca94f
CB
5027{
5028 free(c->syslog);
5029 c->syslog = NULL;
5030 return 0;
5031}
adad12ca 5032
26471403
CB
5033static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
5034 void *data)
adad12ca
CB
5035{
5036 c->monitor_unshare = 0;
5037 return 0;
5038}
4850d223 5039
258f8051
CB
5040static inline int clr_config_monitor_signal_pdeath(const char *key,
5041 struct lxc_conf *c, void *data)
5042{
5043 c->monitor_signal_pdeath = 0;
5044 return 0;
5045}
5046
26471403
CB
5047static inline int clr_config_group(const char *key, struct lxc_conf *c,
5048 void *data)
4850d223
CB
5049{
5050 return lxc_clear_groups(c);
5051}
832fb63a 5052
26471403
CB
5053static inline int clr_config_environment(const char *key, struct lxc_conf *c,
5054 void *data)
832fb63a
CB
5055{
5056 return lxc_clear_environment(c);
4850d223 5057}
8e90af3e 5058
5cda27c1
SH
5059static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
5060 void *data)
5061{
5062 free(c->execute_cmd);
5063 c->execute_cmd = NULL;
5064 return 0;
5065}
5066
26471403
CB
5067static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
5068 void *data)
8e90af3e
CB
5069{
5070 free(c->init_cmd);
5071 c->init_cmd = NULL;
5072 return 0;
5073}
ec76dcfb 5074
3c491553
L
5075static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
5076 void *data)
5077{
5078 free(c->init_cwd);
5079 c->init_cwd = NULL;
5080 return 0;
5081}
5082
26471403
CB
5083static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
5084 void *data)
ec76dcfb
CB
5085{
5086 c->init_uid = 0;
5087 return 0;
5088}
1044b247 5089
26471403
CB
5090static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
5091 void *data)
1044b247
CB
5092{
5093 c->init_gid = 0;
5094 return 0;
5095}
59e370db 5096
26471403
CB
5097static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
5098 void *data)
59e370db
CB
5099{
5100 c->ephemeral = 0;
5101 return 0;
5102}
b98c5ab0 5103
26471403
CB
5104static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
5105 void *data)
b98c5ab0
CB
5106{
5107 c->no_new_privs = false;
5108 return 0;
5109}
715ccc96 5110
240d4b74 5111static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
26471403 5112 void *data)
715ccc96
CB
5113{
5114 return lxc_clear_limits(c, key);
5115}
fdf3c589 5116
7edd0540
L
5117static inline int clr_config_sysctl(const char *key, struct lxc_conf *c,
5118 void *data)
5119{
5120 return lxc_clear_sysctls(c, key);
5121}
5122
61d7a733
YT
5123static inline int clr_config_proc(const char *key, struct lxc_conf *c,
5124 void *data)
5125{
5126 return lxc_clear_procs(c, key);
5127}
5128
26471403
CB
5129static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
5130 void *data)
fdf3c589
CB
5131{
5132 lxc_clear_includes(c);
5133 return 0;
5134}
a3c8e600 5135
1d8d3676
CB
5136static int clr_config_namespace_clone(const char *key,
5137 struct lxc_conf *lxc_conf, void *data)
5138{
5139 lxc_conf->ns_clone = 0;
5140 return 0;
5141}
5142
abeb5bba
CB
5143static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf,
5144 void *data)
5145{
5146 lxc_conf->ns_keep = 0;
5147 return 0;
5148}
5149
70fd7fc9
CB
5150static int clr_config_time_offset_boot(const char *key, struct lxc_conf *lxc_conf, void *data)
5151{
5152 lxc_conf->timens.s_boot = 0;
5153 lxc_conf->timens.ns_boot = 0;
5154 return 0;
5155}
5156
5157static int clr_config_time_offset_monotonic(const char *key, struct lxc_conf *lxc_conf, void *data)
5158{
5159 lxc_conf->timens.s_monotonic = 0;
5160 lxc_conf->timens.ns_monotonic = 0;
5161 return 0;
5162}
5163
b074bbf1
CB
5164static int clr_config_namespace_share(const char *key,
5165 struct lxc_conf *lxc_conf, void *data)
28d9e29e
CB
5166{
5167 int ns_idx;
5168 const char *namespace;
5169
6333c915 5170 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
5171 ns_idx = lxc_namespace_2_ns_idx(namespace);
5172 if (ns_idx < 0)
5173 return ns_idx;
5174
b074bbf1
CB
5175 free(lxc_conf->ns_share[ns_idx]);
5176 lxc_conf->ns_share[ns_idx] = NULL;
28d9e29e
CB
5177
5178 return 0;
5179}
5180
a3c8e600 5181static int get_config_includefiles(const char *key, char *retv, int inlen,
cccd2219 5182 struct lxc_conf *c, void *data)
a3c8e600
CB
5183{
5184 return -ENOSYS;
5185}
40db5d2f 5186
31ee747b
CB
5187static struct lxc_config_t *get_network_config_ops(const char *key,
5188 struct lxc_conf *lxc_conf,
5189 ssize_t *idx,
5190 char **deindexed_key)
40db5d2f 5191{
31ee747b
CB
5192 int ret;
5193 unsigned int tmpidx;
5194 size_t numstrlen;
40db5d2f
CB
5195 char *copy, *idx_start, *idx_end;
5196 struct lxc_config_t *config = NULL;
5197
5198 /* check that this is a sensible network key */
31ee747b
CB
5199 if (strncmp("lxc.net.", key, 8)) {
5200 ERROR("Invalid network configuration key \"%s\"", key);
40db5d2f 5201 return NULL;
31ee747b 5202 }
40db5d2f
CB
5203
5204 copy = strdup(key);
31ee747b
CB
5205 if (!copy) {
5206 ERROR("Failed to duplicate string \"%s\"", key);
40db5d2f 5207 return NULL;
31ee747b 5208 }
40db5d2f 5209
f9373e40 5210 /* lxc.net.<n> */
31ee747b
CB
5211 if (!isdigit(*(key + 8))) {
5212 ERROR("Failed to detect digit in string \"%s\"", key + 8);
5213 goto on_error;
5214 }
40db5d2f 5215
31ee747b
CB
5216 /* beginning of index string */
5217 idx_start = (copy + 7);
5218 *idx_start = '\0';
40db5d2f 5219
31ee747b
CB
5220 /* end of index string */
5221 idx_end = strchr((copy + 8), '.');
7451daf8
CB
5222 if (idx_end)
5223 *idx_end = '\0';
31ee747b
CB
5224
5225 /* parse current index */
5226 ret = lxc_safe_uint((idx_start + 1), &tmpidx);
5227 if (ret < 0) {
6d1400b5 5228 errno = -ret;
ae52f396 5229 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
6d1400b5 5230 idx_start + 1);
31ee747b
CB
5231 *idx = ret;
5232 goto on_error;
5233 }
40db5d2f 5234
31ee747b
CB
5235 /* This, of course is utterly nonsensical on so many levels, but
5236 * better safe than sorry.
5237 * (Checking for INT_MAX here is intentional.)
5238 */
5239 if (tmpidx == INT_MAX) {
25a908b8 5240 SYSERROR("Number of configured networks would overflow the "
47903908 5241 "counter");
31ee747b 5242 goto on_error;
40db5d2f 5243 }
31ee747b
CB
5244 *idx = tmpidx;
5245
5246 numstrlen = strlen((idx_start + 1));
5247
5248 /* repair configuration key */
5249 *idx_start = '.';
31ee747b 5250
7451daf8
CB
5251 /* lxc.net.<idx>.<subkey> */
5252 if (idx_end) {
5253 *idx_end = '.';
24fa7df6
FA
5254 if (strlen(idx_end + 1) == 0) {
5255 ERROR("No subkey in network configuration key \"%s\"", key);
5256 goto on_error;
5257 }
40db5d2f 5258
7451daf8 5259 memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
52ab03ac 5260 copy[strlen(key) - (numstrlen + 1)] = '\0';
7451daf8 5261
300df83e 5262 config = lxc_get_config(copy);
7451daf8 5263 if (!config) {
25a908b8 5264 ERROR("Unknown network configuration key \"%s\"", key);
7451daf8
CB
5265 goto on_error;
5266 }
31ee747b
CB
5267 }
5268
7451daf8
CB
5269 if (deindexed_key)
5270 *deindexed_key = copy;
5271
31ee747b 5272 return config;
40db5d2f
CB
5273
5274on_error:
5275 free(copy);
31ee747b 5276 return NULL;
40db5d2f
CB
5277}
5278
25a908b8
CB
5279/* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5280 * found. So we make sure next comes an integer, find the right callback (by
5281 * rewriting the key), and call it.
40db5d2f 5282 */
f9373e40
CB
5283static int set_config_net_nic(const char *key, const char *value,
5284 struct lxc_conf *lxc_conf, void *data)
40db5d2f 5285{
31ee747b
CB
5286 int ret;
5287 const char *idxstring;
40db5d2f
CB
5288 struct lxc_config_t *config;
5289 struct lxc_netdev *netdev;
5290 ssize_t idx = -1;
31ee747b
CB
5291 char *deindexed_key = NULL;
5292
5293 idxstring = key + 8;
5294 if (!isdigit(*idxstring))
5295 return -1;
40db5d2f 5296
6bed0fb6 5297 if (lxc_config_value_empty(value))
f9373e40 5298 return clr_config_net_nic(key, lxc_conf, data);
6bed0fb6 5299
31ee747b 5300 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
40db5d2f
CB
5301 if (!config || idx < 0)
5302 return -1;
5303
0070b1c4 5304 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
31ee747b
CB
5305 if (!netdev) {
5306 free(deindexed_key);
40db5d2f 5307 return -1;
31ee747b 5308 }
40db5d2f 5309
31ee747b
CB
5310 ret = config->set(deindexed_key, value, lxc_conf, netdev);
5311 free(deindexed_key);
47903908 5312
31ee747b 5313 return ret;
40db5d2f 5314}
ff6da295 5315
f9373e40
CB
5316static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
5317 void *data)
ff6da295 5318{
31ee747b 5319 int ret;
519df1c1 5320 const char *idxstring;
ff6da295
CB
5321 struct lxc_config_t *config;
5322 struct lxc_netdev *netdev;
31ee747b
CB
5323 ssize_t idx = -1;
5324 char *deindexed_key = NULL;
519df1c1 5325
31ee747b
CB
5326 idxstring = key + 8;
5327 if (!isdigit(*idxstring))
519df1c1
CB
5328 return -1;
5329
519df1c1 5330 /* The left conjunct is pretty self-explanatory. The right conjunct
31ee747b 5331 * checks whether the two pointers are equal. If they are we know that
519df1c1
CB
5332 * this is not a key that is namespaced any further and so we are
5333 * supposed to clear the whole network.
5334 */
5335 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
5336 unsigned int rmnetdevidx;
5337
5338 if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0)
5339 return -1;
5340
5341 /* Remove network from network list. */
5342 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
5343 return 0;
5344 }
ff6da295 5345
31ee747b 5346 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
ff6da295
CB
5347 if (!config || idx < 0)
5348 return -1;
5349
0070b1c4 5350 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false);
31ee747b
CB
5351 if (!netdev) {
5352 free(deindexed_key);
ff6da295 5353 return -1;
31ee747b 5354 }
ff6da295 5355
31ee747b
CB
5356 ret = config->clr(deindexed_key, lxc_conf, netdev);
5357 free(deindexed_key);
47903908 5358
31ee747b 5359 return ret;
ff6da295
CB
5360}
5361
f9373e40
CB
5362static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
5363 void *data)
ff6da295 5364{
070a05af 5365 struct lxc_netdev *netdev = data;
ff6da295 5366
ff6da295
CB
5367 if (!netdev)
5368 return -1;
5369
5370 netdev->type = -1;
5371
5372 return 0;
5373}
5374
f9373e40
CB
5375static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
5376 void *data)
ff6da295 5377{
070a05af 5378 struct lxc_netdev *netdev = data;
ff6da295 5379
ff6da295
CB
5380 if (!netdev)
5381 return -1;
5382
de4855a8 5383 netdev->name[0] = '\0';
ff6da295
CB
5384
5385 return 0;
5386}
5387
f9373e40
CB
5388static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
5389 void *data)
ff6da295 5390{
070a05af 5391 struct lxc_netdev *netdev = data;
ff6da295 5392
ff6da295
CB
5393 if (!netdev)
5394 return -1;
5395
5396 netdev->flags = 0;
5397
5398 return 0;
5399}
5400
f9373e40
CB
5401static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
5402 void *data)
ff6da295 5403{
070a05af 5404 struct lxc_netdev *netdev = data;
ff6da295 5405
ff6da295
CB
5406 if (!netdev)
5407 return -1;
5408
de4855a8 5409 netdev->link[0] = '\0';
ff6da295
CB
5410
5411 return 0;
5412}
5413
6509154d 5414static int clr_config_net_l2proxy(const char *key, struct lxc_conf *lxc_conf,
5415 void *data)
5416{
5417 struct lxc_netdev *netdev = data;
5418
5419 if (!netdev)
21fce08c 5420 return ret_set_errno(-1, EINVAL);
6509154d 5421
5422 netdev->l2proxy = false;
5423
5424 return 0;
5425}
5426
f9373e40
CB
5427static int clr_config_net_macvlan_mode(const char *key,
5428 struct lxc_conf *lxc_conf, void *data)
ff6da295 5429{
070a05af 5430 struct lxc_netdev *netdev = data;
ff6da295 5431
ff6da295
CB
5432 if (!netdev)
5433 return -1;
5434
6bed0fb6
CB
5435 if (netdev->type != LXC_NET_MACVLAN)
5436 return 0;
5437
ff6da295
CB
5438 netdev->priv.macvlan_attr.mode = -1;
5439
5440 return 0;
5441}
5442
c9f52382 5443static int clr_config_net_ipvlan_mode(const char *key,
5444 struct lxc_conf *lxc_conf, void *data)
5445{
5446 struct lxc_netdev *netdev = data;
5447
5448 if (!netdev)
21fce08c 5449 return ret_set_errno(-1, EINVAL);
c9f52382 5450
5451 if (netdev->type != LXC_NET_IPVLAN)
5452 return 0;
5453
5454 netdev->priv.ipvlan_attr.mode = -1;
5455
5456 return 0;
5457}
5458
5459static int clr_config_net_ipvlan_isolation(const char *key,
5460 struct lxc_conf *lxc_conf, void *data)
5461{
5462 struct lxc_netdev *netdev = data;
5463
5464 if (!netdev)
21fce08c 5465 return ret_set_errno(-1, EINVAL);
c9f52382 5466
5467 if (netdev->type != LXC_NET_IPVLAN)
5468 return 0;
5469
5470 netdev->priv.ipvlan_attr.isolation = -1;
5471
5472 return 0;
5473}
5474
3f0ed090
TP
5475static int clr_config_net_veth_mode(const char *key,
5476 struct lxc_conf *lxc_conf, void *data)
5477{
5478 struct lxc_netdev *netdev = data;
5479
5480 if (!netdev)
21fce08c 5481 return ret_set_errno(-1, EINVAL);
3f0ed090
TP
5482
5483 if (netdev->type != LXC_NET_VETH)
5484 return 0;
5485
5486 netdev->priv.veth_attr.mode = -1;
5487
5488 return 0;
5489}
5490
f9373e40
CB
5491static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
5492 void *data)
ff6da295 5493{
070a05af 5494 struct lxc_netdev *netdev = data;
ff6da295 5495
ff6da295
CB
5496 if (!netdev)
5497 return -1;
5498
de4855a8 5499 netdev->priv.veth_attr.pair[0] = '\0';
ff6da295
CB
5500
5501 return 0;
5502}
5503
fdf76c6d
TP
5504static int clr_config_net_veth_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5505 void *data)
5506{
5507 struct lxc_netdev *netdev = data;
5508
5509 if (!netdev)
5510 return ret_errno(EINVAL);
5511
5512 netdev->priv.veth_attr.vlan_id = 0;
5513 netdev->priv.veth_attr.vlan_id_set = false;
5514
5515 return 0;
5516}
5517
b8e06d33
TP
5518static int clr_config_net_veth_vlan_tagged_id(const char *key,
5519 struct lxc_conf *lxc_conf, void *data)
5520{
5521 struct lxc_netdev *netdev = data;
5522 struct lxc_list *cur, *next;
5523
5524 if (!netdev)
5525 return ret_errno(EINVAL);
5526
5527 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.vlan_tagged_ids, next) {
5528 lxc_list_del(cur);
5529 free(cur);
5530 }
5531
5532 return 0;
5533}
5534
5535
f9373e40
CB
5536static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
5537 void *data)
ff6da295 5538{
070a05af 5539 struct lxc_netdev *netdev = data;
ff6da295 5540
ff6da295
CB
5541 if (!netdev)
5542 return -1;
5543
5544 free(netdev->upscript);
5545 netdev->upscript = NULL;
5546
5547 return 0;
5548}
5549
f9373e40
CB
5550static int clr_config_net_script_down(const char *key,
5551 struct lxc_conf *lxc_conf, void *data)
ff6da295 5552{
070a05af 5553 struct lxc_netdev *netdev = data;
ff6da295 5554
ff6da295
CB
5555 if (!netdev)
5556 return -1;
5557
5558 free(netdev->downscript);
5559 netdev->downscript = NULL;
5560
5561 return 0;
5562}
5563
f9373e40
CB
5564static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
5565 void *data)
ff6da295 5566{
070a05af 5567 struct lxc_netdev *netdev = data;
ff6da295 5568
ff6da295
CB
5569 if (!netdev)
5570 return -1;
5571
5572 free(netdev->hwaddr);
5573 netdev->hwaddr = NULL;
5574
5575 return 0;
5576}
5577
f9373e40
CB
5578static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
5579 void *data)
ff6da295 5580{
070a05af 5581 struct lxc_netdev *netdev = data;
ff6da295 5582
ff6da295
CB
5583 if (!netdev)
5584 return -1;
5585
5586 free(netdev->mtu);
5587 netdev->mtu = NULL;
5588
5589 return 0;
5590}
5591
f9373e40
CB
5592static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5593 void *data)
ff6da295 5594{
070a05af 5595 struct lxc_netdev *netdev = data;
ff6da295 5596
ff6da295
CB
5597 if (!netdev)
5598 return -1;
5599
5600 netdev->priv.vlan_attr.vid = 0;
5601
5602 return 0;
5603}
5604
f9373e40
CB
5605static int clr_config_net_ipv4_gateway(const char *key,
5606 struct lxc_conf *lxc_conf, void *data)
ff6da295 5607{
070a05af 5608 struct lxc_netdev *netdev = data;
ff6da295 5609
ff6da295
CB
5610 if (!netdev)
5611 return -1;
5612
5613 free(netdev->ipv4_gateway);
5614 netdev->ipv4_gateway = NULL;
5615
5616 return 0;
5617}
5618
9ff60df2
CB
5619static int clr_config_net_ipv4_address(const char *key,
5620 struct lxc_conf *lxc_conf, void *data)
ff6da295 5621{
070a05af 5622 struct lxc_netdev *netdev = data;
ff6da295
CB
5623 struct lxc_list *cur, *next;
5624
ecbb3790
CB
5625 if (!netdev)
5626 return -1;
ff6da295
CB
5627
5628 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
5629 lxc_list_del(cur);
5630 free(cur->elem);
5631 free(cur);
5632 }
5633
5634 return 0;
5635}
5636
d4a7da46 5637static int clr_config_net_veth_ipv4_route(const char *key,
5638 struct lxc_conf *lxc_conf, void *data)
5639{
5640 struct lxc_netdev *netdev = data;
5641 struct lxc_list *cur, *next;
5642
5643 if (!netdev)
5644 return -1;
5645
5646 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) {
5647 lxc_list_del(cur);
5648 free(cur->elem);
5649 free(cur);
5650 }
5651
5652 return 0;
5653}
5654
f9373e40
CB
5655static int clr_config_net_ipv6_gateway(const char *key,
5656 struct lxc_conf *lxc_conf, void *data)
ff6da295 5657{
070a05af 5658 struct lxc_netdev *netdev = data;
ff6da295 5659
ff6da295
CB
5660 if (!netdev)
5661 return -1;
5662
5663 free(netdev->ipv6_gateway);
5664 netdev->ipv6_gateway = NULL;
5665
5666 return 0;
5667}
5668
2e44ae28
CB
5669static int clr_config_net_ipv6_address(const char *key,
5670 struct lxc_conf *lxc_conf, void *data)
ff6da295 5671{
070a05af 5672 struct lxc_netdev *netdev = data;
ff6da295
CB
5673 struct lxc_list *cur, *next;
5674
ecbb3790
CB
5675 if (!netdev)
5676 return -1;
ff6da295
CB
5677
5678 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
5679 lxc_list_del(cur);
5680 free(cur->elem);
5681 free(cur);
5682 }
5683
5684 return 0;
5685}
9d4bf22d 5686
d4a7da46 5687static int clr_config_net_veth_ipv6_route(const char *key,
5688 struct lxc_conf *lxc_conf, void *data)
5689{
5690 struct lxc_netdev *netdev = data;
5691 struct lxc_list *cur, *next;
5692
5693 if (!netdev)
5694 return -1;
5695
5696 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) {
5697 lxc_list_del(cur);
5698 free(cur->elem);
5699 free(cur);
5700 }
5701
5702 return 0;
5703}
5704
f9373e40
CB
5705static int get_config_net_nic(const char *key, char *retv, int inlen,
5706 struct lxc_conf *c, void *data)
9d4bf22d 5707{
31ee747b
CB
5708 int ret;
5709 const char *idxstring;
9d4bf22d
CB
5710 struct lxc_config_t *config;
5711 struct lxc_netdev *netdev;
5712 ssize_t idx = -1;
31ee747b 5713 char *deindexed_key = NULL;
9d4bf22d 5714
31ee747b
CB
5715 idxstring = key + 8;
5716 if (!isdigit(*idxstring))
5717 return -1;
5718
5719 config = get_network_config_ops(key, c, &idx, &deindexed_key);
9d4bf22d
CB
5720 if (!config || idx < 0)
5721 return -1;
5722
0070b1c4 5723 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
31ee747b
CB
5724 if (!netdev) {
5725 free(deindexed_key);
9d4bf22d 5726 return -1;
31ee747b 5727 }
9d4bf22d 5728
31ee747b
CB
5729 ret = config->get(deindexed_key, retv, inlen, c, netdev);
5730 free(deindexed_key);
47903908 5731
31ee747b 5732 return ret;
9d4bf22d
CB
5733}
5734
f9373e40
CB
5735static int get_config_net_type(const char *key, char *retv, int inlen,
5736 struct lxc_conf *c, void *data)
9d4bf22d 5737{
070a05af
CB
5738 int len;
5739 int fulllen = 0;
5740 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5741
5742 if (!retv)
5743 inlen = 0;
5744 else
5745 memset(retv, 0, inlen);
5746
9d4bf22d
CB
5747 if (!netdev)
5748 return -1;
5749
5750 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
5751
5752 return fulllen;
5753}
5754
f9373e40
CB
5755static int get_config_net_flags(const char *key, char *retv, int inlen,
5756 struct lxc_conf *c, void *data)
9d4bf22d 5757{
070a05af
CB
5758 int len;
5759 int fulllen = 0;
5760 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5761
5762 if (!retv)
5763 inlen = 0;
5764 else
5765 memset(retv, 0, inlen);
5766
9d4bf22d
CB
5767 if (!netdev)
5768 return -1;
5769
5770 if (netdev->flags & IFF_UP)
5771 strprint(retv, inlen, "up");
5772
5773 return fulllen;
5774}
5775
f9373e40
CB
5776static int get_config_net_link(const char *key, char *retv, int inlen,
5777 struct lxc_conf *c, void *data)
9d4bf22d 5778{
070a05af
CB
5779 int len;
5780 int fulllen = 0;
5781 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5782
5783 if (!retv)
5784 inlen = 0;
5785 else
5786 memset(retv, 0, inlen);
5787
9d4bf22d
CB
5788 if (!netdev)
5789 return -1;
5790
de4855a8 5791 if (netdev->link[0] != '\0')
9d4bf22d
CB
5792 strprint(retv, inlen, "%s", netdev->link);
5793
5794 return fulllen;
5795}
5796
6509154d 5797static int get_config_net_l2proxy(const char *key, char *retv, int inlen,
5798 struct lxc_conf *c, void *data)
5799{
5800 struct lxc_netdev *netdev = data;
5801 return lxc_get_conf_bool(c, retv, inlen, netdev->l2proxy);
5802}
5803
f9373e40
CB
5804static int get_config_net_name(const char *key, char *retv, int inlen,
5805 struct lxc_conf *c, void *data)
9d4bf22d 5806{
070a05af
CB
5807 int len;
5808 int fulllen = 0;
5809 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5810
5811 if (!retv)
5812 inlen = 0;
5813 else
5814 memset(retv, 0, inlen);
5815
9d4bf22d
CB
5816 if (!netdev)
5817 return -1;
5818
de4855a8 5819 if (netdev->name[0] != '\0')
9d4bf22d
CB
5820 strprint(retv, inlen, "%s", netdev->name);
5821
5822 return fulllen;
5823}
5824
f9373e40
CB
5825static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
5826 struct lxc_conf *c, void *data)
9d4bf22d 5827{
070a05af
CB
5828 int len;
5829 int fulllen = 0;
9d4bf22d 5830 const char *mode;
070a05af 5831 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5832
5833 if (!retv)
5834 inlen = 0;
5835 else
5836 memset(retv, 0, inlen);
5837
9d4bf22d
CB
5838 if (!netdev)
5839 return -1;
5840
5841 if (netdev->type != LXC_NET_MACVLAN)
5842 return 0;
5843
5844 switch (netdev->priv.macvlan_attr.mode) {
5845 case MACVLAN_MODE_PRIVATE:
5846 mode = "private";
5847 break;
5848 case MACVLAN_MODE_VEPA:
5849 mode = "vepa";
5850 break;
5851 case MACVLAN_MODE_BRIDGE:
5852 mode = "bridge";
5853 break;
5854 case MACVLAN_MODE_PASSTHRU:
5855 mode = "passthru";
5856 break;
5857 default:
5858 mode = "(invalid)";
5859 break;
5860 }
5861
5862 strprint(retv, inlen, "%s", mode);
5863
5864 return fulllen;
5865}
5866
c9f52382 5867static int get_config_net_ipvlan_mode(const char *key, char *retv, int inlen,
5868 struct lxc_conf *c, void *data)
5869{
5870 int len;
5871 int fulllen = 0;
5872 const char *mode;
5873 struct lxc_netdev *netdev = data;
5874
5875 if (!retv)
5876 inlen = 0;
5877 else
5878 memset(retv, 0, inlen);
5879
5880 if (!netdev)
21fce08c 5881 return ret_set_errno(-1, EINVAL);
c9f52382 5882
5883 if (netdev->type != LXC_NET_IPVLAN)
5884 return 0;
5885
5886 switch (netdev->priv.ipvlan_attr.mode) {
5887 case IPVLAN_MODE_L3:
5888 mode = "l3";
5889 break;
5890 case IPVLAN_MODE_L3S:
5891 mode = "l3s";
5892 break;
5893 case IPVLAN_MODE_L2:
5894 mode = "l2";
5895 break;
5896 default:
5897 mode = "(invalid)";
5898 break;
5899 }
5900
5901 strprint(retv, inlen, "%s", mode);
5902
5903 return fulllen;
5904}
5905
5906static int get_config_net_ipvlan_isolation(const char *key, char *retv, int inlen,
5907 struct lxc_conf *c, void *data)
5908{
5909 int len;
5910 int fulllen = 0;
5911 const char *mode;
5912 struct lxc_netdev *netdev = data;
5913
5914 if (!retv)
5915 inlen = 0;
5916 else
5917 memset(retv, 0, inlen);
5918
5919 if (!netdev)
21fce08c 5920 return ret_set_errno(-1, EINVAL);
c9f52382 5921
5922 if (netdev->type != LXC_NET_IPVLAN)
5923 return 0;
5924
5925 switch (netdev->priv.ipvlan_attr.isolation) {
5926 case IPVLAN_ISOLATION_BRIDGE:
5927 mode = "bridge";
5928 break;
5929 case IPVLAN_ISOLATION_PRIVATE:
5930 mode = "private";
5931 break;
5932 case IPVLAN_ISOLATION_VEPA:
5933 mode = "vepa";
5934 break;
5935 default:
5936 mode = "(invalid)";
5937 break;
5938 }
5939
5940 strprint(retv, inlen, "%s", mode);
5941
5942 return fulllen;
5943}
5944
3f0ed090
TP
5945static int get_config_net_veth_mode(const char *key, char *retv, int inlen,
5946 struct lxc_conf *c, void *data)
5947{
5948 int len;
5949 int fulllen = 0;
5950 const char *mode;
5951 struct lxc_netdev *netdev = data;
5952
5953 if (!retv)
5954 inlen = 0;
5955 else
5956 memset(retv, 0, inlen);
5957
5958 if (!netdev)
21fce08c 5959 return ret_set_errno(-1, EINVAL);
3f0ed090
TP
5960
5961 if (netdev->type != LXC_NET_VETH)
5962 return 0;
5963
5964 switch (netdev->priv.veth_attr.mode) {
5965 case VETH_MODE_BRIDGE:
5966 mode = "bridge";
5967 break;
5968 case VETH_MODE_ROUTER:
5969 mode = "router";
5970 break;
5971 default:
5972 mode = "(invalid)";
5973 break;
5974 }
5975
5976 strprint(retv, inlen, "%s", mode);
5977
5978 return fulllen;
5979}
5980
f9373e40
CB
5981static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
5982 struct lxc_conf *c, void *data)
9d4bf22d 5983{
070a05af
CB
5984 int len;
5985 int fulllen = 0;
5986 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5987
5988 if (!retv)
5989 inlen = 0;
5990 else
5991 memset(retv, 0, inlen);
5992
9d4bf22d
CB
5993 if (!netdev)
5994 return -1;
5995
5996 if (netdev->type != LXC_NET_VETH)
5997 return 0;
5998
5999 strprint(retv, inlen, "%s",
de4855a8
CB
6000 netdev->priv.veth_attr.pair[0] != '\0'
6001 ? netdev->priv.veth_attr.pair
6002 : netdev->priv.veth_attr.veth1);
9d4bf22d
CB
6003
6004 return fulllen;
6005}
6006
fdf76c6d
TP
6007static int get_config_net_veth_vlan_id(const char *key, char *retv, int inlen,
6008 struct lxc_conf *c, void *data)
6009{
6010 int len;
6011 int fulllen = 0;
6012 struct lxc_netdev *netdev = data;
6013
6014 if (!netdev)
6015 return ret_errno(EINVAL);
6016
6017 if (netdev->type != LXC_NET_VETH)
6018 return 0;
6019
6020 if (!retv)
6021 inlen = 0;
6022 else
6023 memset(retv, 0, inlen);
6024
6025 strprint(retv, inlen, "%d", netdev->priv.veth_attr.vlan_id);
6026
6027 return fulllen;
6028}
6029
b8e06d33
TP
6030static int get_config_net_veth_vlan_tagged_id(const char *key, char *retv, int inlen,
6031 struct lxc_conf *c, void *data)
6032{
6033 int len;
6034 size_t listlen;
6035 struct lxc_list *it;
6036 int fulllen = 0;
6037 struct lxc_netdev *netdev = data;
6038
6039 if (!netdev)
4e61b19d 6040 return ret_errno(EINVAL);
b8e06d33
TP
6041
6042 if (netdev->type != LXC_NET_VETH)
6043 return 0;
6044
6045 if (!retv)
6046 inlen = 0;
6047 else
6048 memset(retv, 0, inlen);
6049
6050 listlen = lxc_list_len(&netdev->priv.veth_attr.vlan_tagged_ids);
6051
6052 lxc_list_for_each(it, &netdev->priv.veth_attr.vlan_tagged_ids) {
6053 unsigned short i = PTR_TO_USHORT(it->elem);
6054 strprint(retv, inlen, "%u%s", i,
6055 (listlen-- > 1) ? "\n" : "");
6056 }
6057
6058 return fulllen;
6059}
6060
f9373e40
CB
6061static int get_config_net_script_up(const char *key, char *retv, int inlen,
6062 struct lxc_conf *c, void *data)
9d4bf22d 6063{
070a05af
CB
6064 int len;
6065 int fulllen = 0;
6066 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6067
6068 if (!retv)
6069 inlen = 0;
6070 else
6071 memset(retv, 0, inlen);
6072
9d4bf22d
CB
6073 if (!netdev)
6074 return -1;
6075
6076 if (netdev->upscript)
6077 strprint(retv, inlen, "%s", netdev->upscript);
6078
6079 return fulllen;
6080}
6081
f9373e40
CB
6082static int get_config_net_script_down(const char *key, char *retv, int inlen,
6083 struct lxc_conf *c, void *data)
9d4bf22d 6084{
070a05af
CB
6085 int len;
6086 int fulllen = 0;
6087 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6088
6089 if (!retv)
6090 inlen = 0;
6091 else
6092 memset(retv, 0, inlen);
6093
9d4bf22d
CB
6094 if (!netdev)
6095 return -1;
6096
6097 if (netdev->downscript)
6098 strprint(retv, inlen, "%s", netdev->downscript);
6099
6100 return fulllen;
6101}
6102
f9373e40
CB
6103static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
6104 struct lxc_conf *c, void *data)
9d4bf22d 6105{
070a05af
CB
6106 int len;
6107 int fulllen = 0;
6108 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6109
6110 if (!retv)
6111 inlen = 0;
6112 else
6113 memset(retv, 0, inlen);
6114
9d4bf22d
CB
6115 if (!netdev)
6116 return -1;
6117
6118 if (netdev->hwaddr)
6119 strprint(retv, inlen, "%s", netdev->hwaddr);
6120
6121 return fulllen;
6122}
6123
f9373e40
CB
6124static int get_config_net_mtu(const char *key, char *retv, int inlen,
6125 struct lxc_conf *c, void *data)
9d4bf22d 6126{
070a05af
CB
6127 int len;
6128 int fulllen = 0;
6129 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6130
6131 if (!retv)
6132 inlen = 0;
6133 else
6134 memset(retv, 0, inlen);
6135
9d4bf22d
CB
6136 if (!netdev)
6137 return -1;
6138
6139 if (netdev->mtu)
6140 strprint(retv, inlen, "%s", netdev->mtu);
6141
6142 return fulllen;
6143}
6144
f9373e40
CB
6145static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
6146 struct lxc_conf *c, void *data)
9d4bf22d 6147{
070a05af
CB
6148 int len;
6149 int fulllen = 0;
6150 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6151
6152 if (!retv)
6153 inlen = 0;
6154 else
6155 memset(retv, 0, inlen);
6156
9d4bf22d
CB
6157 if (!netdev)
6158 return -1;
6159
6160 if (netdev->type != LXC_NET_VLAN)
6161 return 0;
6162
6163 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
6164
6165 return fulllen;
6166}
6167
f9373e40
CB
6168static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
6169 struct lxc_conf *c, void *data)
9d4bf22d 6170{
070a05af 6171 int len;
9d4bf22d 6172 char buf[INET_ADDRSTRLEN];
070a05af
CB
6173 int fulllen = 0;
6174 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6175
6176 if (!retv)
6177 inlen = 0;
6178 else
6179 memset(retv, 0, inlen);
6180
9d4bf22d
CB
6181 if (!netdev)
6182 return -1;
6183
6184 if (netdev->ipv4_gateway_auto) {
6185 strprint(retv, inlen, "auto");
a2f9a670 6186 } else if (netdev->ipv4_gateway_dev) {
6187 strprint(retv, inlen, "dev");
9d4bf22d
CB
6188 } else if (netdev->ipv4_gateway) {
6189 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
6190 strprint(retv, inlen, "%s", buf);
6191 }
6192
6193 return fulllen;
6194}
6195
9ff60df2
CB
6196static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
6197 struct lxc_conf *c, void *data)
9d4bf22d 6198{
070a05af 6199 int len;
9d4bf22d
CB
6200 size_t listlen;
6201 char buf[INET_ADDRSTRLEN];
9d4bf22d 6202 struct lxc_list *it;
070a05af
CB
6203 int fulllen = 0;
6204 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6205
6206 if (!retv)
6207 inlen = 0;
6208 else
6209 memset(retv, 0, inlen);
6210
9d4bf22d
CB
6211 if (!netdev)
6212 return -1;
6213
6214 listlen = lxc_list_len(&netdev->ipv4);
47903908 6215
9d4bf22d
CB
6216 lxc_list_for_each(it, &netdev->ipv4) {
6217 struct lxc_inetdev *i = it->elem;
6218 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6fc7d8b6 6219 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
6220 (listlen-- > 1) ? "\n" : "");
6221 }
6222
6223 return fulllen;
6224}
6225
d4a7da46 6226static int get_config_net_veth_ipv4_route(const char *key, char *retv, int inlen,
6227 struct lxc_conf *c, void *data)
6228{
6229 int len;
6230 size_t listlen;
6231 char buf[INET_ADDRSTRLEN];
6232 struct lxc_list *it;
6233 int fulllen = 0;
6234 struct lxc_netdev *netdev = data;
6235
6236 if (!retv)
6237 inlen = 0;
6238 else
6239 memset(retv, 0, inlen);
6240
6241 if (!netdev)
21fce08c 6242 return ret_set_errno(-1, EINVAL);
d4a7da46 6243
6244 if (netdev->type != LXC_NET_VETH)
6245 return 0;
6246
6247 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv4_routes);
6248
6249 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv4_routes) {
6250 struct lxc_inetdev *i = it->elem;
6251 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6252 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6253 (listlen-- > 1) ? "\n" : "");
6254 }
6255
6256 return fulllen;
6257}
6258
f9373e40
CB
6259static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
6260 struct lxc_conf *c, void *data)
9d4bf22d 6261{
070a05af 6262 int len;
9d4bf22d 6263 char buf[INET6_ADDRSTRLEN];
070a05af
CB
6264 int fulllen = 0;
6265 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6266
6267 if (!retv)
6268 inlen = 0;
6269 else
6270 memset(retv, 0, inlen);
6271
9d4bf22d
CB
6272 if (!netdev)
6273 return -1;
6274
6275 if (netdev->ipv6_gateway_auto) {
6276 strprint(retv, inlen, "auto");
a2f9a670 6277 } else if (netdev->ipv6_gateway_dev) {
6278 strprint(retv, inlen, "dev");
9d4bf22d
CB
6279 } else if (netdev->ipv6_gateway) {
6280 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
6281 strprint(retv, inlen, "%s", buf);
6282 }
6283
6284 return fulllen;
6285}
6286
2e44ae28
CB
6287static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
6288 struct lxc_conf *c, void *data)
9d4bf22d 6289{
070a05af 6290 int len;
9d4bf22d
CB
6291 size_t listlen;
6292 char buf[INET6_ADDRSTRLEN];
9d4bf22d 6293 struct lxc_list *it;
070a05af
CB
6294 int fulllen = 0;
6295 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6296
6297 if (!retv)
6298 inlen = 0;
6299 else
6300 memset(retv, 0, inlen);
6301
9d4bf22d
CB
6302 if (!netdev)
6303 return -1;
6304
6305 listlen = lxc_list_len(&netdev->ipv6);
47903908 6306
9d4bf22d
CB
6307 lxc_list_for_each(it, &netdev->ipv6) {
6308 struct lxc_inet6dev *i = it->elem;
6309 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6fc7d8b6 6310 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
6311 (listlen-- > 1) ? "\n" : "");
6312 }
6313
6314 return fulllen;
6315}
cfc67626 6316
d4a7da46 6317static int get_config_net_veth_ipv6_route(const char *key, char *retv, int inlen,
6318 struct lxc_conf *c, void *data)
6319{
6320 int len;
6321 size_t listlen;
6322 char buf[INET6_ADDRSTRLEN];
6323 struct lxc_list *it;
6324 int fulllen = 0;
6325 struct lxc_netdev *netdev = data;
6326
6327 if (!retv)
6328 inlen = 0;
6329 else
6330 memset(retv, 0, inlen);
6331
6332 if (!netdev)
21fce08c 6333 return ret_set_errno(-1, EINVAL);
d4a7da46 6334
6335 if (netdev->type != LXC_NET_VETH)
6336 return 0;
6337
6338 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv6_routes);
6339
6340 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv6_routes) {
6341 struct lxc_inet6dev *i = it->elem;
6342 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6343 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6344 (listlen-- > 1) ? "\n" : "");
6345 }
6346
6347 return fulllen;
6348}
6349
cfc67626
CB
6350int lxc_list_config_items(char *retv, int inlen)
6351{
6352 size_t i;
6353 int len;
6354 int fulllen = 0;
6355
6356 if (!retv)
6357 inlen = 0;
6358 else
6359 memset(retv, 0, inlen);
6360
33eb2ec1
CB
6361 for (i = 0; i < config_jump_table_size; i++) {
6362 char *s = config_jump_table[i].name;
300df83e 6363
cfc67626
CB
6364 if (s[strlen(s) - 1] == '.')
6365 continue;
300df83e 6366
cfc67626
CB
6367 strprint(retv, inlen, "%s\n", s);
6368 }
6369
6370 return fulllen;
6371}
a5448c15 6372
fe9b7349
CB
6373int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
6374 int inlen)
6375{
6376 int len;
6377 int fulllen = 0;
6378
6379 if (!retv)
6380 inlen = 0;
6381 else
6382 memset(retv, 0, inlen);
6383
6384 if (!strcmp(key, "lxc.apparmor")) {
6385 strprint(retv, inlen, "allow_incomplete\n");
1800f924 6386 strprint(retv, inlen, "allow_nesting\n");
fe9b7349 6387 strprint(retv, inlen, "profile\n");
1800f924 6388 strprint(retv, inlen, "raw\n");
dae8c253
CB
6389 } else if (!strcmp(key, "lxc.cgroup")) {
6390 strprint(retv, inlen, "dir\n");
fe9b7349
CB
6391 } else if (!strcmp(key, "lxc.selinux")) {
6392 strprint(retv, inlen, "context\n");
4fef78bc 6393 strprint(retv, inlen, "context.keyring\n");
fe9b7349
CB
6394 } else if (!strcmp(key, "lxc.mount")) {
6395 strprint(retv, inlen, "auto\n");
6396 strprint(retv, inlen, "entry\n");
6397 strprint(retv, inlen, "fstab\n");
6398 } else if (!strcmp(key, "lxc.rootfs")) {
6399 strprint(retv, inlen, "mount\n");
6400 strprint(retv, inlen, "options\n");
6401 strprint(retv, inlen, "path\n");
6402 } else if (!strcmp(key, "lxc.uts")) {
6403 strprint(retv, inlen, "name\n");
6404 } else if (!strcmp(key, "lxc.hook")) {
6405 strprint(retv, inlen, "autodev\n");
63012bdd 6406 strprint(retv, inlen, "autodevtmpfssize\n");
fe9b7349
CB
6407 strprint(retv, inlen, "clone\n");
6408 strprint(retv, inlen, "destroy\n");
6409 strprint(retv, inlen, "mount\n");
6410 strprint(retv, inlen, "post-stop\n");
6411 strprint(retv, inlen, "pre-mount\n");
6412 strprint(retv, inlen, "pre-start\n");
08dd2805 6413 strprint(retv, inlen, "start-host\n");
fe9b7349
CB
6414 strprint(retv, inlen, "start\n");
6415 strprint(retv, inlen, "stop\n");
6416 } else if (!strcmp(key, "lxc.cap")) {
6417 strprint(retv, inlen, "drop\n");
6418 strprint(retv, inlen, "keep\n");
6419 } else if (!strcmp(key, "lxc.console")) {
6420 strprint(retv, inlen, "logfile\n");
6421 strprint(retv, inlen, "path\n");
6422 } else if (!strcmp(key, "lxc.seccomp")) {
6423 strprint(retv, inlen, "profile\n");
6424 } else if (!strcmp(key, "lxc.signal")) {
6425 strprint(retv, inlen, "halt\n");
6426 strprint(retv, inlen, "reboot\n");
6427 strprint(retv, inlen, "stop\n");
6428 } else if (!strcmp(key, "lxc.start")) {
6429 strprint(retv, inlen, "auto\n");
6430 strprint(retv, inlen, "delay\n");
6431 strprint(retv, inlen, "order\n");
6432 } else if (!strcmp(key, "lxc.monitor")) {
6433 strprint(retv, inlen, "unshare\n");
8f818a84
MB
6434 } else if (!strcmp(key, "lxc.keyring")) {
6435 strprint(retv, inlen, "session\n");
fe9b7349
CB
6436 } else {
6437 fulllen = -1;
6438 }
6439
6440 return fulllen;
6441}
6442
a5448c15
CB
6443int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
6444{
6445 int len;
6446 const char *idxstring;
a5448c15
CB
6447 struct lxc_netdev *netdev;
6448 int fulllen = 0;
6449 ssize_t idx = -1;
a5448c15
CB
6450
6451 idxstring = key + 8;
6452 if (!isdigit(*idxstring))
6453 return -1;
6454
7451daf8
CB
6455 (void)get_network_config_ops(key, c, &idx, NULL);
6456 if (idx < 0)
a5448c15
CB
6457 return -1;
6458
6459 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
a5448c15
CB
6460 if (!netdev)
6461 return -1;
6462
6463 if (!retv)
6464 inlen = 0;
6465 else
6466 memset(retv, 0, inlen);
6467
6468 strprint(retv, inlen, "type\n");
6469 strprint(retv, inlen, "script.up\n");
6470 strprint(retv, inlen, "script.down\n");
47903908 6471
a5448c15
CB
6472 if (netdev->type != LXC_NET_EMPTY) {
6473 strprint(retv, inlen, "flags\n");
6474 strprint(retv, inlen, "link\n");
6475 strprint(retv, inlen, "name\n");
6476 strprint(retv, inlen, "hwaddr\n");
6477 strprint(retv, inlen, "mtu\n");
6478 strprint(retv, inlen, "ipv6.address\n");
6479 strprint(retv, inlen, "ipv6.gateway\n");
6480 strprint(retv, inlen, "ipv4.address\n");
6481 strprint(retv, inlen, "ipv4.gateway\n");
6482 }
6483
6484 switch (netdev->type) {
6485 case LXC_NET_VETH:
6486 strprint(retv, inlen, "veth.pair\n");
d4a7da46 6487 strprint(retv, inlen, "veth.ipv4.route\n");
6488 strprint(retv, inlen, "veth.ipv6.route\n");
fdf76c6d 6489 strprint(retv, inlen, "veth.vlan.id\n");
a5448c15
CB
6490 break;
6491 case LXC_NET_MACVLAN:
6492 strprint(retv, inlen, "macvlan.mode\n");
6493 break;
c9f52382 6494 case LXC_NET_IPVLAN:
6495 strprint(retv, inlen, "ipvlan.mode\n");
6496 strprint(retv, inlen, "ipvlan.isolation\n");
6497 break;
a5448c15
CB
6498 case LXC_NET_VLAN:
6499 strprint(retv, inlen, "vlan.id\n");
6500 break;
6501 case LXC_NET_PHYS:
6502 break;
6503 }
6504
6505 return fulllen;
6506}