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