]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/confile.c
confile: cleanup parse_line()
[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{
2874 char *dot, *key, *line, *linep, *value;
2875 int ret = 0;
2876 char *dup = buffer;
2877 struct new_config_item *new = NULL;
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))
d899f11b 2887 goto on_error;
2888
2889 ret = -1;
2890 dot = strchr(line, '=');
2891 if (!dot) {
2892 ERROR("Invalid configuration item: %s", line);
2893 goto on_error;
2894 }
2895
2896 *dot = '\0';
2897 value = dot + 1;
2898
2899 key = line;
2900 key[lxc_char_right_gc(key, strlen(key))] = '\0';
2901
2902 value += lxc_char_left_gc(value, strlen(value));
2903 value[lxc_char_right_gc(value, strlen(value))] = '\0';
2904
2905 if (*value == '\'' || *value == '\"') {
2906 size_t len;
2907
2908 len = strlen(value);
2909 if (len > 1 && value[len - 1] == *value) {
2910 value[len - 1] = '\0';
2911 value++;
2912 }
2913 }
2914
2915 ret = -1;
2916 new = malloc(sizeof(struct new_config_item));
2917 if (!new)
2918 goto on_error;
2919
2920 new->key = strdup(key);
2921 new->val = strdup(value);
2922 if (!new->val || !new->key)
2923 goto on_error;
2924
2925 ret = 0;
2926
2927on_error:
2928 free(linep);
2929
2930 if (ret < 0 && new) {
2931 free(new->key);
2932 free(new->val);
2933 free(new);
2934 new = NULL;
2935 }
2936
2937 return new;
2938}
2939
6b0d5538 2940int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
c2cc9f0a 2941{
6b0d5538
SH
2942 struct parse_line_conf c;
2943
2944 c.conf = conf;
2945 c.from_include = from_include;
f979ac15 2946
25a908b8 2947 /* Catch only the top level config file name in the structure. */
504a2217 2948 if (!conf->rcfile)
76d0127f 2949 conf->rcfile = strdup(file);
f979ac15 2950
9994d140 2951 return lxc_file_for_each_line_mmap(file, parse_line, &c);
c2cc9f0a 2952}
62e46035 2953
504a2217 2954int lxc_config_define_add(struct lxc_list *defines, char *arg)
62e46035
CLG
2955{
2956 struct lxc_list *dent;
2957
2958 dent = malloc(sizeof(struct lxc_list));
2959 if (!dent)
2960 return -1;
2961
d899f11b 2962 dent->elem = parse_new_conf_line(arg);
2963 if (!dent->elem) {
2964 free(dent);
2965 return -1;
2966 }
2967
62e46035 2968 lxc_list_add_tail(defines, dent);
47903908 2969
62e46035
CLG
2970 return 0;
2971}
2972
eb0c9382 2973bool lxc_config_define_load(struct lxc_list *defines, struct lxc_container *c)
62e46035 2974{
eb0c9382 2975 struct lxc_list *it;
2976 bool bret = true;
62e46035
CLG
2977
2978 lxc_list_for_each(it, defines) {
eb0c9382 2979 struct new_config_item *new_item = it->elem;
2980 bret = c->set_config_item(c, new_item->key, new_item->val);
2981 if (!bret)
62e46035
CLG
2982 break;
2983 }
2984
eb0c9382 2985 lxc_config_define_free(defines);
47903908 2986
eb0c9382 2987 return bret;
2988}
2989
2990void lxc_config_define_free(struct lxc_list *defines)
2991{
2992 struct lxc_list *it, *next;
2993
9ebb03ad 2994 lxc_list_for_each_safe(it, defines, next) {
eb0c9382 2995 struct new_config_item *new_item = it->elem;
2996 free(new_item->key);
2997 free(new_item->val);
62e46035
CLG
2998 lxc_list_del(it);
2999 free(it);
3000 }
62e46035 3001}
525f0002
CS
3002
3003signed long lxc_config_parse_arch(const char *arch)
3004{
504a2217
CB
3005#if HAVE_SYS_PERSONALITY_H
3006 size_t i;
525f0002
CS
3007 struct per_name {
3008 char *name;
3009 unsigned long per;
bb8d8207 3010 } pername[] = {
8f3e3c13
CB
3011 { "arm", PER_LINUX32 },
3012 { "armel", PER_LINUX32 },
3013 { "armhf", PER_LINUX32 },
3014 { "armv7l", PER_LINUX32 },
3015 { "athlon", PER_LINUX32 },
504a2217
CB
3016 { "i386", PER_LINUX32 },
3017 { "i486", PER_LINUX32 },
3018 { "i586", PER_LINUX32 },
3019 { "i686", PER_LINUX32 },
8f3e3c13 3020 { "linux32", PER_LINUX32 },
504a2217
CB
3021 { "mips", PER_LINUX32 },
3022 { "mipsel", PER_LINUX32 },
3023 { "ppc", PER_LINUX32 },
504a2217 3024 { "powerpc", PER_LINUX32 },
8f3e3c13 3025 { "x86", PER_LINUX32 },
504a2217 3026 { "amd64", PER_LINUX },
8f3e3c13
CB
3027 { "arm64", PER_LINUX },
3028 { "linux64", PER_LINUX },
504a2217
CB
3029 { "mips64", PER_LINUX },
3030 { "mips64el", PER_LINUX },
3031 { "ppc64", PER_LINUX },
504a2217 3032 { "ppc64el", PER_LINUX },
8f3e3c13 3033 { "ppc64le", PER_LINUX },
504a2217
CB
3034 { "powerpc64", PER_LINUX },
3035 { "s390x", PER_LINUX },
8f3e3c13 3036 { "x86_64", PER_LINUX },
525f0002
CS
3037 };
3038 size_t len = sizeof(pername) / sizeof(pername[0]);
3039
8f3e3c13 3040 for (i = 0; i < len; i++)
525f0002 3041 if (!strcmp(pername[i].name, arch))
504a2217 3042 return pername[i].per;
504a2217 3043#endif
525f0002
CS
3044
3045 return -1;
3046}
72d0e1cb 3047
eb0c9382 3048int lxc_fill_elevated_privileges(char *flaglist, int *flags)
3049{
62dd965e 3050 char *token;
eb0c9382 3051 int i, aflag;
3052 struct {
3053 const char *token;
3054 int flag;
3055 } all_privs[] = {
3056 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP },
3057 { "CAP", LXC_ATTACH_DROP_CAPABILITIES },
3058 { "LSM", LXC_ATTACH_LSM_EXEC },
3059 { NULL, 0 }
3060 };
3061
3062 if (!flaglist) {
3063 /* For the sake of backward compatibility, drop all privileges
3064 * if none is specified.
3065 */
3066 for (i = 0; all_privs[i].token; i++)
3067 *flags |= all_privs[i].flag;
3068
3069 return 0;
3070 }
3071
62dd965e 3072 lxc_iterate_parts(token, flaglist, "|") {
eb0c9382 3073 aflag = -1;
3074
3075 for (i = 0; all_privs[i].token; i++)
3076 if (!strcmp(all_privs[i].token, token))
3077 aflag = all_privs[i].flag;
3078
3079 if (aflag < 0)
3080 return -1;
3081
3082 *flags |= aflag;
eb0c9382 3083 }
3084
3085 return 0;
3086}
3087
504a2217 3088/* Write out a configuration file. */
0e1a60b0 3089int write_config(int fd, const struct lxc_conf *conf)
72d0e1cb 3090{
6b0d5538 3091 int ret;
0e1a60b0 3092 size_t len = conf->unexpanded_len;
72d0e1cb 3093
0e1a60b0
CB
3094 if (len == 0)
3095 return 0;
504a2217 3096
0e1a60b0
CB
3097 ret = lxc_write_nointr(fd, conf->unexpanded_config, len);
3098 if (ret < 0) {
25a908b8 3099 SYSERROR("Failed to write configuration file");
0e1a60b0
CB
3100 return -1;
3101 }
3102
3103 return 0;
6b0d5538 3104}
f979ac15 3105
504a2217
CB
3106bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key,
3107 const char *v)
6b0d5538 3108{
0cacddda 3109 __do_free char *tmp = NULL;
6b0d5538 3110 int ret;
151d2da2 3111 size_t len;
4184c3e1 3112
151d2da2 3113 len = strlen(key) + strlen(v) + 4;
f01d0358 3114 tmp = must_realloc(NULL, len);
151d2da2 3115
663e9916 3116 if (lxc_config_value_empty(v))
151d2da2
CB
3117 ret = snprintf(tmp, len, "%s =", key);
3118 else
3119 ret = snprintf(tmp, len, "%s = %s", key, v);
6b0d5538
SH
3120 if (ret < 0 || ret >= len)
3121 return false;
3122
3123 /* Save the line verbatim into unexpanded_conf */
3124 if (append_unexp_config_line(tmp, conf))
3125 return false;
3126
3127 return true;
3128}
3129
504a2217
CB
3130void clear_unexp_config_line(struct lxc_conf *conf, const char *key,
3131 bool rm_subkeys)
6b0d5538 3132{
504a2217
CB
3133 char *lend;
3134 char *lstart = conf->unexpanded_config;
6b0d5538
SH
3135
3136 if (!conf->unexpanded_config)
3137 return;
504a2217 3138
6b0d5538
SH
3139 while (*lstart) {
3140 lend = strchr(lstart, '\n');
3141 char v;
47903908 3142
6b0d5538
SH
3143 if (!lend)
3144 lend = lstart + strlen(lstart);
3145 else
3146 lend++;
47903908 3147
6b0d5538
SH
3148 if (strncmp(lstart, key, strlen(key)) != 0) {
3149 lstart = lend;
3150 continue;
5f62730e 3151 }
47903908 3152
6b0d5538
SH
3153 if (!rm_subkeys) {
3154 v = lstart[strlen(key)];
3155 if (!isspace(v) && v != '=') {
3156 lstart = lend;
3157 continue;
3158 }
5f62730e 3159 }
47903908 3160
6b0d5538 3161 conf->unexpanded_len -= (lend - lstart);
47903908 3162
6b0d5538
SH
3163 if (*lend == '\0') {
3164 *lstart = '\0';
3165 return;
fd986e08 3166 }
47903908 3167
504a2217 3168 memmove(lstart, lend, strlen(lend) + 1);
fd986e08 3169 }
6b0d5538
SH
3170}
3171
329b3625
CB
3172bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
3173 const char *newpath, const char *oldname,
3174 const char *newname, const char *ovldir)
3175{
f01d0358
CB
3176 __do_free char *newdir = NULL,
3177 *olddir = NULL;
329b3625 3178 int ret;
f01d0358 3179 char *lend, *p, *q;
504a2217 3180 size_t newdirlen, olddirlen;
329b3625 3181 char *lstart = conf->unexpanded_config;
504a2217 3182 const char *key = "lxc.mount.entry";
329b3625 3183
504a2217 3184 olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
f01d0358 3185 olddir = must_realloc(NULL, olddirlen + 1);
504a2217
CB
3186 ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath,
3187 oldname);
25a908b8 3188 if (ret < 0 || ret >= olddirlen + 1)
329b3625 3189 return false;
504a2217
CB
3190
3191 newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
f01d0358 3192 newdir = must_realloc(NULL, newdirlen + 1);
504a2217
CB
3193 ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath,
3194 newname);
25a908b8 3195 if (ret < 0 || ret >= newdirlen + 1)
329b3625 3196 return false;
504a2217 3197
329b3625
CB
3198 if (!conf->unexpanded_config)
3199 return true;
504a2217 3200
329b3625
CB
3201 while (*lstart) {
3202 lend = strchr(lstart, '\n');
3203 if (!lend)
3204 lend = lstart + strlen(lstart);
3205 else
3206 lend++;
504a2217 3207
329b3625 3208 if (strncmp(lstart, key, strlen(key)) != 0)
504a2217
CB
3209 goto next;
3210
329b3625
CB
3211 p = strchr(lstart + strlen(key), '=');
3212 if (!p)
504a2217 3213 goto next;
329b3625 3214 p++;
504a2217 3215
329b3625
CB
3216 while (isblank(*p))
3217 p++;
504a2217 3218
329b3625 3219 if (p >= lend)
504a2217
CB
3220 goto next;
3221
12e6ab5d
CB
3222 /* Whenever a lxc.mount.entry entry is found in a line we check
3223 * if the substring "overlay" is present before doing any
3224 * further work. We check for "overlay" because substrings need
3225 * to have at least one space before them in a valid overlay
504a2217
CB
3226 * lxc.mount.entry (/A B overlay). When the space before is
3227 * missing it is very likely that these substrings are part of a
3228 * path or something else. (Checking q >= lend ensures that we
3229 * only count matches in the current line.) */
12e6ab5d
CB
3230 q = strstr(p, " overlay");
3231 if (!q || q >= lend)
504a2217
CB
3232 goto next;
3233
329b3625 3234 if (!(q = strstr(p, olddir)) || (q >= lend))
504a2217 3235 goto next;
329b3625
CB
3236
3237 /* replace the olddir with newdir */
3238 if (olddirlen >= newdirlen) {
3239 size_t diff = olddirlen - newdirlen;
3240 memcpy(q, newdir, newdirlen);
47903908 3241
329b3625
CB
3242 if (olddirlen != newdirlen) {
3243 memmove(q + newdirlen, q + newdirlen + diff,
47903908 3244 strlen(q) - newdirlen - diff + 1);
329b3625
CB
3245 lend -= diff;
3246 conf->unexpanded_len -= diff;
3247 }
3248 } else {
3249 char *new;
3250 size_t diff = newdirlen - olddirlen;
3251 size_t oldlen = conf->unexpanded_len;
3252 size_t newlen = oldlen + diff;
3253 size_t poffset = q - conf->unexpanded_config;
504a2217 3254
329b3625 3255 new = realloc(conf->unexpanded_config, newlen + 1);
25a908b8 3256 if (!new)
329b3625 3257 return false;
25a908b8 3258
329b3625
CB
3259 conf->unexpanded_len = newlen;
3260 conf->unexpanded_alloced = newlen + 1;
3261 new[newlen - 1] = '\0';
3262 lend = new + (lend - conf->unexpanded_config);
47903908 3263
25a908b8 3264 /* Move over the remainder to make room for the newdir.
504a2217 3265 */
329b3625 3266 memmove(new + poffset + newdirlen,
47903908 3267 new + poffset + olddirlen,
3268 oldlen - poffset - olddirlen + 1);
329b3625 3269 conf->unexpanded_config = new;
47903908 3270
329b3625
CB
3271 memcpy(new + poffset, newdir, newdirlen);
3272 lend += diff;
3273 }
47903908 3274
504a2217
CB
3275 next:
3276 lstart = lend;
329b3625 3277 }
504a2217 3278
329b3625
CB
3279 return true;
3280}
3281
67702c21 3282bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
d546aa0e
CB
3283 const char *newpath, const char *oldname,
3284 const char *newname)
6b0d5538 3285{
f01d0358
CB
3286 __do_free char *newdir = NULL,
3287 *olddir = NULL;
67702c21 3288 int ret;
f01d0358 3289 char *lend, *p;
504a2217
CB
3290 char *lstart = conf->unexpanded_config;
3291 size_t newdirlen, olddirlen;
3292 const char *key = "lxc.hook";
67702c21 3293
504a2217 3294 olddirlen = strlen(oldpath) + strlen(oldname) + 1;
f01d0358 3295 olddir = must_realloc(NULL, olddirlen + 1);
d546aa0e 3296 ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
25a908b8 3297 if (ret < 0 || ret >= olddirlen + 1)
67702c21 3298 return false;
504a2217
CB
3299
3300 newdirlen = strlen(newpath) + strlen(newname) + 1;
f01d0358 3301 newdir = must_realloc(NULL, newdirlen + 1);
d546aa0e 3302 ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
25a908b8 3303 if (ret < 0 || ret >= newdirlen + 1)
67702c21 3304 return false;
25a908b8 3305
67702c21
SH
3306 if (!conf->unexpanded_config)
3307 return true;
25a908b8 3308
67702c21
SH
3309 while (*lstart) {
3310 lend = strchr(lstart, '\n');
3311 if (!lend)
3312 lend = lstart + strlen(lstart);
3313 else
3314 lend++;
504a2217 3315
d546aa0e 3316 if (strncmp(lstart, key, strlen(key)) != 0)
504a2217
CB
3317 goto next;
3318
d546aa0e
CB
3319 p = strchr(lstart + strlen(key), '=');
3320 if (!p)
504a2217 3321 goto next;
67702c21 3322 p++;
504a2217 3323
67702c21
SH
3324 while (isblank(*p))
3325 p++;
504a2217
CB
3326
3327 if (p >= lend)
3328 goto next;
3329
d546aa0e 3330 if (strncmp(p, olddir, strlen(olddir)) != 0)
504a2217
CB
3331 goto next;
3332
67702c21
SH
3333 /* replace the olddir with newdir */
3334 if (olddirlen >= newdirlen) {
3335 size_t diff = olddirlen - newdirlen;
3336 memcpy(p, newdir, newdirlen);
47903908 3337
67702c21 3338 if (olddirlen != newdirlen) {
d546aa0e 3339 memmove(p + newdirlen, p + newdirlen + diff,
47903908 3340 strlen(p) - newdirlen - diff + 1);
67702c21
SH
3341 lend -= diff;
3342 conf->unexpanded_len -= diff;
3343 }
67702c21
SH
3344 } else {
3345 char *new;
3346 size_t diff = newdirlen - olddirlen;
3347 size_t oldlen = conf->unexpanded_len;
3348 size_t newlen = oldlen + diff;
3349 size_t poffset = p - conf->unexpanded_config;
504a2217 3350
d546aa0e 3351 new = realloc(conf->unexpanded_config, newlen + 1);
25a908b8 3352 if (!new)
6b0d5538 3353 return false;
25a908b8 3354
67702c21 3355 conf->unexpanded_len = newlen;
d546aa0e
CB
3356 conf->unexpanded_alloced = newlen + 1;
3357 new[newlen - 1] = '\0';
67702c21 3358 lend = new + (lend - conf->unexpanded_config);
47903908 3359
25a908b8 3360 /* Move over the remainder to make room for the newdir.
504a2217 3361 */
d546aa0e 3362 memmove(new + poffset + newdirlen,
47903908 3363 new + poffset + olddirlen,
3364 oldlen - poffset - olddirlen + 1);
67702c21 3365 conf->unexpanded_config = new;
47903908 3366
d546aa0e
CB
3367 memcpy(new + poffset, newdir, newdirlen);
3368 lend += diff;
fd986e08 3369 }
47903908 3370
504a2217
CB
3371 next:
3372 lstart = lend;
fd986e08 3373 }
504a2217 3374
6b0d5538
SH
3375 return true;
3376}
3377
504a2217
CB
3378#define DO(cmd) \
3379 { \
3380 if (!(cmd)) { \
3381 ERROR("Error writing to new config"); \
3382 return false; \
3383 } \
3384 }
6b0d5538 3385
25a908b8
CB
3386/* This is called only from clone. We wish to update all hwaddrs in the
3387 * unexpanded config file. We can't/don't want to update any which come from
504a2217
CB
3388 * lxc.includes (there shouldn't be any).
3389 * We can't just walk the c->lxc-conf->network list because that includes netifs
3390 * from the include files. So we update the ones which we find in the unexp
3391 * config file, then find the original macaddr in the conf->network, and update
3392 * that to the same value.
67702c21
SH
3393 */
3394bool network_new_hwaddrs(struct lxc_conf *conf)
6b0d5538 3395{
504a2217 3396 char *lend, *p, *p2;
6b0d5538 3397 struct lxc_list *it;
504a2217 3398 char *lstart = conf->unexpanded_config;
6b0d5538 3399
67702c21
SH
3400 if (!conf->unexpanded_config)
3401 return true;
091045f8 3402
67702c21
SH
3403 while (*lstart) {
3404 char newhwaddr[18], oldhwaddr[17];
091045f8 3405
67702c21
SH
3406 lend = strchr(lstart, '\n');
3407 if (!lend)
3408 lend = lstart + strlen(lstart);
3409 else
3410 lend++;
091045f8 3411
3db41a6c 3412 if (!lxc_config_net_is_hwaddr(lstart)) {
67702c21
SH
3413 lstart = lend;
3414 continue;
72d0e1cb 3415 }
091045f8 3416
4a787c27 3417 p = strchr(lstart, '=');
67702c21
SH
3418 if (!p) {
3419 lstart = lend;
3420 continue;
72d0e1cb 3421 }
091045f8 3422
67702c21
SH
3423 p++;
3424 while (isblank(*p))
3425 p++;
3426 if (!*p)
3427 return true;
091045f8 3428
67702c21
SH
3429 p2 = p;
3430 while (*p2 && !isblank(*p2) && *p2 != '\n')
3431 p2++;
504a2217
CB
3432
3433 if ((p2 - p) != 17) {
67702c21
SH
3434 WARN("Bad hwaddr entry");
3435 lstart = lend;
3436 continue;
72d0e1cb 3437 }
091045f8 3438
67702c21 3439 memcpy(oldhwaddr, p, 17);
091045f8
CB
3440
3441 if (!new_hwaddr(newhwaddr))
3442 return false;
3443
67702c21 3444 memcpy(p, newhwaddr, 17);
25a908b8 3445 lxc_list_for_each(it, &conf->network) {
67702c21 3446 struct lxc_netdev *n = it->elem;
25a908b8 3447
67702c21
SH
3448 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
3449 memcpy(n->hwaddr, newhwaddr, 17);
72d0e1cb 3450 }
67702c21
SH
3451
3452 lstart = lend;
72d0e1cb 3453 }
091045f8 3454
6b0d5538 3455 return true;
72d0e1cb 3456}
8796becf 3457
713046e3 3458static int set_config_ephemeral(const char *key, const char *value,
c7e27aaf 3459 struct lxc_conf *lxc_conf, void *data)
8796becf 3460{
663e9916 3461 if (lxc_config_value_empty(value)) {
3c6cf53a 3462 lxc_conf->ephemeral = 0;
78304622 3463 return 0;
3c6cf53a 3464 }
78304622 3465
66ffdb1a
CB
3466 if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0)
3467 return -1;
8796becf 3468
25a908b8 3469 if (lxc_conf->ephemeral > 1)
8796becf 3470 return -1;
8796becf
CB
3471
3472 return 0;
3473}
3474
46cc906d 3475static int set_config_log_syslog(const char *key, const char *value,
c7e27aaf 3476 struct lxc_conf *lxc_conf, void *data)
64c57ea1 3477{
76d0127f 3478 int facility;
7ca56b84 3479
ee10a69c
CB
3480 if (lxc_conf->syslog) {
3481 free(lxc_conf->syslog);
3482 lxc_conf->syslog = NULL;
3483 }
3484
663e9916 3485 if (lxc_config_value_empty(value))
7ca56b84
CB
3486 return 0;
3487
76d0127f 3488 facility = lxc_syslog_priority_to_int(value);
25a908b8 3489 if (facility == -EINVAL)
76d0127f 3490 return -1;
64c57ea1 3491
76d0127f 3492 lxc_log_syslog(facility);
47903908 3493
713046e3 3494 return set_config_string_item(&lxc_conf->syslog, value);
64c57ea1 3495}
5a46f283 3496
713046e3 3497static int set_config_no_new_privs(const char *key, const char *value,
c7e27aaf 3498 struct lxc_conf *lxc_conf, void *data)
5a46f283 3499{
e8ec7c9e 3500 unsigned int v;
5a46f283 3501
663e9916 3502 if (lxc_config_value_empty(value)) {
cf3f8bf6 3503 lxc_conf->no_new_privs = false;
80926845 3504 return 0;
cf3f8bf6 3505 }
80926845 3506
e8ec7c9e
CB
3507 if (lxc_safe_uint(value, &v) < 0)
3508 return -1;
3509
25a908b8 3510 if (v > 1)
5a46f283 3511 return -1;
e8ec7c9e 3512
5a46f283
CB
3513 lxc_conf->no_new_privs = v ? true : false;
3514
3515 return 0;
3516}
7b992a3e
CB
3517
3518/* Callbacks to get configuration items. */
6bede808 3519static int get_config_personality(const char *key, char *retv, int inlen,
cccd2219 3520 struct lxc_conf *c, void *data)
7b992a3e
CB
3521{
3522 int fulllen = 0;
3523
3524 if (!retv)
3525 inlen = 0;
3526 else
3527 memset(retv, 0, inlen);
3528
3529#if HAVE_SYS_PERSONALITY_H
3530 int len = 0;
3531
6bede808 3532 switch (c->personality) {
7b992a3e
CB
3533 case PER_LINUX32:
3534 strprint(retv, inlen, "i686");
3535 break;
3536 case PER_LINUX:
3537 strprint(retv, inlen, "x86_64");
3538 break;
3539 default:
3540 break;
3541 }
3542#endif
3543
3544 return fulllen;
3545}
bdf91ab4 3546
232763d6
CB
3547static int get_config_pty_max(const char *key, char *retv, int inlen,
3548 struct lxc_conf *c, void *data)
bdf91ab4 3549{
e528c735 3550 return lxc_get_conf_size_t(c, retv, inlen, c->pty_max);
bdf91ab4 3551}
5485782f 3552
fe1c5887
CB
3553static int get_config_tty_max(const char *key, char *retv, int inlen,
3554 struct lxc_conf *c, void *data)
5485782f 3555{
885766f5 3556 return lxc_get_conf_size_t(c, retv, inlen, c->ttys.max);
5485782f 3557}
8015e018 3558
42e53c29 3559static int get_config_tty_dir(const char *key, char *retv, int inlen,
cccd2219 3560 struct lxc_conf *c, void *data)
8015e018 3561{
885766f5 3562 return lxc_get_conf_str(retv, inlen, c->ttys.dir);
8015e018 3563}
de1ede69 3564
953fe44f
CB
3565static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
3566 struct lxc_conf *c, void *data)
104c8e6c 3567{
6bede808 3568 return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile);
104c8e6c 3569}
d60d18c6 3570
953fe44f
CB
3571static int get_config_apparmor_allow_incomplete(const char *key, char *retv,
3572 int inlen, struct lxc_conf *c,
3573 void *data)
d60d18c6 3574{
6bede808
CB
3575 return lxc_get_conf_int(c, retv, inlen,
3576 c->lsm_aa_allow_incomplete);
d60d18c6 3577}
4203a0b5 3578
1800f924
WB
3579static int get_config_apparmor_allow_nesting(const char *key, char *retv,
3580 int inlen, struct lxc_conf *c,
3581 void *data)
3582{
3583 return lxc_get_conf_int(c, retv, inlen,
3584 c->lsm_aa_allow_nesting);
3585}
3586
3587static int get_config_apparmor_raw(const char *key, char *retv,
3588 int inlen, struct lxc_conf *c,
3589 void *data)
3590{
3591 int len;
3592 struct lxc_list *it;
3593 int fulllen = 0;
3594
3595 if (!retv)
3596 inlen = 0;
3597 else
3598 memset(retv, 0, inlen);
3599
3600 lxc_list_for_each(it, &c->lsm_aa_raw) {
3601 strprint(retv, inlen, "%s\n", (char *)it->elem);
3602 }
3603
3604 return fulllen;
3605}
3606
953fe44f
CB
3607static int get_config_selinux_context(const char *key, char *retv, int inlen,
3608 struct lxc_conf *c, void *data)
4203a0b5 3609{
6bede808 3610 return lxc_get_conf_str(retv, inlen, c->lsm_se_context);
4203a0b5 3611}
b863bf92 3612
4fef78bc
MB
3613static int get_config_selinux_context_keyring(const char *key, char *retv, int inlen,
3614 struct lxc_conf *c, void *data)
3615{
3616 return lxc_get_conf_str(retv, inlen, c->lsm_se_keyring_context);
3617}
3618
8f818a84
MB
3619static int get_config_keyring_session(const char *key, char *retv, int inlen,
3620 struct lxc_conf *c, void *data)
3621{
3622 return lxc_get_conf_bool(c, retv, inlen, c->keyring_disable_session);
3623}
3624
4fef78bc 3625
25a908b8
CB
3626/* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3627 * just the value(s) will be printed. Since there still could be more than one,
3628 * it is newline-separated.
8d47350b 3629 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
25a908b8
CB
3630 * have newlines?)
3631 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3632 * 'lxc.cgroup.subsystem.key = value' format.
b863bf92 3633 */
54860ed0
CB
3634static int __get_config_cgroup_controller(const char *key, char *retv,
3635 int inlen, struct lxc_conf *c,
3636 int version)
b863bf92 3637{
b863bf92 3638 int len;
54860ed0
CB
3639 size_t namespaced_token_len;
3640 char *global_token, *namespaced_token;
3641 struct lxc_list *it;
b863bf92
CB
3642 int fulllen = 0;
3643 bool get_all = false;
3644
3645 if (!retv)
3646 inlen = 0;
3647 else
3648 memset(retv, 0, inlen);
3649
54860ed0
CB
3650 if (version == CGROUP2_SUPER_MAGIC) {
3651 global_token = "lxc.cgroup2";
3652 namespaced_token = "lxc.cgroup2.";
6333c915 3653 namespaced_token_len = STRLITERALLEN("lxc.cgroup2.");
54860ed0
CB
3654 } else if (version == CGROUP_SUPER_MAGIC) {
3655 global_token = "lxc.cgroup";
3656 namespaced_token = "lxc.cgroup.";
6333c915 3657 namespaced_token_len = STRLITERALLEN("lxc.cgroup.");
54860ed0
CB
3658 } else {
3659 return -1;
3660 }
3661
3662 if (strcmp(key, global_token) == 0)
b863bf92 3663 get_all = true;
54860ed0
CB
3664 else if (strncmp(key, namespaced_token, namespaced_token_len) == 0)
3665 key += namespaced_token_len;
b863bf92
CB
3666 else
3667 return -1;
3668
6bede808 3669 lxc_list_for_each(it, &c->cgroup) {
b863bf92 3670 struct lxc_cgroup *cg = it->elem;
25a908b8 3671
b863bf92 3672 if (get_all) {
54860ed0
CB
3673 if (version != cg->version)
3674 continue;
3675
47903908 3676 strprint(retv, inlen, "%s.%s = %s\n", global_token,
3677 cg->subsystem, cg->value);
3678 } else if (strcmp(cg->subsystem, key) == 0) {
b863bf92
CB
3679 strprint(retv, inlen, "%s\n", cg->value);
3680 }
3681 }
3682
3683 return fulllen;
3684}
5014ff2e 3685
54860ed0
CB
3686static int get_config_cgroup_controller(const char *key, char *retv, int inlen,
3687 struct lxc_conf *c, void *data)
3688{
3689 return __get_config_cgroup_controller(key, retv, inlen, c,
3690 CGROUP_SUPER_MAGIC);
3691}
3692
3693static int get_config_cgroup2_controller(const char *key, char *retv, int inlen,
3694 struct lxc_conf *c, void *data)
3695{
3696 return __get_config_cgroup_controller(key, retv, inlen, c,
3697 CGROUP2_SUPER_MAGIC);
3698}
3699
43654d34
CB
3700static int get_config_cgroup_dir(const char *key, char *retv, int inlen,
3701 struct lxc_conf *lxc_conf, void *data)
3702{
3703 int len;
3704 int fulllen = 0;
3705
3706 if (!retv)
3707 inlen = 0;
3708 else
3709 memset(retv, 0, inlen);
3710
3711 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir);
3712
3713 return fulllen;
3714}
3715
a900cbaf
WB
3716static int get_config_cgroup_monitor_dir(const char *key, char *retv, int inlen,
3717 struct lxc_conf *lxc_conf, void *data)
3718{
3719 int len;
3720 int fulllen = 0;
3721
3722 if (!retv)
3723 inlen = 0;
3724 else
3725 memset(retv, 0, inlen);
3726
3727 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.monitor_dir);
3728
3729 return fulllen;
3730}
3731
7696c1f9
RJ
3732static int get_config_cgroup_monitor_pivot_dir(const char *key, char *retv, int inlen,
3733 struct lxc_conf *lxc_conf, void *data)
3734{
3735 int len;
3736 int fulllen = 0;
3737
3738 if (!retv)
3739 inlen = 0;
3740 else
3741 memset(retv, 0, inlen);
3742
3743 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.monitor_pivot_dir);
3744
3745 return fulllen;
3746}
3747
a900cbaf
WB
3748static int get_config_cgroup_container_dir(const char *key, char *retv,
3749 int inlen,
3750 struct lxc_conf *lxc_conf,
3751 void *data)
3752{
3753 int len;
3754 int fulllen = 0;
3755
3756 if (!retv)
3757 inlen = 0;
3758 else
3759 memset(retv, 0, inlen);
3760
3761 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.container_dir);
3762
3763 return fulllen;
3764}
3765
3766static int get_config_cgroup_container_inner_dir(const char *key, char *retv,
3767 int inlen,
3768 struct lxc_conf *lxc_conf,
3769 void *data)
3770{
3771 int len;
3772 int fulllen = 0;
3773
3774 if (!retv)
3775 inlen = 0;
3776 else
3777 memset(retv, 0, inlen);
3778
3779 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.namespace_dir);
3780
3781 return fulllen;
3782}
3783
9caee129
CB
3784static inline int get_config_cgroup_relative(const char *key, char *retv,
3785 int inlen, struct lxc_conf *lxc_conf,
3786 void *data)
76f0e2e7
CB
3787{
3788 return lxc_get_conf_int(lxc_conf, retv, inlen,
9caee129 3789 lxc_conf->cgroup_meta.relative);
76f0e2e7
CB
3790}
3791
6bede808 3792static int get_config_idmaps(const char *key, char *retv, int inlen,
cccd2219 3793 struct lxc_conf *c, void *data)
5014ff2e
CB
3794{
3795 struct lxc_list *it;
3796 int len, listlen, ret;
3797 int fulllen = 0;
3798/* "u 1000 1000000 65536"
3799 *
3800 * let's render this as
3801 *
3802 * sizeof(char)
3803 * +
3804 * sizeof(" ")
3805 * +
c77aee64 3806 * sizeof(uint32_t)
5014ff2e
CB
3807 * +
3808 * sizeof(" ")
3809 * +
c77aee64 3810 * sizeof(uint32_t)
5014ff2e
CB
3811 * +
3812 * sizeof(" ")
3813 * +
c77aee64 3814 * sizeof(uint32_t)
5014ff2e
CB
3815 * +
3816 * \0
3817 */
c77aee64 3818#define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
5014ff2e
CB
3819 char buf[__LXC_IDMAP_STR_BUF];
3820
3821 if (!retv)
3822 inlen = 0;
3823 else
3824 memset(retv, 0, inlen);
3825
6bede808 3826 listlen = lxc_list_len(&c->id_map);
c77aee64 3827 lxc_list_for_each(it, &c->id_map) {
5014ff2e
CB
3828 struct id_map *map = it->elem;
3829 ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu",
3830 (map->idtype == ID_TYPE_UID) ? 'u' : 'g',
3831 map->nsid, map->hostid, map->range);
3832 if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF)
3833 return -1;
3834
3835 strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : "");
3836 }
47903908 3837
5014ff2e
CB
3838 return fulllen;
3839}
b29b29be 3840
46cc906d 3841static int get_config_log_level(const char *key, char *retv, int inlen,
cccd2219 3842 struct lxc_conf *c, void *data)
b29b29be
CB
3843{
3844 const char *v;
6bede808 3845 v = lxc_log_priority_to_string(c->loglevel);
b29b29be
CB
3846 return lxc_get_conf_str(retv, inlen, v);
3847}
3d4630ab 3848
46cc906d 3849static int get_config_log_file(const char *key, char *retv, int inlen,
cccd2219 3850 struct lxc_conf *c, void *data)
3d4630ab 3851{
6bede808 3852 return lxc_get_conf_str(retv, inlen, c->logfile);
3d4630ab 3853}
0d601acb 3854
47148e96
CB
3855static int get_config_mount_fstab(const char *key, char *retv, int inlen,
3856 struct lxc_conf *c, void *data)
0d601acb 3857{
6bede808 3858 return lxc_get_conf_str(retv, inlen, c->fstab);
0d601acb 3859}
43fbf8d9 3860
6bede808 3861static int get_config_mount_auto(const char *key, char *retv, int inlen,
cccd2219 3862 struct lxc_conf *c, void *data)
43fbf8d9
CB
3863{
3864 int len, fulllen = 0;
3865 const char *sep = "";
3866
3867 if (!retv)
3868 inlen = 0;
3869 else
3870 memset(retv, 0, inlen);
3871
6bede808 3872 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
43fbf8d9
CB
3873 return 0;
3874
6bede808 3875 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
43fbf8d9
CB
3876 case LXC_AUTO_PROC_MIXED:
3877 strprint(retv, inlen, "%sproc:mixed", sep);
3878 sep = " ";
3879 break;
3880 case LXC_AUTO_PROC_RW:
3881 strprint(retv, inlen, "%sproc:rw", sep);
3882 sep = " ";
3883 break;
3884 default:
3885 break;
3886 }
3887
6bede808 3888 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
43fbf8d9
CB
3889 case LXC_AUTO_SYS_RO:
3890 strprint(retv, inlen, "%ssys:ro", sep);
3891 sep = " ";
3892 break;
3893 case LXC_AUTO_SYS_RW:
3894 strprint(retv, inlen, "%ssys:rw", sep);
3895 sep = " ";
3896 break;
3897 case LXC_AUTO_SYS_MIXED:
3898 strprint(retv, inlen, "%ssys:mixed", sep);
3899 sep = " ";
3900 break;
3901 default:
3902 break;
3903 }
3904
6bede808 3905 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
43fbf8d9
CB
3906 case LXC_AUTO_CGROUP_NOSPEC:
3907 strprint(retv, inlen, "%scgroup", sep);
43fbf8d9
CB
3908 break;
3909 case LXC_AUTO_CGROUP_MIXED:
3910 strprint(retv, inlen, "%scgroup:mixed", sep);
43fbf8d9
CB
3911 break;
3912 case LXC_AUTO_CGROUP_RO:
3913 strprint(retv, inlen, "%scgroup:ro", sep);
43fbf8d9
CB
3914 break;
3915 case LXC_AUTO_CGROUP_RW:
3916 strprint(retv, inlen, "%scgroup:rw", sep);
43fbf8d9
CB
3917 break;
3918 case LXC_AUTO_CGROUP_FULL_NOSPEC:
3919 strprint(retv, inlen, "%scgroup-full", sep);
43fbf8d9
CB
3920 break;
3921 case LXC_AUTO_CGROUP_FULL_MIXED:
3922 strprint(retv, inlen, "%scgroup-full:mixed", sep);
43fbf8d9
CB
3923 break;
3924 case LXC_AUTO_CGROUP_FULL_RO:
3925 strprint(retv, inlen, "%scgroup-full:ro", sep);
43fbf8d9
CB
3926 break;
3927 case LXC_AUTO_CGROUP_FULL_RW:
3928 strprint(retv, inlen, "%scgroup-full:rw", sep);
43fbf8d9
CB
3929 break;
3930 default:
3931 break;
3932 }
3933
3934 return fulllen;
3935}
cc921848 3936
6bede808 3937static int get_config_mount(const char *key, char *retv, int inlen,
cccd2219 3938 struct lxc_conf *c, void *data)
cc921848
CB
3939{
3940 int len, fulllen = 0;
3941 struct lxc_list *it;
3942
3943 if (!retv)
3944 inlen = 0;
3945 else
3946 memset(retv, 0, inlen);
3947
25a908b8 3948 lxc_list_for_each(it, &c->mount_list) {
cc921848
CB
3949 strprint(retv, inlen, "%s\n", (char *)it->elem);
3950 }
3951
3952 return fulllen;
3953}
819114b6 3954
7a96a068
CB
3955static int get_config_rootfs_path(const char *key, char *retv, int inlen,
3956 struct lxc_conf *c, void *data)
819114b6 3957{
6bede808 3958 return lxc_get_conf_str(retv, inlen, c->rootfs.path);
819114b6 3959}
3af60359 3960
6e54330c
CB
3961static int get_config_rootfs_managed(const char *key, char *retv, int inlen,
3962 struct lxc_conf *c, void *data)
3963{
3964 return lxc_get_conf_bool(c, retv, inlen, c->rootfs.managed);
3965}
3966
6bede808 3967static int get_config_rootfs_mount(const char *key, char *retv, int inlen,
cccd2219 3968 struct lxc_conf *c, void *data)
3af60359 3969{
6bede808 3970 return lxc_get_conf_str(retv, inlen, c->rootfs.mount);
3af60359 3971}
0e9db631 3972
6bede808 3973static int get_config_rootfs_options(const char *key, char *retv, int inlen,
cccd2219 3974 struct lxc_conf *c, void *data)
0e9db631 3975{
6bede808 3976 return lxc_get_conf_str(retv, inlen, c->rootfs.options);
0e9db631 3977}
8f183f38 3978
b67771bc 3979static int get_config_uts_name(const char *key, char *retv, int inlen,
cccd2219 3980 struct lxc_conf *c, void *data)
e274f8b0
CB
3981{
3982 return lxc_get_conf_str(
3983 retv, inlen,
6bede808 3984 c->utsname ? c->utsname->nodename : NULL);
e274f8b0 3985}
466c2e93 3986
6bede808 3987static int get_config_hooks(const char *key, char *retv, int inlen,
cccd2219 3988 struct lxc_conf *c, void *data)
466c2e93
CB
3989{
3990 char *subkey;
3991 int len, fulllen = 0, found = -1;
3992 struct lxc_list *it;
3993 int i;
3994
466c2e93 3995 subkey = strchr(key, '.');
466c2e93
CB
3996 if (!subkey)
3997 return -1;
47903908 3998
3999 subkey = strchr(subkey + 1, '.');
637d38f2
CB
4000 if (!subkey)
4001 return -1;
466c2e93 4002 subkey++;
637d38f2 4003 if (*subkey == '\0')
466c2e93 4004 return -1;
47903908 4005
466c2e93
CB
4006 for (i = 0; i < NUM_LXC_HOOKS; i++) {
4007 if (strcmp(lxchook_names[i], subkey) == 0) {
4008 found = i;
4009 break;
4010 }
4011 }
47903908 4012
466c2e93
CB
4013 if (found == -1)
4014 return -1;
4015
4016 if (!retv)
4017 inlen = 0;
4018 else
4019 memset(retv, 0, inlen);
4020
6bede808 4021 lxc_list_for_each(it, &c->hooks[found]) {
466c2e93
CB
4022 strprint(retv, inlen, "%s\n", (char *)it->elem);
4023 }
47903908 4024
466c2e93
CB
4025 return fulllen;
4026}
d2ceff53 4027
44ae0fb6
CB
4028static int get_config_hooks_version(const char *key, char *retv, int inlen,
4029 struct lxc_conf *c, void *data)
4030{
4031 return lxc_get_conf_int(c, retv, inlen, c->hooks_version);
4032}
4033
f9373e40
CB
4034static int get_config_net(const char *key, char *retv, int inlen,
4035 struct lxc_conf *c, void *data)
d2ceff53
CB
4036{
4037 int len, fulllen = 0;
4038 struct lxc_list *it;
4039
4040 if (!retv)
4041 inlen = 0;
4042 else
4043 memset(retv, 0, inlen);
4044
6bede808 4045 lxc_list_for_each(it, &c->network) {
d2ceff53
CB
4046 struct lxc_netdev *n = it->elem;
4047 const char *t = lxc_net_type_to_str(n->type);
4048 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
4049 }
4050
4051 return fulllen;
4052}
bdccff60 4053
6bede808 4054static int get_config_cap_drop(const char *key, char *retv, int inlen,
cccd2219 4055 struct lxc_conf *c, void *data)
1c96d6d8
CB
4056{
4057 int len, fulllen = 0;
4058 struct lxc_list *it;
4059
4060 if (!retv)
4061 inlen = 0;
4062 else
4063 memset(retv, 0, inlen);
4064
6bede808 4065 lxc_list_for_each(it, &c->caps) {
b80927f2
CB
4066 strprint(retv, inlen, "%s\n", (char *)it->elem);
4067 }
25a908b8 4068
b80927f2
CB
4069 return fulllen;
4070}
4071
6bede808 4072static int get_config_cap_keep(const char *key, char *retv, int inlen,
cccd2219 4073 struct lxc_conf *c, void *data)
b80927f2
CB
4074{
4075 int len, fulllen = 0;
4076 struct lxc_list *it;
4077
4078 if (!retv)
4079 inlen = 0;
4080 else
4081 memset(retv, 0, inlen);
4082
6bede808 4083 lxc_list_for_each(it, &c->keepcaps) {
1c96d6d8
CB
4084 strprint(retv, inlen, "%s\n", (char *)it->elem);
4085 }
25a908b8 4086
1c96d6d8
CB
4087 return fulllen;
4088}
0692663a 4089
3aed4934
CB
4090static int get_config_console_path(const char *key, char *retv, int inlen,
4091 struct lxc_conf *c, void *data)
0692663a 4092{
6bede808 4093 return lxc_get_conf_str(retv, inlen, c->console.path);
0692663a 4094}
794d1c06 4095
6bede808 4096static int get_config_console_logfile(const char *key, char *retv, int inlen,
cccd2219 4097 struct lxc_conf *c, void *data)
794d1c06 4098{
6bede808 4099 return lxc_get_conf_str(retv, inlen, c->console.log_path);
794d1c06 4100}
75f55b1f 4101
d91adfa6
CB
4102static int get_config_console_rotate(const char *key, char *retv, int inlen,
4103 struct lxc_conf *c, void *data)
4104{
4105 return lxc_get_conf_int(c, retv, inlen, c->console.log_rotate);
4106}
4107
4108
28f3b1cd
CB
4109static int get_config_console_buffer_size(const char *key, char *retv,
4110 int inlen, struct lxc_conf *c,
4111 void *data)
a04220de 4112{
dcea90a0 4113 return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size);
a04220de
CB
4114}
4115
861813e5
CB
4116static int get_config_console_size(const char *key, char *retv, int inlen,
4117 struct lxc_conf *c, void *data)
4118{
4119 return lxc_get_conf_uint64(c, retv, inlen, c->console.log_size);
4120}
4121
50d86993
CB
4122static int get_config_seccomp_allow_nesting(const char *key, char *retv,
4123 int inlen, struct lxc_conf *c,
4124 void *data)
4125{
c3e3c21a
CB
4126#ifdef HAVE_SECCOMP
4127 return lxc_get_conf_int(c, retv, inlen, c->seccomp.allow_nesting);
4128#else
4129 errno = ENOSYS;
4130 return -1;
4131#endif
50d86993 4132}
861813e5 4133
84cf6d25
WB
4134static int get_config_seccomp_notify_cookie(const char *key, char *retv, int inlen,
4135 struct lxc_conf *c, void *data)
4136{
4137#ifdef HAVE_SECCOMP_NOTIFY
4138 return lxc_get_conf_str(retv, inlen, c->seccomp.notifier.cookie);
4139#else
21fce08c 4140 return ret_set_errno(-1, ENOSYS);
84cf6d25
WB
4141#endif
4142}
4143
86ce1da1
CB
4144static int get_config_seccomp_notify_proxy(const char *key, char *retv, int inlen,
4145 struct lxc_conf *c, void *data)
4146{
c3e3c21a 4147#ifdef HAVE_SECCOMP_NOTIFY
86ce1da1 4148 return lxc_get_conf_str(retv, inlen,
c3e3c21a
CB
4149 (c->seccomp.notifier.proxy_addr.sun_path[0]) == '/'
4150 ? &c->seccomp.notifier.proxy_addr.sun_path[0]
4151 : &c->seccomp.notifier.proxy_addr.sun_path[1]);
86ce1da1 4152#else
21fce08c 4153 return ret_set_errno(-1, ENOSYS);
86ce1da1
CB
4154#endif
4155}
4156
0b427da0
CB
4157static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
4158 struct lxc_conf *c, void *data)
75f55b1f 4159{
c3e3c21a 4160 return lxc_get_conf_str(retv, inlen, c->seccomp.seccomp);
75f55b1f 4161}
97f6dad0 4162
6bede808 4163static int get_config_autodev(const char *key, char *retv, int inlen,
cccd2219 4164 struct lxc_conf *c, void *data)
97f6dad0 4165{
6bede808 4166 return lxc_get_conf_int(c, retv, inlen, c->autodev);
97f6dad0 4167}
afee4324 4168
63012bdd
CK
4169static int get_config_autodev_tmpfs_size(const char *key, char *retv, int inlen,
4170 struct lxc_conf *c, void *data)
4171{
4172 return lxc_get_conf_int(c, retv, inlen, c->autodevtmpfssize);
4173}
4174
55c84efc 4175static int get_config_signal_halt(const char *key, char *retv, int inlen,
25a908b8 4176 struct lxc_conf *c, void *data)
afee4324 4177{
6bede808 4178 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
afee4324 4179}
3aa8f359 4180
55c84efc 4181static int get_config_signal_reboot(const char *key, char *retv, int inlen,
25a908b8 4182 struct lxc_conf *c, void *data)
3aa8f359 4183{
6bede808 4184 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
3aa8f359 4185}
2e16269f 4186
55c84efc 4187static int get_config_signal_stop(const char *key, char *retv, int inlen,
25a908b8 4188 struct lxc_conf *c, void *data)
2e16269f 4189{
6bede808 4190 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
2e16269f 4191}
54345299 4192
6bede808 4193static int get_config_start(const char *key, char *retv, int inlen,
cccd2219 4194 struct lxc_conf *c, void *data)
54345299 4195{
c6182222 4196 if (strcmp(key + 10, "auto") == 0)
6bede808 4197 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
c6182222 4198 else if (strcmp(key + 10, "delay") == 0)
6bede808 4199 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
c6182222 4200 else if (strcmp(key + 10, "order") == 0)
6bede808 4201 return lxc_get_conf_int(c, retv, inlen, c->start_order);
54345299
CB
4202
4203 return -1;
4204}
de9df15e 4205
46cc906d 4206static int get_config_log_syslog(const char *key, char *retv, int inlen,
25a908b8 4207 struct lxc_conf *c, void *data)
de9df15e 4208{
6bede808 4209 return lxc_get_conf_str(retv, inlen, c->syslog);
de9df15e 4210}
ac0f949c 4211
6bede808 4212static int get_config_monitor(const char *key, char *retv, int inlen,
cccd2219 4213 struct lxc_conf *c, void *data)
ac0f949c 4214{
6bede808 4215 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
ac0f949c 4216}
90ec7b6e 4217
258f8051
CB
4218static int get_config_monitor_signal_pdeath(const char *key, char *retv,
4219 int inlen, struct lxc_conf *c,
4220 void *data)
4221{
4222 return lxc_get_conf_int(c, retv, inlen, c->monitor_signal_pdeath);
4223}
4224
6bede808 4225static int get_config_group(const char *key, char *retv, int inlen,
cccd2219 4226 struct lxc_conf *c, void *data)
90ec7b6e
CB
4227{
4228 int len, fulllen = 0;
4229 struct lxc_list *it;
4230
4231 if (!retv)
4232 inlen = 0;
4233 else
4234 memset(retv, 0, inlen);
4235
6bede808 4236 lxc_list_for_each(it, &c->groups) {
90ec7b6e
CB
4237 strprint(retv, inlen, "%s\n", (char *)it->elem);
4238 }
25a908b8 4239
90ec7b6e
CB
4240 return fulllen;
4241}
aa0db7d3 4242
6bede808 4243static int get_config_environment(const char *key, char *retv, int inlen,
cccd2219 4244 struct lxc_conf *c, void *data)
aa0db7d3
CB
4245{
4246 int len, fulllen = 0;
4247 struct lxc_list *it;
4248
4249 if (!retv)
4250 inlen = 0;
4251 else
4252 memset(retv, 0, inlen);
4253
6bede808 4254 lxc_list_for_each(it, &c->environment) {
aa0db7d3
CB
4255 strprint(retv, inlen, "%s\n", (char *)it->elem);
4256 }
25a908b8 4257
aa0db7d3
CB
4258 return fulllen;
4259}
96dfcb7d 4260
5cda27c1
SH
4261static int get_config_execute_cmd(const char *key, char *retv, int inlen,
4262 struct lxc_conf *c, void *data)
4263{
4264 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
4265}
4266
6bede808 4267static int get_config_init_cmd(const char *key, char *retv, int inlen,
cccd2219 4268 struct lxc_conf *c, void *data)
96dfcb7d 4269{
6bede808 4270 return lxc_get_conf_str(retv, inlen, c->init_cmd);
96dfcb7d 4271}
1398e9b1 4272
3c491553
L
4273static int get_config_init_cwd(const char *key, char *retv, int inlen,
4274 struct lxc_conf *c, void *data)
4275{
4276 return lxc_get_conf_str(retv, inlen, c->init_cwd);
4277}
4278
6bede808 4279static int get_config_init_uid(const char *key, char *retv, int inlen,
cccd2219 4280 struct lxc_conf *c, void *data)
1398e9b1 4281{
6bede808 4282 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
1398e9b1 4283}
dfeb7e42 4284
6bede808 4285static int get_config_init_gid(const char *key, char *retv, int inlen,
cccd2219 4286 struct lxc_conf *c, void *data)
dfeb7e42 4287{
6bede808 4288 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
dfeb7e42 4289}
62048afe 4290
6bede808 4291static int get_config_ephemeral(const char *key, char *retv, int inlen,
cccd2219 4292 struct lxc_conf *c, void *data)
62048afe 4293{
6bede808 4294 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
62048afe 4295}
b09521ac 4296
6bede808 4297static int get_config_no_new_privs(const char *key, char *retv, int inlen,
cccd2219 4298 struct lxc_conf *c, void *data)
b09521ac 4299{
6bede808 4300 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
b09521ac 4301}
389f6466 4302
25a908b8 4303/* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
240d4b74 4304 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4305 * printed, in 'lxc.prlimit.resource = value' format.
389f6466 4306 */
240d4b74 4307static int get_config_prlimit(const char *key, char *retv, int inlen,
47903908 4308 struct lxc_conf *c, void *data)
389f6466
CB
4309{
4310 int fulllen = 0, len;
4311 bool get_all = false;
4312 struct lxc_list *it;
4313
4314 if (!retv)
4315 inlen = 0;
4316 else
4317 memset(retv, 0, inlen);
4318
240d4b74 4319 if (!strcmp(key, "lxc.prlimit"))
389f6466 4320 get_all = true;
240d4b74 4321 else if (strncmp(key, "lxc.prlimit.", 12) == 0)
4322 key += 12;
389f6466
CB
4323 else
4324 return -1;
4325
6bede808 4326 lxc_list_for_each(it, &c->limits) {
c77aee64
CB
4327 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4328 char buf[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
389f6466
CB
4329 int partlen;
4330 struct lxc_limit *lim = it->elem;
4331
4332 if (lim->limit.rlim_cur == RLIM_INFINITY) {
6333c915
CB
4333 memcpy(buf, "unlimited", STRLITERALLEN("unlimited") + 1);
4334 partlen = STRLITERALLEN("unlimited");
389f6466
CB
4335 } else {
4336 partlen = sprintf(buf, "%" PRIu64,
4337 (uint64_t)lim->limit.rlim_cur);
4338 }
47903908 4339
389f6466 4340 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
25a908b8 4341 if (lim->limit.rlim_max == RLIM_INFINITY)
389f6466 4342 memcpy(buf + partlen, ":unlimited",
6333c915 4343 STRLITERALLEN(":unlimited") + 1);
25a908b8 4344 else
389f6466
CB
4345 sprintf(buf + partlen, ":%" PRIu64,
4346 (uint64_t)lim->limit.rlim_max);
389f6466
CB
4347 }
4348
4349 if (get_all) {
240d4b74 4350 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
389f6466 4351 lim->resource, buf);
47903908 4352 } else if (strcmp(lim->resource, key) == 0) {
389f6466
CB
4353 strprint(retv, inlen, "%s", buf);
4354 }
4355 }
4356
4357 return fulllen;
4358}
e08cb901 4359
e409b214
CB
4360/* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4361 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4362 * entries will be printed, in 'lxc.sysctl.key = value' format.
7edd0540
L
4363 */
4364static int get_config_sysctl(const char *key, char *retv, int inlen,
47903908 4365 struct lxc_conf *c, void *data)
7edd0540
L
4366{
4367 int len;
4368 struct lxc_list *it;
4369 int fulllen = 0;
4370 bool get_all = false;
4371
4372 if (!retv)
4373 inlen = 0;
4374 else
4375 memset(retv, 0, inlen);
4376
4377 if (strcmp(key, "lxc.sysctl") == 0)
4378 get_all = true;
6333c915
CB
4379 else if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
4380 key += STRLITERALLEN("lxc.sysctl.");
7edd0540
L
4381 else
4382 return -1;
4383
4384 lxc_list_for_each(it, &c->sysctls) {
4385 struct lxc_sysctl *elem = it->elem;
4386 if (get_all) {
47903908 4387 strprint(retv, inlen, "lxc.sysctl.%s = %s\n", elem->key,
4388 elem->value);
7edd0540
L
4389 } else if (strcmp(elem->key, key) == 0) {
4390 strprint(retv, inlen, "%s", elem->value);
4391 }
4392 }
4393
4394 return fulllen;
4395}
4396
61d7a733 4397static int get_config_proc(const char *key, char *retv, int inlen,
47903908 4398 struct lxc_conf *c, void *data)
61d7a733
YT
4399{
4400 struct lxc_list *it;
4401 int len;
4402 int fulllen = 0;
4403 bool get_all = false;
4404
4405 if (!retv)
4406 inlen = 0;
4407 else
4408 memset(retv, 0, inlen);
4409
4410 if (strcmp(key, "lxc.proc") == 0)
4411 get_all = true;
6333c915
CB
4412 else if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
4413 key += STRLITERALLEN("lxc.proc.");
61d7a733
YT
4414 else
4415 return -1;
4416
4417 lxc_list_for_each(it, &c->procs) {
4418 struct lxc_proc *proc = it->elem;
4419
4420 if (get_all) {
4421 strprint(retv, inlen, "lxc.proc.%s = %s\n",
47903908 4422 proc->filename, proc->value);
61d7a733
YT
4423 } else if (strcmp(proc->filename, key) == 0) {
4424 strprint(retv, inlen, "%s", proc->value);
4425 }
4426 }
4427
4428 return fulllen;
4429}
4430
1d8d3676
CB
4431static int get_config_namespace_clone(const char *key, char *retv, int inlen,
4432 struct lxc_conf *c, void *data)
4433{
4434 int i, len;
4435 int fulllen = 0;
4436
4437 if (!retv)
4438 inlen = 0;
4439 else
4440 memset(retv, 0, inlen);
4441
4442 for (i = 0; i < LXC_NS_MAX; i++) {
4443 if (c->ns_clone & ns_info[i].clone_flag)
4444 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4445 }
4446
4447 return fulllen;
4448}
4449
abeb5bba
CB
4450static int get_config_namespace_keep(const char *key, char *retv, int inlen,
4451 struct lxc_conf *c, void *data)
4452{
4453 int i, len;
4454 int fulllen = 0;
4455
4456 if (!retv)
4457 inlen = 0;
4458 else
4459 memset(retv, 0, inlen);
4460
4461 for (i = 0; i < LXC_NS_MAX; i++) {
4462 if (c->ns_keep & ns_info[i].clone_flag)
4463 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4464 }
4465
4466 return fulllen;
4467}
4468
70fd7fc9
CB
4469static int get_config_time_offset_boot(const char *key, char *retv, int inlen, struct lxc_conf *c,
4470 void *data)
4471{
4472 int len;
4473 int fulllen = 0;
4474
4475 if (!retv)
4476 inlen = 0;
4477 else
4478 memset(retv, 0, inlen);
4479
4480 if (c->timens.s_boot) {
4481 strprint(retv, inlen, "%" PRId64 " s\n", c->timens.s_boot);
4482 } else {
4483 strprint(retv, inlen, "%" PRId64 " ns\n", c->timens.ns_boot);
4484 }
4485
4486 return fulllen;
4487}
4488
4489static int get_config_time_offset_monotonic(const char *key, char *retv, int inlen,
4490 struct lxc_conf *c, void *data)
4491{
4492 int len;
4493 int fulllen = 0;
4494
4495 if (!retv)
4496 inlen = 0;
4497 else
4498 memset(retv, 0, inlen);
4499
4500 if (c->timens.s_monotonic) {
4501 strprint(retv, inlen, "%" PRId64 "s\n", c->timens.s_monotonic);
4502 } else {
4503 strprint(retv, inlen, "%" PRId64 "ns\n", c->timens.ns_monotonic);
4504 }
4505
4506 return fulllen;
4507}
4508
b074bbf1
CB
4509static int get_config_namespace_share(const char *key, char *retv, int inlen,
4510 struct lxc_conf *c, void *data)
28d9e29e
CB
4511{
4512 int len, ns_idx;
4513 const char *namespace;
4514 int fulllen = 0;
4515
4516 if (!retv)
4517 inlen = 0;
4518 else
4519 memset(retv, 0, inlen);
4520
6333c915 4521 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
4522 ns_idx = lxc_namespace_2_ns_idx(namespace);
4523 if (ns_idx < 0)
4524 return ns_idx;
4525
b074bbf1 4526 strprint(retv, inlen, "%s", c->ns_share[ns_idx]);
28d9e29e
CB
4527
4528 return fulllen;
4529}
4530
e08cb901 4531/* Callbacks to clear config items. */
26471403
CB
4532static inline int clr_config_personality(const char *key, struct lxc_conf *c,
4533 void *data)
e08cb901
CB
4534{
4535 c->personality = -1;
4536 return 0;
4537}
03818ae3 4538
232763d6
CB
4539static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
4540 void *data)
03818ae3 4541{
e528c735 4542 c->pty_max = 0;
03818ae3
CB
4543 return 0;
4544}
e7a4b096 4545
fe1c5887
CB
4546static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
4547 void *data)
e7a4b096 4548{
885766f5 4549 c->ttys.tty = 0;
e7a4b096
CB
4550 return 0;
4551}
eaf8c0c7 4552
42e53c29 4553static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
26471403 4554 void *data)
eaf8c0c7 4555{
885766f5
CB
4556 free(c->ttys.dir);
4557 c->ttys.dir = NULL;
eaf8c0c7
CB
4558 return 0;
4559}
6bd86308 4560
953fe44f
CB
4561static inline int clr_config_apparmor_profile(const char *key,
4562 struct lxc_conf *c, void *data)
025718fb
CB
4563{
4564 free(c->lsm_aa_profile);
4565 c->lsm_aa_profile = NULL;
4566 return 0;
4567}
3061e04e 4568
953fe44f
CB
4569static inline int clr_config_apparmor_allow_incomplete(const char *key,
4570 struct lxc_conf *c,
4571 void *data)
3061e04e
CB
4572{
4573 c->lsm_aa_allow_incomplete = 0;
4574 return 0;
4575}
31fc3494 4576
1800f924
WB
4577static inline int clr_config_apparmor_allow_nesting(const char *key,
4578 struct lxc_conf *c,
4579 void *data)
4580{
4581 c->lsm_aa_allow_nesting = 0;
4582 return 0;
4583}
4584
4585static inline int clr_config_apparmor_raw(const char *key,
4586 struct lxc_conf *c,
4587 void *data)
4588{
4589 return lxc_clear_apparmor_raw(c);
4590}
4591
953fe44f
CB
4592static inline int clr_config_selinux_context(const char *key,
4593 struct lxc_conf *c, void *data)
31fc3494
CB
4594{
4595 free(c->lsm_se_context);
4596 c->lsm_se_context = NULL;
4597 return 0;
4598}
754d01ce 4599
4fef78bc
MB
4600static inline int clr_config_selinux_context_keyring(const char *key,
4601 struct lxc_conf *c, void *data)
4602{
4603 free(c->lsm_se_keyring_context);
4604 c->lsm_se_keyring_context = NULL;
4605 return 0;
4606}
4607
8f818a84
MB
4608static inline int clr_config_keyring_session(const char *key,
4609 struct lxc_conf *c, void *data)
4610{
4611 c->keyring_disable_session = false;
4612 return 0;
4613}
4614
43654d34
CB
4615static inline int clr_config_cgroup_controller(const char *key,
4616 struct lxc_conf *c, void *data)
754d01ce 4617{
54860ed0
CB
4618 return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC);
4619}
4620
4621static inline int clr_config_cgroup2_controller(const char *key,
4622 struct lxc_conf *c, void *data)
4623{
4624 return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC);
754d01ce 4625}
d3a178de 4626
43654d34
CB
4627static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
4628 void *data)
4629{
4630 if (lxc_conf->cgroup_meta.dir) {
4631 free(lxc_conf->cgroup_meta.dir);
4632 lxc_conf->cgroup_meta.dir = NULL;
4633 }
4634
4635 return 0;
4636}
4637
a900cbaf
WB
4638static int clr_config_cgroup_monitor_dir(const char *key,
4639 struct lxc_conf *lxc_conf,
4640 void *data)
4641{
4642 free_disarm(lxc_conf->cgroup_meta.monitor_dir);
4643 return 0;
4644}
4645
7696c1f9
RJ
4646static int clr_config_cgroup_monitor_pivot_dir(const char *key,
4647 struct lxc_conf *lxc_conf,
4648 void *data)
4649{
4650 free_disarm(lxc_conf->cgroup_meta.monitor_pivot_dir);
4651 return 0;
4652}
4653
a900cbaf
WB
4654static int clr_config_cgroup_container_dir(const char *key,
4655 struct lxc_conf *lxc_conf,
4656 void *data)
4657{
4658 free_disarm(lxc_conf->cgroup_meta.container_dir);
4659 return 0;
4660}
4661
4662static int clr_config_cgroup_container_inner_dir(const char *key,
4663 struct lxc_conf *lxc_conf,
4664 void *data)
4665{
4666 free_disarm(lxc_conf->cgroup_meta.namespace_dir);
4667 return 0;
4668}
4669
9caee129
CB
4670static inline int clr_config_cgroup_relative(const char *key,
4671 struct lxc_conf *lxc_conf,
4672 void *data)
76f0e2e7 4673{
9caee129 4674 lxc_conf->cgroup_meta.relative = false;
76f0e2e7
CB
4675 return 0;
4676}
4677
26471403
CB
4678static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
4679 void *data)
d3a178de
CB
4680{
4681 return lxc_clear_idmaps(c);
4682}
97d3338d 4683
46cc906d 4684static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
26471403 4685 void *data)
97d3338d 4686{
4b73005c 4687 c->loglevel = LXC_LOG_LEVEL_NOTSET;
97d3338d
CB
4688 return 0;
4689}
de46099c 4690
46cc906d 4691static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
26471403 4692 void *data)
de46099c
CB
4693{
4694 free(c->logfile);
4695 c->logfile = NULL;
4696 return 0;
4697}
b4fa13cd 4698
26471403
CB
4699static inline int clr_config_mount(const char *key, struct lxc_conf *c,
4700 void *data)
b4fa13cd
CB
4701{
4702 return lxc_clear_mount_entries(c);
4703}
4be81021 4704
26471403
CB
4705static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
4706 void *data)
4be81021
CB
4707{
4708 return lxc_clear_automounts(c);
4709}
350d4b15 4710
47148e96
CB
4711static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
4712 void *data)
350d4b15
CB
4713{
4714 free(c->fstab);
4715 c->fstab = NULL;
4716 return 0;
4717}
faca124d 4718
7a96a068
CB
4719static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
4720 void *data)
faca124d
CB
4721{
4722 free(c->rootfs.path);
4723 c->rootfs.path = NULL;
4724 return 0;
4725}
4726
6e54330c
CB
4727static inline int clr_config_rootfs_managed(const char *key, struct lxc_conf *c,
4728 void *data)
4729{
4730 c->rootfs.managed = true;
4731 return 0;
4732}
4733
26471403
CB
4734static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
4735 void *data)
fddefc2d
CB
4736{
4737 free(c->rootfs.mount);
4738 c->rootfs.mount = NULL;
4739 return 0;
4740}
7b1eb67d 4741
26471403
CB
4742static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
4743 void *data)
7b1eb67d
CB
4744{
4745 free(c->rootfs.options);
4746 c->rootfs.options = NULL;
3437f95c
CB
4747
4748 free(c->rootfs.data);
4749 c->rootfs.data = NULL;
4750
7b1eb67d
CB
4751 return 0;
4752}
02becb8d 4753
b67771bc 4754static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
26471403 4755 void *data)
d31d0103
CB
4756{
4757 free(c->utsname);
4758 c->utsname = NULL;
4759 return 0;
4760}
c9eeb90c 4761
26471403
CB
4762static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
4763 void *data)
c9eeb90c
CB
4764{
4765 return lxc_clear_hooks(c, key);
4766}
4767
44ae0fb6
CB
4768static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c,
4769 void *data)
4770{
4771 /* default to legacy hooks version */
4772 c->hooks_version = 0;
4773 return 0;
4774}
4775
f9373e40
CB
4776static inline int clr_config_net(const char *key, struct lxc_conf *c,
4777 void *data)
f4488271 4778{
c302b476
CB
4779 lxc_free_networks(&c->network);
4780
e5d2fd7c 4781 return 0;
f4488271
CB
4782}
4783
26471403
CB
4784static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
4785 void *data)
244cb55b
CB
4786{
4787 return lxc_clear_config_caps(c);
4788}
c74cc490 4789
26471403
CB
4790static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
4791 void *data)
c74cc490
CB
4792{
4793 return lxc_clear_config_keepcaps(c);
4794}
4e5b633f 4795
3aed4934
CB
4796static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
4797 void *data)
4e5b633f
CB
4798{
4799 free(c->console.path);
4800 c->console.path = NULL;
4801 return 0;
4802}
4803
7c2ec23a 4804static inline int clr_config_console_logfile(const char *key,
26471403 4805 struct lxc_conf *c, void *data)
7c2ec23a
CB
4806{
4807 free(c->console.log_path);
4808 c->console.log_path = NULL;
4809 return 0;
4810}
bbca37d8 4811
d91adfa6
CB
4812static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c,
4813 void *data)
4814{
4815 c->console.log_rotate = 0;
4816 return 0;
4817}
4818
28f3b1cd
CB
4819static inline int clr_config_console_buffer_size(const char *key,
4820 struct lxc_conf *c, void *data)
a04220de 4821{
28f3b1cd 4822 c->console.buffer_size = 0;
a04220de
CB
4823 return 0;
4824}
4825
861813e5
CB
4826static inline int clr_config_console_size(const char *key, struct lxc_conf *c,
4827 void *data)
4828{
4829 c->console.log_size = 0;
4830 return 0;
4831}
4832
50d86993
CB
4833static inline int clr_config_seccomp_allow_nesting(const char *key,
4834 struct lxc_conf *c, void *data)
4835{
c3e3c21a
CB
4836#ifdef HAVE_SECCOMP
4837 c->seccomp.allow_nesting = 0;
50d86993 4838 return 0;
c3e3c21a
CB
4839#else
4840 errno = ENOSYS;
4841 return -1;
4842#endif
50d86993
CB
4843}
4844
84cf6d25
WB
4845static inline int clr_config_seccomp_notify_cookie(const char *key,
4846 struct lxc_conf *c, void *data)
4847{
4848#ifdef HAVE_SECCOMP_NOTIFY
4849 free(c->seccomp.notifier.cookie);
4850 c->seccomp.notifier.cookie = NULL;
4851 return 0;
4852#else
21fce08c 4853 return ret_set_errno(-1, ENOSYS);
84cf6d25
WB
4854#endif
4855}
4856
86ce1da1
CB
4857static inline int clr_config_seccomp_notify_proxy(const char *key,
4858 struct lxc_conf *c, void *data)
4859{
c3e3c21a
CB
4860#ifdef HAVE_SECCOMP_NOTIFY
4861 memset(&c->seccomp.notifier.proxy_addr, 0,
4862 sizeof(c->seccomp.notifier.proxy_addr));
86ce1da1
CB
4863 return 0;
4864#else
21fce08c 4865 return ret_set_errno(-1, ENOSYS);
86ce1da1
CB
4866#endif
4867}
4868
0b427da0
CB
4869static inline int clr_config_seccomp_profile(const char *key,
4870 struct lxc_conf *c, void *data)
bbca37d8 4871{
c3e3c21a
CB
4872 free(c->seccomp.seccomp);
4873 c->seccomp.seccomp = NULL;
bbca37d8
CB
4874 return 0;
4875}
c721e86c 4876
26471403
CB
4877static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
4878 void *data)
c721e86c
CB
4879{
4880 c->autodev = 1;
4881 return 0;
4882}
87b288d1 4883
63012bdd
CK
4884static inline int clr_config_autodev_tmpfs_size(const char *key, struct lxc_conf *c,
4885 void *data)
4886{
4887 c->autodevtmpfssize = 500000;
4888 return 0;
4889}
4890
55c84efc 4891static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
26471403 4892 void *data)
87b288d1
CB
4893{
4894 c->haltsignal = 0;
4895 return 0;
4896}
cae63cfa 4897
55c84efc 4898static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
26471403 4899 void *data)
cae63cfa
CB
4900{
4901 c->rebootsignal = 0;
4902 return 0;
4903}
de45f3a8 4904
55c84efc 4905static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
26471403 4906 void *data)
de45f3a8
CB
4907{
4908 c->stopsignal = 0;
4909 return 0;
4910}
c6182222 4911
26471403
CB
4912static inline int clr_config_start(const char *key, struct lxc_conf *c,
4913 void *data)
c6182222
CB
4914{
4915 if (strcmp(key + 10, "auto") == 0)
4916 c->start_auto = 0;
4917 else if (strcmp(key + 10, "delay") == 0)
4918 c->start_delay = 0;
4919 else if (strcmp(key + 10, "order") == 0)
4920 c->start_order = 0;
4921
4922 return 0;
4923}
998ca94f 4924
46cc906d 4925static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
26471403 4926 void *data)
998ca94f
CB
4927{
4928 free(c->syslog);
4929 c->syslog = NULL;
4930 return 0;
4931}
adad12ca 4932
26471403
CB
4933static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
4934 void *data)
adad12ca
CB
4935{
4936 c->monitor_unshare = 0;
4937 return 0;
4938}
4850d223 4939
258f8051
CB
4940static inline int clr_config_monitor_signal_pdeath(const char *key,
4941 struct lxc_conf *c, void *data)
4942{
4943 c->monitor_signal_pdeath = 0;
4944 return 0;
4945}
4946
26471403
CB
4947static inline int clr_config_group(const char *key, struct lxc_conf *c,
4948 void *data)
4850d223
CB
4949{
4950 return lxc_clear_groups(c);
4951}
832fb63a 4952
26471403
CB
4953static inline int clr_config_environment(const char *key, struct lxc_conf *c,
4954 void *data)
832fb63a
CB
4955{
4956 return lxc_clear_environment(c);
4850d223 4957}
8e90af3e 4958
5cda27c1
SH
4959static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
4960 void *data)
4961{
4962 free(c->execute_cmd);
4963 c->execute_cmd = NULL;
4964 return 0;
4965}
4966
26471403
CB
4967static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
4968 void *data)
8e90af3e
CB
4969{
4970 free(c->init_cmd);
4971 c->init_cmd = NULL;
4972 return 0;
4973}
ec76dcfb 4974
3c491553
L
4975static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
4976 void *data)
4977{
4978 free(c->init_cwd);
4979 c->init_cwd = NULL;
4980 return 0;
4981}
4982
26471403
CB
4983static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
4984 void *data)
ec76dcfb
CB
4985{
4986 c->init_uid = 0;
4987 return 0;
4988}
1044b247 4989
26471403
CB
4990static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
4991 void *data)
1044b247
CB
4992{
4993 c->init_gid = 0;
4994 return 0;
4995}
59e370db 4996
26471403
CB
4997static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
4998 void *data)
59e370db
CB
4999{
5000 c->ephemeral = 0;
5001 return 0;
5002}
b98c5ab0 5003
26471403
CB
5004static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
5005 void *data)
b98c5ab0
CB
5006{
5007 c->no_new_privs = false;
5008 return 0;
5009}
715ccc96 5010
240d4b74 5011static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
26471403 5012 void *data)
715ccc96
CB
5013{
5014 return lxc_clear_limits(c, key);
5015}
fdf3c589 5016
7edd0540
L
5017static inline int clr_config_sysctl(const char *key, struct lxc_conf *c,
5018 void *data)
5019{
5020 return lxc_clear_sysctls(c, key);
5021}
5022
61d7a733
YT
5023static inline int clr_config_proc(const char *key, struct lxc_conf *c,
5024 void *data)
5025{
5026 return lxc_clear_procs(c, key);
5027}
5028
26471403
CB
5029static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
5030 void *data)
fdf3c589
CB
5031{
5032 lxc_clear_includes(c);
5033 return 0;
5034}
a3c8e600 5035
1d8d3676
CB
5036static int clr_config_namespace_clone(const char *key,
5037 struct lxc_conf *lxc_conf, void *data)
5038{
5039 lxc_conf->ns_clone = 0;
5040 return 0;
5041}
5042
abeb5bba
CB
5043static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf,
5044 void *data)
5045{
5046 lxc_conf->ns_keep = 0;
5047 return 0;
5048}
5049
70fd7fc9
CB
5050static int clr_config_time_offset_boot(const char *key, struct lxc_conf *lxc_conf, void *data)
5051{
5052 lxc_conf->timens.s_boot = 0;
5053 lxc_conf->timens.ns_boot = 0;
5054 return 0;
5055}
5056
5057static int clr_config_time_offset_monotonic(const char *key, struct lxc_conf *lxc_conf, void *data)
5058{
5059 lxc_conf->timens.s_monotonic = 0;
5060 lxc_conf->timens.ns_monotonic = 0;
5061 return 0;
5062}
5063
b074bbf1
CB
5064static int clr_config_namespace_share(const char *key,
5065 struct lxc_conf *lxc_conf, void *data)
28d9e29e
CB
5066{
5067 int ns_idx;
5068 const char *namespace;
5069
6333c915 5070 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
5071 ns_idx = lxc_namespace_2_ns_idx(namespace);
5072 if (ns_idx < 0)
5073 return ns_idx;
5074
b074bbf1
CB
5075 free(lxc_conf->ns_share[ns_idx]);
5076 lxc_conf->ns_share[ns_idx] = NULL;
28d9e29e
CB
5077
5078 return 0;
5079}
5080
a3c8e600 5081static int get_config_includefiles(const char *key, char *retv, int inlen,
cccd2219 5082 struct lxc_conf *c, void *data)
a3c8e600
CB
5083{
5084 return -ENOSYS;
5085}
40db5d2f 5086
31ee747b
CB
5087static struct lxc_config_t *get_network_config_ops(const char *key,
5088 struct lxc_conf *lxc_conf,
5089 ssize_t *idx,
5090 char **deindexed_key)
40db5d2f 5091{
31ee747b
CB
5092 int ret;
5093 unsigned int tmpidx;
5094 size_t numstrlen;
40db5d2f
CB
5095 char *copy, *idx_start, *idx_end;
5096 struct lxc_config_t *config = NULL;
5097
5098 /* check that this is a sensible network key */
31ee747b
CB
5099 if (strncmp("lxc.net.", key, 8)) {
5100 ERROR("Invalid network configuration key \"%s\"", key);
40db5d2f 5101 return NULL;
31ee747b 5102 }
40db5d2f
CB
5103
5104 copy = strdup(key);
31ee747b
CB
5105 if (!copy) {
5106 ERROR("Failed to duplicate string \"%s\"", key);
40db5d2f 5107 return NULL;
31ee747b 5108 }
40db5d2f 5109
f9373e40 5110 /* lxc.net.<n> */
31ee747b
CB
5111 if (!isdigit(*(key + 8))) {
5112 ERROR("Failed to detect digit in string \"%s\"", key + 8);
5113 goto on_error;
5114 }
40db5d2f 5115
31ee747b
CB
5116 /* beginning of index string */
5117 idx_start = (copy + 7);
5118 *idx_start = '\0';
40db5d2f 5119
31ee747b
CB
5120 /* end of index string */
5121 idx_end = strchr((copy + 8), '.');
7451daf8
CB
5122 if (idx_end)
5123 *idx_end = '\0';
31ee747b
CB
5124
5125 /* parse current index */
5126 ret = lxc_safe_uint((idx_start + 1), &tmpidx);
5127 if (ret < 0) {
6d1400b5 5128 errno = -ret;
ae52f396 5129 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
6d1400b5 5130 idx_start + 1);
31ee747b
CB
5131 *idx = ret;
5132 goto on_error;
5133 }
40db5d2f 5134
31ee747b
CB
5135 /* This, of course is utterly nonsensical on so many levels, but
5136 * better safe than sorry.
5137 * (Checking for INT_MAX here is intentional.)
5138 */
5139 if (tmpidx == INT_MAX) {
25a908b8 5140 SYSERROR("Number of configured networks would overflow the "
47903908 5141 "counter");
31ee747b 5142 goto on_error;
40db5d2f 5143 }
31ee747b
CB
5144 *idx = tmpidx;
5145
5146 numstrlen = strlen((idx_start + 1));
5147
5148 /* repair configuration key */
5149 *idx_start = '.';
31ee747b 5150
7451daf8
CB
5151 /* lxc.net.<idx>.<subkey> */
5152 if (idx_end) {
5153 *idx_end = '.';
24fa7df6
FA
5154 if (strlen(idx_end + 1) == 0) {
5155 ERROR("No subkey in network configuration key \"%s\"", key);
5156 goto on_error;
5157 }
40db5d2f 5158
7451daf8 5159 memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
52ab03ac 5160 copy[strlen(key) - (numstrlen + 1)] = '\0';
7451daf8 5161
300df83e 5162 config = lxc_get_config(copy);
7451daf8 5163 if (!config) {
25a908b8 5164 ERROR("Unknown network configuration key \"%s\"", key);
7451daf8
CB
5165 goto on_error;
5166 }
31ee747b
CB
5167 }
5168
7451daf8
CB
5169 if (deindexed_key)
5170 *deindexed_key = copy;
5171
31ee747b 5172 return config;
40db5d2f
CB
5173
5174on_error:
5175 free(copy);
31ee747b 5176 return NULL;
40db5d2f
CB
5177}
5178
25a908b8
CB
5179/* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5180 * found. So we make sure next comes an integer, find the right callback (by
5181 * rewriting the key), and call it.
40db5d2f 5182 */
f9373e40
CB
5183static int set_config_net_nic(const char *key, const char *value,
5184 struct lxc_conf *lxc_conf, void *data)
40db5d2f 5185{
31ee747b
CB
5186 int ret;
5187 const char *idxstring;
40db5d2f
CB
5188 struct lxc_config_t *config;
5189 struct lxc_netdev *netdev;
5190 ssize_t idx = -1;
31ee747b
CB
5191 char *deindexed_key = NULL;
5192
5193 idxstring = key + 8;
5194 if (!isdigit(*idxstring))
5195 return -1;
40db5d2f 5196
6bed0fb6 5197 if (lxc_config_value_empty(value))
f9373e40 5198 return clr_config_net_nic(key, lxc_conf, data);
6bed0fb6 5199
31ee747b 5200 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
40db5d2f
CB
5201 if (!config || idx < 0)
5202 return -1;
5203
0070b1c4 5204 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
31ee747b
CB
5205 if (!netdev) {
5206 free(deindexed_key);
40db5d2f 5207 return -1;
31ee747b 5208 }
40db5d2f 5209
31ee747b
CB
5210 ret = config->set(deindexed_key, value, lxc_conf, netdev);
5211 free(deindexed_key);
47903908 5212
31ee747b 5213 return ret;
40db5d2f 5214}
ff6da295 5215
f9373e40
CB
5216static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
5217 void *data)
ff6da295 5218{
31ee747b 5219 int ret;
519df1c1 5220 const char *idxstring;
ff6da295
CB
5221 struct lxc_config_t *config;
5222 struct lxc_netdev *netdev;
31ee747b
CB
5223 ssize_t idx = -1;
5224 char *deindexed_key = NULL;
519df1c1 5225
31ee747b
CB
5226 idxstring = key + 8;
5227 if (!isdigit(*idxstring))
519df1c1
CB
5228 return -1;
5229
519df1c1 5230 /* The left conjunct is pretty self-explanatory. The right conjunct
31ee747b 5231 * checks whether the two pointers are equal. If they are we know that
519df1c1
CB
5232 * this is not a key that is namespaced any further and so we are
5233 * supposed to clear the whole network.
5234 */
5235 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
5236 unsigned int rmnetdevidx;
5237
5238 if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0)
5239 return -1;
5240
5241 /* Remove network from network list. */
5242 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
5243 return 0;
5244 }
ff6da295 5245
31ee747b 5246 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
ff6da295
CB
5247 if (!config || idx < 0)
5248 return -1;
5249
0070b1c4 5250 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false);
31ee747b
CB
5251 if (!netdev) {
5252 free(deindexed_key);
ff6da295 5253 return -1;
31ee747b 5254 }
ff6da295 5255
31ee747b
CB
5256 ret = config->clr(deindexed_key, lxc_conf, netdev);
5257 free(deindexed_key);
47903908 5258
31ee747b 5259 return ret;
ff6da295
CB
5260}
5261
f9373e40
CB
5262static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
5263 void *data)
ff6da295 5264{
070a05af 5265 struct lxc_netdev *netdev = data;
ff6da295 5266
ff6da295
CB
5267 if (!netdev)
5268 return -1;
5269
5270 netdev->type = -1;
5271
5272 return 0;
5273}
5274
f9373e40
CB
5275static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
5276 void *data)
ff6da295 5277{
070a05af 5278 struct lxc_netdev *netdev = data;
ff6da295 5279
ff6da295
CB
5280 if (!netdev)
5281 return -1;
5282
de4855a8 5283 netdev->name[0] = '\0';
ff6da295
CB
5284
5285 return 0;
5286}
5287
f9373e40
CB
5288static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
5289 void *data)
ff6da295 5290{
070a05af 5291 struct lxc_netdev *netdev = data;
ff6da295 5292
ff6da295
CB
5293 if (!netdev)
5294 return -1;
5295
5296 netdev->flags = 0;
5297
5298 return 0;
5299}
5300
f9373e40
CB
5301static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
5302 void *data)
ff6da295 5303{
070a05af 5304 struct lxc_netdev *netdev = data;
ff6da295 5305
ff6da295
CB
5306 if (!netdev)
5307 return -1;
5308
de4855a8 5309 netdev->link[0] = '\0';
ff6da295
CB
5310
5311 return 0;
5312}
5313
6509154d 5314static int clr_config_net_l2proxy(const char *key, struct lxc_conf *lxc_conf,
5315 void *data)
5316{
5317 struct lxc_netdev *netdev = data;
5318
5319 if (!netdev)
21fce08c 5320 return ret_set_errno(-1, EINVAL);
6509154d 5321
5322 netdev->l2proxy = false;
5323
5324 return 0;
5325}
5326
f9373e40
CB
5327static int clr_config_net_macvlan_mode(const char *key,
5328 struct lxc_conf *lxc_conf, void *data)
ff6da295 5329{
070a05af 5330 struct lxc_netdev *netdev = data;
ff6da295 5331
ff6da295
CB
5332 if (!netdev)
5333 return -1;
5334
6bed0fb6
CB
5335 if (netdev->type != LXC_NET_MACVLAN)
5336 return 0;
5337
ff6da295
CB
5338 netdev->priv.macvlan_attr.mode = -1;
5339
5340 return 0;
5341}
5342
c9f52382 5343static int clr_config_net_ipvlan_mode(const char *key,
5344 struct lxc_conf *lxc_conf, void *data)
5345{
5346 struct lxc_netdev *netdev = data;
5347
5348 if (!netdev)
21fce08c 5349 return ret_set_errno(-1, EINVAL);
c9f52382 5350
5351 if (netdev->type != LXC_NET_IPVLAN)
5352 return 0;
5353
5354 netdev->priv.ipvlan_attr.mode = -1;
5355
5356 return 0;
5357}
5358
5359static int clr_config_net_ipvlan_isolation(const char *key,
5360 struct lxc_conf *lxc_conf, void *data)
5361{
5362 struct lxc_netdev *netdev = data;
5363
5364 if (!netdev)
21fce08c 5365 return ret_set_errno(-1, EINVAL);
c9f52382 5366
5367 if (netdev->type != LXC_NET_IPVLAN)
5368 return 0;
5369
5370 netdev->priv.ipvlan_attr.isolation = -1;
5371
5372 return 0;
5373}
5374
3f0ed090
TP
5375static int clr_config_net_veth_mode(const char *key,
5376 struct lxc_conf *lxc_conf, void *data)
5377{
5378 struct lxc_netdev *netdev = data;
5379
5380 if (!netdev)
21fce08c 5381 return ret_set_errno(-1, EINVAL);
3f0ed090
TP
5382
5383 if (netdev->type != LXC_NET_VETH)
5384 return 0;
5385
5386 netdev->priv.veth_attr.mode = -1;
5387
5388 return 0;
5389}
5390
f9373e40
CB
5391static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
5392 void *data)
ff6da295 5393{
070a05af 5394 struct lxc_netdev *netdev = data;
ff6da295 5395
ff6da295
CB
5396 if (!netdev)
5397 return -1;
5398
de4855a8 5399 netdev->priv.veth_attr.pair[0] = '\0';
ff6da295
CB
5400
5401 return 0;
5402}
5403
fdf76c6d
TP
5404static int clr_config_net_veth_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5405 void *data)
5406{
5407 struct lxc_netdev *netdev = data;
5408
5409 if (!netdev)
5410 return ret_errno(EINVAL);
5411
5412 netdev->priv.veth_attr.vlan_id = 0;
5413 netdev->priv.veth_attr.vlan_id_set = false;
5414
5415 return 0;
5416}
5417
b8e06d33
TP
5418static int clr_config_net_veth_vlan_tagged_id(const char *key,
5419 struct lxc_conf *lxc_conf, void *data)
5420{
5421 struct lxc_netdev *netdev = data;
5422 struct lxc_list *cur, *next;
5423
5424 if (!netdev)
5425 return ret_errno(EINVAL);
5426
5427 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.vlan_tagged_ids, next) {
5428 lxc_list_del(cur);
5429 free(cur);
5430 }
5431
5432 return 0;
5433}
5434
5435
f9373e40
CB
5436static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
5437 void *data)
ff6da295 5438{
070a05af 5439 struct lxc_netdev *netdev = data;
ff6da295 5440
ff6da295
CB
5441 if (!netdev)
5442 return -1;
5443
5444 free(netdev->upscript);
5445 netdev->upscript = NULL;
5446
5447 return 0;
5448}
5449
f9373e40
CB
5450static int clr_config_net_script_down(const char *key,
5451 struct lxc_conf *lxc_conf, void *data)
ff6da295 5452{
070a05af 5453 struct lxc_netdev *netdev = data;
ff6da295 5454
ff6da295
CB
5455 if (!netdev)
5456 return -1;
5457
5458 free(netdev->downscript);
5459 netdev->downscript = NULL;
5460
5461 return 0;
5462}
5463
f9373e40
CB
5464static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
5465 void *data)
ff6da295 5466{
070a05af 5467 struct lxc_netdev *netdev = data;
ff6da295 5468
ff6da295
CB
5469 if (!netdev)
5470 return -1;
5471
5472 free(netdev->hwaddr);
5473 netdev->hwaddr = NULL;
5474
5475 return 0;
5476}
5477
f9373e40
CB
5478static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
5479 void *data)
ff6da295 5480{
070a05af 5481 struct lxc_netdev *netdev = data;
ff6da295 5482
ff6da295
CB
5483 if (!netdev)
5484 return -1;
5485
5486 free(netdev->mtu);
5487 netdev->mtu = NULL;
5488
5489 return 0;
5490}
5491
f9373e40
CB
5492static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5493 void *data)
ff6da295 5494{
070a05af 5495 struct lxc_netdev *netdev = data;
ff6da295 5496
ff6da295
CB
5497 if (!netdev)
5498 return -1;
5499
5500 netdev->priv.vlan_attr.vid = 0;
5501
5502 return 0;
5503}
5504
f9373e40
CB
5505static int clr_config_net_ipv4_gateway(const char *key,
5506 struct lxc_conf *lxc_conf, void *data)
ff6da295 5507{
070a05af 5508 struct lxc_netdev *netdev = data;
ff6da295 5509
ff6da295
CB
5510 if (!netdev)
5511 return -1;
5512
5513 free(netdev->ipv4_gateway);
5514 netdev->ipv4_gateway = NULL;
5515
5516 return 0;
5517}
5518
9ff60df2
CB
5519static int clr_config_net_ipv4_address(const char *key,
5520 struct lxc_conf *lxc_conf, void *data)
ff6da295 5521{
070a05af 5522 struct lxc_netdev *netdev = data;
ff6da295
CB
5523 struct lxc_list *cur, *next;
5524
ecbb3790
CB
5525 if (!netdev)
5526 return -1;
ff6da295
CB
5527
5528 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
5529 lxc_list_del(cur);
5530 free(cur->elem);
5531 free(cur);
5532 }
5533
5534 return 0;
5535}
5536
d4a7da46 5537static int clr_config_net_veth_ipv4_route(const char *key,
5538 struct lxc_conf *lxc_conf, void *data)
5539{
5540 struct lxc_netdev *netdev = data;
5541 struct lxc_list *cur, *next;
5542
5543 if (!netdev)
5544 return -1;
5545
5546 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) {
5547 lxc_list_del(cur);
5548 free(cur->elem);
5549 free(cur);
5550 }
5551
5552 return 0;
5553}
5554
f9373e40
CB
5555static int clr_config_net_ipv6_gateway(const char *key,
5556 struct lxc_conf *lxc_conf, void *data)
ff6da295 5557{
070a05af 5558 struct lxc_netdev *netdev = data;
ff6da295 5559
ff6da295
CB
5560 if (!netdev)
5561 return -1;
5562
5563 free(netdev->ipv6_gateway);
5564 netdev->ipv6_gateway = NULL;
5565
5566 return 0;
5567}
5568
2e44ae28
CB
5569static int clr_config_net_ipv6_address(const char *key,
5570 struct lxc_conf *lxc_conf, void *data)
ff6da295 5571{
070a05af 5572 struct lxc_netdev *netdev = data;
ff6da295
CB
5573 struct lxc_list *cur, *next;
5574
ecbb3790
CB
5575 if (!netdev)
5576 return -1;
ff6da295
CB
5577
5578 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
5579 lxc_list_del(cur);
5580 free(cur->elem);
5581 free(cur);
5582 }
5583
5584 return 0;
5585}
9d4bf22d 5586
d4a7da46 5587static int clr_config_net_veth_ipv6_route(const char *key,
5588 struct lxc_conf *lxc_conf, void *data)
5589{
5590 struct lxc_netdev *netdev = data;
5591 struct lxc_list *cur, *next;
5592
5593 if (!netdev)
5594 return -1;
5595
5596 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) {
5597 lxc_list_del(cur);
5598 free(cur->elem);
5599 free(cur);
5600 }
5601
5602 return 0;
5603}
5604
f9373e40
CB
5605static int get_config_net_nic(const char *key, char *retv, int inlen,
5606 struct lxc_conf *c, void *data)
9d4bf22d 5607{
31ee747b
CB
5608 int ret;
5609 const char *idxstring;
9d4bf22d
CB
5610 struct lxc_config_t *config;
5611 struct lxc_netdev *netdev;
5612 ssize_t idx = -1;
31ee747b 5613 char *deindexed_key = NULL;
9d4bf22d 5614
31ee747b
CB
5615 idxstring = key + 8;
5616 if (!isdigit(*idxstring))
5617 return -1;
5618
5619 config = get_network_config_ops(key, c, &idx, &deindexed_key);
9d4bf22d
CB
5620 if (!config || idx < 0)
5621 return -1;
5622
0070b1c4 5623 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
31ee747b
CB
5624 if (!netdev) {
5625 free(deindexed_key);
9d4bf22d 5626 return -1;
31ee747b 5627 }
9d4bf22d 5628
31ee747b
CB
5629 ret = config->get(deindexed_key, retv, inlen, c, netdev);
5630 free(deindexed_key);
47903908 5631
31ee747b 5632 return ret;
9d4bf22d
CB
5633}
5634
f9373e40
CB
5635static int get_config_net_type(const char *key, char *retv, int inlen,
5636 struct lxc_conf *c, void *data)
9d4bf22d 5637{
070a05af
CB
5638 int len;
5639 int fulllen = 0;
5640 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5641
5642 if (!retv)
5643 inlen = 0;
5644 else
5645 memset(retv, 0, inlen);
5646
9d4bf22d
CB
5647 if (!netdev)
5648 return -1;
5649
5650 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
5651
5652 return fulllen;
5653}
5654
f9373e40
CB
5655static int get_config_net_flags(const char *key, char *retv, int inlen,
5656 struct lxc_conf *c, void *data)
9d4bf22d 5657{
070a05af
CB
5658 int len;
5659 int fulllen = 0;
5660 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5661
5662 if (!retv)
5663 inlen = 0;
5664 else
5665 memset(retv, 0, inlen);
5666
9d4bf22d
CB
5667 if (!netdev)
5668 return -1;
5669
5670 if (netdev->flags & IFF_UP)
5671 strprint(retv, inlen, "up");
5672
5673 return fulllen;
5674}
5675
f9373e40
CB
5676static int get_config_net_link(const char *key, char *retv, int inlen,
5677 struct lxc_conf *c, void *data)
9d4bf22d 5678{
070a05af
CB
5679 int len;
5680 int fulllen = 0;
5681 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5682
5683 if (!retv)
5684 inlen = 0;
5685 else
5686 memset(retv, 0, inlen);
5687
9d4bf22d
CB
5688 if (!netdev)
5689 return -1;
5690
de4855a8 5691 if (netdev->link[0] != '\0')
9d4bf22d
CB
5692 strprint(retv, inlen, "%s", netdev->link);
5693
5694 return fulllen;
5695}
5696
6509154d 5697static int get_config_net_l2proxy(const char *key, char *retv, int inlen,
5698 struct lxc_conf *c, void *data)
5699{
5700 struct lxc_netdev *netdev = data;
5701 return lxc_get_conf_bool(c, retv, inlen, netdev->l2proxy);
5702}
5703
f9373e40
CB
5704static int get_config_net_name(const char *key, char *retv, int inlen,
5705 struct lxc_conf *c, void *data)
9d4bf22d 5706{
070a05af
CB
5707 int len;
5708 int fulllen = 0;
5709 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5710
5711 if (!retv)
5712 inlen = 0;
5713 else
5714 memset(retv, 0, inlen);
5715
9d4bf22d
CB
5716 if (!netdev)
5717 return -1;
5718
de4855a8 5719 if (netdev->name[0] != '\0')
9d4bf22d
CB
5720 strprint(retv, inlen, "%s", netdev->name);
5721
5722 return fulllen;
5723}
5724
f9373e40
CB
5725static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
5726 struct lxc_conf *c, void *data)
9d4bf22d 5727{
070a05af
CB
5728 int len;
5729 int fulllen = 0;
9d4bf22d 5730 const char *mode;
070a05af 5731 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5732
5733 if (!retv)
5734 inlen = 0;
5735 else
5736 memset(retv, 0, inlen);
5737
9d4bf22d
CB
5738 if (!netdev)
5739 return -1;
5740
5741 if (netdev->type != LXC_NET_MACVLAN)
5742 return 0;
5743
5744 switch (netdev->priv.macvlan_attr.mode) {
5745 case MACVLAN_MODE_PRIVATE:
5746 mode = "private";
5747 break;
5748 case MACVLAN_MODE_VEPA:
5749 mode = "vepa";
5750 break;
5751 case MACVLAN_MODE_BRIDGE:
5752 mode = "bridge";
5753 break;
5754 case MACVLAN_MODE_PASSTHRU:
5755 mode = "passthru";
5756 break;
5757 default:
5758 mode = "(invalid)";
5759 break;
5760 }
5761
5762 strprint(retv, inlen, "%s", mode);
5763
5764 return fulllen;
5765}
5766
c9f52382 5767static int get_config_net_ipvlan_mode(const char *key, char *retv, int inlen,
5768 struct lxc_conf *c, void *data)
5769{
5770 int len;
5771 int fulllen = 0;
5772 const char *mode;
5773 struct lxc_netdev *netdev = data;
5774
5775 if (!retv)
5776 inlen = 0;
5777 else
5778 memset(retv, 0, inlen);
5779
5780 if (!netdev)
21fce08c 5781 return ret_set_errno(-1, EINVAL);
c9f52382 5782
5783 if (netdev->type != LXC_NET_IPVLAN)
5784 return 0;
5785
5786 switch (netdev->priv.ipvlan_attr.mode) {
5787 case IPVLAN_MODE_L3:
5788 mode = "l3";
5789 break;
5790 case IPVLAN_MODE_L3S:
5791 mode = "l3s";
5792 break;
5793 case IPVLAN_MODE_L2:
5794 mode = "l2";
5795 break;
5796 default:
5797 mode = "(invalid)";
5798 break;
5799 }
5800
5801 strprint(retv, inlen, "%s", mode);
5802
5803 return fulllen;
5804}
5805
5806static int get_config_net_ipvlan_isolation(const char *key, char *retv, int inlen,
5807 struct lxc_conf *c, void *data)
5808{
5809 int len;
5810 int fulllen = 0;
5811 const char *mode;
5812 struct lxc_netdev *netdev = data;
5813
5814 if (!retv)
5815 inlen = 0;
5816 else
5817 memset(retv, 0, inlen);
5818
5819 if (!netdev)
21fce08c 5820 return ret_set_errno(-1, EINVAL);
c9f52382 5821
5822 if (netdev->type != LXC_NET_IPVLAN)
5823 return 0;
5824
5825 switch (netdev->priv.ipvlan_attr.isolation) {
5826 case IPVLAN_ISOLATION_BRIDGE:
5827 mode = "bridge";
5828 break;
5829 case IPVLAN_ISOLATION_PRIVATE:
5830 mode = "private";
5831 break;
5832 case IPVLAN_ISOLATION_VEPA:
5833 mode = "vepa";
5834 break;
5835 default:
5836 mode = "(invalid)";
5837 break;
5838 }
5839
5840 strprint(retv, inlen, "%s", mode);
5841
5842 return fulllen;
5843}
5844
3f0ed090
TP
5845static int get_config_net_veth_mode(const char *key, char *retv, int inlen,
5846 struct lxc_conf *c, void *data)
5847{
5848 int len;
5849 int fulllen = 0;
5850 const char *mode;
5851 struct lxc_netdev *netdev = data;
5852
5853 if (!retv)
5854 inlen = 0;
5855 else
5856 memset(retv, 0, inlen);
5857
5858 if (!netdev)
21fce08c 5859 return ret_set_errno(-1, EINVAL);
3f0ed090
TP
5860
5861 if (netdev->type != LXC_NET_VETH)
5862 return 0;
5863
5864 switch (netdev->priv.veth_attr.mode) {
5865 case VETH_MODE_BRIDGE:
5866 mode = "bridge";
5867 break;
5868 case VETH_MODE_ROUTER:
5869 mode = "router";
5870 break;
5871 default:
5872 mode = "(invalid)";
5873 break;
5874 }
5875
5876 strprint(retv, inlen, "%s", mode);
5877
5878 return fulllen;
5879}
5880
f9373e40
CB
5881static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
5882 struct lxc_conf *c, void *data)
9d4bf22d 5883{
070a05af
CB
5884 int len;
5885 int fulllen = 0;
5886 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5887
5888 if (!retv)
5889 inlen = 0;
5890 else
5891 memset(retv, 0, inlen);
5892
9d4bf22d
CB
5893 if (!netdev)
5894 return -1;
5895
5896 if (netdev->type != LXC_NET_VETH)
5897 return 0;
5898
5899 strprint(retv, inlen, "%s",
de4855a8
CB
5900 netdev->priv.veth_attr.pair[0] != '\0'
5901 ? netdev->priv.veth_attr.pair
5902 : netdev->priv.veth_attr.veth1);
9d4bf22d
CB
5903
5904 return fulllen;
5905}
5906
fdf76c6d
TP
5907static int get_config_net_veth_vlan_id(const char *key, char *retv, int inlen,
5908 struct lxc_conf *c, void *data)
5909{
5910 int len;
5911 int fulllen = 0;
5912 struct lxc_netdev *netdev = data;
5913
5914 if (!netdev)
5915 return ret_errno(EINVAL);
5916
5917 if (netdev->type != LXC_NET_VETH)
5918 return 0;
5919
5920 if (!retv)
5921 inlen = 0;
5922 else
5923 memset(retv, 0, inlen);
5924
5925 strprint(retv, inlen, "%d", netdev->priv.veth_attr.vlan_id);
5926
5927 return fulllen;
5928}
5929
b8e06d33
TP
5930static int get_config_net_veth_vlan_tagged_id(const char *key, char *retv, int inlen,
5931 struct lxc_conf *c, void *data)
5932{
5933 int len;
5934 size_t listlen;
5935 struct lxc_list *it;
5936 int fulllen = 0;
5937 struct lxc_netdev *netdev = data;
5938
5939 if (!netdev)
4e61b19d 5940 return ret_errno(EINVAL);
b8e06d33
TP
5941
5942 if (netdev->type != LXC_NET_VETH)
5943 return 0;
5944
5945 if (!retv)
5946 inlen = 0;
5947 else
5948 memset(retv, 0, inlen);
5949
5950 listlen = lxc_list_len(&netdev->priv.veth_attr.vlan_tagged_ids);
5951
5952 lxc_list_for_each(it, &netdev->priv.veth_attr.vlan_tagged_ids) {
5953 unsigned short i = PTR_TO_USHORT(it->elem);
5954 strprint(retv, inlen, "%u%s", i,
5955 (listlen-- > 1) ? "\n" : "");
5956 }
5957
5958 return fulllen;
5959}
5960
f9373e40
CB
5961static int get_config_net_script_up(const char *key, char *retv, int inlen,
5962 struct lxc_conf *c, void *data)
9d4bf22d 5963{
070a05af
CB
5964 int len;
5965 int fulllen = 0;
5966 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5967
5968 if (!retv)
5969 inlen = 0;
5970 else
5971 memset(retv, 0, inlen);
5972
9d4bf22d
CB
5973 if (!netdev)
5974 return -1;
5975
5976 if (netdev->upscript)
5977 strprint(retv, inlen, "%s", netdev->upscript);
5978
5979 return fulllen;
5980}
5981
f9373e40
CB
5982static int get_config_net_script_down(const char *key, char *retv, int inlen,
5983 struct lxc_conf *c, void *data)
9d4bf22d 5984{
070a05af
CB
5985 int len;
5986 int fulllen = 0;
5987 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5988
5989 if (!retv)
5990 inlen = 0;
5991 else
5992 memset(retv, 0, inlen);
5993
9d4bf22d
CB
5994 if (!netdev)
5995 return -1;
5996
5997 if (netdev->downscript)
5998 strprint(retv, inlen, "%s", netdev->downscript);
5999
6000 return fulllen;
6001}
6002
f9373e40
CB
6003static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
6004 struct lxc_conf *c, void *data)
9d4bf22d 6005{
070a05af
CB
6006 int len;
6007 int fulllen = 0;
6008 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6009
6010 if (!retv)
6011 inlen = 0;
6012 else
6013 memset(retv, 0, inlen);
6014
9d4bf22d
CB
6015 if (!netdev)
6016 return -1;
6017
6018 if (netdev->hwaddr)
6019 strprint(retv, inlen, "%s", netdev->hwaddr);
6020
6021 return fulllen;
6022}
6023
f9373e40
CB
6024static int get_config_net_mtu(const char *key, char *retv, int inlen,
6025 struct lxc_conf *c, void *data)
9d4bf22d 6026{
070a05af
CB
6027 int len;
6028 int fulllen = 0;
6029 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6030
6031 if (!retv)
6032 inlen = 0;
6033 else
6034 memset(retv, 0, inlen);
6035
9d4bf22d
CB
6036 if (!netdev)
6037 return -1;
6038
6039 if (netdev->mtu)
6040 strprint(retv, inlen, "%s", netdev->mtu);
6041
6042 return fulllen;
6043}
6044
f9373e40
CB
6045static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
6046 struct lxc_conf *c, void *data)
9d4bf22d 6047{
070a05af
CB
6048 int len;
6049 int fulllen = 0;
6050 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6051
6052 if (!retv)
6053 inlen = 0;
6054 else
6055 memset(retv, 0, inlen);
6056
9d4bf22d
CB
6057 if (!netdev)
6058 return -1;
6059
6060 if (netdev->type != LXC_NET_VLAN)
6061 return 0;
6062
6063 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
6064
6065 return fulllen;
6066}
6067
f9373e40
CB
6068static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
6069 struct lxc_conf *c, void *data)
9d4bf22d 6070{
070a05af 6071 int len;
9d4bf22d 6072 char buf[INET_ADDRSTRLEN];
070a05af
CB
6073 int fulllen = 0;
6074 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6075
6076 if (!retv)
6077 inlen = 0;
6078 else
6079 memset(retv, 0, inlen);
6080
9d4bf22d
CB
6081 if (!netdev)
6082 return -1;
6083
6084 if (netdev->ipv4_gateway_auto) {
6085 strprint(retv, inlen, "auto");
a2f9a670 6086 } else if (netdev->ipv4_gateway_dev) {
6087 strprint(retv, inlen, "dev");
9d4bf22d
CB
6088 } else if (netdev->ipv4_gateway) {
6089 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
6090 strprint(retv, inlen, "%s", buf);
6091 }
6092
6093 return fulllen;
6094}
6095
9ff60df2
CB
6096static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
6097 struct lxc_conf *c, void *data)
9d4bf22d 6098{
070a05af 6099 int len;
9d4bf22d
CB
6100 size_t listlen;
6101 char buf[INET_ADDRSTRLEN];
9d4bf22d 6102 struct lxc_list *it;
070a05af
CB
6103 int fulllen = 0;
6104 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6105
6106 if (!retv)
6107 inlen = 0;
6108 else
6109 memset(retv, 0, inlen);
6110
9d4bf22d
CB
6111 if (!netdev)
6112 return -1;
6113
6114 listlen = lxc_list_len(&netdev->ipv4);
47903908 6115
9d4bf22d
CB
6116 lxc_list_for_each(it, &netdev->ipv4) {
6117 struct lxc_inetdev *i = it->elem;
6118 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6fc7d8b6 6119 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
6120 (listlen-- > 1) ? "\n" : "");
6121 }
6122
6123 return fulllen;
6124}
6125
d4a7da46 6126static int get_config_net_veth_ipv4_route(const char *key, char *retv, int inlen,
6127 struct lxc_conf *c, void *data)
6128{
6129 int len;
6130 size_t listlen;
6131 char buf[INET_ADDRSTRLEN];
6132 struct lxc_list *it;
6133 int fulllen = 0;
6134 struct lxc_netdev *netdev = data;
6135
6136 if (!retv)
6137 inlen = 0;
6138 else
6139 memset(retv, 0, inlen);
6140
6141 if (!netdev)
21fce08c 6142 return ret_set_errno(-1, EINVAL);
d4a7da46 6143
6144 if (netdev->type != LXC_NET_VETH)
6145 return 0;
6146
6147 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv4_routes);
6148
6149 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv4_routes) {
6150 struct lxc_inetdev *i = it->elem;
6151 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6152 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6153 (listlen-- > 1) ? "\n" : "");
6154 }
6155
6156 return fulllen;
6157}
6158
f9373e40
CB
6159static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
6160 struct lxc_conf *c, void *data)
9d4bf22d 6161{
070a05af 6162 int len;
9d4bf22d 6163 char buf[INET6_ADDRSTRLEN];
070a05af
CB
6164 int fulllen = 0;
6165 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6166
6167 if (!retv)
6168 inlen = 0;
6169 else
6170 memset(retv, 0, inlen);
6171
9d4bf22d
CB
6172 if (!netdev)
6173 return -1;
6174
6175 if (netdev->ipv6_gateway_auto) {
6176 strprint(retv, inlen, "auto");
a2f9a670 6177 } else if (netdev->ipv6_gateway_dev) {
6178 strprint(retv, inlen, "dev");
9d4bf22d
CB
6179 } else if (netdev->ipv6_gateway) {
6180 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
6181 strprint(retv, inlen, "%s", buf);
6182 }
6183
6184 return fulllen;
6185}
6186
2e44ae28
CB
6187static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
6188 struct lxc_conf *c, void *data)
9d4bf22d 6189{
070a05af 6190 int len;
9d4bf22d
CB
6191 size_t listlen;
6192 char buf[INET6_ADDRSTRLEN];
9d4bf22d 6193 struct lxc_list *it;
070a05af
CB
6194 int fulllen = 0;
6195 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6196
6197 if (!retv)
6198 inlen = 0;
6199 else
6200 memset(retv, 0, inlen);
6201
9d4bf22d
CB
6202 if (!netdev)
6203 return -1;
6204
6205 listlen = lxc_list_len(&netdev->ipv6);
47903908 6206
9d4bf22d
CB
6207 lxc_list_for_each(it, &netdev->ipv6) {
6208 struct lxc_inet6dev *i = it->elem;
6209 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6fc7d8b6 6210 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
6211 (listlen-- > 1) ? "\n" : "");
6212 }
6213
6214 return fulllen;
6215}
cfc67626 6216
d4a7da46 6217static int get_config_net_veth_ipv6_route(const char *key, char *retv, int inlen,
6218 struct lxc_conf *c, void *data)
6219{
6220 int len;
6221 size_t listlen;
6222 char buf[INET6_ADDRSTRLEN];
6223 struct lxc_list *it;
6224 int fulllen = 0;
6225 struct lxc_netdev *netdev = data;
6226
6227 if (!retv)
6228 inlen = 0;
6229 else
6230 memset(retv, 0, inlen);
6231
6232 if (!netdev)
21fce08c 6233 return ret_set_errno(-1, EINVAL);
d4a7da46 6234
6235 if (netdev->type != LXC_NET_VETH)
6236 return 0;
6237
6238 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv6_routes);
6239
6240 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv6_routes) {
6241 struct lxc_inet6dev *i = it->elem;
6242 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6243 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6244 (listlen-- > 1) ? "\n" : "");
6245 }
6246
6247 return fulllen;
6248}
6249
cfc67626
CB
6250int lxc_list_config_items(char *retv, int inlen)
6251{
6252 size_t i;
6253 int len;
6254 int fulllen = 0;
6255
6256 if (!retv)
6257 inlen = 0;
6258 else
6259 memset(retv, 0, inlen);
6260
33eb2ec1
CB
6261 for (i = 0; i < config_jump_table_size; i++) {
6262 char *s = config_jump_table[i].name;
300df83e 6263
cfc67626
CB
6264 if (s[strlen(s) - 1] == '.')
6265 continue;
300df83e 6266
cfc67626
CB
6267 strprint(retv, inlen, "%s\n", s);
6268 }
6269
6270 return fulllen;
6271}
a5448c15 6272
fe9b7349
CB
6273int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
6274 int inlen)
6275{
6276 int len;
6277 int fulllen = 0;
6278
6279 if (!retv)
6280 inlen = 0;
6281 else
6282 memset(retv, 0, inlen);
6283
6284 if (!strcmp(key, "lxc.apparmor")) {
6285 strprint(retv, inlen, "allow_incomplete\n");
1800f924 6286 strprint(retv, inlen, "allow_nesting\n");
fe9b7349 6287 strprint(retv, inlen, "profile\n");
1800f924 6288 strprint(retv, inlen, "raw\n");
dae8c253
CB
6289 } else if (!strcmp(key, "lxc.cgroup")) {
6290 strprint(retv, inlen, "dir\n");
fe9b7349
CB
6291 } else if (!strcmp(key, "lxc.selinux")) {
6292 strprint(retv, inlen, "context\n");
4fef78bc 6293 strprint(retv, inlen, "context.keyring\n");
fe9b7349
CB
6294 } else if (!strcmp(key, "lxc.mount")) {
6295 strprint(retv, inlen, "auto\n");
6296 strprint(retv, inlen, "entry\n");
6297 strprint(retv, inlen, "fstab\n");
6298 } else if (!strcmp(key, "lxc.rootfs")) {
6299 strprint(retv, inlen, "mount\n");
6300 strprint(retv, inlen, "options\n");
6301 strprint(retv, inlen, "path\n");
6302 } else if (!strcmp(key, "lxc.uts")) {
6303 strprint(retv, inlen, "name\n");
6304 } else if (!strcmp(key, "lxc.hook")) {
6305 strprint(retv, inlen, "autodev\n");
63012bdd 6306 strprint(retv, inlen, "autodevtmpfssize\n");
fe9b7349
CB
6307 strprint(retv, inlen, "clone\n");
6308 strprint(retv, inlen, "destroy\n");
6309 strprint(retv, inlen, "mount\n");
6310 strprint(retv, inlen, "post-stop\n");
6311 strprint(retv, inlen, "pre-mount\n");
6312 strprint(retv, inlen, "pre-start\n");
08dd2805 6313 strprint(retv, inlen, "start-host\n");
fe9b7349
CB
6314 strprint(retv, inlen, "start\n");
6315 strprint(retv, inlen, "stop\n");
6316 } else if (!strcmp(key, "lxc.cap")) {
6317 strprint(retv, inlen, "drop\n");
6318 strprint(retv, inlen, "keep\n");
6319 } else if (!strcmp(key, "lxc.console")) {
6320 strprint(retv, inlen, "logfile\n");
6321 strprint(retv, inlen, "path\n");
6322 } else if (!strcmp(key, "lxc.seccomp")) {
6323 strprint(retv, inlen, "profile\n");
6324 } else if (!strcmp(key, "lxc.signal")) {
6325 strprint(retv, inlen, "halt\n");
6326 strprint(retv, inlen, "reboot\n");
6327 strprint(retv, inlen, "stop\n");
6328 } else if (!strcmp(key, "lxc.start")) {
6329 strprint(retv, inlen, "auto\n");
6330 strprint(retv, inlen, "delay\n");
6331 strprint(retv, inlen, "order\n");
6332 } else if (!strcmp(key, "lxc.monitor")) {
6333 strprint(retv, inlen, "unshare\n");
8f818a84
MB
6334 } else if (!strcmp(key, "lxc.keyring")) {
6335 strprint(retv, inlen, "session\n");
fe9b7349
CB
6336 } else {
6337 fulllen = -1;
6338 }
6339
6340 return fulllen;
6341}
6342
a5448c15
CB
6343int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
6344{
6345 int len;
6346 const char *idxstring;
a5448c15
CB
6347 struct lxc_netdev *netdev;
6348 int fulllen = 0;
6349 ssize_t idx = -1;
a5448c15
CB
6350
6351 idxstring = key + 8;
6352 if (!isdigit(*idxstring))
6353 return -1;
6354
7451daf8
CB
6355 (void)get_network_config_ops(key, c, &idx, NULL);
6356 if (idx < 0)
a5448c15
CB
6357 return -1;
6358
6359 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
a5448c15
CB
6360 if (!netdev)
6361 return -1;
6362
6363 if (!retv)
6364 inlen = 0;
6365 else
6366 memset(retv, 0, inlen);
6367
6368 strprint(retv, inlen, "type\n");
6369 strprint(retv, inlen, "script.up\n");
6370 strprint(retv, inlen, "script.down\n");
47903908 6371
a5448c15
CB
6372 if (netdev->type != LXC_NET_EMPTY) {
6373 strprint(retv, inlen, "flags\n");
6374 strprint(retv, inlen, "link\n");
6375 strprint(retv, inlen, "name\n");
6376 strprint(retv, inlen, "hwaddr\n");
6377 strprint(retv, inlen, "mtu\n");
6378 strprint(retv, inlen, "ipv6.address\n");
6379 strprint(retv, inlen, "ipv6.gateway\n");
6380 strprint(retv, inlen, "ipv4.address\n");
6381 strprint(retv, inlen, "ipv4.gateway\n");
6382 }
6383
6384 switch (netdev->type) {
6385 case LXC_NET_VETH:
6386 strprint(retv, inlen, "veth.pair\n");
d4a7da46 6387 strprint(retv, inlen, "veth.ipv4.route\n");
6388 strprint(retv, inlen, "veth.ipv6.route\n");
fdf76c6d 6389 strprint(retv, inlen, "veth.vlan.id\n");
a5448c15
CB
6390 break;
6391 case LXC_NET_MACVLAN:
6392 strprint(retv, inlen, "macvlan.mode\n");
6393 break;
c9f52382 6394 case LXC_NET_IPVLAN:
6395 strprint(retv, inlen, "ipvlan.mode\n");
6396 strprint(retv, inlen, "ipvlan.isolation\n");
6397 break;
a5448c15
CB
6398 case LXC_NET_VLAN:
6399 strprint(retv, inlen, "vlan.id\n");
6400 break;
6401 case LXC_NET_PHYS:
6402 break;
6403 }
6404
6405 return fulllen;
6406}