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