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