]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/confile.c
confile: cleanup set_config_signal_stop()
[mirror_lxc.git] / src / lxc / confile.c
CommitLineData
cc73685d 1/* SPDX-License-Identifier: LGPL-2.1+ */
70c1e708 2
d38dd64a
CB
3#ifndef _GNU_SOURCE
4#define _GNU_SOURCE 1
5#endif
70c1e708 6#define __STDC_FORMAT_MACROS
d38dd64a 7#include <arpa/inet.h>
70c1e708
CB
8#include <ctype.h>
9#include <dirent.h>
10#include <errno.h>
11#include <fcntl.h>
12#include <inttypes.h>
d38dd64a
CB
13#include <net/if.h>
14#include <netinet/in.h>
70c1e708 15#include <signal.h>
c2cc9f0a 16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
70c1e708 19#include <sys/param.h>
63376d7d 20#include <sys/stat.h>
c2cc9f0a 21#include <sys/types.h>
c2cc9f0a 22#include <sys/utsname.h>
d38dd64a
CB
23#include <syslog.h>
24#include <time.h>
25#include <unistd.h>
c2cc9f0a 26
86ce1da1 27#include "af_unix.h"
70c1e708 28#include "conf.h"
6ff05e18 29#include "config.h"
525f0002 30#include "confile.h"
0b843d35 31#include "confile_utils.h"
d38dd64a 32#include "../include/netns_ifaddrs.h"
f2363e38 33#include "log.h"
58e0f57d 34#include "lxcseccomp.h"
fdf76c6d 35#include "macro.h"
f01d0358 36#include "memory_utils.h"
70c1e708
CB
37#include "network.h"
38#include "parse.h"
28d832c4 39#include "storage.h"
70c1e708 40#include "utils.h"
36eb9bde 41
6ff05e18
SG
42#if HAVE_SYS_PERSONALITY_H
43#include <sys/personality.h>
44#endif
45
43f984ea
DJ
46#ifndef HAVE_STRLCPY
47#include "include/strlcpy.h"
48#endif
49
ebcd5140
DJ
50#ifndef HAVE_STRLCAT
51#include "include/strlcat.h"
52#endif
53
ac2cecc4 54lxc_log_define(confile, lxc);
576f946d 55
afeec9b7
CB
56#define lxc_config_define(name) \
57 __hot static int set_config_##name(const char *, const char *, \
58 struct lxc_conf *, void *); \
59 __hot static int get_config_##name(const char *, char *, int, \
60 struct lxc_conf *, void *); \
61 __hot static int clr_config_##name(const char *, struct lxc_conf *, \
62 void *);
71e287ca 63
63bab717 64lxc_config_define(autodev);
63012bdd 65lxc_config_define(autodev_tmpfs_size);
71e287ca 66lxc_config_define(apparmor_allow_incomplete);
1800f924 67lxc_config_define(apparmor_allow_nesting);
63bab717 68lxc_config_define(apparmor_profile);
1800f924 69lxc_config_define(apparmor_raw);
63bab717
CB
70lxc_config_define(cap_drop);
71lxc_config_define(cap_keep);
43654d34 72lxc_config_define(cgroup_controller);
54860ed0 73lxc_config_define(cgroup2_controller);
43654d34 74lxc_config_define(cgroup_dir);
a900cbaf 75lxc_config_define(cgroup_monitor_dir);
7696c1f9 76lxc_config_define(cgroup_monitor_pivot_dir);
a900cbaf
WB
77lxc_config_define(cgroup_container_dir);
78lxc_config_define(cgroup_container_inner_dir);
9caee129 79lxc_config_define(cgroup_relative);
28f3b1cd 80lxc_config_define(console_buffer_size);
861813e5 81lxc_config_define(console_logfile);
63bab717 82lxc_config_define(console_path);
861813e5
CB
83lxc_config_define(console_rotate);
84lxc_config_define(console_size);
63bab717
CB
85lxc_config_define(environment);
86lxc_config_define(ephemeral);
87lxc_config_define(execute_cmd);
88lxc_config_define(group);
89lxc_config_define(hooks);
44ae0fb6 90lxc_config_define(hooks_version);
71e287ca 91lxc_config_define(idmaps);
63bab717
CB
92lxc_config_define(includefiles);
93lxc_config_define(init_cmd);
3c491553 94lxc_config_define(init_cwd);
63bab717
CB
95lxc_config_define(init_gid);
96lxc_config_define(init_uid);
8f818a84 97lxc_config_define(keyring_session);
46cc906d 98lxc_config_define(log_file);
63bab717
CB
99lxc_config_define(log_level);
100lxc_config_define(log_syslog);
101lxc_config_define(monitor);
258f8051 102lxc_config_define(monitor_signal_pdeath);
71e287ca 103lxc_config_define(mount);
104lxc_config_define(mount_auto);
47148e96 105lxc_config_define(mount_fstab);
1d8d3676 106lxc_config_define(namespace_clone);
abeb5bba 107lxc_config_define(namespace_keep);
70fd7fc9
CB
108lxc_config_define(time_offset_boot);
109lxc_config_define(time_offset_monotonic);
b074bbf1 110lxc_config_define(namespace_share);
63bab717 111lxc_config_define(net);
71e287ca 112lxc_config_define(net_flags);
71e287ca 113lxc_config_define(net_hwaddr);
9ff60df2 114lxc_config_define(net_ipv4_address);
71e287ca 115lxc_config_define(net_ipv4_gateway);
2e44ae28 116lxc_config_define(net_ipv6_address);
71e287ca 117lxc_config_define(net_ipv6_gateway);
63bab717 118lxc_config_define(net_link);
6509154d 119lxc_config_define(net_l2proxy);
63bab717 120lxc_config_define(net_macvlan_mode);
c9f52382 121lxc_config_define(net_ipvlan_mode);
122lxc_config_define(net_ipvlan_isolation);
63bab717
CB
123lxc_config_define(net_mtu);
124lxc_config_define(net_name);
71e287ca 125lxc_config_define(net_nic);
63bab717
CB
126lxc_config_define(net_script_down);
127lxc_config_define(net_script_up);
128lxc_config_define(net_type);
3f0ed090 129lxc_config_define(net_veth_mode);
63bab717 130lxc_config_define(net_veth_pair);
d4a7da46 131lxc_config_define(net_veth_ipv4_route);
132lxc_config_define(net_veth_ipv6_route);
fdf76c6d 133lxc_config_define(net_veth_vlan_id);
b8e06d33 134lxc_config_define(net_veth_vlan_tagged_id);
63bab717
CB
135lxc_config_define(net_vlan_id);
136lxc_config_define(no_new_privs);
63bab717
CB
137lxc_config_define(personality);
138lxc_config_define(prlimit);
139lxc_config_define(pty_max);
6e54330c 140lxc_config_define(rootfs_managed);
63bab717
CB
141lxc_config_define(rootfs_mount);
142lxc_config_define(rootfs_options);
143lxc_config_define(rootfs_path);
0b427da0 144lxc_config_define(seccomp_profile);
50d86993 145lxc_config_define(seccomp_allow_nesting);
84cf6d25 146lxc_config_define(seccomp_notify_cookie);
86ce1da1 147lxc_config_define(seccomp_notify_proxy);
63bab717 148lxc_config_define(selinux_context);
4fef78bc 149lxc_config_define(selinux_context_keyring);
55c84efc 150lxc_config_define(signal_halt);
151lxc_config_define(signal_reboot);
152lxc_config_define(signal_stop);
71e287ca 153lxc_config_define(start);
63bab717
CB
154lxc_config_define(tty_max);
155lxc_config_define(tty_dir);
156lxc_config_define(uts_name);
7edd0540 157lxc_config_define(sysctl);
61d7a733 158lxc_config_define(proc);
c2cc9f0a 159
20c3318a
MB
160/*
161 * Important Note:
4110345b 162 * If a new config option is added to this table, be aware that
20c3318a
MB
163 * the order in which the options are places into the table matters.
164 * That means that more specific options of a namespace have to be
165 * placed above more generic ones.
166 *
167 * For instance: If lxc.ab is placed before lxc.ab.c, the config option
168 * lxc.ab.c will always be matched to lxc.ab. That is, the lxc.ab.c option
169 * has to be placed above lxc.ab.
170 */
33eb2ec1 171static struct lxc_config_t config_jump_table[] = {
70fd7fc9
CB
172 { "lxc.arch", set_config_personality, get_config_personality, clr_config_personality, },
173 { "lxc.apparmor.profile", set_config_apparmor_profile, get_config_apparmor_profile, clr_config_apparmor_profile, },
174 { "lxc.apparmor.allow_incomplete", set_config_apparmor_allow_incomplete, get_config_apparmor_allow_incomplete, clr_config_apparmor_allow_incomplete, },
175 { "lxc.apparmor.allow_nesting", set_config_apparmor_allow_nesting, get_config_apparmor_allow_nesting, clr_config_apparmor_allow_nesting, },
176 { "lxc.apparmor.raw", set_config_apparmor_raw, get_config_apparmor_raw, clr_config_apparmor_raw, },
177 { "lxc.autodev.tmpfs.size", set_config_autodev_tmpfs_size, get_config_autodev_tmpfs_size, clr_config_autodev_tmpfs_size, },
178 { "lxc.autodev", set_config_autodev, get_config_autodev, clr_config_autodev, },
179 { "lxc.cap.drop", set_config_cap_drop, get_config_cap_drop, clr_config_cap_drop, },
180 { "lxc.cap.keep", set_config_cap_keep, get_config_cap_keep, clr_config_cap_keep, },
181 { "lxc.cgroup2", set_config_cgroup2_controller, get_config_cgroup2_controller, clr_config_cgroup2_controller, },
7696c1f9 182 { "lxc.cgroup.dir.monitor.pivot", set_config_cgroup_monitor_pivot_dir, get_config_cgroup_monitor_pivot_dir, clr_config_cgroup_monitor_pivot_dir, },
70fd7fc9
CB
183 { "lxc.cgroup.dir.monitor", set_config_cgroup_monitor_dir, get_config_cgroup_monitor_dir, clr_config_cgroup_monitor_dir, },
184 { "lxc.cgroup.dir.container.inner", set_config_cgroup_container_inner_dir, get_config_cgroup_container_inner_dir, clr_config_cgroup_container_inner_dir, },
185 { "lxc.cgroup.dir.container", set_config_cgroup_container_dir, get_config_cgroup_container_dir, clr_config_cgroup_container_dir, },
186 { "lxc.cgroup.dir", set_config_cgroup_dir, get_config_cgroup_dir, clr_config_cgroup_dir, },
187 { "lxc.cgroup.relative", set_config_cgroup_relative, get_config_cgroup_relative, clr_config_cgroup_relative, },
188 { "lxc.cgroup", set_config_cgroup_controller, get_config_cgroup_controller, clr_config_cgroup_controller, },
189 { "lxc.console.buffer.size", set_config_console_buffer_size, get_config_console_buffer_size, clr_config_console_buffer_size, },
190 { "lxc.console.logfile", set_config_console_logfile, get_config_console_logfile, clr_config_console_logfile, },
191 { "lxc.console.path", set_config_console_path, get_config_console_path, clr_config_console_path, },
192 { "lxc.console.rotate", set_config_console_rotate, get_config_console_rotate, clr_config_console_rotate, },
193 { "lxc.console.size", set_config_console_size, get_config_console_size, clr_config_console_size, },
194 { "lxc.environment", set_config_environment, get_config_environment, clr_config_environment, },
195 { "lxc.ephemeral", set_config_ephemeral, get_config_ephemeral, clr_config_ephemeral, },
196 { "lxc.execute.cmd", set_config_execute_cmd, get_config_execute_cmd, clr_config_execute_cmd, },
197 { "lxc.group", set_config_group, get_config_group, clr_config_group, },
198 { "lxc.hook.autodev", set_config_hooks, get_config_hooks, clr_config_hooks, },
199 { "lxc.hook.clone", set_config_hooks, get_config_hooks, clr_config_hooks, },
200 { "lxc.hook.destroy", set_config_hooks, get_config_hooks, clr_config_hooks, },
201 { "lxc.hook.mount", set_config_hooks, get_config_hooks, clr_config_hooks, },
202 { "lxc.hook.post-stop", set_config_hooks, get_config_hooks, clr_config_hooks, },
203 { "lxc.hook.pre-mount", set_config_hooks, get_config_hooks, clr_config_hooks, },
204 { "lxc.hook.pre-start", set_config_hooks, get_config_hooks, clr_config_hooks, },
205 { "lxc.hook.start", set_config_hooks, get_config_hooks, clr_config_hooks, },
206 { "lxc.hook.start-host", set_config_hooks, get_config_hooks, clr_config_hooks, },
207 { "lxc.hook.stop", set_config_hooks, get_config_hooks, clr_config_hooks, },
208 { "lxc.hook.version", set_config_hooks_version, get_config_hooks_version, clr_config_hooks_version, },
209 { "lxc.hook", set_config_hooks, get_config_hooks, clr_config_hooks, },
210 { "lxc.idmap", set_config_idmaps, get_config_idmaps, clr_config_idmaps, },
211 { "lxc.include", set_config_includefiles, get_config_includefiles, clr_config_includefiles, },
212 { "lxc.init.cmd", set_config_init_cmd, get_config_init_cmd, clr_config_init_cmd, },
213 { "lxc.init.gid", set_config_init_gid, get_config_init_gid, clr_config_init_gid, },
214 { "lxc.init.uid", set_config_init_uid, get_config_init_uid, clr_config_init_uid, },
215 { "lxc.init.cwd", set_config_init_cwd, get_config_init_cwd, clr_config_init_cwd, },
216 { "lxc.keyring.session", set_config_keyring_session, get_config_keyring_session, clr_config_keyring_session },
217 { "lxc.log.file", set_config_log_file, get_config_log_file, clr_config_log_file, },
218 { "lxc.log.level", set_config_log_level, get_config_log_level, clr_config_log_level, },
219 { "lxc.log.syslog", set_config_log_syslog, get_config_log_syslog, clr_config_log_syslog, },
220 { "lxc.monitor.unshare", set_config_monitor, get_config_monitor, clr_config_monitor, },
221 { "lxc.monitor.signal.pdeath", set_config_monitor_signal_pdeath, get_config_monitor_signal_pdeath, clr_config_monitor_signal_pdeath, },
222 { "lxc.mount.auto", set_config_mount_auto, get_config_mount_auto, clr_config_mount_auto, },
223 { "lxc.mount.entry", set_config_mount, get_config_mount, clr_config_mount, },
224 { "lxc.mount.fstab", set_config_mount_fstab, get_config_mount_fstab, clr_config_mount_fstab, },
225 { "lxc.namespace.clone", set_config_namespace_clone, get_config_namespace_clone, clr_config_namespace_clone, },
226 { "lxc.namespace.keep", set_config_namespace_keep, get_config_namespace_keep, clr_config_namespace_keep, },
227 { "lxc.namespace.share", set_config_namespace_share, get_config_namespace_share, clr_config_namespace_share, },
228 { "lxc.time.offset.boot", set_config_time_offset_boot, get_config_time_offset_boot, clr_config_time_offset_boot, },
229 { "lxc.time.offset.monotonic", set_config_time_offset_monotonic, get_config_time_offset_monotonic, clr_config_time_offset_monotonic, },
230 { "lxc.net.flags", set_config_net_flags, get_config_net_flags, clr_config_net_flags, },
231 { "lxc.net.hwaddr", set_config_net_hwaddr, get_config_net_hwaddr, clr_config_net_hwaddr, },
232 { "lxc.net.ipv4.address", set_config_net_ipv4_address, get_config_net_ipv4_address, clr_config_net_ipv4_address, },
233 { "lxc.net.ipv4.gateway", set_config_net_ipv4_gateway, get_config_net_ipv4_gateway, clr_config_net_ipv4_gateway, },
234 { "lxc.net.ipv6.address", set_config_net_ipv6_address, get_config_net_ipv6_address, clr_config_net_ipv6_address, },
235 { "lxc.net.ipv6.gateway", set_config_net_ipv6_gateway, get_config_net_ipv6_gateway, clr_config_net_ipv6_gateway, },
236 { "lxc.net.link", set_config_net_link, get_config_net_link, clr_config_net_link, },
237 { "lxc.net.l2proxy", set_config_net_l2proxy, get_config_net_l2proxy, clr_config_net_l2proxy, },
238 { "lxc.net.macvlan.mode", set_config_net_macvlan_mode, get_config_net_macvlan_mode, clr_config_net_macvlan_mode, },
239 { "lxc.net.ipvlan.mode", set_config_net_ipvlan_mode, get_config_net_ipvlan_mode, clr_config_net_ipvlan_mode, },
240 { "lxc.net.ipvlan.isolation", set_config_net_ipvlan_isolation, get_config_net_ipvlan_isolation, clr_config_net_ipvlan_isolation, },
241 { "lxc.net.mtu", set_config_net_mtu, get_config_net_mtu, clr_config_net_mtu, },
242 { "lxc.net.name", set_config_net_name, get_config_net_name, clr_config_net_name, },
243 { "lxc.net.script.down", set_config_net_script_down, get_config_net_script_down, clr_config_net_script_down, },
244 { "lxc.net.script.up", set_config_net_script_up, get_config_net_script_up, clr_config_net_script_up, },
245 { "lxc.net.type", set_config_net_type, get_config_net_type, clr_config_net_type, },
246 { "lxc.net.vlan.id", set_config_net_vlan_id, get_config_net_vlan_id, clr_config_net_vlan_id, },
247 { "lxc.net.veth.mode", set_config_net_veth_mode, get_config_net_veth_mode, clr_config_net_veth_mode, },
248 { "lxc.net.veth.pair", set_config_net_veth_pair, get_config_net_veth_pair, clr_config_net_veth_pair, },
249 { "lxc.net.veth.ipv4.route", set_config_net_veth_ipv4_route, get_config_net_veth_ipv4_route, clr_config_net_veth_ipv4_route, },
250 { "lxc.net.veth.ipv6.route", set_config_net_veth_ipv6_route, get_config_net_veth_ipv6_route, clr_config_net_veth_ipv6_route, },
251 { "lxc.net.veth.vlan.id", set_config_net_veth_vlan_id, get_config_net_veth_vlan_id, clr_config_net_veth_vlan_id, },
252 { "lxc.net.veth.vlan.tagged.id", set_config_net_veth_vlan_tagged_id, get_config_net_veth_vlan_tagged_id, clr_config_net_veth_vlan_tagged_id, },
253 { "lxc.net.", set_config_net_nic, get_config_net_nic, clr_config_net_nic, },
254 { "lxc.net", set_config_net, get_config_net, clr_config_net, },
255 { "lxc.no_new_privs", set_config_no_new_privs, get_config_no_new_privs, clr_config_no_new_privs, },
256 { "lxc.prlimit", set_config_prlimit, get_config_prlimit, clr_config_prlimit, },
257 { "lxc.pty.max", set_config_pty_max, get_config_pty_max, clr_config_pty_max, },
258 { "lxc.rootfs.managed", set_config_rootfs_managed, get_config_rootfs_managed, clr_config_rootfs_managed, },
259 { "lxc.rootfs.mount", set_config_rootfs_mount, get_config_rootfs_mount, clr_config_rootfs_mount, },
260 { "lxc.rootfs.options", set_config_rootfs_options, get_config_rootfs_options, clr_config_rootfs_options, },
261 { "lxc.rootfs.path", set_config_rootfs_path, get_config_rootfs_path, clr_config_rootfs_path, },
262 { "lxc.seccomp.allow_nesting", set_config_seccomp_allow_nesting, get_config_seccomp_allow_nesting, clr_config_seccomp_allow_nesting, },
263 { "lxc.seccomp.notify.cookie", set_config_seccomp_notify_cookie, get_config_seccomp_notify_cookie, clr_config_seccomp_notify_cookie, },
264 { "lxc.seccomp.notify.proxy", set_config_seccomp_notify_proxy, get_config_seccomp_notify_proxy, clr_config_seccomp_notify_proxy, },
265 { "lxc.seccomp.profile", set_config_seccomp_profile, get_config_seccomp_profile, clr_config_seccomp_profile, },
266 { "lxc.selinux.context.keyring", set_config_selinux_context_keyring, get_config_selinux_context_keyring, clr_config_selinux_context_keyring },
267 { "lxc.selinux.context", set_config_selinux_context, get_config_selinux_context, clr_config_selinux_context, },
268 { "lxc.signal.halt", set_config_signal_halt, get_config_signal_halt, clr_config_signal_halt, },
269 { "lxc.signal.reboot", set_config_signal_reboot, get_config_signal_reboot, clr_config_signal_reboot, },
270 { "lxc.signal.stop", set_config_signal_stop, get_config_signal_stop, clr_config_signal_stop, },
271 { "lxc.start.auto", set_config_start, get_config_start, clr_config_start, },
272 { "lxc.start.delay", set_config_start, get_config_start, clr_config_start, },
273 { "lxc.start.order", set_config_start, get_config_start, clr_config_start, },
274 { "lxc.tty.dir", set_config_tty_dir, get_config_tty_dir, clr_config_tty_dir, },
275 { "lxc.tty.max", set_config_tty_max, get_config_tty_max, clr_config_tty_max, },
276 { "lxc.uts.name", set_config_uts_name, get_config_uts_name, clr_config_uts_name, },
277 { "lxc.sysctl", set_config_sysctl, get_config_sysctl, clr_config_sysctl, },
278 { "lxc.proc", set_config_proc, get_config_proc, clr_config_proc, },
a84b9932
AV
279};
280
33eb2ec1 281static const size_t config_jump_table_size = sizeof(config_jump_table) / sizeof(struct lxc_config_t);
c2cc9f0a 282
6eb516a7
RJ
283struct lxc_config_t *lxc_get_config_exact(const char *key)
284{
285 size_t i;
286
287 for (i = 0; i < config_jump_table_size; i++)
288 if (!strcmp(config_jump_table[i].name, key))
289 return &config_jump_table[i];
290
291 return NULL;
292}
293
294
300df83e 295struct lxc_config_t *lxc_get_config(const char *key)
c2cc9f0a 296{
84760c11 297 size_t i;
c2cc9f0a 298
33eb2ec1
CB
299 for (i = 0; i < config_jump_table_size; i++)
300 if (!strncmp(config_jump_table[i].name, key, strlen(config_jump_table[i].name)))
301 return &config_jump_table[i];
300df83e 302
c2cc9f0a 303 return NULL;
304}
305
f9373e40
CB
306static int set_config_net(const char *key, const char *value,
307 struct lxc_conf *lxc_conf, void *data)
6b0d5538 308{
663e9916 309 if (!lxc_config_value_empty(value)) {
f9373e40 310 ERROR("lxc.net must not have a value");
6b0d5538
SH
311 return -1;
312 }
313
f9373e40 314 return clr_config_net(key, lxc_conf, data);
6b0d5538
SH
315}
316
f9373e40
CB
317static int set_config_net_type(const char *key, const char *value,
318 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 319{
070a05af 320 struct lxc_netdev *netdev = data;
c2cc9f0a 321
663e9916 322 if (lxc_config_value_empty(value))
f9373e40 323 return clr_config_net_type(key, lxc_conf, data);
c302b476 324
bbc079cf 325 if (!netdev)
059a1ec3 326 return ret_errno(EINVAL);
c2cc9f0a 327
c9f52382 328 if (strcmp(value, "veth") == 0) {
24654103 329 netdev->type = LXC_NET_VETH;
d4a7da46 330 lxc_list_init(&netdev->priv.veth_attr.ipv4_routes);
331 lxc_list_init(&netdev->priv.veth_attr.ipv6_routes);
b8e06d33 332 lxc_list_init(&netdev->priv.veth_attr.vlan_tagged_ids);
756cadb6
CB
333 if (!lxc_veth_flag_to_mode(netdev->priv.veth_attr.mode))
334 lxc_veth_mode_to_flag(&netdev->priv.veth_attr.mode, "bridge");
c9f52382 335 } else if (strcmp(value, "macvlan") == 0) {
24654103 336 netdev->type = LXC_NET_MACVLAN;
756cadb6
CB
337 if (!lxc_macvlan_flag_to_mode(netdev->priv.veth_attr.mode))
338 lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, "private");
c9f52382 339 } else if (strcmp(value, "ipvlan") == 0) {
340 netdev->type = LXC_NET_IPVLAN;
756cadb6
CB
341 if (!lxc_ipvlan_flag_to_mode(netdev->priv.ipvlan_attr.mode))
342 lxc_ipvlan_mode_to_flag(&netdev->priv.ipvlan_attr.mode, "l3");
343 if (!lxc_ipvlan_flag_to_isolation(netdev->priv.ipvlan_attr.isolation))
344 lxc_ipvlan_isolation_to_flag(&netdev->priv.ipvlan_attr.isolation, "bridge");
c9f52382 345 } else if (strcmp(value, "vlan") == 0) {
24654103 346 netdev->type = LXC_NET_VLAN;
c9f52382 347 } else if (strcmp(value, "phys") == 0) {
24654103 348 netdev->type = LXC_NET_PHYS;
c9f52382 349 } else if (strcmp(value, "empty") == 0) {
24654103 350 netdev->type = LXC_NET_EMPTY;
c9f52382 351 } else if (strcmp(value, "none") == 0) {
26b797f3 352 netdev->type = LXC_NET_NONE;
bbc079cf 353 } else {
059a1ec3 354 return log_error(-1, "Invalid network type %s", value);
c2cc9f0a 355 }
bbc079cf 356
c2cc9f0a 357 return 0;
358}
359
f9373e40
CB
360static int set_config_net_flags(const char *key, const char *value,
361 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 362{
070a05af 363 struct lxc_netdev *netdev = data;
c2cc9f0a 364
9d4bf22d 365 if (lxc_config_value_empty(value))
f9373e40 366 return clr_config_net_flags(key, lxc_conf, data);
9d4bf22d 367
33c945e0 368 if (!netdev)
059a1ec3 369 return ret_errno(EINVAL);
c2cc9f0a 370
33c945e0 371 netdev->flags |= IFF_UP;
c2cc9f0a 372
33c945e0
MT
373 return 0;
374}
375
b45e32f9 376static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf,
d5aba460 377 struct lxc_netdev *netdev)
576400e5 378{
c4ef8f4c
CB
379 call_cleaner(netns_freeifaddrs) struct netns_ifaddrs *ifaddr = NULL;
380 struct netns_ifaddrs *ifa;
504a2217
CB
381 int n;
382 int ret = 0;
f9373e40
CB
383 const char *type_key = "lxc.net.type";
384 const char *link_key = "lxc.net.link";
576400e5 385 const char *tmpvalue = "phys";
576400e5 386
c4ef8f4c
CB
387 if (netns_getifaddrs(&ifaddr, -1, &(bool){false}) < 0)
388 return log_error_errno(-1, errno, "Failed to get network interfaces");
576400e5 389
390 for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) {
391 if (!ifa->ifa_addr)
392 continue;
47903908 393
576400e5 394 if (ifa->ifa_addr->sa_family != AF_PACKET)
395 continue;
396
504a2217 397 if (!strncmp(value, ifa->ifa_name, strlen(value) - 1)) {
f9373e40
CB
398 ret = set_config_net_type(type_key, tmpvalue, lxc_conf,
399 netdev);
576400e5 400 if (!ret) {
f9373e40 401 ret = set_config_net_link(
c302b476 402 link_key, ifa->ifa_name, lxc_conf, netdev);
576400e5 403 if (ret) {
47903908 404 ERROR("Failed to create matched ifnames");
576400e5 405 break;
406 }
407 } else {
47903908 408 ERROR("Failed to create matched ifnames");
576400e5 409 break;
410 }
411 }
412 }
413
576400e5 414 return ret;
415}
416
f9373e40
CB
417static int set_config_net_link(const char *key, const char *value,
418 struct lxc_conf *lxc_conf, void *data)
33c945e0 419{
070a05af 420 struct lxc_netdev *netdev = data;
576400e5 421 int ret = 0;
33c945e0 422
6bed0fb6 423 if (lxc_config_value_empty(value))
f9373e40 424 return clr_config_net_link(key, lxc_conf, data);
6bed0fb6 425
33c945e0 426 if (!netdev)
059a1ec3 427 return ret_errno(EINVAL);
c2cc9f0a 428
b45e32f9
CB
429 if (value[strlen(value) - 1] == '+' && netdev->type == LXC_NET_PHYS)
430 ret = create_matched_ifnames(value, lxc_conf, netdev);
431 else
18cd4b54 432 ret = network_ifname(netdev->link, value, sizeof(netdev->link));
576400e5 433
434 return ret;
c2cc9f0a 435}
436
6509154d 437static int set_config_net_l2proxy(const char *key, const char *value,
438 struct lxc_conf *lxc_conf, void *data)
439{
440 struct lxc_netdev *netdev = data;
441 unsigned int val = 0;
442 int ret;
443
444 if (lxc_config_value_empty(value))
445 return clr_config_net_l2proxy(key, lxc_conf, data);
446
447 if (!netdev)
059a1ec3 448 return ret_errno(EINVAL);
6509154d 449
450 ret = lxc_safe_uint(value, &val);
451 if (ret < 0)
059a1ec3 452 return ret_errno(-ret);
6509154d 453
454 switch (val) {
455 case 0:
456 netdev->l2proxy = false;
457 return 0;
458 case 1:
459 netdev->l2proxy = true;
460 return 0;
461 }
462
21fce08c 463 return ret_set_errno(-1, EINVAL);
6509154d 464}
465
f9373e40
CB
466static int set_config_net_name(const char *key, const char *value,
467 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 468{
070a05af 469 struct lxc_netdev *netdev = data;
c2cc9f0a 470
6bed0fb6 471 if (lxc_config_value_empty(value))
f9373e40 472 return clr_config_net_name(key, lxc_conf, data);
6bed0fb6 473
33c945e0 474 if (!netdev)
059a1ec3 475 return ret_errno(EINVAL);
c2cc9f0a 476
18cd4b54 477 return network_ifname(netdev->name, value, sizeof(netdev->name));
33c945e0
MT
478}
479
3f0ed090
TP
480
481static int set_config_net_veth_mode(const char *key, const char *value,
482 struct lxc_conf *lxc_conf, void *data)
483{
484 struct lxc_netdev *netdev = data;
485
486 if (lxc_config_value_empty(value))
487 return clr_config_net_veth_mode(key, lxc_conf, data);
488
489 if (!netdev)
059a1ec3 490 return ret_errno(EINVAL);
3f0ed090
TP
491
492 return lxc_veth_mode_to_flag(&netdev->priv.veth_attr.mode, value);
493}
494
f9373e40
CB
495static int set_config_net_veth_pair(const char *key, const char *value,
496 struct lxc_conf *lxc_conf, void *data)
e892973e 497{
070a05af 498 struct lxc_netdev *netdev = data;
e892973e 499
6bed0fb6 500 if (lxc_config_value_empty(value))
f9373e40 501 return clr_config_net_veth_pair(key, lxc_conf, data);
6bed0fb6 502
e892973e 503 if (!netdev)
059a1ec3 504 return ret_errno(EINVAL);
e892973e 505
059a1ec3
CB
506 return network_ifname(netdev->priv.veth_attr.pair, value,
507 sizeof(netdev->priv.veth_attr.pair));
e892973e
DL
508}
509
fdf76c6d
TP
510static int set_config_net_veth_vlan_id(const char *key, const char *value,
511 struct lxc_conf *lxc_conf, void *data)
512{
513 int ret;
514 struct lxc_netdev *netdev = data;
515
516 if (!netdev)
517 return ret_errno(EINVAL);
518
519 if (lxc_config_value_empty(value))
520 return clr_config_net_veth_vlan_id(key, lxc_conf, data);
521
522 if (strcmp(value, "none") == 0) {
523 netdev->priv.veth_attr.vlan_id = BRIDGE_VLAN_NONE;
524 } else {
525 unsigned short vlan_id;
526 ret = get_u16(&vlan_id, value, 0);
527 if (ret < 0)
528 return ret_errno(EINVAL);
529
530 if (vlan_id > BRIDGE_VLAN_ID_MAX)
531 return ret_errno(EINVAL);
532
533 netdev->priv.veth_attr.vlan_id = vlan_id;
534 }
535
536 netdev->priv.veth_attr.vlan_id_set = true;
537 return 0;
538}
539
b8e06d33
TP
540static int set_config_net_veth_vlan_tagged_id(const char *key, const char *value,
541 struct lxc_conf *lxc_conf, void *data)
542{
543 __do_free struct lxc_list *list = NULL;
544 int ret;
545 unsigned short vlan_id;
546 struct lxc_netdev *netdev = data;
547
548 if (!netdev)
549 return ret_errno(EINVAL);
550
551 if (lxc_config_value_empty(value))
552 return clr_config_net_veth_vlan_tagged_id(key, lxc_conf, data);
553
554 ret = get_u16(&vlan_id, value, 0);
555 if (ret < 0)
556 ret_errno(EINVAL);
557
558 if (vlan_id > BRIDGE_VLAN_ID_MAX)
559 ret_errno(EINVAL);
560
561 list = malloc(sizeof(*list));
562 if (!list)
563 return ret_errno(ENOMEM);
564
565 lxc_list_init(list);
566 list->elem = UINT_TO_PTR(vlan_id);
567
568 lxc_list_add_tail(&netdev->priv.veth_attr.vlan_tagged_ids, move_ptr(list));
569
570 return 0;
571}
572
f9373e40
CB
573static int set_config_net_macvlan_mode(const char *key, const char *value,
574 struct lxc_conf *lxc_conf, void *data)
8634bc19 575{
070a05af 576 struct lxc_netdev *netdev = data;
8634bc19 577
6bed0fb6 578 if (lxc_config_value_empty(value))
f9373e40 579 return clr_config_net_macvlan_mode(key, lxc_conf, data);
6bed0fb6 580
8634bc19 581 if (!netdev)
059a1ec3 582 return ret_errno(EINVAL);
8634bc19 583
9b0df30f 584 return lxc_macvlan_mode_to_flag(&netdev->priv.macvlan_attr.mode, value);
8634bc19
MT
585}
586
c9f52382 587static int set_config_net_ipvlan_mode(const char *key, const char *value,
588 struct lxc_conf *lxc_conf, void *data)
589{
590 struct lxc_netdev *netdev = data;
591
592 if (lxc_config_value_empty(value))
593 return clr_config_net_ipvlan_mode(key, lxc_conf, data);
594
595 if (!netdev)
059a1ec3 596 return ret_errno(EINVAL);
c9f52382 597
059a1ec3
CB
598 if (netdev->type != LXC_NET_IPVLAN)
599 return log_error_errno(-EINVAL,
600 EINVAL, "Invalid ipvlan mode \"%s\", can only be used with ipvlan network",
601 value);
c9f52382 602
603 return lxc_ipvlan_mode_to_flag(&netdev->priv.ipvlan_attr.mode, value);
604}
605
606static int set_config_net_ipvlan_isolation(const char *key, const char *value,
607 struct lxc_conf *lxc_conf, void *data)
608{
609 struct lxc_netdev *netdev = data;
610
611 if (lxc_config_value_empty(value))
612 return clr_config_net_ipvlan_isolation(key, lxc_conf, data);
613
614 if (!netdev)
059a1ec3 615 return ret_errno(EINVAL);
c9f52382 616
059a1ec3
CB
617 if (netdev->type != LXC_NET_IPVLAN)
618 return log_error_errno(-EINVAL,
619 EINVAL, "Invalid ipvlan isolation \"%s\", can only be used with ipvlan network",
620 value);
c9f52382 621
622 return lxc_ipvlan_isolation_to_flag(&netdev->priv.ipvlan_attr.isolation, value);
623}
624
f9373e40
CB
625static int set_config_net_hwaddr(const char *key, const char *value,
626 struct lxc_conf *lxc_conf, void *data)
33c945e0 627{
070a05af 628 struct lxc_netdev *netdev = data;
504a2217 629 char *new_value;
33c945e0 630
6bed0fb6 631 if (lxc_config_value_empty(value))
f9373e40 632 return clr_config_net_hwaddr(key, lxc_conf, data);
6bed0fb6 633
ecbb3790 634 if (!netdev)
059a1ec3 635 return ret_errno(EINVAL);
ecbb3790 636
504a2217 637 new_value = strdup(value);
d5aba460 638 if (!new_value)
059a1ec3 639 return ret_errno(ENOMEM);
d5aba460 640
508c263e 641 rand_complete_hwaddr(new_value);
c2cc9f0a 642
663e9916 643 if (lxc_config_value_empty(new_value)) {
508c263e
SH
644 free(new_value);
645 netdev->hwaddr = NULL;
646 return 0;
647 }
648
649 netdev->hwaddr = new_value;
47903908 650
508c263e 651 return 0;
c2cc9f0a 652}
653
f9373e40
CB
654static int set_config_net_vlan_id(const char *key, const char *value,
655 struct lxc_conf *lxc_conf, void *data)
26c39028 656{
d5aba460 657 int ret;
070a05af 658 struct lxc_netdev *netdev = data;
26c39028 659
6bed0fb6 660 if (lxc_config_value_empty(value))
f9373e40 661 return clr_config_net_vlan_id(key, lxc_conf, data);
6bed0fb6 662
26c39028 663 if (!netdev)
059a1ec3 664 return ret_errno(EINVAL);
26c39028 665
d5aba460
CB
666 ret = get_u16(&netdev->priv.vlan_attr.vid, value, 0);
667 if (ret < 0)
059a1ec3 668 return ret;
26c39028
JHS
669
670 return 0;
671}
672
f9373e40
CB
673static int set_config_net_mtu(const char *key, const char *value,
674 struct lxc_conf *lxc_conf, void *data)
442cbbe6 675{
070a05af 676 struct lxc_netdev *netdev = data;
442cbbe6 677
6bed0fb6 678 if (lxc_config_value_empty(value))
f9373e40 679 return clr_config_net_mtu(key, lxc_conf, data);
6bed0fb6 680
33c945e0 681 if (!netdev)
059a1ec3 682 return ret_errno(EINVAL);
442cbbe6 683
713046e3 684 return set_config_string_item(&netdev->mtu, value);
442cbbe6
TR
685}
686
9ff60df2
CB
687static int set_config_net_ipv4_address(const char *key, const char *value,
688 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 689{
059a1ec3
CB
690 __do_free char *addr = NULL;
691 __do_free struct lxc_inetdev *inetdev = NULL;
692 __do_free struct lxc_list *list = NULL;
d5aba460 693 int ret;
070a05af 694 struct lxc_netdev *netdev = data;
504a2217 695 char *cursor, *slash;
059a1ec3 696 char *bcast = NULL, *prefix = NULL;
c2cc9f0a 697
663e9916 698 if (lxc_config_value_empty(value))
9ff60df2 699 return clr_config_net_ipv4_address(key, lxc_conf, data);
0797e123 700
33c945e0 701 if (!netdev)
059a1ec3 702 return ret_errno(EINVAL);
c2cc9f0a 703
704 inetdev = malloc(sizeof(*inetdev));
d5aba460 705 if (!inetdev)
059a1ec3 706 return ret_errno(ENOMEM);
c2cc9f0a 707 memset(inetdev, 0, sizeof(*inetdev));
708
709 list = malloc(sizeof(*list));
059a1ec3
CB
710 if (!list)
711 return ret_errno(ENOMEM);
c2cc9f0a 712
713 lxc_list_init(list);
c2cc9f0a 714
956edc54 715 addr = strdup(value);
059a1ec3
CB
716 if (!addr)
717 return ret_errno(ENOMEM);
c2cc9f0a 718
719 cursor = strstr(addr, " ");
720 if (cursor) {
721 *cursor = '\0';
722 bcast = cursor + 1;
723 }
724
725 slash = strstr(addr, "/");
726 if (slash) {
727 *slash = '\0';
728 prefix = slash + 1;
729 }
730
d5aba460 731 ret = inet_pton(AF_INET, addr, &inetdev->addr);
059a1ec3
CB
732 if (!ret || ret < 0)
733 return log_error_errno(-1, errno, "Invalid ipv4 address \"%s\"", value);
c2cc9f0a 734
d5aba460
CB
735 if (bcast) {
736 ret = inet_pton(AF_INET, bcast, &inetdev->bcast);
059a1ec3
CB
737 if (!ret || ret < 0)
738 return log_error_errno(-1, errno, "Invalid ipv4 broadcast address \"%s\"", value);
d5aba460 739
0093bb8c 740 }
c2cc9f0a 741
504a2217 742 /* No prefix specified, determine it from the network class. */
1c633398 743 if (prefix) {
d5aba460 744 ret = lxc_safe_uint(prefix, &inetdev->prefix);
059a1ec3
CB
745 if (ret < 0)
746 return ret;
1c633398
CB
747 } else {
748 inetdev->prefix = config_ip_prefix(&inetdev->addr);
749 }
a059591e 750
504a2217
CB
751 /* If no broadcast address, let compute one from the
752 * prefix and address.
0093bb8c
DL
753 */
754 if (!bcast) {
1b7d4743 755 inetdev->bcast.s_addr = inetdev->addr.s_addr;
d5aba460 756 inetdev->bcast.s_addr |= htonl(INADDR_BROADCAST >> inetdev->prefix);
0093bb8c 757 }
c2cc9f0a 758
059a1ec3 759 list->elem = inetdev;
8538f388 760 lxc_list_add_tail(&netdev->ipv4, list);
059a1ec3
CB
761 move_ptr(inetdev);
762 move_ptr(list);
47903908 763
c2cc9f0a 764 return 0;
765}
766
f9373e40
CB
767static int set_config_net_ipv4_gateway(const char *key, const char *value,
768 struct lxc_conf *lxc_conf, void *data)
f8fee0e2 769{
070a05af 770 struct lxc_netdev *netdev = data;
f8fee0e2 771
6bed0fb6 772 if (lxc_config_value_empty(value))
f9373e40 773 return clr_config_net_ipv4_gateway(key, lxc_conf, data);
6bed0fb6 774
f8fee0e2
MK
775 if (!netdev)
776 return -1;
777
e088e926 778 free(netdev->ipv4_gateway);
f8fee0e2 779
a2f9a670 780 if (strcmp(value, "auto") == 0) {
19a26f82
MK
781 netdev->ipv4_gateway = NULL;
782 netdev->ipv4_gateway_auto = true;
a2f9a670 783 } else if (strcmp(value, "dev") == 0) {
784 netdev->ipv4_gateway = NULL;
785 netdev->ipv4_gateway_auto = false;
786 netdev->ipv4_gateway_dev = true;
19a26f82 787 } else {
059a1ec3 788 __do_free struct in_addr *gw = NULL;
25a908b8 789 int ret;
e088e926
SG
790
791 gw = malloc(sizeof(*gw));
25a908b8 792 if (!gw)
059a1ec3 793 return ret_errno(ENOMEM);
e088e926 794
25a908b8 795 ret = inet_pton(AF_INET, value, gw);
059a1ec3
CB
796 if (!ret || ret < 0)
797 return log_error_errno(-1, errno, "Invalid ipv4 gateway address \"%s\"", value);
19a26f82 798
059a1ec3 799 netdev->ipv4_gateway = move_ptr(gw);
19a26f82 800 netdev->ipv4_gateway_auto = false;
f8fee0e2
MK
801 }
802
f8fee0e2
MK
803 return 0;
804}
805
d4a7da46 806static int set_config_net_veth_ipv4_route(const char *key, const char *value,
807 struct lxc_conf *lxc_conf, void *data)
808{
809 __do_free char *valdup = NULL;
810 __do_free struct lxc_inetdev *inetdev = NULL;
811 __do_free struct lxc_list *list = NULL;
812 int ret;
813 char *netmask, *slash;
814 struct lxc_netdev *netdev = data;
815
816 if (lxc_config_value_empty(value))
817 return clr_config_net_veth_ipv4_route(key, lxc_conf, data);
818
819 if (!netdev)
059a1ec3 820 return ret_errno(EINVAL);
d4a7da46 821
059a1ec3
CB
822 if (netdev->type != LXC_NET_VETH)
823 return log_error_errno(-EINVAL,
824 EINVAL, "Invalid ipv4 route \"%s\", can only be used with veth network",
825 value);
d4a7da46 826
827 inetdev = malloc(sizeof(*inetdev));
828 if (!inetdev)
059a1ec3 829 return ret_errno(ENOMEM);
d4a7da46 830 memset(inetdev, 0, sizeof(*inetdev));
831
832 list = malloc(sizeof(*list));
833 if (!list)
059a1ec3 834 return ret_errno(ENOMEM);
d4a7da46 835
836 lxc_list_init(list);
837 list->elem = inetdev;
838
839 valdup = strdup(value);
840 if (!valdup)
059a1ec3 841 return ret_errno(ENOMEM);
d4a7da46 842
843 slash = strchr(valdup, '/');
844 if (!slash)
059a1ec3 845 return ret_errno(EINVAL);
d4a7da46 846
847 *slash = '\0';
848 slash++;
849 if (*slash == '\0')
059a1ec3 850 return ret_errno(EINVAL);
d4a7da46 851
852 netmask = slash;
853
854 ret = lxc_safe_uint(netmask, &inetdev->prefix);
855 if (ret < 0 || inetdev->prefix > 32)
059a1ec3 856 return ret_errno(EINVAL);
d4a7da46 857
858 ret = inet_pton(AF_INET, valdup, &inetdev->addr);
859 if (!ret || ret < 0)
059a1ec3 860 return ret_errno(EINVAL);
d4a7da46 861
862 lxc_list_add_tail(&netdev->priv.veth_attr.ipv4_routes, list);
863 move_ptr(inetdev);
864 move_ptr(list);
865
866 return 0;
867}
868
2e44ae28
CB
869static int set_config_net_ipv6_address(const char *key, const char *value,
870 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 871{
059a1ec3
CB
872 __do_free char *valdup = NULL;
873 __do_free struct lxc_inet6dev *inet6dev = NULL;
874 __do_free struct lxc_list *list = NULL;
25a908b8 875 int ret;
070a05af 876 struct lxc_netdev *netdev = data;
059a1ec3 877 char *slash, *netmask;
c2cc9f0a 878
6bed0fb6 879 if (lxc_config_value_empty(value))
2e44ae28 880 return clr_config_net_ipv6_address(key, lxc_conf, data);
6bed0fb6 881
33c945e0 882 if (!netdev)
059a1ec3 883 return ret_errno(EINVAL);
c2cc9f0a 884
885 inet6dev = malloc(sizeof(*inet6dev));
25a908b8 886 if (!inet6dev)
059a1ec3 887 return ret_errno(ENOMEM);
c2cc9f0a 888 memset(inet6dev, 0, sizeof(*inet6dev));
889
890 list = malloc(sizeof(*list));
059a1ec3
CB
891 if (!list)
892 return ret_errno(ENOMEM);
c2cc9f0a 893
894 lxc_list_init(list);
c2cc9f0a 895
956edc54 896 valdup = strdup(value);
059a1ec3
CB
897 if (!valdup)
898 return ret_errno(ENOMEM);
956edc54 899
a059591e 900 inet6dev->prefix = 64;
12a50cc6 901 slash = strstr(valdup, "/");
c2cc9f0a 902 if (slash) {
903 *slash = '\0';
904 netmask = slash + 1;
47903908 905
f54f8d0b 906 ret = lxc_safe_uint(netmask, &inet6dev->prefix);
059a1ec3
CB
907 if (ret < 0)
908 return ret;
c2cc9f0a 909 }
910
25a908b8 911 ret = inet_pton(AF_INET6, valdup, &inet6dev->addr);
059a1ec3
CB
912 if (!ret || ret < 0)
913 return log_error_errno(-EINVAL, EINVAL, "Invalid ipv6 address \"%s\"", valdup);
c2cc9f0a 914
059a1ec3 915 list->elem = inet6dev;
8538f388 916 lxc_list_add_tail(&netdev->ipv6, list);
059a1ec3
CB
917 move_ptr(inet6dev);
918 move_ptr(list);
47903908 919
c2cc9f0a 920 return 0;
921}
922
f9373e40
CB
923static int set_config_net_ipv6_gateway(const char *key, const char *value,
924 struct lxc_conf *lxc_conf, void *data)
f8fee0e2 925{
070a05af 926 struct lxc_netdev *netdev = data;
f8fee0e2 927
6bed0fb6 928 if (lxc_config_value_empty(value))
f9373e40 929 return clr_config_net_ipv6_gateway(key, lxc_conf, data);
6bed0fb6 930
f8fee0e2 931 if (!netdev)
059a1ec3 932 return ret_errno(EINVAL);
f8fee0e2 933
e088e926 934 free(netdev->ipv6_gateway);
f8fee0e2 935
a2f9a670 936 if (strcmp(value, "auto") == 0) {
19a26f82
MK
937 netdev->ipv6_gateway = NULL;
938 netdev->ipv6_gateway_auto = true;
a2f9a670 939 } else if (strcmp(value, "dev") == 0) {
940 netdev->ipv6_gateway = NULL;
941 netdev->ipv6_gateway_auto = false;
942 netdev->ipv6_gateway_dev = true;
19a26f82 943 } else {
25a908b8 944 int ret;
059a1ec3 945 __do_free struct in6_addr *gw = NULL;
8fb86a37 946
bec695f3 947 gw = malloc(sizeof(*gw));
25a908b8 948 if (!gw)
059a1ec3 949 return ret_errno(ENOMEM);
bec695f3 950
25a908b8 951 ret = inet_pton(AF_INET6, value, gw);
059a1ec3
CB
952 if (!ret || ret < 0)
953 return log_error_errno(-EINVAL, EINVAL,
954 "Invalid ipv6 gateway address \"%s\"", value);
19a26f82 955
059a1ec3 956 netdev->ipv6_gateway = move_ptr(gw);
19a26f82 957 netdev->ipv6_gateway_auto = false;
f8fee0e2
MK
958 }
959
f8fee0e2
MK
960 return 0;
961}
962
d4a7da46 963static int set_config_net_veth_ipv6_route(const char *key, const char *value,
964 struct lxc_conf *lxc_conf, void *data)
965{
6453ba56
CB
966 __do_free char *valdup = NULL;
967 __do_free struct lxc_inet6dev *inet6dev = NULL;
968 __do_free struct lxc_list *list = NULL;
d4a7da46 969 int ret;
970 char *netmask, *slash;
971 struct lxc_netdev *netdev = data;
972
973 if (lxc_config_value_empty(value))
974 return clr_config_net_veth_ipv6_route(key, lxc_conf, data);
975
976 if (!netdev)
059a1ec3 977 return ret_errno(EINVAL);
d4a7da46 978
059a1ec3
CB
979 if (netdev->type != LXC_NET_VETH)
980 return log_error_errno(-EINVAL,
981 EINVAL, "Invalid ipv6 route \"%s\", can only be used with veth network",
982 value);
d4a7da46 983
984 inet6dev = malloc(sizeof(*inet6dev));
985 if (!inet6dev)
059a1ec3 986 return ret_errno(ENOMEM);
d4a7da46 987 memset(inet6dev, 0, sizeof(*inet6dev));
988
989 list = malloc(sizeof(*list));
990 if (!list)
059a1ec3 991 return ret_errno(ENOMEM);
d4a7da46 992
993 lxc_list_init(list);
d4a7da46 994
995 valdup = strdup(value);
996 if (!valdup)
997 return -1;
998
999 slash = strchr(valdup, '/');
1000 if (!slash)
059a1ec3 1001 return ret_errno(EINVAL);
d4a7da46 1002
1003 *slash = '\0';
1004 slash++;
1005 if (*slash == '\0')
059a1ec3 1006 return ret_errno(EINVAL);
d4a7da46 1007
1008 netmask = slash;
1009
1010 ret = lxc_safe_uint(netmask, &inet6dev->prefix);
1011 if (ret < 0 || inet6dev->prefix > 128)
059a1ec3 1012 return ret_errno(EINVAL);
d4a7da46 1013
1014 ret = inet_pton(AF_INET6, valdup, &inet6dev->addr);
1015 if (!ret || ret < 0)
059a1ec3 1016 return ret_errno(EINVAL);
d4a7da46 1017
059a1ec3 1018 list->elem = inet6dev;
d4a7da46 1019 lxc_list_add_tail(&netdev->priv.veth_attr.ipv6_routes, list);
1020 move_ptr(inet6dev);
1021 move_ptr(list);
1022
1023 return 0;
1024}
1025
f9373e40
CB
1026static int set_config_net_script_up(const char *key, const char *value,
1027 struct lxc_conf *lxc_conf, void *data)
e3b4c4c4 1028{
070a05af 1029 struct lxc_netdev *netdev = data;
e3b4c4c4 1030
6bed0fb6 1031 if (lxc_config_value_empty(value))
f9373e40 1032 return clr_config_net_script_up(key, lxc_conf, data);
6bed0fb6 1033
e3b4c4c4 1034 if (!netdev)
059a1ec3 1035 return ret_errno(EINVAL);
e3b4c4c4 1036
713046e3 1037 return set_config_string_item(&netdev->upscript, value);
8fc8295a
DE
1038}
1039
f9373e40
CB
1040static int set_config_net_script_down(const char *key, const char *value,
1041 struct lxc_conf *lxc_conf, void *data)
8fc8295a 1042{
070a05af 1043 struct lxc_netdev *netdev = data;
8fc8295a 1044
6bed0fb6 1045 if (lxc_config_value_empty(value))
f9373e40 1046 return clr_config_net_script_down(key, lxc_conf, data);
6bed0fb6 1047
8fc8295a 1048 if (!netdev)
059a1ec3 1049 return ret_errno(EINVAL);
8fc8295a 1050
713046e3 1051 return set_config_string_item(&netdev->downscript, value);
e3b4c4c4
ST
1052}
1053
059a1ec3 1054static int add_hook(struct lxc_conf *lxc_conf, int which, __owns char *hook)
26ddeedd 1055{
059a1ec3 1056 __do_free char *val = hook;
26ddeedd
SH
1057 struct lxc_list *hooklist;
1058
1059 hooklist = malloc(sizeof(*hooklist));
059a1ec3
CB
1060 if (!hooklist)
1061 return ret_errno(ENOMEM);
504a2217 1062
059a1ec3 1063 hooklist->elem = move_ptr(val);
26ddeedd 1064 lxc_list_add_tail(&lxc_conf->hooks[which], hooklist);
47903908 1065
26ddeedd
SH
1066 return 0;
1067}
1068
50d86993
CB
1069static int set_config_seccomp_allow_nesting(const char *key, const char *value,
1070 struct lxc_conf *lxc_conf, void *data)
1071{
c3e3c21a 1072#ifdef HAVE_SECCOMP
50d86993
CB
1073 if (lxc_config_value_empty(value))
1074 return clr_config_seccomp_allow_nesting(key, lxc_conf, NULL);
1075
c3e3c21a 1076 if (lxc_safe_uint(value, &lxc_conf->seccomp.allow_nesting) < 0)
50d86993
CB
1077 return -1;
1078
c3e3c21a 1079 if (lxc_conf->seccomp.allow_nesting > 1)
21fce08c 1080 return ret_set_errno(-1, EINVAL);
86ce1da1
CB
1081
1082 return 0;
c3e3c21a
CB
1083#else
1084 errno = ENOSYS;
1085 return -1;
1086#endif
86ce1da1
CB
1087}
1088
84cf6d25
WB
1089static int set_config_seccomp_notify_cookie(const char *key, const char *value,
1090 struct lxc_conf *lxc_conf, void *data)
1091{
1092#ifdef HAVE_SECCOMP_NOTIFY
1093 return set_config_string_item(&lxc_conf->seccomp.notifier.cookie, value);
1094#else
21fce08c 1095 return ret_set_errno(-1, ENOSYS);
84cf6d25
WB
1096#endif
1097}
1098
86ce1da1
CB
1099static int set_config_seccomp_notify_proxy(const char *key, const char *value,
1100 struct lxc_conf *lxc_conf, void *data)
1101{
c3e3c21a 1102#ifdef HAVE_SECCOMP_NOTIFY
86ce1da1
CB
1103 const char *offset;
1104
1105 if (lxc_config_value_empty(value))
1106 return clr_config_seccomp_notify_proxy(key, lxc_conf, NULL);
1107
1108 if (strncmp(value, "unix:", 5) != 0)
21fce08c 1109 return ret_set_errno(-1, EINVAL);
86ce1da1
CB
1110
1111 offset = value + 5;
c3e3c21a 1112 if (lxc_unix_sockaddr(&lxc_conf->seccomp.notifier.proxy_addr, offset) < 0)
50d86993
CB
1113 return -1;
1114
1115 return 0;
86ce1da1 1116#else
21fce08c 1117 return ret_set_errno(-1, ENOSYS);
86ce1da1 1118#endif
50d86993
CB
1119}
1120
0b427da0
CB
1121static int set_config_seccomp_profile(const char *key, const char *value,
1122 struct lxc_conf *lxc_conf, void *data)
8f2c3a70 1123{
c3e3c21a 1124 return set_config_path_item(&lxc_conf->seccomp.seccomp, value);
8f2c3a70
SH
1125}
1126
5cda27c1
SH
1127static int set_config_execute_cmd(const char *key, const char *value,
1128 struct lxc_conf *lxc_conf, void *data)
1129{
1130 return set_config_path_item(&lxc_conf->execute_cmd, value);
1131}
1132
713046e3 1133static int set_config_init_cmd(const char *key, const char *value,
c7e27aaf 1134 struct lxc_conf *lxc_conf, void *data)
67c660d0 1135{
713046e3 1136 return set_config_path_item(&lxc_conf->init_cmd, value);
67c660d0
SG
1137}
1138
3c491553
L
1139static int set_config_init_cwd(const char *key, const char *value,
1140 struct lxc_conf *lxc_conf, void *data)
1141{
1142 return set_config_path_item(&lxc_conf->init_cwd, value);
1143}
1144
713046e3 1145static int set_config_init_uid(const char *key, const char *value,
c7e27aaf 1146 struct lxc_conf *lxc_conf, void *data)
72bb04e4 1147{
d1e5d636
CB
1148 unsigned int init_uid;
1149
663e9916 1150 if (lxc_config_value_empty(value)) {
2e7cde40 1151 lxc_conf->init_uid = 0;
fee80911 1152 return 0;
2e7cde40 1153 }
fee80911 1154
d1e5d636
CB
1155 if (lxc_safe_uint(value, &init_uid) < 0)
1156 return -1;
25a908b8 1157
d1e5d636
CB
1158 lxc_conf->init_uid = init_uid;
1159
72bb04e4
PT
1160 return 0;
1161}
1162
713046e3 1163static int set_config_init_gid(const char *key, const char *value,
c7e27aaf 1164 struct lxc_conf *lxc_conf, void *data)
72bb04e4 1165{
d1e5d636
CB
1166 unsigned int init_gid;
1167
663e9916 1168 if (lxc_config_value_empty(value)) {
2debb6e6 1169 lxc_conf->init_gid = 0;
a757cc7d 1170 return 0;
2debb6e6 1171 }
a757cc7d 1172
d1e5d636
CB
1173 if (lxc_safe_uint(value, &init_gid) < 0)
1174 return -1;
25a908b8 1175
d1e5d636
CB
1176 lxc_conf->init_gid = init_gid;
1177
72bb04e4
PT
1178 return 0;
1179}
1180
466c2e93 1181static int set_config_hooks(const char *key, const char *value,
c7e27aaf 1182 struct lxc_conf *lxc_conf, void *data)
26ddeedd 1183{
059a1ec3 1184 __do_free char *copy = NULL;
72bb04e4 1185
663e9916 1186 if (lxc_config_value_empty(value))
7d0eb87e
SH
1187 return lxc_clear_hooks(lxc_conf, key);
1188
ed1454e8
CB
1189 if (strcmp(key + 4, "hook") == 0)
1190 return log_error_errno(-EINVAL, EINVAL, "lxc.hook must not have a value");
25a908b8 1191
7d0eb87e 1192 copy = strdup(value);
25a908b8 1193 if (!copy)
059a1ec3 1194 return ret_errno(ENOMEM);
504a2217 1195
a182feae 1196 if (strcmp(key + 9, "pre-start") == 0)
059a1ec3 1197 return add_hook(lxc_conf, LXCHOOK_PRESTART, move_ptr(copy));
08dd2805 1198 else if (strcmp(key + 9, "start-host") == 0)
059a1ec3 1199 return add_hook(lxc_conf, LXCHOOK_START_HOST, move_ptr(copy));
a182feae 1200 else if (strcmp(key + 9, "pre-mount") == 0)
059a1ec3 1201 return add_hook(lxc_conf, LXCHOOK_PREMOUNT, move_ptr(copy));
a182feae 1202 else if (strcmp(key + 9, "autodev") == 0)
059a1ec3 1203 return add_hook(lxc_conf, LXCHOOK_AUTODEV, move_ptr(copy));
a182feae 1204 else if (strcmp(key + 9, "mount") == 0)
059a1ec3 1205 return add_hook(lxc_conf, LXCHOOK_MOUNT, move_ptr(copy));
a182feae 1206 else if (strcmp(key + 9, "start") == 0)
059a1ec3 1207 return add_hook(lxc_conf, LXCHOOK_START, move_ptr(copy));
a182feae 1208 else if (strcmp(key + 9, "stop") == 0)
059a1ec3 1209 return add_hook(lxc_conf, LXCHOOK_STOP, move_ptr(copy));
a182feae 1210 else if (strcmp(key + 9, "post-stop") == 0)
059a1ec3 1211 return add_hook(lxc_conf, LXCHOOK_POSTSTOP, move_ptr(copy));
a182feae 1212 else if (strcmp(key + 9, "clone") == 0)
059a1ec3 1213 return add_hook(lxc_conf, LXCHOOK_CLONE, move_ptr(copy));
a182feae 1214 else if (strcmp(key + 9, "destroy") == 0)
059a1ec3 1215 return add_hook(lxc_conf, LXCHOOK_DESTROY, move_ptr(copy));
47903908 1216
26ddeedd
SH
1217 return -1;
1218}
1219
44ae0fb6
CB
1220static int set_config_hooks_version(const char *key, const char *value,
1221 struct lxc_conf *lxc_conf, void *data)
1222{
1223 int ret;
1224 unsigned int tmp;
1225
1226 if (lxc_config_value_empty(value))
1227 return clr_config_hooks_version(key, lxc_conf, NULL);
1228
1229 ret = lxc_safe_uint(value, &tmp);
1230 if (ret < 0)
1231 return -1;
1232
ed1454e8
CB
1233 if (tmp > 1)
1234 return log_error_errno(-EINVAL,
1235 EINVAL, "Invalid hook version specified. Currently only 0 (legacy) and 1 are supported");
44ae0fb6
CB
1236
1237 lxc_conf->hooks_version = tmp;
47903908 1238
44ae0fb6
CB
1239 return 0;
1240}
1241
713046e3 1242static int set_config_personality(const char *key, const char *value,
c7e27aaf 1243 struct lxc_conf *lxc_conf, void *data)
cccc74b5 1244{
62af653c 1245 signed long personality;
cccc74b5 1246
62af653c 1247 personality = lxc_config_parse_arch(value);
525f0002
CS
1248 if (personality >= 0)
1249 lxc_conf->personality = personality;
1250 else
25a908b8 1251 WARN("Unsupported personality \"%s\"", value);
970ab589
DL
1252
1253 return 0;
cccc74b5
DL
1254}
1255
232763d6
CB
1256static int set_config_pty_max(const char *key, const char *value,
1257 struct lxc_conf *lxc_conf, void *data)
10db618d 1258{
e528c735
CB
1259 int ret;
1260 unsigned int max = 0;
1261
663e9916 1262 if (lxc_config_value_empty(value)) {
e528c735 1263 lxc_conf->pty_max = 0;
884a4580 1264 return 0;
ec200ce9 1265 }
884a4580 1266
e528c735
CB
1267 ret = lxc_safe_uint(value, &max);
1268 if (ret < 0)
49aabd9d 1269 return ret_errno(EINVAL);
10db618d 1270
e528c735 1271 lxc_conf->pty_max = max;
47903908 1272
10db618d 1273 return 0;
1274}
1275
a182feae
CB
1276/* We only need to check whether the first byte of the key after the lxc.start.
1277 * prefix matches our expectations since they fortunately all start with a
1278 * different letter. If anything was wrong with the key we would have already
1279 * noticed when the callback was called.
1280 */
713046e3 1281static int set_config_start(const char *key, const char *value,
c7e27aaf 1282 struct lxc_conf *lxc_conf, void *data)
ee1e7aa0 1283{
572f6a14 1284 int ret;
ebb80f95
CB
1285 bool is_empty;
1286
663e9916 1287 is_empty = lxc_config_value_empty(value);
ebb80f95 1288
a182feae 1289 if (*(key + 10) == 'a') { /* lxc.start.auto */
ebb80f95
CB
1290 if (is_empty) {
1291 lxc_conf->start_auto = 0;
1292 return 0;
1293 }
61ff8fc8 1294
572f6a14
CB
1295 ret = lxc_safe_uint(value, &lxc_conf->start_auto);
1296 if (ret)
1297 return ret;
ebb80f95 1298
3590152f 1299 if (lxc_conf->start_auto > 1)
572f6a14 1300 return ret_errno(EINVAL);
ebb80f95 1301
ee1e7aa0 1302 return 0;
a182feae 1303 } else if (*(key + 10) == 'd') { /* lxc.start.delay */
ebb80f95
CB
1304 if (is_empty) {
1305 lxc_conf->start_delay = 0;
1306 return 0;
1307 }
1308
ebb80f95 1309 return lxc_safe_uint(value, &lxc_conf->start_delay);
a182feae 1310 } else if (*(key + 10) == 'o') { /* lxc.start.order */
ebb80f95
CB
1311 if (is_empty) {
1312 lxc_conf->start_order = 0;
1313 return 0;
1314 }
1315
ebb80f95 1316 return lxc_safe_int(value, &lxc_conf->start_order);
ee1e7aa0 1317 }
ebb80f95 1318
572f6a14 1319 return ret_errno(EINVAL);
ee1e7aa0
SG
1320}
1321
713046e3 1322static int set_config_monitor(const char *key, const char *value,
c7e27aaf 1323 struct lxc_conf *lxc_conf, void *data)
a8dfe4e0 1324{
663e9916 1325 if (lxc_config_value_empty(value)) {
4ad9cd26 1326 lxc_conf->monitor_unshare = 0;
a8dfe4e0
WB
1327 return 0;
1328 }
4ad9cd26 1329
a182feae 1330 if (strcmp(key + 12, "unshare") == 0)
4ad9cd26
CB
1331 return lxc_safe_uint(value, &lxc_conf->monitor_unshare);
1332
e1665973 1333 return ret_errno(EINVAL);
a8dfe4e0
WB
1334}
1335
258f8051
CB
1336static int set_config_monitor_signal_pdeath(const char *key, const char *value,
1337 struct lxc_conf *lxc_conf, void *data)
1338{
1339 if (lxc_config_value_empty(value)) {
1340 lxc_conf->monitor_signal_pdeath = 0;
1341 return 0;
1342 }
1343
1344 if (strcmp(key + 12, "signal.pdeath") == 0) {
1345 int sig_n;
1346
1347 sig_n = sig_parse(value);
1348 if (sig_n < 0)
cb5f3df2 1349 return ret_errno(EINVAL);
258f8051
CB
1350
1351 lxc_conf->monitor_signal_pdeath = sig_n;
1352 return 0;
1353 }
1354
cb5f3df2 1355 return ret_errno(EINVAL);
258f8051
CB
1356}
1357
713046e3 1358static int set_config_group(const char *key, const char *value,
c7e27aaf 1359 struct lxc_conf *lxc_conf, void *data)
ee1e7aa0 1360{
15436995
CB
1361 __do_free char *groups = NULL;
1362 char *token;
ee1e7aa0 1363
663e9916 1364 if (lxc_config_value_empty(value))
ee1e7aa0
SG
1365 return lxc_clear_groups(lxc_conf);
1366
1367 groups = strdup(value);
25a908b8 1368 if (!groups)
15436995 1369 return ret_errno(ENOMEM);
ee1e7aa0 1370
25a908b8
CB
1371 /* In case several groups are specified in a single line split these
1372 * groups in a single element for the list.
504a2217 1373 */
62dd965e 1374 lxc_iterate_parts(token, groups, " \t") {
15436995
CB
1375 __do_free struct lxc_list *grouplist = NULL;
1376
ee1e7aa0 1377 grouplist = malloc(sizeof(*grouplist));
15436995
CB
1378 if (!grouplist)
1379 return ret_errno(ENOMEM);
ee1e7aa0
SG
1380
1381 grouplist->elem = strdup(token);
15436995
CB
1382 if (!grouplist->elem)
1383 return ret_errno(ENOMEM);
ee1e7aa0 1384
15436995 1385 lxc_list_add_tail(&lxc_conf->groups, move_ptr(grouplist));
d028235d 1386 }
ee1e7aa0 1387
15436995 1388 return 0;
ee1e7aa0
SG
1389}
1390
713046e3 1391static int set_config_environment(const char *key, const char *value,
c7e27aaf 1392 struct lxc_conf *lxc_conf, void *data)
7c661726 1393{
a6bf1128 1394 __do_free struct lxc_list *list_item = NULL;
7c661726 1395
663e9916 1396 if (lxc_config_value_empty(value))
ab799c0b
SG
1397 return lxc_clear_environment(lxc_conf);
1398
7c661726
MP
1399 list_item = malloc(sizeof(*list_item));
1400 if (!list_item)
a6bf1128 1401 return ret_errno(ENOMEM);
7c661726 1402
5eab47bc
CB
1403 if (!strchr(value, '=')) {
1404 const char *env_val;
1405 const char *env_key = value;
1406 const char *env_var[3] = {0};
1407
1408 env_val = getenv(env_key);
1409 if (!env_val)
a6bf1128 1410 return ret_errno(ENOENT);
5eab47bc
CB
1411
1412 env_var[0] = env_key;
1413 env_var[1] = env_val;
1414 list_item->elem = lxc_string_join("=", env_var, false);
1415 } else {
1416 list_item->elem = strdup(value);
1417 }
7c661726
MP
1418
1419 if (!list_item->elem)
a6bf1128 1420 return ret_errno(ENOMEM);
7c661726 1421
a6bf1128 1422 lxc_list_add_tail(&lxc_conf->environment, move_ptr(list_item));
7c661726
MP
1423
1424 return 0;
7c661726
MP
1425}
1426
fe1c5887
CB
1427static int set_config_tty_max(const char *key, const char *value,
1428 struct lxc_conf *lxc_conf, void *data)
b0a33c1e 1429{
885766f5
CB
1430 int ret;
1431 unsigned int nbtty = 0;
1432
663e9916 1433 if (lxc_config_value_empty(value)) {
885766f5 1434 lxc_conf->ttys.max = 0;
fb12b12a 1435 return 0;
cb508ee8 1436 }
fb12b12a 1437
885766f5
CB
1438 ret = lxc_safe_uint(value, &nbtty);
1439 if (ret < 0)
755d6532 1440 return ret;
885766f5
CB
1441
1442 lxc_conf->ttys.max = nbtty;
47903908 1443
885766f5 1444 return 0;
b0a33c1e 1445}
1446
42e53c29 1447static int set_config_tty_dir(const char *key, const char *value,
c7e27aaf 1448 struct lxc_conf *lxc_conf, void *data)
7c6ef2a2 1449{
885766f5 1450 return set_config_string_item_max(&lxc_conf->ttys.dir, value,
504a2217 1451 NAME_MAX + 1);
7c6ef2a2
SH
1452}
1453
953fe44f
CB
1454static int set_config_apparmor_profile(const char *key, const char *value,
1455 struct lxc_conf *lxc_conf, void *data)
e075f5d9 1456{
713046e3 1457 return set_config_string_item(&lxc_conf->lsm_aa_profile, value);
fe4de9a6
DE
1458}
1459
953fe44f
CB
1460static int set_config_apparmor_allow_incomplete(const char *key,
1461 const char *value,
1462 struct lxc_conf *lxc_conf,
1463 void *data)
7aff4f43 1464{
042f8711
CB
1465 int ret;
1466
663e9916 1467 if (lxc_config_value_empty(value)) {
cccfa758 1468 lxc_conf->lsm_aa_allow_incomplete = 0;
a678e9fa 1469 return 0;
cccfa758 1470 }
a678e9fa 1471
042f8711
CB
1472 ret = lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete);
1473 if (ret)
1474 return ret;
7aff4f43 1475
25a908b8 1476 if (lxc_conf->lsm_aa_allow_incomplete > 1)
042f8711 1477 return ret_errno(EINVAL);
7aff4f43
SH
1478
1479 return 0;
1480}
1481
1800f924
WB
1482static int set_config_apparmor_allow_nesting(const char *key,
1483 const char *value,
1484 struct lxc_conf *lxc_conf,
1485 void *data)
1486{
55a76891
CB
1487 int ret;
1488
1800f924
WB
1489 if (lxc_config_value_empty(value))
1490 return clr_config_apparmor_allow_nesting(key, lxc_conf, NULL);
1491
55a76891
CB
1492 ret = lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_nesting);
1493 if (ret)
1494 return ret;
1800f924
WB
1495
1496 if (lxc_conf->lsm_aa_allow_nesting > 1)
55a76891 1497 return ret_errno(EINVAL);
1800f924
WB
1498
1499 return 0;
1500}
1501
1502static int set_config_apparmor_raw(const char *key,
1503 const char *value,
1504 struct lxc_conf *lxc_conf,
1505 void *data)
1506{
7f44fda1
CB
1507 __do_free char *elem = NULL;
1508 __do_free struct lxc_list *list = NULL;
1800f924
WB
1509
1510 if (lxc_config_value_empty(value))
1511 return lxc_clear_apparmor_raw(lxc_conf);
1512
1513 list = malloc(sizeof(*list));
7f44fda1
CB
1514 if (!list)
1515 return ret_errno(ENOMEM);
1800f924
WB
1516
1517 elem = strdup(value);
7f44fda1
CB
1518 if (!elem)
1519 return ret_errno(ENOMEM);
1800f924 1520
7f44fda1
CB
1521 list->elem = move_ptr(elem);
1522 lxc_list_add_tail(&lxc_conf->lsm_aa_raw, move_ptr(list));
1800f924
WB
1523
1524 return 0;
1525}
1526
953fe44f
CB
1527static int set_config_selinux_context(const char *key, const char *value,
1528 struct lxc_conf *lxc_conf, void *data)
fe4de9a6 1529{
713046e3 1530 return set_config_string_item(&lxc_conf->lsm_se_context, value);
e075f5d9 1531}
e075f5d9 1532
4fef78bc
MB
1533static int set_config_selinux_context_keyring(const char *key, const char *value,
1534 struct lxc_conf *lxc_conf, void *data)
1535{
1536 return set_config_string_item(&lxc_conf->lsm_se_keyring_context, value);
1537}
1538
8f818a84
MB
1539static int set_config_keyring_session(const char *key, const char *value,
1540 struct lxc_conf *lxc_conf, void *data)
1541{
1542 return set_config_bool_item(&lxc_conf->keyring_disable_session, value, false);
1543}
1544
46cc906d 1545static int set_config_log_file(const char *key, const char *value,
c7e27aaf 1546 struct lxc_conf *c, void *data)
4a85ce2a 1547{
6d03d92a
DE
1548 int ret;
1549
663e9916 1550 if (lxc_config_value_empty(value)) {
34f3b30a 1551 free_disarm(c->logfile);
0d601acb
CB
1552 return 0;
1553 }
1554
34f3b30a
CB
1555 /*
1556 * Store these values in the lxc_conf, and then try to set for actual
504a2217
CB
1557 * current logging.
1558 */
713046e3 1559 ret = set_config_path_item(&c->logfile, value);
6d03d92a 1560 if (ret == 0)
858377e4 1561 ret = lxc_log_set_file(&c->logfd, c->logfile);
25a908b8 1562
6d03d92a 1563 return ret;
4a85ce2a
SH
1564}
1565
46cc906d 1566static int set_config_log_level(const char *key, const char *value,
c7e27aaf 1567 struct lxc_conf *lxc_conf, void *data)
4a85ce2a 1568{
9ea87d5d
SH
1569 int newlevel;
1570
663e9916 1571 if (lxc_config_value_empty(value)) {
4b73005c 1572 lxc_conf->loglevel = LXC_LOG_LEVEL_NOTSET;
4a85ce2a 1573 return 0;
575b9745 1574 }
4a85ce2a 1575
a56e2df9 1576 if (value[0] >= '0' && value[0] <= '9') {
806244c6
CB
1577 int ret;
1578
1579 ret = lxc_safe_int(value, &newlevel);
1580 if (ret)
1581 return ret_errno(EINVAL);
a56e2df9 1582 } else {
9ea87d5d 1583 newlevel = lxc_log_priority_to_int(value);
a56e2df9 1584 }
575b9745 1585
806244c6
CB
1586 /*
1587 * Store these values in the lxc_conf, and then try to set for actual
575b9745
CB
1588 * current logging.
1589 */
b40a606e 1590 lxc_conf->loglevel = newlevel;
47903908 1591
858377e4 1592 return lxc_log_set_level(&lxc_conf->loglevel, newlevel);
4a85ce2a
SH
1593}
1594
713046e3 1595static int set_config_autodev(const char *key, const char *value,
c7e27aaf 1596 struct lxc_conf *lxc_conf, void *data)
c6883f38 1597{
0c48b874
CB
1598 int ret;
1599
663e9916 1600 if (lxc_config_value_empty(value)) {
1045031e 1601 lxc_conf->autodev = 0;
180abbc0 1602 return 0;
1045031e 1603 }
180abbc0 1604
0c48b874
CB
1605 ret = lxc_safe_uint(value, &lxc_conf->autodev);
1606 if (ret)
1607 return ret_errno(EINVAL);
c6883f38 1608
25a908b8 1609 if (lxc_conf->autodev > 1)
0c48b874 1610 return ret_errno(EINVAL);
c6883f38
SH
1611
1612 return 0;
1613}
1614
63012bdd
CK
1615static int set_config_autodev_tmpfs_size(const char *key, const char *value,
1616 struct lxc_conf *lxc_conf, void *data)
1617{
1618 if (lxc_config_value_empty(value)) {
1619 lxc_conf->autodevtmpfssize = 500000;
1620 return 0;
1621 }
1622
1623 if (lxc_safe_int(value, &lxc_conf->autodevtmpfssize) < 0)
1624 lxc_conf->autodevtmpfssize = 500000;
1625
1626 return 0;
1627}
1628
55c84efc 1629static int set_config_signal_halt(const char *key, const char *value,
c7e27aaf 1630 struct lxc_conf *lxc_conf, void *data)
f0f1d8c0 1631{
62a085fb 1632 int sig_n;
f0f1d8c0 1633
663e9916 1634 if (lxc_config_value_empty(value)) {
c1a64603 1635 lxc_conf->haltsignal = 0;
955912f0 1636 return 0;
c1a64603 1637 }
955912f0 1638
62a085fb 1639 sig_n = sig_parse(value);
f0f1d8c0 1640 if (sig_n < 0)
d12fabf8 1641 return ret_errno(EINVAL);
25a908b8 1642
f0f1d8c0
DE
1643 lxc_conf->haltsignal = sig_n;
1644
1645 return 0;
1646}
1647
55c84efc 1648static int set_config_signal_reboot(const char *key, const char *value,
c7e27aaf 1649 struct lxc_conf *lxc_conf, void *data)
dd267776 1650{
9d7e7587 1651 int sig_n;
dd267776 1652
663e9916 1653 if (lxc_config_value_empty(value)) {
18fcee44 1654 lxc_conf->rebootsignal = 0;
9d7e7587 1655 return 0;
18fcee44 1656 }
9d7e7587
CB
1657
1658 sig_n = sig_parse(value);
dd267776 1659 if (sig_n < 0)
7d6b1a20 1660 return ret_errno(EINVAL);
25a908b8 1661
dd267776
BP
1662 lxc_conf->rebootsignal = sig_n;
1663
1664 return 0;
1665}
1666
55c84efc 1667static int set_config_signal_stop(const char *key, const char *value,
c7e27aaf 1668 struct lxc_conf *lxc_conf, void *data)
a84b9932 1669{
6ca6aedd 1670 int sig_n;
a84b9932 1671
663e9916 1672 if (lxc_config_value_empty(value)) {
4100d1a7 1673 lxc_conf->stopsignal = 0;
6ca6aedd 1674 return 0;
4100d1a7 1675 }
6ca6aedd
CB
1676
1677 sig_n = sig_parse(value);
a84b9932 1678 if (sig_n < 0)
c4d9b159 1679 return ret_errno(EINVAL);
25a908b8 1680
a84b9932
AV
1681 lxc_conf->stopsignal = sig_n;
1682
1683 return 0;
1684}
1685
54860ed0
CB
1686static int __set_config_cgroup_controller(const char *key, const char *value,
1687 struct lxc_conf *lxc_conf, int version)
576f946d 1688{
54860ed0
CB
1689 const char *subkey, *token;
1690 size_t token_len;
bf83c5b9
MS
1691 struct lxc_list *cglist = NULL;
1692 struct lxc_cgroup *cgelem = NULL;
576f946d 1693
663e9916 1694 if (lxc_config_value_empty(value))
54860ed0
CB
1695 return lxc_clear_cgroups(lxc_conf, key, version);
1696
1697 if (version == CGROUP2_SUPER_MAGIC) {
1698 token = "lxc.cgroup2.";
1699 token_len = 12;
1700 } else if (version == CGROUP_SUPER_MAGIC) {
1701 token = "lxc.cgroup.";
1702 token_len = 11;
1703 } else {
1704 return -EINVAL;
1705 }
576f946d 1706
54860ed0
CB
1707 if (strncmp(key, token, token_len) != 0)
1708 return -EINVAL;
a871ff6b 1709
54860ed0
CB
1710 subkey = key + token_len;
1711 if (*subkey == '\0')
1712 return -EINVAL;
576f946d 1713
1714 cglist = malloc(sizeof(*cglist));
1715 if (!cglist)
bf83c5b9 1716 goto out;
576f946d 1717
1718 cgelem = malloc(sizeof(*cgelem));
bf83c5b9
MS
1719 if (!cgelem)
1720 goto out;
1721 memset(cgelem, 0, sizeof(*cgelem));
576f946d 1722
1723 cgelem->subsystem = strdup(subkey);
54860ed0
CB
1724 if (!cgelem->subsystem)
1725 goto out;
bf83c5b9 1726
54860ed0
CB
1727 cgelem->value = strdup(value);
1728 if (!cgelem->value)
bf83c5b9
MS
1729 goto out;
1730
54860ed0
CB
1731 cgelem->version = version;
1732
1733 lxc_list_add_elem(cglist, cgelem);
576f946d 1734
54860ed0
CB
1735 if (version == CGROUP2_SUPER_MAGIC)
1736 lxc_list_add_tail(&lxc_conf->cgroup2, cglist);
1737 else
1738 lxc_list_add_tail(&lxc_conf->cgroup, cglist);
576f946d 1739
1740 return 0;
bf83c5b9
MS
1741
1742out:
f10fad2f 1743 free(cglist);
bf83c5b9 1744 if (cgelem) {
f10fad2f 1745 free(cgelem->subsystem);
f10fad2f 1746 free(cgelem->value);
bf83c5b9
MS
1747 free(cgelem);
1748 }
1749
1750 return -1;
576f946d 1751}
1752
54860ed0
CB
1753static int set_config_cgroup_controller(const char *key, const char *value,
1754 struct lxc_conf *lxc_conf, void *data)
1755{
1756 return __set_config_cgroup_controller(key, value, lxc_conf,
1757 CGROUP_SUPER_MAGIC);
1758}
1759
1760static int set_config_cgroup2_controller(const char *key, const char *value,
1761 struct lxc_conf *lxc_conf, void *data)
1762{
1763 return __set_config_cgroup_controller(key, value, lxc_conf,
1764 CGROUP2_SUPER_MAGIC);
1765}
1766
1767
43654d34
CB
1768static int set_config_cgroup_dir(const char *key, const char *value,
1769 struct lxc_conf *lxc_conf, void *data)
1770{
1771 if (lxc_config_value_empty(value))
1772 return clr_config_cgroup_dir(key, lxc_conf, NULL);
1773
43654d34
CB
1774 return set_config_string_item(&lxc_conf->cgroup_meta.dir, value);
1775}
1776
a900cbaf
WB
1777static int set_config_cgroup_monitor_dir(const char *key, const char *value,
1778 struct lxc_conf *lxc_conf, void *data)
1779{
1780 if (lxc_config_value_empty(value))
1781 return clr_config_cgroup_monitor_dir(key, lxc_conf, NULL);
1782
1783 return set_config_string_item(&lxc_conf->cgroup_meta.monitor_dir,
1784 value);
1785}
1786
7696c1f9
RJ
1787static int set_config_cgroup_monitor_pivot_dir(const char *key, const char *value,
1788 struct lxc_conf *lxc_conf, void *data)
1789{
1790 if (lxc_config_value_empty(value))
1791 return clr_config_cgroup_monitor_pivot_dir(key, lxc_conf, NULL);
1792
1793 return set_config_string_item(&lxc_conf->cgroup_meta.monitor_pivot_dir,
1794 value);
1795}
1796
a900cbaf
WB
1797static int set_config_cgroup_container_dir(const char *key, const char *value,
1798 struct lxc_conf *lxc_conf,
1799 void *data)
1800{
1801 if (lxc_config_value_empty(value))
1802 return clr_config_cgroup_container_dir(key, lxc_conf, NULL);
1803
1804 return set_config_string_item(&lxc_conf->cgroup_meta.container_dir,
1805 value);
1806}
1807
1808static int set_config_cgroup_container_inner_dir(const char *key,
1809 const char *value,
1810 struct lxc_conf *lxc_conf,
1811 void *data)
1812{
1813 if (lxc_config_value_empty(value))
e93197e7 1814 return clr_config_cgroup_container_inner_dir(key, lxc_conf, NULL);
a900cbaf
WB
1815
1816 if (strchr(value, '/') ||
1817 strcmp(value, ".") == 0 ||
1818 strcmp(value, "..") == 0)
e93197e7 1819 return log_error_errno(-EINVAL, EINVAL, "lxc.cgroup.dir.container.inner must be a single directory name");
a900cbaf 1820
e93197e7 1821 return set_config_string_item(&lxc_conf->cgroup_meta.namespace_dir, value);
a900cbaf
WB
1822}
1823
9caee129
CB
1824static int set_config_cgroup_relative(const char *key, const char *value,
1825 struct lxc_conf *lxc_conf, void *data)
76f0e2e7
CB
1826{
1827 unsigned int converted;
1828 int ret;
1829
1830 if (lxc_config_value_empty(value))
9caee129 1831 return clr_config_cgroup_relative(key, lxc_conf, NULL);
76f0e2e7
CB
1832
1833 ret = lxc_safe_uint(value, &converted);
1834 if (ret < 0)
1835 return -ret;
1836
1837 if (converted == 1) {
9caee129 1838 lxc_conf->cgroup_meta.relative = true;
76f0e2e7
CB
1839 return 0;
1840 }
1841
1842 if (converted == 0) {
9caee129 1843 lxc_conf->cgroup_meta.relative = false;
76f0e2e7
CB
1844 return 0;
1845 }
1846
1847 return -EINVAL;
1848}
1849
f7662514
CB
1850static bool parse_limit_value(const char **value, rlim_t *res)
1851{
1852 char *endptr = NULL;
1853
1854 if (strncmp(*value, "unlimited", STRLITERALLEN("unlimited")) == 0) {
1855 *res = RLIM_INFINITY;
1856 *value += STRLITERALLEN("unlimited");
1857 return true;
1858 }
1859
1860 errno = 0;
1861 *res = strtoull(*value, &endptr, 10);
1862 if (errno || !endptr)
1863 return false;
1864
1865 *value = endptr;
1866
1867 return true;
1868}
1869
240d4b74 1870static int set_config_prlimit(const char *key, const char *value,
c7e27aaf 1871 struct lxc_conf *lxc_conf, void *data)
c6d09e15 1872{
c6d09e15
WB
1873 struct lxc_list *iter;
1874 struct rlimit limit;
71460831 1875 rlim_t limit_value;
504a2217
CB
1876 struct lxc_list *limlist = NULL;
1877 struct lxc_limit *limelem = NULL;
c6d09e15 1878
663e9916 1879 if (lxc_config_value_empty(value))
c6d09e15
WB
1880 return lxc_clear_limits(lxc_conf, key);
1881
6333c915 1882 if (strncmp(key, "lxc.prlimit.", STRLITERALLEN("lxc.prlimit.")) != 0)
c6d09e15
WB
1883 return -1;
1884
6333c915 1885 key += STRLITERALLEN("lxc.prlimit.");
c6d09e15
WB
1886
1887 /* soft limit comes first in the value */
1888 if (!parse_limit_value(&value, &limit_value))
1889 return -1;
47903908 1890
c6d09e15
WB
1891 limit.rlim_cur = limit_value;
1892
1893 /* skip spaces and a colon */
1894 while (isspace(*value))
1895 ++value;
504a2217 1896
c6d09e15
WB
1897 if (*value == ':')
1898 ++value;
1899 else if (*value) /* any other character is an error here */
1900 return -1;
504a2217 1901
c6d09e15
WB
1902 while (isspace(*value))
1903 ++value;
1904
1905 /* optional hard limit */
1906 if (*value) {
1907 if (!parse_limit_value(&value, &limit_value))
1908 return -1;
47903908 1909
c6d09e15 1910 limit.rlim_max = limit_value;
504a2217 1911
c6d09e15
WB
1912 /* check for trailing garbage */
1913 while (isspace(*value))
1914 ++value;
504a2217 1915
c6d09e15
WB
1916 if (*value)
1917 return -1;
1918 } else {
1919 /* a single value sets both hard and soft limit */
1920 limit.rlim_max = limit.rlim_cur;
1921 }
1922
1923 /* find existing list element */
7edd0540 1924 lxc_list_for_each(iter, &lxc_conf->limits) {
c6d09e15
WB
1925 limelem = iter->elem;
1926 if (!strcmp(key, limelem->resource)) {
1927 limelem->limit = limit;
1928 return 0;
1929 }
1930 }
1931
1932 /* allocate list element */
1933 limlist = malloc(sizeof(*limlist));
1934 if (!limlist)
7edd0540 1935 goto on_error;
2e6e3feb 1936
c6d09e15
WB
1937 limelem = malloc(sizeof(*limelem));
1938 if (!limelem)
7edd0540 1939 goto on_error;
c6d09e15
WB
1940 memset(limelem, 0, sizeof(*limelem));
1941
1942 limelem->resource = strdup(key);
1943 if (!limelem->resource)
7edd0540 1944 goto on_error;
c6d09e15 1945
47903908 1946 limelem->limit = limit;
7edd0540 1947 lxc_list_add_elem(limlist, limelem);;
c6d09e15
WB
1948 lxc_list_add_tail(&lxc_conf->limits, limlist);
1949
1950 return 0;
1951
7edd0540 1952on_error:
c6d09e15 1953 free(limlist);
47903908 1954
c6d09e15
WB
1955 if (limelem) {
1956 free(limelem->resource);
1957 free(limelem);
1958 }
47903908 1959
c6d09e15
WB
1960 return -1;
1961}
1962
7edd0540
L
1963static int set_config_sysctl(const char *key, const char *value,
1964 struct lxc_conf *lxc_conf, void *data)
1965{
1966 struct lxc_list *iter;
e409b214 1967 char *replace_value = NULL;
7edd0540
L
1968 struct lxc_list *sysctl_list = NULL;
1969 struct lxc_sysctl *sysctl_elem = NULL;
7edd0540
L
1970
1971 if (lxc_config_value_empty(value))
e409b214 1972 return clr_config_sysctl(key, lxc_conf, NULL);
7edd0540 1973
6333c915 1974 if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) != 0)
7edd0540
L
1975 return -1;
1976
6333c915 1977 key += STRLITERALLEN("lxc.sysctl.");
7edd0540
L
1978
1979 /* find existing list element */
1980 lxc_list_for_each(iter, &lxc_conf->sysctls) {
1981 sysctl_elem = iter->elem;
e409b214
CB
1982
1983 if (strcmp(key, sysctl_elem->key) != 0)
1984 continue;
1985
1986 replace_value = strdup(value);
1987 if (!replace_value)
1988 return -1;
1989
1990 free(sysctl_elem->value);
1991 sysctl_elem->value = replace_value;
47903908 1992
e409b214 1993 return 0;
7edd0540
L
1994 }
1995
1996 /* allocate list element */
1997 sysctl_list = malloc(sizeof(*sysctl_list));
1998 if (!sysctl_list)
1999 goto on_error;
2000
2001 sysctl_elem = malloc(sizeof(*sysctl_elem));
2002 if (!sysctl_elem)
2003 goto on_error;
2004 memset(sysctl_elem, 0, sizeof(*sysctl_elem));
2005
2006 sysctl_elem->key = strdup(key);
2007 if (!sysctl_elem->key)
2008 goto on_error;
2009
2010 sysctl_elem->value = strdup(value);
2011 if (!sysctl_elem->value)
2012 goto on_error;
2013
2014 lxc_list_add_elem(sysctl_list, sysctl_elem);
7edd0540
L
2015 lxc_list_add_tail(&lxc_conf->sysctls, sysctl_list);
2016
2017 return 0;
2018
2019on_error:
2020 free(sysctl_list);
47903908 2021
7edd0540
L
2022 if (sysctl_elem) {
2023 free(sysctl_elem->key);
2024 free(sysctl_elem->value);
2025 free(sysctl_elem);
2026 }
47903908 2027
7edd0540 2028 return -1;
7edd0540
L
2029}
2030
61d7a733
YT
2031static int set_config_proc(const char *key, const char *value,
2032 struct lxc_conf *lxc_conf, void *data)
2033{
2034 const char *subkey;
2035 struct lxc_list *proclist = NULL;
2036 struct lxc_proc *procelem = NULL;
2037
2038 if (lxc_config_value_empty(value))
2039 return clr_config_proc(key, lxc_conf, NULL);
2040
6333c915 2041 if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) != 0)
61d7a733
YT
2042 return -1;
2043
6333c915 2044 subkey = key + STRLITERALLEN("lxc.proc.");
61d7a733
YT
2045 if (*subkey == '\0')
2046 return -EINVAL;
2047
2048 proclist = malloc(sizeof(*proclist));
2049 if (!proclist)
2050 goto on_error;
2051
2052 procelem = malloc(sizeof(*procelem));
2053 if (!procelem)
2054 goto on_error;
2055 memset(procelem, 0, sizeof(*procelem));
2056
2057 procelem->filename = strdup(subkey);
2058 procelem->value = strdup(value);
2059
2060 if (!procelem->filename || !procelem->value)
2061 goto on_error;
2062
2063 proclist->elem = procelem;
2064
2065 lxc_list_add_tail(&lxc_conf->procs, proclist);
2066
2067 return 0;
2068
2069on_error:
2070 free(proclist);
47903908 2071
61d7a733
YT
2072 if (procelem) {
2073 free(procelem->filename);
2074 free(procelem->value);
2075 free(procelem);
2076 }
2077
2078 return -1;
2079}
2080
5014ff2e 2081static int set_config_idmaps(const char *key, const char *value,
c7e27aaf 2082 struct lxc_conf *lxc_conf, void *data)
f6d3e3e4 2083{
251d0d2a 2084 unsigned long hostid, nsid, range;
f6d3e3e4 2085 char type;
0b843d35 2086 int ret;
34a7a4c6
CB
2087 struct lxc_list *idmaplist = NULL;
2088 struct id_map *idmap = NULL;
f6d3e3e4 2089
663e9916 2090 if (lxc_config_value_empty(value))
7d0eb87e
SH
2091 return lxc_clear_idmaps(lxc_conf);
2092
f6d3e3e4
SH
2093 idmaplist = malloc(sizeof(*idmaplist));
2094 if (!idmaplist)
34a7a4c6 2095 goto on_error;
f6d3e3e4
SH
2096
2097 idmap = malloc(sizeof(*idmap));
2098 if (!idmap)
34a7a4c6 2099 goto on_error;
f6d3e3e4
SH
2100 memset(idmap, 0, sizeof(*idmap));
2101
0b843d35 2102 ret = parse_idmaps(value, &type, &nsid, &hostid, &range);
a8b1ac78 2103 if (ret < 0) {
f37d1c22 2104 ERROR("Failed to parse id mappings");
34a7a4c6 2105 goto on_error;
a8b1ac78 2106 }
34a7a4c6 2107
25a908b8 2108 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range);
ac7725e7 2109 if (type == 'u')
f6d3e3e4 2110 idmap->idtype = ID_TYPE_UID;
ac7725e7 2111 else if (type == 'g')
f6d3e3e4
SH
2112 idmap->idtype = ID_TYPE_GID;
2113 else
34a7a4c6 2114 goto on_error;
7e60c3f0 2115
f6d3e3e4
SH
2116 idmap->hostid = hostid;
2117 idmap->nsid = nsid;
2118 idmap->range = range;
7e60c3f0
SG
2119 idmaplist->elem = idmap;
2120 lxc_list_add_tail(&lxc_conf->id_map, idmaplist);
46ad64ab
CB
2121
2122 if (!lxc_conf->root_nsuid_map && idmap->idtype == ID_TYPE_UID)
2123 if (idmap->nsid == 0)
2124 lxc_conf->root_nsuid_map = idmap;
2125
4160c3a0 2126 if (!lxc_conf->root_nsgid_map && idmap->idtype == ID_TYPE_GID)
46ad64ab
CB
2127 if (idmap->nsid == 0)
2128 lxc_conf->root_nsgid_map = idmap;
2129
34a7a4c6 2130 idmap = NULL;
7e60c3f0 2131
f6d3e3e4
SH
2132 return 0;
2133
34a7a4c6 2134on_error:
f10fad2f 2135 free(idmaplist);
34a7a4c6 2136 free(idmap);
f6d3e3e4
SH
2137
2138 return -1;
2139}
2140
47148e96
CB
2141static int set_config_mount_fstab(const char *key, const char *value,
2142 struct lxc_conf *lxc_conf, void *data)
d95db067 2143{
663e9916 2144 if (lxc_config_value_empty(value)) {
47148e96 2145 clr_config_mount_fstab(key, lxc_conf, NULL);
d9192f5d 2146 return -1;
46f3de30 2147 }
6f5685f0 2148
713046e3 2149 return set_config_path_item(&lxc_conf->fstab, value);
d95db067
DE
2150}
2151
713046e3 2152static int set_config_mount_auto(const char *key, const char *value,
c7e27aaf 2153 struct lxc_conf *lxc_conf, void *data)
368bbc02 2154{
62dd965e 2155 char *autos, *token;
368bbc02
CS
2156 int i;
2157 int ret = -1;
504a2217
CB
2158 static struct {
2159 const char *token;
2160 int mask;
2161 int flag;
2162 } allowed_auto_mounts[] = {
6812d833
CB
2163 { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
2164 { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
2165 { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW },
2166 { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
2167 { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO },
2168 { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
2169 { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW },
2170 { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC },
2171 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED },
2172 { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO },
2173 { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW },
2174 { "cgroup:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC | LXC_AUTO_CGROUP_FORCE },
2175 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED | LXC_AUTO_CGROUP_FORCE },
2176 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO | LXC_AUTO_CGROUP_FORCE },
2177 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW | LXC_AUTO_CGROUP_FORCE },
2178 { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC },
2179 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED },
2180 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO },
2181 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW },
2182 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC | LXC_AUTO_CGROUP_FORCE },
2183 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED | LXC_AUTO_CGROUP_FORCE },
2184 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO | LXC_AUTO_CGROUP_FORCE },
2185 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW | LXC_AUTO_CGROUP_FORCE },
adf0ba1f 2186 { "shmounts:", LXC_AUTO_SHMOUNTS_MASK, LXC_AUTO_SHMOUNTS },
25a908b8 2187 /* For adding anything that is just a single on/off, but has no
3f69fb12
SY
2188 * options: keep mask and flag identical and just define the enum
2189 * value as an unused bit so far
504a2217 2190 */
3f69fb12 2191 { NULL, 0, 0 }
504a2217 2192 };
368bbc02 2193
663e9916 2194 if (lxc_config_value_empty(value)) {
d9192f5d
SH
2195 lxc_conf->auto_mounts = 0;
2196 return 0;
2197 }
368bbc02
CS
2198
2199 autos = strdup(value);
25a908b8 2200 if (!autos)
368bbc02 2201 return -1;
368bbc02 2202
62dd965e 2203 lxc_iterate_parts(token, autos, " \t") {
0d190408
LT
2204 bool is_shmounts = false;
2205
368bbc02
CS
2206 for (i = 0; allowed_auto_mounts[i].token; i++) {
2207 if (!strcmp(allowed_auto_mounts[i].token, token))
2208 break;
0d190408 2209
fd14fdb8 2210 if (strcmp("shmounts:", allowed_auto_mounts[i].token) == 0 &&
6333c915 2211 strncmp("shmounts:", token, STRLITERALLEN("shmounts:")) == 0) {
0d190408
LT
2212 is_shmounts = true;
2213 break;
2214 }
368bbc02
CS
2215 }
2216
2217 if (!allowed_auto_mounts[i].token) {
25a908b8 2218 ERROR("Invalid filesystem to automount \"%s\"", token);
62dd965e 2219 goto on_error;
368bbc02
CS
2220 }
2221
b06b8511 2222 lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask;
368bbc02 2223 lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag;
47903908 2224
0d190408 2225 if (is_shmounts) {
6437f1c1
CB
2226 char *container_path;
2227 char *host_path;
594426ca 2228
6437f1c1
CB
2229 host_path = token + STRLITERALLEN("shmounts:");
2230 if (*host_path == '\0') {
fd14fdb8 2231 SYSERROR("Failed to copy shmounts host path");
62dd965e 2232 goto on_error;
fd14fdb8
LT
2233 }
2234
6437f1c1
CB
2235 container_path = strchr(host_path, ':');
2236 if (!container_path || *(container_path + 1) == '\0')
2237 container_path = "/dev/.lxc-mounts";
2238 else
2239 *container_path++ = '\0';
2240
6437f1c1 2241 lxc_conf->shmount.path_host = strdup(host_path);
594426ca
CB
2242 if (!lxc_conf->shmount.path_host) {
2243 SYSERROR("Failed to copy shmounts host path");
62dd965e 2244 goto on_error;
0d190408 2245 }
fd14fdb8 2246
6437f1c1 2247 lxc_conf->shmount.path_cont = strdup(container_path);
fd14fdb8
LT
2248 if(!lxc_conf->shmount.path_cont) {
2249 SYSERROR("Failed to copy shmounts container path");
62dd965e 2250 goto on_error;
fd14fdb8 2251 }
0d190408 2252 }
d028235d 2253 }
368bbc02 2254
62dd965e
CB
2255 ret = 0;
2256
2257on_error:
368bbc02 2258 free(autos);
62dd965e 2259
368bbc02
CS
2260 return ret;
2261}
2262
713046e3 2263static int set_config_mount(const char *key, const char *value,
c7e27aaf 2264 struct lxc_conf *lxc_conf, void *data)
e7938e9e 2265{
e7938e9e
MN
2266 char *mntelem;
2267 struct lxc_list *mntlist;
2268
663e9916 2269 if (lxc_config_value_empty(value))
d9192f5d 2270 return lxc_clear_mount_entries(lxc_conf);
e7938e9e
MN
2271
2272 mntlist = malloc(sizeof(*mntlist));
2273 if (!mntlist)
2274 return -1;
2275
2276 mntelem = strdup(value);
00b6be44
SH
2277 if (!mntelem) {
2278 free(mntlist);
bf83c5b9 2279 return -1;
00b6be44 2280 }
e7938e9e
MN
2281 mntlist->elem = mntelem;
2282
2283 lxc_list_add_tail(&lxc_conf->mount_list, mntlist);
2284
2285 return 0;
2286}
2287
0d190408
LT
2288int add_elem_to_mount_list(const char *value, struct lxc_conf *lxc_conf) {
2289 return set_config_mount(NULL, value, lxc_conf, NULL);
2290}
2291
713046e3 2292static int set_config_cap_keep(const char *key, const char *value,
c7e27aaf 2293 struct lxc_conf *lxc_conf, void *data)
1fb86a7c 2294{
62dd965e 2295 char *keepcaps, *token;
1fb86a7c
SH
2296 struct lxc_list *keeplist;
2297 int ret = -1;
2298
663e9916 2299 if (lxc_config_value_empty(value))
7d0eb87e 2300 return lxc_clear_config_keepcaps(lxc_conf);
1fb86a7c
SH
2301
2302 keepcaps = strdup(value);
25a908b8 2303 if (!keepcaps)
1fb86a7c 2304 return -1;
1fb86a7c 2305
504a2217
CB
2306 /* In case several capability keep is specified in a single line
2307 * split these caps in a single element for the list.
2308 */
62dd965e 2309 lxc_iterate_parts(token, keepcaps, " \t") {
7035407c
DE
2310 if (!strcmp(token, "none"))
2311 lxc_clear_config_keepcaps(lxc_conf);
2312
1fb86a7c 2313 keeplist = malloc(sizeof(*keeplist));
25a908b8 2314 if (!keeplist)
62dd965e 2315 goto on_error;
1fb86a7c
SH
2316
2317 keeplist->elem = strdup(token);
2318 if (!keeplist->elem) {
1fb86a7c 2319 free(keeplist);
62dd965e 2320 goto on_error;
1fb86a7c
SH
2321 }
2322
2323 lxc_list_add_tail(&lxc_conf->keepcaps, keeplist);
d028235d 2324 }
1fb86a7c 2325
62dd965e
CB
2326 ret = 0;
2327
2328on_error:
1fb86a7c
SH
2329 free(keepcaps);
2330
2331 return ret;
2332}
2333
713046e3 2334static int set_config_cap_drop(const char *key, const char *value,
c7e27aaf 2335 struct lxc_conf *lxc_conf, void *data)
81810dd1 2336{
62dd965e 2337 char *dropcaps, *token;
81810dd1
DL
2338 struct lxc_list *droplist;
2339 int ret = -1;
2340
663e9916 2341 if (lxc_config_value_empty(value))
7d0eb87e 2342 return lxc_clear_config_caps(lxc_conf);
81810dd1
DL
2343
2344 dropcaps = strdup(value);
25a908b8 2345 if (!dropcaps)
81810dd1 2346 return -1;
81810dd1 2347
504a2217
CB
2348 /* In case several capability drop is specified in a single line
2349 * split these caps in a single element for the list.
2350 */
62dd965e 2351 lxc_iterate_parts(token, dropcaps, " \t") {
81810dd1 2352 droplist = malloc(sizeof(*droplist));
25a908b8 2353 if (!droplist)
62dd965e 2354 goto on_error;
81810dd1
DL
2355
2356 droplist->elem = strdup(token);
2357 if (!droplist->elem) {
81810dd1 2358 free(droplist);
62dd965e 2359 goto on_error;
81810dd1
DL
2360 }
2361
2362 lxc_list_add_tail(&lxc_conf->caps, droplist);
d028235d 2363 }
81810dd1 2364
62dd965e
CB
2365 ret = 0;
2366
2367on_error:
81810dd1
DL
2368 free(dropcaps);
2369
2370 return ret;
2371}
2372
3aed4934
CB
2373static int set_config_console_path(const char *key, const char *value,
2374 struct lxc_conf *lxc_conf, void *data)
28a4b0e5 2375{
713046e3 2376 return set_config_path_item(&lxc_conf->console.path, value);
28a4b0e5
DL
2377}
2378
d91adfa6
CB
2379static int set_config_console_rotate(const char *key, const char *value,
2380 struct lxc_conf *lxc_conf, void *data)
2381{
2382 if (lxc_config_value_empty(value)) {
2383 lxc_conf->console.log_rotate = 0;
2384 return 0;
2385 }
2386
2387 if (lxc_safe_uint(value, &lxc_conf->console.log_rotate) < 0)
2388 return -1;
2389
966b9ecd
CB
2390 if (lxc_conf->console.log_rotate > 1) {
2391 ERROR("The \"lxc.console.rotate\" config key can only be set "
2392 "to 0 or 1");
d91adfa6 2393 return -1;
966b9ecd 2394 }
d91adfa6
CB
2395
2396 return 0;
2397}
2398
713046e3 2399static int set_config_console_logfile(const char *key, const char *value,
c7e27aaf 2400 struct lxc_conf *lxc_conf, void *data)
96f15ca1 2401{
713046e3 2402 return set_config_path_item(&lxc_conf->console.log_path, value);
96f15ca1
SH
2403}
2404
28f3b1cd
CB
2405static int set_config_console_buffer_size(const char *key, const char *value,
2406 struct lxc_conf *lxc_conf, void *data)
a04220de
CB
2407{
2408 int ret;
2409 int64_t size;
28f3b1cd 2410 uint64_t buffer_size, pgsz;
a04220de
CB
2411
2412 if (lxc_config_value_empty(value)) {
28f3b1cd 2413 lxc_conf->console.buffer_size = 0;
a04220de
CB
2414 return 0;
2415 }
2416
2417 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2418 if (!strcmp(value, "auto")) {
28f3b1cd 2419 lxc_conf->console.buffer_size = 1 << 17;
a04220de
CB
2420 return 0;
2421 }
2422
2423 ret = parse_byte_size_string(value, &size);
2424 if (ret < 0)
2425 return -1;
2426
2427 if (size < 0)
2428 return -EINVAL;
2429
2430 /* must be at least a page size */
2431 pgsz = lxc_getpagesize();
2432 if ((uint64_t)size < pgsz) {
2433 NOTICE("Requested ringbuffer size for the console is %" PRId64
2434 " but must be at least %" PRId64
2435 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
2436 size, pgsz, pgsz);
2437 size = pgsz;
2438 }
2439
28f3b1cd
CB
2440 buffer_size = lxc_find_next_power2((uint64_t)size);
2441 if (buffer_size == 0)
a04220de
CB
2442 return -EINVAL;
2443
28f3b1cd 2444 if (buffer_size != size)
a04220de 2445 NOTICE("Passed size was not a power of 2. Rounding log size to "
28f3b1cd 2446 "next power of two: %" PRIu64 " bytes", buffer_size);
a04220de 2447
28f3b1cd 2448 lxc_conf->console.buffer_size = buffer_size;
47903908 2449
a04220de
CB
2450 return 0;
2451}
2452
861813e5
CB
2453static int set_config_console_size(const char *key, const char *value,
2454 struct lxc_conf *lxc_conf, void *data)
2455{
2456 int ret;
2457 int64_t size;
2458 uint64_t log_size, pgsz;
2459
2460 if (lxc_config_value_empty(value)) {
2461 lxc_conf->console.log_size = 0;
2462 return 0;
2463 }
2464
2465 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2466 if (!strcmp(value, "auto")) {
2467 lxc_conf->console.log_size = 1 << 17;
2468 return 0;
2469 }
2470
2471 ret = parse_byte_size_string(value, &size);
2472 if (ret < 0)
2473 return -1;
2474
2475 if (size < 0)
2476 return -EINVAL;
2477
2478 /* must be at least a page size */
2479 pgsz = lxc_getpagesize();
2480 if ((uint64_t)size < pgsz) {
2481 NOTICE("Requested ringbuffer size for the console is %" PRId64
2482 " but must be at least %" PRId64
2483 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
2484 size, pgsz, pgsz);
2485 size = pgsz;
2486 }
2487
2488 log_size = lxc_find_next_power2((uint64_t)size);
2489 if (log_size == 0)
2490 return -EINVAL;
2491
2492 if (log_size != size)
2493 NOTICE("Passed size was not a power of 2. Rounding log size to "
2494 "next power of two: %" PRIu64 " bytes", log_size);
2495
2496 lxc_conf->console.log_size = log_size;
47903908 2497
861813e5
CB
2498 return 0;
2499}
2500
5648fc19
CB
2501/*
2502 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2503 * file, we expand it in the unexpanded_config, so that after a save_config we
2504 * store the hwaddr for re-use.
2505 * This is only called when reading the config file, not when executing a
2506 * lxc.include.
2507 * 'x' and 'X' are substituted in-place.
2508 */
2509static void update_hwaddr(const char *line)
2510{
2511 char *p;
2512
2513 line += lxc_char_left_gc(line, strlen(line));
2514 if (line[0] == '#')
2515 return;
2516
2517 if (!lxc_config_net_is_hwaddr(line))
2518 return;
2519
2520 /* Let config_net_hwaddr raise the error. */
2521 p = strchr(line, '=');
2522 if (!p)
2523 return;
2524 p++;
2525
2526 while (isblank(*p))
2527 p++;
2528
2529 if (!*p)
2530 return;
2531
2532 rand_complete_hwaddr(p);
2533}
2534
6b0d5538 2535int append_unexp_config_line(const char *line, struct lxc_conf *conf)
f979ac15 2536{
62a821f1
CB
2537 size_t linelen;
2538 size_t len = conf->unexpanded_len;
f979ac15 2539
e6744e9b
SH
2540 update_hwaddr(line);
2541
62a821f1 2542 linelen = strlen(line);
6b0d5538 2543 while (conf->unexpanded_alloced <= len + linelen + 2) {
504a2217
CB
2544 char *tmp = realloc(conf->unexpanded_config,
2545 conf->unexpanded_alloced + 1024);
6b0d5538
SH
2546 if (!tmp)
2547 return -1;
504a2217 2548
6b0d5538
SH
2549 if (!conf->unexpanded_config)
2550 *tmp = '\0';
62a821f1 2551
6b0d5538
SH
2552 conf->unexpanded_config = tmp;
2553 conf->unexpanded_alloced += 1024;
2554 }
efed99a4 2555
62a821f1 2556 memcpy(conf->unexpanded_config + conf->unexpanded_len, line, linelen);
6b0d5538 2557 conf->unexpanded_len += linelen;
62a821f1
CB
2558 if (line[linelen - 1] != '\n')
2559 conf->unexpanded_config[conf->unexpanded_len++] = '\n';
2560 conf->unexpanded_config[conf->unexpanded_len] = '\0';
25a908b8 2561
f979ac15
SH
2562 return 0;
2563}
2564
e1daebd9
SH
2565static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf)
2566{
4110345b 2567 __do_closedir DIR *dir = NULL;
74f96976 2568 struct dirent *direntp;
18740311 2569 char path[PATH_MAX];
504a2217
CB
2570 int len;
2571 int ret = -1;
e1daebd9
SH
2572
2573 dir = opendir(dirp);
25a908b8 2574 if (!dir)
e1daebd9 2575 return -1;
e1daebd9 2576
74f96976 2577 while ((direntp = readdir(dir))) {
e1daebd9 2578 const char *fnam;
e1daebd9
SH
2579
2580 fnam = direntp->d_name;
2581 if (!strcmp(fnam, "."))
2582 continue;
2583
2584 if (!strcmp(fnam, ".."))
2585 continue;
2586
2587 len = strlen(fnam);
504a2217 2588 if (len < 6 || strncmp(fnam + len - 5, ".conf", 5) != 0)
e1daebd9 2589 continue;
25a908b8 2590
18740311 2591 len = snprintf(path, PATH_MAX, "%s/%s", dirp, fnam);
4110345b
CB
2592 if (len < 0 || len >= PATH_MAX)
2593 return -1;
e1daebd9
SH
2594
2595 ret = lxc_config_read(path, lxc_conf, true);
2596 if (ret < 0)
4110345b 2597 return -1;
e1daebd9 2598 }
e1daebd9 2599
4110345b 2600 return 0;
e1daebd9
SH
2601}
2602
973082f5 2603static int set_config_includefiles(const char *key, const char *value,
c7e27aaf 2604 struct lxc_conf *lxc_conf, void *data)
09ad6246 2605{
663e9916 2606 if (lxc_config_value_empty(value)) {
26471403 2607 clr_config_includefiles(key, lxc_conf, NULL);
616422f1 2608 return 0;
355c5701 2609 }
616422f1 2610
e1daebd9
SH
2611 if (is_dir(value))
2612 return do_includedir(value, lxc_conf);
2613
6b0d5538 2614 return lxc_config_read(value, lxc_conf, true);
09ad6246
SH
2615}
2616
7a96a068
CB
2617static int set_config_rootfs_path(const char *key, const char *value,
2618 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 2619{
693dbdb9
CB
2620 int ret;
2621 char *dup, *tmp;
2622 const char *container_path;
2623
2624 if (lxc_config_value_empty(value)) {
2625 free(lxc_conf->rootfs.path);
2626 lxc_conf->rootfs.path = NULL;
2627 return 0;
2628 }
2629
2630 dup = strdup(value);
2631 if (!dup)
2632 return -1;
2633
2634 /* Split <storage type>:<container path> into <storage type> and
2635 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2636 * "rootfs.path" to <container path>.
2637 */
2638 tmp = strchr(dup, ':');
2639 if (tmp) {
2640 *tmp = '\0';
47903908 2641
693dbdb9
CB
2642 ret = set_config_path_item(&lxc_conf->rootfs.bdev_type, dup);
2643 if (ret < 0) {
2644 free(dup);
2645 return -1;
2646 }
47903908 2647
693dbdb9
CB
2648 tmp++;
2649 container_path = tmp;
2650 } else {
2651 container_path = value;
2652 }
2653
2654 ret = set_config_path_item(&lxc_conf->rootfs.path, container_path);
2655 free(dup);
47903908 2656
693dbdb9 2657 return ret;
c2cc9f0a 2658}
2659
6e54330c
CB
2660static int set_config_rootfs_managed(const char *key, const char *value,
2661 struct lxc_conf *lxc_conf, void *data)
2662{
8f818a84 2663 return set_config_bool_item(&lxc_conf->rootfs.managed, value, true);
6e54330c
CB
2664}
2665
713046e3 2666static int set_config_rootfs_mount(const char *key, const char *value,
c7e27aaf 2667 struct lxc_conf *lxc_conf, void *data)
23b7ea69 2668{
713046e3 2669 return set_config_path_item(&lxc_conf->rootfs.mount, value);
23b7ea69
DL
2670}
2671
713046e3 2672static int set_config_rootfs_options(const char *key, const char *value,
c7e27aaf 2673 struct lxc_conf *lxc_conf, void *data)
a17b1e65 2674{
3437f95c
CB
2675 unsigned long mflags = 0, pflags = 0;
2676 char *mdata = NULL, *opts = NULL;
a08bfbe3 2677 int ret;
3437f95c
CB
2678 struct lxc_rootfs *rootfs = &lxc_conf->rootfs;
2679
2680 ret = parse_mntopts(value, &mflags, &mdata);
2681 if (ret < 0)
2682 return -EINVAL;
2683
2684 ret = parse_propagationopts(value, &pflags);
2685 if (ret < 0) {
2686 free(mdata);
2687 return -EINVAL;
2688 }
2689
2690 ret = set_config_string_item(&opts, value);
2691 if (ret < 0) {
2692 free(mdata);
2693 return -ENOMEM;
2694 }
2695
2696 rootfs->mountflags = mflags | pflags;
2697 rootfs->options = opts;
2698 rootfs->data = mdata;
2699
2700 return 0;
a17b1e65
SG
2701}
2702
b67771bc 2703static int set_config_uts_name(const char *key, const char *value,
c7e27aaf 2704 struct lxc_conf *lxc_conf, void *data)
c2cc9f0a 2705{
2706 struct utsname *utsname;
2707
663e9916 2708 if (lxc_config_value_empty(value)) {
b67771bc 2709 clr_config_uts_name(key, lxc_conf, NULL);
1939e73d 2710 return 0;
00cd8039 2711 }
1939e73d 2712
c2cc9f0a 2713 utsname = malloc(sizeof(*utsname));
25a908b8 2714 if (!utsname)
c2cc9f0a 2715 return -1;
c2cc9f0a 2716
2717 if (strlen(value) >= sizeof(utsname->nodename)) {
b6f24d54 2718 free(utsname);
c2cc9f0a 2719 return -1;
2720 }
2721
43f984ea 2722 (void)strlcpy(utsname->nodename, value, sizeof(utsname->nodename));
f10fad2f 2723 free(lxc_conf->utsname);
c2cc9f0a 2724 lxc_conf->utsname = utsname;
2725
2726 return 0;
2727}
2728
1d8d3676
CB
2729static int set_config_namespace_clone(const char *key, const char *value,
2730 struct lxc_conf *lxc_conf, void *data)
2731{
62dd965e 2732 char *ns, *token;
1d8d3676 2733 int cloneflag = 0;
1d8d3676
CB
2734
2735 if (lxc_config_value_empty(value))
2736 return clr_config_namespace_clone(key, lxc_conf, data);
2737
8bc8c715 2738 if (lxc_conf->ns_keep != 0) {
6d1400b5 2739 errno = EINVAL;
2740 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2741 "\"lxc.namespace.keep\"");
8bc8c715
CB
2742 return -EINVAL;
2743 }
2744
1d8d3676
CB
2745 ns = strdup(value);
2746 if (!ns)
2747 return -1;
1d8d3676 2748
62dd965e 2749 lxc_iterate_parts(token, ns, " \t") {
1d8d3676
CB
2750 token += lxc_char_left_gc(token, strlen(token));
2751 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2752 cloneflag = lxc_namespace_2_cloneflag(token);
2753 if (cloneflag < 0) {
2754 free(ns);
2755 return -EINVAL;
2756 }
2757 lxc_conf->ns_clone |= cloneflag;
2758 }
2759 free(ns);
2760
2761 return 0;
2762}
2763
abeb5bba
CB
2764static int set_config_namespace_keep(const char *key, const char *value,
2765 struct lxc_conf *lxc_conf, void *data)
2766{
62dd965e 2767 char *ns, *token;
abeb5bba 2768 int cloneflag = 0;
abeb5bba
CB
2769
2770 if (lxc_config_value_empty(value))
2771 return clr_config_namespace_keep(key, lxc_conf, data);
2772
8bc8c715 2773 if (lxc_conf->ns_clone != 0) {
6d1400b5 2774 errno = EINVAL;
2775 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2776 "\"lxc.namespace.keep\"");
8bc8c715
CB
2777 return -EINVAL;
2778 }
2779
abeb5bba
CB
2780 ns = strdup(value);
2781 if (!ns)
2782 return -1;
abeb5bba 2783
62dd965e 2784 lxc_iterate_parts(token, ns, " \t") {
abeb5bba
CB
2785 token += lxc_char_left_gc(token, strlen(token));
2786 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2787 cloneflag = lxc_namespace_2_cloneflag(token);
2788 if (cloneflag < 0) {
2789 free(ns);
2790 return -EINVAL;
2791 }
2792 lxc_conf->ns_keep |= cloneflag;
2793 }
2794 free(ns);
2795
2796 return 0;
2797}
2798
70fd7fc9
CB
2799static int set_config_time_offset_boot(const char *key, const char *value,
2800 struct lxc_conf *lxc_conf, void *data)
2801{
2802 int ret;
2803 char *unit;
2804 int64_t offset = 0;
2805 char buf[STRLITERALLEN("ms") + 1];
2806
2807 if (lxc_config_value_empty(value))
2808 return clr_config_time_offset_boot(key, lxc_conf, data);
2809
2810 ret = lxc_safe_int64_residual(value, &offset, 10, buf, sizeof(buf));
2811 if (ret)
2812 return ret;
2813
70fd7fc9 2814 unit = lxc_trim_whitespace_in_place(buf);
07f89c1e
CB
2815 if (strcmp(unit, "h") == 0) {
2816 if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_boot))
2817 return -EOVERFLOW;
2818 } else if (strcmp(unit, "m") == 0) {
2819 if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_boot))
2820 return -EOVERFLOW;
2821 } else if (strcmp(unit, "s") == 0) {
70fd7fc9 2822 lxc_conf->timens.s_boot = offset;
07f89c1e
CB
2823 } else if (strcmp(unit, "ms") == 0) {
2824 if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_boot))
2825 return -EOVERFLOW;
2826 } else if (strcmp(unit, "us") == 0) {
2827 if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_boot))
2828 return -EOVERFLOW;
2829 } else if (strcmp(unit, "ns") == 0) {
70fd7fc9 2830 lxc_conf->timens.ns_boot = offset;
07f89c1e 2831 } else {
70fd7fc9 2832 return ret_errno(EINVAL);
07f89c1e 2833 }
70fd7fc9
CB
2834
2835 return 0;
2836}
2837
2838static int set_config_time_offset_monotonic(const char *key, const char *value,
2839 struct lxc_conf *lxc_conf, void *data)
2840{
2841 int ret;
2842 char *unit;
2843 int64_t offset = 0;
2844 char buf[STRLITERALLEN("ms") + 1];
2845
2846 if (lxc_config_value_empty(value))
2847 return clr_config_time_offset_monotonic(key, lxc_conf, data);
2848
2849 ret = lxc_safe_int64_residual(value, &offset, 10, buf, sizeof(buf));
2850 if (ret)
2851 return ret;
2852
70fd7fc9 2853 unit = lxc_trim_whitespace_in_place(buf);
07f89c1e
CB
2854 if (strcmp(unit, "h") == 0) {
2855 if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_monotonic))
2856 return -EOVERFLOW;
2857 } else if (strcmp(unit, "m") == 0) {
2858 if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_monotonic))
2859 return -EOVERFLOW;
2860 } else if (strcmp(unit, "s") == 0) {
70fd7fc9 2861 lxc_conf->timens.s_monotonic = offset;
07f89c1e
CB
2862 } else if (strcmp(unit, "ms") == 0) {
2863 if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_monotonic))
2864 return -EOVERFLOW;
2865 } else if (strcmp(unit, "us") == 0) {
2866 if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_monotonic))
2867 return -EOVERFLOW;
2868 } else if (strcmp(unit, "ns") == 0) {
70fd7fc9 2869 lxc_conf->timens.ns_monotonic = offset;
07f89c1e 2870 } else {
70fd7fc9 2871 return ret_errno(EINVAL);
07f89c1e 2872 }
70fd7fc9
CB
2873
2874 return 0;
2875}
2876
b074bbf1
CB
2877static int set_config_namespace_share(const char *key, const char *value,
2878 struct lxc_conf *lxc_conf, void *data)
28d9e29e
CB
2879{
2880 int ns_idx;
2881 const char *namespace;
2882
2883 if (lxc_config_value_empty(value))
b074bbf1 2884 return clr_config_namespace_share(key, lxc_conf, data);
28d9e29e 2885
6333c915 2886 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
2887 ns_idx = lxc_namespace_2_ns_idx(namespace);
2888 if (ns_idx < 0)
2889 return ns_idx;
2890
b074bbf1 2891 return set_config_string_item(&lxc_conf->ns_share[ns_idx], value);
28d9e29e
CB
2892}
2893
6b0d5538
SH
2894struct parse_line_conf {
2895 struct lxc_conf *conf;
2896 bool from_include;
2897};
4184c3e1 2898
7a7ff0c6 2899static int parse_line(char *buffer, void *data)
c2cc9f0a 2900{
504a2217 2901 char *dot, *key, *line, *linep, *value;
3b13691d
CB
2902 bool empty_line;
2903 struct lxc_config_t *config;
504a2217 2904 int ret = 0;
3b13691d
CB
2905 char *dup = buffer;
2906 struct parse_line_conf *plc = data;
c2cc9f0a 2907
3b13691d
CB
2908 /* If there are newlines in the config file we should keep them. */
2909 empty_line = lxc_is_line_empty(dup);
2910 if (empty_line)
2911 dup = "\n";
c2cc9f0a 2912
25a908b8
CB
2913 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2914 * modified the original string on the stack by replacing '=' by '\0'
2915 * below.
91480a0f 2916 */
3b13691d 2917 linep = line = strdup(dup);
25a908b8 2918 if (!line)
81192358 2919 return -1;
91480a0f 2920
3b13691d
CB
2921 if (!plc->from_include) {
2922 ret = append_unexp_config_line(line, plc->conf);
2923 if (ret < 0)
2924 goto on_error;
2925 }
2926
2927 if (empty_line)
327500d4 2928 goto on_error;
6b0d5538 2929
b2718c72 2930 line += lxc_char_left_gc(line, strlen(line));
476d4cf1 2931
4184c3e1
SH
2932 /* ignore comments */
2933 if (line[0] == '#')
3b13691d 2934 goto on_error;
476d4cf1 2935
6b0d5538
SH
2936 /* martian option - don't add it to the config itself */
2937 if (strncmp(line, "lxc.", 4))
3b13691d 2938 goto on_error;
4184c3e1 2939
476d4cf1 2940 ret = -1;
c2cc9f0a 2941
3b13691d 2942 dot = strchr(line, '=');
c2cc9f0a 2943 if (!dot) {
3b13691d
CB
2944 ERROR("Invalid configuration line: %s", line);
2945 goto on_error;
c2cc9f0a 2946 }
a871ff6b 2947
c2cc9f0a 2948 *dot = '\0';
2949 value = dot + 1;
2950
b2718c72 2951 key = line;
2952 key[lxc_char_right_gc(key, strlen(key))] = '\0';
c2cc9f0a 2953
b2718c72 2954 value += lxc_char_left_gc(value, strlen(value));
2955 value[lxc_char_right_gc(value, strlen(value))] = '\0';
c2cc9f0a 2956
bd878dee 2957 if (*value == '\'' || *value == '\"') {
25a908b8
CB
2958 size_t len;
2959
2960 len = strlen(value);
504a2217
CB
2961 if (len > 1 && value[len - 1] == *value) {
2962 value[len - 1] = '\0';
bd878dee
SB
2963 value++;
2964 }
2965 }
2966
300df83e 2967 config = lxc_get_config(key);
c2cc9f0a 2968 if (!config) {
25a908b8 2969 ERROR("Unknown configuration key \"%s\"", key);
3b13691d 2970 goto on_error;
c2cc9f0a 2971 }
2972
bbc079cf 2973 ret = config->set(key, value, plc->conf, NULL);
91480a0f 2974
3b13691d 2975on_error:
81192358 2976 free(linep);
47903908 2977
91480a0f 2978 return ret;
c2cc9f0a 2979}
2980
d899f11b 2981static struct new_config_item *parse_new_conf_line(char *buffer)
2982{
2983 char *dot, *key, *line, *linep, *value;
2984 int ret = 0;
2985 char *dup = buffer;
2986 struct new_config_item *new = NULL;
2987
2988 linep = line = strdup(dup);
2989 if (!line)
2990 return NULL;
2991
2992 line += lxc_char_left_gc(line, strlen(line));
2993
2994 /* martian option - don't add it to the config itself */
85d49873 2995 if (strncmp(line, "lxc.", 4))
d899f11b 2996 goto on_error;
2997
2998 ret = -1;
2999 dot = strchr(line, '=');
3000 if (!dot) {
3001 ERROR("Invalid configuration item: %s", line);
3002 goto on_error;
3003 }
3004
3005 *dot = '\0';
3006 value = dot + 1;
3007
3008 key = line;
3009 key[lxc_char_right_gc(key, strlen(key))] = '\0';
3010
3011 value += lxc_char_left_gc(value, strlen(value));
3012 value[lxc_char_right_gc(value, strlen(value))] = '\0';
3013
3014 if (*value == '\'' || *value == '\"') {
3015 size_t len;
3016
3017 len = strlen(value);
3018 if (len > 1 && value[len - 1] == *value) {
3019 value[len - 1] = '\0';
3020 value++;
3021 }
3022 }
3023
3024 ret = -1;
3025 new = malloc(sizeof(struct new_config_item));
3026 if (!new)
3027 goto on_error;
3028
3029 new->key = strdup(key);
3030 new->val = strdup(value);
3031 if (!new->val || !new->key)
3032 goto on_error;
3033
3034 ret = 0;
3035
3036on_error:
3037 free(linep);
3038
3039 if (ret < 0 && new) {
3040 free(new->key);
3041 free(new->val);
3042 free(new);
3043 new = NULL;
3044 }
3045
3046 return new;
3047}
3048
6b0d5538 3049int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
c2cc9f0a 3050{
6b0d5538
SH
3051 struct parse_line_conf c;
3052
3053 c.conf = conf;
3054 c.from_include = from_include;
f979ac15 3055
25a908b8 3056 /* Catch only the top level config file name in the structure. */
504a2217 3057 if (!conf->rcfile)
76d0127f 3058 conf->rcfile = strdup(file);
f979ac15 3059
9994d140 3060 return lxc_file_for_each_line_mmap(file, parse_line, &c);
c2cc9f0a 3061}
62e46035 3062
504a2217 3063int lxc_config_define_add(struct lxc_list *defines, char *arg)
62e46035
CLG
3064{
3065 struct lxc_list *dent;
3066
3067 dent = malloc(sizeof(struct lxc_list));
3068 if (!dent)
3069 return -1;
3070
d899f11b 3071 dent->elem = parse_new_conf_line(arg);
3072 if (!dent->elem) {
3073 free(dent);
3074 return -1;
3075 }
3076
62e46035 3077 lxc_list_add_tail(defines, dent);
47903908 3078
62e46035
CLG
3079 return 0;
3080}
3081
eb0c9382 3082bool lxc_config_define_load(struct lxc_list *defines, struct lxc_container *c)
62e46035 3083{
eb0c9382 3084 struct lxc_list *it;
3085 bool bret = true;
62e46035
CLG
3086
3087 lxc_list_for_each(it, defines) {
eb0c9382 3088 struct new_config_item *new_item = it->elem;
3089 bret = c->set_config_item(c, new_item->key, new_item->val);
3090 if (!bret)
62e46035
CLG
3091 break;
3092 }
3093
eb0c9382 3094 lxc_config_define_free(defines);
47903908 3095
eb0c9382 3096 return bret;
3097}
3098
3099void lxc_config_define_free(struct lxc_list *defines)
3100{
3101 struct lxc_list *it, *next;
3102
9ebb03ad 3103 lxc_list_for_each_safe(it, defines, next) {
eb0c9382 3104 struct new_config_item *new_item = it->elem;
3105 free(new_item->key);
3106 free(new_item->val);
62e46035
CLG
3107 lxc_list_del(it);
3108 free(it);
3109 }
62e46035 3110}
525f0002
CS
3111
3112signed long lxc_config_parse_arch(const char *arch)
3113{
504a2217
CB
3114#if HAVE_SYS_PERSONALITY_H
3115 size_t i;
525f0002
CS
3116 struct per_name {
3117 char *name;
3118 unsigned long per;
bb8d8207 3119 } pername[] = {
8f3e3c13
CB
3120 { "arm", PER_LINUX32 },
3121 { "armel", PER_LINUX32 },
3122 { "armhf", PER_LINUX32 },
3123 { "armv7l", PER_LINUX32 },
3124 { "athlon", PER_LINUX32 },
504a2217
CB
3125 { "i386", PER_LINUX32 },
3126 { "i486", PER_LINUX32 },
3127 { "i586", PER_LINUX32 },
3128 { "i686", PER_LINUX32 },
8f3e3c13 3129 { "linux32", PER_LINUX32 },
504a2217
CB
3130 { "mips", PER_LINUX32 },
3131 { "mipsel", PER_LINUX32 },
3132 { "ppc", PER_LINUX32 },
504a2217 3133 { "powerpc", PER_LINUX32 },
8f3e3c13 3134 { "x86", PER_LINUX32 },
504a2217 3135 { "amd64", PER_LINUX },
8f3e3c13
CB
3136 { "arm64", PER_LINUX },
3137 { "linux64", PER_LINUX },
504a2217
CB
3138 { "mips64", PER_LINUX },
3139 { "mips64el", PER_LINUX },
3140 { "ppc64", PER_LINUX },
504a2217 3141 { "ppc64el", PER_LINUX },
8f3e3c13 3142 { "ppc64le", PER_LINUX },
504a2217
CB
3143 { "powerpc64", PER_LINUX },
3144 { "s390x", PER_LINUX },
8f3e3c13 3145 { "x86_64", PER_LINUX },
525f0002
CS
3146 };
3147 size_t len = sizeof(pername) / sizeof(pername[0]);
3148
8f3e3c13 3149 for (i = 0; i < len; i++)
525f0002 3150 if (!strcmp(pername[i].name, arch))
504a2217 3151 return pername[i].per;
504a2217 3152#endif
525f0002
CS
3153
3154 return -1;
3155}
72d0e1cb 3156
eb0c9382 3157int lxc_fill_elevated_privileges(char *flaglist, int *flags)
3158{
62dd965e 3159 char *token;
eb0c9382 3160 int i, aflag;
3161 struct {
3162 const char *token;
3163 int flag;
3164 } all_privs[] = {
3165 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP },
3166 { "CAP", LXC_ATTACH_DROP_CAPABILITIES },
3167 { "LSM", LXC_ATTACH_LSM_EXEC },
3168 { NULL, 0 }
3169 };
3170
3171 if (!flaglist) {
3172 /* For the sake of backward compatibility, drop all privileges
3173 * if none is specified.
3174 */
3175 for (i = 0; all_privs[i].token; i++)
3176 *flags |= all_privs[i].flag;
3177
3178 return 0;
3179 }
3180
62dd965e 3181 lxc_iterate_parts(token, flaglist, "|") {
eb0c9382 3182 aflag = -1;
3183
3184 for (i = 0; all_privs[i].token; i++)
3185 if (!strcmp(all_privs[i].token, token))
3186 aflag = all_privs[i].flag;
3187
3188 if (aflag < 0)
3189 return -1;
3190
3191 *flags |= aflag;
eb0c9382 3192 }
3193
3194 return 0;
3195}
3196
504a2217 3197/* Write out a configuration file. */
0e1a60b0 3198int write_config(int fd, const struct lxc_conf *conf)
72d0e1cb 3199{
6b0d5538 3200 int ret;
0e1a60b0 3201 size_t len = conf->unexpanded_len;
72d0e1cb 3202
0e1a60b0
CB
3203 if (len == 0)
3204 return 0;
504a2217 3205
0e1a60b0
CB
3206 ret = lxc_write_nointr(fd, conf->unexpanded_config, len);
3207 if (ret < 0) {
25a908b8 3208 SYSERROR("Failed to write configuration file");
0e1a60b0
CB
3209 return -1;
3210 }
3211
3212 return 0;
6b0d5538 3213}
f979ac15 3214
504a2217
CB
3215bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key,
3216 const char *v)
6b0d5538 3217{
0cacddda 3218 __do_free char *tmp = NULL;
6b0d5538 3219 int ret;
151d2da2 3220 size_t len;
4184c3e1 3221
151d2da2 3222 len = strlen(key) + strlen(v) + 4;
f01d0358 3223 tmp = must_realloc(NULL, len);
151d2da2 3224
663e9916 3225 if (lxc_config_value_empty(v))
151d2da2
CB
3226 ret = snprintf(tmp, len, "%s =", key);
3227 else
3228 ret = snprintf(tmp, len, "%s = %s", key, v);
6b0d5538
SH
3229 if (ret < 0 || ret >= len)
3230 return false;
3231
3232 /* Save the line verbatim into unexpanded_conf */
3233 if (append_unexp_config_line(tmp, conf))
3234 return false;
3235
3236 return true;
3237}
3238
504a2217
CB
3239void clear_unexp_config_line(struct lxc_conf *conf, const char *key,
3240 bool rm_subkeys)
6b0d5538 3241{
504a2217
CB
3242 char *lend;
3243 char *lstart = conf->unexpanded_config;
6b0d5538
SH
3244
3245 if (!conf->unexpanded_config)
3246 return;
504a2217 3247
6b0d5538
SH
3248 while (*lstart) {
3249 lend = strchr(lstart, '\n');
3250 char v;
47903908 3251
6b0d5538
SH
3252 if (!lend)
3253 lend = lstart + strlen(lstart);
3254 else
3255 lend++;
47903908 3256
6b0d5538
SH
3257 if (strncmp(lstart, key, strlen(key)) != 0) {
3258 lstart = lend;
3259 continue;
5f62730e 3260 }
47903908 3261
6b0d5538
SH
3262 if (!rm_subkeys) {
3263 v = lstart[strlen(key)];
3264 if (!isspace(v) && v != '=') {
3265 lstart = lend;
3266 continue;
3267 }
5f62730e 3268 }
47903908 3269
6b0d5538 3270 conf->unexpanded_len -= (lend - lstart);
47903908 3271
6b0d5538
SH
3272 if (*lend == '\0') {
3273 *lstart = '\0';
3274 return;
fd986e08 3275 }
47903908 3276
504a2217 3277 memmove(lstart, lend, strlen(lend) + 1);
fd986e08 3278 }
6b0d5538
SH
3279}
3280
329b3625
CB
3281bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
3282 const char *newpath, const char *oldname,
3283 const char *newname, const char *ovldir)
3284{
f01d0358
CB
3285 __do_free char *newdir = NULL,
3286 *olddir = NULL;
329b3625 3287 int ret;
f01d0358 3288 char *lend, *p, *q;
504a2217 3289 size_t newdirlen, olddirlen;
329b3625 3290 char *lstart = conf->unexpanded_config;
504a2217 3291 const char *key = "lxc.mount.entry";
329b3625 3292
504a2217 3293 olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
f01d0358 3294 olddir = must_realloc(NULL, olddirlen + 1);
504a2217
CB
3295 ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath,
3296 oldname);
25a908b8 3297 if (ret < 0 || ret >= olddirlen + 1)
329b3625 3298 return false;
504a2217
CB
3299
3300 newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
f01d0358 3301 newdir = must_realloc(NULL, newdirlen + 1);
504a2217
CB
3302 ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath,
3303 newname);
25a908b8 3304 if (ret < 0 || ret >= newdirlen + 1)
329b3625 3305 return false;
504a2217 3306
329b3625
CB
3307 if (!conf->unexpanded_config)
3308 return true;
504a2217 3309
329b3625
CB
3310 while (*lstart) {
3311 lend = strchr(lstart, '\n');
3312 if (!lend)
3313 lend = lstart + strlen(lstart);
3314 else
3315 lend++;
504a2217 3316
329b3625 3317 if (strncmp(lstart, key, strlen(key)) != 0)
504a2217
CB
3318 goto next;
3319
329b3625
CB
3320 p = strchr(lstart + strlen(key), '=');
3321 if (!p)
504a2217 3322 goto next;
329b3625 3323 p++;
504a2217 3324
329b3625
CB
3325 while (isblank(*p))
3326 p++;
504a2217 3327
329b3625 3328 if (p >= lend)
504a2217
CB
3329 goto next;
3330
12e6ab5d
CB
3331 /* Whenever a lxc.mount.entry entry is found in a line we check
3332 * if the substring "overlay" is present before doing any
3333 * further work. We check for "overlay" because substrings need
3334 * to have at least one space before them in a valid overlay
504a2217
CB
3335 * lxc.mount.entry (/A B overlay). When the space before is
3336 * missing it is very likely that these substrings are part of a
3337 * path or something else. (Checking q >= lend ensures that we
3338 * only count matches in the current line.) */
12e6ab5d
CB
3339 q = strstr(p, " overlay");
3340 if (!q || q >= lend)
504a2217
CB
3341 goto next;
3342
329b3625 3343 if (!(q = strstr(p, olddir)) || (q >= lend))
504a2217 3344 goto next;
329b3625
CB
3345
3346 /* replace the olddir with newdir */
3347 if (olddirlen >= newdirlen) {
3348 size_t diff = olddirlen - newdirlen;
3349 memcpy(q, newdir, newdirlen);
47903908 3350
329b3625
CB
3351 if (olddirlen != newdirlen) {
3352 memmove(q + newdirlen, q + newdirlen + diff,
47903908 3353 strlen(q) - newdirlen - diff + 1);
329b3625
CB
3354 lend -= diff;
3355 conf->unexpanded_len -= diff;
3356 }
3357 } else {
3358 char *new;
3359 size_t diff = newdirlen - olddirlen;
3360 size_t oldlen = conf->unexpanded_len;
3361 size_t newlen = oldlen + diff;
3362 size_t poffset = q - conf->unexpanded_config;
504a2217 3363
329b3625 3364 new = realloc(conf->unexpanded_config, newlen + 1);
25a908b8 3365 if (!new)
329b3625 3366 return false;
25a908b8 3367
329b3625
CB
3368 conf->unexpanded_len = newlen;
3369 conf->unexpanded_alloced = newlen + 1;
3370 new[newlen - 1] = '\0';
3371 lend = new + (lend - conf->unexpanded_config);
47903908 3372
25a908b8 3373 /* Move over the remainder to make room for the newdir.
504a2217 3374 */
329b3625 3375 memmove(new + poffset + newdirlen,
47903908 3376 new + poffset + olddirlen,
3377 oldlen - poffset - olddirlen + 1);
329b3625 3378 conf->unexpanded_config = new;
47903908 3379
329b3625
CB
3380 memcpy(new + poffset, newdir, newdirlen);
3381 lend += diff;
3382 }
47903908 3383
504a2217
CB
3384 next:
3385 lstart = lend;
329b3625 3386 }
504a2217 3387
329b3625
CB
3388 return true;
3389}
3390
67702c21 3391bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
d546aa0e
CB
3392 const char *newpath, const char *oldname,
3393 const char *newname)
6b0d5538 3394{
f01d0358
CB
3395 __do_free char *newdir = NULL,
3396 *olddir = NULL;
67702c21 3397 int ret;
f01d0358 3398 char *lend, *p;
504a2217
CB
3399 char *lstart = conf->unexpanded_config;
3400 size_t newdirlen, olddirlen;
3401 const char *key = "lxc.hook";
67702c21 3402
504a2217 3403 olddirlen = strlen(oldpath) + strlen(oldname) + 1;
f01d0358 3404 olddir = must_realloc(NULL, olddirlen + 1);
d546aa0e 3405 ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
25a908b8 3406 if (ret < 0 || ret >= olddirlen + 1)
67702c21 3407 return false;
504a2217
CB
3408
3409 newdirlen = strlen(newpath) + strlen(newname) + 1;
f01d0358 3410 newdir = must_realloc(NULL, newdirlen + 1);
d546aa0e 3411 ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
25a908b8 3412 if (ret < 0 || ret >= newdirlen + 1)
67702c21 3413 return false;
25a908b8 3414
67702c21
SH
3415 if (!conf->unexpanded_config)
3416 return true;
25a908b8 3417
67702c21
SH
3418 while (*lstart) {
3419 lend = strchr(lstart, '\n');
3420 if (!lend)
3421 lend = lstart + strlen(lstart);
3422 else
3423 lend++;
504a2217 3424
d546aa0e 3425 if (strncmp(lstart, key, strlen(key)) != 0)
504a2217
CB
3426 goto next;
3427
d546aa0e
CB
3428 p = strchr(lstart + strlen(key), '=');
3429 if (!p)
504a2217 3430 goto next;
67702c21 3431 p++;
504a2217 3432
67702c21
SH
3433 while (isblank(*p))
3434 p++;
504a2217
CB
3435
3436 if (p >= lend)
3437 goto next;
3438
d546aa0e 3439 if (strncmp(p, olddir, strlen(olddir)) != 0)
504a2217
CB
3440 goto next;
3441
67702c21
SH
3442 /* replace the olddir with newdir */
3443 if (olddirlen >= newdirlen) {
3444 size_t diff = olddirlen - newdirlen;
3445 memcpy(p, newdir, newdirlen);
47903908 3446
67702c21 3447 if (olddirlen != newdirlen) {
d546aa0e 3448 memmove(p + newdirlen, p + newdirlen + diff,
47903908 3449 strlen(p) - newdirlen - diff + 1);
67702c21
SH
3450 lend -= diff;
3451 conf->unexpanded_len -= diff;
3452 }
67702c21
SH
3453 } else {
3454 char *new;
3455 size_t diff = newdirlen - olddirlen;
3456 size_t oldlen = conf->unexpanded_len;
3457 size_t newlen = oldlen + diff;
3458 size_t poffset = p - conf->unexpanded_config;
504a2217 3459
d546aa0e 3460 new = realloc(conf->unexpanded_config, newlen + 1);
25a908b8 3461 if (!new)
6b0d5538 3462 return false;
25a908b8 3463
67702c21 3464 conf->unexpanded_len = newlen;
d546aa0e
CB
3465 conf->unexpanded_alloced = newlen + 1;
3466 new[newlen - 1] = '\0';
67702c21 3467 lend = new + (lend - conf->unexpanded_config);
47903908 3468
25a908b8 3469 /* Move over the remainder to make room for the newdir.
504a2217 3470 */
d546aa0e 3471 memmove(new + poffset + newdirlen,
47903908 3472 new + poffset + olddirlen,
3473 oldlen - poffset - olddirlen + 1);
67702c21 3474 conf->unexpanded_config = new;
47903908 3475
d546aa0e
CB
3476 memcpy(new + poffset, newdir, newdirlen);
3477 lend += diff;
fd986e08 3478 }
47903908 3479
504a2217
CB
3480 next:
3481 lstart = lend;
fd986e08 3482 }
504a2217 3483
6b0d5538
SH
3484 return true;
3485}
3486
504a2217
CB
3487#define DO(cmd) \
3488 { \
3489 if (!(cmd)) { \
3490 ERROR("Error writing to new config"); \
3491 return false; \
3492 } \
3493 }
6b0d5538 3494
25a908b8
CB
3495/* This is called only from clone. We wish to update all hwaddrs in the
3496 * unexpanded config file. We can't/don't want to update any which come from
504a2217
CB
3497 * lxc.includes (there shouldn't be any).
3498 * We can't just walk the c->lxc-conf->network list because that includes netifs
3499 * from the include files. So we update the ones which we find in the unexp
3500 * config file, then find the original macaddr in the conf->network, and update
3501 * that to the same value.
67702c21
SH
3502 */
3503bool network_new_hwaddrs(struct lxc_conf *conf)
6b0d5538 3504{
504a2217 3505 char *lend, *p, *p2;
6b0d5538 3506 struct lxc_list *it;
504a2217 3507 char *lstart = conf->unexpanded_config;
6b0d5538 3508
67702c21
SH
3509 if (!conf->unexpanded_config)
3510 return true;
091045f8 3511
67702c21
SH
3512 while (*lstart) {
3513 char newhwaddr[18], oldhwaddr[17];
091045f8 3514
67702c21
SH
3515 lend = strchr(lstart, '\n');
3516 if (!lend)
3517 lend = lstart + strlen(lstart);
3518 else
3519 lend++;
091045f8 3520
3db41a6c 3521 if (!lxc_config_net_is_hwaddr(lstart)) {
67702c21
SH
3522 lstart = lend;
3523 continue;
72d0e1cb 3524 }
091045f8 3525
4a787c27 3526 p = strchr(lstart, '=');
67702c21
SH
3527 if (!p) {
3528 lstart = lend;
3529 continue;
72d0e1cb 3530 }
091045f8 3531
67702c21
SH
3532 p++;
3533 while (isblank(*p))
3534 p++;
3535 if (!*p)
3536 return true;
091045f8 3537
67702c21
SH
3538 p2 = p;
3539 while (*p2 && !isblank(*p2) && *p2 != '\n')
3540 p2++;
504a2217
CB
3541
3542 if ((p2 - p) != 17) {
67702c21
SH
3543 WARN("Bad hwaddr entry");
3544 lstart = lend;
3545 continue;
72d0e1cb 3546 }
091045f8 3547
67702c21 3548 memcpy(oldhwaddr, p, 17);
091045f8
CB
3549
3550 if (!new_hwaddr(newhwaddr))
3551 return false;
3552
67702c21 3553 memcpy(p, newhwaddr, 17);
25a908b8 3554 lxc_list_for_each(it, &conf->network) {
67702c21 3555 struct lxc_netdev *n = it->elem;
25a908b8 3556
67702c21
SH
3557 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
3558 memcpy(n->hwaddr, newhwaddr, 17);
72d0e1cb 3559 }
67702c21
SH
3560
3561 lstart = lend;
72d0e1cb 3562 }
091045f8 3563
6b0d5538 3564 return true;
72d0e1cb 3565}
8796becf 3566
713046e3 3567static int set_config_ephemeral(const char *key, const char *value,
c7e27aaf 3568 struct lxc_conf *lxc_conf, void *data)
8796becf 3569{
663e9916 3570 if (lxc_config_value_empty(value)) {
3c6cf53a 3571 lxc_conf->ephemeral = 0;
78304622 3572 return 0;
3c6cf53a 3573 }
78304622 3574
66ffdb1a
CB
3575 if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0)
3576 return -1;
8796becf 3577
25a908b8 3578 if (lxc_conf->ephemeral > 1)
8796becf 3579 return -1;
8796becf
CB
3580
3581 return 0;
3582}
3583
46cc906d 3584static int set_config_log_syslog(const char *key, const char *value,
c7e27aaf 3585 struct lxc_conf *lxc_conf, void *data)
64c57ea1 3586{
76d0127f 3587 int facility;
7ca56b84 3588
ee10a69c
CB
3589 if (lxc_conf->syslog) {
3590 free(lxc_conf->syslog);
3591 lxc_conf->syslog = NULL;
3592 }
3593
663e9916 3594 if (lxc_config_value_empty(value))
7ca56b84
CB
3595 return 0;
3596
76d0127f 3597 facility = lxc_syslog_priority_to_int(value);
25a908b8 3598 if (facility == -EINVAL)
76d0127f 3599 return -1;
64c57ea1 3600
76d0127f 3601 lxc_log_syslog(facility);
47903908 3602
713046e3 3603 return set_config_string_item(&lxc_conf->syslog, value);
64c57ea1 3604}
5a46f283 3605
713046e3 3606static int set_config_no_new_privs(const char *key, const char *value,
c7e27aaf 3607 struct lxc_conf *lxc_conf, void *data)
5a46f283 3608{
e8ec7c9e 3609 unsigned int v;
5a46f283 3610
663e9916 3611 if (lxc_config_value_empty(value)) {
cf3f8bf6 3612 lxc_conf->no_new_privs = false;
80926845 3613 return 0;
cf3f8bf6 3614 }
80926845 3615
e8ec7c9e
CB
3616 if (lxc_safe_uint(value, &v) < 0)
3617 return -1;
3618
25a908b8 3619 if (v > 1)
5a46f283 3620 return -1;
e8ec7c9e 3621
5a46f283
CB
3622 lxc_conf->no_new_privs = v ? true : false;
3623
3624 return 0;
3625}
7b992a3e
CB
3626
3627/* Callbacks to get configuration items. */
6bede808 3628static int get_config_personality(const char *key, char *retv, int inlen,
cccd2219 3629 struct lxc_conf *c, void *data)
7b992a3e
CB
3630{
3631 int fulllen = 0;
3632
3633 if (!retv)
3634 inlen = 0;
3635 else
3636 memset(retv, 0, inlen);
3637
3638#if HAVE_SYS_PERSONALITY_H
3639 int len = 0;
3640
6bede808 3641 switch (c->personality) {
7b992a3e
CB
3642 case PER_LINUX32:
3643 strprint(retv, inlen, "i686");
3644 break;
3645 case PER_LINUX:
3646 strprint(retv, inlen, "x86_64");
3647 break;
3648 default:
3649 break;
3650 }
3651#endif
3652
3653 return fulllen;
3654}
bdf91ab4 3655
232763d6
CB
3656static int get_config_pty_max(const char *key, char *retv, int inlen,
3657 struct lxc_conf *c, void *data)
bdf91ab4 3658{
e528c735 3659 return lxc_get_conf_size_t(c, retv, inlen, c->pty_max);
bdf91ab4 3660}
5485782f 3661
fe1c5887
CB
3662static int get_config_tty_max(const char *key, char *retv, int inlen,
3663 struct lxc_conf *c, void *data)
5485782f 3664{
885766f5 3665 return lxc_get_conf_size_t(c, retv, inlen, c->ttys.max);
5485782f 3666}
8015e018 3667
42e53c29 3668static int get_config_tty_dir(const char *key, char *retv, int inlen,
cccd2219 3669 struct lxc_conf *c, void *data)
8015e018 3670{
885766f5 3671 return lxc_get_conf_str(retv, inlen, c->ttys.dir);
8015e018 3672}
de1ede69 3673
953fe44f
CB
3674static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
3675 struct lxc_conf *c, void *data)
104c8e6c 3676{
6bede808 3677 return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile);
104c8e6c 3678}
d60d18c6 3679
953fe44f
CB
3680static int get_config_apparmor_allow_incomplete(const char *key, char *retv,
3681 int inlen, struct lxc_conf *c,
3682 void *data)
d60d18c6 3683{
6bede808
CB
3684 return lxc_get_conf_int(c, retv, inlen,
3685 c->lsm_aa_allow_incomplete);
d60d18c6 3686}
4203a0b5 3687
1800f924
WB
3688static int get_config_apparmor_allow_nesting(const char *key, char *retv,
3689 int inlen, struct lxc_conf *c,
3690 void *data)
3691{
3692 return lxc_get_conf_int(c, retv, inlen,
3693 c->lsm_aa_allow_nesting);
3694}
3695
3696static int get_config_apparmor_raw(const char *key, char *retv,
3697 int inlen, struct lxc_conf *c,
3698 void *data)
3699{
3700 int len;
3701 struct lxc_list *it;
3702 int fulllen = 0;
3703
3704 if (!retv)
3705 inlen = 0;
3706 else
3707 memset(retv, 0, inlen);
3708
3709 lxc_list_for_each(it, &c->lsm_aa_raw) {
3710 strprint(retv, inlen, "%s\n", (char *)it->elem);
3711 }
3712
3713 return fulllen;
3714}
3715
953fe44f
CB
3716static int get_config_selinux_context(const char *key, char *retv, int inlen,
3717 struct lxc_conf *c, void *data)
4203a0b5 3718{
6bede808 3719 return lxc_get_conf_str(retv, inlen, c->lsm_se_context);
4203a0b5 3720}
b863bf92 3721
4fef78bc
MB
3722static int get_config_selinux_context_keyring(const char *key, char *retv, int inlen,
3723 struct lxc_conf *c, void *data)
3724{
3725 return lxc_get_conf_str(retv, inlen, c->lsm_se_keyring_context);
3726}
3727
8f818a84
MB
3728static int get_config_keyring_session(const char *key, char *retv, int inlen,
3729 struct lxc_conf *c, void *data)
3730{
3731 return lxc_get_conf_bool(c, retv, inlen, c->keyring_disable_session);
3732}
3733
4fef78bc 3734
25a908b8
CB
3735/* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3736 * just the value(s) will be printed. Since there still could be more than one,
3737 * it is newline-separated.
8d47350b 3738 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
25a908b8
CB
3739 * have newlines?)
3740 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3741 * 'lxc.cgroup.subsystem.key = value' format.
b863bf92 3742 */
54860ed0
CB
3743static int __get_config_cgroup_controller(const char *key, char *retv,
3744 int inlen, struct lxc_conf *c,
3745 int version)
b863bf92 3746{
b863bf92 3747 int len;
54860ed0
CB
3748 size_t namespaced_token_len;
3749 char *global_token, *namespaced_token;
3750 struct lxc_list *it;
b863bf92
CB
3751 int fulllen = 0;
3752 bool get_all = false;
3753
3754 if (!retv)
3755 inlen = 0;
3756 else
3757 memset(retv, 0, inlen);
3758
54860ed0
CB
3759 if (version == CGROUP2_SUPER_MAGIC) {
3760 global_token = "lxc.cgroup2";
3761 namespaced_token = "lxc.cgroup2.";
6333c915 3762 namespaced_token_len = STRLITERALLEN("lxc.cgroup2.");
54860ed0
CB
3763 } else if (version == CGROUP_SUPER_MAGIC) {
3764 global_token = "lxc.cgroup";
3765 namespaced_token = "lxc.cgroup.";
6333c915 3766 namespaced_token_len = STRLITERALLEN("lxc.cgroup.");
54860ed0
CB
3767 } else {
3768 return -1;
3769 }
3770
3771 if (strcmp(key, global_token) == 0)
b863bf92 3772 get_all = true;
54860ed0
CB
3773 else if (strncmp(key, namespaced_token, namespaced_token_len) == 0)
3774 key += namespaced_token_len;
b863bf92
CB
3775 else
3776 return -1;
3777
6bede808 3778 lxc_list_for_each(it, &c->cgroup) {
b863bf92 3779 struct lxc_cgroup *cg = it->elem;
25a908b8 3780
b863bf92 3781 if (get_all) {
54860ed0
CB
3782 if (version != cg->version)
3783 continue;
3784
47903908 3785 strprint(retv, inlen, "%s.%s = %s\n", global_token,
3786 cg->subsystem, cg->value);
3787 } else if (strcmp(cg->subsystem, key) == 0) {
b863bf92
CB
3788 strprint(retv, inlen, "%s\n", cg->value);
3789 }
3790 }
3791
3792 return fulllen;
3793}
5014ff2e 3794
54860ed0
CB
3795static int get_config_cgroup_controller(const char *key, char *retv, int inlen,
3796 struct lxc_conf *c, void *data)
3797{
3798 return __get_config_cgroup_controller(key, retv, inlen, c,
3799 CGROUP_SUPER_MAGIC);
3800}
3801
3802static int get_config_cgroup2_controller(const char *key, char *retv, int inlen,
3803 struct lxc_conf *c, void *data)
3804{
3805 return __get_config_cgroup_controller(key, retv, inlen, c,
3806 CGROUP2_SUPER_MAGIC);
3807}
3808
43654d34
CB
3809static int get_config_cgroup_dir(const char *key, char *retv, int inlen,
3810 struct lxc_conf *lxc_conf, void *data)
3811{
3812 int len;
3813 int fulllen = 0;
3814
3815 if (!retv)
3816 inlen = 0;
3817 else
3818 memset(retv, 0, inlen);
3819
3820 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir);
3821
3822 return fulllen;
3823}
3824
a900cbaf
WB
3825static int get_config_cgroup_monitor_dir(const char *key, char *retv, int inlen,
3826 struct lxc_conf *lxc_conf, void *data)
3827{
3828 int len;
3829 int fulllen = 0;
3830
3831 if (!retv)
3832 inlen = 0;
3833 else
3834 memset(retv, 0, inlen);
3835
3836 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.monitor_dir);
3837
3838 return fulllen;
3839}
3840
7696c1f9
RJ
3841static int get_config_cgroup_monitor_pivot_dir(const char *key, char *retv, int inlen,
3842 struct lxc_conf *lxc_conf, void *data)
3843{
3844 int len;
3845 int fulllen = 0;
3846
3847 if (!retv)
3848 inlen = 0;
3849 else
3850 memset(retv, 0, inlen);
3851
3852 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.monitor_pivot_dir);
3853
3854 return fulllen;
3855}
3856
a900cbaf
WB
3857static int get_config_cgroup_container_dir(const char *key, char *retv,
3858 int inlen,
3859 struct lxc_conf *lxc_conf,
3860 void *data)
3861{
3862 int len;
3863 int fulllen = 0;
3864
3865 if (!retv)
3866 inlen = 0;
3867 else
3868 memset(retv, 0, inlen);
3869
3870 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.container_dir);
3871
3872 return fulllen;
3873}
3874
3875static int get_config_cgroup_container_inner_dir(const char *key, char *retv,
3876 int inlen,
3877 struct lxc_conf *lxc_conf,
3878 void *data)
3879{
3880 int len;
3881 int fulllen = 0;
3882
3883 if (!retv)
3884 inlen = 0;
3885 else
3886 memset(retv, 0, inlen);
3887
3888 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.namespace_dir);
3889
3890 return fulllen;
3891}
3892
9caee129
CB
3893static inline int get_config_cgroup_relative(const char *key, char *retv,
3894 int inlen, struct lxc_conf *lxc_conf,
3895 void *data)
76f0e2e7
CB
3896{
3897 return lxc_get_conf_int(lxc_conf, retv, inlen,
9caee129 3898 lxc_conf->cgroup_meta.relative);
76f0e2e7
CB
3899}
3900
6bede808 3901static int get_config_idmaps(const char *key, char *retv, int inlen,
cccd2219 3902 struct lxc_conf *c, void *data)
5014ff2e
CB
3903{
3904 struct lxc_list *it;
3905 int len, listlen, ret;
3906 int fulllen = 0;
3907/* "u 1000 1000000 65536"
3908 *
3909 * let's render this as
3910 *
3911 * sizeof(char)
3912 * +
3913 * sizeof(" ")
3914 * +
c77aee64 3915 * sizeof(uint32_t)
5014ff2e
CB
3916 * +
3917 * sizeof(" ")
3918 * +
c77aee64 3919 * sizeof(uint32_t)
5014ff2e
CB
3920 * +
3921 * sizeof(" ")
3922 * +
c77aee64 3923 * sizeof(uint32_t)
5014ff2e
CB
3924 * +
3925 * \0
3926 */
c77aee64 3927#define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
5014ff2e
CB
3928 char buf[__LXC_IDMAP_STR_BUF];
3929
3930 if (!retv)
3931 inlen = 0;
3932 else
3933 memset(retv, 0, inlen);
3934
6bede808 3935 listlen = lxc_list_len(&c->id_map);
c77aee64 3936 lxc_list_for_each(it, &c->id_map) {
5014ff2e
CB
3937 struct id_map *map = it->elem;
3938 ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu",
3939 (map->idtype == ID_TYPE_UID) ? 'u' : 'g',
3940 map->nsid, map->hostid, map->range);
3941 if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF)
3942 return -1;
3943
3944 strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : "");
3945 }
47903908 3946
5014ff2e
CB
3947 return fulllen;
3948}
b29b29be 3949
46cc906d 3950static int get_config_log_level(const char *key, char *retv, int inlen,
cccd2219 3951 struct lxc_conf *c, void *data)
b29b29be
CB
3952{
3953 const char *v;
6bede808 3954 v = lxc_log_priority_to_string(c->loglevel);
b29b29be
CB
3955 return lxc_get_conf_str(retv, inlen, v);
3956}
3d4630ab 3957
46cc906d 3958static int get_config_log_file(const char *key, char *retv, int inlen,
cccd2219 3959 struct lxc_conf *c, void *data)
3d4630ab 3960{
6bede808 3961 return lxc_get_conf_str(retv, inlen, c->logfile);
3d4630ab 3962}
0d601acb 3963
47148e96
CB
3964static int get_config_mount_fstab(const char *key, char *retv, int inlen,
3965 struct lxc_conf *c, void *data)
0d601acb 3966{
6bede808 3967 return lxc_get_conf_str(retv, inlen, c->fstab);
0d601acb 3968}
43fbf8d9 3969
6bede808 3970static int get_config_mount_auto(const char *key, char *retv, int inlen,
cccd2219 3971 struct lxc_conf *c, void *data)
43fbf8d9
CB
3972{
3973 int len, fulllen = 0;
3974 const char *sep = "";
3975
3976 if (!retv)
3977 inlen = 0;
3978 else
3979 memset(retv, 0, inlen);
3980
6bede808 3981 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
43fbf8d9
CB
3982 return 0;
3983
6bede808 3984 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
43fbf8d9
CB
3985 case LXC_AUTO_PROC_MIXED:
3986 strprint(retv, inlen, "%sproc:mixed", sep);
3987 sep = " ";
3988 break;
3989 case LXC_AUTO_PROC_RW:
3990 strprint(retv, inlen, "%sproc:rw", sep);
3991 sep = " ";
3992 break;
3993 default:
3994 break;
3995 }
3996
6bede808 3997 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
43fbf8d9
CB
3998 case LXC_AUTO_SYS_RO:
3999 strprint(retv, inlen, "%ssys:ro", sep);
4000 sep = " ";
4001 break;
4002 case LXC_AUTO_SYS_RW:
4003 strprint(retv, inlen, "%ssys:rw", sep);
4004 sep = " ";
4005 break;
4006 case LXC_AUTO_SYS_MIXED:
4007 strprint(retv, inlen, "%ssys:mixed", sep);
4008 sep = " ";
4009 break;
4010 default:
4011 break;
4012 }
4013
6bede808 4014 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
43fbf8d9
CB
4015 case LXC_AUTO_CGROUP_NOSPEC:
4016 strprint(retv, inlen, "%scgroup", sep);
43fbf8d9
CB
4017 break;
4018 case LXC_AUTO_CGROUP_MIXED:
4019 strprint(retv, inlen, "%scgroup:mixed", sep);
43fbf8d9
CB
4020 break;
4021 case LXC_AUTO_CGROUP_RO:
4022 strprint(retv, inlen, "%scgroup:ro", sep);
43fbf8d9
CB
4023 break;
4024 case LXC_AUTO_CGROUP_RW:
4025 strprint(retv, inlen, "%scgroup:rw", sep);
43fbf8d9
CB
4026 break;
4027 case LXC_AUTO_CGROUP_FULL_NOSPEC:
4028 strprint(retv, inlen, "%scgroup-full", sep);
43fbf8d9
CB
4029 break;
4030 case LXC_AUTO_CGROUP_FULL_MIXED:
4031 strprint(retv, inlen, "%scgroup-full:mixed", sep);
43fbf8d9
CB
4032 break;
4033 case LXC_AUTO_CGROUP_FULL_RO:
4034 strprint(retv, inlen, "%scgroup-full:ro", sep);
43fbf8d9
CB
4035 break;
4036 case LXC_AUTO_CGROUP_FULL_RW:
4037 strprint(retv, inlen, "%scgroup-full:rw", sep);
43fbf8d9
CB
4038 break;
4039 default:
4040 break;
4041 }
4042
4043 return fulllen;
4044}
cc921848 4045
6bede808 4046static int get_config_mount(const char *key, char *retv, int inlen,
cccd2219 4047 struct lxc_conf *c, void *data)
cc921848
CB
4048{
4049 int len, fulllen = 0;
4050 struct lxc_list *it;
4051
4052 if (!retv)
4053 inlen = 0;
4054 else
4055 memset(retv, 0, inlen);
4056
25a908b8 4057 lxc_list_for_each(it, &c->mount_list) {
cc921848
CB
4058 strprint(retv, inlen, "%s\n", (char *)it->elem);
4059 }
4060
4061 return fulllen;
4062}
819114b6 4063
7a96a068
CB
4064static int get_config_rootfs_path(const char *key, char *retv, int inlen,
4065 struct lxc_conf *c, void *data)
819114b6 4066{
6bede808 4067 return lxc_get_conf_str(retv, inlen, c->rootfs.path);
819114b6 4068}
3af60359 4069
6e54330c
CB
4070static int get_config_rootfs_managed(const char *key, char *retv, int inlen,
4071 struct lxc_conf *c, void *data)
4072{
4073 return lxc_get_conf_bool(c, retv, inlen, c->rootfs.managed);
4074}
4075
6bede808 4076static int get_config_rootfs_mount(const char *key, char *retv, int inlen,
cccd2219 4077 struct lxc_conf *c, void *data)
3af60359 4078{
6bede808 4079 return lxc_get_conf_str(retv, inlen, c->rootfs.mount);
3af60359 4080}
0e9db631 4081
6bede808 4082static int get_config_rootfs_options(const char *key, char *retv, int inlen,
cccd2219 4083 struct lxc_conf *c, void *data)
0e9db631 4084{
6bede808 4085 return lxc_get_conf_str(retv, inlen, c->rootfs.options);
0e9db631 4086}
8f183f38 4087
b67771bc 4088static int get_config_uts_name(const char *key, char *retv, int inlen,
cccd2219 4089 struct lxc_conf *c, void *data)
e274f8b0
CB
4090{
4091 return lxc_get_conf_str(
4092 retv, inlen,
6bede808 4093 c->utsname ? c->utsname->nodename : NULL);
e274f8b0 4094}
466c2e93 4095
6bede808 4096static int get_config_hooks(const char *key, char *retv, int inlen,
cccd2219 4097 struct lxc_conf *c, void *data)
466c2e93
CB
4098{
4099 char *subkey;
4100 int len, fulllen = 0, found = -1;
4101 struct lxc_list *it;
4102 int i;
4103
466c2e93 4104 subkey = strchr(key, '.');
466c2e93
CB
4105 if (!subkey)
4106 return -1;
47903908 4107
4108 subkey = strchr(subkey + 1, '.');
637d38f2
CB
4109 if (!subkey)
4110 return -1;
466c2e93 4111 subkey++;
637d38f2 4112 if (*subkey == '\0')
466c2e93 4113 return -1;
47903908 4114
466c2e93
CB
4115 for (i = 0; i < NUM_LXC_HOOKS; i++) {
4116 if (strcmp(lxchook_names[i], subkey) == 0) {
4117 found = i;
4118 break;
4119 }
4120 }
47903908 4121
466c2e93
CB
4122 if (found == -1)
4123 return -1;
4124
4125 if (!retv)
4126 inlen = 0;
4127 else
4128 memset(retv, 0, inlen);
4129
6bede808 4130 lxc_list_for_each(it, &c->hooks[found]) {
466c2e93
CB
4131 strprint(retv, inlen, "%s\n", (char *)it->elem);
4132 }
47903908 4133
466c2e93
CB
4134 return fulllen;
4135}
d2ceff53 4136
44ae0fb6
CB
4137static int get_config_hooks_version(const char *key, char *retv, int inlen,
4138 struct lxc_conf *c, void *data)
4139{
4140 return lxc_get_conf_int(c, retv, inlen, c->hooks_version);
4141}
4142
f9373e40
CB
4143static int get_config_net(const char *key, char *retv, int inlen,
4144 struct lxc_conf *c, void *data)
d2ceff53
CB
4145{
4146 int len, fulllen = 0;
4147 struct lxc_list *it;
4148
4149 if (!retv)
4150 inlen = 0;
4151 else
4152 memset(retv, 0, inlen);
4153
6bede808 4154 lxc_list_for_each(it, &c->network) {
d2ceff53
CB
4155 struct lxc_netdev *n = it->elem;
4156 const char *t = lxc_net_type_to_str(n->type);
4157 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
4158 }
4159
4160 return fulllen;
4161}
bdccff60 4162
6bede808 4163static int get_config_cap_drop(const char *key, char *retv, int inlen,
cccd2219 4164 struct lxc_conf *c, void *data)
1c96d6d8
CB
4165{
4166 int len, fulllen = 0;
4167 struct lxc_list *it;
4168
4169 if (!retv)
4170 inlen = 0;
4171 else
4172 memset(retv, 0, inlen);
4173
6bede808 4174 lxc_list_for_each(it, &c->caps) {
b80927f2
CB
4175 strprint(retv, inlen, "%s\n", (char *)it->elem);
4176 }
25a908b8 4177
b80927f2
CB
4178 return fulllen;
4179}
4180
6bede808 4181static int get_config_cap_keep(const char *key, char *retv, int inlen,
cccd2219 4182 struct lxc_conf *c, void *data)
b80927f2
CB
4183{
4184 int len, fulllen = 0;
4185 struct lxc_list *it;
4186
4187 if (!retv)
4188 inlen = 0;
4189 else
4190 memset(retv, 0, inlen);
4191
6bede808 4192 lxc_list_for_each(it, &c->keepcaps) {
1c96d6d8
CB
4193 strprint(retv, inlen, "%s\n", (char *)it->elem);
4194 }
25a908b8 4195
1c96d6d8
CB
4196 return fulllen;
4197}
0692663a 4198
3aed4934
CB
4199static int get_config_console_path(const char *key, char *retv, int inlen,
4200 struct lxc_conf *c, void *data)
0692663a 4201{
6bede808 4202 return lxc_get_conf_str(retv, inlen, c->console.path);
0692663a 4203}
794d1c06 4204
6bede808 4205static int get_config_console_logfile(const char *key, char *retv, int inlen,
cccd2219 4206 struct lxc_conf *c, void *data)
794d1c06 4207{
6bede808 4208 return lxc_get_conf_str(retv, inlen, c->console.log_path);
794d1c06 4209}
75f55b1f 4210
d91adfa6
CB
4211static int get_config_console_rotate(const char *key, char *retv, int inlen,
4212 struct lxc_conf *c, void *data)
4213{
4214 return lxc_get_conf_int(c, retv, inlen, c->console.log_rotate);
4215}
4216
4217
28f3b1cd
CB
4218static int get_config_console_buffer_size(const char *key, char *retv,
4219 int inlen, struct lxc_conf *c,
4220 void *data)
a04220de 4221{
dcea90a0 4222 return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size);
a04220de
CB
4223}
4224
861813e5
CB
4225static int get_config_console_size(const char *key, char *retv, int inlen,
4226 struct lxc_conf *c, void *data)
4227{
4228 return lxc_get_conf_uint64(c, retv, inlen, c->console.log_size);
4229}
4230
50d86993
CB
4231static int get_config_seccomp_allow_nesting(const char *key, char *retv,
4232 int inlen, struct lxc_conf *c,
4233 void *data)
4234{
c3e3c21a
CB
4235#ifdef HAVE_SECCOMP
4236 return lxc_get_conf_int(c, retv, inlen, c->seccomp.allow_nesting);
4237#else
4238 errno = ENOSYS;
4239 return -1;
4240#endif
50d86993 4241}
861813e5 4242
84cf6d25
WB
4243static int get_config_seccomp_notify_cookie(const char *key, char *retv, int inlen,
4244 struct lxc_conf *c, void *data)
4245{
4246#ifdef HAVE_SECCOMP_NOTIFY
4247 return lxc_get_conf_str(retv, inlen, c->seccomp.notifier.cookie);
4248#else
21fce08c 4249 return ret_set_errno(-1, ENOSYS);
84cf6d25
WB
4250#endif
4251}
4252
86ce1da1
CB
4253static int get_config_seccomp_notify_proxy(const char *key, char *retv, int inlen,
4254 struct lxc_conf *c, void *data)
4255{
c3e3c21a 4256#ifdef HAVE_SECCOMP_NOTIFY
86ce1da1 4257 return lxc_get_conf_str(retv, inlen,
c3e3c21a
CB
4258 (c->seccomp.notifier.proxy_addr.sun_path[0]) == '/'
4259 ? &c->seccomp.notifier.proxy_addr.sun_path[0]
4260 : &c->seccomp.notifier.proxy_addr.sun_path[1]);
86ce1da1 4261#else
21fce08c 4262 return ret_set_errno(-1, ENOSYS);
86ce1da1
CB
4263#endif
4264}
4265
0b427da0
CB
4266static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
4267 struct lxc_conf *c, void *data)
75f55b1f 4268{
c3e3c21a 4269 return lxc_get_conf_str(retv, inlen, c->seccomp.seccomp);
75f55b1f 4270}
97f6dad0 4271
6bede808 4272static int get_config_autodev(const char *key, char *retv, int inlen,
cccd2219 4273 struct lxc_conf *c, void *data)
97f6dad0 4274{
6bede808 4275 return lxc_get_conf_int(c, retv, inlen, c->autodev);
97f6dad0 4276}
afee4324 4277
63012bdd
CK
4278static int get_config_autodev_tmpfs_size(const char *key, char *retv, int inlen,
4279 struct lxc_conf *c, void *data)
4280{
4281 return lxc_get_conf_int(c, retv, inlen, c->autodevtmpfssize);
4282}
4283
55c84efc 4284static int get_config_signal_halt(const char *key, char *retv, int inlen,
25a908b8 4285 struct lxc_conf *c, void *data)
afee4324 4286{
6bede808 4287 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
afee4324 4288}
3aa8f359 4289
55c84efc 4290static int get_config_signal_reboot(const char *key, char *retv, int inlen,
25a908b8 4291 struct lxc_conf *c, void *data)
3aa8f359 4292{
6bede808 4293 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
3aa8f359 4294}
2e16269f 4295
55c84efc 4296static int get_config_signal_stop(const char *key, char *retv, int inlen,
25a908b8 4297 struct lxc_conf *c, void *data)
2e16269f 4298{
6bede808 4299 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
2e16269f 4300}
54345299 4301
6bede808 4302static int get_config_start(const char *key, char *retv, int inlen,
cccd2219 4303 struct lxc_conf *c, void *data)
54345299 4304{
c6182222 4305 if (strcmp(key + 10, "auto") == 0)
6bede808 4306 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
c6182222 4307 else if (strcmp(key + 10, "delay") == 0)
6bede808 4308 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
c6182222 4309 else if (strcmp(key + 10, "order") == 0)
6bede808 4310 return lxc_get_conf_int(c, retv, inlen, c->start_order);
54345299
CB
4311
4312 return -1;
4313}
de9df15e 4314
46cc906d 4315static int get_config_log_syslog(const char *key, char *retv, int inlen,
25a908b8 4316 struct lxc_conf *c, void *data)
de9df15e 4317{
6bede808 4318 return lxc_get_conf_str(retv, inlen, c->syslog);
de9df15e 4319}
ac0f949c 4320
6bede808 4321static int get_config_monitor(const char *key, char *retv, int inlen,
cccd2219 4322 struct lxc_conf *c, void *data)
ac0f949c 4323{
6bede808 4324 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
ac0f949c 4325}
90ec7b6e 4326
258f8051
CB
4327static int get_config_monitor_signal_pdeath(const char *key, char *retv,
4328 int inlen, struct lxc_conf *c,
4329 void *data)
4330{
4331 return lxc_get_conf_int(c, retv, inlen, c->monitor_signal_pdeath);
4332}
4333
6bede808 4334static int get_config_group(const char *key, char *retv, int inlen,
cccd2219 4335 struct lxc_conf *c, void *data)
90ec7b6e
CB
4336{
4337 int len, fulllen = 0;
4338 struct lxc_list *it;
4339
4340 if (!retv)
4341 inlen = 0;
4342 else
4343 memset(retv, 0, inlen);
4344
6bede808 4345 lxc_list_for_each(it, &c->groups) {
90ec7b6e
CB
4346 strprint(retv, inlen, "%s\n", (char *)it->elem);
4347 }
25a908b8 4348
90ec7b6e
CB
4349 return fulllen;
4350}
aa0db7d3 4351
6bede808 4352static int get_config_environment(const char *key, char *retv, int inlen,
cccd2219 4353 struct lxc_conf *c, void *data)
aa0db7d3
CB
4354{
4355 int len, fulllen = 0;
4356 struct lxc_list *it;
4357
4358 if (!retv)
4359 inlen = 0;
4360 else
4361 memset(retv, 0, inlen);
4362
6bede808 4363 lxc_list_for_each(it, &c->environment) {
aa0db7d3
CB
4364 strprint(retv, inlen, "%s\n", (char *)it->elem);
4365 }
25a908b8 4366
aa0db7d3
CB
4367 return fulllen;
4368}
96dfcb7d 4369
5cda27c1
SH
4370static int get_config_execute_cmd(const char *key, char *retv, int inlen,
4371 struct lxc_conf *c, void *data)
4372{
4373 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
4374}
4375
6bede808 4376static int get_config_init_cmd(const char *key, char *retv, int inlen,
cccd2219 4377 struct lxc_conf *c, void *data)
96dfcb7d 4378{
6bede808 4379 return lxc_get_conf_str(retv, inlen, c->init_cmd);
96dfcb7d 4380}
1398e9b1 4381
3c491553
L
4382static int get_config_init_cwd(const char *key, char *retv, int inlen,
4383 struct lxc_conf *c, void *data)
4384{
4385 return lxc_get_conf_str(retv, inlen, c->init_cwd);
4386}
4387
6bede808 4388static int get_config_init_uid(const char *key, char *retv, int inlen,
cccd2219 4389 struct lxc_conf *c, void *data)
1398e9b1 4390{
6bede808 4391 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
1398e9b1 4392}
dfeb7e42 4393
6bede808 4394static int get_config_init_gid(const char *key, char *retv, int inlen,
cccd2219 4395 struct lxc_conf *c, void *data)
dfeb7e42 4396{
6bede808 4397 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
dfeb7e42 4398}
62048afe 4399
6bede808 4400static int get_config_ephemeral(const char *key, char *retv, int inlen,
cccd2219 4401 struct lxc_conf *c, void *data)
62048afe 4402{
6bede808 4403 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
62048afe 4404}
b09521ac 4405
6bede808 4406static int get_config_no_new_privs(const char *key, char *retv, int inlen,
cccd2219 4407 struct lxc_conf *c, void *data)
b09521ac 4408{
6bede808 4409 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
b09521ac 4410}
389f6466 4411
25a908b8 4412/* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
240d4b74 4413 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4414 * printed, in 'lxc.prlimit.resource = value' format.
389f6466 4415 */
240d4b74 4416static int get_config_prlimit(const char *key, char *retv, int inlen,
47903908 4417 struct lxc_conf *c, void *data)
389f6466
CB
4418{
4419 int fulllen = 0, len;
4420 bool get_all = false;
4421 struct lxc_list *it;
4422
4423 if (!retv)
4424 inlen = 0;
4425 else
4426 memset(retv, 0, inlen);
4427
240d4b74 4428 if (!strcmp(key, "lxc.prlimit"))
389f6466 4429 get_all = true;
240d4b74 4430 else if (strncmp(key, "lxc.prlimit.", 12) == 0)
4431 key += 12;
389f6466
CB
4432 else
4433 return -1;
4434
6bede808 4435 lxc_list_for_each(it, &c->limits) {
c77aee64
CB
4436 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4437 char buf[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
389f6466
CB
4438 int partlen;
4439 struct lxc_limit *lim = it->elem;
4440
4441 if (lim->limit.rlim_cur == RLIM_INFINITY) {
6333c915
CB
4442 memcpy(buf, "unlimited", STRLITERALLEN("unlimited") + 1);
4443 partlen = STRLITERALLEN("unlimited");
389f6466
CB
4444 } else {
4445 partlen = sprintf(buf, "%" PRIu64,
4446 (uint64_t)lim->limit.rlim_cur);
4447 }
47903908 4448
389f6466 4449 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
25a908b8 4450 if (lim->limit.rlim_max == RLIM_INFINITY)
389f6466 4451 memcpy(buf + partlen, ":unlimited",
6333c915 4452 STRLITERALLEN(":unlimited") + 1);
25a908b8 4453 else
389f6466
CB
4454 sprintf(buf + partlen, ":%" PRIu64,
4455 (uint64_t)lim->limit.rlim_max);
389f6466
CB
4456 }
4457
4458 if (get_all) {
240d4b74 4459 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
389f6466 4460 lim->resource, buf);
47903908 4461 } else if (strcmp(lim->resource, key) == 0) {
389f6466
CB
4462 strprint(retv, inlen, "%s", buf);
4463 }
4464 }
4465
4466 return fulllen;
4467}
e08cb901 4468
e409b214
CB
4469/* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4470 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4471 * entries will be printed, in 'lxc.sysctl.key = value' format.
7edd0540
L
4472 */
4473static int get_config_sysctl(const char *key, char *retv, int inlen,
47903908 4474 struct lxc_conf *c, void *data)
7edd0540
L
4475{
4476 int len;
4477 struct lxc_list *it;
4478 int fulllen = 0;
4479 bool get_all = false;
4480
4481 if (!retv)
4482 inlen = 0;
4483 else
4484 memset(retv, 0, inlen);
4485
4486 if (strcmp(key, "lxc.sysctl") == 0)
4487 get_all = true;
6333c915
CB
4488 else if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
4489 key += STRLITERALLEN("lxc.sysctl.");
7edd0540
L
4490 else
4491 return -1;
4492
4493 lxc_list_for_each(it, &c->sysctls) {
4494 struct lxc_sysctl *elem = it->elem;
4495 if (get_all) {
47903908 4496 strprint(retv, inlen, "lxc.sysctl.%s = %s\n", elem->key,
4497 elem->value);
7edd0540
L
4498 } else if (strcmp(elem->key, key) == 0) {
4499 strprint(retv, inlen, "%s", elem->value);
4500 }
4501 }
4502
4503 return fulllen;
4504}
4505
61d7a733 4506static int get_config_proc(const char *key, char *retv, int inlen,
47903908 4507 struct lxc_conf *c, void *data)
61d7a733
YT
4508{
4509 struct lxc_list *it;
4510 int len;
4511 int fulllen = 0;
4512 bool get_all = false;
4513
4514 if (!retv)
4515 inlen = 0;
4516 else
4517 memset(retv, 0, inlen);
4518
4519 if (strcmp(key, "lxc.proc") == 0)
4520 get_all = true;
6333c915
CB
4521 else if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
4522 key += STRLITERALLEN("lxc.proc.");
61d7a733
YT
4523 else
4524 return -1;
4525
4526 lxc_list_for_each(it, &c->procs) {
4527 struct lxc_proc *proc = it->elem;
4528
4529 if (get_all) {
4530 strprint(retv, inlen, "lxc.proc.%s = %s\n",
47903908 4531 proc->filename, proc->value);
61d7a733
YT
4532 } else if (strcmp(proc->filename, key) == 0) {
4533 strprint(retv, inlen, "%s", proc->value);
4534 }
4535 }
4536
4537 return fulllen;
4538}
4539
1d8d3676
CB
4540static int get_config_namespace_clone(const char *key, char *retv, int inlen,
4541 struct lxc_conf *c, void *data)
4542{
4543 int i, len;
4544 int fulllen = 0;
4545
4546 if (!retv)
4547 inlen = 0;
4548 else
4549 memset(retv, 0, inlen);
4550
4551 for (i = 0; i < LXC_NS_MAX; i++) {
4552 if (c->ns_clone & ns_info[i].clone_flag)
4553 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4554 }
4555
4556 return fulllen;
4557}
4558
abeb5bba
CB
4559static int get_config_namespace_keep(const char *key, char *retv, int inlen,
4560 struct lxc_conf *c, void *data)
4561{
4562 int i, len;
4563 int fulllen = 0;
4564
4565 if (!retv)
4566 inlen = 0;
4567 else
4568 memset(retv, 0, inlen);
4569
4570 for (i = 0; i < LXC_NS_MAX; i++) {
4571 if (c->ns_keep & ns_info[i].clone_flag)
4572 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4573 }
4574
4575 return fulllen;
4576}
4577
70fd7fc9
CB
4578static int get_config_time_offset_boot(const char *key, char *retv, int inlen, struct lxc_conf *c,
4579 void *data)
4580{
4581 int len;
4582 int fulllen = 0;
4583
4584 if (!retv)
4585 inlen = 0;
4586 else
4587 memset(retv, 0, inlen);
4588
4589 if (c->timens.s_boot) {
4590 strprint(retv, inlen, "%" PRId64 " s\n", c->timens.s_boot);
4591 } else {
4592 strprint(retv, inlen, "%" PRId64 " ns\n", c->timens.ns_boot);
4593 }
4594
4595 return fulllen;
4596}
4597
4598static int get_config_time_offset_monotonic(const char *key, char *retv, int inlen,
4599 struct lxc_conf *c, void *data)
4600{
4601 int len;
4602 int fulllen = 0;
4603
4604 if (!retv)
4605 inlen = 0;
4606 else
4607 memset(retv, 0, inlen);
4608
4609 if (c->timens.s_monotonic) {
4610 strprint(retv, inlen, "%" PRId64 "s\n", c->timens.s_monotonic);
4611 } else {
4612 strprint(retv, inlen, "%" PRId64 "ns\n", c->timens.ns_monotonic);
4613 }
4614
4615 return fulllen;
4616}
4617
b074bbf1
CB
4618static int get_config_namespace_share(const char *key, char *retv, int inlen,
4619 struct lxc_conf *c, void *data)
28d9e29e
CB
4620{
4621 int len, ns_idx;
4622 const char *namespace;
4623 int fulllen = 0;
4624
4625 if (!retv)
4626 inlen = 0;
4627 else
4628 memset(retv, 0, inlen);
4629
6333c915 4630 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
4631 ns_idx = lxc_namespace_2_ns_idx(namespace);
4632 if (ns_idx < 0)
4633 return ns_idx;
4634
b074bbf1 4635 strprint(retv, inlen, "%s", c->ns_share[ns_idx]);
28d9e29e
CB
4636
4637 return fulllen;
4638}
4639
e08cb901 4640/* Callbacks to clear config items. */
26471403
CB
4641static inline int clr_config_personality(const char *key, struct lxc_conf *c,
4642 void *data)
e08cb901
CB
4643{
4644 c->personality = -1;
4645 return 0;
4646}
03818ae3 4647
232763d6
CB
4648static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
4649 void *data)
03818ae3 4650{
e528c735 4651 c->pty_max = 0;
03818ae3
CB
4652 return 0;
4653}
e7a4b096 4654
fe1c5887
CB
4655static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
4656 void *data)
e7a4b096 4657{
885766f5 4658 c->ttys.tty = 0;
e7a4b096
CB
4659 return 0;
4660}
eaf8c0c7 4661
42e53c29 4662static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
26471403 4663 void *data)
eaf8c0c7 4664{
885766f5
CB
4665 free(c->ttys.dir);
4666 c->ttys.dir = NULL;
eaf8c0c7
CB
4667 return 0;
4668}
6bd86308 4669
953fe44f
CB
4670static inline int clr_config_apparmor_profile(const char *key,
4671 struct lxc_conf *c, void *data)
025718fb
CB
4672{
4673 free(c->lsm_aa_profile);
4674 c->lsm_aa_profile = NULL;
4675 return 0;
4676}
3061e04e 4677
953fe44f
CB
4678static inline int clr_config_apparmor_allow_incomplete(const char *key,
4679 struct lxc_conf *c,
4680 void *data)
3061e04e
CB
4681{
4682 c->lsm_aa_allow_incomplete = 0;
4683 return 0;
4684}
31fc3494 4685
1800f924
WB
4686static inline int clr_config_apparmor_allow_nesting(const char *key,
4687 struct lxc_conf *c,
4688 void *data)
4689{
4690 c->lsm_aa_allow_nesting = 0;
4691 return 0;
4692}
4693
4694static inline int clr_config_apparmor_raw(const char *key,
4695 struct lxc_conf *c,
4696 void *data)
4697{
4698 return lxc_clear_apparmor_raw(c);
4699}
4700
953fe44f
CB
4701static inline int clr_config_selinux_context(const char *key,
4702 struct lxc_conf *c, void *data)
31fc3494
CB
4703{
4704 free(c->lsm_se_context);
4705 c->lsm_se_context = NULL;
4706 return 0;
4707}
754d01ce 4708
4fef78bc
MB
4709static inline int clr_config_selinux_context_keyring(const char *key,
4710 struct lxc_conf *c, void *data)
4711{
4712 free(c->lsm_se_keyring_context);
4713 c->lsm_se_keyring_context = NULL;
4714 return 0;
4715}
4716
8f818a84
MB
4717static inline int clr_config_keyring_session(const char *key,
4718 struct lxc_conf *c, void *data)
4719{
4720 c->keyring_disable_session = false;
4721 return 0;
4722}
4723
43654d34
CB
4724static inline int clr_config_cgroup_controller(const char *key,
4725 struct lxc_conf *c, void *data)
754d01ce 4726{
54860ed0
CB
4727 return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC);
4728}
4729
4730static inline int clr_config_cgroup2_controller(const char *key,
4731 struct lxc_conf *c, void *data)
4732{
4733 return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC);
754d01ce 4734}
d3a178de 4735
43654d34
CB
4736static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
4737 void *data)
4738{
4739 if (lxc_conf->cgroup_meta.dir) {
4740 free(lxc_conf->cgroup_meta.dir);
4741 lxc_conf->cgroup_meta.dir = NULL;
4742 }
4743
4744 return 0;
4745}
4746
a900cbaf
WB
4747static int clr_config_cgroup_monitor_dir(const char *key,
4748 struct lxc_conf *lxc_conf,
4749 void *data)
4750{
4751 free_disarm(lxc_conf->cgroup_meta.monitor_dir);
4752 return 0;
4753}
4754
7696c1f9
RJ
4755static int clr_config_cgroup_monitor_pivot_dir(const char *key,
4756 struct lxc_conf *lxc_conf,
4757 void *data)
4758{
4759 free_disarm(lxc_conf->cgroup_meta.monitor_pivot_dir);
4760 return 0;
4761}
4762
a900cbaf
WB
4763static int clr_config_cgroup_container_dir(const char *key,
4764 struct lxc_conf *lxc_conf,
4765 void *data)
4766{
4767 free_disarm(lxc_conf->cgroup_meta.container_dir);
4768 return 0;
4769}
4770
4771static int clr_config_cgroup_container_inner_dir(const char *key,
4772 struct lxc_conf *lxc_conf,
4773 void *data)
4774{
4775 free_disarm(lxc_conf->cgroup_meta.namespace_dir);
4776 return 0;
4777}
4778
9caee129
CB
4779static inline int clr_config_cgroup_relative(const char *key,
4780 struct lxc_conf *lxc_conf,
4781 void *data)
76f0e2e7 4782{
9caee129 4783 lxc_conf->cgroup_meta.relative = false;
76f0e2e7
CB
4784 return 0;
4785}
4786
26471403
CB
4787static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
4788 void *data)
d3a178de
CB
4789{
4790 return lxc_clear_idmaps(c);
4791}
97d3338d 4792
46cc906d 4793static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
26471403 4794 void *data)
97d3338d 4795{
4b73005c 4796 c->loglevel = LXC_LOG_LEVEL_NOTSET;
97d3338d
CB
4797 return 0;
4798}
de46099c 4799
46cc906d 4800static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
26471403 4801 void *data)
de46099c
CB
4802{
4803 free(c->logfile);
4804 c->logfile = NULL;
4805 return 0;
4806}
b4fa13cd 4807
26471403
CB
4808static inline int clr_config_mount(const char *key, struct lxc_conf *c,
4809 void *data)
b4fa13cd
CB
4810{
4811 return lxc_clear_mount_entries(c);
4812}
4be81021 4813
26471403
CB
4814static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
4815 void *data)
4be81021
CB
4816{
4817 return lxc_clear_automounts(c);
4818}
350d4b15 4819
47148e96
CB
4820static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
4821 void *data)
350d4b15
CB
4822{
4823 free(c->fstab);
4824 c->fstab = NULL;
4825 return 0;
4826}
faca124d 4827
7a96a068
CB
4828static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
4829 void *data)
faca124d
CB
4830{
4831 free(c->rootfs.path);
4832 c->rootfs.path = NULL;
4833 return 0;
4834}
4835
6e54330c
CB
4836static inline int clr_config_rootfs_managed(const char *key, struct lxc_conf *c,
4837 void *data)
4838{
4839 c->rootfs.managed = true;
4840 return 0;
4841}
4842
26471403
CB
4843static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
4844 void *data)
fddefc2d
CB
4845{
4846 free(c->rootfs.mount);
4847 c->rootfs.mount = NULL;
4848 return 0;
4849}
7b1eb67d 4850
26471403
CB
4851static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
4852 void *data)
7b1eb67d
CB
4853{
4854 free(c->rootfs.options);
4855 c->rootfs.options = NULL;
3437f95c
CB
4856
4857 free(c->rootfs.data);
4858 c->rootfs.data = NULL;
4859
7b1eb67d
CB
4860 return 0;
4861}
02becb8d 4862
b67771bc 4863static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
26471403 4864 void *data)
d31d0103
CB
4865{
4866 free(c->utsname);
4867 c->utsname = NULL;
4868 return 0;
4869}
c9eeb90c 4870
26471403
CB
4871static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
4872 void *data)
c9eeb90c
CB
4873{
4874 return lxc_clear_hooks(c, key);
4875}
4876
44ae0fb6
CB
4877static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c,
4878 void *data)
4879{
4880 /* default to legacy hooks version */
4881 c->hooks_version = 0;
4882 return 0;
4883}
4884
f9373e40
CB
4885static inline int clr_config_net(const char *key, struct lxc_conf *c,
4886 void *data)
f4488271 4887{
c302b476
CB
4888 lxc_free_networks(&c->network);
4889
e5d2fd7c 4890 return 0;
f4488271
CB
4891}
4892
26471403
CB
4893static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
4894 void *data)
244cb55b
CB
4895{
4896 return lxc_clear_config_caps(c);
4897}
c74cc490 4898
26471403
CB
4899static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
4900 void *data)
c74cc490
CB
4901{
4902 return lxc_clear_config_keepcaps(c);
4903}
4e5b633f 4904
3aed4934
CB
4905static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
4906 void *data)
4e5b633f
CB
4907{
4908 free(c->console.path);
4909 c->console.path = NULL;
4910 return 0;
4911}
4912
7c2ec23a 4913static inline int clr_config_console_logfile(const char *key,
26471403 4914 struct lxc_conf *c, void *data)
7c2ec23a
CB
4915{
4916 free(c->console.log_path);
4917 c->console.log_path = NULL;
4918 return 0;
4919}
bbca37d8 4920
d91adfa6
CB
4921static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c,
4922 void *data)
4923{
4924 c->console.log_rotate = 0;
4925 return 0;
4926}
4927
28f3b1cd
CB
4928static inline int clr_config_console_buffer_size(const char *key,
4929 struct lxc_conf *c, void *data)
a04220de 4930{
28f3b1cd 4931 c->console.buffer_size = 0;
a04220de
CB
4932 return 0;
4933}
4934
861813e5
CB
4935static inline int clr_config_console_size(const char *key, struct lxc_conf *c,
4936 void *data)
4937{
4938 c->console.log_size = 0;
4939 return 0;
4940}
4941
50d86993
CB
4942static inline int clr_config_seccomp_allow_nesting(const char *key,
4943 struct lxc_conf *c, void *data)
4944{
c3e3c21a
CB
4945#ifdef HAVE_SECCOMP
4946 c->seccomp.allow_nesting = 0;
50d86993 4947 return 0;
c3e3c21a
CB
4948#else
4949 errno = ENOSYS;
4950 return -1;
4951#endif
50d86993
CB
4952}
4953
84cf6d25
WB
4954static inline int clr_config_seccomp_notify_cookie(const char *key,
4955 struct lxc_conf *c, void *data)
4956{
4957#ifdef HAVE_SECCOMP_NOTIFY
4958 free(c->seccomp.notifier.cookie);
4959 c->seccomp.notifier.cookie = NULL;
4960 return 0;
4961#else
21fce08c 4962 return ret_set_errno(-1, ENOSYS);
84cf6d25
WB
4963#endif
4964}
4965
86ce1da1
CB
4966static inline int clr_config_seccomp_notify_proxy(const char *key,
4967 struct lxc_conf *c, void *data)
4968{
c3e3c21a
CB
4969#ifdef HAVE_SECCOMP_NOTIFY
4970 memset(&c->seccomp.notifier.proxy_addr, 0,
4971 sizeof(c->seccomp.notifier.proxy_addr));
86ce1da1
CB
4972 return 0;
4973#else
21fce08c 4974 return ret_set_errno(-1, ENOSYS);
86ce1da1
CB
4975#endif
4976}
4977
0b427da0
CB
4978static inline int clr_config_seccomp_profile(const char *key,
4979 struct lxc_conf *c, void *data)
bbca37d8 4980{
c3e3c21a
CB
4981 free(c->seccomp.seccomp);
4982 c->seccomp.seccomp = NULL;
bbca37d8
CB
4983 return 0;
4984}
c721e86c 4985
26471403
CB
4986static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
4987 void *data)
c721e86c
CB
4988{
4989 c->autodev = 1;
4990 return 0;
4991}
87b288d1 4992
63012bdd
CK
4993static inline int clr_config_autodev_tmpfs_size(const char *key, struct lxc_conf *c,
4994 void *data)
4995{
4996 c->autodevtmpfssize = 500000;
4997 return 0;
4998}
4999
55c84efc 5000static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
26471403 5001 void *data)
87b288d1
CB
5002{
5003 c->haltsignal = 0;
5004 return 0;
5005}
cae63cfa 5006
55c84efc 5007static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
26471403 5008 void *data)
cae63cfa
CB
5009{
5010 c->rebootsignal = 0;
5011 return 0;
5012}
de45f3a8 5013
55c84efc 5014static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
26471403 5015 void *data)
de45f3a8
CB
5016{
5017 c->stopsignal = 0;
5018 return 0;
5019}
c6182222 5020
26471403
CB
5021static inline int clr_config_start(const char *key, struct lxc_conf *c,
5022 void *data)
c6182222
CB
5023{
5024 if (strcmp(key + 10, "auto") == 0)
5025 c->start_auto = 0;
5026 else if (strcmp(key + 10, "delay") == 0)
5027 c->start_delay = 0;
5028 else if (strcmp(key + 10, "order") == 0)
5029 c->start_order = 0;
5030
5031 return 0;
5032}
998ca94f 5033
46cc906d 5034static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
26471403 5035 void *data)
998ca94f
CB
5036{
5037 free(c->syslog);
5038 c->syslog = NULL;
5039 return 0;
5040}
adad12ca 5041
26471403
CB
5042static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
5043 void *data)
adad12ca
CB
5044{
5045 c->monitor_unshare = 0;
5046 return 0;
5047}
4850d223 5048
258f8051
CB
5049static inline int clr_config_monitor_signal_pdeath(const char *key,
5050 struct lxc_conf *c, void *data)
5051{
5052 c->monitor_signal_pdeath = 0;
5053 return 0;
5054}
5055
26471403
CB
5056static inline int clr_config_group(const char *key, struct lxc_conf *c,
5057 void *data)
4850d223
CB
5058{
5059 return lxc_clear_groups(c);
5060}
832fb63a 5061
26471403
CB
5062static inline int clr_config_environment(const char *key, struct lxc_conf *c,
5063 void *data)
832fb63a
CB
5064{
5065 return lxc_clear_environment(c);
4850d223 5066}
8e90af3e 5067
5cda27c1
SH
5068static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
5069 void *data)
5070{
5071 free(c->execute_cmd);
5072 c->execute_cmd = NULL;
5073 return 0;
5074}
5075
26471403
CB
5076static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
5077 void *data)
8e90af3e
CB
5078{
5079 free(c->init_cmd);
5080 c->init_cmd = NULL;
5081 return 0;
5082}
ec76dcfb 5083
3c491553
L
5084static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
5085 void *data)
5086{
5087 free(c->init_cwd);
5088 c->init_cwd = NULL;
5089 return 0;
5090}
5091
26471403
CB
5092static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
5093 void *data)
ec76dcfb
CB
5094{
5095 c->init_uid = 0;
5096 return 0;
5097}
1044b247 5098
26471403
CB
5099static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
5100 void *data)
1044b247
CB
5101{
5102 c->init_gid = 0;
5103 return 0;
5104}
59e370db 5105
26471403
CB
5106static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
5107 void *data)
59e370db
CB
5108{
5109 c->ephemeral = 0;
5110 return 0;
5111}
b98c5ab0 5112
26471403
CB
5113static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
5114 void *data)
b98c5ab0
CB
5115{
5116 c->no_new_privs = false;
5117 return 0;
5118}
715ccc96 5119
240d4b74 5120static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
26471403 5121 void *data)
715ccc96
CB
5122{
5123 return lxc_clear_limits(c, key);
5124}
fdf3c589 5125
7edd0540
L
5126static inline int clr_config_sysctl(const char *key, struct lxc_conf *c,
5127 void *data)
5128{
5129 return lxc_clear_sysctls(c, key);
5130}
5131
61d7a733
YT
5132static inline int clr_config_proc(const char *key, struct lxc_conf *c,
5133 void *data)
5134{
5135 return lxc_clear_procs(c, key);
5136}
5137
26471403
CB
5138static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
5139 void *data)
fdf3c589
CB
5140{
5141 lxc_clear_includes(c);
5142 return 0;
5143}
a3c8e600 5144
1d8d3676
CB
5145static int clr_config_namespace_clone(const char *key,
5146 struct lxc_conf *lxc_conf, void *data)
5147{
5148 lxc_conf->ns_clone = 0;
5149 return 0;
5150}
5151
abeb5bba
CB
5152static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf,
5153 void *data)
5154{
5155 lxc_conf->ns_keep = 0;
5156 return 0;
5157}
5158
70fd7fc9
CB
5159static int clr_config_time_offset_boot(const char *key, struct lxc_conf *lxc_conf, void *data)
5160{
5161 lxc_conf->timens.s_boot = 0;
5162 lxc_conf->timens.ns_boot = 0;
5163 return 0;
5164}
5165
5166static int clr_config_time_offset_monotonic(const char *key, struct lxc_conf *lxc_conf, void *data)
5167{
5168 lxc_conf->timens.s_monotonic = 0;
5169 lxc_conf->timens.ns_monotonic = 0;
5170 return 0;
5171}
5172
b074bbf1
CB
5173static int clr_config_namespace_share(const char *key,
5174 struct lxc_conf *lxc_conf, void *data)
28d9e29e
CB
5175{
5176 int ns_idx;
5177 const char *namespace;
5178
6333c915 5179 namespace = key + STRLITERALLEN("lxc.namespace.share.");
28d9e29e
CB
5180 ns_idx = lxc_namespace_2_ns_idx(namespace);
5181 if (ns_idx < 0)
5182 return ns_idx;
5183
b074bbf1
CB
5184 free(lxc_conf->ns_share[ns_idx]);
5185 lxc_conf->ns_share[ns_idx] = NULL;
28d9e29e
CB
5186
5187 return 0;
5188}
5189
a3c8e600 5190static int get_config_includefiles(const char *key, char *retv, int inlen,
cccd2219 5191 struct lxc_conf *c, void *data)
a3c8e600
CB
5192{
5193 return -ENOSYS;
5194}
40db5d2f 5195
31ee747b
CB
5196static struct lxc_config_t *get_network_config_ops(const char *key,
5197 struct lxc_conf *lxc_conf,
5198 ssize_t *idx,
5199 char **deindexed_key)
40db5d2f 5200{
31ee747b
CB
5201 int ret;
5202 unsigned int tmpidx;
5203 size_t numstrlen;
40db5d2f
CB
5204 char *copy, *idx_start, *idx_end;
5205 struct lxc_config_t *config = NULL;
5206
5207 /* check that this is a sensible network key */
31ee747b
CB
5208 if (strncmp("lxc.net.", key, 8)) {
5209 ERROR("Invalid network configuration key \"%s\"", key);
40db5d2f 5210 return NULL;
31ee747b 5211 }
40db5d2f
CB
5212
5213 copy = strdup(key);
31ee747b
CB
5214 if (!copy) {
5215 ERROR("Failed to duplicate string \"%s\"", key);
40db5d2f 5216 return NULL;
31ee747b 5217 }
40db5d2f 5218
f9373e40 5219 /* lxc.net.<n> */
31ee747b
CB
5220 if (!isdigit(*(key + 8))) {
5221 ERROR("Failed to detect digit in string \"%s\"", key + 8);
5222 goto on_error;
5223 }
40db5d2f 5224
31ee747b
CB
5225 /* beginning of index string */
5226 idx_start = (copy + 7);
5227 *idx_start = '\0';
40db5d2f 5228
31ee747b
CB
5229 /* end of index string */
5230 idx_end = strchr((copy + 8), '.');
7451daf8
CB
5231 if (idx_end)
5232 *idx_end = '\0';
31ee747b
CB
5233
5234 /* parse current index */
5235 ret = lxc_safe_uint((idx_start + 1), &tmpidx);
5236 if (ret < 0) {
6d1400b5 5237 errno = -ret;
ae52f396 5238 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
6d1400b5 5239 idx_start + 1);
31ee747b
CB
5240 *idx = ret;
5241 goto on_error;
5242 }
40db5d2f 5243
31ee747b
CB
5244 /* This, of course is utterly nonsensical on so many levels, but
5245 * better safe than sorry.
5246 * (Checking for INT_MAX here is intentional.)
5247 */
5248 if (tmpidx == INT_MAX) {
25a908b8 5249 SYSERROR("Number of configured networks would overflow the "
47903908 5250 "counter");
31ee747b 5251 goto on_error;
40db5d2f 5252 }
31ee747b
CB
5253 *idx = tmpidx;
5254
5255 numstrlen = strlen((idx_start + 1));
5256
5257 /* repair configuration key */
5258 *idx_start = '.';
31ee747b 5259
7451daf8
CB
5260 /* lxc.net.<idx>.<subkey> */
5261 if (idx_end) {
5262 *idx_end = '.';
24fa7df6
FA
5263 if (strlen(idx_end + 1) == 0) {
5264 ERROR("No subkey in network configuration key \"%s\"", key);
5265 goto on_error;
5266 }
40db5d2f 5267
7451daf8 5268 memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
52ab03ac 5269 copy[strlen(key) - (numstrlen + 1)] = '\0';
7451daf8 5270
300df83e 5271 config = lxc_get_config(copy);
7451daf8 5272 if (!config) {
25a908b8 5273 ERROR("Unknown network configuration key \"%s\"", key);
7451daf8
CB
5274 goto on_error;
5275 }
31ee747b
CB
5276 }
5277
7451daf8
CB
5278 if (deindexed_key)
5279 *deindexed_key = copy;
5280
31ee747b 5281 return config;
40db5d2f
CB
5282
5283on_error:
5284 free(copy);
31ee747b 5285 return NULL;
40db5d2f
CB
5286}
5287
25a908b8
CB
5288/* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5289 * found. So we make sure next comes an integer, find the right callback (by
5290 * rewriting the key), and call it.
40db5d2f 5291 */
f9373e40
CB
5292static int set_config_net_nic(const char *key, const char *value,
5293 struct lxc_conf *lxc_conf, void *data)
40db5d2f 5294{
31ee747b
CB
5295 int ret;
5296 const char *idxstring;
40db5d2f
CB
5297 struct lxc_config_t *config;
5298 struct lxc_netdev *netdev;
5299 ssize_t idx = -1;
31ee747b
CB
5300 char *deindexed_key = NULL;
5301
5302 idxstring = key + 8;
5303 if (!isdigit(*idxstring))
5304 return -1;
40db5d2f 5305
6bed0fb6 5306 if (lxc_config_value_empty(value))
f9373e40 5307 return clr_config_net_nic(key, lxc_conf, data);
6bed0fb6 5308
31ee747b 5309 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
40db5d2f
CB
5310 if (!config || idx < 0)
5311 return -1;
5312
0070b1c4 5313 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
31ee747b
CB
5314 if (!netdev) {
5315 free(deindexed_key);
40db5d2f 5316 return -1;
31ee747b 5317 }
40db5d2f 5318
31ee747b
CB
5319 ret = config->set(deindexed_key, value, lxc_conf, netdev);
5320 free(deindexed_key);
47903908 5321
31ee747b 5322 return ret;
40db5d2f 5323}
ff6da295 5324
f9373e40
CB
5325static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
5326 void *data)
ff6da295 5327{
31ee747b 5328 int ret;
519df1c1 5329 const char *idxstring;
ff6da295
CB
5330 struct lxc_config_t *config;
5331 struct lxc_netdev *netdev;
31ee747b
CB
5332 ssize_t idx = -1;
5333 char *deindexed_key = NULL;
519df1c1 5334
31ee747b
CB
5335 idxstring = key + 8;
5336 if (!isdigit(*idxstring))
519df1c1
CB
5337 return -1;
5338
519df1c1 5339 /* The left conjunct is pretty self-explanatory. The right conjunct
31ee747b 5340 * checks whether the two pointers are equal. If they are we know that
519df1c1
CB
5341 * this is not a key that is namespaced any further and so we are
5342 * supposed to clear the whole network.
5343 */
5344 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
5345 unsigned int rmnetdevidx;
5346
5347 if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0)
5348 return -1;
5349
5350 /* Remove network from network list. */
5351 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
5352 return 0;
5353 }
ff6da295 5354
31ee747b 5355 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
ff6da295
CB
5356 if (!config || idx < 0)
5357 return -1;
5358
0070b1c4 5359 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false);
31ee747b
CB
5360 if (!netdev) {
5361 free(deindexed_key);
ff6da295 5362 return -1;
31ee747b 5363 }
ff6da295 5364
31ee747b
CB
5365 ret = config->clr(deindexed_key, lxc_conf, netdev);
5366 free(deindexed_key);
47903908 5367
31ee747b 5368 return ret;
ff6da295
CB
5369}
5370
f9373e40
CB
5371static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
5372 void *data)
ff6da295 5373{
070a05af 5374 struct lxc_netdev *netdev = data;
ff6da295 5375
ff6da295
CB
5376 if (!netdev)
5377 return -1;
5378
5379 netdev->type = -1;
5380
5381 return 0;
5382}
5383
f9373e40
CB
5384static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
5385 void *data)
ff6da295 5386{
070a05af 5387 struct lxc_netdev *netdev = data;
ff6da295 5388
ff6da295
CB
5389 if (!netdev)
5390 return -1;
5391
de4855a8 5392 netdev->name[0] = '\0';
ff6da295
CB
5393
5394 return 0;
5395}
5396
f9373e40
CB
5397static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
5398 void *data)
ff6da295 5399{
070a05af 5400 struct lxc_netdev *netdev = data;
ff6da295 5401
ff6da295
CB
5402 if (!netdev)
5403 return -1;
5404
5405 netdev->flags = 0;
5406
5407 return 0;
5408}
5409
f9373e40
CB
5410static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
5411 void *data)
ff6da295 5412{
070a05af 5413 struct lxc_netdev *netdev = data;
ff6da295 5414
ff6da295
CB
5415 if (!netdev)
5416 return -1;
5417
de4855a8 5418 netdev->link[0] = '\0';
ff6da295
CB
5419
5420 return 0;
5421}
5422
6509154d 5423static int clr_config_net_l2proxy(const char *key, struct lxc_conf *lxc_conf,
5424 void *data)
5425{
5426 struct lxc_netdev *netdev = data;
5427
5428 if (!netdev)
21fce08c 5429 return ret_set_errno(-1, EINVAL);
6509154d 5430
5431 netdev->l2proxy = false;
5432
5433 return 0;
5434}
5435
f9373e40
CB
5436static int clr_config_net_macvlan_mode(const char *key,
5437 struct lxc_conf *lxc_conf, void *data)
ff6da295 5438{
070a05af 5439 struct lxc_netdev *netdev = data;
ff6da295 5440
ff6da295
CB
5441 if (!netdev)
5442 return -1;
5443
6bed0fb6
CB
5444 if (netdev->type != LXC_NET_MACVLAN)
5445 return 0;
5446
ff6da295
CB
5447 netdev->priv.macvlan_attr.mode = -1;
5448
5449 return 0;
5450}
5451
c9f52382 5452static int clr_config_net_ipvlan_mode(const char *key,
5453 struct lxc_conf *lxc_conf, void *data)
5454{
5455 struct lxc_netdev *netdev = data;
5456
5457 if (!netdev)
21fce08c 5458 return ret_set_errno(-1, EINVAL);
c9f52382 5459
5460 if (netdev->type != LXC_NET_IPVLAN)
5461 return 0;
5462
5463 netdev->priv.ipvlan_attr.mode = -1;
5464
5465 return 0;
5466}
5467
5468static int clr_config_net_ipvlan_isolation(const char *key,
5469 struct lxc_conf *lxc_conf, void *data)
5470{
5471 struct lxc_netdev *netdev = data;
5472
5473 if (!netdev)
21fce08c 5474 return ret_set_errno(-1, EINVAL);
c9f52382 5475
5476 if (netdev->type != LXC_NET_IPVLAN)
5477 return 0;
5478
5479 netdev->priv.ipvlan_attr.isolation = -1;
5480
5481 return 0;
5482}
5483
3f0ed090
TP
5484static int clr_config_net_veth_mode(const char *key,
5485 struct lxc_conf *lxc_conf, void *data)
5486{
5487 struct lxc_netdev *netdev = data;
5488
5489 if (!netdev)
21fce08c 5490 return ret_set_errno(-1, EINVAL);
3f0ed090
TP
5491
5492 if (netdev->type != LXC_NET_VETH)
5493 return 0;
5494
5495 netdev->priv.veth_attr.mode = -1;
5496
5497 return 0;
5498}
5499
f9373e40
CB
5500static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
5501 void *data)
ff6da295 5502{
070a05af 5503 struct lxc_netdev *netdev = data;
ff6da295 5504
ff6da295
CB
5505 if (!netdev)
5506 return -1;
5507
de4855a8 5508 netdev->priv.veth_attr.pair[0] = '\0';
ff6da295
CB
5509
5510 return 0;
5511}
5512
fdf76c6d
TP
5513static int clr_config_net_veth_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5514 void *data)
5515{
5516 struct lxc_netdev *netdev = data;
5517
5518 if (!netdev)
5519 return ret_errno(EINVAL);
5520
5521 netdev->priv.veth_attr.vlan_id = 0;
5522 netdev->priv.veth_attr.vlan_id_set = false;
5523
5524 return 0;
5525}
5526
b8e06d33
TP
5527static int clr_config_net_veth_vlan_tagged_id(const char *key,
5528 struct lxc_conf *lxc_conf, void *data)
5529{
5530 struct lxc_netdev *netdev = data;
5531 struct lxc_list *cur, *next;
5532
5533 if (!netdev)
5534 return ret_errno(EINVAL);
5535
5536 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.vlan_tagged_ids, next) {
5537 lxc_list_del(cur);
5538 free(cur);
5539 }
5540
5541 return 0;
5542}
5543
5544
f9373e40
CB
5545static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
5546 void *data)
ff6da295 5547{
070a05af 5548 struct lxc_netdev *netdev = data;
ff6da295 5549
ff6da295
CB
5550 if (!netdev)
5551 return -1;
5552
5553 free(netdev->upscript);
5554 netdev->upscript = NULL;
5555
5556 return 0;
5557}
5558
f9373e40
CB
5559static int clr_config_net_script_down(const char *key,
5560 struct lxc_conf *lxc_conf, void *data)
ff6da295 5561{
070a05af 5562 struct lxc_netdev *netdev = data;
ff6da295 5563
ff6da295
CB
5564 if (!netdev)
5565 return -1;
5566
5567 free(netdev->downscript);
5568 netdev->downscript = NULL;
5569
5570 return 0;
5571}
5572
f9373e40
CB
5573static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
5574 void *data)
ff6da295 5575{
070a05af 5576 struct lxc_netdev *netdev = data;
ff6da295 5577
ff6da295
CB
5578 if (!netdev)
5579 return -1;
5580
5581 free(netdev->hwaddr);
5582 netdev->hwaddr = NULL;
5583
5584 return 0;
5585}
5586
f9373e40
CB
5587static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
5588 void *data)
ff6da295 5589{
070a05af 5590 struct lxc_netdev *netdev = data;
ff6da295 5591
ff6da295
CB
5592 if (!netdev)
5593 return -1;
5594
5595 free(netdev->mtu);
5596 netdev->mtu = NULL;
5597
5598 return 0;
5599}
5600
f9373e40
CB
5601static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5602 void *data)
ff6da295 5603{
070a05af 5604 struct lxc_netdev *netdev = data;
ff6da295 5605
ff6da295
CB
5606 if (!netdev)
5607 return -1;
5608
5609 netdev->priv.vlan_attr.vid = 0;
5610
5611 return 0;
5612}
5613
f9373e40
CB
5614static int clr_config_net_ipv4_gateway(const char *key,
5615 struct lxc_conf *lxc_conf, void *data)
ff6da295 5616{
070a05af 5617 struct lxc_netdev *netdev = data;
ff6da295 5618
ff6da295
CB
5619 if (!netdev)
5620 return -1;
5621
5622 free(netdev->ipv4_gateway);
5623 netdev->ipv4_gateway = NULL;
5624
5625 return 0;
5626}
5627
9ff60df2
CB
5628static int clr_config_net_ipv4_address(const char *key,
5629 struct lxc_conf *lxc_conf, void *data)
ff6da295 5630{
070a05af 5631 struct lxc_netdev *netdev = data;
ff6da295
CB
5632 struct lxc_list *cur, *next;
5633
ecbb3790
CB
5634 if (!netdev)
5635 return -1;
ff6da295
CB
5636
5637 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
5638 lxc_list_del(cur);
5639 free(cur->elem);
5640 free(cur);
5641 }
5642
5643 return 0;
5644}
5645
d4a7da46 5646static int clr_config_net_veth_ipv4_route(const char *key,
5647 struct lxc_conf *lxc_conf, void *data)
5648{
5649 struct lxc_netdev *netdev = data;
5650 struct lxc_list *cur, *next;
5651
5652 if (!netdev)
5653 return -1;
5654
5655 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) {
5656 lxc_list_del(cur);
5657 free(cur->elem);
5658 free(cur);
5659 }
5660
5661 return 0;
5662}
5663
f9373e40
CB
5664static int clr_config_net_ipv6_gateway(const char *key,
5665 struct lxc_conf *lxc_conf, void *data)
ff6da295 5666{
070a05af 5667 struct lxc_netdev *netdev = data;
ff6da295 5668
ff6da295
CB
5669 if (!netdev)
5670 return -1;
5671
5672 free(netdev->ipv6_gateway);
5673 netdev->ipv6_gateway = NULL;
5674
5675 return 0;
5676}
5677
2e44ae28
CB
5678static int clr_config_net_ipv6_address(const char *key,
5679 struct lxc_conf *lxc_conf, void *data)
ff6da295 5680{
070a05af 5681 struct lxc_netdev *netdev = data;
ff6da295
CB
5682 struct lxc_list *cur, *next;
5683
ecbb3790
CB
5684 if (!netdev)
5685 return -1;
ff6da295
CB
5686
5687 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
5688 lxc_list_del(cur);
5689 free(cur->elem);
5690 free(cur);
5691 }
5692
5693 return 0;
5694}
9d4bf22d 5695
d4a7da46 5696static int clr_config_net_veth_ipv6_route(const char *key,
5697 struct lxc_conf *lxc_conf, void *data)
5698{
5699 struct lxc_netdev *netdev = data;
5700 struct lxc_list *cur, *next;
5701
5702 if (!netdev)
5703 return -1;
5704
5705 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) {
5706 lxc_list_del(cur);
5707 free(cur->elem);
5708 free(cur);
5709 }
5710
5711 return 0;
5712}
5713
f9373e40
CB
5714static int get_config_net_nic(const char *key, char *retv, int inlen,
5715 struct lxc_conf *c, void *data)
9d4bf22d 5716{
31ee747b
CB
5717 int ret;
5718 const char *idxstring;
9d4bf22d
CB
5719 struct lxc_config_t *config;
5720 struct lxc_netdev *netdev;
5721 ssize_t idx = -1;
31ee747b 5722 char *deindexed_key = NULL;
9d4bf22d 5723
31ee747b
CB
5724 idxstring = key + 8;
5725 if (!isdigit(*idxstring))
5726 return -1;
5727
5728 config = get_network_config_ops(key, c, &idx, &deindexed_key);
9d4bf22d
CB
5729 if (!config || idx < 0)
5730 return -1;
5731
0070b1c4 5732 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
31ee747b
CB
5733 if (!netdev) {
5734 free(deindexed_key);
9d4bf22d 5735 return -1;
31ee747b 5736 }
9d4bf22d 5737
31ee747b
CB
5738 ret = config->get(deindexed_key, retv, inlen, c, netdev);
5739 free(deindexed_key);
47903908 5740
31ee747b 5741 return ret;
9d4bf22d
CB
5742}
5743
f9373e40
CB
5744static int get_config_net_type(const char *key, char *retv, int inlen,
5745 struct lxc_conf *c, void *data)
9d4bf22d 5746{
070a05af
CB
5747 int len;
5748 int fulllen = 0;
5749 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5750
5751 if (!retv)
5752 inlen = 0;
5753 else
5754 memset(retv, 0, inlen);
5755
9d4bf22d
CB
5756 if (!netdev)
5757 return -1;
5758
5759 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
5760
5761 return fulllen;
5762}
5763
f9373e40
CB
5764static int get_config_net_flags(const char *key, char *retv, int inlen,
5765 struct lxc_conf *c, void *data)
9d4bf22d 5766{
070a05af
CB
5767 int len;
5768 int fulllen = 0;
5769 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5770
5771 if (!retv)
5772 inlen = 0;
5773 else
5774 memset(retv, 0, inlen);
5775
9d4bf22d
CB
5776 if (!netdev)
5777 return -1;
5778
5779 if (netdev->flags & IFF_UP)
5780 strprint(retv, inlen, "up");
5781
5782 return fulllen;
5783}
5784
f9373e40
CB
5785static int get_config_net_link(const char *key, char *retv, int inlen,
5786 struct lxc_conf *c, void *data)
9d4bf22d 5787{
070a05af
CB
5788 int len;
5789 int fulllen = 0;
5790 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5791
5792 if (!retv)
5793 inlen = 0;
5794 else
5795 memset(retv, 0, inlen);
5796
9d4bf22d
CB
5797 if (!netdev)
5798 return -1;
5799
de4855a8 5800 if (netdev->link[0] != '\0')
9d4bf22d
CB
5801 strprint(retv, inlen, "%s", netdev->link);
5802
5803 return fulllen;
5804}
5805
6509154d 5806static int get_config_net_l2proxy(const char *key, char *retv, int inlen,
5807 struct lxc_conf *c, void *data)
5808{
5809 struct lxc_netdev *netdev = data;
5810 return lxc_get_conf_bool(c, retv, inlen, netdev->l2proxy);
5811}
5812
f9373e40
CB
5813static int get_config_net_name(const char *key, char *retv, int inlen,
5814 struct lxc_conf *c, void *data)
9d4bf22d 5815{
070a05af
CB
5816 int len;
5817 int fulllen = 0;
5818 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5819
5820 if (!retv)
5821 inlen = 0;
5822 else
5823 memset(retv, 0, inlen);
5824
9d4bf22d
CB
5825 if (!netdev)
5826 return -1;
5827
de4855a8 5828 if (netdev->name[0] != '\0')
9d4bf22d
CB
5829 strprint(retv, inlen, "%s", netdev->name);
5830
5831 return fulllen;
5832}
5833
f9373e40
CB
5834static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
5835 struct lxc_conf *c, void *data)
9d4bf22d 5836{
070a05af
CB
5837 int len;
5838 int fulllen = 0;
9d4bf22d 5839 const char *mode;
070a05af 5840 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5841
5842 if (!retv)
5843 inlen = 0;
5844 else
5845 memset(retv, 0, inlen);
5846
9d4bf22d
CB
5847 if (!netdev)
5848 return -1;
5849
5850 if (netdev->type != LXC_NET_MACVLAN)
5851 return 0;
5852
5853 switch (netdev->priv.macvlan_attr.mode) {
5854 case MACVLAN_MODE_PRIVATE:
5855 mode = "private";
5856 break;
5857 case MACVLAN_MODE_VEPA:
5858 mode = "vepa";
5859 break;
5860 case MACVLAN_MODE_BRIDGE:
5861 mode = "bridge";
5862 break;
5863 case MACVLAN_MODE_PASSTHRU:
5864 mode = "passthru";
5865 break;
5866 default:
5867 mode = "(invalid)";
5868 break;
5869 }
5870
5871 strprint(retv, inlen, "%s", mode);
5872
5873 return fulllen;
5874}
5875
c9f52382 5876static int get_config_net_ipvlan_mode(const char *key, char *retv, int inlen,
5877 struct lxc_conf *c, void *data)
5878{
5879 int len;
5880 int fulllen = 0;
5881 const char *mode;
5882 struct lxc_netdev *netdev = data;
5883
5884 if (!retv)
5885 inlen = 0;
5886 else
5887 memset(retv, 0, inlen);
5888
5889 if (!netdev)
21fce08c 5890 return ret_set_errno(-1, EINVAL);
c9f52382 5891
5892 if (netdev->type != LXC_NET_IPVLAN)
5893 return 0;
5894
5895 switch (netdev->priv.ipvlan_attr.mode) {
5896 case IPVLAN_MODE_L3:
5897 mode = "l3";
5898 break;
5899 case IPVLAN_MODE_L3S:
5900 mode = "l3s";
5901 break;
5902 case IPVLAN_MODE_L2:
5903 mode = "l2";
5904 break;
5905 default:
5906 mode = "(invalid)";
5907 break;
5908 }
5909
5910 strprint(retv, inlen, "%s", mode);
5911
5912 return fulllen;
5913}
5914
5915static int get_config_net_ipvlan_isolation(const char *key, char *retv, int inlen,
5916 struct lxc_conf *c, void *data)
5917{
5918 int len;
5919 int fulllen = 0;
5920 const char *mode;
5921 struct lxc_netdev *netdev = data;
5922
5923 if (!retv)
5924 inlen = 0;
5925 else
5926 memset(retv, 0, inlen);
5927
5928 if (!netdev)
21fce08c 5929 return ret_set_errno(-1, EINVAL);
c9f52382 5930
5931 if (netdev->type != LXC_NET_IPVLAN)
5932 return 0;
5933
5934 switch (netdev->priv.ipvlan_attr.isolation) {
5935 case IPVLAN_ISOLATION_BRIDGE:
5936 mode = "bridge";
5937 break;
5938 case IPVLAN_ISOLATION_PRIVATE:
5939 mode = "private";
5940 break;
5941 case IPVLAN_ISOLATION_VEPA:
5942 mode = "vepa";
5943 break;
5944 default:
5945 mode = "(invalid)";
5946 break;
5947 }
5948
5949 strprint(retv, inlen, "%s", mode);
5950
5951 return fulllen;
5952}
5953
3f0ed090
TP
5954static int get_config_net_veth_mode(const char *key, char *retv, int inlen,
5955 struct lxc_conf *c, void *data)
5956{
5957 int len;
5958 int fulllen = 0;
5959 const char *mode;
5960 struct lxc_netdev *netdev = data;
5961
5962 if (!retv)
5963 inlen = 0;
5964 else
5965 memset(retv, 0, inlen);
5966
5967 if (!netdev)
21fce08c 5968 return ret_set_errno(-1, EINVAL);
3f0ed090
TP
5969
5970 if (netdev->type != LXC_NET_VETH)
5971 return 0;
5972
5973 switch (netdev->priv.veth_attr.mode) {
5974 case VETH_MODE_BRIDGE:
5975 mode = "bridge";
5976 break;
5977 case VETH_MODE_ROUTER:
5978 mode = "router";
5979 break;
5980 default:
5981 mode = "(invalid)";
5982 break;
5983 }
5984
5985 strprint(retv, inlen, "%s", mode);
5986
5987 return fulllen;
5988}
5989
f9373e40
CB
5990static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
5991 struct lxc_conf *c, void *data)
9d4bf22d 5992{
070a05af
CB
5993 int len;
5994 int fulllen = 0;
5995 struct lxc_netdev *netdev = data;
9d4bf22d
CB
5996
5997 if (!retv)
5998 inlen = 0;
5999 else
6000 memset(retv, 0, inlen);
6001
9d4bf22d
CB
6002 if (!netdev)
6003 return -1;
6004
6005 if (netdev->type != LXC_NET_VETH)
6006 return 0;
6007
6008 strprint(retv, inlen, "%s",
de4855a8
CB
6009 netdev->priv.veth_attr.pair[0] != '\0'
6010 ? netdev->priv.veth_attr.pair
6011 : netdev->priv.veth_attr.veth1);
9d4bf22d
CB
6012
6013 return fulllen;
6014}
6015
fdf76c6d
TP
6016static int get_config_net_veth_vlan_id(const char *key, char *retv, int inlen,
6017 struct lxc_conf *c, void *data)
6018{
6019 int len;
6020 int fulllen = 0;
6021 struct lxc_netdev *netdev = data;
6022
6023 if (!netdev)
6024 return ret_errno(EINVAL);
6025
6026 if (netdev->type != LXC_NET_VETH)
6027 return 0;
6028
6029 if (!retv)
6030 inlen = 0;
6031 else
6032 memset(retv, 0, inlen);
6033
6034 strprint(retv, inlen, "%d", netdev->priv.veth_attr.vlan_id);
6035
6036 return fulllen;
6037}
6038
b8e06d33
TP
6039static int get_config_net_veth_vlan_tagged_id(const char *key, char *retv, int inlen,
6040 struct lxc_conf *c, void *data)
6041{
6042 int len;
6043 size_t listlen;
6044 struct lxc_list *it;
6045 int fulllen = 0;
6046 struct lxc_netdev *netdev = data;
6047
6048 if (!netdev)
4e61b19d 6049 return ret_errno(EINVAL);
b8e06d33
TP
6050
6051 if (netdev->type != LXC_NET_VETH)
6052 return 0;
6053
6054 if (!retv)
6055 inlen = 0;
6056 else
6057 memset(retv, 0, inlen);
6058
6059 listlen = lxc_list_len(&netdev->priv.veth_attr.vlan_tagged_ids);
6060
6061 lxc_list_for_each(it, &netdev->priv.veth_attr.vlan_tagged_ids) {
6062 unsigned short i = PTR_TO_USHORT(it->elem);
6063 strprint(retv, inlen, "%u%s", i,
6064 (listlen-- > 1) ? "\n" : "");
6065 }
6066
6067 return fulllen;
6068}
6069
f9373e40
CB
6070static int get_config_net_script_up(const char *key, char *retv, int inlen,
6071 struct lxc_conf *c, void *data)
9d4bf22d 6072{
070a05af
CB
6073 int len;
6074 int fulllen = 0;
6075 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6076
6077 if (!retv)
6078 inlen = 0;
6079 else
6080 memset(retv, 0, inlen);
6081
9d4bf22d
CB
6082 if (!netdev)
6083 return -1;
6084
6085 if (netdev->upscript)
6086 strprint(retv, inlen, "%s", netdev->upscript);
6087
6088 return fulllen;
6089}
6090
f9373e40
CB
6091static int get_config_net_script_down(const char *key, char *retv, int inlen,
6092 struct lxc_conf *c, void *data)
9d4bf22d 6093{
070a05af
CB
6094 int len;
6095 int fulllen = 0;
6096 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6097
6098 if (!retv)
6099 inlen = 0;
6100 else
6101 memset(retv, 0, inlen);
6102
9d4bf22d
CB
6103 if (!netdev)
6104 return -1;
6105
6106 if (netdev->downscript)
6107 strprint(retv, inlen, "%s", netdev->downscript);
6108
6109 return fulllen;
6110}
6111
f9373e40
CB
6112static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
6113 struct lxc_conf *c, void *data)
9d4bf22d 6114{
070a05af
CB
6115 int len;
6116 int fulllen = 0;
6117 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6118
6119 if (!retv)
6120 inlen = 0;
6121 else
6122 memset(retv, 0, inlen);
6123
9d4bf22d
CB
6124 if (!netdev)
6125 return -1;
6126
6127 if (netdev->hwaddr)
6128 strprint(retv, inlen, "%s", netdev->hwaddr);
6129
6130 return fulllen;
6131}
6132
f9373e40
CB
6133static int get_config_net_mtu(const char *key, char *retv, int inlen,
6134 struct lxc_conf *c, void *data)
9d4bf22d 6135{
070a05af
CB
6136 int len;
6137 int fulllen = 0;
6138 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6139
6140 if (!retv)
6141 inlen = 0;
6142 else
6143 memset(retv, 0, inlen);
6144
9d4bf22d
CB
6145 if (!netdev)
6146 return -1;
6147
6148 if (netdev->mtu)
6149 strprint(retv, inlen, "%s", netdev->mtu);
6150
6151 return fulllen;
6152}
6153
f9373e40
CB
6154static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
6155 struct lxc_conf *c, void *data)
9d4bf22d 6156{
070a05af
CB
6157 int len;
6158 int fulllen = 0;
6159 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6160
6161 if (!retv)
6162 inlen = 0;
6163 else
6164 memset(retv, 0, inlen);
6165
9d4bf22d
CB
6166 if (!netdev)
6167 return -1;
6168
6169 if (netdev->type != LXC_NET_VLAN)
6170 return 0;
6171
6172 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
6173
6174 return fulllen;
6175}
6176
f9373e40
CB
6177static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
6178 struct lxc_conf *c, void *data)
9d4bf22d 6179{
070a05af 6180 int len;
9d4bf22d 6181 char buf[INET_ADDRSTRLEN];
070a05af
CB
6182 int fulllen = 0;
6183 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6184
6185 if (!retv)
6186 inlen = 0;
6187 else
6188 memset(retv, 0, inlen);
6189
9d4bf22d
CB
6190 if (!netdev)
6191 return -1;
6192
6193 if (netdev->ipv4_gateway_auto) {
6194 strprint(retv, inlen, "auto");
a2f9a670 6195 } else if (netdev->ipv4_gateway_dev) {
6196 strprint(retv, inlen, "dev");
9d4bf22d
CB
6197 } else if (netdev->ipv4_gateway) {
6198 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
6199 strprint(retv, inlen, "%s", buf);
6200 }
6201
6202 return fulllen;
6203}
6204
9ff60df2
CB
6205static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
6206 struct lxc_conf *c, void *data)
9d4bf22d 6207{
070a05af 6208 int len;
9d4bf22d
CB
6209 size_t listlen;
6210 char buf[INET_ADDRSTRLEN];
9d4bf22d 6211 struct lxc_list *it;
070a05af
CB
6212 int fulllen = 0;
6213 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6214
6215 if (!retv)
6216 inlen = 0;
6217 else
6218 memset(retv, 0, inlen);
6219
9d4bf22d
CB
6220 if (!netdev)
6221 return -1;
6222
6223 listlen = lxc_list_len(&netdev->ipv4);
47903908 6224
9d4bf22d
CB
6225 lxc_list_for_each(it, &netdev->ipv4) {
6226 struct lxc_inetdev *i = it->elem;
6227 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6fc7d8b6 6228 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
6229 (listlen-- > 1) ? "\n" : "");
6230 }
6231
6232 return fulllen;
6233}
6234
d4a7da46 6235static int get_config_net_veth_ipv4_route(const char *key, char *retv, int inlen,
6236 struct lxc_conf *c, void *data)
6237{
6238 int len;
6239 size_t listlen;
6240 char buf[INET_ADDRSTRLEN];
6241 struct lxc_list *it;
6242 int fulllen = 0;
6243 struct lxc_netdev *netdev = data;
6244
6245 if (!retv)
6246 inlen = 0;
6247 else
6248 memset(retv, 0, inlen);
6249
6250 if (!netdev)
21fce08c 6251 return ret_set_errno(-1, EINVAL);
d4a7da46 6252
6253 if (netdev->type != LXC_NET_VETH)
6254 return 0;
6255
6256 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv4_routes);
6257
6258 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv4_routes) {
6259 struct lxc_inetdev *i = it->elem;
6260 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6261 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6262 (listlen-- > 1) ? "\n" : "");
6263 }
6264
6265 return fulllen;
6266}
6267
f9373e40
CB
6268static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
6269 struct lxc_conf *c, void *data)
9d4bf22d 6270{
070a05af 6271 int len;
9d4bf22d 6272 char buf[INET6_ADDRSTRLEN];
070a05af
CB
6273 int fulllen = 0;
6274 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6275
6276 if (!retv)
6277 inlen = 0;
6278 else
6279 memset(retv, 0, inlen);
6280
9d4bf22d
CB
6281 if (!netdev)
6282 return -1;
6283
6284 if (netdev->ipv6_gateway_auto) {
6285 strprint(retv, inlen, "auto");
a2f9a670 6286 } else if (netdev->ipv6_gateway_dev) {
6287 strprint(retv, inlen, "dev");
9d4bf22d
CB
6288 } else if (netdev->ipv6_gateway) {
6289 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
6290 strprint(retv, inlen, "%s", buf);
6291 }
6292
6293 return fulllen;
6294}
6295
2e44ae28
CB
6296static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
6297 struct lxc_conf *c, void *data)
9d4bf22d 6298{
070a05af 6299 int len;
9d4bf22d
CB
6300 size_t listlen;
6301 char buf[INET6_ADDRSTRLEN];
9d4bf22d 6302 struct lxc_list *it;
070a05af
CB
6303 int fulllen = 0;
6304 struct lxc_netdev *netdev = data;
9d4bf22d
CB
6305
6306 if (!retv)
6307 inlen = 0;
6308 else
6309 memset(retv, 0, inlen);
6310
9d4bf22d
CB
6311 if (!netdev)
6312 return -1;
6313
6314 listlen = lxc_list_len(&netdev->ipv6);
47903908 6315
9d4bf22d
CB
6316 lxc_list_for_each(it, &netdev->ipv6) {
6317 struct lxc_inet6dev *i = it->elem;
6318 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6fc7d8b6 6319 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
9d4bf22d
CB
6320 (listlen-- > 1) ? "\n" : "");
6321 }
6322
6323 return fulllen;
6324}
cfc67626 6325
d4a7da46 6326static int get_config_net_veth_ipv6_route(const char *key, char *retv, int inlen,
6327 struct lxc_conf *c, void *data)
6328{
6329 int len;
6330 size_t listlen;
6331 char buf[INET6_ADDRSTRLEN];
6332 struct lxc_list *it;
6333 int fulllen = 0;
6334 struct lxc_netdev *netdev = data;
6335
6336 if (!retv)
6337 inlen = 0;
6338 else
6339 memset(retv, 0, inlen);
6340
6341 if (!netdev)
21fce08c 6342 return ret_set_errno(-1, EINVAL);
d4a7da46 6343
6344 if (netdev->type != LXC_NET_VETH)
6345 return 0;
6346
6347 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv6_routes);
6348
6349 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv6_routes) {
6350 struct lxc_inet6dev *i = it->elem;
6351 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6352 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6353 (listlen-- > 1) ? "\n" : "");
6354 }
6355
6356 return fulllen;
6357}
6358
cfc67626
CB
6359int lxc_list_config_items(char *retv, int inlen)
6360{
6361 size_t i;
6362 int len;
6363 int fulllen = 0;
6364
6365 if (!retv)
6366 inlen = 0;
6367 else
6368 memset(retv, 0, inlen);
6369
33eb2ec1
CB
6370 for (i = 0; i < config_jump_table_size; i++) {
6371 char *s = config_jump_table[i].name;
300df83e 6372
cfc67626
CB
6373 if (s[strlen(s) - 1] == '.')
6374 continue;
300df83e 6375
cfc67626
CB
6376 strprint(retv, inlen, "%s\n", s);
6377 }
6378
6379 return fulllen;
6380}
a5448c15 6381
fe9b7349
CB
6382int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
6383 int inlen)
6384{
6385 int len;
6386 int fulllen = 0;
6387
6388 if (!retv)
6389 inlen = 0;
6390 else
6391 memset(retv, 0, inlen);
6392
6393 if (!strcmp(key, "lxc.apparmor")) {
6394 strprint(retv, inlen, "allow_incomplete\n");
1800f924 6395 strprint(retv, inlen, "allow_nesting\n");
fe9b7349 6396 strprint(retv, inlen, "profile\n");
1800f924 6397 strprint(retv, inlen, "raw\n");
dae8c253
CB
6398 } else if (!strcmp(key, "lxc.cgroup")) {
6399 strprint(retv, inlen, "dir\n");
fe9b7349
CB
6400 } else if (!strcmp(key, "lxc.selinux")) {
6401 strprint(retv, inlen, "context\n");
4fef78bc 6402 strprint(retv, inlen, "context.keyring\n");
fe9b7349
CB
6403 } else if (!strcmp(key, "lxc.mount")) {
6404 strprint(retv, inlen, "auto\n");
6405 strprint(retv, inlen, "entry\n");
6406 strprint(retv, inlen, "fstab\n");
6407 } else if (!strcmp(key, "lxc.rootfs")) {
6408 strprint(retv, inlen, "mount\n");
6409 strprint(retv, inlen, "options\n");
6410 strprint(retv, inlen, "path\n");
6411 } else if (!strcmp(key, "lxc.uts")) {
6412 strprint(retv, inlen, "name\n");
6413 } else if (!strcmp(key, "lxc.hook")) {
6414 strprint(retv, inlen, "autodev\n");
63012bdd 6415 strprint(retv, inlen, "autodevtmpfssize\n");
fe9b7349
CB
6416 strprint(retv, inlen, "clone\n");
6417 strprint(retv, inlen, "destroy\n");
6418 strprint(retv, inlen, "mount\n");
6419 strprint(retv, inlen, "post-stop\n");
6420 strprint(retv, inlen, "pre-mount\n");
6421 strprint(retv, inlen, "pre-start\n");
08dd2805 6422 strprint(retv, inlen, "start-host\n");
fe9b7349
CB
6423 strprint(retv, inlen, "start\n");
6424 strprint(retv, inlen, "stop\n");
6425 } else if (!strcmp(key, "lxc.cap")) {
6426 strprint(retv, inlen, "drop\n");
6427 strprint(retv, inlen, "keep\n");
6428 } else if (!strcmp(key, "lxc.console")) {
6429 strprint(retv, inlen, "logfile\n");
6430 strprint(retv, inlen, "path\n");
6431 } else if (!strcmp(key, "lxc.seccomp")) {
6432 strprint(retv, inlen, "profile\n");
6433 } else if (!strcmp(key, "lxc.signal")) {
6434 strprint(retv, inlen, "halt\n");
6435 strprint(retv, inlen, "reboot\n");
6436 strprint(retv, inlen, "stop\n");
6437 } else if (!strcmp(key, "lxc.start")) {
6438 strprint(retv, inlen, "auto\n");
6439 strprint(retv, inlen, "delay\n");
6440 strprint(retv, inlen, "order\n");
6441 } else if (!strcmp(key, "lxc.monitor")) {
6442 strprint(retv, inlen, "unshare\n");
8f818a84
MB
6443 } else if (!strcmp(key, "lxc.keyring")) {
6444 strprint(retv, inlen, "session\n");
fe9b7349
CB
6445 } else {
6446 fulllen = -1;
6447 }
6448
6449 return fulllen;
6450}
6451
a5448c15
CB
6452int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
6453{
6454 int len;
6455 const char *idxstring;
a5448c15
CB
6456 struct lxc_netdev *netdev;
6457 int fulllen = 0;
6458 ssize_t idx = -1;
a5448c15
CB
6459
6460 idxstring = key + 8;
6461 if (!isdigit(*idxstring))
6462 return -1;
6463
7451daf8
CB
6464 (void)get_network_config_ops(key, c, &idx, NULL);
6465 if (idx < 0)
a5448c15
CB
6466 return -1;
6467
6468 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
a5448c15
CB
6469 if (!netdev)
6470 return -1;
6471
6472 if (!retv)
6473 inlen = 0;
6474 else
6475 memset(retv, 0, inlen);
6476
6477 strprint(retv, inlen, "type\n");
6478 strprint(retv, inlen, "script.up\n");
6479 strprint(retv, inlen, "script.down\n");
47903908 6480
a5448c15
CB
6481 if (netdev->type != LXC_NET_EMPTY) {
6482 strprint(retv, inlen, "flags\n");
6483 strprint(retv, inlen, "link\n");
6484 strprint(retv, inlen, "name\n");
6485 strprint(retv, inlen, "hwaddr\n");
6486 strprint(retv, inlen, "mtu\n");
6487 strprint(retv, inlen, "ipv6.address\n");
6488 strprint(retv, inlen, "ipv6.gateway\n");
6489 strprint(retv, inlen, "ipv4.address\n");
6490 strprint(retv, inlen, "ipv4.gateway\n");
6491 }
6492
6493 switch (netdev->type) {
6494 case LXC_NET_VETH:
6495 strprint(retv, inlen, "veth.pair\n");
d4a7da46 6496 strprint(retv, inlen, "veth.ipv4.route\n");
6497 strprint(retv, inlen, "veth.ipv6.route\n");
fdf76c6d 6498 strprint(retv, inlen, "veth.vlan.id\n");
a5448c15
CB
6499 break;
6500 case LXC_NET_MACVLAN:
6501 strprint(retv, inlen, "macvlan.mode\n");
6502 break;
c9f52382 6503 case LXC_NET_IPVLAN:
6504 strprint(retv, inlen, "ipvlan.mode\n");
6505 strprint(retv, inlen, "ipvlan.isolation\n");
6506 break;
a5448c15
CB
6507 case LXC_NET_VLAN:
6508 strprint(retv, inlen, "vlan.id\n");
6509 break;
6510 case LXC_NET_PHYS:
6511 break;
6512 }
6513
6514 return fulllen;
6515}