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