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