]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/confile.c
confile: cleanup get_config_hooks()
[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
4114 errno = ENOSYS;
4115 return -1;
4116#endif
50d86993 4117}
861813e5 4118
84cf6d25
WB
4119static int get_config_seccomp_notify_cookie(const char *key, char *retv, int inlen,
4120 struct lxc_conf *c, void *data)
4121{
4122#ifdef HAVE_SECCOMP_NOTIFY
4123 return lxc_get_conf_str(retv, inlen, c->seccomp.notifier.cookie);
4124#else
21fce08c 4125 return ret_set_errno(-1, ENOSYS);
84cf6d25
WB
4126#endif
4127}
4128
86ce1da1
CB
4129static int get_config_seccomp_notify_proxy(const char *key, char *retv, int inlen,
4130 struct lxc_conf *c, void *data)
4131{
c3e3c21a 4132#ifdef HAVE_SECCOMP_NOTIFY
86ce1da1 4133 return lxc_get_conf_str(retv, inlen,
c3e3c21a
CB
4134 (c->seccomp.notifier.proxy_addr.sun_path[0]) == '/'
4135 ? &c->seccomp.notifier.proxy_addr.sun_path[0]
4136 : &c->seccomp.notifier.proxy_addr.sun_path[1]);
86ce1da1 4137#else
21fce08c 4138 return ret_set_errno(-1, ENOSYS);
86ce1da1
CB
4139#endif
4140}
4141
0b427da0
CB
4142static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
4143 struct lxc_conf *c, void *data)
75f55b1f 4144{
c3e3c21a 4145 return lxc_get_conf_str(retv, inlen, c->seccomp.seccomp);
75f55b1f 4146}
97f6dad0 4147
6bede808 4148static int get_config_autodev(const char *key, char *retv, int inlen,
cccd2219 4149 struct lxc_conf *c, void *data)
97f6dad0 4150{
6bede808 4151 return lxc_get_conf_int(c, retv, inlen, c->autodev);
97f6dad0 4152}
afee4324 4153
63012bdd
CK
4154static int get_config_autodev_tmpfs_size(const char *key, char *retv, int inlen,
4155 struct lxc_conf *c, void *data)
4156{
4157 return lxc_get_conf_int(c, retv, inlen, c->autodevtmpfssize);
4158}
4159
55c84efc 4160static int get_config_signal_halt(const char *key, char *retv, int inlen,
25a908b8 4161 struct lxc_conf *c, void *data)
afee4324 4162{
6bede808 4163 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
afee4324 4164}
3aa8f359 4165
55c84efc 4166static int get_config_signal_reboot(const char *key, char *retv, int inlen,
25a908b8 4167 struct lxc_conf *c, void *data)
3aa8f359 4168{
6bede808 4169 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
3aa8f359 4170}
2e16269f 4171
55c84efc 4172static int get_config_signal_stop(const char *key, char *retv, int inlen,
25a908b8 4173 struct lxc_conf *c, void *data)
2e16269f 4174{
6bede808 4175 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
2e16269f 4176}
54345299 4177
6bede808 4178static int get_config_start(const char *key, char *retv, int inlen,
cccd2219 4179 struct lxc_conf *c, void *data)
54345299 4180{
c6182222 4181 if (strcmp(key + 10, "auto") == 0)
6bede808 4182 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
c6182222 4183 else if (strcmp(key + 10, "delay") == 0)
6bede808 4184 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
c6182222 4185 else if (strcmp(key + 10, "order") == 0)
6bede808 4186 return lxc_get_conf_int(c, retv, inlen, c->start_order);
54345299
CB
4187
4188 return -1;
4189}
de9df15e 4190
46cc906d 4191static int get_config_log_syslog(const char *key, char *retv, int inlen,
25a908b8 4192 struct lxc_conf *c, void *data)
de9df15e 4193{
6bede808 4194 return lxc_get_conf_str(retv, inlen, c->syslog);
de9df15e 4195}
ac0f949c 4196
6bede808 4197static int get_config_monitor(const char *key, char *retv, int inlen,
cccd2219 4198 struct lxc_conf *c, void *data)
ac0f949c 4199{
6bede808 4200 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
ac0f949c 4201}
90ec7b6e 4202
258f8051
CB
4203static int get_config_monitor_signal_pdeath(const char *key, char *retv,
4204 int inlen, struct lxc_conf *c,
4205 void *data)
4206{
4207 return lxc_get_conf_int(c, retv, inlen, c->monitor_signal_pdeath);
4208}
4209
6bede808 4210static int get_config_group(const char *key, char *retv, int inlen,
cccd2219 4211 struct lxc_conf *c, void *data)
90ec7b6e
CB
4212{
4213 int len, fulllen = 0;
4214 struct lxc_list *it;
4215
4216 if (!retv)
4217 inlen = 0;
4218 else
4219 memset(retv, 0, inlen);
4220
6bede808 4221 lxc_list_for_each(it, &c->groups) {
90ec7b6e
CB
4222 strprint(retv, inlen, "%s\n", (char *)it->elem);
4223 }
25a908b8 4224
90ec7b6e
CB
4225 return fulllen;
4226}
aa0db7d3 4227
6bede808 4228static int get_config_environment(const char *key, char *retv, int inlen,
cccd2219 4229 struct lxc_conf *c, void *data)
aa0db7d3
CB
4230{
4231 int len, fulllen = 0;
4232 struct lxc_list *it;
4233
4234 if (!retv)
4235 inlen = 0;
4236 else
4237 memset(retv, 0, inlen);
4238
6bede808 4239 lxc_list_for_each(it, &c->environment) {
aa0db7d3
CB
4240 strprint(retv, inlen, "%s\n", (char *)it->elem);
4241 }
25a908b8 4242
aa0db7d3
CB
4243 return fulllen;
4244}
96dfcb7d 4245
5cda27c1
SH
4246static int get_config_execute_cmd(const char *key, char *retv, int inlen,
4247 struct lxc_conf *c, void *data)
4248{
4249 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
4250}
4251
6bede808 4252static int get_config_init_cmd(const char *key, char *retv, int inlen,
cccd2219 4253 struct lxc_conf *c, void *data)
96dfcb7d 4254{
6bede808 4255 return lxc_get_conf_str(retv, inlen, c->init_cmd);
96dfcb7d 4256}
1398e9b1 4257
3c491553
L
4258static int get_config_init_cwd(const char *key, char *retv, int inlen,
4259 struct lxc_conf *c, void *data)
4260{
4261 return lxc_get_conf_str(retv, inlen, c->init_cwd);
4262}
4263
6bede808 4264static int get_config_init_uid(const char *key, char *retv, int inlen,
cccd2219 4265 struct lxc_conf *c, void *data)
1398e9b1 4266{
6bede808 4267 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
1398e9b1 4268}
dfeb7e42 4269
6bede808 4270static int get_config_init_gid(const char *key, char *retv, int inlen,
cccd2219 4271 struct lxc_conf *c, void *data)
dfeb7e42 4272{
6bede808 4273 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
dfeb7e42 4274}
62048afe 4275
6bede808 4276static int get_config_ephemeral(const char *key, char *retv, int inlen,
cccd2219 4277 struct lxc_conf *c, void *data)
62048afe 4278{
6bede808 4279 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
62048afe 4280}
b09521ac 4281
6bede808 4282static int get_config_no_new_privs(const char *key, char *retv, int inlen,
cccd2219 4283 struct lxc_conf *c, void *data)
b09521ac 4284{
6bede808 4285 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
b09521ac 4286}
389f6466 4287
25a908b8 4288/* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
240d4b74 4289 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4290 * printed, in 'lxc.prlimit.resource = value' format.
389f6466 4291 */
240d4b74 4292static int get_config_prlimit(const char *key, char *retv, int inlen,
47903908 4293 struct lxc_conf *c, void *data)
389f6466
CB
4294{
4295 int fulllen = 0, len;
4296 bool get_all = false;
4297 struct lxc_list *it;
4298
4299 if (!retv)
4300 inlen = 0;
4301 else
4302 memset(retv, 0, inlen);
4303
240d4b74 4304 if (!strcmp(key, "lxc.prlimit"))
389f6466 4305 get_all = true;
240d4b74 4306 else if (strncmp(key, "lxc.prlimit.", 12) == 0)
4307 key += 12;
389f6466
CB
4308 else
4309 return -1;
4310
6bede808 4311 lxc_list_for_each(it, &c->limits) {
c77aee64
CB
4312 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4313 char buf[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
389f6466
CB
4314 int partlen;
4315 struct lxc_limit *lim = it->elem;
4316
4317 if (lim->limit.rlim_cur == RLIM_INFINITY) {
6333c915
CB
4318 memcpy(buf, "unlimited", STRLITERALLEN("unlimited") + 1);
4319 partlen = STRLITERALLEN("unlimited");
389f6466
CB
4320 } else {
4321 partlen = sprintf(buf, "%" PRIu64,
4322 (uint64_t)lim->limit.rlim_cur);
4323 }
47903908 4324
389f6466 4325 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
25a908b8 4326 if (lim->limit.rlim_max == RLIM_INFINITY)
389f6466 4327 memcpy(buf + partlen, ":unlimited",
6333c915 4328 STRLITERALLEN(":unlimited") + 1);
25a908b8 4329 else
389f6466
CB
4330 sprintf(buf + partlen, ":%" PRIu64,
4331 (uint64_t)lim->limit.rlim_max);
389f6466
CB
4332 }
4333
4334 if (get_all) {
240d4b74 4335 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
389f6466 4336 lim->resource, buf);
47903908 4337 } else if (strcmp(lim->resource, key) == 0) {
389f6466
CB
4338 strprint(retv, inlen, "%s", buf);
4339 }
4340 }
4341
4342 return fulllen;
4343}
e08cb901 4344
e409b214
CB
4345/* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4346 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4347 * entries will be printed, in 'lxc.sysctl.key = value' format.
7edd0540
L
4348 */
4349static int get_config_sysctl(const char *key, char *retv, int inlen,
47903908 4350 struct lxc_conf *c, void *data)
7edd0540
L
4351{
4352 int len;
4353 struct lxc_list *it;
4354 int fulllen = 0;
4355 bool get_all = false;
4356
4357 if (!retv)
4358 inlen = 0;
4359 else
4360 memset(retv, 0, inlen);
4361
4362 if (strcmp(key, "lxc.sysctl") == 0)
4363 get_all = true;
6333c915
CB
4364 else if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
4365 key += STRLITERALLEN("lxc.sysctl.");
7edd0540
L
4366 else
4367 return -1;
4368
4369 lxc_list_for_each(it, &c->sysctls) {
4370 struct lxc_sysctl *elem = it->elem;
4371 if (get_all) {
47903908 4372 strprint(retv, inlen, "lxc.sysctl.%s = %s\n", elem->key,
4373 elem->value);
7edd0540
L
4374 } else if (strcmp(elem->key, key) == 0) {
4375 strprint(retv, inlen, "%s", elem->value);
4376 }
4377 }
4378
4379 return fulllen;
4380}
4381
61d7a733 4382static int get_config_proc(const char *key, char *retv, int inlen,
47903908 4383 struct lxc_conf *c, void *data)
61d7a733
YT
4384{
4385 struct lxc_list *it;
4386 int len;
4387 int fulllen = 0;
4388 bool get_all = false;
4389
4390 if (!retv)
4391 inlen = 0;
4392 else
4393 memset(retv, 0, inlen);
4394
4395 if (strcmp(key, "lxc.proc") == 0)
4396 get_all = true;
6333c915
CB
4397 else if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
4398 key += STRLITERALLEN("lxc.proc.");
61d7a733
YT
4399 else
4400 return -1;
4401
4402 lxc_list_for_each(it, &c->procs) {
4403 struct lxc_proc *proc = it->elem;
4404
4405 if (get_all) {
4406 strprint(retv, inlen, "lxc.proc.%s = %s\n",
47903908 4407 proc->filename, proc->value);
61d7a733
YT
4408 } else if (strcmp(proc->filename, key) == 0) {
4409 strprint(retv, inlen, "%s", proc->value);
4410 }
4411 }
4412
4413 return fulllen;
4414}
4415
1d8d3676
CB
4416static int get_config_namespace_clone(const char *key, char *retv, int inlen,
4417 struct lxc_conf *c, void *data)
4418{
4419 int i, len;
4420 int fulllen = 0;
4421
4422 if (!retv)
4423 inlen = 0;
4424 else
4425 memset(retv, 0, inlen);
4426
4427 for (i = 0; i < LXC_NS_MAX; i++) {
4428 if (c->ns_clone & ns_info[i].clone_flag)
4429 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4430 }
4431
4432 return fulllen;
4433}
4434
abeb5bba
CB
4435static int get_config_namespace_keep(const char *key, char *retv, int inlen,
4436 struct lxc_conf *c, void *data)
4437{
4438 int i, len;
4439 int fulllen = 0;
4440
4441 if (!retv)
4442 inlen = 0;
4443 else
4444 memset(retv, 0, inlen);
4445
4446 for (i = 0; i < LXC_NS_MAX; i++) {
4447 if (c->ns_keep & ns_info[i].clone_flag)
4448 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4449 }
4450
4451 return fulllen;
4452}
4453
70fd7fc9
CB
4454static int get_config_time_offset_boot(const char *key, char *retv, int inlen, struct lxc_conf *c,
4455 void *data)
4456{
4457 int len;
4458 int fulllen = 0;
4459
4460 if (!retv)
4461 inlen = 0;
4462 else
4463 memset(retv, 0, inlen);
4464
4465 if (c->timens.s_boot) {
4466 strprint(retv, inlen, "%" PRId64 " s\n", c->timens.s_boot);
4467 } else {
4468 strprint(retv, inlen, "%" PRId64 " ns\n", c->timens.ns_boot);
4469 }
4470
4471 return fulllen;
4472}
4473
4474static int get_config_time_offset_monotonic(const char *key, char *retv, int inlen,
4475 struct lxc_conf *c, void *data)
4476{
4477 int len;
4478 int fulllen = 0;
4479
4480 if (!retv)
4481 inlen = 0;
4482 else
4483 memset(retv, 0, inlen);
4484
4485 if (c->timens.s_monotonic) {
4486 strprint(retv, inlen, "%" PRId64 "s\n", c->timens.s_monotonic);
4487 } else {
4488 strprint(retv, inlen, "%" PRId64 "ns\n", c->timens.ns_monotonic);
4489 }
4490
4491 return fulllen;
4492}
4493
b074bbf1
CB
4494static int get_config_namespace_share(const char *key, char *retv, int inlen,
4495 struct lxc_conf *c, void *data)
28d9e29e
CB
4496{
4497 int len, ns_idx;
4498 const char *namespace;
4499 int fulllen = 0;
4500
4501 if (!retv)
4502 inlen = 0;
4503 else
4504 memset(retv, 0, inlen);
4505
6333c915 4506 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
4507 ns_idx = lxc_namespace_2_ns_idx(namespace);
4508 if (ns_idx < 0)
4509 return ns_idx;
4510
b074bbf1 4511 strprint(retv, inlen, "%s", c->ns_share[ns_idx]);
28d9e29e
CB
4512
4513 return fulllen;
4514}
4515
e08cb901 4516/* Callbacks to clear config items. */
26471403
CB
4517static inline int clr_config_personality(const char *key, struct lxc_conf *c,
4518 void *data)
e08cb901
CB
4519{
4520 c->personality = -1;
4521 return 0;
4522}
03818ae3 4523
232763d6
CB
4524static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
4525 void *data)
03818ae3 4526{
e528c735 4527 c->pty_max = 0;
03818ae3
CB
4528 return 0;
4529}
e7a4b096 4530
fe1c5887
CB
4531static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
4532 void *data)
e7a4b096 4533{
885766f5 4534 c->ttys.tty = 0;
e7a4b096
CB
4535 return 0;
4536}
eaf8c0c7 4537
42e53c29 4538static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
26471403 4539 void *data)
eaf8c0c7 4540{
885766f5
CB
4541 free(c->ttys.dir);
4542 c->ttys.dir = NULL;
eaf8c0c7
CB
4543 return 0;
4544}
6bd86308 4545
953fe44f
CB
4546static inline int clr_config_apparmor_profile(const char *key,
4547 struct lxc_conf *c, void *data)
025718fb
CB
4548{
4549 free(c->lsm_aa_profile);
4550 c->lsm_aa_profile = NULL;
4551 return 0;
4552}
3061e04e 4553
953fe44f
CB
4554static inline int clr_config_apparmor_allow_incomplete(const char *key,
4555 struct lxc_conf *c,
4556 void *data)
3061e04e
CB
4557{
4558 c->lsm_aa_allow_incomplete = 0;
4559 return 0;
4560}
31fc3494 4561
1800f924
WB
4562static inline int clr_config_apparmor_allow_nesting(const char *key,
4563 struct lxc_conf *c,
4564 void *data)
4565{
4566 c->lsm_aa_allow_nesting = 0;
4567 return 0;
4568}
4569
4570static inline int clr_config_apparmor_raw(const char *key,
4571 struct lxc_conf *c,
4572 void *data)
4573{
4574 return lxc_clear_apparmor_raw(c);
4575}
4576
953fe44f
CB
4577static inline int clr_config_selinux_context(const char *key,
4578 struct lxc_conf *c, void *data)
31fc3494
CB
4579{
4580 free(c->lsm_se_context);
4581 c->lsm_se_context = NULL;
4582 return 0;
4583}
754d01ce 4584
4fef78bc
MB
4585static inline int clr_config_selinux_context_keyring(const char *key,
4586 struct lxc_conf *c, void *data)
4587{
4588 free(c->lsm_se_keyring_context);
4589 c->lsm_se_keyring_context = NULL;
4590 return 0;
4591}
4592
8f818a84
MB
4593static inline int clr_config_keyring_session(const char *key,
4594 struct lxc_conf *c, void *data)
4595{
4596 c->keyring_disable_session = false;
4597 return 0;
4598}
4599
43654d34
CB
4600static inline int clr_config_cgroup_controller(const char *key,
4601 struct lxc_conf *c, void *data)
754d01ce 4602{
54860ed0
CB
4603 return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC);
4604}
4605
4606static inline int clr_config_cgroup2_controller(const char *key,
4607 struct lxc_conf *c, void *data)
4608{
4609 return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC);
754d01ce 4610}
d3a178de 4611
43654d34
CB
4612static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
4613 void *data)
4614{
4615 if (lxc_conf->cgroup_meta.dir) {
4616 free(lxc_conf->cgroup_meta.dir);
4617 lxc_conf->cgroup_meta.dir = NULL;
4618 }
4619
4620 return 0;
4621}
4622
a900cbaf
WB
4623static int clr_config_cgroup_monitor_dir(const char *key,
4624 struct lxc_conf *lxc_conf,
4625 void *data)
4626{
4627 free_disarm(lxc_conf->cgroup_meta.monitor_dir);
4628 return 0;
4629}
4630
7696c1f9
RJ
4631static int clr_config_cgroup_monitor_pivot_dir(const char *key,
4632 struct lxc_conf *lxc_conf,
4633 void *data)
4634{
4635 free_disarm(lxc_conf->cgroup_meta.monitor_pivot_dir);
4636 return 0;
4637}
4638
a900cbaf
WB
4639static int clr_config_cgroup_container_dir(const char *key,
4640 struct lxc_conf *lxc_conf,
4641 void *data)
4642{
4643 free_disarm(lxc_conf->cgroup_meta.container_dir);
4644 return 0;
4645}
4646
4647static int clr_config_cgroup_container_inner_dir(const char *key,
4648 struct lxc_conf *lxc_conf,
4649 void *data)
4650{
4651 free_disarm(lxc_conf->cgroup_meta.namespace_dir);
4652 return 0;
4653}
4654
9caee129
CB
4655static inline int clr_config_cgroup_relative(const char *key,
4656 struct lxc_conf *lxc_conf,
4657 void *data)
76f0e2e7 4658{
9caee129 4659 lxc_conf->cgroup_meta.relative = false;
76f0e2e7
CB
4660 return 0;
4661}
4662
26471403
CB
4663static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
4664 void *data)
d3a178de
CB
4665{
4666 return lxc_clear_idmaps(c);
4667}
97d3338d 4668
46cc906d 4669static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
26471403 4670 void *data)
97d3338d 4671{
4b73005c 4672 c->loglevel = LXC_LOG_LEVEL_NOTSET;
97d3338d
CB
4673 return 0;
4674}
de46099c 4675
46cc906d 4676static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
26471403 4677 void *data)
de46099c
CB
4678{
4679 free(c->logfile);
4680 c->logfile = NULL;
4681 return 0;
4682}
b4fa13cd 4683
26471403
CB
4684static inline int clr_config_mount(const char *key, struct lxc_conf *c,
4685 void *data)
b4fa13cd
CB
4686{
4687 return lxc_clear_mount_entries(c);
4688}
4be81021 4689
26471403
CB
4690static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
4691 void *data)
4be81021
CB
4692{
4693 return lxc_clear_automounts(c);
4694}
350d4b15 4695
47148e96
CB
4696static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
4697 void *data)
350d4b15
CB
4698{
4699 free(c->fstab);
4700 c->fstab = NULL;
4701 return 0;
4702}
faca124d 4703
7a96a068
CB
4704static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
4705 void *data)
faca124d
CB
4706{
4707 free(c->rootfs.path);
4708 c->rootfs.path = NULL;
4709 return 0;
4710}
4711
6e54330c
CB
4712static inline int clr_config_rootfs_managed(const char *key, struct lxc_conf *c,
4713 void *data)
4714{
4715 c->rootfs.managed = true;
4716 return 0;
4717}
4718
26471403
CB
4719static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
4720 void *data)
fddefc2d
CB
4721{
4722 free(c->rootfs.mount);
4723 c->rootfs.mount = NULL;
4724 return 0;
4725}
7b1eb67d 4726
26471403
CB
4727static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
4728 void *data)
7b1eb67d
CB
4729{
4730 free(c->rootfs.options);
4731 c->rootfs.options = NULL;
3437f95c
CB
4732
4733 free(c->rootfs.data);
4734 c->rootfs.data = NULL;
4735
7b1eb67d
CB
4736 return 0;
4737}
02becb8d 4738
b67771bc 4739static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
26471403 4740 void *data)
d31d0103
CB
4741{
4742 free(c->utsname);
4743 c->utsname = NULL;
4744 return 0;
4745}
c9eeb90c 4746
26471403
CB
4747static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
4748 void *data)
c9eeb90c
CB
4749{
4750 return lxc_clear_hooks(c, key);
4751}
4752
44ae0fb6
CB
4753static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c,
4754 void *data)
4755{
4756 /* default to legacy hooks version */
4757 c->hooks_version = 0;
4758 return 0;
4759}
4760
f9373e40
CB
4761static inline int clr_config_net(const char *key, struct lxc_conf *c,
4762 void *data)
f4488271 4763{
c302b476
CB
4764 lxc_free_networks(&c->network);
4765
e5d2fd7c 4766 return 0;
f4488271
CB
4767}
4768
26471403
CB
4769static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
4770 void *data)
244cb55b
CB
4771{
4772 return lxc_clear_config_caps(c);
4773}
c74cc490 4774
26471403
CB
4775static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
4776 void *data)
c74cc490
CB
4777{
4778 return lxc_clear_config_keepcaps(c);
4779}
4e5b633f 4780
3aed4934
CB
4781static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
4782 void *data)
4e5b633f
CB
4783{
4784 free(c->console.path);
4785 c->console.path = NULL;
4786 return 0;
4787}
4788
7c2ec23a 4789static inline int clr_config_console_logfile(const char *key,
26471403 4790 struct lxc_conf *c, void *data)
7c2ec23a
CB
4791{
4792 free(c->console.log_path);
4793 c->console.log_path = NULL;
4794 return 0;
4795}
bbca37d8 4796
d91adfa6
CB
4797static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c,
4798 void *data)
4799{
4800 c->console.log_rotate = 0;
4801 return 0;
4802}
4803
28f3b1cd
CB
4804static inline int clr_config_console_buffer_size(const char *key,
4805 struct lxc_conf *c, void *data)
a04220de 4806{
28f3b1cd 4807 c->console.buffer_size = 0;
a04220de
CB
4808 return 0;
4809}
4810
861813e5
CB
4811static inline int clr_config_console_size(const char *key, struct lxc_conf *c,
4812 void *data)
4813{
4814 c->console.log_size = 0;
4815 return 0;
4816}
4817
50d86993
CB
4818static inline int clr_config_seccomp_allow_nesting(const char *key,
4819 struct lxc_conf *c, void *data)
4820{
c3e3c21a
CB
4821#ifdef HAVE_SECCOMP
4822 c->seccomp.allow_nesting = 0;
50d86993 4823 return 0;
c3e3c21a
CB
4824#else
4825 errno = ENOSYS;
4826 return -1;
4827#endif
50d86993
CB
4828}
4829
84cf6d25
WB
4830static inline int clr_config_seccomp_notify_cookie(const char *key,
4831 struct lxc_conf *c, void *data)
4832{
4833#ifdef HAVE_SECCOMP_NOTIFY
4834 free(c->seccomp.notifier.cookie);
4835 c->seccomp.notifier.cookie = NULL;
4836 return 0;
4837#else
21fce08c 4838 return ret_set_errno(-1, ENOSYS);
84cf6d25
WB
4839#endif
4840}
4841
86ce1da1
CB
4842static inline int clr_config_seccomp_notify_proxy(const char *key,
4843 struct lxc_conf *c, void *data)
4844{
c3e3c21a
CB
4845#ifdef HAVE_SECCOMP_NOTIFY
4846 memset(&c->seccomp.notifier.proxy_addr, 0,
4847 sizeof(c->seccomp.notifier.proxy_addr));
86ce1da1
CB
4848 return 0;
4849#else
21fce08c 4850 return ret_set_errno(-1, ENOSYS);
86ce1da1
CB
4851#endif
4852}
4853
0b427da0
CB
4854static inline int clr_config_seccomp_profile(const char *key,
4855 struct lxc_conf *c, void *data)
bbca37d8 4856{
c3e3c21a
CB
4857 free(c->seccomp.seccomp);
4858 c->seccomp.seccomp = NULL;
bbca37d8
CB
4859 return 0;
4860}
c721e86c 4861
26471403
CB
4862static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
4863 void *data)
c721e86c
CB
4864{
4865 c->autodev = 1;
4866 return 0;
4867}
87b288d1 4868
63012bdd
CK
4869static inline int clr_config_autodev_tmpfs_size(const char *key, struct lxc_conf *c,
4870 void *data)
4871{
4872 c->autodevtmpfssize = 500000;
4873 return 0;
4874}
4875
55c84efc 4876static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
26471403 4877 void *data)
87b288d1
CB
4878{
4879 c->haltsignal = 0;
4880 return 0;
4881}
cae63cfa 4882
55c84efc 4883static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
26471403 4884 void *data)
cae63cfa
CB
4885{
4886 c->rebootsignal = 0;
4887 return 0;
4888}
de45f3a8 4889
55c84efc 4890static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
26471403 4891 void *data)
de45f3a8
CB
4892{
4893 c->stopsignal = 0;
4894 return 0;
4895}
c6182222 4896
26471403
CB
4897static inline int clr_config_start(const char *key, struct lxc_conf *c,
4898 void *data)
c6182222
CB
4899{
4900 if (strcmp(key + 10, "auto") == 0)
4901 c->start_auto = 0;
4902 else if (strcmp(key + 10, "delay") == 0)
4903 c->start_delay = 0;
4904 else if (strcmp(key + 10, "order") == 0)
4905 c->start_order = 0;
4906
4907 return 0;
4908}
998ca94f 4909
46cc906d 4910static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
26471403 4911 void *data)
998ca94f
CB
4912{
4913 free(c->syslog);
4914 c->syslog = NULL;
4915 return 0;
4916}
adad12ca 4917
26471403
CB
4918static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
4919 void *data)
adad12ca
CB
4920{
4921 c->monitor_unshare = 0;
4922 return 0;
4923}
4850d223 4924
258f8051
CB
4925static inline int clr_config_monitor_signal_pdeath(const char *key,
4926 struct lxc_conf *c, void *data)
4927{
4928 c->monitor_signal_pdeath = 0;
4929 return 0;
4930}
4931
26471403
CB
4932static inline int clr_config_group(const char *key, struct lxc_conf *c,
4933 void *data)
4850d223
CB
4934{
4935 return lxc_clear_groups(c);
4936}
832fb63a 4937
26471403
CB
4938static inline int clr_config_environment(const char *key, struct lxc_conf *c,
4939 void *data)
832fb63a
CB
4940{
4941 return lxc_clear_environment(c);
4850d223 4942}
8e90af3e 4943
5cda27c1
SH
4944static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
4945 void *data)
4946{
4947 free(c->execute_cmd);
4948 c->execute_cmd = NULL;
4949 return 0;
4950}
4951
26471403
CB
4952static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
4953 void *data)
8e90af3e
CB
4954{
4955 free(c->init_cmd);
4956 c->init_cmd = NULL;
4957 return 0;
4958}
ec76dcfb 4959
3c491553
L
4960static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
4961 void *data)
4962{
4963 free(c->init_cwd);
4964 c->init_cwd = NULL;
4965 return 0;
4966}
4967
26471403
CB
4968static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
4969 void *data)
ec76dcfb
CB
4970{
4971 c->init_uid = 0;
4972 return 0;
4973}
1044b247 4974
26471403
CB
4975static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
4976 void *data)
1044b247
CB
4977{
4978 c->init_gid = 0;
4979 return 0;
4980}
59e370db 4981
26471403
CB
4982static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
4983 void *data)
59e370db
CB
4984{
4985 c->ephemeral = 0;
4986 return 0;
4987}
b98c5ab0 4988
26471403
CB
4989static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
4990 void *data)
b98c5ab0
CB
4991{
4992 c->no_new_privs = false;
4993 return 0;
4994}
715ccc96 4995
240d4b74 4996static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
26471403 4997 void *data)
715ccc96
CB
4998{
4999 return lxc_clear_limits(c, key);
5000}
fdf3c589 5001
7edd0540
L
5002static inline int clr_config_sysctl(const char *key, struct lxc_conf *c,
5003 void *data)
5004{
5005 return lxc_clear_sysctls(c, key);
5006}
5007
61d7a733
YT
5008static inline int clr_config_proc(const char *key, struct lxc_conf *c,
5009 void *data)
5010{
5011 return lxc_clear_procs(c, key);
5012}
5013
26471403
CB
5014static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
5015 void *data)
fdf3c589
CB
5016{
5017 lxc_clear_includes(c);
5018 return 0;
5019}
a3c8e600 5020
1d8d3676
CB
5021static int clr_config_namespace_clone(const char *key,
5022 struct lxc_conf *lxc_conf, void *data)
5023{
5024 lxc_conf->ns_clone = 0;
5025 return 0;
5026}
5027
abeb5bba
CB
5028static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf,
5029 void *data)
5030{
5031 lxc_conf->ns_keep = 0;
5032 return 0;
5033}
5034
70fd7fc9
CB
5035static int clr_config_time_offset_boot(const char *key, struct lxc_conf *lxc_conf, void *data)
5036{
5037 lxc_conf->timens.s_boot = 0;
5038 lxc_conf->timens.ns_boot = 0;
5039 return 0;
5040}
5041
5042static int clr_config_time_offset_monotonic(const char *key, struct lxc_conf *lxc_conf, void *data)
5043{
5044 lxc_conf->timens.s_monotonic = 0;
5045 lxc_conf->timens.ns_monotonic = 0;
5046 return 0;
5047}
5048
b074bbf1
CB
5049static int clr_config_namespace_share(const char *key,
5050 struct lxc_conf *lxc_conf, void *data)
28d9e29e
CB
5051{
5052 int ns_idx;
5053 const char *namespace;
5054
6333c915 5055 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
5056 ns_idx = lxc_namespace_2_ns_idx(namespace);
5057 if (ns_idx < 0)
5058 return ns_idx;
5059
b074bbf1
CB
5060 free(lxc_conf->ns_share[ns_idx]);
5061 lxc_conf->ns_share[ns_idx] = NULL;
28d9e29e
CB
5062
5063 return 0;
5064}
5065
a3c8e600 5066static int get_config_includefiles(const char *key, char *retv, int inlen,
cccd2219 5067 struct lxc_conf *c, void *data)
a3c8e600
CB
5068{
5069 return -ENOSYS;
5070}
40db5d2f 5071
31ee747b
CB
5072static struct lxc_config_t *get_network_config_ops(const char *key,
5073 struct lxc_conf *lxc_conf,
5074 ssize_t *idx,
5075 char **deindexed_key)
40db5d2f 5076{
31ee747b
CB
5077 int ret;
5078 unsigned int tmpidx;
5079 size_t numstrlen;
40db5d2f
CB
5080 char *copy, *idx_start, *idx_end;
5081 struct lxc_config_t *config = NULL;
5082
5083 /* check that this is a sensible network key */
31ee747b
CB
5084 if (strncmp("lxc.net.", key, 8)) {
5085 ERROR("Invalid network configuration key \"%s\"", key);
40db5d2f 5086 return NULL;
31ee747b 5087 }
40db5d2f
CB
5088
5089 copy = strdup(key);
31ee747b
CB
5090 if (!copy) {
5091 ERROR("Failed to duplicate string \"%s\"", key);
40db5d2f 5092 return NULL;
31ee747b 5093 }
40db5d2f 5094
f9373e40 5095 /* lxc.net.<n> */
31ee747b
CB
5096 if (!isdigit(*(key + 8))) {
5097 ERROR("Failed to detect digit in string \"%s\"", key + 8);
5098 goto on_error;
5099 }
40db5d2f 5100
31ee747b
CB
5101 /* beginning of index string */
5102 idx_start = (copy + 7);
5103 *idx_start = '\0';
40db5d2f 5104
31ee747b
CB
5105 /* end of index string */
5106 idx_end = strchr((copy + 8), '.');
7451daf8
CB
5107 if (idx_end)
5108 *idx_end = '\0';
31ee747b
CB
5109
5110 /* parse current index */
5111 ret = lxc_safe_uint((idx_start + 1), &tmpidx);
5112 if (ret < 0) {
6d1400b5 5113 errno = -ret;
ae52f396 5114 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
6d1400b5 5115 idx_start + 1);
31ee747b
CB
5116 *idx = ret;
5117 goto on_error;
5118 }
40db5d2f 5119
31ee747b
CB
5120 /* This, of course is utterly nonsensical on so many levels, but
5121 * better safe than sorry.
5122 * (Checking for INT_MAX here is intentional.)
5123 */
5124 if (tmpidx == INT_MAX) {
25a908b8 5125 SYSERROR("Number of configured networks would overflow the "
47903908 5126 "counter");
31ee747b 5127 goto on_error;
40db5d2f 5128 }
31ee747b
CB
5129 *idx = tmpidx;
5130
5131 numstrlen = strlen((idx_start + 1));
5132
5133 /* repair configuration key */
5134 *idx_start = '.';
31ee747b 5135
7451daf8
CB
5136 /* lxc.net.<idx>.<subkey> */
5137 if (idx_end) {
5138 *idx_end = '.';
24fa7df6
FA
5139 if (strlen(idx_end + 1) == 0) {
5140 ERROR("No subkey in network configuration key \"%s\"", key);
5141 goto on_error;
5142 }
40db5d2f 5143
7451daf8 5144 memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
52ab03ac 5145 copy[strlen(key) - (numstrlen + 1)] = '\0';
7451daf8 5146
300df83e 5147 config = lxc_get_config(copy);
7451daf8 5148 if (!config) {
25a908b8 5149 ERROR("Unknown network configuration key \"%s\"", key);
7451daf8
CB
5150 goto on_error;
5151 }
31ee747b
CB
5152 }
5153
7451daf8
CB
5154 if (deindexed_key)
5155 *deindexed_key = copy;
5156
31ee747b 5157 return config;
40db5d2f
CB
5158
5159on_error:
5160 free(copy);
31ee747b 5161 return NULL;
40db5d2f
CB
5162}
5163
25a908b8
CB
5164/* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5165 * found. So we make sure next comes an integer, find the right callback (by
5166 * rewriting the key), and call it.
40db5d2f 5167 */
f9373e40
CB
5168static int set_config_net_nic(const char *key, const char *value,
5169 struct lxc_conf *lxc_conf, void *data)
40db5d2f 5170{
31ee747b
CB
5171 int ret;
5172 const char *idxstring;
40db5d2f
CB
5173 struct lxc_config_t *config;
5174 struct lxc_netdev *netdev;
5175 ssize_t idx = -1;
31ee747b
CB
5176 char *deindexed_key = NULL;
5177
5178 idxstring = key + 8;
5179 if (!isdigit(*idxstring))
5180 return -1;
40db5d2f 5181
6bed0fb6 5182 if (lxc_config_value_empty(value))
f9373e40 5183 return clr_config_net_nic(key, lxc_conf, data);
6bed0fb6 5184
31ee747b 5185 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
40db5d2f
CB
5186 if (!config || idx < 0)
5187 return -1;
5188
0070b1c4 5189 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
31ee747b
CB
5190 if (!netdev) {
5191 free(deindexed_key);
40db5d2f 5192 return -1;
31ee747b 5193 }
40db5d2f 5194
31ee747b
CB
5195 ret = config->set(deindexed_key, value, lxc_conf, netdev);
5196 free(deindexed_key);
47903908 5197
31ee747b 5198 return ret;
40db5d2f 5199}
ff6da295 5200
f9373e40
CB
5201static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
5202 void *data)
ff6da295 5203{
31ee747b 5204 int ret;
519df1c1 5205 const char *idxstring;
ff6da295
CB
5206 struct lxc_config_t *config;
5207 struct lxc_netdev *netdev;
31ee747b
CB
5208 ssize_t idx = -1;
5209 char *deindexed_key = NULL;
519df1c1 5210
31ee747b
CB
5211 idxstring = key + 8;
5212 if (!isdigit(*idxstring))
519df1c1
CB
5213 return -1;
5214
519df1c1 5215 /* The left conjunct is pretty self-explanatory. The right conjunct
31ee747b 5216 * checks whether the two pointers are equal. If they are we know that
519df1c1
CB
5217 * this is not a key that is namespaced any further and so we are
5218 * supposed to clear the whole network.
5219 */
5220 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
5221 unsigned int rmnetdevidx;
5222
5223 if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0)
5224 return -1;
5225
5226 /* Remove network from network list. */
5227 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
5228 return 0;
5229 }
ff6da295 5230
31ee747b 5231 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
ff6da295
CB
5232 if (!config || idx < 0)
5233 return -1;
5234
0070b1c4 5235 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false);
31ee747b
CB
5236 if (!netdev) {
5237 free(deindexed_key);
ff6da295 5238 return -1;
31ee747b 5239 }
ff6da295 5240
31ee747b
CB
5241 ret = config->clr(deindexed_key, lxc_conf, netdev);
5242 free(deindexed_key);
47903908 5243
31ee747b 5244 return ret;
ff6da295
CB
5245}
5246
f9373e40
CB
5247static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
5248 void *data)
ff6da295 5249{
070a05af 5250 struct lxc_netdev *netdev = data;
ff6da295 5251
ff6da295
CB
5252 if (!netdev)
5253 return -1;
5254
5255 netdev->type = -1;
5256
5257 return 0;
5258}
5259
f9373e40
CB
5260static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
5261 void *data)
ff6da295 5262{
070a05af 5263 struct lxc_netdev *netdev = data;
ff6da295 5264
ff6da295
CB
5265 if (!netdev)
5266 return -1;
5267
de4855a8 5268 netdev->name[0] = '\0';
ff6da295
CB
5269
5270 return 0;
5271}
5272
f9373e40
CB
5273static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
5274 void *data)
ff6da295 5275{
070a05af 5276 struct lxc_netdev *netdev = data;
ff6da295 5277
ff6da295
CB
5278 if (!netdev)
5279 return -1;
5280
5281 netdev->flags = 0;
5282
5283 return 0;
5284}
5285
f9373e40
CB
5286static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
5287 void *data)
ff6da295 5288{
070a05af 5289 struct lxc_netdev *netdev = data;
ff6da295 5290
ff6da295
CB
5291 if (!netdev)
5292 return -1;
5293
de4855a8 5294 netdev->link[0] = '\0';
ff6da295
CB
5295
5296 return 0;
5297}
5298
6509154d 5299static int clr_config_net_l2proxy(const char *key, struct lxc_conf *lxc_conf,
5300 void *data)
5301{
5302 struct lxc_netdev *netdev = data;
5303
5304 if (!netdev)
21fce08c 5305 return ret_set_errno(-1, EINVAL);
6509154d 5306
5307 netdev->l2proxy = false;
5308
5309 return 0;
5310}
5311
f9373e40
CB
5312static int clr_config_net_macvlan_mode(const char *key,
5313 struct lxc_conf *lxc_conf, void *data)
ff6da295 5314{
070a05af 5315 struct lxc_netdev *netdev = data;
ff6da295 5316
ff6da295
CB
5317 if (!netdev)
5318 return -1;
5319
6bed0fb6
CB
5320 if (netdev->type != LXC_NET_MACVLAN)
5321 return 0;
5322
ff6da295
CB
5323 netdev->priv.macvlan_attr.mode = -1;
5324
5325 return 0;
5326}
5327
c9f52382 5328static int clr_config_net_ipvlan_mode(const char *key,
5329 struct lxc_conf *lxc_conf, void *data)
5330{
5331 struct lxc_netdev *netdev = data;
5332
5333 if (!netdev)
21fce08c 5334 return ret_set_errno(-1, EINVAL);
c9f52382 5335
5336 if (netdev->type != LXC_NET_IPVLAN)
5337 return 0;
5338
5339 netdev->priv.ipvlan_attr.mode = -1;
5340
5341 return 0;
5342}
5343
5344static int clr_config_net_ipvlan_isolation(const char *key,
5345 struct lxc_conf *lxc_conf, void *data)
5346{
5347 struct lxc_netdev *netdev = data;
5348
5349 if (!netdev)
21fce08c 5350 return ret_set_errno(-1, EINVAL);
c9f52382 5351
5352 if (netdev->type != LXC_NET_IPVLAN)
5353 return 0;
5354
5355 netdev->priv.ipvlan_attr.isolation = -1;
5356
5357 return 0;
5358}
5359
3f0ed090
TP
5360static int clr_config_net_veth_mode(const char *key,
5361 struct lxc_conf *lxc_conf, void *data)
5362{
5363 struct lxc_netdev *netdev = data;
5364
5365 if (!netdev)
21fce08c 5366 return ret_set_errno(-1, EINVAL);
3f0ed090
TP
5367
5368 if (netdev->type != LXC_NET_VETH)
5369 return 0;
5370
5371 netdev->priv.veth_attr.mode = -1;
5372
5373 return 0;
5374}
5375
f9373e40
CB
5376static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
5377 void *data)
ff6da295 5378{
070a05af 5379 struct lxc_netdev *netdev = data;
ff6da295 5380
ff6da295
CB
5381 if (!netdev)
5382 return -1;
5383
de4855a8 5384 netdev->priv.veth_attr.pair[0] = '\0';
ff6da295
CB
5385
5386 return 0;
5387}
5388
fdf76c6d
TP
5389static int clr_config_net_veth_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5390 void *data)
5391{
5392 struct lxc_netdev *netdev = data;
5393
5394 if (!netdev)
5395 return ret_errno(EINVAL);
5396
5397 netdev->priv.veth_attr.vlan_id = 0;
5398 netdev->priv.veth_attr.vlan_id_set = false;
5399
5400 return 0;
5401}
5402
b8e06d33
TP
5403static int clr_config_net_veth_vlan_tagged_id(const char *key,
5404 struct lxc_conf *lxc_conf, void *data)
5405{
5406 struct lxc_netdev *netdev = data;
5407 struct lxc_list *cur, *next;
5408
5409 if (!netdev)
5410 return ret_errno(EINVAL);
5411
5412 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.vlan_tagged_ids, next) {
5413 lxc_list_del(cur);
5414 free(cur);
5415 }
5416
5417 return 0;
5418}
5419
5420
f9373e40
CB
5421static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
5422 void *data)
ff6da295 5423{
070a05af 5424 struct lxc_netdev *netdev = data;
ff6da295 5425
ff6da295
CB
5426 if (!netdev)
5427 return -1;
5428
5429 free(netdev->upscript);
5430 netdev->upscript = NULL;
5431
5432 return 0;
5433}
5434
f9373e40
CB
5435static int clr_config_net_script_down(const char *key,
5436 struct lxc_conf *lxc_conf, void *data)
ff6da295 5437{
070a05af 5438 struct lxc_netdev *netdev = data;
ff6da295 5439
ff6da295
CB
5440 if (!netdev)
5441 return -1;
5442
5443 free(netdev->downscript);
5444 netdev->downscript = NULL;
5445
5446 return 0;
5447}
5448
f9373e40
CB
5449static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
5450 void *data)
ff6da295 5451{
070a05af 5452 struct lxc_netdev *netdev = data;
ff6da295 5453
ff6da295
CB
5454 if (!netdev)
5455 return -1;
5456
5457 free(netdev->hwaddr);
5458 netdev->hwaddr = NULL;
5459
5460 return 0;
5461}
5462
f9373e40
CB
5463static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
5464 void *data)
ff6da295 5465{
070a05af 5466 struct lxc_netdev *netdev = data;
ff6da295 5467
ff6da295
CB
5468 if (!netdev)
5469 return -1;
5470
5471 free(netdev->mtu);
5472 netdev->mtu = NULL;
5473
5474 return 0;
5475}
5476
f9373e40
CB
5477static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5478 void *data)
ff6da295 5479{
070a05af 5480 struct lxc_netdev *netdev = data;
ff6da295 5481
ff6da295
CB
5482 if (!netdev)
5483 return -1;
5484
5485 netdev->priv.vlan_attr.vid = 0;
5486
5487 return 0;
5488}
5489
f9373e40
CB
5490static int clr_config_net_ipv4_gateway(const char *key,
5491 struct lxc_conf *lxc_conf, void *data)
ff6da295 5492{
070a05af 5493 struct lxc_netdev *netdev = data;
ff6da295 5494
ff6da295
CB
5495 if (!netdev)
5496 return -1;
5497
5498 free(netdev->ipv4_gateway);
5499 netdev->ipv4_gateway = NULL;
5500
5501 return 0;
5502}
5503
9ff60df2
CB
5504static int clr_config_net_ipv4_address(const char *key,
5505 struct lxc_conf *lxc_conf, void *data)
ff6da295 5506{
070a05af 5507 struct lxc_netdev *netdev = data;
ff6da295
CB
5508 struct lxc_list *cur, *next;
5509
ecbb3790
CB
5510 if (!netdev)
5511 return -1;
ff6da295
CB
5512
5513 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
5514 lxc_list_del(cur);
5515 free(cur->elem);
5516 free(cur);
5517 }
5518
5519 return 0;
5520}
5521
d4a7da46 5522static int clr_config_net_veth_ipv4_route(const char *key,
5523 struct lxc_conf *lxc_conf, void *data)
5524{
5525 struct lxc_netdev *netdev = data;
5526 struct lxc_list *cur, *next;
5527
5528 if (!netdev)
5529 return -1;
5530
5531 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) {
5532 lxc_list_del(cur);
5533 free(cur->elem);
5534 free(cur);
5535 }
5536
5537 return 0;
5538}
5539
f9373e40
CB
5540static int clr_config_net_ipv6_gateway(const char *key,
5541 struct lxc_conf *lxc_conf, void *data)
ff6da295 5542{
070a05af 5543 struct lxc_netdev *netdev = data;
ff6da295 5544
ff6da295
CB
5545 if (!netdev)
5546 return -1;
5547
5548 free(netdev->ipv6_gateway);
5549 netdev->ipv6_gateway = NULL;
5550
5551 return 0;
5552}
5553
2e44ae28
CB
5554static int clr_config_net_ipv6_address(const char *key,
5555 struct lxc_conf *lxc_conf, void *data)
ff6da295 5556{
070a05af 5557 struct lxc_netdev *netdev = data;
ff6da295
CB
5558 struct lxc_list *cur, *next;
5559
ecbb3790
CB
5560 if (!netdev)
5561 return -1;
ff6da295
CB
5562
5563 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
5564 lxc_list_del(cur);
5565 free(cur->elem);
5566 free(cur);
5567 }
5568
5569 return 0;
5570}
9d4bf22d 5571
d4a7da46 5572static int clr_config_net_veth_ipv6_route(const char *key,
5573 struct lxc_conf *lxc_conf, void *data)
5574{
5575 struct lxc_netdev *netdev = data;
5576 struct lxc_list *cur, *next;
5577
5578 if (!netdev)
5579 return -1;
5580
5581 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) {
5582 lxc_list_del(cur);
5583 free(cur->elem);
5584 free(cur);
5585 }
5586
5587 return 0;
5588}
5589
f9373e40
CB
5590static int get_config_net_nic(const char *key, char *retv, int inlen,
5591 struct lxc_conf *c, void *data)
9d4bf22d 5592{
31ee747b
CB
5593 int ret;
5594 const char *idxstring;
9d4bf22d
CB
5595 struct lxc_config_t *config;
5596 struct lxc_netdev *netdev;
5597 ssize_t idx = -1;
31ee747b 5598 char *deindexed_key = NULL;
9d4bf22d 5599
31ee747b
CB
5600 idxstring = key + 8;
5601 if (!isdigit(*idxstring))
5602 return -1;
5603
5604 config = get_network_config_ops(key, c, &idx, &deindexed_key);
9d4bf22d
CB
5605 if (!config || idx < 0)
5606 return -1;
5607
0070b1c4 5608 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
31ee747b
CB
5609 if (!netdev) {
5610 free(deindexed_key);
9d4bf22d 5611 return -1;
31ee747b 5612 }
9d4bf22d 5613
31ee747b
CB
5614 ret = config->get(deindexed_key, retv, inlen, c, netdev);
5615 free(deindexed_key);
47903908 5616
31ee747b 5617 return ret;
9d4bf22d
CB
5618}
5619
f9373e40
CB
5620static int get_config_net_type(const char *key, char *retv, int inlen,
5621 struct lxc_conf *c, void *data)
9d4bf22d 5622{
070a05af
CB
5623 int len;
5624 int fulllen = 0;
5625 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5626
5627 if (!retv)
5628 inlen = 0;
5629 else
5630 memset(retv, 0, inlen);
5631
9d4bf22d
CB
5632 if (!netdev)
5633 return -1;
5634
5635 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
5636
5637 return fulllen;
5638}
5639
f9373e40
CB
5640static int get_config_net_flags(const char *key, char *retv, int inlen,
5641 struct lxc_conf *c, void *data)
9d4bf22d 5642{
070a05af
CB
5643 int len;
5644 int fulllen = 0;
5645 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5646
5647 if (!retv)
5648 inlen = 0;
5649 else
5650 memset(retv, 0, inlen);
5651
9d4bf22d
CB
5652 if (!netdev)
5653 return -1;
5654
5655 if (netdev->flags & IFF_UP)
5656 strprint(retv, inlen, "up");
5657
5658 return fulllen;
5659}
5660
f9373e40
CB
5661static int get_config_net_link(const char *key, char *retv, int inlen,
5662 struct lxc_conf *c, void *data)
9d4bf22d 5663{
070a05af
CB
5664 int len;
5665 int fulllen = 0;
5666 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5667
5668 if (!retv)
5669 inlen = 0;
5670 else
5671 memset(retv, 0, inlen);
5672
9d4bf22d
CB
5673 if (!netdev)
5674 return -1;
5675
de4855a8 5676 if (netdev->link[0] != '\0')
9d4bf22d
CB
5677 strprint(retv, inlen, "%s", netdev->link);
5678
5679 return fulllen;
5680}
5681
6509154d 5682static int get_config_net_l2proxy(const char *key, char *retv, int inlen,
5683 struct lxc_conf *c, void *data)
5684{
5685 struct lxc_netdev *netdev = data;
5686 return lxc_get_conf_bool(c, retv, inlen, netdev->l2proxy);
5687}
5688
f9373e40
CB
5689static int get_config_net_name(const char *key, char *retv, int inlen,
5690 struct lxc_conf *c, void *data)
9d4bf22d 5691{
070a05af
CB
5692 int len;
5693 int fulllen = 0;
5694 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5695
5696 if (!retv)
5697 inlen = 0;
5698 else
5699 memset(retv, 0, inlen);
5700
9d4bf22d
CB
5701 if (!netdev)
5702 return -1;
5703
de4855a8 5704 if (netdev->name[0] != '\0')
9d4bf22d
CB
5705 strprint(retv, inlen, "%s", netdev->name);
5706
5707 return fulllen;
5708}
5709
f9373e40
CB
5710static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
5711 struct lxc_conf *c, void *data)
9d4bf22d 5712{
070a05af
CB
5713 int len;
5714 int fulllen = 0;
9d4bf22d 5715 const char *mode;
070a05af 5716 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5717
5718 if (!retv)
5719 inlen = 0;
5720 else
5721 memset(retv, 0, inlen);
5722
9d4bf22d
CB
5723 if (!netdev)
5724 return -1;
5725
5726 if (netdev->type != LXC_NET_MACVLAN)
5727 return 0;
5728
5729 switch (netdev->priv.macvlan_attr.mode) {
5730 case MACVLAN_MODE_PRIVATE:
5731 mode = "private";
5732 break;
5733 case MACVLAN_MODE_VEPA:
5734 mode = "vepa";
5735 break;
5736 case MACVLAN_MODE_BRIDGE:
5737 mode = "bridge";
5738 break;
5739 case MACVLAN_MODE_PASSTHRU:
5740 mode = "passthru";
5741 break;
5742 default:
5743 mode = "(invalid)";
5744 break;
5745 }
5746
5747 strprint(retv, inlen, "%s", mode);
5748
5749 return fulllen;
5750}
5751
c9f52382 5752static int get_config_net_ipvlan_mode(const char *key, char *retv, int inlen,
5753 struct lxc_conf *c, void *data)
5754{
5755 int len;
5756 int fulllen = 0;
5757 const char *mode;
5758 struct lxc_netdev *netdev = data;
5759
5760 if (!retv)
5761 inlen = 0;
5762 else
5763 memset(retv, 0, inlen);
5764
5765 if (!netdev)
21fce08c 5766 return ret_set_errno(-1, EINVAL);
c9f52382 5767
5768 if (netdev->type != LXC_NET_IPVLAN)
5769 return 0;
5770
5771 switch (netdev->priv.ipvlan_attr.mode) {
5772 case IPVLAN_MODE_L3:
5773 mode = "l3";
5774 break;
5775 case IPVLAN_MODE_L3S:
5776 mode = "l3s";
5777 break;
5778 case IPVLAN_MODE_L2:
5779 mode = "l2";
5780 break;
5781 default:
5782 mode = "(invalid)";
5783 break;
5784 }
5785
5786 strprint(retv, inlen, "%s", mode);
5787
5788 return fulllen;
5789}
5790
5791static int get_config_net_ipvlan_isolation(const char *key, char *retv, int inlen,
5792 struct lxc_conf *c, void *data)
5793{
5794 int len;
5795 int fulllen = 0;
5796 const char *mode;
5797 struct lxc_netdev *netdev = data;
5798
5799 if (!retv)
5800 inlen = 0;
5801 else
5802 memset(retv, 0, inlen);
5803
5804 if (!netdev)
21fce08c 5805 return ret_set_errno(-1, EINVAL);
c9f52382 5806
5807 if (netdev->type != LXC_NET_IPVLAN)
5808 return 0;
5809
5810 switch (netdev->priv.ipvlan_attr.isolation) {
5811 case IPVLAN_ISOLATION_BRIDGE:
5812 mode = "bridge";
5813 break;
5814 case IPVLAN_ISOLATION_PRIVATE:
5815 mode = "private";
5816 break;
5817 case IPVLAN_ISOLATION_VEPA:
5818 mode = "vepa";
5819 break;
5820 default:
5821 mode = "(invalid)";
5822 break;
5823 }
5824
5825 strprint(retv, inlen, "%s", mode);
5826
5827 return fulllen;
5828}
5829
3f0ed090
TP
5830static int get_config_net_veth_mode(const char *key, char *retv, int inlen,
5831 struct lxc_conf *c, void *data)
5832{
5833 int len;
5834 int fulllen = 0;
5835 const char *mode;
5836 struct lxc_netdev *netdev = data;
5837
5838 if (!retv)
5839 inlen = 0;
5840 else
5841 memset(retv, 0, inlen);
5842
5843 if (!netdev)
21fce08c 5844 return ret_set_errno(-1, EINVAL);
3f0ed090
TP
5845
5846 if (netdev->type != LXC_NET_VETH)
5847 return 0;
5848
5849 switch (netdev->priv.veth_attr.mode) {
5850 case VETH_MODE_BRIDGE:
5851 mode = "bridge";
5852 break;
5853 case VETH_MODE_ROUTER:
5854 mode = "router";
5855 break;
5856 default:
5857 mode = "(invalid)";
5858 break;
5859 }
5860
5861 strprint(retv, inlen, "%s", mode);
5862
5863 return fulllen;
5864}
5865
f9373e40
CB
5866static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
5867 struct lxc_conf *c, void *data)
9d4bf22d 5868{
070a05af
CB
5869 int len;
5870 int fulllen = 0;
5871 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5872
5873 if (!retv)
5874 inlen = 0;
5875 else
5876 memset(retv, 0, inlen);
5877
9d4bf22d
CB
5878 if (!netdev)
5879 return -1;
5880
5881 if (netdev->type != LXC_NET_VETH)
5882 return 0;
5883
5884 strprint(retv, inlen, "%s",
de4855a8
CB
5885 netdev->priv.veth_attr.pair[0] != '\0'
5886 ? netdev->priv.veth_attr.pair
5887 : netdev->priv.veth_attr.veth1);
9d4bf22d
CB
5888
5889 return fulllen;
5890}
5891
fdf76c6d
TP
5892static int get_config_net_veth_vlan_id(const char *key, char *retv, int inlen,
5893 struct lxc_conf *c, void *data)
5894{
5895 int len;
5896 int fulllen = 0;
5897 struct lxc_netdev *netdev = data;
5898
5899 if (!netdev)
5900 return ret_errno(EINVAL);
5901
5902 if (netdev->type != LXC_NET_VETH)
5903 return 0;
5904
5905 if (!retv)
5906 inlen = 0;
5907 else
5908 memset(retv, 0, inlen);
5909
5910 strprint(retv, inlen, "%d", netdev->priv.veth_attr.vlan_id);
5911
5912 return fulllen;
5913}
5914
b8e06d33
TP
5915static int get_config_net_veth_vlan_tagged_id(const char *key, char *retv, int inlen,
5916 struct lxc_conf *c, void *data)
5917{
5918 int len;
5919 size_t listlen;
5920 struct lxc_list *it;
5921 int fulllen = 0;
5922 struct lxc_netdev *netdev = data;
5923
5924 if (!netdev)
4e61b19d 5925 return ret_errno(EINVAL);
b8e06d33
TP
5926
5927 if (netdev->type != LXC_NET_VETH)
5928 return 0;
5929
5930 if (!retv)
5931 inlen = 0;
5932 else
5933 memset(retv, 0, inlen);
5934
5935 listlen = lxc_list_len(&netdev->priv.veth_attr.vlan_tagged_ids);
5936
5937 lxc_list_for_each(it, &netdev->priv.veth_attr.vlan_tagged_ids) {
5938 unsigned short i = PTR_TO_USHORT(it->elem);
5939 strprint(retv, inlen, "%u%s", i,
5940 (listlen-- > 1) ? "\n" : "");
5941 }
5942
5943 return fulllen;
5944}
5945
f9373e40
CB
5946static int get_config_net_script_up(const char *key, char *retv, int inlen,
5947 struct lxc_conf *c, void *data)
9d4bf22d 5948{
070a05af
CB
5949 int len;
5950 int fulllen = 0;
5951 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5952
5953 if (!retv)
5954 inlen = 0;
5955 else
5956 memset(retv, 0, inlen);
5957
9d4bf22d
CB
5958 if (!netdev)
5959 return -1;
5960
5961 if (netdev->upscript)
5962 strprint(retv, inlen, "%s", netdev->upscript);
5963
5964 return fulllen;
5965}
5966
f9373e40
CB
5967static int get_config_net_script_down(const char *key, char *retv, int inlen,
5968 struct lxc_conf *c, void *data)
9d4bf22d 5969{
070a05af
CB
5970 int len;
5971 int fulllen = 0;
5972 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5973
5974 if (!retv)
5975 inlen = 0;
5976 else
5977 memset(retv, 0, inlen);
5978
9d4bf22d
CB
5979 if (!netdev)
5980 return -1;
5981
5982 if (netdev->downscript)
5983 strprint(retv, inlen, "%s", netdev->downscript);
5984
5985 return fulllen;
5986}
5987
f9373e40
CB
5988static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
5989 struct lxc_conf *c, void *data)
9d4bf22d 5990{
070a05af
CB
5991 int len;
5992 int fulllen = 0;
5993 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5994
5995 if (!retv)
5996 inlen = 0;
5997 else
5998 memset(retv, 0, inlen);
5999
9d4bf22d
CB
6000 if (!netdev)
6001 return -1;
6002
6003 if (netdev->hwaddr)
6004 strprint(retv, inlen, "%s", netdev->hwaddr);
6005
6006 return fulllen;
6007}
6008
f9373e40
CB
6009static int get_config_net_mtu(const char *key, char *retv, int inlen,
6010 struct lxc_conf *c, void *data)
9d4bf22d 6011{
070a05af
CB
6012 int len;
6013 int fulllen = 0;
6014 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6015
6016 if (!retv)
6017 inlen = 0;
6018 else
6019 memset(retv, 0, inlen);
6020
9d4bf22d
CB
6021 if (!netdev)
6022 return -1;
6023
6024 if (netdev->mtu)
6025 strprint(retv, inlen, "%s", netdev->mtu);
6026
6027 return fulllen;
6028}
6029
f9373e40
CB
6030static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
6031 struct lxc_conf *c, void *data)
9d4bf22d 6032{
070a05af
CB
6033 int len;
6034 int fulllen = 0;
6035 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6036
6037 if (!retv)
6038 inlen = 0;
6039 else
6040 memset(retv, 0, inlen);
6041
9d4bf22d
CB
6042 if (!netdev)
6043 return -1;
6044
6045 if (netdev->type != LXC_NET_VLAN)
6046 return 0;
6047
6048 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
6049
6050 return fulllen;
6051}
6052
f9373e40
CB
6053static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
6054 struct lxc_conf *c, void *data)
9d4bf22d 6055{
070a05af 6056 int len;
9d4bf22d 6057 char buf[INET_ADDRSTRLEN];
070a05af
CB
6058 int fulllen = 0;
6059 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6060
6061 if (!retv)
6062 inlen = 0;
6063 else
6064 memset(retv, 0, inlen);
6065
9d4bf22d
CB
6066 if (!netdev)
6067 return -1;
6068
6069 if (netdev->ipv4_gateway_auto) {
6070 strprint(retv, inlen, "auto");
a2f9a670 6071 } else if (netdev->ipv4_gateway_dev) {
6072 strprint(retv, inlen, "dev");
9d4bf22d
CB
6073 } else if (netdev->ipv4_gateway) {
6074 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
6075 strprint(retv, inlen, "%s", buf);
6076 }
6077
6078 return fulllen;
6079}
6080
9ff60df2
CB
6081static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
6082 struct lxc_conf *c, void *data)
9d4bf22d 6083{
070a05af 6084 int len;
9d4bf22d
CB
6085 size_t listlen;
6086 char buf[INET_ADDRSTRLEN];
9d4bf22d 6087 struct lxc_list *it;
070a05af
CB
6088 int fulllen = 0;
6089 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6090
6091 if (!retv)
6092 inlen = 0;
6093 else
6094 memset(retv, 0, inlen);
6095
9d4bf22d
CB
6096 if (!netdev)
6097 return -1;
6098
6099 listlen = lxc_list_len(&netdev->ipv4);
47903908 6100
9d4bf22d
CB
6101 lxc_list_for_each(it, &netdev->ipv4) {
6102 struct lxc_inetdev *i = it->elem;
6103 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6fc7d8b6 6104 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
6105 (listlen-- > 1) ? "\n" : "");
6106 }
6107
6108 return fulllen;
6109}
6110
d4a7da46 6111static int get_config_net_veth_ipv4_route(const char *key, char *retv, int inlen,
6112 struct lxc_conf *c, void *data)
6113{
6114 int len;
6115 size_t listlen;
6116 char buf[INET_ADDRSTRLEN];
6117 struct lxc_list *it;
6118 int fulllen = 0;
6119 struct lxc_netdev *netdev = data;
6120
6121 if (!retv)
6122 inlen = 0;
6123 else
6124 memset(retv, 0, inlen);
6125
6126 if (!netdev)
21fce08c 6127 return ret_set_errno(-1, EINVAL);
d4a7da46 6128
6129 if (netdev->type != LXC_NET_VETH)
6130 return 0;
6131
6132 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv4_routes);
6133
6134 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv4_routes) {
6135 struct lxc_inetdev *i = it->elem;
6136 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6137 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6138 (listlen-- > 1) ? "\n" : "");
6139 }
6140
6141 return fulllen;
6142}
6143
f9373e40
CB
6144static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
6145 struct lxc_conf *c, void *data)
9d4bf22d 6146{
070a05af 6147 int len;
9d4bf22d 6148 char buf[INET6_ADDRSTRLEN];
070a05af
CB
6149 int fulllen = 0;
6150 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6151
6152 if (!retv)
6153 inlen = 0;
6154 else
6155 memset(retv, 0, inlen);
6156
9d4bf22d
CB
6157 if (!netdev)
6158 return -1;
6159
6160 if (netdev->ipv6_gateway_auto) {
6161 strprint(retv, inlen, "auto");
a2f9a670 6162 } else if (netdev->ipv6_gateway_dev) {
6163 strprint(retv, inlen, "dev");
9d4bf22d
CB
6164 } else if (netdev->ipv6_gateway) {
6165 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
6166 strprint(retv, inlen, "%s", buf);
6167 }
6168
6169 return fulllen;
6170}
6171
2e44ae28
CB
6172static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
6173 struct lxc_conf *c, void *data)
9d4bf22d 6174{
070a05af 6175 int len;
9d4bf22d
CB
6176 size_t listlen;
6177 char buf[INET6_ADDRSTRLEN];
9d4bf22d 6178 struct lxc_list *it;
070a05af
CB
6179 int fulllen = 0;
6180 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6181
6182 if (!retv)
6183 inlen = 0;
6184 else
6185 memset(retv, 0, inlen);
6186
9d4bf22d
CB
6187 if (!netdev)
6188 return -1;
6189
6190 listlen = lxc_list_len(&netdev->ipv6);
47903908 6191
9d4bf22d
CB
6192 lxc_list_for_each(it, &netdev->ipv6) {
6193 struct lxc_inet6dev *i = it->elem;
6194 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6fc7d8b6 6195 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
6196 (listlen-- > 1) ? "\n" : "");
6197 }
6198
6199 return fulllen;
6200}
cfc67626 6201
d4a7da46 6202static int get_config_net_veth_ipv6_route(const char *key, char *retv, int inlen,
6203 struct lxc_conf *c, void *data)
6204{
6205 int len;
6206 size_t listlen;
6207 char buf[INET6_ADDRSTRLEN];
6208 struct lxc_list *it;
6209 int fulllen = 0;
6210 struct lxc_netdev *netdev = data;
6211
6212 if (!retv)
6213 inlen = 0;
6214 else
6215 memset(retv, 0, inlen);
6216
6217 if (!netdev)
21fce08c 6218 return ret_set_errno(-1, EINVAL);
d4a7da46 6219
6220 if (netdev->type != LXC_NET_VETH)
6221 return 0;
6222
6223 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv6_routes);
6224
6225 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv6_routes) {
6226 struct lxc_inet6dev *i = it->elem;
6227 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6228 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6229 (listlen-- > 1) ? "\n" : "");
6230 }
6231
6232 return fulllen;
6233}
6234
cfc67626
CB
6235int lxc_list_config_items(char *retv, int inlen)
6236{
6237 size_t i;
6238 int len;
6239 int fulllen = 0;
6240
6241 if (!retv)
6242 inlen = 0;
6243 else
6244 memset(retv, 0, inlen);
6245
33eb2ec1
CB
6246 for (i = 0; i < config_jump_table_size; i++) {
6247 char *s = config_jump_table[i].name;
300df83e 6248
cfc67626
CB
6249 if (s[strlen(s) - 1] == '.')
6250 continue;
300df83e 6251
cfc67626
CB
6252 strprint(retv, inlen, "%s\n", s);
6253 }
6254
6255 return fulllen;
6256}
a5448c15 6257
fe9b7349
CB
6258int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
6259 int inlen)
6260{
6261 int len;
6262 int fulllen = 0;
6263
6264 if (!retv)
6265 inlen = 0;
6266 else
6267 memset(retv, 0, inlen);
6268
6269 if (!strcmp(key, "lxc.apparmor")) {
6270 strprint(retv, inlen, "allow_incomplete\n");
1800f924 6271 strprint(retv, inlen, "allow_nesting\n");
fe9b7349 6272 strprint(retv, inlen, "profile\n");
1800f924 6273 strprint(retv, inlen, "raw\n");
dae8c253
CB
6274 } else if (!strcmp(key, "lxc.cgroup")) {
6275 strprint(retv, inlen, "dir\n");
fe9b7349
CB
6276 } else if (!strcmp(key, "lxc.selinux")) {
6277 strprint(retv, inlen, "context\n");
4fef78bc 6278 strprint(retv, inlen, "context.keyring\n");
fe9b7349
CB
6279 } else if (!strcmp(key, "lxc.mount")) {
6280 strprint(retv, inlen, "auto\n");
6281 strprint(retv, inlen, "entry\n");
6282 strprint(retv, inlen, "fstab\n");
6283 } else if (!strcmp(key, "lxc.rootfs")) {
6284 strprint(retv, inlen, "mount\n");
6285 strprint(retv, inlen, "options\n");
6286 strprint(retv, inlen, "path\n");
6287 } else if (!strcmp(key, "lxc.uts")) {
6288 strprint(retv, inlen, "name\n");
6289 } else if (!strcmp(key, "lxc.hook")) {
6290 strprint(retv, inlen, "autodev\n");
63012bdd 6291 strprint(retv, inlen, "autodevtmpfssize\n");
fe9b7349
CB
6292 strprint(retv, inlen, "clone\n");
6293 strprint(retv, inlen, "destroy\n");
6294 strprint(retv, inlen, "mount\n");
6295 strprint(retv, inlen, "post-stop\n");
6296 strprint(retv, inlen, "pre-mount\n");
6297 strprint(retv, inlen, "pre-start\n");
08dd2805 6298 strprint(retv, inlen, "start-host\n");
fe9b7349
CB
6299 strprint(retv, inlen, "start\n");
6300 strprint(retv, inlen, "stop\n");
6301 } else if (!strcmp(key, "lxc.cap")) {
6302 strprint(retv, inlen, "drop\n");
6303 strprint(retv, inlen, "keep\n");
6304 } else if (!strcmp(key, "lxc.console")) {
6305 strprint(retv, inlen, "logfile\n");
6306 strprint(retv, inlen, "path\n");
6307 } else if (!strcmp(key, "lxc.seccomp")) {
6308 strprint(retv, inlen, "profile\n");
6309 } else if (!strcmp(key, "lxc.signal")) {
6310 strprint(retv, inlen, "halt\n");
6311 strprint(retv, inlen, "reboot\n");
6312 strprint(retv, inlen, "stop\n");
6313 } else if (!strcmp(key, "lxc.start")) {
6314 strprint(retv, inlen, "auto\n");
6315 strprint(retv, inlen, "delay\n");
6316 strprint(retv, inlen, "order\n");
6317 } else if (!strcmp(key, "lxc.monitor")) {
6318 strprint(retv, inlen, "unshare\n");
8f818a84
MB
6319 } else if (!strcmp(key, "lxc.keyring")) {
6320 strprint(retv, inlen, "session\n");
fe9b7349
CB
6321 } else {
6322 fulllen = -1;
6323 }
6324
6325 return fulllen;
6326}
6327
a5448c15
CB
6328int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
6329{
6330 int len;
6331 const char *idxstring;
a5448c15
CB
6332 struct lxc_netdev *netdev;
6333 int fulllen = 0;
6334 ssize_t idx = -1;
a5448c15
CB
6335
6336 idxstring = key + 8;
6337 if (!isdigit(*idxstring))
6338 return -1;
6339
7451daf8
CB
6340 (void)get_network_config_ops(key, c, &idx, NULL);
6341 if (idx < 0)
a5448c15
CB
6342 return -1;
6343
6344 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
a5448c15
CB
6345 if (!netdev)
6346 return -1;
6347
6348 if (!retv)
6349 inlen = 0;
6350 else
6351 memset(retv, 0, inlen);
6352
6353 strprint(retv, inlen, "type\n");
6354 strprint(retv, inlen, "script.up\n");
6355 strprint(retv, inlen, "script.down\n");
47903908 6356
a5448c15
CB
6357 if (netdev->type != LXC_NET_EMPTY) {
6358 strprint(retv, inlen, "flags\n");
6359 strprint(retv, inlen, "link\n");
6360 strprint(retv, inlen, "name\n");
6361 strprint(retv, inlen, "hwaddr\n");
6362 strprint(retv, inlen, "mtu\n");
6363 strprint(retv, inlen, "ipv6.address\n");
6364 strprint(retv, inlen, "ipv6.gateway\n");
6365 strprint(retv, inlen, "ipv4.address\n");
6366 strprint(retv, inlen, "ipv4.gateway\n");
6367 }
6368
6369 switch (netdev->type) {
6370 case LXC_NET_VETH:
6371 strprint(retv, inlen, "veth.pair\n");
d4a7da46 6372 strprint(retv, inlen, "veth.ipv4.route\n");
6373 strprint(retv, inlen, "veth.ipv6.route\n");
fdf76c6d 6374 strprint(retv, inlen, "veth.vlan.id\n");
a5448c15
CB
6375 break;
6376 case LXC_NET_MACVLAN:
6377 strprint(retv, inlen, "macvlan.mode\n");
6378 break;
c9f52382 6379 case LXC_NET_IPVLAN:
6380 strprint(retv, inlen, "ipvlan.mode\n");
6381 strprint(retv, inlen, "ipvlan.isolation\n");
6382 break;
a5448c15
CB
6383 case LXC_NET_VLAN:
6384 strprint(retv, inlen, "vlan.id\n");
6385 break;
6386 case LXC_NET_PHYS:
6387 break;
6388 }
6389
6390 return fulllen;
6391}