]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/confile.c
Merge pull request #3762 from brauner/2021-03-31/fixes
[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, },
229 { "lxc.namespace.share", true, 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{
713046e3 1558 return set_config_string_item(&lxc_conf->lsm_aa_profile, value);
fe4de9a6
DE
1559}
1560
953fe44f
CB
1561static int set_config_apparmor_allow_incomplete(const char *key,
1562 const char *value,
1563 struct lxc_conf *lxc_conf,
1564 void *data)
7aff4f43 1565{
042f8711
CB
1566 int ret;
1567
663e9916 1568 if (lxc_config_value_empty(value)) {
cccfa758 1569 lxc_conf->lsm_aa_allow_incomplete = 0;
a678e9fa 1570 return 0;
cccfa758 1571 }
a678e9fa 1572
042f8711
CB
1573 ret = lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete);
1574 if (ret)
1575 return ret;
7aff4f43 1576
25a908b8 1577 if (lxc_conf->lsm_aa_allow_incomplete > 1)
042f8711 1578 return ret_errno(EINVAL);
7aff4f43
SH
1579
1580 return 0;
1581}
1582
1800f924
WB
1583static int set_config_apparmor_allow_nesting(const char *key,
1584 const char *value,
1585 struct lxc_conf *lxc_conf,
1586 void *data)
1587{
55a76891
CB
1588 int ret;
1589
1800f924
WB
1590 if (lxc_config_value_empty(value))
1591 return clr_config_apparmor_allow_nesting(key, lxc_conf, NULL);
1592
55a76891
CB
1593 ret = lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_nesting);
1594 if (ret)
1595 return ret;
1800f924
WB
1596
1597 if (lxc_conf->lsm_aa_allow_nesting > 1)
55a76891 1598 return ret_errno(EINVAL);
1800f924
WB
1599
1600 return 0;
1601}
1602
1603static int set_config_apparmor_raw(const char *key,
1604 const char *value,
1605 struct lxc_conf *lxc_conf,
1606 void *data)
1607{
7f44fda1
CB
1608 __do_free char *elem = NULL;
1609 __do_free struct lxc_list *list = NULL;
1800f924
WB
1610
1611 if (lxc_config_value_empty(value))
1612 return lxc_clear_apparmor_raw(lxc_conf);
1613
642751cc 1614 list = lxc_list_new();
7f44fda1
CB
1615 if (!list)
1616 return ret_errno(ENOMEM);
1800f924
WB
1617
1618 elem = strdup(value);
7f44fda1
CB
1619 if (!elem)
1620 return ret_errno(ENOMEM);
1800f924 1621
7f44fda1
CB
1622 list->elem = move_ptr(elem);
1623 lxc_list_add_tail(&lxc_conf->lsm_aa_raw, move_ptr(list));
1800f924
WB
1624
1625 return 0;
1626}
1627
953fe44f
CB
1628static int set_config_selinux_context(const char *key, const char *value,
1629 struct lxc_conf *lxc_conf, void *data)
fe4de9a6 1630{
713046e3 1631 return set_config_string_item(&lxc_conf->lsm_se_context, value);
e075f5d9 1632}
e075f5d9 1633
4fef78bc
MB
1634static int set_config_selinux_context_keyring(const char *key, const char *value,
1635 struct lxc_conf *lxc_conf, void *data)
1636{
1637 return set_config_string_item(&lxc_conf->lsm_se_keyring_context, value);
1638}
1639
8f818a84
MB
1640static int set_config_keyring_session(const char *key, const char *value,
1641 struct lxc_conf *lxc_conf, void *data)
1642{
1643 return set_config_bool_item(&lxc_conf->keyring_disable_session, value, false);
1644}
1645
46cc906d 1646static int set_config_log_file(const char *key, const char *value,
c7e27aaf 1647 struct lxc_conf *c, void *data)
4a85ce2a 1648{
6d03d92a
DE
1649 int ret;
1650
663e9916 1651 if (lxc_config_value_empty(value)) {
34f3b30a 1652 free_disarm(c->logfile);
0d601acb
CB
1653 return 0;
1654 }
1655
34f3b30a
CB
1656 /*
1657 * Store these values in the lxc_conf, and then try to set for actual
504a2217
CB
1658 * current logging.
1659 */
713046e3 1660 ret = set_config_path_item(&c->logfile, value);
6d03d92a 1661 if (ret == 0)
858377e4 1662 ret = lxc_log_set_file(&c->logfd, c->logfile);
25a908b8 1663
6d03d92a 1664 return ret;
4a85ce2a
SH
1665}
1666
46cc906d 1667static int set_config_log_level(const char *key, const char *value,
c7e27aaf 1668 struct lxc_conf *lxc_conf, void *data)
4a85ce2a 1669{
9ea87d5d
SH
1670 int newlevel;
1671
663e9916 1672 if (lxc_config_value_empty(value)) {
4b73005c 1673 lxc_conf->loglevel = LXC_LOG_LEVEL_NOTSET;
4a85ce2a 1674 return 0;
575b9745 1675 }
4a85ce2a 1676
a56e2df9 1677 if (value[0] >= '0' && value[0] <= '9') {
806244c6
CB
1678 int ret;
1679
1680 ret = lxc_safe_int(value, &newlevel);
1681 if (ret)
1682 return ret_errno(EINVAL);
a56e2df9 1683 } else {
9ea87d5d 1684 newlevel = lxc_log_priority_to_int(value);
a56e2df9 1685 }
575b9745 1686
806244c6
CB
1687 /*
1688 * Store these values in the lxc_conf, and then try to set for actual
575b9745
CB
1689 * current logging.
1690 */
b40a606e 1691 lxc_conf->loglevel = newlevel;
47903908 1692
858377e4 1693 return lxc_log_set_level(&lxc_conf->loglevel, newlevel);
4a85ce2a
SH
1694}
1695
713046e3 1696static int set_config_autodev(const char *key, const char *value,
c7e27aaf 1697 struct lxc_conf *lxc_conf, void *data)
c6883f38 1698{
0c48b874
CB
1699 int ret;
1700
663e9916 1701 if (lxc_config_value_empty(value)) {
1045031e 1702 lxc_conf->autodev = 0;
180abbc0 1703 return 0;
1045031e 1704 }
180abbc0 1705
0c48b874
CB
1706 ret = lxc_safe_uint(value, &lxc_conf->autodev);
1707 if (ret)
1708 return ret_errno(EINVAL);
c6883f38 1709
25a908b8 1710 if (lxc_conf->autodev > 1)
0c48b874 1711 return ret_errno(EINVAL);
c6883f38
SH
1712
1713 return 0;
1714}
1715
63012bdd
CK
1716static int set_config_autodev_tmpfs_size(const char *key, const char *value,
1717 struct lxc_conf *lxc_conf, void *data)
1718{
1719 if (lxc_config_value_empty(value)) {
1720 lxc_conf->autodevtmpfssize = 500000;
1721 return 0;
1722 }
1723
1724 if (lxc_safe_int(value, &lxc_conf->autodevtmpfssize) < 0)
1725 lxc_conf->autodevtmpfssize = 500000;
1726
1727 return 0;
1728}
1729
55c84efc 1730static int set_config_signal_halt(const char *key, const char *value,
c7e27aaf 1731 struct lxc_conf *lxc_conf, void *data)
f0f1d8c0 1732{
62a085fb 1733 int sig_n;
f0f1d8c0 1734
663e9916 1735 if (lxc_config_value_empty(value)) {
c1a64603 1736 lxc_conf->haltsignal = 0;
955912f0 1737 return 0;
c1a64603 1738 }
955912f0 1739
62a085fb 1740 sig_n = sig_parse(value);
f0f1d8c0 1741 if (sig_n < 0)
d12fabf8 1742 return ret_errno(EINVAL);
25a908b8 1743
f0f1d8c0
DE
1744 lxc_conf->haltsignal = sig_n;
1745
1746 return 0;
1747}
1748
55c84efc 1749static int set_config_signal_reboot(const char *key, const char *value,
c7e27aaf 1750 struct lxc_conf *lxc_conf, void *data)
dd267776 1751{
9d7e7587 1752 int sig_n;
dd267776 1753
663e9916 1754 if (lxc_config_value_empty(value)) {
18fcee44 1755 lxc_conf->rebootsignal = 0;
9d7e7587 1756 return 0;
18fcee44 1757 }
9d7e7587
CB
1758
1759 sig_n = sig_parse(value);
dd267776 1760 if (sig_n < 0)
7d6b1a20 1761 return ret_errno(EINVAL);
25a908b8 1762
dd267776
BP
1763 lxc_conf->rebootsignal = sig_n;
1764
1765 return 0;
1766}
1767
55c84efc 1768static int set_config_signal_stop(const char *key, const char *value,
c7e27aaf 1769 struct lxc_conf *lxc_conf, void *data)
a84b9932 1770{
6ca6aedd 1771 int sig_n;
a84b9932 1772
663e9916 1773 if (lxc_config_value_empty(value)) {
4100d1a7 1774 lxc_conf->stopsignal = 0;
6ca6aedd 1775 return 0;
4100d1a7 1776 }
6ca6aedd
CB
1777
1778 sig_n = sig_parse(value);
a84b9932 1779 if (sig_n < 0)
c4d9b159 1780 return ret_errno(EINVAL);
25a908b8 1781
a84b9932
AV
1782 lxc_conf->stopsignal = sig_n;
1783
1784 return 0;
1785}
1786
54860ed0
CB
1787static int __set_config_cgroup_controller(const char *key, const char *value,
1788 struct lxc_conf *lxc_conf, int version)
576f946d 1789{
ee91fa06
CB
1790 __do_free struct lxc_list *cglist = NULL;
1791 call_cleaner(free_lxc_cgroup) struct lxc_cgroup *cgelem = NULL;
54860ed0
CB
1792 const char *subkey, *token;
1793 size_t token_len;
576f946d 1794
663e9916 1795 if (lxc_config_value_empty(value))
54860ed0
CB
1796 return lxc_clear_cgroups(lxc_conf, key, version);
1797
1798 if (version == CGROUP2_SUPER_MAGIC) {
1799 token = "lxc.cgroup2.";
1800 token_len = 12;
1801 } else if (version == CGROUP_SUPER_MAGIC) {
1802 token = "lxc.cgroup.";
1803 token_len = 11;
1804 } else {
ee91fa06 1805 return ret_errno(EINVAL);
54860ed0 1806 }
576f946d 1807
1af3044f 1808 if (!strnequal(key, token, token_len))
ee91fa06 1809 return ret_errno(EINVAL);
a871ff6b 1810
54860ed0
CB
1811 subkey = key + token_len;
1812 if (*subkey == '\0')
ee91fa06 1813 return ret_errno(EINVAL);
576f946d 1814
642751cc 1815 cglist = lxc_list_new();
576f946d 1816 if (!cglist)
ee91fa06 1817 return ret_errno(ENOMEM);
576f946d 1818
642751cc 1819 cgelem = zalloc(sizeof(*cgelem));
bf83c5b9 1820 if (!cgelem)
ee91fa06 1821 return ret_errno(ENOMEM);
576f946d 1822
1823 cgelem->subsystem = strdup(subkey);
54860ed0 1824 if (!cgelem->subsystem)
ee91fa06 1825 return ret_errno(ENOMEM);
bf83c5b9 1826
54860ed0
CB
1827 cgelem->value = strdup(value);
1828 if (!cgelem->value)
ee91fa06 1829 return ret_errno(ENOMEM);
bf83c5b9 1830
54860ed0
CB
1831 cgelem->version = version;
1832
ee91fa06 1833 lxc_list_add_elem(cglist, move_ptr(cgelem));
576f946d 1834
54860ed0
CB
1835 if (version == CGROUP2_SUPER_MAGIC)
1836 lxc_list_add_tail(&lxc_conf->cgroup2, cglist);
1837 else
1838 lxc_list_add_tail(&lxc_conf->cgroup, cglist);
ee91fa06 1839 move_ptr(cglist);
576f946d 1840
1841 return 0;
1842}
1843
54860ed0
CB
1844static int set_config_cgroup_controller(const char *key, const char *value,
1845 struct lxc_conf *lxc_conf, void *data)
1846{
1847 return __set_config_cgroup_controller(key, value, lxc_conf,
1848 CGROUP_SUPER_MAGIC);
1849}
1850
1851static int set_config_cgroup2_controller(const char *key, const char *value,
1852 struct lxc_conf *lxc_conf, void *data)
1853{
1854 return __set_config_cgroup_controller(key, value, lxc_conf,
1855 CGROUP2_SUPER_MAGIC);
1856}
1857
43654d34
CB
1858static int set_config_cgroup_dir(const char *key, const char *value,
1859 struct lxc_conf *lxc_conf, void *data)
1860{
d62177e9 1861 if (!strequal(key, "lxc.cgroup.dir"))
c583072d
CB
1862 return ret_errno(EINVAL);
1863
43654d34
CB
1864 if (lxc_config_value_empty(value))
1865 return clr_config_cgroup_dir(key, lxc_conf, NULL);
1866
f63ef155 1867 if (abspath(value))
060aaa39 1868 return syserror_set(-EINVAL, "%s paths may not be absolute", key);
f63ef155 1869
0a48ee66 1870 if (dotdot(value))
060aaa39 1871 return syserror_set(-EINVAL, "%s paths may not walk upwards via \"../\"", key);
0a48ee66 1872
ee94a8b5 1873 return set_config_path_item(&lxc_conf->cgroup_meta.dir, value);
43654d34
CB
1874}
1875
a900cbaf
WB
1876static int set_config_cgroup_monitor_dir(const char *key, const char *value,
1877 struct lxc_conf *lxc_conf, void *data)
1878{
1879 if (lxc_config_value_empty(value))
1880 return clr_config_cgroup_monitor_dir(key, lxc_conf, NULL);
1881
f63ef155 1882 if (abspath(value))
060aaa39 1883 return syserror_set(-EINVAL, "%s paths may not be absolute", key);
f63ef155 1884
0a48ee66 1885 if (dotdot(value))
060aaa39 1886 return syserror_set(-EINVAL, "%s paths may not walk upwards via \"../\"", key);
0a48ee66 1887
ee94a8b5 1888 return set_config_path_item(&lxc_conf->cgroup_meta.monitor_dir, value);
a900cbaf
WB
1889}
1890
7696c1f9
RJ
1891static int set_config_cgroup_monitor_pivot_dir(const char *key, const char *value,
1892 struct lxc_conf *lxc_conf, void *data)
1893{
1894 if (lxc_config_value_empty(value))
1895 return clr_config_cgroup_monitor_pivot_dir(key, lxc_conf, NULL);
1896
f63ef155 1897 if (abspath(value))
060aaa39 1898 return syserror_set(-EINVAL, "%s paths may not be absolute", key);
f63ef155 1899
0a48ee66 1900 if (dotdot(value))
060aaa39 1901 return syserror_set(-EINVAL, "%s paths may not walk upwards via \"../\"", key);
0a48ee66 1902
ee94a8b5 1903 return set_config_path_item(&lxc_conf->cgroup_meta.monitor_pivot_dir, value);
7696c1f9
RJ
1904}
1905
a900cbaf
WB
1906static int set_config_cgroup_container_dir(const char *key, const char *value,
1907 struct lxc_conf *lxc_conf,
1908 void *data)
1909{
1910 if (lxc_config_value_empty(value))
1911 return clr_config_cgroup_container_dir(key, lxc_conf, NULL);
1912
f63ef155 1913 if (abspath(value))
060aaa39 1914 return syserror_set(-EINVAL, "%s paths may not be absolute", key);
f63ef155 1915
0a48ee66 1916 if (dotdot(value))
060aaa39 1917 return syserror_set(-EINVAL, "%s paths may not walk upwards via \"../\"", key);
0a48ee66 1918
ee94a8b5 1919 return set_config_path_item(&lxc_conf->cgroup_meta.container_dir, value);
a900cbaf
WB
1920}
1921
1922static int set_config_cgroup_container_inner_dir(const char *key,
1923 const char *value,
1924 struct lxc_conf *lxc_conf,
1925 void *data)
1926{
1927 if (lxc_config_value_empty(value))
e93197e7 1928 return clr_config_cgroup_container_inner_dir(key, lxc_conf, NULL);
a900cbaf 1929
f63ef155 1930 if (abspath(value))
060aaa39 1931 return syserror_set(-EINVAL, "%s paths may not be absolute", key);
f63ef155 1932
d62177e9 1933 if (strchr(value, '/') || strequal(value, ".") || strequal(value, ".."))
e93197e7 1934 return log_error_errno(-EINVAL, EINVAL, "lxc.cgroup.dir.container.inner must be a single directory name");
a900cbaf 1935
e93197e7 1936 return set_config_string_item(&lxc_conf->cgroup_meta.namespace_dir, value);
a900cbaf
WB
1937}
1938
9caee129
CB
1939static int set_config_cgroup_relative(const char *key, const char *value,
1940 struct lxc_conf *lxc_conf, void *data)
76f0e2e7
CB
1941{
1942 unsigned int converted;
1943 int ret;
1944
1945 if (lxc_config_value_empty(value))
9caee129 1946 return clr_config_cgroup_relative(key, lxc_conf, NULL);
76f0e2e7
CB
1947
1948 ret = lxc_safe_uint(value, &converted);
c521771a
CB
1949 if (ret)
1950 return ret;
76f0e2e7
CB
1951
1952 if (converted == 1) {
9caee129 1953 lxc_conf->cgroup_meta.relative = true;
76f0e2e7
CB
1954 return 0;
1955 }
1956
1957 if (converted == 0) {
9caee129 1958 lxc_conf->cgroup_meta.relative = false;
76f0e2e7
CB
1959 return 0;
1960 }
1961
c521771a 1962 return ret_errno(EINVAL);
76f0e2e7
CB
1963}
1964
f7662514
CB
1965static bool parse_limit_value(const char **value, rlim_t *res)
1966{
1967 char *endptr = NULL;
1968
1af3044f 1969 if (strnequal(*value, "unlimited", STRLITERALLEN("unlimited"))) {
f7662514
CB
1970 *res = RLIM_INFINITY;
1971 *value += STRLITERALLEN("unlimited");
1972 return true;
1973 }
1974
1975 errno = 0;
1976 *res = strtoull(*value, &endptr, 10);
1977 if (errno || !endptr)
1978 return false;
1979
1980 *value = endptr;
1981
1982 return true;
1983}
1984
240d4b74 1985static int set_config_prlimit(const char *key, const char *value,
c7e27aaf 1986 struct lxc_conf *lxc_conf, void *data)
c6d09e15 1987{
631d2715
CB
1988 __do_free struct lxc_list *list = NULL;
1989 call_cleaner(free_lxc_limit) struct lxc_limit *elem = NULL;
c6d09e15
WB
1990 struct lxc_list *iter;
1991 struct rlimit limit;
71460831 1992 rlim_t limit_value;
c6d09e15 1993
663e9916 1994 if (lxc_config_value_empty(value))
c6d09e15
WB
1995 return lxc_clear_limits(lxc_conf, key);
1996
1af3044f 1997 if (!strnequal(key, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")))
8fa831e0 1998 return ret_errno(EINVAL);
c6d09e15 1999
6333c915 2000 key += STRLITERALLEN("lxc.prlimit.");
c6d09e15
WB
2001
2002 /* soft limit comes first in the value */
2003 if (!parse_limit_value(&value, &limit_value))
8fa831e0 2004 return ret_errno(EINVAL);
47903908 2005
c6d09e15
WB
2006 limit.rlim_cur = limit_value;
2007
2008 /* skip spaces and a colon */
2009 while (isspace(*value))
2010 ++value;
504a2217 2011
c6d09e15
WB
2012 if (*value == ':')
2013 ++value;
2014 else if (*value) /* any other character is an error here */
8fa831e0 2015 return ret_errno(EINVAL);
504a2217 2016
c6d09e15
WB
2017 while (isspace(*value))
2018 ++value;
2019
2020 /* optional hard limit */
2021 if (*value) {
2022 if (!parse_limit_value(&value, &limit_value))
8fa831e0 2023 return ret_errno(EINVAL);
47903908 2024
c6d09e15 2025 limit.rlim_max = limit_value;
504a2217 2026
c6d09e15
WB
2027 /* check for trailing garbage */
2028 while (isspace(*value))
2029 ++value;
504a2217 2030
c6d09e15 2031 if (*value)
8fa831e0 2032 return ret_errno(EINVAL);
c6d09e15
WB
2033 } else {
2034 /* a single value sets both hard and soft limit */
2035 limit.rlim_max = limit.rlim_cur;
2036 }
2037
2038 /* find existing list element */
7edd0540 2039 lxc_list_for_each(iter, &lxc_conf->limits) {
631d2715
CB
2040 struct lxc_limit *cur = iter->elem;
2041
2042 if (!strequal(key, cur->resource))
2043 continue;
2044
2045 cur->limit = limit;
2046 return 0;
c6d09e15
WB
2047 }
2048
2049 /* allocate list element */
631d2715
CB
2050 list = lxc_list_new();
2051 if (!list)
8fa831e0 2052 return ret_errno(ENOMEM);
2e6e3feb 2053
631d2715
CB
2054 elem = zalloc(sizeof(*elem));
2055 if (!elem)
8fa831e0 2056 return ret_errno(ENOMEM);
c6d09e15 2057
631d2715
CB
2058 elem->resource = strdup(key);
2059 if (!elem->resource)
8fa831e0 2060 return ret_errno(ENOMEM);
c6d09e15 2061
631d2715
CB
2062 elem->limit = limit;
2063 lxc_list_add_elem(list, move_ptr(elem));;
2064 lxc_list_add_tail(&lxc_conf->limits, move_ptr(list));
c6d09e15
WB
2065
2066 return 0;
c6d09e15
WB
2067}
2068
7edd0540
L
2069static int set_config_sysctl(const char *key, const char *value,
2070 struct lxc_conf *lxc_conf, void *data)
2071{
f10c80d2
CB
2072 __do_free struct lxc_list *sysctl_list = NULL;
2073 call_cleaner(free_lxc_sysctl) struct lxc_sysctl *sysctl_elem = NULL;
7edd0540 2074 struct lxc_list *iter;
7edd0540
L
2075
2076 if (lxc_config_value_empty(value))
e409b214 2077 return clr_config_sysctl(key, lxc_conf, NULL);
7edd0540 2078
1af3044f 2079 if (!strnequal(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
7edd0540
L
2080 return -1;
2081
6333c915 2082 key += STRLITERALLEN("lxc.sysctl.");
b5fdc164
CB
2083 if (is_empty_string(key))
2084 return ret_errno(-EINVAL);
7edd0540
L
2085
2086 /* find existing list element */
2087 lxc_list_for_each(iter, &lxc_conf->sysctls) {
f10c80d2 2088 __do_free char *replace_value = NULL;
b5fdc164 2089 struct lxc_sysctl *cur = iter->elem;
f10c80d2 2090
b5fdc164 2091 if (!strequal(key, cur->key))
e409b214
CB
2092 continue;
2093
2094 replace_value = strdup(value);
2095 if (!replace_value)
f10c80d2 2096 return ret_errno(EINVAL);
e409b214 2097
b5fdc164
CB
2098 free(cur->value);
2099 cur->value = move_ptr(replace_value);
47903908 2100
e409b214 2101 return 0;
7edd0540
L
2102 }
2103
2104 /* allocate list element */
642751cc 2105 sysctl_list = lxc_list_new();
7edd0540 2106 if (!sysctl_list)
f10c80d2 2107 return ret_errno(ENOMEM);
7edd0540 2108
b5fdc164 2109 sysctl_elem = zalloc(sizeof(*sysctl_elem));
7edd0540 2110 if (!sysctl_elem)
f10c80d2 2111 return ret_errno(ENOMEM);
7edd0540
L
2112
2113 sysctl_elem->key = strdup(key);
2114 if (!sysctl_elem->key)
f10c80d2 2115 return ret_errno(ENOMEM);
7edd0540
L
2116
2117 sysctl_elem->value = strdup(value);
2118 if (!sysctl_elem->value)
f10c80d2 2119 return ret_errno(ENOMEM);
7edd0540 2120
f10c80d2
CB
2121 lxc_list_add_elem(sysctl_list, move_ptr(sysctl_elem));
2122 lxc_list_add_tail(&lxc_conf->sysctls, move_ptr(sysctl_list));
7edd0540
L
2123
2124 return 0;
7edd0540
L
2125}
2126
61d7a733
YT
2127static int set_config_proc(const char *key, const char *value,
2128 struct lxc_conf *lxc_conf, void *data)
2129{
83332c24
CB
2130 __do_free struct lxc_list *proclist = NULL;
2131 call_cleaner(free_lxc_proc) struct lxc_proc *procelem = NULL;
61d7a733 2132 const char *subkey;
61d7a733
YT
2133
2134 if (lxc_config_value_empty(value))
2135 return clr_config_proc(key, lxc_conf, NULL);
2136
1af3044f 2137 if (!strnequal(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
61d7a733
YT
2138 return -1;
2139
6333c915 2140 subkey = key + STRLITERALLEN("lxc.proc.");
61d7a733 2141 if (*subkey == '\0')
83332c24 2142 return ret_errno(EINVAL);
61d7a733 2143
642751cc 2144 proclist = lxc_list_new();
61d7a733 2145 if (!proclist)
83332c24 2146 return ret_errno(ENOMEM);
61d7a733 2147
642751cc 2148 procelem = zalloc(sizeof(*procelem));
61d7a733 2149 if (!procelem)
83332c24 2150 return ret_errno(ENOMEM);
61d7a733
YT
2151
2152 procelem->filename = strdup(subkey);
83332c24
CB
2153 if (!procelem->filename)
2154 return ret_errno(ENOMEM);
61d7a733 2155
83332c24
CB
2156 procelem->value = strdup(value);
2157 if (!procelem->value)
2158 return ret_errno(ENOMEM);
61d7a733 2159
83332c24
CB
2160 proclist->elem = move_ptr(procelem);
2161 lxc_list_add_tail(&lxc_conf->procs, move_ptr(proclist));
61d7a733
YT
2162
2163 return 0;
61d7a733
YT
2164}
2165
5014ff2e 2166static int set_config_idmaps(const char *key, const char *value,
c7e27aaf 2167 struct lxc_conf *lxc_conf, void *data)
f6d3e3e4 2168{
5c856bcb
CB
2169 __do_free struct lxc_list *idmaplist = NULL;
2170 __do_free struct id_map *idmap = NULL;
251d0d2a 2171 unsigned long hostid, nsid, range;
f6d3e3e4 2172 char type;
0b843d35 2173 int ret;
f6d3e3e4 2174
663e9916 2175 if (lxc_config_value_empty(value))
7d0eb87e
SH
2176 return lxc_clear_idmaps(lxc_conf);
2177
642751cc 2178 idmaplist = lxc_list_new();
f6d3e3e4 2179 if (!idmaplist)
5c856bcb 2180 return ret_errno(ENOMEM);
f6d3e3e4 2181
642751cc 2182 idmap = zalloc(sizeof(*idmap));
f6d3e3e4 2183 if (!idmap)
5c856bcb 2184 return ret_errno(ENOMEM);
f6d3e3e4 2185
0b843d35 2186 ret = parse_idmaps(value, &type, &nsid, &hostid, &range);
5c856bcb
CB
2187 if (ret < 0)
2188 return log_error_errno(-EINVAL, EINVAL, "Failed to parse id mappings");
34a7a4c6 2189
25a908b8 2190 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range);
ac7725e7 2191 if (type == 'u')
f6d3e3e4 2192 idmap->idtype = ID_TYPE_UID;
ac7725e7 2193 else if (type == 'g')
f6d3e3e4
SH
2194 idmap->idtype = ID_TYPE_GID;
2195 else
5c856bcb 2196 return ret_errno(EINVAL);
7e60c3f0 2197
f6d3e3e4
SH
2198 idmap->hostid = hostid;
2199 idmap->nsid = nsid;
2200 idmap->range = range;
7e60c3f0
SG
2201 idmaplist->elem = idmap;
2202 lxc_list_add_tail(&lxc_conf->id_map, idmaplist);
46ad64ab
CB
2203
2204 if (!lxc_conf->root_nsuid_map && idmap->idtype == ID_TYPE_UID)
2205 if (idmap->nsid == 0)
2206 lxc_conf->root_nsuid_map = idmap;
2207
4160c3a0 2208 if (!lxc_conf->root_nsgid_map && idmap->idtype == ID_TYPE_GID)
46ad64ab
CB
2209 if (idmap->nsid == 0)
2210 lxc_conf->root_nsgid_map = idmap;
2211
5c856bcb
CB
2212 move_ptr(idmap);
2213 move_ptr(idmaplist);
7e60c3f0 2214
f6d3e3e4 2215 return 0;
f6d3e3e4
SH
2216}
2217
47148e96
CB
2218static int set_config_mount_fstab(const char *key, const char *value,
2219 struct lxc_conf *lxc_conf, void *data)
d95db067 2220{
663e9916 2221 if (lxc_config_value_empty(value)) {
47148e96 2222 clr_config_mount_fstab(key, lxc_conf, NULL);
e9cda8ec 2223 return ret_errno(EINVAL);
46f3de30 2224 }
6f5685f0 2225
713046e3 2226 return set_config_path_item(&lxc_conf->fstab, value);
d95db067
DE
2227}
2228
713046e3 2229static int set_config_mount_auto(const char *key, const char *value,
c7e27aaf 2230 struct lxc_conf *lxc_conf, void *data)
368bbc02 2231{
138079ee
CB
2232 __do_free char *autos = NULL;
2233 char *token;
368bbc02 2234 int i;
504a2217
CB
2235 static struct {
2236 const char *token;
2237 int mask;
2238 int flag;
2239 } allowed_auto_mounts[] = {
138079ee
CB
2240 { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
2241 { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
2242 { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW },
2243 { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
2244 { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO },
2245 { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
2246 { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW },
2247 { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC },
2248 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED },
2249 { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO },
2250 { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW },
2251 { "cgroup:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC | LXC_AUTO_CGROUP_FORCE },
2252 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED | LXC_AUTO_CGROUP_FORCE },
2253 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO | LXC_AUTO_CGROUP_FORCE },
2254 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW | LXC_AUTO_CGROUP_FORCE },
2255 { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC },
2256 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED },
2257 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO },
2258 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW },
2259 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC | LXC_AUTO_CGROUP_FORCE },
2260 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED | LXC_AUTO_CGROUP_FORCE },
2261 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO | LXC_AUTO_CGROUP_FORCE },
2262 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW | LXC_AUTO_CGROUP_FORCE },
2263 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK, LXC_AUTO_SHMOUNTS },
2264 /*
2265 * For adding anything that is just a single on/off, but has no
2266 * options: keep mask and flag identical and just define the
2267 * enum value as an unused bit so far
2268 */
2269 { NULL, 0, 0 }
504a2217 2270 };
368bbc02 2271
663e9916 2272 if (lxc_config_value_empty(value)) {
d9192f5d
SH
2273 lxc_conf->auto_mounts = 0;
2274 return 0;
2275 }
368bbc02
CS
2276
2277 autos = strdup(value);
25a908b8 2278 if (!autos)
138079ee 2279 return ret_errno(ENOMEM);
368bbc02 2280
62dd965e 2281 lxc_iterate_parts(token, autos, " \t") {
0d190408
LT
2282 bool is_shmounts = false;
2283
368bbc02 2284 for (i = 0; allowed_auto_mounts[i].token; i++) {
d62177e9 2285 if (strequal(allowed_auto_mounts[i].token, token))
368bbc02 2286 break;
0d190408 2287
d62177e9 2288 if (strequal("shmounts:", allowed_auto_mounts[i].token) &&
1af3044f 2289 strnequal("shmounts:", token, STRLITERALLEN("shmounts:"))) {
0d190408
LT
2290 is_shmounts = true;
2291 break;
2292 }
368bbc02
CS
2293 }
2294
138079ee
CB
2295 if (!allowed_auto_mounts[i].token)
2296 return log_error_errno(-EINVAL, EINVAL, "Invalid filesystem to automount \"%s\"", token);
368bbc02 2297
b06b8511 2298 lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask;
368bbc02 2299 lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag;
47903908 2300
0d190408 2301 if (is_shmounts) {
138079ee
CB
2302 __do_free char *container_path = NULL, *host_path = NULL;
2303 char *val;
594426ca 2304
138079ee
CB
2305 val = token + STRLITERALLEN("shmounts:");
2306 if (*val == '\0')
2307 return log_error_errno(-EINVAL, EINVAL, "Failed to copy shmounts host path");
2308
2309 host_path = strdup(val);
2310 if (!host_path)
2311 return log_error_errno(-EINVAL, EINVAL, "Failed to copy shmounts host path");
fd14fdb8 2312
138079ee
CB
2313 val = strchr(host_path, ':');
2314 if (!val || *(val + 1) == '\0')
2315 val = "/dev/.lxc-mounts";
6437f1c1 2316 else
138079ee 2317 *val++ = '\0';
6437f1c1 2318
138079ee
CB
2319 container_path = strdup(val);
2320 if(!container_path)
2321 return log_error_errno(-EINVAL, EINVAL, "Failed to copy shmounts container path");
fd14fdb8 2322
405b28a4 2323 free_disarm(lxc_conf->shmount.path_host);
138079ee 2324 lxc_conf->shmount.path_host = move_ptr(host_path);
405b28a4
CB
2325
2326 free_disarm(lxc_conf->shmount.path_cont);
138079ee 2327 lxc_conf->shmount.path_cont = move_ptr(container_path);
0d190408 2328 }
d028235d 2329 }
368bbc02 2330
138079ee 2331 return 0;
368bbc02
CS
2332}
2333
713046e3 2334static int set_config_mount(const char *key, const char *value,
c7e27aaf 2335 struct lxc_conf *lxc_conf, void *data)
e7938e9e 2336{
48c367c0
CB
2337 __do_free char *mntelem = NULL;
2338 __do_free struct lxc_list *mntlist = NULL;
e7938e9e 2339
663e9916 2340 if (lxc_config_value_empty(value))
d9192f5d 2341 return lxc_clear_mount_entries(lxc_conf);
e7938e9e 2342
642751cc 2343 mntlist = lxc_list_new();
e7938e9e 2344 if (!mntlist)
48c367c0 2345 return ret_errno(ENOMEM);
e7938e9e
MN
2346
2347 mntelem = strdup(value);
48c367c0
CB
2348 if (!mntelem)
2349 return ret_errno(ENOMEM);
e7938e9e 2350
48c367c0
CB
2351 mntlist->elem = move_ptr(mntelem);
2352 lxc_list_add_tail(&lxc_conf->mount_list, move_ptr(mntlist));
e7938e9e
MN
2353
2354 return 0;
2355}
2356
0d190408
LT
2357int add_elem_to_mount_list(const char *value, struct lxc_conf *lxc_conf) {
2358 return set_config_mount(NULL, value, lxc_conf, NULL);
2359}
2360
713046e3 2361static int set_config_cap_keep(const char *key, const char *value,
c7e27aaf 2362 struct lxc_conf *lxc_conf, void *data)
1fb86a7c 2363{
bd7c5371
CB
2364 __do_free char *keepcaps = NULL;
2365 __do_free struct lxc_list *keeplist = NULL;
2366 char *token;
1fb86a7c 2367
663e9916 2368 if (lxc_config_value_empty(value))
7d0eb87e 2369 return lxc_clear_config_keepcaps(lxc_conf);
1fb86a7c
SH
2370
2371 keepcaps = strdup(value);
25a908b8 2372 if (!keepcaps)
bd7c5371 2373 return ret_errno(ENOMEM);
1fb86a7c 2374
504a2217
CB
2375 /* In case several capability keep is specified in a single line
2376 * split these caps in a single element for the list.
2377 */
62dd965e 2378 lxc_iterate_parts(token, keepcaps, " \t") {
d62177e9 2379 if (strequal(token, "none"))
7035407c
DE
2380 lxc_clear_config_keepcaps(lxc_conf);
2381
642751cc 2382 keeplist = lxc_list_new();
25a908b8 2383 if (!keeplist)
bd7c5371 2384 return ret_errno(ENOMEM);
1fb86a7c
SH
2385
2386 keeplist->elem = strdup(token);
bd7c5371
CB
2387 if (!keeplist->elem)
2388 return ret_errno(ENOMEM);
1fb86a7c 2389
bd7c5371 2390 lxc_list_add_tail(&lxc_conf->keepcaps, move_ptr(keeplist));
d028235d 2391 }
1fb86a7c 2392
bd7c5371 2393 return 0;
1fb86a7c
SH
2394}
2395
713046e3 2396static int set_config_cap_drop(const char *key, const char *value,
c7e27aaf 2397 struct lxc_conf *lxc_conf, void *data)
81810dd1 2398{
c5c4831c
CB
2399 __do_free char *dropcaps = NULL;
2400 __do_free struct lxc_list *droplist = NULL;
2401 char *token;
81810dd1 2402
663e9916 2403 if (lxc_config_value_empty(value))
7d0eb87e 2404 return lxc_clear_config_caps(lxc_conf);
81810dd1
DL
2405
2406 dropcaps = strdup(value);
25a908b8 2407 if (!dropcaps)
c5c4831c 2408 return ret_errno(ENOMEM);
81810dd1 2409
504a2217
CB
2410 /* In case several capability drop is specified in a single line
2411 * split these caps in a single element for the list.
2412 */
62dd965e 2413 lxc_iterate_parts(token, dropcaps, " \t") {
642751cc 2414 droplist = lxc_list_new();
25a908b8 2415 if (!droplist)
c5c4831c 2416 return ret_errno(ENOMEM);
81810dd1
DL
2417
2418 droplist->elem = strdup(token);
c5c4831c
CB
2419 if (!droplist->elem)
2420 return ret_errno(ENOMEM);
81810dd1 2421
c5c4831c 2422 lxc_list_add_tail(&lxc_conf->caps, move_ptr(droplist));
d028235d 2423 }
81810dd1 2424
c5c4831c 2425 return 0;
81810dd1
DL
2426}
2427
3aed4934
CB
2428static int set_config_console_path(const char *key, const char *value,
2429 struct lxc_conf *lxc_conf, void *data)
28a4b0e5 2430{
713046e3 2431 return set_config_path_item(&lxc_conf->console.path, value);
28a4b0e5
DL
2432}
2433
d91adfa6
CB
2434static int set_config_console_rotate(const char *key, const char *value,
2435 struct lxc_conf *lxc_conf, void *data)
2436{
9a26e4af
CB
2437 int ret;
2438
d91adfa6
CB
2439 if (lxc_config_value_empty(value)) {
2440 lxc_conf->console.log_rotate = 0;
2441 return 0;
2442 }
2443
9a26e4af
CB
2444 ret = lxc_safe_uint(value, &lxc_conf->console.log_rotate);
2445 if (ret)
2446 return ret_errno(EINVAL);
d91adfa6 2447
9a26e4af
CB
2448 if (lxc_conf->console.log_rotate > 1)
2449 return log_error_errno(-EINVAL, EINVAL, "The \"lxc.console.rotate\" config key can only be set to 0 or 1");
d91adfa6
CB
2450
2451 return 0;
2452}
2453
713046e3 2454static int set_config_console_logfile(const char *key, const char *value,
c7e27aaf 2455 struct lxc_conf *lxc_conf, void *data)
96f15ca1 2456{
713046e3 2457 return set_config_path_item(&lxc_conf->console.log_path, value);
96f15ca1
SH
2458}
2459
28f3b1cd
CB
2460static int set_config_console_buffer_size(const char *key, const char *value,
2461 struct lxc_conf *lxc_conf, void *data)
a04220de
CB
2462{
2463 int ret;
4c5479d2 2464 long long int size;
28f3b1cd 2465 uint64_t buffer_size, pgsz;
a04220de
CB
2466
2467 if (lxc_config_value_empty(value)) {
28f3b1cd 2468 lxc_conf->console.buffer_size = 0;
a04220de
CB
2469 return 0;
2470 }
2471
2472 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
d62177e9 2473 if (strequal(value, "auto")) {
28f3b1cd 2474 lxc_conf->console.buffer_size = 1 << 17;
a04220de
CB
2475 return 0;
2476 }
2477
2478 ret = parse_byte_size_string(value, &size);
3f5c01db
CB
2479 if (ret)
2480 return ret;
a04220de
CB
2481
2482 if (size < 0)
3f5c01db 2483 return ret_errno(EINVAL);
a04220de
CB
2484
2485 /* must be at least a page size */
2486 pgsz = lxc_getpagesize();
2487 if ((uint64_t)size < pgsz) {
4c5479d2 2488 NOTICE("Requested ringbuffer size for the console is %lld but must be at least %" PRId64 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
a04220de
CB
2489 size, pgsz, pgsz);
2490 size = pgsz;
2491 }
2492
28f3b1cd
CB
2493 buffer_size = lxc_find_next_power2((uint64_t)size);
2494 if (buffer_size == 0)
3f5c01db 2495 return ret_errno(EINVAL);
a04220de 2496
28f3b1cd 2497 if (buffer_size != size)
3f5c01db 2498 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64 " bytes", buffer_size);
a04220de 2499
28f3b1cd 2500 lxc_conf->console.buffer_size = buffer_size;
47903908 2501
a04220de
CB
2502 return 0;
2503}
2504
861813e5
CB
2505static int set_config_console_size(const char *key, const char *value,
2506 struct lxc_conf *lxc_conf, void *data)
2507{
2508 int ret;
4c5479d2 2509 long long int size;
861813e5
CB
2510 uint64_t log_size, pgsz;
2511
2512 if (lxc_config_value_empty(value)) {
2513 lxc_conf->console.log_size = 0;
2514 return 0;
2515 }
2516
2517 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
d62177e9 2518 if (strequal(value, "auto")) {
861813e5
CB
2519 lxc_conf->console.log_size = 1 << 17;
2520 return 0;
2521 }
2522
2523 ret = parse_byte_size_string(value, &size);
a7ac0d1e
CB
2524 if (ret)
2525 return ret_errno(EINVAL);
861813e5
CB
2526
2527 if (size < 0)
a7ac0d1e 2528 return ret_errno(EINVAL);
861813e5
CB
2529
2530 /* must be at least a page size */
2531 pgsz = lxc_getpagesize();
2532 if ((uint64_t)size < pgsz) {
4c5479d2 2533 NOTICE("Requested ringbuffer size for the console is %lld but must be at least %" PRId64 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
861813e5
CB
2534 size, pgsz, pgsz);
2535 size = pgsz;
2536 }
2537
2538 log_size = lxc_find_next_power2((uint64_t)size);
2539 if (log_size == 0)
a7ac0d1e 2540 return ret_errno(EINVAL);
861813e5
CB
2541
2542 if (log_size != size)
a7ac0d1e 2543 NOTICE("Passed size was not a power of 2. Rounding log size to next power of two: %" PRIu64 " bytes", log_size);
861813e5
CB
2544
2545 lxc_conf->console.log_size = log_size;
47903908 2546
861813e5
CB
2547 return 0;
2548}
2549
5648fc19
CB
2550/*
2551 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2552 * file, we expand it in the unexpanded_config, so that after a save_config we
2553 * store the hwaddr for re-use.
2554 * This is only called when reading the config file, not when executing a
2555 * lxc.include.
2556 * 'x' and 'X' are substituted in-place.
2557 */
2558static void update_hwaddr(const char *line)
2559{
2560 char *p;
2561
2562 line += lxc_char_left_gc(line, strlen(line));
2563 if (line[0] == '#')
2564 return;
2565
2566 if (!lxc_config_net_is_hwaddr(line))
2567 return;
2568
2569 /* Let config_net_hwaddr raise the error. */
2570 p = strchr(line, '=');
2571 if (!p)
2572 return;
2573 p++;
2574
2575 while (isblank(*p))
2576 p++;
2577
2578 if (!*p)
2579 return;
2580
2581 rand_complete_hwaddr(p);
2582}
2583
6b0d5538 2584int append_unexp_config_line(const char *line, struct lxc_conf *conf)
f979ac15 2585{
62a821f1
CB
2586 size_t linelen;
2587 size_t len = conf->unexpanded_len;
f979ac15 2588
e6744e9b
SH
2589 update_hwaddr(line);
2590
62a821f1 2591 linelen = strlen(line);
6b0d5538 2592 while (conf->unexpanded_alloced <= len + linelen + 2) {
1161f50d
CB
2593 char *tmp;
2594
2595 tmp = realloc(conf->unexpanded_config, conf->unexpanded_alloced + 1024);
6b0d5538 2596 if (!tmp)
1161f50d 2597 return ret_errno(EINVAL);
504a2217 2598
6b0d5538
SH
2599 if (!conf->unexpanded_config)
2600 *tmp = '\0';
62a821f1 2601
6b0d5538
SH
2602 conf->unexpanded_config = tmp;
2603 conf->unexpanded_alloced += 1024;
2604 }
efed99a4 2605
62a821f1 2606 memcpy(conf->unexpanded_config + conf->unexpanded_len, line, linelen);
6b0d5538 2607 conf->unexpanded_len += linelen;
62a821f1
CB
2608 if (line[linelen - 1] != '\n')
2609 conf->unexpanded_config[conf->unexpanded_len++] = '\n';
2610 conf->unexpanded_config[conf->unexpanded_len] = '\0';
25a908b8 2611
f979ac15
SH
2612 return 0;
2613}
2614
e1daebd9
SH
2615static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf)
2616{
4110345b 2617 __do_closedir DIR *dir = NULL;
74f96976 2618 struct dirent *direntp;
c1b2319b 2619 int len, ret;
e1daebd9
SH
2620
2621 dir = opendir(dirp);
25a908b8 2622 if (!dir)
c1b2319b 2623 return -errno;
e1daebd9 2624
74f96976 2625 while ((direntp = readdir(dir))) {
e1daebd9 2626 const char *fnam;
c1b2319b 2627 char path[PATH_MAX];
e1daebd9
SH
2628
2629 fnam = direntp->d_name;
d62177e9 2630 if (strequal(fnam, "."))
e1daebd9
SH
2631 continue;
2632
d62177e9 2633 if (strequal(fnam, ".."))
e1daebd9
SH
2634 continue;
2635
2636 len = strlen(fnam);
1af3044f 2637 if (len < 6 || !strnequal(fnam + len - 5, ".conf", 5))
e1daebd9 2638 continue;
25a908b8 2639
3948c252
CB
2640 len = strnprintf(path, sizeof(path), "%s/%s", dirp, fnam);
2641 if (len < 0)
c1b2319b 2642 return ret_errno(EIO);
e1daebd9
SH
2643
2644 ret = lxc_config_read(path, lxc_conf, true);
2645 if (ret < 0)
c1b2319b 2646 return ret;
e1daebd9 2647 }
e1daebd9 2648
4110345b 2649 return 0;
e1daebd9
SH
2650}
2651
973082f5 2652static int set_config_includefiles(const char *key, const char *value,
c7e27aaf 2653 struct lxc_conf *lxc_conf, void *data)
09ad6246 2654{
663e9916 2655 if (lxc_config_value_empty(value)) {
26471403 2656 clr_config_includefiles(key, lxc_conf, NULL);
616422f1 2657 return 0;
355c5701 2658 }
616422f1 2659
e1daebd9
SH
2660 if (is_dir(value))
2661 return do_includedir(value, lxc_conf);
2662
6b0d5538 2663 return lxc_config_read(value, lxc_conf, true);
09ad6246
SH
2664}
2665
7a96a068
CB
2666static int set_config_rootfs_path(const char *key, const char *value,
2667 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 2668{
d8cf0289 2669 __do_free char *dup = NULL;
693dbdb9 2670 int ret;
d8cf0289 2671 char *tmp;
693dbdb9
CB
2672 const char *container_path;
2673
2674 if (lxc_config_value_empty(value)) {
2675 free(lxc_conf->rootfs.path);
2676 lxc_conf->rootfs.path = NULL;
2677 return 0;
2678 }
2679
2680 dup = strdup(value);
2681 if (!dup)
d8cf0289 2682 return ret_errno(ENOMEM);
693dbdb9
CB
2683
2684 /* Split <storage type>:<container path> into <storage type> and
2685 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2686 * "rootfs.path" to <container path>.
2687 */
2688 tmp = strchr(dup, ':');
2689 if (tmp) {
2690 *tmp = '\0';
47903908 2691
693dbdb9 2692 ret = set_config_path_item(&lxc_conf->rootfs.bdev_type, dup);
d8cf0289
CB
2693 if (ret < 0)
2694 return ret_errno(ENOMEM);
47903908 2695
693dbdb9
CB
2696 tmp++;
2697 container_path = tmp;
2698 } else {
2699 container_path = value;
2700 }
2701
d8cf0289 2702 return set_config_path_item(&lxc_conf->rootfs.path, container_path);
c2cc9f0a 2703}
2704
6e54330c
CB
2705static int set_config_rootfs_managed(const char *key, const char *value,
2706 struct lxc_conf *lxc_conf, void *data)
2707{
8f818a84 2708 return set_config_bool_item(&lxc_conf->rootfs.managed, value, true);
6e54330c
CB
2709}
2710
713046e3 2711static int set_config_rootfs_mount(const char *key, const char *value,
c7e27aaf 2712 struct lxc_conf *lxc_conf, void *data)
23b7ea69 2713{
713046e3 2714 return set_config_path_item(&lxc_conf->rootfs.mount, value);
23b7ea69
DL
2715}
2716
713046e3 2717static int set_config_rootfs_options(const char *key, const char *value,
c7e27aaf 2718 struct lxc_conf *lxc_conf, void *data)
a17b1e65 2719{
f9d29e1f 2720 __do_free char *mdata = NULL, *opts = NULL;
3437f95c 2721 unsigned long mflags = 0, pflags = 0;
3437f95c 2722 struct lxc_rootfs *rootfs = &lxc_conf->rootfs;
f9d29e1f 2723 int ret;
3437f95c 2724
c04f4221
CB
2725 clr_config_rootfs_options(key, lxc_conf, data);
2726 if (lxc_config_value_empty(value))
2727 return 0;
2728
3437f95c
CB
2729 ret = parse_mntopts(value, &mflags, &mdata);
2730 if (ret < 0)
f9d29e1f 2731 return ret_errno(EINVAL);
3437f95c
CB
2732
2733 ret = parse_propagationopts(value, &pflags);
f9d29e1f
CB
2734 if (ret < 0)
2735 return ret_errno(EINVAL);
3437f95c
CB
2736
2737 ret = set_config_string_item(&opts, value);
f9d29e1f
CB
2738 if (ret < 0)
2739 return ret_errno(ENOMEM);
3437f95c 2740
c04f4221
CB
2741 rootfs->mountflags = mflags | pflags;
2742 rootfs->options = move_ptr(opts);
2743 rootfs->data = move_ptr(mdata);
3437f95c
CB
2744
2745 return 0;
a17b1e65
SG
2746}
2747
b67771bc 2748static int set_config_uts_name(const char *key, const char *value,
c7e27aaf 2749 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 2750{
cde6d8b4 2751 __do_free struct utsname *utsname = NULL;
c2cc9f0a 2752
663e9916 2753 if (lxc_config_value_empty(value)) {
b67771bc 2754 clr_config_uts_name(key, lxc_conf, NULL);
1939e73d 2755 return 0;
00cd8039 2756 }
1939e73d 2757
642751cc 2758 utsname = zalloc(sizeof(*utsname));
25a908b8 2759 if (!utsname)
cde6d8b4 2760 return ret_errno(ENOMEM);
c2cc9f0a 2761
cde6d8b4
CB
2762 if (strlen(value) >= sizeof(utsname->nodename))
2763 return ret_errno(EINVAL);
c2cc9f0a 2764
43f984ea 2765 (void)strlcpy(utsname->nodename, value, sizeof(utsname->nodename));
f10fad2f 2766 free(lxc_conf->utsname);
cde6d8b4 2767 lxc_conf->utsname = move_ptr(utsname);
c2cc9f0a 2768
2769 return 0;
2770}
2771
1d8d3676
CB
2772static int set_config_namespace_clone(const char *key, const char *value,
2773 struct lxc_conf *lxc_conf, void *data)
2774{
7454047d
CB
2775 __do_free char *ns = NULL;
2776 char *token;
1d8d3676 2777 int cloneflag = 0;
1d8d3676
CB
2778
2779 if (lxc_config_value_empty(value))
2780 return clr_config_namespace_clone(key, lxc_conf, data);
2781
7454047d
CB
2782 if (lxc_conf->ns_keep != 0)
2783 return log_error_errno(-EINVAL, EINVAL, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
8bc8c715 2784
1d8d3676
CB
2785 ns = strdup(value);
2786 if (!ns)
7454047d 2787 return ret_errno(ENOMEM);
1d8d3676 2788
62dd965e 2789 lxc_iterate_parts(token, ns, " \t") {
1d8d3676
CB
2790 token += lxc_char_left_gc(token, strlen(token));
2791 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2792 cloneflag = lxc_namespace_2_cloneflag(token);
7454047d
CB
2793 if (cloneflag < 0)
2794 return ret_errno(EINVAL);
1d8d3676
CB
2795 lxc_conf->ns_clone |= cloneflag;
2796 }
1d8d3676
CB
2797
2798 return 0;
2799}
2800
abeb5bba
CB
2801static int set_config_namespace_keep(const char *key, const char *value,
2802 struct lxc_conf *lxc_conf, void *data)
2803{
6c90df0e
CB
2804 __do_free char *ns = NULL;
2805 char *token;
abeb5bba 2806 int cloneflag = 0;
abeb5bba
CB
2807
2808 if (lxc_config_value_empty(value))
2809 return clr_config_namespace_keep(key, lxc_conf, data);
2810
6c90df0e
CB
2811 if (lxc_conf->ns_clone != 0)
2812 return log_error_errno(-EINVAL, EINVAL, "Cannot set both \"lxc.namespace.clone\" and \"lxc.namespace.keep\"");
8bc8c715 2813
abeb5bba
CB
2814 ns = strdup(value);
2815 if (!ns)
6c90df0e 2816 return ret_errno(ENOMEM);
abeb5bba 2817
62dd965e 2818 lxc_iterate_parts(token, ns, " \t") {
abeb5bba
CB
2819 token += lxc_char_left_gc(token, strlen(token));
2820 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2821 cloneflag = lxc_namespace_2_cloneflag(token);
6c90df0e
CB
2822 if (cloneflag < 0)
2823 return ret_errno(EINVAL);
abeb5bba
CB
2824 lxc_conf->ns_keep |= cloneflag;
2825 }
abeb5bba
CB
2826
2827 return 0;
2828}
2829
70fd7fc9
CB
2830static int set_config_time_offset_boot(const char *key, const char *value,
2831 struct lxc_conf *lxc_conf, void *data)
2832{
2833 int ret;
2834 char *unit;
2835 int64_t offset = 0;
2836 char buf[STRLITERALLEN("ms") + 1];
2837
2838 if (lxc_config_value_empty(value))
2839 return clr_config_time_offset_boot(key, lxc_conf, data);
2840
2841 ret = lxc_safe_int64_residual(value, &offset, 10, buf, sizeof(buf));
2842 if (ret)
2843 return ret;
2844
70fd7fc9 2845 unit = lxc_trim_whitespace_in_place(buf);
d62177e9 2846 if (strequal(unit, "h")) {
07f89c1e 2847 if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_boot))
b9965fd7 2848 return ret_errno(EOVERFLOW);
d62177e9 2849 } else if (strequal(unit, "m")) {
07f89c1e 2850 if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_boot))
b9965fd7 2851 return ret_errno(EOVERFLOW);
d62177e9 2852 } else if (strequal(unit, "s")) {
70fd7fc9 2853 lxc_conf->timens.s_boot = offset;
d62177e9 2854 } else if (strequal(unit, "ms")) {
07f89c1e 2855 if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_boot))
b9965fd7 2856 return ret_errno(EOVERFLOW);
d62177e9 2857 } else if (strequal(unit, "us")) {
07f89c1e 2858 if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_boot))
b9965fd7 2859 return ret_errno(EOVERFLOW);
d62177e9 2860 } else if (strequal(unit, "ns")) {
70fd7fc9 2861 lxc_conf->timens.ns_boot = offset;
07f89c1e 2862 } else {
70fd7fc9 2863 return ret_errno(EINVAL);
07f89c1e 2864 }
70fd7fc9
CB
2865
2866 return 0;
2867}
2868
2869static int set_config_time_offset_monotonic(const char *key, const char *value,
2870 struct lxc_conf *lxc_conf, void *data)
2871{
2872 int ret;
2873 char *unit;
2874 int64_t offset = 0;
2875 char buf[STRLITERALLEN("ms") + 1];
2876
2877 if (lxc_config_value_empty(value))
2878 return clr_config_time_offset_monotonic(key, lxc_conf, data);
2879
2880 ret = lxc_safe_int64_residual(value, &offset, 10, buf, sizeof(buf));
2881 if (ret)
2882 return ret;
2883
70fd7fc9 2884 unit = lxc_trim_whitespace_in_place(buf);
d62177e9 2885 if (strequal(unit, "h")) {
07f89c1e 2886 if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_monotonic))
8f204445 2887 return ret_errno(EOVERFLOW);
d62177e9 2888 } else if (strequal(unit, "m")) {
07f89c1e 2889 if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_monotonic))
8f204445 2890 return ret_errno(EOVERFLOW);
d62177e9 2891 } else if (strequal(unit, "s")) {
70fd7fc9 2892 lxc_conf->timens.s_monotonic = offset;
d62177e9 2893 } else if (strequal(unit, "ms")) {
07f89c1e 2894 if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_monotonic))
8f204445 2895 return ret_errno(EOVERFLOW);
d62177e9 2896 } else if (strequal(unit, "us")) {
07f89c1e 2897 if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_monotonic))
8f204445 2898 return ret_errno(EOVERFLOW);
d62177e9 2899 } else if (strequal(unit, "ns")) {
70fd7fc9 2900 lxc_conf->timens.ns_monotonic = offset;
07f89c1e 2901 } else {
70fd7fc9 2902 return ret_errno(EINVAL);
07f89c1e 2903 }
70fd7fc9
CB
2904
2905 return 0;
2906}
2907
b074bbf1
CB
2908static int set_config_namespace_share(const char *key, const char *value,
2909 struct lxc_conf *lxc_conf, void *data)
28d9e29e
CB
2910{
2911 int ns_idx;
2912 const char *namespace;
2913
2914 if (lxc_config_value_empty(value))
b074bbf1 2915 return clr_config_namespace_share(key, lxc_conf, data);
28d9e29e 2916
6333c915 2917 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
2918 ns_idx = lxc_namespace_2_ns_idx(namespace);
2919 if (ns_idx < 0)
2920 return ns_idx;
2921
b074bbf1 2922 return set_config_string_item(&lxc_conf->ns_share[ns_idx], value);
28d9e29e
CB
2923}
2924
6b0d5538
SH
2925struct parse_line_conf {
2926 struct lxc_conf *conf;
2927 bool from_include;
2928};
4184c3e1 2929
7a7ff0c6 2930static int parse_line(char *buffer, void *data)
c2cc9f0a 2931{
2e373df3
CB
2932 __do_free char *linep = NULL;
2933 char *dot, *key, *line, *value;
3b13691d
CB
2934 bool empty_line;
2935 struct lxc_config_t *config;
2e373df3 2936 int ret;
3b13691d
CB
2937 char *dup = buffer;
2938 struct parse_line_conf *plc = data;
c2cc9f0a 2939
d60ba568
CB
2940 if (!plc->conf)
2941 return syserror_set(-EINVAL, "Missing config");
2942
3b13691d
CB
2943 /* If there are newlines in the config file we should keep them. */
2944 empty_line = lxc_is_line_empty(dup);
2945 if (empty_line)
2946 dup = "\n";
c2cc9f0a 2947
25a908b8
CB
2948 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2949 * modified the original string on the stack by replacing '=' by '\0'
2950 * below.
91480a0f 2951 */
3b13691d 2952 linep = line = strdup(dup);
25a908b8 2953 if (!line)
2e373df3 2954 return ret_errno(ENOMEM);
91480a0f 2955
3b13691d
CB
2956 if (!plc->from_include) {
2957 ret = append_unexp_config_line(line, plc->conf);
2958 if (ret < 0)
2e373df3 2959 return ret;
3b13691d
CB
2960 }
2961
2962 if (empty_line)
2e373df3 2963 return 0;
6b0d5538 2964
b2718c72 2965 line += lxc_char_left_gc(line, strlen(line));
476d4cf1 2966
4184c3e1
SH
2967 /* ignore comments */
2968 if (line[0] == '#')
2e373df3 2969 return 0;
476d4cf1 2970
6b0d5538 2971 /* martian option - don't add it to the config itself */
1af3044f 2972 if (!strnequal(line, "lxc.", 4))
2e373df3 2973 return 0;
c2cc9f0a 2974
3b13691d 2975 dot = strchr(line, '=');
2e373df3
CB
2976 if (!dot)
2977 return log_error_errno(-EINVAL, EINVAL, "Invalid configuration line: %s", line);
a871ff6b 2978
c2cc9f0a 2979 *dot = '\0';
2980 value = dot + 1;
2981
b2718c72 2982 key = line;
2983 key[lxc_char_right_gc(key, strlen(key))] = '\0';
c2cc9f0a 2984
b2718c72 2985 value += lxc_char_left_gc(value, strlen(value));
2986 value[lxc_char_right_gc(value, strlen(value))] = '\0';
c2cc9f0a 2987
bd878dee 2988 if (*value == '\'' || *value == '\"') {
25a908b8
CB
2989 size_t len;
2990
2991 len = strlen(value);
504a2217
CB
2992 if (len > 1 && value[len - 1] == *value) {
2993 value[len - 1] = '\0';
bd878dee
SB
2994 value++;
2995 }
2996 }
2997
300df83e 2998 config = lxc_get_config(key);
2e373df3
CB
2999 if (!config)
3000 return log_error_errno(-EINVAL, EINVAL, "Unknown configuration key \"%s\"", key);
91480a0f 3001
2e373df3 3002 return config->set(key, value, plc->conf, NULL);
c2cc9f0a 3003}
3004
d899f11b 3005static struct new_config_item *parse_new_conf_line(char *buffer)
3006{
2b4cdcdb
CB
3007 __do_free char *k = NULL, *linep = NULL, *v = NULL;
3008 __do_free struct new_config_item *new = NULL;
d899f11b 3009 char *dup = buffer;
2b4cdcdb 3010 char *dot, *key, *line, *value;
d899f11b 3011
d60ba568
CB
3012 if (is_empty_string(buffer))
3013 return log_error_errno(NULL, EINVAL, "Empty configuration line");
3014
d899f11b 3015 linep = line = strdup(dup);
3016 if (!line)
3017 return NULL;
3018
3019 line += lxc_char_left_gc(line, strlen(line));
3020
3021 /* martian option - don't add it to the config itself */
1af3044f 3022 if (!strnequal(line, "lxc.", 4))
2b4cdcdb 3023 return 0;
d899f11b 3024
d899f11b 3025 dot = strchr(line, '=');
2b4cdcdb
CB
3026 if (!dot)
3027 return log_error_errno(NULL, EINVAL, "Invalid configuration line: %s", line);
d899f11b 3028
3029 *dot = '\0';
3030 value = dot + 1;
3031
3032 key = line;
3033 key[lxc_char_right_gc(key, strlen(key))] = '\0';
3034
3035 value += lxc_char_left_gc(value, strlen(value));
3036 value[lxc_char_right_gc(value, strlen(value))] = '\0';
3037
3038 if (*value == '\'' || *value == '\"') {
3039 size_t len;
3040
3041 len = strlen(value);
3042 if (len > 1 && value[len - 1] == *value) {
3043 value[len - 1] = '\0';
3044 value++;
3045 }
3046 }
3047
642751cc 3048 new = zalloc(sizeof(struct new_config_item));
d899f11b 3049 if (!new)
2b4cdcdb 3050 return NULL;
d899f11b 3051
2b4cdcdb
CB
3052 k = strdup(key);
3053 if (!k)
3054 return NULL;
d899f11b 3055
2b4cdcdb
CB
3056 v = strdup(value);
3057 if (!v)
3058 return NULL;
d899f11b 3059
2b4cdcdb
CB
3060 new->key = move_ptr(k);
3061 new->val = move_ptr(v);
3062 return move_ptr(new);
d899f11b 3063}
3064
6b0d5538 3065int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
c2cc9f0a 3066{
d60ba568
CB
3067 struct parse_line_conf plc;
3068
3069 if (!conf)
3070 return syserror_set(-EINVAL, "Missing config");
6b0d5538 3071
d60ba568
CB
3072 plc.conf = conf;
3073 plc.from_include = from_include;
f979ac15 3074
25a908b8 3075 /* Catch only the top level config file name in the structure. */
504a2217 3076 if (!conf->rcfile)
76d0127f 3077 conf->rcfile = strdup(file);
f979ac15 3078
d60ba568 3079 return lxc_file_for_each_line_mmap(file, parse_line, &plc);
c2cc9f0a 3080}
62e46035 3081
504a2217 3082int lxc_config_define_add(struct lxc_list *defines, char *arg)
62e46035 3083{
bce0472a 3084 __do_free struct lxc_list *dent = NULL;
62e46035 3085
642751cc 3086 dent = lxc_list_new();
62e46035 3087 if (!dent)
bce0472a 3088 return ret_errno(ENOMEM);
62e46035 3089
d899f11b 3090 dent->elem = parse_new_conf_line(arg);
bce0472a
CB
3091 if (!dent->elem)
3092 return ret_errno(ENOMEM);
d899f11b 3093
bce0472a 3094 lxc_list_add_tail(defines, move_ptr(dent));
47903908 3095
62e46035
CLG
3096 return 0;
3097}
3098
eb0c9382 3099bool lxc_config_define_load(struct lxc_list *defines, struct lxc_container *c)
62e46035 3100{
eb0c9382 3101 struct lxc_list *it;
3102 bool bret = true;
62e46035
CLG
3103
3104 lxc_list_for_each(it, defines) {
eb0c9382 3105 struct new_config_item *new_item = it->elem;
3106 bret = c->set_config_item(c, new_item->key, new_item->val);
3107 if (!bret)
62e46035
CLG
3108 break;
3109 }
3110
eb0c9382 3111 lxc_config_define_free(defines);
47903908 3112
eb0c9382 3113 return bret;
3114}
3115
3116void lxc_config_define_free(struct lxc_list *defines)
3117{
3118 struct lxc_list *it, *next;
3119
9ebb03ad 3120 lxc_list_for_each_safe(it, defines, next) {
eb0c9382 3121 struct new_config_item *new_item = it->elem;
3122 free(new_item->key);
3123 free(new_item->val);
62e46035
CLG
3124 lxc_list_del(it);
3125 free(it);
3126 }
62e46035 3127}
525f0002
CS
3128
3129signed long lxc_config_parse_arch(const char *arch)
3130{
504a2217 3131#if HAVE_SYS_PERSONALITY_H
525f0002
CS
3132 struct per_name {
3133 char *name;
3134 unsigned long per;
bb8d8207 3135 } pername[] = {
c852678b
CB
3136 { "arm", PER_LINUX32 },
3137 { "armel", PER_LINUX32 },
3138 { "armhf", PER_LINUX32 },
3139 { "armv7l", PER_LINUX32 },
3140 { "athlon", PER_LINUX32 },
3141 { "i386", PER_LINUX32 },
3142 { "i486", PER_LINUX32 },
3143 { "i586", PER_LINUX32 },
3144 { "i686", PER_LINUX32 },
3145 { "linux32", PER_LINUX32 },
3146 { "mips", PER_LINUX32 },
3147 { "mipsel", PER_LINUX32 },
3148 { "ppc", PER_LINUX32 },
3149 { "powerpc", PER_LINUX32 },
3150 { "x86", PER_LINUX32 },
3151 { "amd64", PER_LINUX },
3152 { "arm64", PER_LINUX },
3153 { "linux64", PER_LINUX },
3154 { "mips64", PER_LINUX },
3155 { "mips64el", PER_LINUX },
3156 { "ppc64", PER_LINUX },
3157 { "ppc64el", PER_LINUX },
3158 { "ppc64le", PER_LINUX },
3159 { "powerpc64", PER_LINUX },
3160 { "s390x", PER_LINUX },
3161 { "x86_64", PER_LINUX },
525f0002
CS
3162 };
3163 size_t len = sizeof(pername) / sizeof(pername[0]);
3164
c852678b 3165 for (int i = 0; i < len; i++)
d62177e9 3166 if (strequal(pername[i].name, arch))
504a2217 3167 return pername[i].per;
504a2217 3168#endif
525f0002 3169
ee142207 3170 return LXC_ARCH_UNCHANGED;
525f0002 3171}
72d0e1cb 3172
eb0c9382 3173int lxc_fill_elevated_privileges(char *flaglist, int *flags)
3174{
62dd965e 3175 char *token;
eb0c9382 3176 int i, aflag;
3177 struct {
3178 const char *token;
3179 int flag;
3180 } all_privs[] = {
3181 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP },
3182 { "CAP", LXC_ATTACH_DROP_CAPABILITIES },
3183 { "LSM", LXC_ATTACH_LSM_EXEC },
3184 { NULL, 0 }
3185 };
3186
3187 if (!flaglist) {
3188 /* For the sake of backward compatibility, drop all privileges
3189 * if none is specified.
3190 */
3191 for (i = 0; all_privs[i].token; i++)
3192 *flags |= all_privs[i].flag;
3193
3194 return 0;
3195 }
3196
62dd965e 3197 lxc_iterate_parts(token, flaglist, "|") {
eb0c9382 3198 aflag = -1;
3199
3200 for (i = 0; all_privs[i].token; i++)
d62177e9 3201 if (strequal(all_privs[i].token, token))
eb0c9382 3202 aflag = all_privs[i].flag;
3203
3204 if (aflag < 0)
966f5662 3205 return ret_errno(EINVAL);
eb0c9382 3206
3207 *flags |= aflag;
eb0c9382 3208 }
3209
3210 return 0;
3211}
3212
504a2217 3213/* Write out a configuration file. */
0e1a60b0 3214int write_config(int fd, const struct lxc_conf *conf)
72d0e1cb 3215{
6b0d5538 3216 int ret;
0e1a60b0 3217 size_t len = conf->unexpanded_len;
72d0e1cb 3218
0e1a60b0
CB
3219 if (len == 0)
3220 return 0;
504a2217 3221
0e1a60b0 3222 ret = lxc_write_nointr(fd, conf->unexpanded_config, len);
f2c64408
CB
3223 if (ret < 0)
3224 return log_error_errno(-errno, errno, "Failed to write configuration file");
0e1a60b0
CB
3225
3226 return 0;
6b0d5538 3227}
f979ac15 3228
504a2217
CB
3229bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key,
3230 const char *v)
6b0d5538 3231{
0cacddda 3232 __do_free char *tmp = NULL;
6b0d5538 3233 int ret;
151d2da2 3234 size_t len;
4184c3e1 3235
151d2da2 3236 len = strlen(key) + strlen(v) + 4;
f01d0358 3237 tmp = must_realloc(NULL, len);
151d2da2 3238
663e9916 3239 if (lxc_config_value_empty(v))
3948c252 3240 ret = strnprintf(tmp, len, "%s =", key);
151d2da2 3241 else
3948c252
CB
3242 ret = strnprintf(tmp, len, "%s = %s", key, v);
3243 if (ret < 0)
6b0d5538
SH
3244 return false;
3245
3246 /* Save the line verbatim into unexpanded_conf */
3247 if (append_unexp_config_line(tmp, conf))
3248 return false;
3249
3250 return true;
3251}
3252
504a2217
CB
3253void clear_unexp_config_line(struct lxc_conf *conf, const char *key,
3254 bool rm_subkeys)
6b0d5538 3255{
504a2217
CB
3256 char *lend;
3257 char *lstart = conf->unexpanded_config;
6b0d5538
SH
3258
3259 if (!conf->unexpanded_config)
3260 return;
504a2217 3261
6b0d5538
SH
3262 while (*lstart) {
3263 lend = strchr(lstart, '\n');
3264 char v;
47903908 3265
6b0d5538
SH
3266 if (!lend)
3267 lend = lstart + strlen(lstart);
3268 else
3269 lend++;
47903908 3270
1af3044f 3271 if (!strnequal(lstart, key, strlen(key))) {
6b0d5538
SH
3272 lstart = lend;
3273 continue;
5f62730e 3274 }
47903908 3275
6b0d5538
SH
3276 if (!rm_subkeys) {
3277 v = lstart[strlen(key)];
3278 if (!isspace(v) && v != '=') {
3279 lstart = lend;
3280 continue;
3281 }
5f62730e 3282 }
47903908 3283
6b0d5538 3284 conf->unexpanded_len -= (lend - lstart);
47903908 3285
6b0d5538
SH
3286 if (*lend == '\0') {
3287 *lstart = '\0';
3288 return;
fd986e08 3289 }
47903908 3290
504a2217 3291 memmove(lstart, lend, strlen(lend) + 1);
fd986e08 3292 }
6b0d5538
SH
3293}
3294
329b3625
CB
3295bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
3296 const char *newpath, const char *oldname,
3297 const char *newname, const char *ovldir)
3298{
b063ba29
CB
3299 __do_free char *newdir = NULL, *olddir = NULL;
3300 char *lstart = conf->unexpanded_config;
3301 const char *key = "lxc.mount.entry";
329b3625 3302 int ret;
f01d0358 3303 char *lend, *p, *q;
504a2217 3304 size_t newdirlen, olddirlen;
329b3625 3305
504a2217 3306 olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
f01d0358 3307 olddir = must_realloc(NULL, olddirlen + 1);
3948c252
CB
3308 ret = strnprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath, oldname);
3309 if (ret < 0)
329b3625 3310 return false;
504a2217
CB
3311
3312 newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
f01d0358 3313 newdir = must_realloc(NULL, newdirlen + 1);
3948c252
CB
3314 ret = strnprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath, newname);
3315 if (ret < 0)
329b3625 3316 return false;
504a2217 3317
329b3625
CB
3318 if (!conf->unexpanded_config)
3319 return true;
504a2217 3320
329b3625
CB
3321 while (*lstart) {
3322 lend = strchr(lstart, '\n');
3323 if (!lend)
3324 lend = lstart + strlen(lstart);
3325 else
3326 lend++;
504a2217 3327
1af3044f 3328 if (!strnequal(lstart, key, strlen(key)))
504a2217
CB
3329 goto next;
3330
329b3625
CB
3331 p = strchr(lstart + strlen(key), '=');
3332 if (!p)
504a2217 3333 goto next;
329b3625 3334 p++;
504a2217 3335
329b3625
CB
3336 while (isblank(*p))
3337 p++;
504a2217 3338
329b3625 3339 if (p >= lend)
504a2217
CB
3340 goto next;
3341
12e6ab5d
CB
3342 /* Whenever a lxc.mount.entry entry is found in a line we check
3343 * if the substring "overlay" is present before doing any
3344 * further work. We check for "overlay" because substrings need
3345 * to have at least one space before them in a valid overlay
504a2217
CB
3346 * lxc.mount.entry (/A B overlay). When the space before is
3347 * missing it is very likely that these substrings are part of a
3348 * path or something else. (Checking q >= lend ensures that we
3349 * only count matches in the current line.) */
12e6ab5d
CB
3350 q = strstr(p, " overlay");
3351 if (!q || q >= lend)
504a2217
CB
3352 goto next;
3353
329b3625 3354 if (!(q = strstr(p, olddir)) || (q >= lend))
504a2217 3355 goto next;
329b3625
CB
3356
3357 /* replace the olddir with newdir */
3358 if (olddirlen >= newdirlen) {
3359 size_t diff = olddirlen - newdirlen;
3360 memcpy(q, newdir, newdirlen);
47903908 3361
329b3625
CB
3362 if (olddirlen != newdirlen) {
3363 memmove(q + newdirlen, q + newdirlen + diff,
47903908 3364 strlen(q) - newdirlen - diff + 1);
329b3625
CB
3365 lend -= diff;
3366 conf->unexpanded_len -= diff;
3367 }
3368 } else {
3369 char *new;
3370 size_t diff = newdirlen - olddirlen;
3371 size_t oldlen = conf->unexpanded_len;
3372 size_t newlen = oldlen + diff;
3373 size_t poffset = q - conf->unexpanded_config;
504a2217 3374
329b3625 3375 new = realloc(conf->unexpanded_config, newlen + 1);
25a908b8 3376 if (!new)
329b3625 3377 return false;
25a908b8 3378
329b3625
CB
3379 conf->unexpanded_len = newlen;
3380 conf->unexpanded_alloced = newlen + 1;
3381 new[newlen - 1] = '\0';
3382 lend = new + (lend - conf->unexpanded_config);
47903908 3383
25a908b8 3384 /* Move over the remainder to make room for the newdir.
504a2217 3385 */
329b3625 3386 memmove(new + poffset + newdirlen,
47903908 3387 new + poffset + olddirlen,
3388 oldlen - poffset - olddirlen + 1);
329b3625 3389 conf->unexpanded_config = new;
47903908 3390
329b3625
CB
3391 memcpy(new + poffset, newdir, newdirlen);
3392 lend += diff;
3393 }
47903908 3394
504a2217
CB
3395 next:
3396 lstart = lend;
329b3625 3397 }
504a2217 3398
329b3625
CB
3399 return true;
3400}
3401
67702c21 3402bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
d546aa0e
CB
3403 const char *newpath, const char *oldname,
3404 const char *newname)
6b0d5538 3405{
29e78cb9
CB
3406 __do_free char *newdir = NULL, *olddir = NULL;
3407 char *lstart = conf->unexpanded_config;
3408 const char *key = "lxc.hook";
67702c21 3409 int ret;
f01d0358 3410 char *lend, *p;
504a2217 3411 size_t newdirlen, olddirlen;
67702c21 3412
504a2217 3413 olddirlen = strlen(oldpath) + strlen(oldname) + 1;
f01d0358 3414 olddir = must_realloc(NULL, olddirlen + 1);
3948c252
CB
3415 ret = strnprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
3416 if (ret < 0)
67702c21 3417 return false;
504a2217
CB
3418
3419 newdirlen = strlen(newpath) + strlen(newname) + 1;
f01d0358 3420 newdir = must_realloc(NULL, newdirlen + 1);
3948c252
CB
3421 ret = strnprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
3422 if (ret < 0)
67702c21 3423 return false;
25a908b8 3424
67702c21
SH
3425 if (!conf->unexpanded_config)
3426 return true;
25a908b8 3427
67702c21
SH
3428 while (*lstart) {
3429 lend = strchr(lstart, '\n');
3430 if (!lend)
3431 lend = lstart + strlen(lstart);
3432 else
3433 lend++;
504a2217 3434
1af3044f 3435 if (!strnequal(lstart, key, strlen(key)))
504a2217
CB
3436 goto next;
3437
d546aa0e
CB
3438 p = strchr(lstart + strlen(key), '=');
3439 if (!p)
504a2217 3440 goto next;
67702c21 3441 p++;
504a2217 3442
67702c21
SH
3443 while (isblank(*p))
3444 p++;
504a2217
CB
3445
3446 if (p >= lend)
3447 goto next;
3448
1af3044f 3449 if (!strnequal(p, olddir, strlen(olddir)))
504a2217
CB
3450 goto next;
3451
67702c21
SH
3452 /* replace the olddir with newdir */
3453 if (olddirlen >= newdirlen) {
3454 size_t diff = olddirlen - newdirlen;
3455 memcpy(p, newdir, newdirlen);
47903908 3456
67702c21 3457 if (olddirlen != newdirlen) {
d546aa0e 3458 memmove(p + newdirlen, p + newdirlen + diff,
47903908 3459 strlen(p) - newdirlen - diff + 1);
67702c21
SH
3460 lend -= diff;
3461 conf->unexpanded_len -= diff;
3462 }
67702c21
SH
3463 } else {
3464 char *new;
3465 size_t diff = newdirlen - olddirlen;
3466 size_t oldlen = conf->unexpanded_len;
3467 size_t newlen = oldlen + diff;
3468 size_t poffset = p - conf->unexpanded_config;
504a2217 3469
d546aa0e 3470 new = realloc(conf->unexpanded_config, newlen + 1);
25a908b8 3471 if (!new)
6b0d5538 3472 return false;
25a908b8 3473
67702c21 3474 conf->unexpanded_len = newlen;
d546aa0e
CB
3475 conf->unexpanded_alloced = newlen + 1;
3476 new[newlen - 1] = '\0';
67702c21 3477 lend = new + (lend - conf->unexpanded_config);
47903908 3478
25a908b8 3479 /* Move over the remainder to make room for the newdir.
504a2217 3480 */
d546aa0e 3481 memmove(new + poffset + newdirlen,
47903908 3482 new + poffset + olddirlen,
3483 oldlen - poffset - olddirlen + 1);
67702c21 3484 conf->unexpanded_config = new;
47903908 3485
d546aa0e
CB
3486 memcpy(new + poffset, newdir, newdirlen);
3487 lend += diff;
fd986e08 3488 }
47903908 3489
504a2217
CB
3490 next:
3491 lstart = lend;
fd986e08 3492 }
504a2217 3493
6b0d5538
SH
3494 return true;
3495}
3496
504a2217
CB
3497#define DO(cmd) \
3498 { \
3499 if (!(cmd)) { \
3500 ERROR("Error writing to new config"); \
3501 return false; \
3502 } \
3503 }
6b0d5538 3504
25a908b8
CB
3505/* This is called only from clone. We wish to update all hwaddrs in the
3506 * unexpanded config file. We can't/don't want to update any which come from
504a2217
CB
3507 * lxc.includes (there shouldn't be any).
3508 * We can't just walk the c->lxc-conf->network list because that includes netifs
3509 * from the include files. So we update the ones which we find in the unexp
3510 * config file, then find the original macaddr in the conf->network, and update
3511 * that to the same value.
67702c21
SH
3512 */
3513bool network_new_hwaddrs(struct lxc_conf *conf)
6b0d5538 3514{
504a2217 3515 char *lend, *p, *p2;
6b0d5538 3516 struct lxc_list *it;
504a2217 3517 char *lstart = conf->unexpanded_config;
6b0d5538 3518
67702c21
SH
3519 if (!conf->unexpanded_config)
3520 return true;
091045f8 3521
67702c21
SH
3522 while (*lstart) {
3523 char newhwaddr[18], oldhwaddr[17];
091045f8 3524
67702c21
SH
3525 lend = strchr(lstart, '\n');
3526 if (!lend)
3527 lend = lstart + strlen(lstart);
3528 else
3529 lend++;
091045f8 3530
3db41a6c 3531 if (!lxc_config_net_is_hwaddr(lstart)) {
67702c21
SH
3532 lstart = lend;
3533 continue;
72d0e1cb 3534 }
091045f8 3535
4a787c27 3536 p = strchr(lstart, '=');
67702c21
SH
3537 if (!p) {
3538 lstart = lend;
3539 continue;
72d0e1cb 3540 }
091045f8 3541
67702c21
SH
3542 p++;
3543 while (isblank(*p))
3544 p++;
3545 if (!*p)
3546 return true;
091045f8 3547
67702c21
SH
3548 p2 = p;
3549 while (*p2 && !isblank(*p2) && *p2 != '\n')
3550 p2++;
504a2217
CB
3551
3552 if ((p2 - p) != 17) {
67702c21
SH
3553 WARN("Bad hwaddr entry");
3554 lstart = lend;
3555 continue;
72d0e1cb 3556 }
091045f8 3557
67702c21 3558 memcpy(oldhwaddr, p, 17);
091045f8
CB
3559
3560 if (!new_hwaddr(newhwaddr))
3561 return false;
3562
67702c21 3563 memcpy(p, newhwaddr, 17);
25a908b8 3564 lxc_list_for_each(it, &conf->network) {
67702c21 3565 struct lxc_netdev *n = it->elem;
25a908b8 3566
67702c21
SH
3567 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
3568 memcpy(n->hwaddr, newhwaddr, 17);
72d0e1cb 3569 }
67702c21
SH
3570
3571 lstart = lend;
72d0e1cb 3572 }
091045f8 3573
6b0d5538 3574 return true;
72d0e1cb 3575}
8796becf 3576
713046e3 3577static int set_config_ephemeral(const char *key, const char *value,
c7e27aaf 3578 struct lxc_conf *lxc_conf, void *data)
8796becf 3579{
4486ea13
CB
3580 int ret;
3581
663e9916 3582 if (lxc_config_value_empty(value)) {
3c6cf53a 3583 lxc_conf->ephemeral = 0;
78304622 3584 return 0;
3c6cf53a 3585 }
78304622 3586
4486ea13
CB
3587 ret = lxc_safe_uint(value, &lxc_conf->ephemeral);
3588 if (ret < 0)
3589 return ret;
8796becf 3590
25a908b8 3591 if (lxc_conf->ephemeral > 1)
4486ea13 3592 return ret_errno(EINVAL);
8796becf
CB
3593
3594 return 0;
3595}
3596
46cc906d 3597static int set_config_log_syslog(const char *key, const char *value,
c7e27aaf 3598 struct lxc_conf *lxc_conf, void *data)
64c57ea1 3599{
76d0127f 3600 int facility;
7ca56b84 3601
b8a0e944
CB
3602 if (lxc_conf->syslog)
3603 free_disarm(lxc_conf->syslog);
ee10a69c 3604
663e9916 3605 if (lxc_config_value_empty(value))
7ca56b84
CB
3606 return 0;
3607
76d0127f 3608 facility = lxc_syslog_priority_to_int(value);
25a908b8 3609 if (facility == -EINVAL)
b8a0e944 3610 return ret_errno(EINVAL);
64c57ea1 3611
76d0127f 3612 lxc_log_syslog(facility);
47903908 3613
713046e3 3614 return set_config_string_item(&lxc_conf->syslog, value);
64c57ea1 3615}
5a46f283 3616
713046e3 3617static int set_config_no_new_privs(const char *key, const char *value,
c7e27aaf 3618 struct lxc_conf *lxc_conf, void *data)
5a46f283 3619{
ad9a0d33 3620 int ret;
e8ec7c9e 3621 unsigned int v;
5a46f283 3622
663e9916 3623 if (lxc_config_value_empty(value)) {
cf3f8bf6 3624 lxc_conf->no_new_privs = false;
80926845 3625 return 0;
cf3f8bf6 3626 }
80926845 3627
ad9a0d33
CB
3628 ret = lxc_safe_uint(value, &v);
3629 if (ret < 0)
3630 return ret;
e8ec7c9e 3631
25a908b8 3632 if (v > 1)
ad9a0d33 3633 return ret_errno(EINVAL);
e8ec7c9e 3634
5a46f283
CB
3635 lxc_conf->no_new_privs = v ? true : false;
3636
3637 return 0;
3638}
7b992a3e
CB
3639
3640/* Callbacks to get configuration items. */
6bede808 3641static int get_config_personality(const char *key, char *retv, int inlen,
cccd2219 3642 struct lxc_conf *c, void *data)
7b992a3e
CB
3643{
3644 int fulllen = 0;
3645
3646 if (!retv)
3647 inlen = 0;
3648 else
3649 memset(retv, 0, inlen);
3650
3651#if HAVE_SYS_PERSONALITY_H
3652 int len = 0;
3653
6bede808 3654 switch (c->personality) {
7b992a3e
CB
3655 case PER_LINUX32:
3656 strprint(retv, inlen, "i686");
3657 break;
3658 case PER_LINUX:
3659 strprint(retv, inlen, "x86_64");
3660 break;
3661 default:
3662 break;
3663 }
3664#endif
3665
3666 return fulllen;
3667}
bdf91ab4 3668
232763d6
CB
3669static int get_config_pty_max(const char *key, char *retv, int inlen,
3670 struct lxc_conf *c, void *data)
bdf91ab4 3671{
e528c735 3672 return lxc_get_conf_size_t(c, retv, inlen, c->pty_max);
bdf91ab4 3673}
5485782f 3674
fe1c5887
CB
3675static int get_config_tty_max(const char *key, char *retv, int inlen,
3676 struct lxc_conf *c, void *data)
5485782f 3677{
885766f5 3678 return lxc_get_conf_size_t(c, retv, inlen, c->ttys.max);
5485782f 3679}
8015e018 3680
42e53c29 3681static int get_config_tty_dir(const char *key, char *retv, int inlen,
cccd2219 3682 struct lxc_conf *c, void *data)
8015e018 3683{
885766f5 3684 return lxc_get_conf_str(retv, inlen, c->ttys.dir);
8015e018 3685}
de1ede69 3686
953fe44f
CB
3687static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
3688 struct lxc_conf *c, void *data)
104c8e6c 3689{
6bede808 3690 return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile);
104c8e6c 3691}
d60d18c6 3692
953fe44f
CB
3693static int get_config_apparmor_allow_incomplete(const char *key, char *retv,
3694 int inlen, struct lxc_conf *c,
3695 void *data)
d60d18c6 3696{
6bede808
CB
3697 return lxc_get_conf_int(c, retv, inlen,
3698 c->lsm_aa_allow_incomplete);
d60d18c6 3699}
4203a0b5 3700
1800f924
WB
3701static int get_config_apparmor_allow_nesting(const char *key, char *retv,
3702 int inlen, struct lxc_conf *c,
3703 void *data)
3704{
3705 return lxc_get_conf_int(c, retv, inlen,
3706 c->lsm_aa_allow_nesting);
3707}
3708
3709static int get_config_apparmor_raw(const char *key, char *retv,
3710 int inlen, struct lxc_conf *c,
3711 void *data)
3712{
3713 int len;
3714 struct lxc_list *it;
3715 int fulllen = 0;
3716
3717 if (!retv)
3718 inlen = 0;
3719 else
3720 memset(retv, 0, inlen);
3721
3722 lxc_list_for_each(it, &c->lsm_aa_raw) {
3723 strprint(retv, inlen, "%s\n", (char *)it->elem);
3724 }
3725
3726 return fulllen;
3727}
3728
953fe44f
CB
3729static int get_config_selinux_context(const char *key, char *retv, int inlen,
3730 struct lxc_conf *c, void *data)
4203a0b5 3731{
6bede808 3732 return lxc_get_conf_str(retv, inlen, c->lsm_se_context);
4203a0b5 3733}
b863bf92 3734
4fef78bc
MB
3735static int get_config_selinux_context_keyring(const char *key, char *retv, int inlen,
3736 struct lxc_conf *c, void *data)
3737{
3738 return lxc_get_conf_str(retv, inlen, c->lsm_se_keyring_context);
3739}
3740
8f818a84
MB
3741static int get_config_keyring_session(const char *key, char *retv, int inlen,
3742 struct lxc_conf *c, void *data)
3743{
3744 return lxc_get_conf_bool(c, retv, inlen, c->keyring_disable_session);
3745}
3746
4fef78bc 3747
25a908b8
CB
3748/* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3749 * just the value(s) will be printed. Since there still could be more than one,
3750 * it is newline-separated.
8d47350b 3751 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
25a908b8
CB
3752 * have newlines?)
3753 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3754 * 'lxc.cgroup.subsystem.key = value' format.
b863bf92 3755 */
54860ed0
CB
3756static int __get_config_cgroup_controller(const char *key, char *retv,
3757 int inlen, struct lxc_conf *c,
3758 int version)
b863bf92 3759{
b863bf92 3760 int len;
54860ed0
CB
3761 size_t namespaced_token_len;
3762 char *global_token, *namespaced_token;
3763 struct lxc_list *it;
b863bf92
CB
3764 int fulllen = 0;
3765 bool get_all = false;
3766
3767 if (!retv)
3768 inlen = 0;
3769 else
3770 memset(retv, 0, inlen);
3771
54860ed0
CB
3772 if (version == CGROUP2_SUPER_MAGIC) {
3773 global_token = "lxc.cgroup2";
3774 namespaced_token = "lxc.cgroup2.";
6333c915 3775 namespaced_token_len = STRLITERALLEN("lxc.cgroup2.");
54860ed0
CB
3776 } else if (version == CGROUP_SUPER_MAGIC) {
3777 global_token = "lxc.cgroup";
3778 namespaced_token = "lxc.cgroup.";
6333c915 3779 namespaced_token_len = STRLITERALLEN("lxc.cgroup.");
54860ed0 3780 } else {
78205814 3781 return ret_errno(EINVAL);
54860ed0
CB
3782 }
3783
d62177e9 3784 if (strequal(key, global_token))
b863bf92 3785 get_all = true;
1af3044f 3786 else if (strnequal(key, namespaced_token, namespaced_token_len))
54860ed0 3787 key += namespaced_token_len;
b863bf92 3788 else
78205814 3789 return ret_errno(EINVAL);
b863bf92 3790
6bede808 3791 lxc_list_for_each(it, &c->cgroup) {
b863bf92 3792 struct lxc_cgroup *cg = it->elem;
25a908b8 3793
b863bf92 3794 if (get_all) {
54860ed0
CB
3795 if (version != cg->version)
3796 continue;
3797
47903908 3798 strprint(retv, inlen, "%s.%s = %s\n", global_token,
3799 cg->subsystem, cg->value);
d62177e9 3800 } else if (strequal(cg->subsystem, key)) {
b863bf92
CB
3801 strprint(retv, inlen, "%s\n", cg->value);
3802 }
3803 }
3804
3805 return fulllen;
3806}
5014ff2e 3807
54860ed0
CB
3808static int get_config_cgroup_controller(const char *key, char *retv, int inlen,
3809 struct lxc_conf *c, void *data)
3810{
3811 return __get_config_cgroup_controller(key, retv, inlen, c,
3812 CGROUP_SUPER_MAGIC);
3813}
3814
3815static int get_config_cgroup2_controller(const char *key, char *retv, int inlen,
3816 struct lxc_conf *c, void *data)
3817{
3818 return __get_config_cgroup_controller(key, retv, inlen, c,
3819 CGROUP2_SUPER_MAGIC);
3820}
3821
43654d34
CB
3822static int get_config_cgroup_dir(const char *key, char *retv, int inlen,
3823 struct lxc_conf *lxc_conf, void *data)
3824{
3825 int len;
3826 int fulllen = 0;
3827
d62177e9 3828 if (!strequal(key, "lxc.cgroup.dir"))
c583072d
CB
3829 return ret_errno(EINVAL);
3830
43654d34
CB
3831 if (!retv)
3832 inlen = 0;
3833 else
3834 memset(retv, 0, inlen);
3835
3836 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir);
3837
3838 return fulllen;
3839}
3840
a900cbaf
WB
3841static int get_config_cgroup_monitor_dir(const char *key, char *retv, int inlen,
3842 struct lxc_conf *lxc_conf, void *data)
3843{
3844 int len;
3845 int fulllen = 0;
3846
3847 if (!retv)
3848 inlen = 0;
3849 else
3850 memset(retv, 0, inlen);
3851
3852 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.monitor_dir);
3853
3854 return fulllen;
3855}
3856
7696c1f9
RJ
3857static int get_config_cgroup_monitor_pivot_dir(const char *key, char *retv, int inlen,
3858 struct lxc_conf *lxc_conf, void *data)
3859{
3860 int len;
3861 int fulllen = 0;
3862
3863 if (!retv)
3864 inlen = 0;
3865 else
3866 memset(retv, 0, inlen);
3867
3868 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.monitor_pivot_dir);
3869
3870 return fulllen;
3871}
3872
a900cbaf
WB
3873static int get_config_cgroup_container_dir(const char *key, char *retv,
3874 int inlen,
3875 struct lxc_conf *lxc_conf,
3876 void *data)
3877{
3878 int len;
3879 int fulllen = 0;
3880
3881 if (!retv)
3882 inlen = 0;
3883 else
3884 memset(retv, 0, inlen);
3885
3886 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.container_dir);
3887
3888 return fulllen;
3889}
3890
3891static int get_config_cgroup_container_inner_dir(const char *key, char *retv,
3892 int inlen,
3893 struct lxc_conf *lxc_conf,
3894 void *data)
3895{
3896 int len;
3897 int fulllen = 0;
3898
3899 if (!retv)
3900 inlen = 0;
3901 else
3902 memset(retv, 0, inlen);
3903
3904 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.namespace_dir);
3905
3906 return fulllen;
3907}
3908
9caee129
CB
3909static inline int get_config_cgroup_relative(const char *key, char *retv,
3910 int inlen, struct lxc_conf *lxc_conf,
3911 void *data)
76f0e2e7
CB
3912{
3913 return lxc_get_conf_int(lxc_conf, retv, inlen,
9caee129 3914 lxc_conf->cgroup_meta.relative);
76f0e2e7
CB
3915}
3916
6bede808 3917static int get_config_idmaps(const char *key, char *retv, int inlen,
cccd2219 3918 struct lxc_conf *c, void *data)
5014ff2e
CB
3919{
3920 struct lxc_list *it;
3921 int len, listlen, ret;
3922 int fulllen = 0;
3923/* "u 1000 1000000 65536"
3924 *
3925 * let's render this as
3926 *
3927 * sizeof(char)
3928 * +
3929 * sizeof(" ")
3930 * +
c77aee64 3931 * sizeof(uint32_t)
5014ff2e
CB
3932 * +
3933 * sizeof(" ")
3934 * +
c77aee64 3935 * sizeof(uint32_t)
5014ff2e
CB
3936 * +
3937 * sizeof(" ")
3938 * +
c77aee64 3939 * sizeof(uint32_t)
5014ff2e
CB
3940 * +
3941 * \0
3942 */
c77aee64 3943#define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
5014ff2e
CB
3944 char buf[__LXC_IDMAP_STR_BUF];
3945
3946 if (!retv)
3947 inlen = 0;
3948 else
3949 memset(retv, 0, inlen);
3950
6bede808 3951 listlen = lxc_list_len(&c->id_map);
c77aee64 3952 lxc_list_for_each(it, &c->id_map) {
5014ff2e 3953 struct id_map *map = it->elem;
3948c252
CB
3954 ret = strnprintf(buf, sizeof(buf), "%c %lu %lu %lu",
3955 (map->idtype == ID_TYPE_UID) ? 'u' : 'g',
3956 map->nsid, map->hostid, map->range);
3957 if (ret < 0)
2e5db3a2 3958 return ret_errno(EIO);
5014ff2e
CB
3959
3960 strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : "");
3961 }
47903908 3962
5014ff2e
CB
3963 return fulllen;
3964}
b29b29be 3965
46cc906d 3966static int get_config_log_level(const char *key, char *retv, int inlen,
cccd2219 3967 struct lxc_conf *c, void *data)
b29b29be
CB
3968{
3969 const char *v;
6bede808 3970 v = lxc_log_priority_to_string(c->loglevel);
b29b29be
CB
3971 return lxc_get_conf_str(retv, inlen, v);
3972}
3d4630ab 3973
46cc906d 3974static int get_config_log_file(const char *key, char *retv, int inlen,
cccd2219 3975 struct lxc_conf *c, void *data)
3d4630ab 3976{
6bede808 3977 return lxc_get_conf_str(retv, inlen, c->logfile);
3d4630ab 3978}
0d601acb 3979
47148e96
CB
3980static int get_config_mount_fstab(const char *key, char *retv, int inlen,
3981 struct lxc_conf *c, void *data)
0d601acb 3982{
6bede808 3983 return lxc_get_conf_str(retv, inlen, c->fstab);
0d601acb 3984}
43fbf8d9 3985
6bede808 3986static int get_config_mount_auto(const char *key, char *retv, int inlen,
cccd2219 3987 struct lxc_conf *c, void *data)
43fbf8d9
CB
3988{
3989 int len, fulllen = 0;
3990 const char *sep = "";
3991
3992 if (!retv)
3993 inlen = 0;
3994 else
3995 memset(retv, 0, inlen);
3996
6bede808 3997 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
43fbf8d9
CB
3998 return 0;
3999
6bede808 4000 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
43fbf8d9
CB
4001 case LXC_AUTO_PROC_MIXED:
4002 strprint(retv, inlen, "%sproc:mixed", sep);
4003 sep = " ";
4004 break;
4005 case LXC_AUTO_PROC_RW:
4006 strprint(retv, inlen, "%sproc:rw", sep);
4007 sep = " ";
4008 break;
4009 default:
4010 break;
4011 }
4012
6bede808 4013 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
43fbf8d9
CB
4014 case LXC_AUTO_SYS_RO:
4015 strprint(retv, inlen, "%ssys:ro", sep);
4016 sep = " ";
4017 break;
4018 case LXC_AUTO_SYS_RW:
4019 strprint(retv, inlen, "%ssys:rw", sep);
4020 sep = " ";
4021 break;
4022 case LXC_AUTO_SYS_MIXED:
4023 strprint(retv, inlen, "%ssys:mixed", sep);
4024 sep = " ";
4025 break;
4026 default:
4027 break;
4028 }
4029
6bede808 4030 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
43fbf8d9
CB
4031 case LXC_AUTO_CGROUP_NOSPEC:
4032 strprint(retv, inlen, "%scgroup", sep);
43fbf8d9
CB
4033 break;
4034 case LXC_AUTO_CGROUP_MIXED:
4035 strprint(retv, inlen, "%scgroup:mixed", sep);
43fbf8d9
CB
4036 break;
4037 case LXC_AUTO_CGROUP_RO:
4038 strprint(retv, inlen, "%scgroup:ro", sep);
43fbf8d9
CB
4039 break;
4040 case LXC_AUTO_CGROUP_RW:
4041 strprint(retv, inlen, "%scgroup:rw", sep);
43fbf8d9
CB
4042 break;
4043 case LXC_AUTO_CGROUP_FULL_NOSPEC:
4044 strprint(retv, inlen, "%scgroup-full", sep);
43fbf8d9
CB
4045 break;
4046 case LXC_AUTO_CGROUP_FULL_MIXED:
4047 strprint(retv, inlen, "%scgroup-full:mixed", sep);
43fbf8d9
CB
4048 break;
4049 case LXC_AUTO_CGROUP_FULL_RO:
4050 strprint(retv, inlen, "%scgroup-full:ro", sep);
43fbf8d9
CB
4051 break;
4052 case LXC_AUTO_CGROUP_FULL_RW:
4053 strprint(retv, inlen, "%scgroup-full:rw", sep);
43fbf8d9
CB
4054 break;
4055 default:
4056 break;
4057 }
4058
4059 return fulllen;
4060}
cc921848 4061
6bede808 4062static int get_config_mount(const char *key, char *retv, int inlen,
cccd2219 4063 struct lxc_conf *c, void *data)
cc921848
CB
4064{
4065 int len, fulllen = 0;
4066 struct lxc_list *it;
4067
4068 if (!retv)
4069 inlen = 0;
4070 else
4071 memset(retv, 0, inlen);
4072
25a908b8 4073 lxc_list_for_each(it, &c->mount_list) {
cc921848
CB
4074 strprint(retv, inlen, "%s\n", (char *)it->elem);
4075 }
4076
4077 return fulllen;
4078}
819114b6 4079
7a96a068
CB
4080static int get_config_rootfs_path(const char *key, char *retv, int inlen,
4081 struct lxc_conf *c, void *data)
819114b6 4082{
6bede808 4083 return lxc_get_conf_str(retv, inlen, c->rootfs.path);
819114b6 4084}
3af60359 4085
6e54330c
CB
4086static int get_config_rootfs_managed(const char *key, char *retv, int inlen,
4087 struct lxc_conf *c, void *data)
4088{
4089 return lxc_get_conf_bool(c, retv, inlen, c->rootfs.managed);
4090}
4091
6bede808 4092static int get_config_rootfs_mount(const char *key, char *retv, int inlen,
cccd2219 4093 struct lxc_conf *c, void *data)
3af60359 4094{
6bede808 4095 return lxc_get_conf_str(retv, inlen, c->rootfs.mount);
3af60359 4096}
0e9db631 4097
6bede808 4098static int get_config_rootfs_options(const char *key, char *retv, int inlen,
cccd2219 4099 struct lxc_conf *c, void *data)
0e9db631 4100{
6bede808 4101 return lxc_get_conf_str(retv, inlen, c->rootfs.options);
0e9db631 4102}
8f183f38 4103
b67771bc 4104static int get_config_uts_name(const char *key, char *retv, int inlen,
cccd2219 4105 struct lxc_conf *c, void *data)
e274f8b0
CB
4106{
4107 return lxc_get_conf_str(
4108 retv, inlen,
6bede808 4109 c->utsname ? c->utsname->nodename : NULL);
e274f8b0 4110}
466c2e93 4111
6bede808 4112static int get_config_hooks(const char *key, char *retv, int inlen,
cccd2219 4113 struct lxc_conf *c, void *data)
466c2e93
CB
4114{
4115 char *subkey;
4116 int len, fulllen = 0, found = -1;
4117 struct lxc_list *it;
4118 int i;
4119
466c2e93 4120 subkey = strchr(key, '.');
466c2e93 4121 if (!subkey)
5a848c4b 4122 return ret_errno(EINVAL);
47903908 4123
4124 subkey = strchr(subkey + 1, '.');
637d38f2 4125 if (!subkey)
5a848c4b 4126 return ret_errno(EINVAL);
466c2e93 4127 subkey++;
637d38f2 4128 if (*subkey == '\0')
5a848c4b 4129 return ret_errno(EINVAL);
47903908 4130
466c2e93 4131 for (i = 0; i < NUM_LXC_HOOKS; i++) {
d62177e9 4132 if (strequal(lxchook_names[i], subkey)) {
466c2e93
CB
4133 found = i;
4134 break;
4135 }
4136 }
47903908 4137
466c2e93 4138 if (found == -1)
5a848c4b 4139 return ret_errno(EINVAL);
466c2e93
CB
4140
4141 if (!retv)
4142 inlen = 0;
4143 else
4144 memset(retv, 0, inlen);
4145
6bede808 4146 lxc_list_for_each(it, &c->hooks[found]) {
466c2e93
CB
4147 strprint(retv, inlen, "%s\n", (char *)it->elem);
4148 }
47903908 4149
466c2e93
CB
4150 return fulllen;
4151}
d2ceff53 4152
44ae0fb6
CB
4153static int get_config_hooks_version(const char *key, char *retv, int inlen,
4154 struct lxc_conf *c, void *data)
4155{
4156 return lxc_get_conf_int(c, retv, inlen, c->hooks_version);
4157}
4158
f9373e40
CB
4159static int get_config_net(const char *key, char *retv, int inlen,
4160 struct lxc_conf *c, void *data)
d2ceff53
CB
4161{
4162 int len, fulllen = 0;
4163 struct lxc_list *it;
4164
4165 if (!retv)
4166 inlen = 0;
4167 else
4168 memset(retv, 0, inlen);
4169
6bede808 4170 lxc_list_for_each(it, &c->network) {
d2ceff53
CB
4171 struct lxc_netdev *n = it->elem;
4172 const char *t = lxc_net_type_to_str(n->type);
4173 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
4174 }
4175
4176 return fulllen;
4177}
bdccff60 4178
6bede808 4179static int get_config_cap_drop(const char *key, char *retv, int inlen,
cccd2219 4180 struct lxc_conf *c, void *data)
1c96d6d8
CB
4181{
4182 int len, fulllen = 0;
4183 struct lxc_list *it;
4184
4185 if (!retv)
4186 inlen = 0;
4187 else
4188 memset(retv, 0, inlen);
4189
6bede808 4190 lxc_list_for_each(it, &c->caps) {
b80927f2
CB
4191 strprint(retv, inlen, "%s\n", (char *)it->elem);
4192 }
25a908b8 4193
b80927f2
CB
4194 return fulllen;
4195}
4196
6bede808 4197static int get_config_cap_keep(const char *key, char *retv, int inlen,
cccd2219 4198 struct lxc_conf *c, void *data)
b80927f2
CB
4199{
4200 int len, fulllen = 0;
4201 struct lxc_list *it;
4202
4203 if (!retv)
4204 inlen = 0;
4205 else
4206 memset(retv, 0, inlen);
4207
6bede808 4208 lxc_list_for_each(it, &c->keepcaps) {
1c96d6d8
CB
4209 strprint(retv, inlen, "%s\n", (char *)it->elem);
4210 }
25a908b8 4211
1c96d6d8
CB
4212 return fulllen;
4213}
0692663a 4214
3aed4934
CB
4215static int get_config_console_path(const char *key, char *retv, int inlen,
4216 struct lxc_conf *c, void *data)
0692663a 4217{
6bede808 4218 return lxc_get_conf_str(retv, inlen, c->console.path);
0692663a 4219}
794d1c06 4220
6bede808 4221static int get_config_console_logfile(const char *key, char *retv, int inlen,
cccd2219 4222 struct lxc_conf *c, void *data)
794d1c06 4223{
6bede808 4224 return lxc_get_conf_str(retv, inlen, c->console.log_path);
794d1c06 4225}
75f55b1f 4226
d91adfa6
CB
4227static int get_config_console_rotate(const char *key, char *retv, int inlen,
4228 struct lxc_conf *c, void *data)
4229{
4230 return lxc_get_conf_int(c, retv, inlen, c->console.log_rotate);
4231}
4232
4233
28f3b1cd
CB
4234static int get_config_console_buffer_size(const char *key, char *retv,
4235 int inlen, struct lxc_conf *c,
4236 void *data)
a04220de 4237{
dcea90a0 4238 return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size);
a04220de
CB
4239}
4240
861813e5
CB
4241static int get_config_console_size(const char *key, char *retv, int inlen,
4242 struct lxc_conf *c, void *data)
4243{
4244 return lxc_get_conf_uint64(c, retv, inlen, c->console.log_size);
4245}
4246
50d86993
CB
4247static int get_config_seccomp_allow_nesting(const char *key, char *retv,
4248 int inlen, struct lxc_conf *c,
4249 void *data)
4250{
c3e3c21a
CB
4251#ifdef HAVE_SECCOMP
4252 return lxc_get_conf_int(c, retv, inlen, c->seccomp.allow_nesting);
4253#else
254b5bdd 4254 return ret_errno(ENOSYS);
c3e3c21a 4255#endif
50d86993 4256}
861813e5 4257
84cf6d25
WB
4258static int get_config_seccomp_notify_cookie(const char *key, char *retv, int inlen,
4259 struct lxc_conf *c, void *data)
4260{
4261#ifdef HAVE_SECCOMP_NOTIFY
4262 return lxc_get_conf_str(retv, inlen, c->seccomp.notifier.cookie);
4263#else
6d38035e 4264 return ret_errno(ENOSYS);
84cf6d25
WB
4265#endif
4266}
4267
86ce1da1
CB
4268static int get_config_seccomp_notify_proxy(const char *key, char *retv, int inlen,
4269 struct lxc_conf *c, void *data)
4270{
c3e3c21a 4271#ifdef HAVE_SECCOMP_NOTIFY
86ce1da1 4272 return lxc_get_conf_str(retv, inlen,
c3e3c21a
CB
4273 (c->seccomp.notifier.proxy_addr.sun_path[0]) == '/'
4274 ? &c->seccomp.notifier.proxy_addr.sun_path[0]
4275 : &c->seccomp.notifier.proxy_addr.sun_path[1]);
86ce1da1 4276#else
010d7deb 4277 return ret_errno(ENOSYS);
86ce1da1
CB
4278#endif
4279}
4280
0b427da0
CB
4281static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
4282 struct lxc_conf *c, void *data)
75f55b1f 4283{
c3e3c21a 4284 return lxc_get_conf_str(retv, inlen, c->seccomp.seccomp);
75f55b1f 4285}
97f6dad0 4286
6bede808 4287static int get_config_autodev(const char *key, char *retv, int inlen,
cccd2219 4288 struct lxc_conf *c, void *data)
97f6dad0 4289{
6bede808 4290 return lxc_get_conf_int(c, retv, inlen, c->autodev);
97f6dad0 4291}
afee4324 4292
63012bdd
CK
4293static int get_config_autodev_tmpfs_size(const char *key, char *retv, int inlen,
4294 struct lxc_conf *c, void *data)
4295{
4296 return lxc_get_conf_int(c, retv, inlen, c->autodevtmpfssize);
4297}
4298
55c84efc 4299static int get_config_signal_halt(const char *key, char *retv, int inlen,
25a908b8 4300 struct lxc_conf *c, void *data)
afee4324 4301{
6bede808 4302 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
afee4324 4303}
3aa8f359 4304
55c84efc 4305static int get_config_signal_reboot(const char *key, char *retv, int inlen,
25a908b8 4306 struct lxc_conf *c, void *data)
3aa8f359 4307{
6bede808 4308 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
3aa8f359 4309}
2e16269f 4310
55c84efc 4311static int get_config_signal_stop(const char *key, char *retv, int inlen,
25a908b8 4312 struct lxc_conf *c, void *data)
2e16269f 4313{
6bede808 4314 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
2e16269f 4315}
54345299 4316
6bede808 4317static int get_config_start(const char *key, char *retv, int inlen,
cccd2219 4318 struct lxc_conf *c, void *data)
54345299 4319{
d62177e9 4320 if (strequal(key + 10, "auto"))
6bede808 4321 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
d62177e9 4322 else if (strequal(key + 10, "delay"))
6bede808 4323 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
d62177e9 4324 else if (strequal(key + 10, "order"))
6bede808 4325 return lxc_get_conf_int(c, retv, inlen, c->start_order);
54345299
CB
4326
4327 return -1;
4328}
de9df15e 4329
46cc906d 4330static int get_config_log_syslog(const char *key, char *retv, int inlen,
25a908b8 4331 struct lxc_conf *c, void *data)
de9df15e 4332{
6bede808 4333 return lxc_get_conf_str(retv, inlen, c->syslog);
de9df15e 4334}
ac0f949c 4335
6bede808 4336static int get_config_monitor(const char *key, char *retv, int inlen,
cccd2219 4337 struct lxc_conf *c, void *data)
ac0f949c 4338{
6bede808 4339 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
ac0f949c 4340}
90ec7b6e 4341
258f8051
CB
4342static int get_config_monitor_signal_pdeath(const char *key, char *retv,
4343 int inlen, struct lxc_conf *c,
4344 void *data)
4345{
4346 return lxc_get_conf_int(c, retv, inlen, c->monitor_signal_pdeath);
4347}
4348
6bede808 4349static int get_config_group(const char *key, char *retv, int inlen,
cccd2219 4350 struct lxc_conf *c, void *data)
90ec7b6e
CB
4351{
4352 int len, fulllen = 0;
4353 struct lxc_list *it;
4354
4355 if (!retv)
4356 inlen = 0;
4357 else
4358 memset(retv, 0, inlen);
4359
6bede808 4360 lxc_list_for_each(it, &c->groups) {
90ec7b6e
CB
4361 strprint(retv, inlen, "%s\n", (char *)it->elem);
4362 }
25a908b8 4363
90ec7b6e
CB
4364 return fulllen;
4365}
aa0db7d3 4366
6bede808 4367static int get_config_environment(const char *key, char *retv, int inlen,
cccd2219 4368 struct lxc_conf *c, void *data)
aa0db7d3
CB
4369{
4370 int len, fulllen = 0;
4371 struct lxc_list *it;
4372
4373 if (!retv)
4374 inlen = 0;
4375 else
4376 memset(retv, 0, inlen);
4377
6bede808 4378 lxc_list_for_each(it, &c->environment) {
aa0db7d3
CB
4379 strprint(retv, inlen, "%s\n", (char *)it->elem);
4380 }
25a908b8 4381
aa0db7d3
CB
4382 return fulllen;
4383}
96dfcb7d 4384
5cda27c1
SH
4385static int get_config_execute_cmd(const char *key, char *retv, int inlen,
4386 struct lxc_conf *c, void *data)
4387{
4388 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
4389}
4390
6bede808 4391static int get_config_init_cmd(const char *key, char *retv, int inlen,
cccd2219 4392 struct lxc_conf *c, void *data)
96dfcb7d 4393{
6bede808 4394 return lxc_get_conf_str(retv, inlen, c->init_cmd);
96dfcb7d 4395}
1398e9b1 4396
3c491553
L
4397static int get_config_init_cwd(const char *key, char *retv, int inlen,
4398 struct lxc_conf *c, void *data)
4399{
4400 return lxc_get_conf_str(retv, inlen, c->init_cwd);
4401}
4402
6bede808 4403static int get_config_init_uid(const char *key, char *retv, int inlen,
cccd2219 4404 struct lxc_conf *c, void *data)
1398e9b1 4405{
6bede808 4406 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
1398e9b1 4407}
dfeb7e42 4408
6bede808 4409static int get_config_init_gid(const char *key, char *retv, int inlen,
cccd2219 4410 struct lxc_conf *c, void *data)
dfeb7e42 4411{
6bede808 4412 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
dfeb7e42 4413}
62048afe 4414
bf31b337
RJ
4415static int get_config_init_groups(const char *key, char *retv, int inlen,
4416 struct lxc_conf *c, void *data)
4417{
4418 int fulllen = 0, len;
4419
4420 if (!retv)
4421 inlen = 0;
4422 else
4423 memset(retv, 0, inlen);
4424
4425 if (c->init_groups.size == 0)
4426 return 0;
4427
4428 for (int i = 0; i < c->init_groups.size; i++)
4429 strprint(retv, inlen, "%s%d", (i > 0) ? "," : "",
4430 c->init_groups.list[i]);
4431
4432 return fulllen;
4433}
4434
6bede808 4435static int get_config_ephemeral(const char *key, char *retv, int inlen,
cccd2219 4436 struct lxc_conf *c, void *data)
62048afe 4437{
6bede808 4438 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
62048afe 4439}
b09521ac 4440
6bede808 4441static int get_config_no_new_privs(const char *key, char *retv, int inlen,
cccd2219 4442 struct lxc_conf *c, void *data)
b09521ac 4443{
6bede808 4444 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
b09521ac 4445}
389f6466 4446
25a908b8 4447/* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
240d4b74 4448 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4449 * printed, in 'lxc.prlimit.resource = value' format.
389f6466 4450 */
240d4b74 4451static int get_config_prlimit(const char *key, char *retv, int inlen,
47903908 4452 struct lxc_conf *c, void *data)
389f6466
CB
4453{
4454 int fulllen = 0, len;
4455 bool get_all = false;
4456 struct lxc_list *it;
4457
4458 if (!retv)
4459 inlen = 0;
4460 else
4461 memset(retv, 0, inlen);
4462
d62177e9 4463 if (strequal(key, "lxc.prlimit"))
389f6466 4464 get_all = true;
1af3044f 4465 else if (strnequal(key, "lxc.prlimit.", 12))
240d4b74 4466 key += 12;
389f6466 4467 else
4c12267e 4468 return ret_errno(EINVAL);
389f6466 4469
6bede808 4470 lxc_list_for_each(it, &c->limits) {
c77aee64
CB
4471 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4472 char buf[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
389f6466
CB
4473 int partlen;
4474 struct lxc_limit *lim = it->elem;
4475
4476 if (lim->limit.rlim_cur == RLIM_INFINITY) {
6333c915
CB
4477 memcpy(buf, "unlimited", STRLITERALLEN("unlimited") + 1);
4478 partlen = STRLITERALLEN("unlimited");
389f6466
CB
4479 } else {
4480 partlen = sprintf(buf, "%" PRIu64,
4481 (uint64_t)lim->limit.rlim_cur);
4482 }
47903908 4483
389f6466 4484 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
25a908b8 4485 if (lim->limit.rlim_max == RLIM_INFINITY)
389f6466 4486 memcpy(buf + partlen, ":unlimited",
6333c915 4487 STRLITERALLEN(":unlimited") + 1);
25a908b8 4488 else
389f6466
CB
4489 sprintf(buf + partlen, ":%" PRIu64,
4490 (uint64_t)lim->limit.rlim_max);
389f6466
CB
4491 }
4492
4493 if (get_all) {
240d4b74 4494 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
389f6466 4495 lim->resource, buf);
d62177e9 4496 } else if (strequal(lim->resource, key)) {
389f6466
CB
4497 strprint(retv, inlen, "%s", buf);
4498 }
4499 }
4500
4501 return fulllen;
4502}
e08cb901 4503
e409b214
CB
4504/* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4505 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4506 * entries will be printed, in 'lxc.sysctl.key = value' format.
7edd0540
L
4507 */
4508static int get_config_sysctl(const char *key, char *retv, int inlen,
47903908 4509 struct lxc_conf *c, void *data)
7edd0540
L
4510{
4511 int len;
4512 struct lxc_list *it;
4513 int fulllen = 0;
4514 bool get_all = false;
4515
4516 if (!retv)
4517 inlen = 0;
4518 else
4519 memset(retv, 0, inlen);
4520
d62177e9 4521 if (strequal(key, "lxc.sysctl"))
7edd0540 4522 get_all = true;
1af3044f 4523 else if (strnequal(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")))
6333c915 4524 key += STRLITERALLEN("lxc.sysctl.");
7edd0540 4525 else
4b1ef6aa 4526 return ret_errno(EINVAL);
7edd0540
L
4527
4528 lxc_list_for_each(it, &c->sysctls) {
4529 struct lxc_sysctl *elem = it->elem;
4530 if (get_all) {
47903908 4531 strprint(retv, inlen, "lxc.sysctl.%s = %s\n", elem->key,
4532 elem->value);
d62177e9 4533 } else if (strequal(elem->key, key)) {
7edd0540
L
4534 strprint(retv, inlen, "%s", elem->value);
4535 }
4536 }
4537
4538 return fulllen;
4539}
4540
61d7a733 4541static int get_config_proc(const char *key, char *retv, int inlen,
47903908 4542 struct lxc_conf *c, void *data)
61d7a733
YT
4543{
4544 struct lxc_list *it;
4545 int len;
4546 int fulllen = 0;
4547 bool get_all = false;
4548
4549 if (!retv)
4550 inlen = 0;
4551 else
4552 memset(retv, 0, inlen);
4553
d62177e9 4554 if (strequal(key, "lxc.proc"))
61d7a733 4555 get_all = true;
1af3044f 4556 else if (strnequal(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")))
6333c915 4557 key += STRLITERALLEN("lxc.proc.");
61d7a733 4558 else
5ab1dbcf 4559 return ret_errno(EINVAL);
61d7a733
YT
4560
4561 lxc_list_for_each(it, &c->procs) {
4562 struct lxc_proc *proc = it->elem;
4563
4564 if (get_all) {
4565 strprint(retv, inlen, "lxc.proc.%s = %s\n",
47903908 4566 proc->filename, proc->value);
d62177e9 4567 } else if (strequal(proc->filename, key)) {
61d7a733
YT
4568 strprint(retv, inlen, "%s", proc->value);
4569 }
4570 }
4571
4572 return fulllen;
4573}
4574
1d8d3676
CB
4575static int get_config_namespace_clone(const char *key, char *retv, int inlen,
4576 struct lxc_conf *c, void *data)
4577{
4578 int i, len;
4579 int fulllen = 0;
4580
4581 if (!retv)
4582 inlen = 0;
4583 else
4584 memset(retv, 0, inlen);
4585
4586 for (i = 0; i < LXC_NS_MAX; i++) {
4587 if (c->ns_clone & ns_info[i].clone_flag)
4588 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4589 }
4590
4591 return fulllen;
4592}
4593
abeb5bba
CB
4594static int get_config_namespace_keep(const char *key, char *retv, int inlen,
4595 struct lxc_conf *c, void *data)
4596{
4597 int i, len;
4598 int fulllen = 0;
4599
4600 if (!retv)
4601 inlen = 0;
4602 else
4603 memset(retv, 0, inlen);
4604
4605 for (i = 0; i < LXC_NS_MAX; i++) {
4606 if (c->ns_keep & ns_info[i].clone_flag)
4607 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4608 }
4609
4610 return fulllen;
4611}
4612
70fd7fc9
CB
4613static int get_config_time_offset_boot(const char *key, char *retv, int inlen, struct lxc_conf *c,
4614 void *data)
4615{
4616 int len;
4617 int fulllen = 0;
4618
4619 if (!retv)
4620 inlen = 0;
4621 else
4622 memset(retv, 0, inlen);
4623
4624 if (c->timens.s_boot) {
4625 strprint(retv, inlen, "%" PRId64 " s\n", c->timens.s_boot);
4626 } else {
4627 strprint(retv, inlen, "%" PRId64 " ns\n", c->timens.ns_boot);
4628 }
4629
4630 return fulllen;
4631}
4632
4633static int get_config_time_offset_monotonic(const char *key, char *retv, int inlen,
4634 struct lxc_conf *c, void *data)
4635{
4636 int len;
4637 int fulllen = 0;
4638
4639 if (!retv)
4640 inlen = 0;
4641 else
4642 memset(retv, 0, inlen);
4643
4644 if (c->timens.s_monotonic) {
4645 strprint(retv, inlen, "%" PRId64 "s\n", c->timens.s_monotonic);
4646 } else {
4647 strprint(retv, inlen, "%" PRId64 "ns\n", c->timens.ns_monotonic);
4648 }
4649
4650 return fulllen;
4651}
4652
b074bbf1
CB
4653static int get_config_namespace_share(const char *key, char *retv, int inlen,
4654 struct lxc_conf *c, void *data)
28d9e29e
CB
4655{
4656 int len, ns_idx;
4657 const char *namespace;
4658 int fulllen = 0;
4659
4660 if (!retv)
4661 inlen = 0;
4662 else
4663 memset(retv, 0, inlen);
4664
6333c915 4665 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
4666 ns_idx = lxc_namespace_2_ns_idx(namespace);
4667 if (ns_idx < 0)
4668 return ns_idx;
4669
b074bbf1 4670 strprint(retv, inlen, "%s", c->ns_share[ns_idx]);
28d9e29e
CB
4671
4672 return fulllen;
4673}
4674
e08cb901 4675/* Callbacks to clear config items. */
26471403
CB
4676static inline int clr_config_personality(const char *key, struct lxc_conf *c,
4677 void *data)
e08cb901
CB
4678{
4679 c->personality = -1;
4680 return 0;
4681}
03818ae3 4682
232763d6
CB
4683static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
4684 void *data)
03818ae3 4685{
e528c735 4686 c->pty_max = 0;
03818ae3
CB
4687 return 0;
4688}
e7a4b096 4689
fe1c5887
CB
4690static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
4691 void *data)
e7a4b096 4692{
885766f5 4693 c->ttys.tty = 0;
e7a4b096
CB
4694 return 0;
4695}
eaf8c0c7 4696
42e53c29 4697static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
26471403 4698 void *data)
eaf8c0c7 4699{
faf3d0ae 4700 free_disarm(c->ttys.dir);
eaf8c0c7
CB
4701 return 0;
4702}
6bd86308 4703
953fe44f
CB
4704static inline int clr_config_apparmor_profile(const char *key,
4705 struct lxc_conf *c, void *data)
025718fb 4706{
1c64e3ed 4707 free_disarm(c->lsm_aa_profile);
025718fb
CB
4708 return 0;
4709}
3061e04e 4710
953fe44f
CB
4711static inline int clr_config_apparmor_allow_incomplete(const char *key,
4712 struct lxc_conf *c,
4713 void *data)
3061e04e
CB
4714{
4715 c->lsm_aa_allow_incomplete = 0;
4716 return 0;
4717}
31fc3494 4718
1800f924
WB
4719static inline int clr_config_apparmor_allow_nesting(const char *key,
4720 struct lxc_conf *c,
4721 void *data)
4722{
4723 c->lsm_aa_allow_nesting = 0;
4724 return 0;
4725}
4726
4727static inline int clr_config_apparmor_raw(const char *key,
4728 struct lxc_conf *c,
4729 void *data)
4730{
4731 return lxc_clear_apparmor_raw(c);
4732}
4733
953fe44f
CB
4734static inline int clr_config_selinux_context(const char *key,
4735 struct lxc_conf *c, void *data)
31fc3494 4736{
6afcf6dd 4737 free_disarm(c->lsm_se_context);
31fc3494
CB
4738 return 0;
4739}
754d01ce 4740
4fef78bc
MB
4741static inline int clr_config_selinux_context_keyring(const char *key,
4742 struct lxc_conf *c, void *data)
4743{
b60c8dd9 4744 free_disarm(c->lsm_se_keyring_context);
4fef78bc
MB
4745 return 0;
4746}
4747
8f818a84
MB
4748static inline int clr_config_keyring_session(const char *key,
4749 struct lxc_conf *c, void *data)
4750{
4751 c->keyring_disable_session = false;
4752 return 0;
4753}
4754
43654d34
CB
4755static inline int clr_config_cgroup_controller(const char *key,
4756 struct lxc_conf *c, void *data)
754d01ce 4757{
54860ed0
CB
4758 return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC);
4759}
4760
4761static inline int clr_config_cgroup2_controller(const char *key,
4762 struct lxc_conf *c, void *data)
4763{
4764 return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC);
754d01ce 4765}
d3a178de 4766
43654d34
CB
4767static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
4768 void *data)
4769{
d62177e9 4770 if (!strequal(key, "lxc.cgroup.dir"))
c583072d
CB
4771 return ret_errno(EINVAL);
4772
bd5501d2
CB
4773 if (lxc_conf->cgroup_meta.dir)
4774 free_disarm(lxc_conf->cgroup_meta.dir);
43654d34
CB
4775
4776 return 0;
4777}
4778
a900cbaf
WB
4779static int clr_config_cgroup_monitor_dir(const char *key,
4780 struct lxc_conf *lxc_conf,
4781 void *data)
4782{
4783 free_disarm(lxc_conf->cgroup_meta.monitor_dir);
4784 return 0;
4785}
4786
7696c1f9
RJ
4787static int clr_config_cgroup_monitor_pivot_dir(const char *key,
4788 struct lxc_conf *lxc_conf,
4789 void *data)
4790{
4791 free_disarm(lxc_conf->cgroup_meta.monitor_pivot_dir);
4792 return 0;
4793}
4794
a900cbaf
WB
4795static int clr_config_cgroup_container_dir(const char *key,
4796 struct lxc_conf *lxc_conf,
4797 void *data)
4798{
4799 free_disarm(lxc_conf->cgroup_meta.container_dir);
4800 return 0;
4801}
4802
4803static int clr_config_cgroup_container_inner_dir(const char *key,
4804 struct lxc_conf *lxc_conf,
4805 void *data)
4806{
4807 free_disarm(lxc_conf->cgroup_meta.namespace_dir);
4808 return 0;
4809}
4810
9caee129
CB
4811static inline int clr_config_cgroup_relative(const char *key,
4812 struct lxc_conf *lxc_conf,
4813 void *data)
76f0e2e7 4814{
9caee129 4815 lxc_conf->cgroup_meta.relative = false;
76f0e2e7
CB
4816 return 0;
4817}
4818
26471403
CB
4819static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
4820 void *data)
d3a178de
CB
4821{
4822 return lxc_clear_idmaps(c);
4823}
97d3338d 4824
46cc906d 4825static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
26471403 4826 void *data)
97d3338d 4827{
4b73005c 4828 c->loglevel = LXC_LOG_LEVEL_NOTSET;
97d3338d
CB
4829 return 0;
4830}
de46099c 4831
46cc906d 4832static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
26471403 4833 void *data)
de46099c 4834{
150c191e 4835 free_disarm(c->logfile);
de46099c
CB
4836 return 0;
4837}
b4fa13cd 4838
26471403
CB
4839static inline int clr_config_mount(const char *key, struct lxc_conf *c,
4840 void *data)
b4fa13cd
CB
4841{
4842 return lxc_clear_mount_entries(c);
4843}
4be81021 4844
26471403
CB
4845static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
4846 void *data)
4be81021
CB
4847{
4848 return lxc_clear_automounts(c);
4849}
350d4b15 4850
47148e96
CB
4851static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
4852 void *data)
350d4b15 4853{
6620228b 4854 free_disarm(c->fstab);
350d4b15
CB
4855 return 0;
4856}
faca124d 4857
7a96a068
CB
4858static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
4859 void *data)
faca124d 4860{
ad16f12d 4861 free_disarm(c->rootfs.path);
faca124d
CB
4862 return 0;
4863}
4864
6e54330c
CB
4865static inline int clr_config_rootfs_managed(const char *key, struct lxc_conf *c,
4866 void *data)
4867{
4868 c->rootfs.managed = true;
4869 return 0;
4870}
4871
26471403
CB
4872static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
4873 void *data)
fddefc2d 4874{
7decd496 4875 free_disarm(c->rootfs.mount);
fddefc2d
CB
4876 return 0;
4877}
7b1eb67d 4878
26471403
CB
4879static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
4880 void *data)
7b1eb67d 4881{
30f8754d
CB
4882 free_disarm(c->rootfs.options);
4883 free_disarm(c->rootfs.data);
3437f95c 4884
7b1eb67d
CB
4885 return 0;
4886}
02becb8d 4887
b67771bc 4888static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
26471403 4889 void *data)
d31d0103 4890{
137ee4b6 4891 free_disarm(c->utsname);
d31d0103
CB
4892 return 0;
4893}
c9eeb90c 4894
26471403
CB
4895static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
4896 void *data)
c9eeb90c
CB
4897{
4898 return lxc_clear_hooks(c, key);
4899}
4900
44ae0fb6
CB
4901static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c,
4902 void *data)
4903{
4904 /* default to legacy hooks version */
4905 c->hooks_version = 0;
4906 return 0;
4907}
4908
f9373e40
CB
4909static inline int clr_config_net(const char *key, struct lxc_conf *c,
4910 void *data)
f4488271 4911{
c302b476
CB
4912 lxc_free_networks(&c->network);
4913
e5d2fd7c 4914 return 0;
f4488271
CB
4915}
4916
26471403
CB
4917static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
4918 void *data)
244cb55b
CB
4919{
4920 return lxc_clear_config_caps(c);
4921}
c74cc490 4922
26471403
CB
4923static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
4924 void *data)
c74cc490
CB
4925{
4926 return lxc_clear_config_keepcaps(c);
4927}
4e5b633f 4928
3aed4934
CB
4929static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
4930 void *data)
4e5b633f 4931{
2ed97e6f 4932 free_disarm(c->console.path);
4e5b633f
CB
4933 return 0;
4934}
4935
7c2ec23a 4936static inline int clr_config_console_logfile(const char *key,
26471403 4937 struct lxc_conf *c, void *data)
7c2ec23a 4938{
e68dfc25 4939 free_disarm(c->console.log_path);
7c2ec23a
CB
4940 return 0;
4941}
bbca37d8 4942
d91adfa6
CB
4943static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c,
4944 void *data)
4945{
4946 c->console.log_rotate = 0;
4947 return 0;
4948}
4949
28f3b1cd
CB
4950static inline int clr_config_console_buffer_size(const char *key,
4951 struct lxc_conf *c, void *data)
a04220de 4952{
28f3b1cd 4953 c->console.buffer_size = 0;
a04220de
CB
4954 return 0;
4955}
4956
861813e5
CB
4957static inline int clr_config_console_size(const char *key, struct lxc_conf *c,
4958 void *data)
4959{
4960 c->console.log_size = 0;
4961 return 0;
4962}
4963
50d86993
CB
4964static inline int clr_config_seccomp_allow_nesting(const char *key,
4965 struct lxc_conf *c, void *data)
4966{
c3e3c21a
CB
4967#ifdef HAVE_SECCOMP
4968 c->seccomp.allow_nesting = 0;
50d86993 4969 return 0;
c3e3c21a 4970#else
0a6980fe 4971 return ret_errno(ENOSYS);
c3e3c21a 4972#endif
50d86993
CB
4973}
4974
84cf6d25
WB
4975static inline int clr_config_seccomp_notify_cookie(const char *key,
4976 struct lxc_conf *c, void *data)
4977{
4978#ifdef HAVE_SECCOMP_NOTIFY
46bc1573 4979 free_disarm(c->seccomp.notifier.cookie);
84cf6d25
WB
4980 return 0;
4981#else
46bc1573 4982 return ret_errno(ENOSYS);
84cf6d25
WB
4983#endif
4984}
4985
86ce1da1
CB
4986static inline int clr_config_seccomp_notify_proxy(const char *key,
4987 struct lxc_conf *c, void *data)
4988{
c3e3c21a
CB
4989#ifdef HAVE_SECCOMP_NOTIFY
4990 memset(&c->seccomp.notifier.proxy_addr, 0,
4991 sizeof(c->seccomp.notifier.proxy_addr));
86ce1da1
CB
4992 return 0;
4993#else
eaade76d 4994 return ret_errno(ENOSYS);
86ce1da1
CB
4995#endif
4996}
4997
0b427da0
CB
4998static inline int clr_config_seccomp_profile(const char *key,
4999 struct lxc_conf *c, void *data)
bbca37d8 5000{
9902a5d0 5001 free_disarm(c->seccomp.seccomp);
bbca37d8
CB
5002 return 0;
5003}
c721e86c 5004
26471403
CB
5005static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
5006 void *data)
c721e86c
CB
5007{
5008 c->autodev = 1;
5009 return 0;
5010}
87b288d1 5011
63012bdd
CK
5012static inline int clr_config_autodev_tmpfs_size(const char *key, struct lxc_conf *c,
5013 void *data)
5014{
5015 c->autodevtmpfssize = 500000;
5016 return 0;
5017}
5018
55c84efc 5019static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
26471403 5020 void *data)
87b288d1
CB
5021{
5022 c->haltsignal = 0;
5023 return 0;
5024}
cae63cfa 5025
55c84efc 5026static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
26471403 5027 void *data)
cae63cfa
CB
5028{
5029 c->rebootsignal = 0;
5030 return 0;
5031}
de45f3a8 5032
55c84efc 5033static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
26471403 5034 void *data)
de45f3a8
CB
5035{
5036 c->stopsignal = 0;
5037 return 0;
5038}
c6182222 5039
26471403
CB
5040static inline int clr_config_start(const char *key, struct lxc_conf *c,
5041 void *data)
c6182222 5042{
d62177e9 5043 if (strequal(key + 10, "auto"))
c6182222 5044 c->start_auto = 0;
d62177e9 5045 else if (strequal(key + 10, "delay"))
c6182222 5046 c->start_delay = 0;
d62177e9 5047 else if (strequal(key + 10, "order"))
c6182222
CB
5048 c->start_order = 0;
5049
5050 return 0;
5051}
998ca94f 5052
46cc906d 5053static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
26471403 5054 void *data)
998ca94f 5055{
f9f6f28f 5056 free_disarm(c->syslog);
998ca94f
CB
5057 return 0;
5058}
adad12ca 5059
26471403
CB
5060static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
5061 void *data)
adad12ca
CB
5062{
5063 c->monitor_unshare = 0;
5064 return 0;
5065}
4850d223 5066
258f8051
CB
5067static inline int clr_config_monitor_signal_pdeath(const char *key,
5068 struct lxc_conf *c, void *data)
5069{
5070 c->monitor_signal_pdeath = 0;
5071 return 0;
5072}
5073
26471403
CB
5074static inline int clr_config_group(const char *key, struct lxc_conf *c,
5075 void *data)
4850d223
CB
5076{
5077 return lxc_clear_groups(c);
5078}
832fb63a 5079
26471403
CB
5080static inline int clr_config_environment(const char *key, struct lxc_conf *c,
5081 void *data)
832fb63a
CB
5082{
5083 return lxc_clear_environment(c);
4850d223 5084}
8e90af3e 5085
5cda27c1
SH
5086static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
5087 void *data)
5088{
e8f6cdb3 5089 free_disarm(c->execute_cmd);
5cda27c1
SH
5090 return 0;
5091}
5092
26471403
CB
5093static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
5094 void *data)
8e90af3e 5095{
df45b761 5096 free_disarm(c->init_cmd);
8e90af3e
CB
5097 return 0;
5098}
ec76dcfb 5099
3c491553
L
5100static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
5101 void *data)
5102{
5fab421a 5103 free_disarm(c->init_cwd);
3c491553
L
5104 return 0;
5105}
5106
26471403
CB
5107static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
5108 void *data)
ec76dcfb
CB
5109{
5110 c->init_uid = 0;
5111 return 0;
5112}
1044b247 5113
26471403
CB
5114static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
5115 void *data)
1044b247
CB
5116{
5117 c->init_gid = 0;
5118 return 0;
5119}
59e370db 5120
bf31b337
RJ
5121static inline int clr_config_init_groups(const char *key, struct lxc_conf *c,
5122 void *data)
5123{
bf31b337 5124 c->init_groups.size = 0;
c71f64cb 5125 free_disarm(c->init_groups.list);
bf31b337
RJ
5126 return 0;
5127}
5128
26471403
CB
5129static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
5130 void *data)
59e370db
CB
5131{
5132 c->ephemeral = 0;
5133 return 0;
5134}
b98c5ab0 5135
26471403
CB
5136static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
5137 void *data)
b98c5ab0
CB
5138{
5139 c->no_new_privs = false;
5140 return 0;
5141}
715ccc96 5142
240d4b74 5143static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
26471403 5144 void *data)
715ccc96
CB
5145{
5146 return lxc_clear_limits(c, key);
5147}
fdf3c589 5148
7edd0540
L
5149static inline int clr_config_sysctl(const char *key, struct lxc_conf *c,
5150 void *data)
5151{
5152 return lxc_clear_sysctls(c, key);
5153}
5154
61d7a733
YT
5155static inline int clr_config_proc(const char *key, struct lxc_conf *c,
5156 void *data)
5157{
5158 return lxc_clear_procs(c, key);
5159}
5160
26471403
CB
5161static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
5162 void *data)
fdf3c589
CB
5163{
5164 lxc_clear_includes(c);
5165 return 0;
5166}
a3c8e600 5167
1d8d3676
CB
5168static int clr_config_namespace_clone(const char *key,
5169 struct lxc_conf *lxc_conf, void *data)
5170{
5171 lxc_conf->ns_clone = 0;
5172 return 0;
5173}
5174
abeb5bba
CB
5175static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf,
5176 void *data)
5177{
5178 lxc_conf->ns_keep = 0;
5179 return 0;
5180}
5181
70fd7fc9
CB
5182static int clr_config_time_offset_boot(const char *key, struct lxc_conf *lxc_conf, void *data)
5183{
5184 lxc_conf->timens.s_boot = 0;
5185 lxc_conf->timens.ns_boot = 0;
5186 return 0;
5187}
5188
5189static int clr_config_time_offset_monotonic(const char *key, struct lxc_conf *lxc_conf, void *data)
5190{
5191 lxc_conf->timens.s_monotonic = 0;
5192 lxc_conf->timens.ns_monotonic = 0;
5193 return 0;
5194}
5195
b074bbf1
CB
5196static int clr_config_namespace_share(const char *key,
5197 struct lxc_conf *lxc_conf, void *data)
28d9e29e
CB
5198{
5199 int ns_idx;
5200 const char *namespace;
5201
6333c915 5202 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
5203 ns_idx = lxc_namespace_2_ns_idx(namespace);
5204 if (ns_idx < 0)
5205 return ns_idx;
5206
b074bbf1
CB
5207 free(lxc_conf->ns_share[ns_idx]);
5208 lxc_conf->ns_share[ns_idx] = NULL;
28d9e29e
CB
5209
5210 return 0;
5211}
5212
a3c8e600 5213static int get_config_includefiles(const char *key, char *retv, int inlen,
cccd2219 5214 struct lxc_conf *c, void *data)
a3c8e600 5215{
218c46ec 5216 return ret_errno(ENOSYS);
a3c8e600 5217}
40db5d2f 5218
7707b0e0
CB
5219struct config_net_info {
5220 char buf[NETWORK_SUBKEY_SIZE_MAX];
5221 const char *subkey;
5222 const struct lxc_config_net_t *ops;
5223 struct lxc_netdev *netdev;
5224};
5225
5226static int get_network_config_ops(const char *key, struct lxc_conf *lxc_conf,
5227 struct config_net_info *info, bool allocate)
40db5d2f 5228{
31ee747b 5229 int ret;
0abcc213 5230 int64_t tmpidx;
7707b0e0
CB
5231 unsigned int idx;
5232 const char *idx_start;
0abcc213
CB
5233
5234 if (is_empty_string(key))
7707b0e0 5235 return ret_errno(EINVAL);
40db5d2f
CB
5236
5237 /* check that this is a sensible network key */
6a52a513 5238 if (!strnequal("lxc.net.", key, STRLITERALLEN("lxc.net.")))
7707b0e0 5239 return syserror_set(-EINVAL, "Invalid network configuration key \"%s\"", key);
40db5d2f 5240
f9373e40 5241 /* lxc.net.<n> */
0abcc213
CB
5242 /* beginning of index string */
5243 idx_start = key + STRLITERALLEN("lxc.net.");
5244 if (!isdigit(*idx_start))
7707b0e0 5245 return syserror_set(-EINVAL, "Failed to detect digit in string \"%s\"", key + 8);
40db5d2f 5246
7707b0e0 5247 ret = lxc_safe_int64_residual(idx_start, &tmpidx, 10, info->buf, sizeof(info->buf));
0abcc213 5248 if (ret)
7707b0e0 5249 return syserror("Failed to parse network index");
40db5d2f 5250
0abcc213 5251 if (tmpidx < 0 || tmpidx >= INT_MAX)
7707b0e0
CB
5252 return syserror_set(-ERANGE, "Number of configured networks would overflow the counter");
5253 idx = (unsigned int)tmpidx;
31ee747b 5254
7707b0e0
CB
5255 info->netdev = lxc_get_netdev_by_idx(lxc_conf, idx, allocate);
5256 if (!info->netdev)
5257 return ret_errno(EINVAL);
5258
5259 /* Make sure subkey points to the empty string. */
5260 info->subkey = info->buf;
5261 if (is_empty_string(info->subkey))
5262 return ret_errno(ENOENT);
31ee747b 5263
7707b0e0
CB
5264 if (info->subkey[0] != '.')
5265 return syserror_set(-EINVAL, "Invalid subkey");
5266 info->subkey++;
31ee747b 5267
7451daf8 5268 /* lxc.net.<idx>.<subkey> */
7707b0e0
CB
5269 info->ops = lxc_get_config_net(info->subkey);
5270 if (!info->ops)
5271 return syserror_set(-ENOENT, "Unknown network configuration key \"%s\"", key);
7451daf8 5272
7707b0e0 5273 return 0;
40db5d2f
CB
5274}
5275
25a908b8
CB
5276/* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5277 * found. So we make sure next comes an integer, find the right callback (by
5278 * rewriting the key), and call it.
40db5d2f 5279 */
7707b0e0
CB
5280static int set_config_jump_table_net(const char *key, const char *value,
5281 struct lxc_conf *lxc_conf, void *data)
40db5d2f 5282{
7707b0e0
CB
5283 struct config_net_info info = {};
5284 int ret;
31ee747b 5285 const char *idxstring;
31ee747b 5286
0abcc213 5287 idxstring = key + STRLITERALLEN("lxc.net.");
31ee747b 5288 if (!isdigit(*idxstring))
d5c2cd94 5289 return ret_errno(EINVAL);
40db5d2f 5290
6bed0fb6 5291 if (lxc_config_value_empty(value))
7707b0e0 5292 return clr_config_jump_table_net(key, lxc_conf, data);
40db5d2f 5293
7707b0e0
CB
5294 ret = get_network_config_ops(key, lxc_conf, &info, true);
5295 if (ret)
5296 return ret;
47903908 5297
7707b0e0 5298 return info.ops->set(info.subkey, value, lxc_conf, info.netdev);
40db5d2f 5299}
ff6da295 5300
7707b0e0
CB
5301static int clr_config_jump_table_net(const char *key, struct lxc_conf *lxc_conf,
5302 void *data)
ff6da295 5303{
7707b0e0 5304 struct config_net_info info = {};
31ee747b 5305 int ret;
519df1c1 5306 const char *idxstring;
519df1c1 5307
31ee747b
CB
5308 idxstring = key + 8;
5309 if (!isdigit(*idxstring))
ec35c656 5310 return ret_errno(EINVAL);
519df1c1 5311
519df1c1 5312 /* The left conjunct is pretty self-explanatory. The right conjunct
31ee747b 5313 * checks whether the two pointers are equal. If they are we know that
519df1c1
CB
5314 * this is not a key that is namespaced any further and so we are
5315 * supposed to clear the whole network.
5316 */
5317 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
5318 unsigned int rmnetdevidx;
5319
ec35c656
CB
5320 ret = lxc_safe_uint(idxstring, &rmnetdevidx);
5321 if (ret < 0)
5322 return ret;
519df1c1
CB
5323
5324 /* Remove network from network list. */
5325 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
5326 return 0;
5327 }
ff6da295 5328
7707b0e0
CB
5329 ret = get_network_config_ops(key, lxc_conf, &info, false);
5330 if (ret)
5331 return ret;
47903908 5332
7707b0e0 5333 return info.ops->clr(info.subkey, lxc_conf, info.netdev);
ff6da295
CB
5334}
5335
f9373e40
CB
5336static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
5337 void *data)
ff6da295 5338{
070a05af 5339 struct lxc_netdev *netdev = data;
ff6da295 5340
ff6da295 5341 if (!netdev)
ee52bb2f 5342 return ret_errno(EINVAL);
ff6da295 5343
8d508eaa 5344 lxc_clear_netdev(netdev);
ff6da295
CB
5345
5346 return 0;
5347}
5348
f9373e40
CB
5349static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
5350 void *data)
ff6da295 5351{
070a05af 5352 struct lxc_netdev *netdev = data;
ff6da295 5353
ff6da295 5354 if (!netdev)
24a28704 5355 return ret_errno(EINVAL);
ff6da295 5356
de4855a8 5357 netdev->name[0] = '\0';
ff6da295
CB
5358
5359 return 0;
5360}
5361
f9373e40
CB
5362static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
5363 void *data)
ff6da295 5364{
070a05af 5365 struct lxc_netdev *netdev = data;
ff6da295 5366
ff6da295 5367 if (!netdev)
a1ff93c9 5368 return ret_errno(EINVAL);
ff6da295
CB
5369
5370 netdev->flags = 0;
5371
5372 return 0;
5373}
5374
f9373e40
CB
5375static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
5376 void *data)
ff6da295 5377{
070a05af 5378 struct lxc_netdev *netdev = data;
ff6da295 5379
ff6da295 5380 if (!netdev)
08146143 5381 return ret_errno(EINVAL);
ff6da295 5382
de4855a8 5383 netdev->link[0] = '\0';
ff6da295
CB
5384
5385 return 0;
5386}
5387
6509154d 5388static int clr_config_net_l2proxy(const char *key, struct lxc_conf *lxc_conf,
5389 void *data)
5390{
5391 struct lxc_netdev *netdev = data;
5392
5393 if (!netdev)
0171e2a9 5394 return ret_errno(EINVAL);
6509154d 5395
5396 netdev->l2proxy = false;
5397
5398 return 0;
5399}
5400
f9373e40
CB
5401static int clr_config_net_macvlan_mode(const char *key,
5402 struct lxc_conf *lxc_conf, void *data)
ff6da295 5403{
070a05af 5404 struct lxc_netdev *netdev = data;
ff6da295 5405
ff6da295 5406 if (!netdev)
a0ea16c5 5407 return ret_errno(EINVAL);
ff6da295 5408
6bed0fb6
CB
5409 if (netdev->type != LXC_NET_MACVLAN)
5410 return 0;
5411
ff6da295
CB
5412 netdev->priv.macvlan_attr.mode = -1;
5413
5414 return 0;
5415}
5416
c9f52382 5417static int clr_config_net_ipvlan_mode(const char *key,
5418 struct lxc_conf *lxc_conf, void *data)
5419{
5420 struct lxc_netdev *netdev = data;
5421
5422 if (!netdev)
6288bd57 5423 return ret_errno(EINVAL);
c9f52382 5424
5425 if (netdev->type != LXC_NET_IPVLAN)
5426 return 0;
5427
5428 netdev->priv.ipvlan_attr.mode = -1;
5429
5430 return 0;
5431}
5432
5433static int clr_config_net_ipvlan_isolation(const char *key,
299ddd16 5434 struct lxc_conf *lxc_conf, void *data)
c9f52382 5435{
5436 struct lxc_netdev *netdev = data;
5437
5438 if (!netdev)
ce79ece8 5439 return ret_errno(EINVAL);
c9f52382 5440
5441 if (netdev->type != LXC_NET_IPVLAN)
5442 return 0;
5443
5444 netdev->priv.ipvlan_attr.isolation = -1;
5445
5446 return 0;
5447}
5448
3f0ed090
TP
5449static int clr_config_net_veth_mode(const char *key,
5450 struct lxc_conf *lxc_conf, void *data)
5451{
5452 struct lxc_netdev *netdev = data;
5453
5454 if (!netdev)
7b39759d 5455 return ret_errno(EINVAL);
3f0ed090
TP
5456
5457 if (netdev->type != LXC_NET_VETH)
5458 return 0;
5459
5460 netdev->priv.veth_attr.mode = -1;
5461
5462 return 0;
5463}
5464
f9373e40
CB
5465static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
5466 void *data)
ff6da295 5467{
070a05af 5468 struct lxc_netdev *netdev = data;
ff6da295 5469
ff6da295 5470 if (!netdev)
a69a835e 5471 return ret_errno(EINVAL);
ff6da295 5472
299ddd16
CB
5473 if (netdev->type != LXC_NET_VETH)
5474 return 0;
5475
de4855a8 5476 netdev->priv.veth_attr.pair[0] = '\0';
ff6da295
CB
5477
5478 return 0;
5479}
5480
fdf76c6d
TP
5481static int clr_config_net_veth_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5482 void *data)
5483{
5484 struct lxc_netdev *netdev = data;
5485
5486 if (!netdev)
5487 return ret_errno(EINVAL);
5488
299ddd16
CB
5489 if (netdev->type != LXC_NET_VETH)
5490 return 0;
5491
fdf76c6d
TP
5492 netdev->priv.veth_attr.vlan_id = 0;
5493 netdev->priv.veth_attr.vlan_id_set = false;
5494
5495 return 0;
5496}
5497
b8e06d33
TP
5498static int clr_config_net_veth_vlan_tagged_id(const char *key,
5499 struct lxc_conf *lxc_conf, void *data)
5500{
5501 struct lxc_netdev *netdev = data;
5502 struct lxc_list *cur, *next;
5503
5504 if (!netdev)
5505 return ret_errno(EINVAL);
5506
299ddd16
CB
5507 if (netdev->type != LXC_NET_VETH)
5508 return 0;
5509
b8e06d33
TP
5510 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.vlan_tagged_ids, next) {
5511 lxc_list_del(cur);
5512 free(cur);
5513 }
5514
5515 return 0;
5516}
5517
5518
f9373e40
CB
5519static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
5520 void *data)
ff6da295 5521{
070a05af 5522 struct lxc_netdev *netdev = data;
ff6da295 5523
ff6da295 5524 if (!netdev)
9e1608ec 5525 return ret_errno(EINVAL);
ff6da295 5526
9e1608ec 5527 free_disarm(netdev->upscript);
ff6da295
CB
5528
5529 return 0;
5530}
5531
f9373e40
CB
5532static int clr_config_net_script_down(const char *key,
5533 struct lxc_conf *lxc_conf, void *data)
ff6da295 5534{
070a05af 5535 struct lxc_netdev *netdev = data;
ff6da295 5536
ff6da295 5537 if (!netdev)
25dea1a3 5538 return ret_errno(EINVAL);
ff6da295 5539
25dea1a3 5540 free_disarm(netdev->downscript);
ff6da295
CB
5541
5542 return 0;
5543}
5544
f9373e40
CB
5545static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
5546 void *data)
ff6da295 5547{
070a05af 5548 struct lxc_netdev *netdev = data;
ff6da295 5549
ff6da295 5550 if (!netdev)
afa97df5 5551 return ret_errno(EINVAL);
ff6da295 5552
afa97df5 5553 free_disarm(netdev->hwaddr);
ff6da295
CB
5554
5555 return 0;
5556}
5557
f9373e40
CB
5558static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
5559 void *data)
ff6da295 5560{
070a05af 5561 struct lxc_netdev *netdev = data;
ff6da295 5562
ff6da295 5563 if (!netdev)
18bab943 5564 return ret_errno(EINVAL);
ff6da295 5565
18bab943 5566 free_disarm(netdev->mtu);
ff6da295
CB
5567
5568 return 0;
5569}
5570
f9373e40
CB
5571static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5572 void *data)
ff6da295 5573{
070a05af 5574 struct lxc_netdev *netdev = data;
ff6da295 5575
ff6da295 5576 if (!netdev)
2c76b1ba 5577 return ret_errno(EINVAL);
ff6da295 5578
299ddd16
CB
5579 if (netdev->type != LXC_NET_VLAN)
5580 return 0;
5581
ff6da295
CB
5582 netdev->priv.vlan_attr.vid = 0;
5583
5584 return 0;
5585}
5586
f9373e40
CB
5587static int clr_config_net_ipv4_gateway(const char *key,
5588 struct lxc_conf *lxc_conf, void *data)
ff6da295 5589{
070a05af 5590 struct lxc_netdev *netdev = data;
ff6da295 5591
ff6da295 5592 if (!netdev)
69a91fd3 5593 return ret_errno(EINVAL);
ff6da295 5594
69a91fd3 5595 free_disarm(netdev->ipv4_gateway);
ff6da295
CB
5596
5597 return 0;
5598}
5599
9ff60df2
CB
5600static int clr_config_net_ipv4_address(const char *key,
5601 struct lxc_conf *lxc_conf, void *data)
ff6da295 5602{
070a05af 5603 struct lxc_netdev *netdev = data;
ff6da295
CB
5604 struct lxc_list *cur, *next;
5605
ecbb3790 5606 if (!netdev)
54148d69 5607 return ret_errno(EINVAL);
ff6da295
CB
5608
5609 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
5610 lxc_list_del(cur);
5611 free(cur->elem);
5612 free(cur);
5613 }
5614
5615 return 0;
5616}
5617
d4a7da46 5618static int clr_config_net_veth_ipv4_route(const char *key,
6340f28f 5619 struct lxc_conf *lxc_conf, void *data)
d4a7da46 5620{
5621 struct lxc_netdev *netdev = data;
5622 struct lxc_list *cur, *next;
5623
5624 if (!netdev)
6340f28f 5625 return ret_errno(EINVAL);
d4a7da46 5626
299ddd16
CB
5627 if (netdev->type != LXC_NET_VETH)
5628 return 0;
5629
d4a7da46 5630 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) {
5631 lxc_list_del(cur);
5632 free(cur->elem);
5633 free(cur);
5634 }
5635
5636 return 0;
5637}
5638
f9373e40
CB
5639static int clr_config_net_ipv6_gateway(const char *key,
5640 struct lxc_conf *lxc_conf, void *data)
ff6da295 5641{
070a05af 5642 struct lxc_netdev *netdev = data;
ff6da295 5643
ff6da295 5644 if (!netdev)
20959044 5645 return ret_errno(EINVAL);
ff6da295 5646
20959044 5647 free_disarm(netdev->ipv6_gateway);
ff6da295
CB
5648
5649 return 0;
5650}
5651
2e44ae28
CB
5652static int clr_config_net_ipv6_address(const char *key,
5653 struct lxc_conf *lxc_conf, void *data)
ff6da295 5654{
070a05af 5655 struct lxc_netdev *netdev = data;
ff6da295
CB
5656 struct lxc_list *cur, *next;
5657
ecbb3790 5658 if (!netdev)
2b592308 5659 return ret_errno(EINVAL);
ff6da295
CB
5660
5661 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
5662 lxc_list_del(cur);
5663 free(cur->elem);
5664 free(cur);
5665 }
5666
5667 return 0;
5668}
9d4bf22d 5669
d4a7da46 5670static int clr_config_net_veth_ipv6_route(const char *key,
b67488ec 5671 struct lxc_conf *lxc_conf, void *data)
d4a7da46 5672{
5673 struct lxc_netdev *netdev = data;
5674 struct lxc_list *cur, *next;
5675
5676 if (!netdev)
b67488ec 5677 return ret_errno(EINVAL);
d4a7da46 5678
299ddd16
CB
5679 if (netdev->type != LXC_NET_VETH)
5680 return 0;
5681
d4a7da46 5682 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) {
5683 lxc_list_del(cur);
5684 free(cur->elem);
5685 free(cur);
5686 }
5687
5688 return 0;
5689}
5690
7707b0e0
CB
5691static int get_config_jump_table_net(const char *key, char *retv, int inlen,
5692 struct lxc_conf *c, void *data)
9d4bf22d 5693{
7707b0e0
CB
5694 struct config_net_info info = {};
5695 int ret;
31ee747b 5696 const char *idxstring;
9d4bf22d 5697
0abcc213 5698 idxstring = key + STRLITERALLEN("lxc.net.");
31ee747b 5699 if (!isdigit(*idxstring))
05a9b967 5700 return ret_errno(EINVAL);
31ee747b 5701
7707b0e0
CB
5702 ret = get_network_config_ops(key, c, &info, false);
5703 if (ret)
5704 return ret;
47903908 5705
7707b0e0 5706 return info.ops->get(info.subkey, retv, inlen, c, info.netdev);
9d4bf22d
CB
5707}
5708
f9373e40
CB
5709static int get_config_net_type(const char *key, char *retv, int inlen,
5710 struct lxc_conf *c, void *data)
9d4bf22d 5711{
070a05af
CB
5712 int len;
5713 int fulllen = 0;
5714 struct lxc_netdev *netdev = data;
9d4bf22d 5715
299ddd16
CB
5716 if (!netdev)
5717 return ret_errno(EINVAL);
5718
9d4bf22d
CB
5719 if (!retv)
5720 inlen = 0;
5721 else
5722 memset(retv, 0, inlen);
5723
9d4bf22d
CB
5724 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
5725
5726 return fulllen;
5727}
5728
f9373e40
CB
5729static int get_config_net_flags(const char *key, char *retv, int inlen,
5730 struct lxc_conf *c, void *data)
9d4bf22d 5731{
070a05af
CB
5732 int len;
5733 int fulllen = 0;
5734 struct lxc_netdev *netdev = data;
9d4bf22d 5735
299ddd16
CB
5736 if (!netdev)
5737 return ret_errno(EINVAL);
5738
9d4bf22d
CB
5739 if (!retv)
5740 inlen = 0;
5741 else
5742 memset(retv, 0, inlen);
5743
9d4bf22d
CB
5744 if (netdev->flags & IFF_UP)
5745 strprint(retv, inlen, "up");
5746
5747 return fulllen;
5748}
5749
f9373e40
CB
5750static int get_config_net_link(const char *key, char *retv, int inlen,
5751 struct lxc_conf *c, void *data)
9d4bf22d 5752{
070a05af
CB
5753 int len;
5754 int fulllen = 0;
5755 struct lxc_netdev *netdev = data;
9d4bf22d 5756
299ddd16
CB
5757 if (!netdev)
5758 return ret_errno(EINVAL);
5759
9d4bf22d
CB
5760 if (!retv)
5761 inlen = 0;
5762 else
5763 memset(retv, 0, inlen);
5764
de4855a8 5765 if (netdev->link[0] != '\0')
9d4bf22d
CB
5766 strprint(retv, inlen, "%s", netdev->link);
5767
5768 return fulllen;
5769}
5770
6509154d 5771static int get_config_net_l2proxy(const char *key, char *retv, int inlen,
5cd78f55 5772 struct lxc_conf *c, void *data)
6509154d 5773{
5774 struct lxc_netdev *netdev = data;
299ddd16
CB
5775
5776 if (!netdev)
5777 return ret_errno(EINVAL);
5778
6509154d 5779 return lxc_get_conf_bool(c, retv, inlen, netdev->l2proxy);
5780}
5781
f9373e40
CB
5782static int get_config_net_name(const char *key, char *retv, int inlen,
5783 struct lxc_conf *c, void *data)
9d4bf22d 5784{
070a05af
CB
5785 int len;
5786 int fulllen = 0;
5787 struct lxc_netdev *netdev = data;
9d4bf22d 5788
299ddd16
CB
5789 if (!netdev)
5790 return ret_errno(EINVAL);
5791
9d4bf22d
CB
5792 if (!retv)
5793 inlen = 0;
5794 else
5795 memset(retv, 0, inlen);
5796
de4855a8 5797 if (netdev->name[0] != '\0')
9d4bf22d
CB
5798 strprint(retv, inlen, "%s", netdev->name);
5799
5800 return fulllen;
5801}
5802
f9373e40
CB
5803static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
5804 struct lxc_conf *c, void *data)
9d4bf22d 5805{
070a05af
CB
5806 int len;
5807 int fulllen = 0;
9d4bf22d 5808 const char *mode;
070a05af 5809 struct lxc_netdev *netdev = data;
9d4bf22d 5810
9d4bf22d 5811 if (!netdev)
3d72edbe 5812 return ret_errno(EINVAL);
9d4bf22d
CB
5813
5814 if (netdev->type != LXC_NET_MACVLAN)
299ddd16
CB
5815 return ret_errno(EINVAL);
5816
5817 if (!retv)
5818 inlen = 0;
5819 else
5820 memset(retv, 0, inlen);
9d4bf22d
CB
5821
5822 switch (netdev->priv.macvlan_attr.mode) {
5823 case MACVLAN_MODE_PRIVATE:
5824 mode = "private";
5825 break;
5826 case MACVLAN_MODE_VEPA:
5827 mode = "vepa";
5828 break;
5829 case MACVLAN_MODE_BRIDGE:
5830 mode = "bridge";
5831 break;
5832 case MACVLAN_MODE_PASSTHRU:
5833 mode = "passthru";
5834 break;
5835 default:
5836 mode = "(invalid)";
5837 break;
5838 }
5839
5840 strprint(retv, inlen, "%s", mode);
5841
5842 return fulllen;
5843}
5844
c9f52382 5845static int get_config_net_ipvlan_mode(const char *key, char *retv, int inlen,
5846 struct lxc_conf *c, void *data)
5847{
c9f52382 5848 int fulllen = 0;
c9f52382 5849 struct lxc_netdev *netdev = data;
dff2db42
CB
5850 int len;
5851 const char *mode;
c9f52382 5852
c9f52382 5853 if (!netdev)
dff2db42 5854 return ret_errno(EINVAL);
c9f52382 5855
5856 if (netdev->type != LXC_NET_IPVLAN)
299ddd16
CB
5857 return ret_errno(EINVAL);
5858
5859 if (!retv)
5860 inlen = 0;
5861 else
5862 memset(retv, 0, inlen);
c9f52382 5863
5864 switch (netdev->priv.ipvlan_attr.mode) {
5865 case IPVLAN_MODE_L3:
5866 mode = "l3";
5867 break;
5868 case IPVLAN_MODE_L3S:
5869 mode = "l3s";
5870 break;
5871 case IPVLAN_MODE_L2:
5872 mode = "l2";
5873 break;
5874 default:
5875 mode = "(invalid)";
5876 break;
5877 }
5878
5879 strprint(retv, inlen, "%s", mode);
5880
5881 return fulllen;
5882}
5883
5884static int get_config_net_ipvlan_isolation(const char *key, char *retv, int inlen,
5885 struct lxc_conf *c, void *data)
5886{
c9f52382 5887 int fulllen = 0;
c9f52382 5888 struct lxc_netdev *netdev = data;
56ce6747
CB
5889 int len;
5890 const char *mode;
c9f52382 5891
c9f52382 5892 if (!netdev)
56ce6747 5893 return ret_errno(EINVAL);
c9f52382 5894
5895 if (netdev->type != LXC_NET_IPVLAN)
299ddd16
CB
5896 return ret_errno(EINVAL);
5897
5898 if (!retv)
5899 inlen = 0;
5900 else
5901 memset(retv, 0, inlen);
c9f52382 5902
5903 switch (netdev->priv.ipvlan_attr.isolation) {
5904 case IPVLAN_ISOLATION_BRIDGE:
5905 mode = "bridge";
5906 break;
5907 case IPVLAN_ISOLATION_PRIVATE:
5908 mode = "private";
5909 break;
5910 case IPVLAN_ISOLATION_VEPA:
5911 mode = "vepa";
5912 break;
5913 default:
5914 mode = "(invalid)";
5915 break;
5916 }
5917
5918 strprint(retv, inlen, "%s", mode);
5919
5920 return fulllen;
5921}
5922
3f0ed090 5923static int get_config_net_veth_mode(const char *key, char *retv, int inlen,
299ddd16 5924 struct lxc_conf *c, void *data)
3f0ed090 5925{
3f0ed090 5926 int fulllen = 0;
3f0ed090 5927 struct lxc_netdev *netdev = data;
7d6ccf8c
CB
5928 int len;
5929 const char *mode;
3f0ed090 5930
3f0ed090 5931 if (!netdev)
7d6ccf8c 5932 return ret_errno(EINVAL);
3f0ed090
TP
5933
5934 if (netdev->type != LXC_NET_VETH)
299ddd16
CB
5935 return ret_errno(EINVAL);
5936
5937 if (!retv)
5938 inlen = 0;
5939 else
5940 memset(retv, 0, inlen);
3f0ed090
TP
5941
5942 switch (netdev->priv.veth_attr.mode) {
5943 case VETH_MODE_BRIDGE:
5944 mode = "bridge";
5945 break;
5946 case VETH_MODE_ROUTER:
5947 mode = "router";
5948 break;
5949 default:
5950 mode = "(invalid)";
5951 break;
5952 }
5953
5954 strprint(retv, inlen, "%s", mode);
5955
5956 return fulllen;
5957}
5958
f9373e40
CB
5959static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
5960 struct lxc_conf *c, void *data)
9d4bf22d 5961{
070a05af
CB
5962 int len;
5963 int fulllen = 0;
5964 struct lxc_netdev *netdev = data;
9d4bf22d 5965
9d4bf22d 5966 if (!netdev)
9e86b442 5967 return ret_errno(EINVAL);
9d4bf22d
CB
5968
5969 if (netdev->type != LXC_NET_VETH)
299ddd16
CB
5970 return ret_errno(EINVAL);
5971
5972 if (!retv)
5973 inlen = 0;
5974 else
5975 memset(retv, 0, inlen);
9d4bf22d
CB
5976
5977 strprint(retv, inlen, "%s",
de4855a8
CB
5978 netdev->priv.veth_attr.pair[0] != '\0'
5979 ? netdev->priv.veth_attr.pair
5980 : netdev->priv.veth_attr.veth1);
9d4bf22d
CB
5981
5982 return fulllen;
5983}
5984
fdf76c6d 5985static int get_config_net_veth_vlan_id(const char *key, char *retv, int inlen,
a90dc552 5986 struct lxc_conf *c, void *data)
fdf76c6d
TP
5987{
5988 int len;
5989 int fulllen = 0;
5990 struct lxc_netdev *netdev = data;
5991
5992 if (!netdev)
5993 return ret_errno(EINVAL);
5994
5995 if (netdev->type != LXC_NET_VETH)
299ddd16 5996 return ret_errno(EINVAL);
fdf76c6d
TP
5997
5998 if (!retv)
5999 inlen = 0;
6000 else
6001 memset(retv, 0, inlen);
6002
6003 strprint(retv, inlen, "%d", netdev->priv.veth_attr.vlan_id);
6004
6005 return fulllen;
6006}
6007
299ddd16
CB
6008static int get_config_net_veth_vlan_tagged_id(const char *key, char *retv,
6009 int inlen, struct lxc_conf *c,
6010 void *data)
b8e06d33
TP
6011{
6012 int len;
6013 size_t listlen;
6014 struct lxc_list *it;
6015 int fulllen = 0;
6016 struct lxc_netdev *netdev = data;
6017
6018 if (!netdev)
4e61b19d 6019 return ret_errno(EINVAL);
b8e06d33
TP
6020
6021 if (netdev->type != LXC_NET_VETH)
299ddd16 6022 return ret_errno(EINVAL);
b8e06d33
TP
6023
6024 if (!retv)
6025 inlen = 0;
6026 else
6027 memset(retv, 0, inlen);
6028
6029 listlen = lxc_list_len(&netdev->priv.veth_attr.vlan_tagged_ids);
6030
6031 lxc_list_for_each(it, &netdev->priv.veth_attr.vlan_tagged_ids) {
6032 unsigned short i = PTR_TO_USHORT(it->elem);
299ddd16 6033 strprint(retv, inlen, "%u%s", i, (listlen-- > 1) ? "\n" : "");
b8e06d33
TP
6034 }
6035
6036 return fulllen;
6037}
6038
f9373e40
CB
6039static int get_config_net_script_up(const char *key, char *retv, int inlen,
6040 struct lxc_conf *c, void *data)
9d4bf22d 6041{
070a05af
CB
6042 int len;
6043 int fulllen = 0;
6044 struct lxc_netdev *netdev = data;
9d4bf22d 6045
299ddd16
CB
6046 if (!netdev)
6047 return ret_errno(EINVAL);
6048
9d4bf22d
CB
6049 if (!retv)
6050 inlen = 0;
6051 else
6052 memset(retv, 0, inlen);
6053
9d4bf22d
CB
6054 if (netdev->upscript)
6055 strprint(retv, inlen, "%s", netdev->upscript);
6056
6057 return fulllen;
6058}
6059
f9373e40
CB
6060static int get_config_net_script_down(const char *key, char *retv, int inlen,
6061 struct lxc_conf *c, void *data)
9d4bf22d 6062{
070a05af
CB
6063 int len;
6064 int fulllen = 0;
6065 struct lxc_netdev *netdev = data;
9d4bf22d 6066
299ddd16
CB
6067 if (!netdev)
6068 return ret_errno(EINVAL);
6069
9d4bf22d
CB
6070 if (!retv)
6071 inlen = 0;
6072 else
6073 memset(retv, 0, inlen);
6074
9d4bf22d
CB
6075 if (netdev->downscript)
6076 strprint(retv, inlen, "%s", netdev->downscript);
6077
6078 return fulllen;
6079}
6080
f9373e40
CB
6081static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
6082 struct lxc_conf *c, void *data)
9d4bf22d 6083{
070a05af
CB
6084 int len;
6085 int fulllen = 0;
6086 struct lxc_netdev *netdev = data;
9d4bf22d 6087
299ddd16
CB
6088 if (!netdev)
6089 return ret_errno(EINVAL);
6090
9d4bf22d
CB
6091 if (!retv)
6092 inlen = 0;
6093 else
6094 memset(retv, 0, inlen);
6095
9d4bf22d
CB
6096 if (netdev->hwaddr)
6097 strprint(retv, inlen, "%s", netdev->hwaddr);
6098
6099 return fulllen;
6100}
6101
f9373e40
CB
6102static int get_config_net_mtu(const char *key, char *retv, int inlen,
6103 struct lxc_conf *c, void *data)
9d4bf22d 6104{
070a05af
CB
6105 int len;
6106 int fulllen = 0;
6107 struct lxc_netdev *netdev = data;
9d4bf22d 6108
299ddd16
CB
6109 if (!netdev)
6110 return ret_errno(EINVAL);
6111
9d4bf22d
CB
6112 if (!retv)
6113 inlen = 0;
6114 else
6115 memset(retv, 0, inlen);
6116
9d4bf22d
CB
6117 if (netdev->mtu)
6118 strprint(retv, inlen, "%s", netdev->mtu);
6119
6120 return fulllen;
6121}
6122
f9373e40
CB
6123static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
6124 struct lxc_conf *c, void *data)
9d4bf22d 6125{
070a05af
CB
6126 int len;
6127 int fulllen = 0;
6128 struct lxc_netdev *netdev = data;
9d4bf22d 6129
9d4bf22d 6130 if (!netdev)
5b2a5509 6131 return ret_errno(EINVAL);
9d4bf22d
CB
6132
6133 if (netdev->type != LXC_NET_VLAN)
299ddd16
CB
6134 return ret_errno(EINVAL);
6135
6136 if (!retv)
6137 inlen = 0;
6138 else
6139 memset(retv, 0, inlen);
9d4bf22d
CB
6140
6141 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
6142
6143 return fulllen;
6144}
6145
f9373e40
CB
6146static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
6147 struct lxc_conf *c, void *data)
9d4bf22d 6148{
070a05af 6149 int len;
9d4bf22d 6150 char buf[INET_ADDRSTRLEN];
070a05af
CB
6151 int fulllen = 0;
6152 struct lxc_netdev *netdev = data;
9d4bf22d 6153
299ddd16
CB
6154 if (!netdev)
6155 return ret_errno(EINVAL);
6156
9d4bf22d
CB
6157 if (!retv)
6158 inlen = 0;
6159 else
6160 memset(retv, 0, inlen);
6161
9d4bf22d
CB
6162 if (netdev->ipv4_gateway_auto) {
6163 strprint(retv, inlen, "auto");
a2f9a670 6164 } else if (netdev->ipv4_gateway_dev) {
6165 strprint(retv, inlen, "dev");
9d4bf22d 6166 } else if (netdev->ipv4_gateway) {
f203e57c
CB
6167 if (!inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf)))
6168 return -errno;
9d4bf22d
CB
6169 strprint(retv, inlen, "%s", buf);
6170 }
6171
6172 return fulllen;
6173}
6174
9ff60df2
CB
6175static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
6176 struct lxc_conf *c, void *data)
9d4bf22d 6177{
070a05af 6178 int len;
9d4bf22d
CB
6179 size_t listlen;
6180 char buf[INET_ADDRSTRLEN];
9d4bf22d 6181 struct lxc_list *it;
070a05af
CB
6182 int fulllen = 0;
6183 struct lxc_netdev *netdev = data;
9d4bf22d 6184
299ddd16
CB
6185 if (!netdev)
6186 return ret_errno(EINVAL);
6187
9d4bf22d
CB
6188 if (!retv)
6189 inlen = 0;
6190 else
6191 memset(retv, 0, inlen);
6192
9d4bf22d 6193 listlen = lxc_list_len(&netdev->ipv4);
47903908 6194
9d4bf22d
CB
6195 lxc_list_for_each(it, &netdev->ipv4) {
6196 struct lxc_inetdev *i = it->elem;
c3410544
CB
6197 if (!inet_ntop(AF_INET, &i->addr, buf, sizeof(buf)))
6198 return -errno;
6fc7d8b6 6199 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
6200 (listlen-- > 1) ? "\n" : "");
6201 }
6202
6203 return fulllen;
6204}
6205
d4a7da46 6206static int get_config_net_veth_ipv4_route(const char *key, char *retv, int inlen,
299ddd16 6207 struct lxc_conf *c, void *data)
d4a7da46 6208{
6209 int len;
6210 size_t listlen;
6211 char buf[INET_ADDRSTRLEN];
6212 struct lxc_list *it;
6213 int fulllen = 0;
6214 struct lxc_netdev *netdev = data;
6215
d4a7da46 6216 if (!netdev)
5d4611dc 6217 return ret_errno(EINVAL);
d4a7da46 6218
6219 if (netdev->type != LXC_NET_VETH)
299ddd16
CB
6220 return ret_errno(EINVAL);
6221
6222 if (!retv)
6223 inlen = 0;
6224 else
6225 memset(retv, 0, inlen);
d4a7da46 6226
6227 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv4_routes);
6228
6229 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv4_routes) {
6230 struct lxc_inetdev *i = it->elem;
5d4611dc
CB
6231 if (!inet_ntop(AF_INET, &i->addr, buf, sizeof(buf)))
6232 return -errno;
d4a7da46 6233 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6234 (listlen-- > 1) ? "\n" : "");
6235 }
6236
6237 return fulllen;
6238}
6239
f9373e40
CB
6240static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
6241 struct lxc_conf *c, void *data)
9d4bf22d 6242{
070a05af 6243 int len;
9d4bf22d 6244 char buf[INET6_ADDRSTRLEN];
070a05af
CB
6245 int fulllen = 0;
6246 struct lxc_netdev *netdev = data;
9d4bf22d 6247
299ddd16
CB
6248 if (!netdev)
6249 return ret_errno(EINVAL);
6250
9d4bf22d
CB
6251 if (!retv)
6252 inlen = 0;
6253 else
6254 memset(retv, 0, inlen);
6255
9d4bf22d
CB
6256 if (netdev->ipv6_gateway_auto) {
6257 strprint(retv, inlen, "auto");
a2f9a670 6258 } else if (netdev->ipv6_gateway_dev) {
6259 strprint(retv, inlen, "dev");
9d4bf22d 6260 } else if (netdev->ipv6_gateway) {
d8009ed2
CB
6261 if (!inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf)))
6262 return -errno;
9d4bf22d
CB
6263 strprint(retv, inlen, "%s", buf);
6264 }
6265
6266 return fulllen;
6267}
6268
2e44ae28
CB
6269static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
6270 struct lxc_conf *c, void *data)
9d4bf22d 6271{
070a05af 6272 int len;
9d4bf22d
CB
6273 size_t listlen;
6274 char buf[INET6_ADDRSTRLEN];
9d4bf22d 6275 struct lxc_list *it;
070a05af
CB
6276 int fulllen = 0;
6277 struct lxc_netdev *netdev = data;
9d4bf22d 6278
299ddd16
CB
6279 if (!netdev)
6280 return ret_errno(EINVAL);
6281
9d4bf22d
CB
6282 if (!retv)
6283 inlen = 0;
6284 else
6285 memset(retv, 0, inlen);
6286
9d4bf22d 6287 listlen = lxc_list_len(&netdev->ipv6);
47903908 6288
9d4bf22d
CB
6289 lxc_list_for_each(it, &netdev->ipv6) {
6290 struct lxc_inet6dev *i = it->elem;
396acb62
CB
6291 if (!inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf)))
6292 return -errno;
6fc7d8b6 6293 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
6294 (listlen-- > 1) ? "\n" : "");
6295 }
6296
6297 return fulllen;
6298}
cfc67626 6299
d4a7da46 6300static int get_config_net_veth_ipv6_route(const char *key, char *retv, int inlen,
98485389 6301 struct lxc_conf *c, void *data)
d4a7da46 6302{
6303 int len;
6304 size_t listlen;
6305 char buf[INET6_ADDRSTRLEN];
6306 struct lxc_list *it;
6307 int fulllen = 0;
6308 struct lxc_netdev *netdev = data;
6309
d4a7da46 6310 if (!netdev)
98485389 6311 return ret_errno(EINVAL);
d4a7da46 6312
6313 if (netdev->type != LXC_NET_VETH)
299ddd16
CB
6314 return ret_errno(EINVAL);
6315
6316 if (!retv)
6317 inlen = 0;
6318 else
6319 memset(retv, 0, inlen);
d4a7da46 6320
6321 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv6_routes);
6322
6323 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv6_routes) {
6324 struct lxc_inet6dev *i = it->elem;
98485389
CB
6325 if (!inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf)))
6326 return -errno;
d4a7da46 6327 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6328 (listlen-- > 1) ? "\n" : "");
6329 }
6330
6331 return fulllen;
6332}
6333
cfc67626
CB
6334int lxc_list_config_items(char *retv, int inlen)
6335{
6336 size_t i;
6337 int len;
6338 int fulllen = 0;
6339
6340 if (!retv)
6341 inlen = 0;
6342 else
6343 memset(retv, 0, inlen);
6344
0abcc213 6345 for (i = 0; i < ARRAY_SIZE(config_jump_table); i++) {
33eb2ec1 6346 char *s = config_jump_table[i].name;
300df83e 6347
cfc67626
CB
6348 if (s[strlen(s) - 1] == '.')
6349 continue;
300df83e 6350
cfc67626
CB
6351 strprint(retv, inlen, "%s\n", s);
6352 }
6353
6354 return fulllen;
6355}
a5448c15 6356
fe9b7349
CB
6357int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
6358 int inlen)
6359{
6360 int len;
6361 int fulllen = 0;
6362
6363 if (!retv)
6364 inlen = 0;
6365 else
6366 memset(retv, 0, inlen);
6367
d62177e9 6368 if (strequal(key, "lxc.apparmor")) {
fe9b7349 6369 strprint(retv, inlen, "allow_incomplete\n");
1800f924 6370 strprint(retv, inlen, "allow_nesting\n");
fe9b7349 6371 strprint(retv, inlen, "profile\n");
1800f924 6372 strprint(retv, inlen, "raw\n");
d62177e9 6373 } else if (strequal(key, "lxc.cgroup")) {
dae8c253 6374 strprint(retv, inlen, "dir\n");
d62177e9 6375 } else if (strequal(key, "lxc.selinux")) {
fe9b7349 6376 strprint(retv, inlen, "context\n");
4fef78bc 6377 strprint(retv, inlen, "context.keyring\n");
d62177e9 6378 } else if (strequal(key, "lxc.mount")) {
fe9b7349
CB
6379 strprint(retv, inlen, "auto\n");
6380 strprint(retv, inlen, "entry\n");
6381 strprint(retv, inlen, "fstab\n");
d62177e9 6382 } else if (strequal(key, "lxc.rootfs")) {
fe9b7349
CB
6383 strprint(retv, inlen, "mount\n");
6384 strprint(retv, inlen, "options\n");
6385 strprint(retv, inlen, "path\n");
d62177e9 6386 } else if (strequal(key, "lxc.uts")) {
fe9b7349 6387 strprint(retv, inlen, "name\n");
d62177e9 6388 } else if (strequal(key, "lxc.hook")) {
fe9b7349 6389 strprint(retv, inlen, "autodev\n");
63012bdd 6390 strprint(retv, inlen, "autodevtmpfssize\n");
fe9b7349
CB
6391 strprint(retv, inlen, "clone\n");
6392 strprint(retv, inlen, "destroy\n");
6393 strprint(retv, inlen, "mount\n");
6394 strprint(retv, inlen, "post-stop\n");
6395 strprint(retv, inlen, "pre-mount\n");
6396 strprint(retv, inlen, "pre-start\n");
08dd2805 6397 strprint(retv, inlen, "start-host\n");
fe9b7349
CB
6398 strprint(retv, inlen, "start\n");
6399 strprint(retv, inlen, "stop\n");
d62177e9 6400 } else if (strequal(key, "lxc.cap")) {
fe9b7349
CB
6401 strprint(retv, inlen, "drop\n");
6402 strprint(retv, inlen, "keep\n");
d62177e9 6403 } else if (strequal(key, "lxc.console")) {
fe9b7349
CB
6404 strprint(retv, inlen, "logfile\n");
6405 strprint(retv, inlen, "path\n");
d62177e9 6406 } else if (strequal(key, "lxc.seccomp")) {
fe9b7349 6407 strprint(retv, inlen, "profile\n");
d62177e9 6408 } else if (strequal(key, "lxc.signal")) {
fe9b7349
CB
6409 strprint(retv, inlen, "halt\n");
6410 strprint(retv, inlen, "reboot\n");
6411 strprint(retv, inlen, "stop\n");
d62177e9 6412 } else if (strequal(key, "lxc.start")) {
fe9b7349
CB
6413 strprint(retv, inlen, "auto\n");
6414 strprint(retv, inlen, "delay\n");
6415 strprint(retv, inlen, "order\n");
d62177e9 6416 } else if (strequal(key, "lxc.monitor")) {
fe9b7349 6417 strprint(retv, inlen, "unshare\n");
d62177e9 6418 } else if (strequal(key, "lxc.keyring")) {
8f818a84 6419 strprint(retv, inlen, "session\n");
fe9b7349 6420 } else {
efcba3c3 6421 fulllen = ret_errno(EINVAL);
fe9b7349
CB
6422 }
6423
6424 return fulllen;
6425}
6426
a5448c15
CB
6427int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
6428{
7707b0e0 6429 struct config_net_info info = {};
a5448c15 6430 struct lxc_netdev *netdev;
7707b0e0
CB
6431 int len, ret;
6432 const char *idxstring;
a5448c15 6433 int fulllen = 0;
a5448c15 6434
0abcc213 6435 idxstring = key + STRLITERALLEN("lxc.net.");
a5448c15 6436 if (!isdigit(*idxstring))
0abcc213 6437 return ret_errno(EINVAL);
a5448c15 6438
7707b0e0
CB
6439 ret = get_network_config_ops(key, c, &info, false);
6440 if (ret) {
6441 if (ret != -ENOENT)
6442 return ret_errno(EINVAL);
6443 }
6444 netdev = info.netdev;
a5448c15
CB
6445
6446 if (!retv)
6447 inlen = 0;
6448 else
6449 memset(retv, 0, inlen);
6450
6451 strprint(retv, inlen, "type\n");
6452 strprint(retv, inlen, "script.up\n");
6453 strprint(retv, inlen, "script.down\n");
47903908 6454
a5448c15
CB
6455 if (netdev->type != LXC_NET_EMPTY) {
6456 strprint(retv, inlen, "flags\n");
6457 strprint(retv, inlen, "link\n");
6458 strprint(retv, inlen, "name\n");
6459 strprint(retv, inlen, "hwaddr\n");
6460 strprint(retv, inlen, "mtu\n");
6461 strprint(retv, inlen, "ipv6.address\n");
6462 strprint(retv, inlen, "ipv6.gateway\n");
6463 strprint(retv, inlen, "ipv4.address\n");
6464 strprint(retv, inlen, "ipv4.gateway\n");
6465 }
6466
6467 switch (netdev->type) {
6468 case LXC_NET_VETH:
6469 strprint(retv, inlen, "veth.pair\n");
d4a7da46 6470 strprint(retv, inlen, "veth.ipv4.route\n");
6471 strprint(retv, inlen, "veth.ipv6.route\n");
fdf76c6d 6472 strprint(retv, inlen, "veth.vlan.id\n");
a5448c15
CB
6473 break;
6474 case LXC_NET_MACVLAN:
6475 strprint(retv, inlen, "macvlan.mode\n");
6476 break;
c9f52382 6477 case LXC_NET_IPVLAN:
6478 strprint(retv, inlen, "ipvlan.mode\n");
6479 strprint(retv, inlen, "ipvlan.isolation\n");
6480 break;
a5448c15
CB
6481 case LXC_NET_VLAN:
6482 strprint(retv, inlen, "vlan.id\n");
6483 break;
6484 case LXC_NET_PHYS:
6485 break;
6486 }
6487
6488 return fulllen;
6489}