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