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