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