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