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