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