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