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