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