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