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