]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/confile.c
confile: cleanup set_config_cap_keep()
[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 __do_free char *keepcaps = NULL;
2239 __do_free struct lxc_list *keeplist = NULL;
2240 char *token;
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 ret_errno(ENOMEM);
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 return ret_errno(ENOMEM);
2259
2260 keeplist->elem = strdup(token);
2261 if (!keeplist->elem)
2262 return ret_errno(ENOMEM);
2263
2264 lxc_list_add_tail(&lxc_conf->keepcaps, move_ptr(keeplist));
2265 }
2266
2267 return 0;
2268 }
2269
2270 static int set_config_cap_drop(const char *key, const char *value,
2271 struct lxc_conf *lxc_conf, void *data)
2272 {
2273 char *dropcaps, *token;
2274 struct lxc_list *droplist;
2275 int ret = -1;
2276
2277 if (lxc_config_value_empty(value))
2278 return lxc_clear_config_caps(lxc_conf);
2279
2280 dropcaps = strdup(value);
2281 if (!dropcaps)
2282 return -1;
2283
2284 /* In case several capability drop is specified in a single line
2285 * split these caps in a single element for the list.
2286 */
2287 lxc_iterate_parts(token, dropcaps, " \t") {
2288 droplist = malloc(sizeof(*droplist));
2289 if (!droplist)
2290 goto on_error;
2291
2292 droplist->elem = strdup(token);
2293 if (!droplist->elem) {
2294 free(droplist);
2295 goto on_error;
2296 }
2297
2298 lxc_list_add_tail(&lxc_conf->caps, droplist);
2299 }
2300
2301 ret = 0;
2302
2303 on_error:
2304 free(dropcaps);
2305
2306 return ret;
2307 }
2308
2309 static int set_config_console_path(const char *key, const char *value,
2310 struct lxc_conf *lxc_conf, void *data)
2311 {
2312 return set_config_path_item(&lxc_conf->console.path, value);
2313 }
2314
2315 static int set_config_console_rotate(const char *key, const char *value,
2316 struct lxc_conf *lxc_conf, void *data)
2317 {
2318 if (lxc_config_value_empty(value)) {
2319 lxc_conf->console.log_rotate = 0;
2320 return 0;
2321 }
2322
2323 if (lxc_safe_uint(value, &lxc_conf->console.log_rotate) < 0)
2324 return -1;
2325
2326 if (lxc_conf->console.log_rotate > 1) {
2327 ERROR("The \"lxc.console.rotate\" config key can only be set "
2328 "to 0 or 1");
2329 return -1;
2330 }
2331
2332 return 0;
2333 }
2334
2335 static int set_config_console_logfile(const char *key, const char *value,
2336 struct lxc_conf *lxc_conf, void *data)
2337 {
2338 return set_config_path_item(&lxc_conf->console.log_path, value);
2339 }
2340
2341 static int set_config_console_buffer_size(const char *key, const char *value,
2342 struct lxc_conf *lxc_conf, void *data)
2343 {
2344 int ret;
2345 int64_t size;
2346 uint64_t buffer_size, pgsz;
2347
2348 if (lxc_config_value_empty(value)) {
2349 lxc_conf->console.buffer_size = 0;
2350 return 0;
2351 }
2352
2353 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2354 if (!strcmp(value, "auto")) {
2355 lxc_conf->console.buffer_size = 1 << 17;
2356 return 0;
2357 }
2358
2359 ret = parse_byte_size_string(value, &size);
2360 if (ret < 0)
2361 return -1;
2362
2363 if (size < 0)
2364 return -EINVAL;
2365
2366 /* must be at least a page size */
2367 pgsz = lxc_getpagesize();
2368 if ((uint64_t)size < pgsz) {
2369 NOTICE("Requested ringbuffer size for the console is %" PRId64
2370 " but must be at least %" PRId64
2371 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
2372 size, pgsz, pgsz);
2373 size = pgsz;
2374 }
2375
2376 buffer_size = lxc_find_next_power2((uint64_t)size);
2377 if (buffer_size == 0)
2378 return -EINVAL;
2379
2380 if (buffer_size != size)
2381 NOTICE("Passed size was not a power of 2. Rounding log size to "
2382 "next power of two: %" PRIu64 " bytes", buffer_size);
2383
2384 lxc_conf->console.buffer_size = buffer_size;
2385
2386 return 0;
2387 }
2388
2389 static int set_config_console_size(const char *key, const char *value,
2390 struct lxc_conf *lxc_conf, void *data)
2391 {
2392 int ret;
2393 int64_t size;
2394 uint64_t log_size, pgsz;
2395
2396 if (lxc_config_value_empty(value)) {
2397 lxc_conf->console.log_size = 0;
2398 return 0;
2399 }
2400
2401 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2402 if (!strcmp(value, "auto")) {
2403 lxc_conf->console.log_size = 1 << 17;
2404 return 0;
2405 }
2406
2407 ret = parse_byte_size_string(value, &size);
2408 if (ret < 0)
2409 return -1;
2410
2411 if (size < 0)
2412 return -EINVAL;
2413
2414 /* must be at least a page size */
2415 pgsz = lxc_getpagesize();
2416 if ((uint64_t)size < pgsz) {
2417 NOTICE("Requested ringbuffer size for the console is %" PRId64
2418 " but must be at least %" PRId64
2419 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
2420 size, pgsz, pgsz);
2421 size = pgsz;
2422 }
2423
2424 log_size = lxc_find_next_power2((uint64_t)size);
2425 if (log_size == 0)
2426 return -EINVAL;
2427
2428 if (log_size != size)
2429 NOTICE("Passed size was not a power of 2. Rounding log size to "
2430 "next power of two: %" PRIu64 " bytes", log_size);
2431
2432 lxc_conf->console.log_size = log_size;
2433
2434 return 0;
2435 }
2436
2437 /*
2438 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
2439 * file, we expand it in the unexpanded_config, so that after a save_config we
2440 * store the hwaddr for re-use.
2441 * This is only called when reading the config file, not when executing a
2442 * lxc.include.
2443 * 'x' and 'X' are substituted in-place.
2444 */
2445 static void update_hwaddr(const char *line)
2446 {
2447 char *p;
2448
2449 line += lxc_char_left_gc(line, strlen(line));
2450 if (line[0] == '#')
2451 return;
2452
2453 if (!lxc_config_net_is_hwaddr(line))
2454 return;
2455
2456 /* Let config_net_hwaddr raise the error. */
2457 p = strchr(line, '=');
2458 if (!p)
2459 return;
2460 p++;
2461
2462 while (isblank(*p))
2463 p++;
2464
2465 if (!*p)
2466 return;
2467
2468 rand_complete_hwaddr(p);
2469 }
2470
2471 int append_unexp_config_line(const char *line, struct lxc_conf *conf)
2472 {
2473 size_t linelen;
2474 size_t len = conf->unexpanded_len;
2475
2476 update_hwaddr(line);
2477
2478 linelen = strlen(line);
2479 while (conf->unexpanded_alloced <= len + linelen + 2) {
2480 char *tmp = realloc(conf->unexpanded_config,
2481 conf->unexpanded_alloced + 1024);
2482 if (!tmp)
2483 return -1;
2484
2485 if (!conf->unexpanded_config)
2486 *tmp = '\0';
2487
2488 conf->unexpanded_config = tmp;
2489 conf->unexpanded_alloced += 1024;
2490 }
2491
2492 memcpy(conf->unexpanded_config + conf->unexpanded_len, line, linelen);
2493 conf->unexpanded_len += linelen;
2494 if (line[linelen - 1] != '\n')
2495 conf->unexpanded_config[conf->unexpanded_len++] = '\n';
2496 conf->unexpanded_config[conf->unexpanded_len] = '\0';
2497
2498 return 0;
2499 }
2500
2501 static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf)
2502 {
2503 __do_closedir DIR *dir = NULL;
2504 struct dirent *direntp;
2505 char path[PATH_MAX];
2506 int len;
2507 int ret = -1;
2508
2509 dir = opendir(dirp);
2510 if (!dir)
2511 return -1;
2512
2513 while ((direntp = readdir(dir))) {
2514 const char *fnam;
2515
2516 fnam = direntp->d_name;
2517 if (!strcmp(fnam, "."))
2518 continue;
2519
2520 if (!strcmp(fnam, ".."))
2521 continue;
2522
2523 len = strlen(fnam);
2524 if (len < 6 || strncmp(fnam + len - 5, ".conf", 5) != 0)
2525 continue;
2526
2527 len = snprintf(path, PATH_MAX, "%s/%s", dirp, fnam);
2528 if (len < 0 || len >= PATH_MAX)
2529 return -1;
2530
2531 ret = lxc_config_read(path, lxc_conf, true);
2532 if (ret < 0)
2533 return -1;
2534 }
2535
2536 return 0;
2537 }
2538
2539 static int set_config_includefiles(const char *key, const char *value,
2540 struct lxc_conf *lxc_conf, void *data)
2541 {
2542 if (lxc_config_value_empty(value)) {
2543 clr_config_includefiles(key, lxc_conf, NULL);
2544 return 0;
2545 }
2546
2547 if (is_dir(value))
2548 return do_includedir(value, lxc_conf);
2549
2550 return lxc_config_read(value, lxc_conf, true);
2551 }
2552
2553 static int set_config_rootfs_path(const char *key, const char *value,
2554 struct lxc_conf *lxc_conf, void *data)
2555 {
2556 int ret;
2557 char *dup, *tmp;
2558 const char *container_path;
2559
2560 if (lxc_config_value_empty(value)) {
2561 free(lxc_conf->rootfs.path);
2562 lxc_conf->rootfs.path = NULL;
2563 return 0;
2564 }
2565
2566 dup = strdup(value);
2567 if (!dup)
2568 return -1;
2569
2570 /* Split <storage type>:<container path> into <storage type> and
2571 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2572 * "rootfs.path" to <container path>.
2573 */
2574 tmp = strchr(dup, ':');
2575 if (tmp) {
2576 *tmp = '\0';
2577
2578 ret = set_config_path_item(&lxc_conf->rootfs.bdev_type, dup);
2579 if (ret < 0) {
2580 free(dup);
2581 return -1;
2582 }
2583
2584 tmp++;
2585 container_path = tmp;
2586 } else {
2587 container_path = value;
2588 }
2589
2590 ret = set_config_path_item(&lxc_conf->rootfs.path, container_path);
2591 free(dup);
2592
2593 return ret;
2594 }
2595
2596 static int set_config_rootfs_managed(const char *key, const char *value,
2597 struct lxc_conf *lxc_conf, void *data)
2598 {
2599 return set_config_bool_item(&lxc_conf->rootfs.managed, value, true);
2600 }
2601
2602 static int set_config_rootfs_mount(const char *key, const char *value,
2603 struct lxc_conf *lxc_conf, void *data)
2604 {
2605 return set_config_path_item(&lxc_conf->rootfs.mount, value);
2606 }
2607
2608 static int set_config_rootfs_options(const char *key, const char *value,
2609 struct lxc_conf *lxc_conf, void *data)
2610 {
2611 unsigned long mflags = 0, pflags = 0;
2612 char *mdata = NULL, *opts = NULL;
2613 int ret;
2614 struct lxc_rootfs *rootfs = &lxc_conf->rootfs;
2615
2616 ret = parse_mntopts(value, &mflags, &mdata);
2617 if (ret < 0)
2618 return -EINVAL;
2619
2620 ret = parse_propagationopts(value, &pflags);
2621 if (ret < 0) {
2622 free(mdata);
2623 return -EINVAL;
2624 }
2625
2626 ret = set_config_string_item(&opts, value);
2627 if (ret < 0) {
2628 free(mdata);
2629 return -ENOMEM;
2630 }
2631
2632 rootfs->mountflags = mflags | pflags;
2633 rootfs->options = opts;
2634 rootfs->data = mdata;
2635
2636 return 0;
2637 }
2638
2639 static int set_config_uts_name(const char *key, const char *value,
2640 struct lxc_conf *lxc_conf, void *data)
2641 {
2642 struct utsname *utsname;
2643
2644 if (lxc_config_value_empty(value)) {
2645 clr_config_uts_name(key, lxc_conf, NULL);
2646 return 0;
2647 }
2648
2649 utsname = malloc(sizeof(*utsname));
2650 if (!utsname)
2651 return -1;
2652
2653 if (strlen(value) >= sizeof(utsname->nodename)) {
2654 free(utsname);
2655 return -1;
2656 }
2657
2658 (void)strlcpy(utsname->nodename, value, sizeof(utsname->nodename));
2659 free(lxc_conf->utsname);
2660 lxc_conf->utsname = utsname;
2661
2662 return 0;
2663 }
2664
2665 static int set_config_namespace_clone(const char *key, const char *value,
2666 struct lxc_conf *lxc_conf, void *data)
2667 {
2668 char *ns, *token;
2669 int cloneflag = 0;
2670
2671 if (lxc_config_value_empty(value))
2672 return clr_config_namespace_clone(key, lxc_conf, data);
2673
2674 if (lxc_conf->ns_keep != 0) {
2675 errno = EINVAL;
2676 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2677 "\"lxc.namespace.keep\"");
2678 return -EINVAL;
2679 }
2680
2681 ns = strdup(value);
2682 if (!ns)
2683 return -1;
2684
2685 lxc_iterate_parts(token, ns, " \t") {
2686 token += lxc_char_left_gc(token, strlen(token));
2687 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2688 cloneflag = lxc_namespace_2_cloneflag(token);
2689 if (cloneflag < 0) {
2690 free(ns);
2691 return -EINVAL;
2692 }
2693 lxc_conf->ns_clone |= cloneflag;
2694 }
2695 free(ns);
2696
2697 return 0;
2698 }
2699
2700 static int set_config_namespace_keep(const char *key, const char *value,
2701 struct lxc_conf *lxc_conf, void *data)
2702 {
2703 char *ns, *token;
2704 int cloneflag = 0;
2705
2706 if (lxc_config_value_empty(value))
2707 return clr_config_namespace_keep(key, lxc_conf, data);
2708
2709 if (lxc_conf->ns_clone != 0) {
2710 errno = EINVAL;
2711 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2712 "\"lxc.namespace.keep\"");
2713 return -EINVAL;
2714 }
2715
2716 ns = strdup(value);
2717 if (!ns)
2718 return -1;
2719
2720 lxc_iterate_parts(token, ns, " \t") {
2721 token += lxc_char_left_gc(token, strlen(token));
2722 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2723 cloneflag = lxc_namespace_2_cloneflag(token);
2724 if (cloneflag < 0) {
2725 free(ns);
2726 return -EINVAL;
2727 }
2728 lxc_conf->ns_keep |= cloneflag;
2729 }
2730 free(ns);
2731
2732 return 0;
2733 }
2734
2735 static int set_config_time_offset_boot(const char *key, const char *value,
2736 struct lxc_conf *lxc_conf, void *data)
2737 {
2738 int ret;
2739 char *unit;
2740 int64_t offset = 0;
2741 char buf[STRLITERALLEN("ms") + 1];
2742
2743 if (lxc_config_value_empty(value))
2744 return clr_config_time_offset_boot(key, lxc_conf, data);
2745
2746 ret = lxc_safe_int64_residual(value, &offset, 10, buf, sizeof(buf));
2747 if (ret)
2748 return ret;
2749
2750 unit = lxc_trim_whitespace_in_place(buf);
2751 if (strcmp(unit, "h") == 0) {
2752 if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_boot))
2753 return -EOVERFLOW;
2754 } else if (strcmp(unit, "m") == 0) {
2755 if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_boot))
2756 return -EOVERFLOW;
2757 } else if (strcmp(unit, "s") == 0) {
2758 lxc_conf->timens.s_boot = offset;
2759 } else if (strcmp(unit, "ms") == 0) {
2760 if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_boot))
2761 return -EOVERFLOW;
2762 } else if (strcmp(unit, "us") == 0) {
2763 if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_boot))
2764 return -EOVERFLOW;
2765 } else if (strcmp(unit, "ns") == 0) {
2766 lxc_conf->timens.ns_boot = offset;
2767 } else {
2768 return ret_errno(EINVAL);
2769 }
2770
2771 return 0;
2772 }
2773
2774 static int set_config_time_offset_monotonic(const char *key, const char *value,
2775 struct lxc_conf *lxc_conf, void *data)
2776 {
2777 int ret;
2778 char *unit;
2779 int64_t offset = 0;
2780 char buf[STRLITERALLEN("ms") + 1];
2781
2782 if (lxc_config_value_empty(value))
2783 return clr_config_time_offset_monotonic(key, lxc_conf, data);
2784
2785 ret = lxc_safe_int64_residual(value, &offset, 10, buf, sizeof(buf));
2786 if (ret)
2787 return ret;
2788
2789 unit = lxc_trim_whitespace_in_place(buf);
2790 if (strcmp(unit, "h") == 0) {
2791 if (!multiply_overflow(offset, 3600, &lxc_conf->timens.s_monotonic))
2792 return -EOVERFLOW;
2793 } else if (strcmp(unit, "m") == 0) {
2794 if (!multiply_overflow(offset, 60, &lxc_conf->timens.s_monotonic))
2795 return -EOVERFLOW;
2796 } else if (strcmp(unit, "s") == 0) {
2797 lxc_conf->timens.s_monotonic = offset;
2798 } else if (strcmp(unit, "ms") == 0) {
2799 if (!multiply_overflow(offset, 1000000, &lxc_conf->timens.ns_monotonic))
2800 return -EOVERFLOW;
2801 } else if (strcmp(unit, "us") == 0) {
2802 if (!multiply_overflow(offset, 1000, &lxc_conf->timens.ns_monotonic))
2803 return -EOVERFLOW;
2804 } else if (strcmp(unit, "ns") == 0) {
2805 lxc_conf->timens.ns_monotonic = offset;
2806 } else {
2807 return ret_errno(EINVAL);
2808 }
2809
2810 return 0;
2811 }
2812
2813 static int set_config_namespace_share(const char *key, const char *value,
2814 struct lxc_conf *lxc_conf, void *data)
2815 {
2816 int ns_idx;
2817 const char *namespace;
2818
2819 if (lxc_config_value_empty(value))
2820 return clr_config_namespace_share(key, lxc_conf, data);
2821
2822 namespace = key + STRLITERALLEN("lxc.namespace.share.");
2823 ns_idx = lxc_namespace_2_ns_idx(namespace);
2824 if (ns_idx < 0)
2825 return ns_idx;
2826
2827 return set_config_string_item(&lxc_conf->ns_share[ns_idx], value);
2828 }
2829
2830 struct parse_line_conf {
2831 struct lxc_conf *conf;
2832 bool from_include;
2833 };
2834
2835 static int parse_line(char *buffer, void *data)
2836 {
2837 char *dot, *key, *line, *linep, *value;
2838 bool empty_line;
2839 struct lxc_config_t *config;
2840 int ret = 0;
2841 char *dup = buffer;
2842 struct parse_line_conf *plc = data;
2843
2844 /* If there are newlines in the config file we should keep them. */
2845 empty_line = lxc_is_line_empty(dup);
2846 if (empty_line)
2847 dup = "\n";
2848
2849 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2850 * modified the original string on the stack by replacing '=' by '\0'
2851 * below.
2852 */
2853 linep = line = strdup(dup);
2854 if (!line)
2855 return -1;
2856
2857 if (!plc->from_include) {
2858 ret = append_unexp_config_line(line, plc->conf);
2859 if (ret < 0)
2860 goto on_error;
2861 }
2862
2863 if (empty_line)
2864 goto on_error;
2865
2866 line += lxc_char_left_gc(line, strlen(line));
2867
2868 /* ignore comments */
2869 if (line[0] == '#')
2870 goto on_error;
2871
2872 /* martian option - don't add it to the config itself */
2873 if (strncmp(line, "lxc.", 4))
2874 goto on_error;
2875
2876 ret = -1;
2877
2878 dot = strchr(line, '=');
2879 if (!dot) {
2880 ERROR("Invalid configuration line: %s", line);
2881 goto on_error;
2882 }
2883
2884 *dot = '\0';
2885 value = dot + 1;
2886
2887 key = line;
2888 key[lxc_char_right_gc(key, strlen(key))] = '\0';
2889
2890 value += lxc_char_left_gc(value, strlen(value));
2891 value[lxc_char_right_gc(value, strlen(value))] = '\0';
2892
2893 if (*value == '\'' || *value == '\"') {
2894 size_t len;
2895
2896 len = strlen(value);
2897 if (len > 1 && value[len - 1] == *value) {
2898 value[len - 1] = '\0';
2899 value++;
2900 }
2901 }
2902
2903 config = lxc_get_config(key);
2904 if (!config) {
2905 ERROR("Unknown configuration key \"%s\"", key);
2906 goto on_error;
2907 }
2908
2909 ret = config->set(key, value, plc->conf, NULL);
2910
2911 on_error:
2912 free(linep);
2913
2914 return ret;
2915 }
2916
2917 static struct new_config_item *parse_new_conf_line(char *buffer)
2918 {
2919 char *dot, *key, *line, *linep, *value;
2920 int ret = 0;
2921 char *dup = buffer;
2922 struct new_config_item *new = NULL;
2923
2924 linep = line = strdup(dup);
2925 if (!line)
2926 return NULL;
2927
2928 line += lxc_char_left_gc(line, strlen(line));
2929
2930 /* martian option - don't add it to the config itself */
2931 if (strncmp(line, "lxc.", 4))
2932 goto on_error;
2933
2934 ret = -1;
2935 dot = strchr(line, '=');
2936 if (!dot) {
2937 ERROR("Invalid configuration item: %s", line);
2938 goto on_error;
2939 }
2940
2941 *dot = '\0';
2942 value = dot + 1;
2943
2944 key = line;
2945 key[lxc_char_right_gc(key, strlen(key))] = '\0';
2946
2947 value += lxc_char_left_gc(value, strlen(value));
2948 value[lxc_char_right_gc(value, strlen(value))] = '\0';
2949
2950 if (*value == '\'' || *value == '\"') {
2951 size_t len;
2952
2953 len = strlen(value);
2954 if (len > 1 && value[len - 1] == *value) {
2955 value[len - 1] = '\0';
2956 value++;
2957 }
2958 }
2959
2960 ret = -1;
2961 new = malloc(sizeof(struct new_config_item));
2962 if (!new)
2963 goto on_error;
2964
2965 new->key = strdup(key);
2966 new->val = strdup(value);
2967 if (!new->val || !new->key)
2968 goto on_error;
2969
2970 ret = 0;
2971
2972 on_error:
2973 free(linep);
2974
2975 if (ret < 0 && new) {
2976 free(new->key);
2977 free(new->val);
2978 free(new);
2979 new = NULL;
2980 }
2981
2982 return new;
2983 }
2984
2985 int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
2986 {
2987 struct parse_line_conf c;
2988
2989 c.conf = conf;
2990 c.from_include = from_include;
2991
2992 /* Catch only the top level config file name in the structure. */
2993 if (!conf->rcfile)
2994 conf->rcfile = strdup(file);
2995
2996 return lxc_file_for_each_line_mmap(file, parse_line, &c);
2997 }
2998
2999 int lxc_config_define_add(struct lxc_list *defines, char *arg)
3000 {
3001 struct lxc_list *dent;
3002
3003 dent = malloc(sizeof(struct lxc_list));
3004 if (!dent)
3005 return -1;
3006
3007 dent->elem = parse_new_conf_line(arg);
3008 if (!dent->elem) {
3009 free(dent);
3010 return -1;
3011 }
3012
3013 lxc_list_add_tail(defines, dent);
3014
3015 return 0;
3016 }
3017
3018 bool lxc_config_define_load(struct lxc_list *defines, struct lxc_container *c)
3019 {
3020 struct lxc_list *it;
3021 bool bret = true;
3022
3023 lxc_list_for_each(it, defines) {
3024 struct new_config_item *new_item = it->elem;
3025 bret = c->set_config_item(c, new_item->key, new_item->val);
3026 if (!bret)
3027 break;
3028 }
3029
3030 lxc_config_define_free(defines);
3031
3032 return bret;
3033 }
3034
3035 void lxc_config_define_free(struct lxc_list *defines)
3036 {
3037 struct lxc_list *it, *next;
3038
3039 lxc_list_for_each_safe(it, defines, next) {
3040 struct new_config_item *new_item = it->elem;
3041 free(new_item->key);
3042 free(new_item->val);
3043 lxc_list_del(it);
3044 free(it);
3045 }
3046 }
3047
3048 signed long lxc_config_parse_arch(const char *arch)
3049 {
3050 #if HAVE_SYS_PERSONALITY_H
3051 size_t i;
3052 struct per_name {
3053 char *name;
3054 unsigned long per;
3055 } pername[] = {
3056 { "arm", PER_LINUX32 },
3057 { "armel", PER_LINUX32 },
3058 { "armhf", PER_LINUX32 },
3059 { "armv7l", PER_LINUX32 },
3060 { "athlon", PER_LINUX32 },
3061 { "i386", PER_LINUX32 },
3062 { "i486", PER_LINUX32 },
3063 { "i586", PER_LINUX32 },
3064 { "i686", PER_LINUX32 },
3065 { "linux32", PER_LINUX32 },
3066 { "mips", PER_LINUX32 },
3067 { "mipsel", PER_LINUX32 },
3068 { "ppc", PER_LINUX32 },
3069 { "powerpc", PER_LINUX32 },
3070 { "x86", PER_LINUX32 },
3071 { "amd64", PER_LINUX },
3072 { "arm64", PER_LINUX },
3073 { "linux64", PER_LINUX },
3074 { "mips64", PER_LINUX },
3075 { "mips64el", PER_LINUX },
3076 { "ppc64", PER_LINUX },
3077 { "ppc64el", PER_LINUX },
3078 { "ppc64le", PER_LINUX },
3079 { "powerpc64", PER_LINUX },
3080 { "s390x", PER_LINUX },
3081 { "x86_64", PER_LINUX },
3082 };
3083 size_t len = sizeof(pername) / sizeof(pername[0]);
3084
3085 for (i = 0; i < len; i++)
3086 if (!strcmp(pername[i].name, arch))
3087 return pername[i].per;
3088 #endif
3089
3090 return -1;
3091 }
3092
3093 int lxc_fill_elevated_privileges(char *flaglist, int *flags)
3094 {
3095 char *token;
3096 int i, aflag;
3097 struct {
3098 const char *token;
3099 int flag;
3100 } all_privs[] = {
3101 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP },
3102 { "CAP", LXC_ATTACH_DROP_CAPABILITIES },
3103 { "LSM", LXC_ATTACH_LSM_EXEC },
3104 { NULL, 0 }
3105 };
3106
3107 if (!flaglist) {
3108 /* For the sake of backward compatibility, drop all privileges
3109 * if none is specified.
3110 */
3111 for (i = 0; all_privs[i].token; i++)
3112 *flags |= all_privs[i].flag;
3113
3114 return 0;
3115 }
3116
3117 lxc_iterate_parts(token, flaglist, "|") {
3118 aflag = -1;
3119
3120 for (i = 0; all_privs[i].token; i++)
3121 if (!strcmp(all_privs[i].token, token))
3122 aflag = all_privs[i].flag;
3123
3124 if (aflag < 0)
3125 return -1;
3126
3127 *flags |= aflag;
3128 }
3129
3130 return 0;
3131 }
3132
3133 /* Write out a configuration file. */
3134 int write_config(int fd, const struct lxc_conf *conf)
3135 {
3136 int ret;
3137 size_t len = conf->unexpanded_len;
3138
3139 if (len == 0)
3140 return 0;
3141
3142 ret = lxc_write_nointr(fd, conf->unexpanded_config, len);
3143 if (ret < 0) {
3144 SYSERROR("Failed to write configuration file");
3145 return -1;
3146 }
3147
3148 return 0;
3149 }
3150
3151 bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key,
3152 const char *v)
3153 {
3154 __do_free char *tmp = NULL;
3155 int ret;
3156 size_t len;
3157
3158 len = strlen(key) + strlen(v) + 4;
3159 tmp = must_realloc(NULL, len);
3160
3161 if (lxc_config_value_empty(v))
3162 ret = snprintf(tmp, len, "%s =", key);
3163 else
3164 ret = snprintf(tmp, len, "%s = %s", key, v);
3165 if (ret < 0 || ret >= len)
3166 return false;
3167
3168 /* Save the line verbatim into unexpanded_conf */
3169 if (append_unexp_config_line(tmp, conf))
3170 return false;
3171
3172 return true;
3173 }
3174
3175 void clear_unexp_config_line(struct lxc_conf *conf, const char *key,
3176 bool rm_subkeys)
3177 {
3178 char *lend;
3179 char *lstart = conf->unexpanded_config;
3180
3181 if (!conf->unexpanded_config)
3182 return;
3183
3184 while (*lstart) {
3185 lend = strchr(lstart, '\n');
3186 char v;
3187
3188 if (!lend)
3189 lend = lstart + strlen(lstart);
3190 else
3191 lend++;
3192
3193 if (strncmp(lstart, key, strlen(key)) != 0) {
3194 lstart = lend;
3195 continue;
3196 }
3197
3198 if (!rm_subkeys) {
3199 v = lstart[strlen(key)];
3200 if (!isspace(v) && v != '=') {
3201 lstart = lend;
3202 continue;
3203 }
3204 }
3205
3206 conf->unexpanded_len -= (lend - lstart);
3207
3208 if (*lend == '\0') {
3209 *lstart = '\0';
3210 return;
3211 }
3212
3213 memmove(lstart, lend, strlen(lend) + 1);
3214 }
3215 }
3216
3217 bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
3218 const char *newpath, const char *oldname,
3219 const char *newname, const char *ovldir)
3220 {
3221 __do_free char *newdir = NULL,
3222 *olddir = NULL;
3223 int ret;
3224 char *lend, *p, *q;
3225 size_t newdirlen, olddirlen;
3226 char *lstart = conf->unexpanded_config;
3227 const char *key = "lxc.mount.entry";
3228
3229 olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
3230 olddir = must_realloc(NULL, olddirlen + 1);
3231 ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath,
3232 oldname);
3233 if (ret < 0 || ret >= olddirlen + 1)
3234 return false;
3235
3236 newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
3237 newdir = must_realloc(NULL, newdirlen + 1);
3238 ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath,
3239 newname);
3240 if (ret < 0 || ret >= newdirlen + 1)
3241 return false;
3242
3243 if (!conf->unexpanded_config)
3244 return true;
3245
3246 while (*lstart) {
3247 lend = strchr(lstart, '\n');
3248 if (!lend)
3249 lend = lstart + strlen(lstart);
3250 else
3251 lend++;
3252
3253 if (strncmp(lstart, key, strlen(key)) != 0)
3254 goto next;
3255
3256 p = strchr(lstart + strlen(key), '=');
3257 if (!p)
3258 goto next;
3259 p++;
3260
3261 while (isblank(*p))
3262 p++;
3263
3264 if (p >= lend)
3265 goto next;
3266
3267 /* Whenever a lxc.mount.entry entry is found in a line we check
3268 * if the substring "overlay" is present before doing any
3269 * further work. We check for "overlay" because substrings need
3270 * to have at least one space before them in a valid overlay
3271 * lxc.mount.entry (/A B overlay). When the space before is
3272 * missing it is very likely that these substrings are part of a
3273 * path or something else. (Checking q >= lend ensures that we
3274 * only count matches in the current line.) */
3275 q = strstr(p, " overlay");
3276 if (!q || q >= lend)
3277 goto next;
3278
3279 if (!(q = strstr(p, olddir)) || (q >= lend))
3280 goto next;
3281
3282 /* replace the olddir with newdir */
3283 if (olddirlen >= newdirlen) {
3284 size_t diff = olddirlen - newdirlen;
3285 memcpy(q, newdir, newdirlen);
3286
3287 if (olddirlen != newdirlen) {
3288 memmove(q + newdirlen, q + newdirlen + diff,
3289 strlen(q) - newdirlen - diff + 1);
3290 lend -= diff;
3291 conf->unexpanded_len -= diff;
3292 }
3293 } else {
3294 char *new;
3295 size_t diff = newdirlen - olddirlen;
3296 size_t oldlen = conf->unexpanded_len;
3297 size_t newlen = oldlen + diff;
3298 size_t poffset = q - conf->unexpanded_config;
3299
3300 new = realloc(conf->unexpanded_config, newlen + 1);
3301 if (!new)
3302 return false;
3303
3304 conf->unexpanded_len = newlen;
3305 conf->unexpanded_alloced = newlen + 1;
3306 new[newlen - 1] = '\0';
3307 lend = new + (lend - conf->unexpanded_config);
3308
3309 /* Move over the remainder to make room for the newdir.
3310 */
3311 memmove(new + poffset + newdirlen,
3312 new + poffset + olddirlen,
3313 oldlen - poffset - olddirlen + 1);
3314 conf->unexpanded_config = new;
3315
3316 memcpy(new + poffset, newdir, newdirlen);
3317 lend += diff;
3318 }
3319
3320 next:
3321 lstart = lend;
3322 }
3323
3324 return true;
3325 }
3326
3327 bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
3328 const char *newpath, const char *oldname,
3329 const char *newname)
3330 {
3331 __do_free char *newdir = NULL,
3332 *olddir = NULL;
3333 int ret;
3334 char *lend, *p;
3335 char *lstart = conf->unexpanded_config;
3336 size_t newdirlen, olddirlen;
3337 const char *key = "lxc.hook";
3338
3339 olddirlen = strlen(oldpath) + strlen(oldname) + 1;
3340 olddir = must_realloc(NULL, olddirlen + 1);
3341 ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
3342 if (ret < 0 || ret >= olddirlen + 1)
3343 return false;
3344
3345 newdirlen = strlen(newpath) + strlen(newname) + 1;
3346 newdir = must_realloc(NULL, newdirlen + 1);
3347 ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
3348 if (ret < 0 || ret >= newdirlen + 1)
3349 return false;
3350
3351 if (!conf->unexpanded_config)
3352 return true;
3353
3354 while (*lstart) {
3355 lend = strchr(lstart, '\n');
3356 if (!lend)
3357 lend = lstart + strlen(lstart);
3358 else
3359 lend++;
3360
3361 if (strncmp(lstart, key, strlen(key)) != 0)
3362 goto next;
3363
3364 p = strchr(lstart + strlen(key), '=');
3365 if (!p)
3366 goto next;
3367 p++;
3368
3369 while (isblank(*p))
3370 p++;
3371
3372 if (p >= lend)
3373 goto next;
3374
3375 if (strncmp(p, olddir, strlen(olddir)) != 0)
3376 goto next;
3377
3378 /* replace the olddir with newdir */
3379 if (olddirlen >= newdirlen) {
3380 size_t diff = olddirlen - newdirlen;
3381 memcpy(p, newdir, newdirlen);
3382
3383 if (olddirlen != newdirlen) {
3384 memmove(p + newdirlen, p + newdirlen + diff,
3385 strlen(p) - newdirlen - diff + 1);
3386 lend -= diff;
3387 conf->unexpanded_len -= diff;
3388 }
3389 } else {
3390 char *new;
3391 size_t diff = newdirlen - olddirlen;
3392 size_t oldlen = conf->unexpanded_len;
3393 size_t newlen = oldlen + diff;
3394 size_t poffset = p - conf->unexpanded_config;
3395
3396 new = realloc(conf->unexpanded_config, newlen + 1);
3397 if (!new)
3398 return false;
3399
3400 conf->unexpanded_len = newlen;
3401 conf->unexpanded_alloced = newlen + 1;
3402 new[newlen - 1] = '\0';
3403 lend = new + (lend - conf->unexpanded_config);
3404
3405 /* Move over the remainder to make room for the newdir.
3406 */
3407 memmove(new + poffset + newdirlen,
3408 new + poffset + olddirlen,
3409 oldlen - poffset - olddirlen + 1);
3410 conf->unexpanded_config = new;
3411
3412 memcpy(new + poffset, newdir, newdirlen);
3413 lend += diff;
3414 }
3415
3416 next:
3417 lstart = lend;
3418 }
3419
3420 return true;
3421 }
3422
3423 #define DO(cmd) \
3424 { \
3425 if (!(cmd)) { \
3426 ERROR("Error writing to new config"); \
3427 return false; \
3428 } \
3429 }
3430
3431 /* This is called only from clone. We wish to update all hwaddrs in the
3432 * unexpanded config file. We can't/don't want to update any which come from
3433 * lxc.includes (there shouldn't be any).
3434 * We can't just walk the c->lxc-conf->network list because that includes netifs
3435 * from the include files. So we update the ones which we find in the unexp
3436 * config file, then find the original macaddr in the conf->network, and update
3437 * that to the same value.
3438 */
3439 bool network_new_hwaddrs(struct lxc_conf *conf)
3440 {
3441 char *lend, *p, *p2;
3442 struct lxc_list *it;
3443 char *lstart = conf->unexpanded_config;
3444
3445 if (!conf->unexpanded_config)
3446 return true;
3447
3448 while (*lstart) {
3449 char newhwaddr[18], oldhwaddr[17];
3450
3451 lend = strchr(lstart, '\n');
3452 if (!lend)
3453 lend = lstart + strlen(lstart);
3454 else
3455 lend++;
3456
3457 if (!lxc_config_net_is_hwaddr(lstart)) {
3458 lstart = lend;
3459 continue;
3460 }
3461
3462 p = strchr(lstart, '=');
3463 if (!p) {
3464 lstart = lend;
3465 continue;
3466 }
3467
3468 p++;
3469 while (isblank(*p))
3470 p++;
3471 if (!*p)
3472 return true;
3473
3474 p2 = p;
3475 while (*p2 && !isblank(*p2) && *p2 != '\n')
3476 p2++;
3477
3478 if ((p2 - p) != 17) {
3479 WARN("Bad hwaddr entry");
3480 lstart = lend;
3481 continue;
3482 }
3483
3484 memcpy(oldhwaddr, p, 17);
3485
3486 if (!new_hwaddr(newhwaddr))
3487 return false;
3488
3489 memcpy(p, newhwaddr, 17);
3490 lxc_list_for_each(it, &conf->network) {
3491 struct lxc_netdev *n = it->elem;
3492
3493 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
3494 memcpy(n->hwaddr, newhwaddr, 17);
3495 }
3496
3497 lstart = lend;
3498 }
3499
3500 return true;
3501 }
3502
3503 static int set_config_ephemeral(const char *key, const char *value,
3504 struct lxc_conf *lxc_conf, void *data)
3505 {
3506 if (lxc_config_value_empty(value)) {
3507 lxc_conf->ephemeral = 0;
3508 return 0;
3509 }
3510
3511 if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0)
3512 return -1;
3513
3514 if (lxc_conf->ephemeral > 1)
3515 return -1;
3516
3517 return 0;
3518 }
3519
3520 static int set_config_log_syslog(const char *key, const char *value,
3521 struct lxc_conf *lxc_conf, void *data)
3522 {
3523 int facility;
3524
3525 if (lxc_conf->syslog) {
3526 free(lxc_conf->syslog);
3527 lxc_conf->syslog = NULL;
3528 }
3529
3530 if (lxc_config_value_empty(value))
3531 return 0;
3532
3533 facility = lxc_syslog_priority_to_int(value);
3534 if (facility == -EINVAL)
3535 return -1;
3536
3537 lxc_log_syslog(facility);
3538
3539 return set_config_string_item(&lxc_conf->syslog, value);
3540 }
3541
3542 static int set_config_no_new_privs(const char *key, const char *value,
3543 struct lxc_conf *lxc_conf, void *data)
3544 {
3545 unsigned int v;
3546
3547 if (lxc_config_value_empty(value)) {
3548 lxc_conf->no_new_privs = false;
3549 return 0;
3550 }
3551
3552 if (lxc_safe_uint(value, &v) < 0)
3553 return -1;
3554
3555 if (v > 1)
3556 return -1;
3557
3558 lxc_conf->no_new_privs = v ? true : false;
3559
3560 return 0;
3561 }
3562
3563 /* Callbacks to get configuration items. */
3564 static int get_config_personality(const char *key, char *retv, int inlen,
3565 struct lxc_conf *c, void *data)
3566 {
3567 int fulllen = 0;
3568
3569 if (!retv)
3570 inlen = 0;
3571 else
3572 memset(retv, 0, inlen);
3573
3574 #if HAVE_SYS_PERSONALITY_H
3575 int len = 0;
3576
3577 switch (c->personality) {
3578 case PER_LINUX32:
3579 strprint(retv, inlen, "i686");
3580 break;
3581 case PER_LINUX:
3582 strprint(retv, inlen, "x86_64");
3583 break;
3584 default:
3585 break;
3586 }
3587 #endif
3588
3589 return fulllen;
3590 }
3591
3592 static int get_config_pty_max(const char *key, char *retv, int inlen,
3593 struct lxc_conf *c, void *data)
3594 {
3595 return lxc_get_conf_size_t(c, retv, inlen, c->pty_max);
3596 }
3597
3598 static int get_config_tty_max(const char *key, char *retv, int inlen,
3599 struct lxc_conf *c, void *data)
3600 {
3601 return lxc_get_conf_size_t(c, retv, inlen, c->ttys.max);
3602 }
3603
3604 static int get_config_tty_dir(const char *key, char *retv, int inlen,
3605 struct lxc_conf *c, void *data)
3606 {
3607 return lxc_get_conf_str(retv, inlen, c->ttys.dir);
3608 }
3609
3610 static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
3611 struct lxc_conf *c, void *data)
3612 {
3613 return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile);
3614 }
3615
3616 static int get_config_apparmor_allow_incomplete(const char *key, char *retv,
3617 int inlen, struct lxc_conf *c,
3618 void *data)
3619 {
3620 return lxc_get_conf_int(c, retv, inlen,
3621 c->lsm_aa_allow_incomplete);
3622 }
3623
3624 static int get_config_apparmor_allow_nesting(const char *key, char *retv,
3625 int inlen, struct lxc_conf *c,
3626 void *data)
3627 {
3628 return lxc_get_conf_int(c, retv, inlen,
3629 c->lsm_aa_allow_nesting);
3630 }
3631
3632 static int get_config_apparmor_raw(const char *key, char *retv,
3633 int inlen, struct lxc_conf *c,
3634 void *data)
3635 {
3636 int len;
3637 struct lxc_list *it;
3638 int fulllen = 0;
3639
3640 if (!retv)
3641 inlen = 0;
3642 else
3643 memset(retv, 0, inlen);
3644
3645 lxc_list_for_each(it, &c->lsm_aa_raw) {
3646 strprint(retv, inlen, "%s\n", (char *)it->elem);
3647 }
3648
3649 return fulllen;
3650 }
3651
3652 static int get_config_selinux_context(const char *key, char *retv, int inlen,
3653 struct lxc_conf *c, void *data)
3654 {
3655 return lxc_get_conf_str(retv, inlen, c->lsm_se_context);
3656 }
3657
3658 static int get_config_selinux_context_keyring(const char *key, char *retv, int inlen,
3659 struct lxc_conf *c, void *data)
3660 {
3661 return lxc_get_conf_str(retv, inlen, c->lsm_se_keyring_context);
3662 }
3663
3664 static int get_config_keyring_session(const char *key, char *retv, int inlen,
3665 struct lxc_conf *c, void *data)
3666 {
3667 return lxc_get_conf_bool(c, retv, inlen, c->keyring_disable_session);
3668 }
3669
3670
3671 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3672 * just the value(s) will be printed. Since there still could be more than one,
3673 * it is newline-separated.
3674 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3675 * have newlines?)
3676 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3677 * 'lxc.cgroup.subsystem.key = value' format.
3678 */
3679 static int __get_config_cgroup_controller(const char *key, char *retv,
3680 int inlen, struct lxc_conf *c,
3681 int version)
3682 {
3683 int len;
3684 size_t namespaced_token_len;
3685 char *global_token, *namespaced_token;
3686 struct lxc_list *it;
3687 int fulllen = 0;
3688 bool get_all = false;
3689
3690 if (!retv)
3691 inlen = 0;
3692 else
3693 memset(retv, 0, inlen);
3694
3695 if (version == CGROUP2_SUPER_MAGIC) {
3696 global_token = "lxc.cgroup2";
3697 namespaced_token = "lxc.cgroup2.";
3698 namespaced_token_len = STRLITERALLEN("lxc.cgroup2.");
3699 } else if (version == CGROUP_SUPER_MAGIC) {
3700 global_token = "lxc.cgroup";
3701 namespaced_token = "lxc.cgroup.";
3702 namespaced_token_len = STRLITERALLEN("lxc.cgroup.");
3703 } else {
3704 return -1;
3705 }
3706
3707 if (strcmp(key, global_token) == 0)
3708 get_all = true;
3709 else if (strncmp(key, namespaced_token, namespaced_token_len) == 0)
3710 key += namespaced_token_len;
3711 else
3712 return -1;
3713
3714 lxc_list_for_each(it, &c->cgroup) {
3715 struct lxc_cgroup *cg = it->elem;
3716
3717 if (get_all) {
3718 if (version != cg->version)
3719 continue;
3720
3721 strprint(retv, inlen, "%s.%s = %s\n", global_token,
3722 cg->subsystem, cg->value);
3723 } else if (strcmp(cg->subsystem, key) == 0) {
3724 strprint(retv, inlen, "%s\n", cg->value);
3725 }
3726 }
3727
3728 return fulllen;
3729 }
3730
3731 static int get_config_cgroup_controller(const char *key, char *retv, int inlen,
3732 struct lxc_conf *c, void *data)
3733 {
3734 return __get_config_cgroup_controller(key, retv, inlen, c,
3735 CGROUP_SUPER_MAGIC);
3736 }
3737
3738 static int get_config_cgroup2_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 CGROUP2_SUPER_MAGIC);
3743 }
3744
3745 static int get_config_cgroup_dir(const char *key, char *retv, int inlen,
3746 struct lxc_conf *lxc_conf, void *data)
3747 {
3748 int len;
3749 int fulllen = 0;
3750
3751 if (!retv)
3752 inlen = 0;
3753 else
3754 memset(retv, 0, inlen);
3755
3756 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir);
3757
3758 return fulllen;
3759 }
3760
3761 static int get_config_cgroup_monitor_dir(const char *key, char *retv, int inlen,
3762 struct lxc_conf *lxc_conf, void *data)
3763 {
3764 int len;
3765 int fulllen = 0;
3766
3767 if (!retv)
3768 inlen = 0;
3769 else
3770 memset(retv, 0, inlen);
3771
3772 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.monitor_dir);
3773
3774 return fulllen;
3775 }
3776
3777 static int get_config_cgroup_monitor_pivot_dir(const char *key, char *retv, int inlen,
3778 struct lxc_conf *lxc_conf, void *data)
3779 {
3780 int len;
3781 int fulllen = 0;
3782
3783 if (!retv)
3784 inlen = 0;
3785 else
3786 memset(retv, 0, inlen);
3787
3788 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.monitor_pivot_dir);
3789
3790 return fulllen;
3791 }
3792
3793 static int get_config_cgroup_container_dir(const char *key, char *retv,
3794 int inlen,
3795 struct lxc_conf *lxc_conf,
3796 void *data)
3797 {
3798 int len;
3799 int fulllen = 0;
3800
3801 if (!retv)
3802 inlen = 0;
3803 else
3804 memset(retv, 0, inlen);
3805
3806 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.container_dir);
3807
3808 return fulllen;
3809 }
3810
3811 static int get_config_cgroup_container_inner_dir(const char *key, char *retv,
3812 int inlen,
3813 struct lxc_conf *lxc_conf,
3814 void *data)
3815 {
3816 int len;
3817 int fulllen = 0;
3818
3819 if (!retv)
3820 inlen = 0;
3821 else
3822 memset(retv, 0, inlen);
3823
3824 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.namespace_dir);
3825
3826 return fulllen;
3827 }
3828
3829 static inline int get_config_cgroup_relative(const char *key, char *retv,
3830 int inlen, struct lxc_conf *lxc_conf,
3831 void *data)
3832 {
3833 return lxc_get_conf_int(lxc_conf, retv, inlen,
3834 lxc_conf->cgroup_meta.relative);
3835 }
3836
3837 static int get_config_idmaps(const char *key, char *retv, int inlen,
3838 struct lxc_conf *c, void *data)
3839 {
3840 struct lxc_list *it;
3841 int len, listlen, ret;
3842 int fulllen = 0;
3843 /* "u 1000 1000000 65536"
3844 *
3845 * let's render this as
3846 *
3847 * sizeof(char)
3848 * +
3849 * sizeof(" ")
3850 * +
3851 * sizeof(uint32_t)
3852 * +
3853 * sizeof(" ")
3854 * +
3855 * sizeof(uint32_t)
3856 * +
3857 * sizeof(" ")
3858 * +
3859 * sizeof(uint32_t)
3860 * +
3861 * \0
3862 */
3863 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3864 char buf[__LXC_IDMAP_STR_BUF];
3865
3866 if (!retv)
3867 inlen = 0;
3868 else
3869 memset(retv, 0, inlen);
3870
3871 listlen = lxc_list_len(&c->id_map);
3872 lxc_list_for_each(it, &c->id_map) {
3873 struct id_map *map = it->elem;
3874 ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu",
3875 (map->idtype == ID_TYPE_UID) ? 'u' : 'g',
3876 map->nsid, map->hostid, map->range);
3877 if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF)
3878 return -1;
3879
3880 strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : "");
3881 }
3882
3883 return fulllen;
3884 }
3885
3886 static int get_config_log_level(const char *key, char *retv, int inlen,
3887 struct lxc_conf *c, void *data)
3888 {
3889 const char *v;
3890 v = lxc_log_priority_to_string(c->loglevel);
3891 return lxc_get_conf_str(retv, inlen, v);
3892 }
3893
3894 static int get_config_log_file(const char *key, char *retv, int inlen,
3895 struct lxc_conf *c, void *data)
3896 {
3897 return lxc_get_conf_str(retv, inlen, c->logfile);
3898 }
3899
3900 static int get_config_mount_fstab(const char *key, char *retv, int inlen,
3901 struct lxc_conf *c, void *data)
3902 {
3903 return lxc_get_conf_str(retv, inlen, c->fstab);
3904 }
3905
3906 static int get_config_mount_auto(const char *key, char *retv, int inlen,
3907 struct lxc_conf *c, void *data)
3908 {
3909 int len, fulllen = 0;
3910 const char *sep = "";
3911
3912 if (!retv)
3913 inlen = 0;
3914 else
3915 memset(retv, 0, inlen);
3916
3917 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
3918 return 0;
3919
3920 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
3921 case LXC_AUTO_PROC_MIXED:
3922 strprint(retv, inlen, "%sproc:mixed", sep);
3923 sep = " ";
3924 break;
3925 case LXC_AUTO_PROC_RW:
3926 strprint(retv, inlen, "%sproc:rw", sep);
3927 sep = " ";
3928 break;
3929 default:
3930 break;
3931 }
3932
3933 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
3934 case LXC_AUTO_SYS_RO:
3935 strprint(retv, inlen, "%ssys:ro", sep);
3936 sep = " ";
3937 break;
3938 case LXC_AUTO_SYS_RW:
3939 strprint(retv, inlen, "%ssys:rw", sep);
3940 sep = " ";
3941 break;
3942 case LXC_AUTO_SYS_MIXED:
3943 strprint(retv, inlen, "%ssys:mixed", sep);
3944 sep = " ";
3945 break;
3946 default:
3947 break;
3948 }
3949
3950 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
3951 case LXC_AUTO_CGROUP_NOSPEC:
3952 strprint(retv, inlen, "%scgroup", sep);
3953 break;
3954 case LXC_AUTO_CGROUP_MIXED:
3955 strprint(retv, inlen, "%scgroup:mixed", sep);
3956 break;
3957 case LXC_AUTO_CGROUP_RO:
3958 strprint(retv, inlen, "%scgroup:ro", sep);
3959 break;
3960 case LXC_AUTO_CGROUP_RW:
3961 strprint(retv, inlen, "%scgroup:rw", sep);
3962 break;
3963 case LXC_AUTO_CGROUP_FULL_NOSPEC:
3964 strprint(retv, inlen, "%scgroup-full", sep);
3965 break;
3966 case LXC_AUTO_CGROUP_FULL_MIXED:
3967 strprint(retv, inlen, "%scgroup-full:mixed", sep);
3968 break;
3969 case LXC_AUTO_CGROUP_FULL_RO:
3970 strprint(retv, inlen, "%scgroup-full:ro", sep);
3971 break;
3972 case LXC_AUTO_CGROUP_FULL_RW:
3973 strprint(retv, inlen, "%scgroup-full:rw", sep);
3974 break;
3975 default:
3976 break;
3977 }
3978
3979 return fulllen;
3980 }
3981
3982 static int get_config_mount(const char *key, char *retv, int inlen,
3983 struct lxc_conf *c, void *data)
3984 {
3985 int len, fulllen = 0;
3986 struct lxc_list *it;
3987
3988 if (!retv)
3989 inlen = 0;
3990 else
3991 memset(retv, 0, inlen);
3992
3993 lxc_list_for_each(it, &c->mount_list) {
3994 strprint(retv, inlen, "%s\n", (char *)it->elem);
3995 }
3996
3997 return fulllen;
3998 }
3999
4000 static int get_config_rootfs_path(const char *key, char *retv, int inlen,
4001 struct lxc_conf *c, void *data)
4002 {
4003 return lxc_get_conf_str(retv, inlen, c->rootfs.path);
4004 }
4005
4006 static int get_config_rootfs_managed(const char *key, char *retv, int inlen,
4007 struct lxc_conf *c, void *data)
4008 {
4009 return lxc_get_conf_bool(c, retv, inlen, c->rootfs.managed);
4010 }
4011
4012 static int get_config_rootfs_mount(const char *key, char *retv, int inlen,
4013 struct lxc_conf *c, void *data)
4014 {
4015 return lxc_get_conf_str(retv, inlen, c->rootfs.mount);
4016 }
4017
4018 static int get_config_rootfs_options(const char *key, char *retv, int inlen,
4019 struct lxc_conf *c, void *data)
4020 {
4021 return lxc_get_conf_str(retv, inlen, c->rootfs.options);
4022 }
4023
4024 static int get_config_uts_name(const char *key, char *retv, int inlen,
4025 struct lxc_conf *c, void *data)
4026 {
4027 return lxc_get_conf_str(
4028 retv, inlen,
4029 c->utsname ? c->utsname->nodename : NULL);
4030 }
4031
4032 static int get_config_hooks(const char *key, char *retv, int inlen,
4033 struct lxc_conf *c, void *data)
4034 {
4035 char *subkey;
4036 int len, fulllen = 0, found = -1;
4037 struct lxc_list *it;
4038 int i;
4039
4040 subkey = strchr(key, '.');
4041 if (!subkey)
4042 return -1;
4043
4044 subkey = strchr(subkey + 1, '.');
4045 if (!subkey)
4046 return -1;
4047 subkey++;
4048 if (*subkey == '\0')
4049 return -1;
4050
4051 for (i = 0; i < NUM_LXC_HOOKS; i++) {
4052 if (strcmp(lxchook_names[i], subkey) == 0) {
4053 found = i;
4054 break;
4055 }
4056 }
4057
4058 if (found == -1)
4059 return -1;
4060
4061 if (!retv)
4062 inlen = 0;
4063 else
4064 memset(retv, 0, inlen);
4065
4066 lxc_list_for_each(it, &c->hooks[found]) {
4067 strprint(retv, inlen, "%s\n", (char *)it->elem);
4068 }
4069
4070 return fulllen;
4071 }
4072
4073 static int get_config_hooks_version(const char *key, char *retv, int inlen,
4074 struct lxc_conf *c, void *data)
4075 {
4076 return lxc_get_conf_int(c, retv, inlen, c->hooks_version);
4077 }
4078
4079 static int get_config_net(const char *key, char *retv, int inlen,
4080 struct lxc_conf *c, void *data)
4081 {
4082 int len, fulllen = 0;
4083 struct lxc_list *it;
4084
4085 if (!retv)
4086 inlen = 0;
4087 else
4088 memset(retv, 0, inlen);
4089
4090 lxc_list_for_each(it, &c->network) {
4091 struct lxc_netdev *n = it->elem;
4092 const char *t = lxc_net_type_to_str(n->type);
4093 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
4094 }
4095
4096 return fulllen;
4097 }
4098
4099 static int get_config_cap_drop(const char *key, char *retv, int inlen,
4100 struct lxc_conf *c, void *data)
4101 {
4102 int len, fulllen = 0;
4103 struct lxc_list *it;
4104
4105 if (!retv)
4106 inlen = 0;
4107 else
4108 memset(retv, 0, inlen);
4109
4110 lxc_list_for_each(it, &c->caps) {
4111 strprint(retv, inlen, "%s\n", (char *)it->elem);
4112 }
4113
4114 return fulllen;
4115 }
4116
4117 static int get_config_cap_keep(const char *key, char *retv, int inlen,
4118 struct lxc_conf *c, void *data)
4119 {
4120 int len, fulllen = 0;
4121 struct lxc_list *it;
4122
4123 if (!retv)
4124 inlen = 0;
4125 else
4126 memset(retv, 0, inlen);
4127
4128 lxc_list_for_each(it, &c->keepcaps) {
4129 strprint(retv, inlen, "%s\n", (char *)it->elem);
4130 }
4131
4132 return fulllen;
4133 }
4134
4135 static int get_config_console_path(const char *key, char *retv, int inlen,
4136 struct lxc_conf *c, void *data)
4137 {
4138 return lxc_get_conf_str(retv, inlen, c->console.path);
4139 }
4140
4141 static int get_config_console_logfile(const char *key, char *retv, int inlen,
4142 struct lxc_conf *c, void *data)
4143 {
4144 return lxc_get_conf_str(retv, inlen, c->console.log_path);
4145 }
4146
4147 static int get_config_console_rotate(const char *key, char *retv, int inlen,
4148 struct lxc_conf *c, void *data)
4149 {
4150 return lxc_get_conf_int(c, retv, inlen, c->console.log_rotate);
4151 }
4152
4153
4154 static int get_config_console_buffer_size(const char *key, char *retv,
4155 int inlen, struct lxc_conf *c,
4156 void *data)
4157 {
4158 return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size);
4159 }
4160
4161 static int get_config_console_size(const char *key, char *retv, int inlen,
4162 struct lxc_conf *c, void *data)
4163 {
4164 return lxc_get_conf_uint64(c, retv, inlen, c->console.log_size);
4165 }
4166
4167 static int get_config_seccomp_allow_nesting(const char *key, char *retv,
4168 int inlen, struct lxc_conf *c,
4169 void *data)
4170 {
4171 #ifdef HAVE_SECCOMP
4172 return lxc_get_conf_int(c, retv, inlen, c->seccomp.allow_nesting);
4173 #else
4174 errno = ENOSYS;
4175 return -1;
4176 #endif
4177 }
4178
4179 static int get_config_seccomp_notify_cookie(const char *key, char *retv, int inlen,
4180 struct lxc_conf *c, void *data)
4181 {
4182 #ifdef HAVE_SECCOMP_NOTIFY
4183 return lxc_get_conf_str(retv, inlen, c->seccomp.notifier.cookie);
4184 #else
4185 return ret_set_errno(-1, ENOSYS);
4186 #endif
4187 }
4188
4189 static int get_config_seccomp_notify_proxy(const char *key, char *retv, int inlen,
4190 struct lxc_conf *c, void *data)
4191 {
4192 #ifdef HAVE_SECCOMP_NOTIFY
4193 return lxc_get_conf_str(retv, inlen,
4194 (c->seccomp.notifier.proxy_addr.sun_path[0]) == '/'
4195 ? &c->seccomp.notifier.proxy_addr.sun_path[0]
4196 : &c->seccomp.notifier.proxy_addr.sun_path[1]);
4197 #else
4198 return ret_set_errno(-1, ENOSYS);
4199 #endif
4200 }
4201
4202 static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
4203 struct lxc_conf *c, void *data)
4204 {
4205 return lxc_get_conf_str(retv, inlen, c->seccomp.seccomp);
4206 }
4207
4208 static int get_config_autodev(const char *key, char *retv, int inlen,
4209 struct lxc_conf *c, void *data)
4210 {
4211 return lxc_get_conf_int(c, retv, inlen, c->autodev);
4212 }
4213
4214 static int get_config_autodev_tmpfs_size(const char *key, char *retv, int inlen,
4215 struct lxc_conf *c, void *data)
4216 {
4217 return lxc_get_conf_int(c, retv, inlen, c->autodevtmpfssize);
4218 }
4219
4220 static int get_config_signal_halt(const char *key, char *retv, int inlen,
4221 struct lxc_conf *c, void *data)
4222 {
4223 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
4224 }
4225
4226 static int get_config_signal_reboot(const char *key, char *retv, int inlen,
4227 struct lxc_conf *c, void *data)
4228 {
4229 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
4230 }
4231
4232 static int get_config_signal_stop(const char *key, char *retv, int inlen,
4233 struct lxc_conf *c, void *data)
4234 {
4235 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
4236 }
4237
4238 static int get_config_start(const char *key, char *retv, int inlen,
4239 struct lxc_conf *c, void *data)
4240 {
4241 if (strcmp(key + 10, "auto") == 0)
4242 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
4243 else if (strcmp(key + 10, "delay") == 0)
4244 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
4245 else if (strcmp(key + 10, "order") == 0)
4246 return lxc_get_conf_int(c, retv, inlen, c->start_order);
4247
4248 return -1;
4249 }
4250
4251 static int get_config_log_syslog(const char *key, char *retv, int inlen,
4252 struct lxc_conf *c, void *data)
4253 {
4254 return lxc_get_conf_str(retv, inlen, c->syslog);
4255 }
4256
4257 static int get_config_monitor(const char *key, char *retv, int inlen,
4258 struct lxc_conf *c, void *data)
4259 {
4260 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
4261 }
4262
4263 static int get_config_monitor_signal_pdeath(const char *key, char *retv,
4264 int inlen, struct lxc_conf *c,
4265 void *data)
4266 {
4267 return lxc_get_conf_int(c, retv, inlen, c->monitor_signal_pdeath);
4268 }
4269
4270 static int get_config_group(const char *key, char *retv, int inlen,
4271 struct lxc_conf *c, void *data)
4272 {
4273 int len, fulllen = 0;
4274 struct lxc_list *it;
4275
4276 if (!retv)
4277 inlen = 0;
4278 else
4279 memset(retv, 0, inlen);
4280
4281 lxc_list_for_each(it, &c->groups) {
4282 strprint(retv, inlen, "%s\n", (char *)it->elem);
4283 }
4284
4285 return fulllen;
4286 }
4287
4288 static int get_config_environment(const char *key, char *retv, int inlen,
4289 struct lxc_conf *c, void *data)
4290 {
4291 int len, fulllen = 0;
4292 struct lxc_list *it;
4293
4294 if (!retv)
4295 inlen = 0;
4296 else
4297 memset(retv, 0, inlen);
4298
4299 lxc_list_for_each(it, &c->environment) {
4300 strprint(retv, inlen, "%s\n", (char *)it->elem);
4301 }
4302
4303 return fulllen;
4304 }
4305
4306 static int get_config_execute_cmd(const char *key, char *retv, int inlen,
4307 struct lxc_conf *c, void *data)
4308 {
4309 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
4310 }
4311
4312 static int get_config_init_cmd(const char *key, char *retv, int inlen,
4313 struct lxc_conf *c, void *data)
4314 {
4315 return lxc_get_conf_str(retv, inlen, c->init_cmd);
4316 }
4317
4318 static int get_config_init_cwd(const char *key, char *retv, int inlen,
4319 struct lxc_conf *c, void *data)
4320 {
4321 return lxc_get_conf_str(retv, inlen, c->init_cwd);
4322 }
4323
4324 static int get_config_init_uid(const char *key, char *retv, int inlen,
4325 struct lxc_conf *c, void *data)
4326 {
4327 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
4328 }
4329
4330 static int get_config_init_gid(const char *key, char *retv, int inlen,
4331 struct lxc_conf *c, void *data)
4332 {
4333 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
4334 }
4335
4336 static int get_config_ephemeral(const char *key, char *retv, int inlen,
4337 struct lxc_conf *c, void *data)
4338 {
4339 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
4340 }
4341
4342 static int get_config_no_new_privs(const char *key, char *retv, int inlen,
4343 struct lxc_conf *c, void *data)
4344 {
4345 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
4346 }
4347
4348 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
4349 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
4350 * printed, in 'lxc.prlimit.resource = value' format.
4351 */
4352 static int get_config_prlimit(const char *key, char *retv, int inlen,
4353 struct lxc_conf *c, void *data)
4354 {
4355 int fulllen = 0, len;
4356 bool get_all = false;
4357 struct lxc_list *it;
4358
4359 if (!retv)
4360 inlen = 0;
4361 else
4362 memset(retv, 0, inlen);
4363
4364 if (!strcmp(key, "lxc.prlimit"))
4365 get_all = true;
4366 else if (strncmp(key, "lxc.prlimit.", 12) == 0)
4367 key += 12;
4368 else
4369 return -1;
4370
4371 lxc_list_for_each(it, &c->limits) {
4372 /* 2 colon separated 64 bit integers or the word 'unlimited' */
4373 char buf[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
4374 int partlen;
4375 struct lxc_limit *lim = it->elem;
4376
4377 if (lim->limit.rlim_cur == RLIM_INFINITY) {
4378 memcpy(buf, "unlimited", STRLITERALLEN("unlimited") + 1);
4379 partlen = STRLITERALLEN("unlimited");
4380 } else {
4381 partlen = sprintf(buf, "%" PRIu64,
4382 (uint64_t)lim->limit.rlim_cur);
4383 }
4384
4385 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
4386 if (lim->limit.rlim_max == RLIM_INFINITY)
4387 memcpy(buf + partlen, ":unlimited",
4388 STRLITERALLEN(":unlimited") + 1);
4389 else
4390 sprintf(buf + partlen, ":%" PRIu64,
4391 (uint64_t)lim->limit.rlim_max);
4392 }
4393
4394 if (get_all) {
4395 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
4396 lim->resource, buf);
4397 } else if (strcmp(lim->resource, key) == 0) {
4398 strprint(retv, inlen, "%s", buf);
4399 }
4400 }
4401
4402 return fulllen;
4403 }
4404
4405 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
4406 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
4407 * entries will be printed, in 'lxc.sysctl.key = value' format.
4408 */
4409 static int get_config_sysctl(const char *key, char *retv, int inlen,
4410 struct lxc_conf *c, void *data)
4411 {
4412 int len;
4413 struct lxc_list *it;
4414 int fulllen = 0;
4415 bool get_all = false;
4416
4417 if (!retv)
4418 inlen = 0;
4419 else
4420 memset(retv, 0, inlen);
4421
4422 if (strcmp(key, "lxc.sysctl") == 0)
4423 get_all = true;
4424 else if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
4425 key += STRLITERALLEN("lxc.sysctl.");
4426 else
4427 return -1;
4428
4429 lxc_list_for_each(it, &c->sysctls) {
4430 struct lxc_sysctl *elem = it->elem;
4431 if (get_all) {
4432 strprint(retv, inlen, "lxc.sysctl.%s = %s\n", elem->key,
4433 elem->value);
4434 } else if (strcmp(elem->key, key) == 0) {
4435 strprint(retv, inlen, "%s", elem->value);
4436 }
4437 }
4438
4439 return fulllen;
4440 }
4441
4442 static int get_config_proc(const char *key, char *retv, int inlen,
4443 struct lxc_conf *c, void *data)
4444 {
4445 struct lxc_list *it;
4446 int len;
4447 int fulllen = 0;
4448 bool get_all = false;
4449
4450 if (!retv)
4451 inlen = 0;
4452 else
4453 memset(retv, 0, inlen);
4454
4455 if (strcmp(key, "lxc.proc") == 0)
4456 get_all = true;
4457 else if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
4458 key += STRLITERALLEN("lxc.proc.");
4459 else
4460 return -1;
4461
4462 lxc_list_for_each(it, &c->procs) {
4463 struct lxc_proc *proc = it->elem;
4464
4465 if (get_all) {
4466 strprint(retv, inlen, "lxc.proc.%s = %s\n",
4467 proc->filename, proc->value);
4468 } else if (strcmp(proc->filename, key) == 0) {
4469 strprint(retv, inlen, "%s", proc->value);
4470 }
4471 }
4472
4473 return fulllen;
4474 }
4475
4476 static int get_config_namespace_clone(const char *key, char *retv, int inlen,
4477 struct lxc_conf *c, void *data)
4478 {
4479 int i, len;
4480 int fulllen = 0;
4481
4482 if (!retv)
4483 inlen = 0;
4484 else
4485 memset(retv, 0, inlen);
4486
4487 for (i = 0; i < LXC_NS_MAX; i++) {
4488 if (c->ns_clone & ns_info[i].clone_flag)
4489 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4490 }
4491
4492 return fulllen;
4493 }
4494
4495 static int get_config_namespace_keep(const char *key, char *retv, int inlen,
4496 struct lxc_conf *c, void *data)
4497 {
4498 int i, len;
4499 int fulllen = 0;
4500
4501 if (!retv)
4502 inlen = 0;
4503 else
4504 memset(retv, 0, inlen);
4505
4506 for (i = 0; i < LXC_NS_MAX; i++) {
4507 if (c->ns_keep & ns_info[i].clone_flag)
4508 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
4509 }
4510
4511 return fulllen;
4512 }
4513
4514 static int get_config_time_offset_boot(const char *key, char *retv, int inlen, struct lxc_conf *c,
4515 void *data)
4516 {
4517 int len;
4518 int fulllen = 0;
4519
4520 if (!retv)
4521 inlen = 0;
4522 else
4523 memset(retv, 0, inlen);
4524
4525 if (c->timens.s_boot) {
4526 strprint(retv, inlen, "%" PRId64 " s\n", c->timens.s_boot);
4527 } else {
4528 strprint(retv, inlen, "%" PRId64 " ns\n", c->timens.ns_boot);
4529 }
4530
4531 return fulllen;
4532 }
4533
4534 static int get_config_time_offset_monotonic(const char *key, char *retv, int inlen,
4535 struct lxc_conf *c, void *data)
4536 {
4537 int len;
4538 int fulllen = 0;
4539
4540 if (!retv)
4541 inlen = 0;
4542 else
4543 memset(retv, 0, inlen);
4544
4545 if (c->timens.s_monotonic) {
4546 strprint(retv, inlen, "%" PRId64 "s\n", c->timens.s_monotonic);
4547 } else {
4548 strprint(retv, inlen, "%" PRId64 "ns\n", c->timens.ns_monotonic);
4549 }
4550
4551 return fulllen;
4552 }
4553
4554 static int get_config_namespace_share(const char *key, char *retv, int inlen,
4555 struct lxc_conf *c, void *data)
4556 {
4557 int len, ns_idx;
4558 const char *namespace;
4559 int fulllen = 0;
4560
4561 if (!retv)
4562 inlen = 0;
4563 else
4564 memset(retv, 0, inlen);
4565
4566 namespace = key + STRLITERALLEN("lxc.namespace.share.");
4567 ns_idx = lxc_namespace_2_ns_idx(namespace);
4568 if (ns_idx < 0)
4569 return ns_idx;
4570
4571 strprint(retv, inlen, "%s", c->ns_share[ns_idx]);
4572
4573 return fulllen;
4574 }
4575
4576 /* Callbacks to clear config items. */
4577 static inline int clr_config_personality(const char *key, struct lxc_conf *c,
4578 void *data)
4579 {
4580 c->personality = -1;
4581 return 0;
4582 }
4583
4584 static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
4585 void *data)
4586 {
4587 c->pty_max = 0;
4588 return 0;
4589 }
4590
4591 static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
4592 void *data)
4593 {
4594 c->ttys.tty = 0;
4595 return 0;
4596 }
4597
4598 static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
4599 void *data)
4600 {
4601 free(c->ttys.dir);
4602 c->ttys.dir = NULL;
4603 return 0;
4604 }
4605
4606 static inline int clr_config_apparmor_profile(const char *key,
4607 struct lxc_conf *c, void *data)
4608 {
4609 free(c->lsm_aa_profile);
4610 c->lsm_aa_profile = NULL;
4611 return 0;
4612 }
4613
4614 static inline int clr_config_apparmor_allow_incomplete(const char *key,
4615 struct lxc_conf *c,
4616 void *data)
4617 {
4618 c->lsm_aa_allow_incomplete = 0;
4619 return 0;
4620 }
4621
4622 static inline int clr_config_apparmor_allow_nesting(const char *key,
4623 struct lxc_conf *c,
4624 void *data)
4625 {
4626 c->lsm_aa_allow_nesting = 0;
4627 return 0;
4628 }
4629
4630 static inline int clr_config_apparmor_raw(const char *key,
4631 struct lxc_conf *c,
4632 void *data)
4633 {
4634 return lxc_clear_apparmor_raw(c);
4635 }
4636
4637 static inline int clr_config_selinux_context(const char *key,
4638 struct lxc_conf *c, void *data)
4639 {
4640 free(c->lsm_se_context);
4641 c->lsm_se_context = NULL;
4642 return 0;
4643 }
4644
4645 static inline int clr_config_selinux_context_keyring(const char *key,
4646 struct lxc_conf *c, void *data)
4647 {
4648 free(c->lsm_se_keyring_context);
4649 c->lsm_se_keyring_context = NULL;
4650 return 0;
4651 }
4652
4653 static inline int clr_config_keyring_session(const char *key,
4654 struct lxc_conf *c, void *data)
4655 {
4656 c->keyring_disable_session = false;
4657 return 0;
4658 }
4659
4660 static inline int clr_config_cgroup_controller(const char *key,
4661 struct lxc_conf *c, void *data)
4662 {
4663 return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC);
4664 }
4665
4666 static inline int clr_config_cgroup2_controller(const char *key,
4667 struct lxc_conf *c, void *data)
4668 {
4669 return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC);
4670 }
4671
4672 static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
4673 void *data)
4674 {
4675 if (lxc_conf->cgroup_meta.dir) {
4676 free(lxc_conf->cgroup_meta.dir);
4677 lxc_conf->cgroup_meta.dir = NULL;
4678 }
4679
4680 return 0;
4681 }
4682
4683 static int clr_config_cgroup_monitor_dir(const char *key,
4684 struct lxc_conf *lxc_conf,
4685 void *data)
4686 {
4687 free_disarm(lxc_conf->cgroup_meta.monitor_dir);
4688 return 0;
4689 }
4690
4691 static int clr_config_cgroup_monitor_pivot_dir(const char *key,
4692 struct lxc_conf *lxc_conf,
4693 void *data)
4694 {
4695 free_disarm(lxc_conf->cgroup_meta.monitor_pivot_dir);
4696 return 0;
4697 }
4698
4699 static int clr_config_cgroup_container_dir(const char *key,
4700 struct lxc_conf *lxc_conf,
4701 void *data)
4702 {
4703 free_disarm(lxc_conf->cgroup_meta.container_dir);
4704 return 0;
4705 }
4706
4707 static int clr_config_cgroup_container_inner_dir(const char *key,
4708 struct lxc_conf *lxc_conf,
4709 void *data)
4710 {
4711 free_disarm(lxc_conf->cgroup_meta.namespace_dir);
4712 return 0;
4713 }
4714
4715 static inline int clr_config_cgroup_relative(const char *key,
4716 struct lxc_conf *lxc_conf,
4717 void *data)
4718 {
4719 lxc_conf->cgroup_meta.relative = false;
4720 return 0;
4721 }
4722
4723 static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
4724 void *data)
4725 {
4726 return lxc_clear_idmaps(c);
4727 }
4728
4729 static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
4730 void *data)
4731 {
4732 c->loglevel = LXC_LOG_LEVEL_NOTSET;
4733 return 0;
4734 }
4735
4736 static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
4737 void *data)
4738 {
4739 free(c->logfile);
4740 c->logfile = NULL;
4741 return 0;
4742 }
4743
4744 static inline int clr_config_mount(const char *key, struct lxc_conf *c,
4745 void *data)
4746 {
4747 return lxc_clear_mount_entries(c);
4748 }
4749
4750 static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
4751 void *data)
4752 {
4753 return lxc_clear_automounts(c);
4754 }
4755
4756 static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
4757 void *data)
4758 {
4759 free(c->fstab);
4760 c->fstab = NULL;
4761 return 0;
4762 }
4763
4764 static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
4765 void *data)
4766 {
4767 free(c->rootfs.path);
4768 c->rootfs.path = NULL;
4769 return 0;
4770 }
4771
4772 static inline int clr_config_rootfs_managed(const char *key, struct lxc_conf *c,
4773 void *data)
4774 {
4775 c->rootfs.managed = true;
4776 return 0;
4777 }
4778
4779 static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
4780 void *data)
4781 {
4782 free(c->rootfs.mount);
4783 c->rootfs.mount = NULL;
4784 return 0;
4785 }
4786
4787 static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
4788 void *data)
4789 {
4790 free(c->rootfs.options);
4791 c->rootfs.options = NULL;
4792
4793 free(c->rootfs.data);
4794 c->rootfs.data = NULL;
4795
4796 return 0;
4797 }
4798
4799 static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
4800 void *data)
4801 {
4802 free(c->utsname);
4803 c->utsname = NULL;
4804 return 0;
4805 }
4806
4807 static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
4808 void *data)
4809 {
4810 return lxc_clear_hooks(c, key);
4811 }
4812
4813 static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c,
4814 void *data)
4815 {
4816 /* default to legacy hooks version */
4817 c->hooks_version = 0;
4818 return 0;
4819 }
4820
4821 static inline int clr_config_net(const char *key, struct lxc_conf *c,
4822 void *data)
4823 {
4824 lxc_free_networks(&c->network);
4825
4826 return 0;
4827 }
4828
4829 static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
4830 void *data)
4831 {
4832 return lxc_clear_config_caps(c);
4833 }
4834
4835 static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
4836 void *data)
4837 {
4838 return lxc_clear_config_keepcaps(c);
4839 }
4840
4841 static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
4842 void *data)
4843 {
4844 free(c->console.path);
4845 c->console.path = NULL;
4846 return 0;
4847 }
4848
4849 static inline int clr_config_console_logfile(const char *key,
4850 struct lxc_conf *c, void *data)
4851 {
4852 free(c->console.log_path);
4853 c->console.log_path = NULL;
4854 return 0;
4855 }
4856
4857 static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c,
4858 void *data)
4859 {
4860 c->console.log_rotate = 0;
4861 return 0;
4862 }
4863
4864 static inline int clr_config_console_buffer_size(const char *key,
4865 struct lxc_conf *c, void *data)
4866 {
4867 c->console.buffer_size = 0;
4868 return 0;
4869 }
4870
4871 static inline int clr_config_console_size(const char *key, struct lxc_conf *c,
4872 void *data)
4873 {
4874 c->console.log_size = 0;
4875 return 0;
4876 }
4877
4878 static inline int clr_config_seccomp_allow_nesting(const char *key,
4879 struct lxc_conf *c, void *data)
4880 {
4881 #ifdef HAVE_SECCOMP
4882 c->seccomp.allow_nesting = 0;
4883 return 0;
4884 #else
4885 errno = ENOSYS;
4886 return -1;
4887 #endif
4888 }
4889
4890 static inline int clr_config_seccomp_notify_cookie(const char *key,
4891 struct lxc_conf *c, void *data)
4892 {
4893 #ifdef HAVE_SECCOMP_NOTIFY
4894 free(c->seccomp.notifier.cookie);
4895 c->seccomp.notifier.cookie = NULL;
4896 return 0;
4897 #else
4898 return ret_set_errno(-1, ENOSYS);
4899 #endif
4900 }
4901
4902 static inline int clr_config_seccomp_notify_proxy(const char *key,
4903 struct lxc_conf *c, void *data)
4904 {
4905 #ifdef HAVE_SECCOMP_NOTIFY
4906 memset(&c->seccomp.notifier.proxy_addr, 0,
4907 sizeof(c->seccomp.notifier.proxy_addr));
4908 return 0;
4909 #else
4910 return ret_set_errno(-1, ENOSYS);
4911 #endif
4912 }
4913
4914 static inline int clr_config_seccomp_profile(const char *key,
4915 struct lxc_conf *c, void *data)
4916 {
4917 free(c->seccomp.seccomp);
4918 c->seccomp.seccomp = NULL;
4919 return 0;
4920 }
4921
4922 static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
4923 void *data)
4924 {
4925 c->autodev = 1;
4926 return 0;
4927 }
4928
4929 static inline int clr_config_autodev_tmpfs_size(const char *key, struct lxc_conf *c,
4930 void *data)
4931 {
4932 c->autodevtmpfssize = 500000;
4933 return 0;
4934 }
4935
4936 static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
4937 void *data)
4938 {
4939 c->haltsignal = 0;
4940 return 0;
4941 }
4942
4943 static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
4944 void *data)
4945 {
4946 c->rebootsignal = 0;
4947 return 0;
4948 }
4949
4950 static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
4951 void *data)
4952 {
4953 c->stopsignal = 0;
4954 return 0;
4955 }
4956
4957 static inline int clr_config_start(const char *key, struct lxc_conf *c,
4958 void *data)
4959 {
4960 if (strcmp(key + 10, "auto") == 0)
4961 c->start_auto = 0;
4962 else if (strcmp(key + 10, "delay") == 0)
4963 c->start_delay = 0;
4964 else if (strcmp(key + 10, "order") == 0)
4965 c->start_order = 0;
4966
4967 return 0;
4968 }
4969
4970 static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
4971 void *data)
4972 {
4973 free(c->syslog);
4974 c->syslog = NULL;
4975 return 0;
4976 }
4977
4978 static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
4979 void *data)
4980 {
4981 c->monitor_unshare = 0;
4982 return 0;
4983 }
4984
4985 static inline int clr_config_monitor_signal_pdeath(const char *key,
4986 struct lxc_conf *c, void *data)
4987 {
4988 c->monitor_signal_pdeath = 0;
4989 return 0;
4990 }
4991
4992 static inline int clr_config_group(const char *key, struct lxc_conf *c,
4993 void *data)
4994 {
4995 return lxc_clear_groups(c);
4996 }
4997
4998 static inline int clr_config_environment(const char *key, struct lxc_conf *c,
4999 void *data)
5000 {
5001 return lxc_clear_environment(c);
5002 }
5003
5004 static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
5005 void *data)
5006 {
5007 free(c->execute_cmd);
5008 c->execute_cmd = NULL;
5009 return 0;
5010 }
5011
5012 static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
5013 void *data)
5014 {
5015 free(c->init_cmd);
5016 c->init_cmd = NULL;
5017 return 0;
5018 }
5019
5020 static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
5021 void *data)
5022 {
5023 free(c->init_cwd);
5024 c->init_cwd = NULL;
5025 return 0;
5026 }
5027
5028 static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
5029 void *data)
5030 {
5031 c->init_uid = 0;
5032 return 0;
5033 }
5034
5035 static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
5036 void *data)
5037 {
5038 c->init_gid = 0;
5039 return 0;
5040 }
5041
5042 static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
5043 void *data)
5044 {
5045 c->ephemeral = 0;
5046 return 0;
5047 }
5048
5049 static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
5050 void *data)
5051 {
5052 c->no_new_privs = false;
5053 return 0;
5054 }
5055
5056 static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
5057 void *data)
5058 {
5059 return lxc_clear_limits(c, key);
5060 }
5061
5062 static inline int clr_config_sysctl(const char *key, struct lxc_conf *c,
5063 void *data)
5064 {
5065 return lxc_clear_sysctls(c, key);
5066 }
5067
5068 static inline int clr_config_proc(const char *key, struct lxc_conf *c,
5069 void *data)
5070 {
5071 return lxc_clear_procs(c, key);
5072 }
5073
5074 static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
5075 void *data)
5076 {
5077 lxc_clear_includes(c);
5078 return 0;
5079 }
5080
5081 static int clr_config_namespace_clone(const char *key,
5082 struct lxc_conf *lxc_conf, void *data)
5083 {
5084 lxc_conf->ns_clone = 0;
5085 return 0;
5086 }
5087
5088 static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf,
5089 void *data)
5090 {
5091 lxc_conf->ns_keep = 0;
5092 return 0;
5093 }
5094
5095 static int clr_config_time_offset_boot(const char *key, struct lxc_conf *lxc_conf, void *data)
5096 {
5097 lxc_conf->timens.s_boot = 0;
5098 lxc_conf->timens.ns_boot = 0;
5099 return 0;
5100 }
5101
5102 static int clr_config_time_offset_monotonic(const char *key, struct lxc_conf *lxc_conf, void *data)
5103 {
5104 lxc_conf->timens.s_monotonic = 0;
5105 lxc_conf->timens.ns_monotonic = 0;
5106 return 0;
5107 }
5108
5109 static int clr_config_namespace_share(const char *key,
5110 struct lxc_conf *lxc_conf, void *data)
5111 {
5112 int ns_idx;
5113 const char *namespace;
5114
5115 namespace = key + STRLITERALLEN("lxc.namespace.share.");
5116 ns_idx = lxc_namespace_2_ns_idx(namespace);
5117 if (ns_idx < 0)
5118 return ns_idx;
5119
5120 free(lxc_conf->ns_share[ns_idx]);
5121 lxc_conf->ns_share[ns_idx] = NULL;
5122
5123 return 0;
5124 }
5125
5126 static int get_config_includefiles(const char *key, char *retv, int inlen,
5127 struct lxc_conf *c, void *data)
5128 {
5129 return -ENOSYS;
5130 }
5131
5132 static struct lxc_config_t *get_network_config_ops(const char *key,
5133 struct lxc_conf *lxc_conf,
5134 ssize_t *idx,
5135 char **deindexed_key)
5136 {
5137 int ret;
5138 unsigned int tmpidx;
5139 size_t numstrlen;
5140 char *copy, *idx_start, *idx_end;
5141 struct lxc_config_t *config = NULL;
5142
5143 /* check that this is a sensible network key */
5144 if (strncmp("lxc.net.", key, 8)) {
5145 ERROR("Invalid network configuration key \"%s\"", key);
5146 return NULL;
5147 }
5148
5149 copy = strdup(key);
5150 if (!copy) {
5151 ERROR("Failed to duplicate string \"%s\"", key);
5152 return NULL;
5153 }
5154
5155 /* lxc.net.<n> */
5156 if (!isdigit(*(key + 8))) {
5157 ERROR("Failed to detect digit in string \"%s\"", key + 8);
5158 goto on_error;
5159 }
5160
5161 /* beginning of index string */
5162 idx_start = (copy + 7);
5163 *idx_start = '\0';
5164
5165 /* end of index string */
5166 idx_end = strchr((copy + 8), '.');
5167 if (idx_end)
5168 *idx_end = '\0';
5169
5170 /* parse current index */
5171 ret = lxc_safe_uint((idx_start + 1), &tmpidx);
5172 if (ret < 0) {
5173 errno = -ret;
5174 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
5175 idx_start + 1);
5176 *idx = ret;
5177 goto on_error;
5178 }
5179
5180 /* This, of course is utterly nonsensical on so many levels, but
5181 * better safe than sorry.
5182 * (Checking for INT_MAX here is intentional.)
5183 */
5184 if (tmpidx == INT_MAX) {
5185 SYSERROR("Number of configured networks would overflow the "
5186 "counter");
5187 goto on_error;
5188 }
5189 *idx = tmpidx;
5190
5191 numstrlen = strlen((idx_start + 1));
5192
5193 /* repair configuration key */
5194 *idx_start = '.';
5195
5196 /* lxc.net.<idx>.<subkey> */
5197 if (idx_end) {
5198 *idx_end = '.';
5199 if (strlen(idx_end + 1) == 0) {
5200 ERROR("No subkey in network configuration key \"%s\"", key);
5201 goto on_error;
5202 }
5203
5204 memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
5205 copy[strlen(key) - (numstrlen + 1)] = '\0';
5206
5207 config = lxc_get_config(copy);
5208 if (!config) {
5209 ERROR("Unknown network configuration key \"%s\"", key);
5210 goto on_error;
5211 }
5212 }
5213
5214 if (deindexed_key)
5215 *deindexed_key = copy;
5216
5217 return config;
5218
5219 on_error:
5220 free(copy);
5221 return NULL;
5222 }
5223
5224 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
5225 * found. So we make sure next comes an integer, find the right callback (by
5226 * rewriting the key), and call it.
5227 */
5228 static int set_config_net_nic(const char *key, const char *value,
5229 struct lxc_conf *lxc_conf, void *data)
5230 {
5231 int ret;
5232 const char *idxstring;
5233 struct lxc_config_t *config;
5234 struct lxc_netdev *netdev;
5235 ssize_t idx = -1;
5236 char *deindexed_key = NULL;
5237
5238 idxstring = key + 8;
5239 if (!isdigit(*idxstring))
5240 return -1;
5241
5242 if (lxc_config_value_empty(value))
5243 return clr_config_net_nic(key, lxc_conf, data);
5244
5245 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
5246 if (!config || idx < 0)
5247 return -1;
5248
5249 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
5250 if (!netdev) {
5251 free(deindexed_key);
5252 return -1;
5253 }
5254
5255 ret = config->set(deindexed_key, value, lxc_conf, netdev);
5256 free(deindexed_key);
5257
5258 return ret;
5259 }
5260
5261 static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
5262 void *data)
5263 {
5264 int ret;
5265 const char *idxstring;
5266 struct lxc_config_t *config;
5267 struct lxc_netdev *netdev;
5268 ssize_t idx = -1;
5269 char *deindexed_key = NULL;
5270
5271 idxstring = key + 8;
5272 if (!isdigit(*idxstring))
5273 return -1;
5274
5275 /* The left conjunct is pretty self-explanatory. The right conjunct
5276 * checks whether the two pointers are equal. If they are we know that
5277 * this is not a key that is namespaced any further and so we are
5278 * supposed to clear the whole network.
5279 */
5280 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
5281 unsigned int rmnetdevidx;
5282
5283 if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0)
5284 return -1;
5285
5286 /* Remove network from network list. */
5287 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
5288 return 0;
5289 }
5290
5291 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
5292 if (!config || idx < 0)
5293 return -1;
5294
5295 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false);
5296 if (!netdev) {
5297 free(deindexed_key);
5298 return -1;
5299 }
5300
5301 ret = config->clr(deindexed_key, lxc_conf, netdev);
5302 free(deindexed_key);
5303
5304 return ret;
5305 }
5306
5307 static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
5308 void *data)
5309 {
5310 struct lxc_netdev *netdev = data;
5311
5312 if (!netdev)
5313 return -1;
5314
5315 netdev->type = -1;
5316
5317 return 0;
5318 }
5319
5320 static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
5321 void *data)
5322 {
5323 struct lxc_netdev *netdev = data;
5324
5325 if (!netdev)
5326 return -1;
5327
5328 netdev->name[0] = '\0';
5329
5330 return 0;
5331 }
5332
5333 static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
5334 void *data)
5335 {
5336 struct lxc_netdev *netdev = data;
5337
5338 if (!netdev)
5339 return -1;
5340
5341 netdev->flags = 0;
5342
5343 return 0;
5344 }
5345
5346 static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
5347 void *data)
5348 {
5349 struct lxc_netdev *netdev = data;
5350
5351 if (!netdev)
5352 return -1;
5353
5354 netdev->link[0] = '\0';
5355
5356 return 0;
5357 }
5358
5359 static int clr_config_net_l2proxy(const char *key, struct lxc_conf *lxc_conf,
5360 void *data)
5361 {
5362 struct lxc_netdev *netdev = data;
5363
5364 if (!netdev)
5365 return ret_set_errno(-1, EINVAL);
5366
5367 netdev->l2proxy = false;
5368
5369 return 0;
5370 }
5371
5372 static int clr_config_net_macvlan_mode(const char *key,
5373 struct lxc_conf *lxc_conf, void *data)
5374 {
5375 struct lxc_netdev *netdev = data;
5376
5377 if (!netdev)
5378 return -1;
5379
5380 if (netdev->type != LXC_NET_MACVLAN)
5381 return 0;
5382
5383 netdev->priv.macvlan_attr.mode = -1;
5384
5385 return 0;
5386 }
5387
5388 static int clr_config_net_ipvlan_mode(const char *key,
5389 struct lxc_conf *lxc_conf, void *data)
5390 {
5391 struct lxc_netdev *netdev = data;
5392
5393 if (!netdev)
5394 return ret_set_errno(-1, EINVAL);
5395
5396 if (netdev->type != LXC_NET_IPVLAN)
5397 return 0;
5398
5399 netdev->priv.ipvlan_attr.mode = -1;
5400
5401 return 0;
5402 }
5403
5404 static int clr_config_net_ipvlan_isolation(const char *key,
5405 struct lxc_conf *lxc_conf, void *data)
5406 {
5407 struct lxc_netdev *netdev = data;
5408
5409 if (!netdev)
5410 return ret_set_errno(-1, EINVAL);
5411
5412 if (netdev->type != LXC_NET_IPVLAN)
5413 return 0;
5414
5415 netdev->priv.ipvlan_attr.isolation = -1;
5416
5417 return 0;
5418 }
5419
5420 static int clr_config_net_veth_mode(const char *key,
5421 struct lxc_conf *lxc_conf, void *data)
5422 {
5423 struct lxc_netdev *netdev = data;
5424
5425 if (!netdev)
5426 return ret_set_errno(-1, EINVAL);
5427
5428 if (netdev->type != LXC_NET_VETH)
5429 return 0;
5430
5431 netdev->priv.veth_attr.mode = -1;
5432
5433 return 0;
5434 }
5435
5436 static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
5437 void *data)
5438 {
5439 struct lxc_netdev *netdev = data;
5440
5441 if (!netdev)
5442 return -1;
5443
5444 netdev->priv.veth_attr.pair[0] = '\0';
5445
5446 return 0;
5447 }
5448
5449 static int clr_config_net_veth_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5450 void *data)
5451 {
5452 struct lxc_netdev *netdev = data;
5453
5454 if (!netdev)
5455 return ret_errno(EINVAL);
5456
5457 netdev->priv.veth_attr.vlan_id = 0;
5458 netdev->priv.veth_attr.vlan_id_set = false;
5459
5460 return 0;
5461 }
5462
5463 static int clr_config_net_veth_vlan_tagged_id(const char *key,
5464 struct lxc_conf *lxc_conf, void *data)
5465 {
5466 struct lxc_netdev *netdev = data;
5467 struct lxc_list *cur, *next;
5468
5469 if (!netdev)
5470 return ret_errno(EINVAL);
5471
5472 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.vlan_tagged_ids, next) {
5473 lxc_list_del(cur);
5474 free(cur);
5475 }
5476
5477 return 0;
5478 }
5479
5480
5481 static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
5482 void *data)
5483 {
5484 struct lxc_netdev *netdev = data;
5485
5486 if (!netdev)
5487 return -1;
5488
5489 free(netdev->upscript);
5490 netdev->upscript = NULL;
5491
5492 return 0;
5493 }
5494
5495 static int clr_config_net_script_down(const char *key,
5496 struct lxc_conf *lxc_conf, void *data)
5497 {
5498 struct lxc_netdev *netdev = data;
5499
5500 if (!netdev)
5501 return -1;
5502
5503 free(netdev->downscript);
5504 netdev->downscript = NULL;
5505
5506 return 0;
5507 }
5508
5509 static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
5510 void *data)
5511 {
5512 struct lxc_netdev *netdev = data;
5513
5514 if (!netdev)
5515 return -1;
5516
5517 free(netdev->hwaddr);
5518 netdev->hwaddr = NULL;
5519
5520 return 0;
5521 }
5522
5523 static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
5524 void *data)
5525 {
5526 struct lxc_netdev *netdev = data;
5527
5528 if (!netdev)
5529 return -1;
5530
5531 free(netdev->mtu);
5532 netdev->mtu = NULL;
5533
5534 return 0;
5535 }
5536
5537 static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
5538 void *data)
5539 {
5540 struct lxc_netdev *netdev = data;
5541
5542 if (!netdev)
5543 return -1;
5544
5545 netdev->priv.vlan_attr.vid = 0;
5546
5547 return 0;
5548 }
5549
5550 static int clr_config_net_ipv4_gateway(const char *key,
5551 struct lxc_conf *lxc_conf, void *data)
5552 {
5553 struct lxc_netdev *netdev = data;
5554
5555 if (!netdev)
5556 return -1;
5557
5558 free(netdev->ipv4_gateway);
5559 netdev->ipv4_gateway = NULL;
5560
5561 return 0;
5562 }
5563
5564 static int clr_config_net_ipv4_address(const char *key,
5565 struct lxc_conf *lxc_conf, void *data)
5566 {
5567 struct lxc_netdev *netdev = data;
5568 struct lxc_list *cur, *next;
5569
5570 if (!netdev)
5571 return -1;
5572
5573 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
5574 lxc_list_del(cur);
5575 free(cur->elem);
5576 free(cur);
5577 }
5578
5579 return 0;
5580 }
5581
5582 static int clr_config_net_veth_ipv4_route(const char *key,
5583 struct lxc_conf *lxc_conf, void *data)
5584 {
5585 struct lxc_netdev *netdev = data;
5586 struct lxc_list *cur, *next;
5587
5588 if (!netdev)
5589 return -1;
5590
5591 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) {
5592 lxc_list_del(cur);
5593 free(cur->elem);
5594 free(cur);
5595 }
5596
5597 return 0;
5598 }
5599
5600 static int clr_config_net_ipv6_gateway(const char *key,
5601 struct lxc_conf *lxc_conf, void *data)
5602 {
5603 struct lxc_netdev *netdev = data;
5604
5605 if (!netdev)
5606 return -1;
5607
5608 free(netdev->ipv6_gateway);
5609 netdev->ipv6_gateway = NULL;
5610
5611 return 0;
5612 }
5613
5614 static int clr_config_net_ipv6_address(const char *key,
5615 struct lxc_conf *lxc_conf, void *data)
5616 {
5617 struct lxc_netdev *netdev = data;
5618 struct lxc_list *cur, *next;
5619
5620 if (!netdev)
5621 return -1;
5622
5623 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
5624 lxc_list_del(cur);
5625 free(cur->elem);
5626 free(cur);
5627 }
5628
5629 return 0;
5630 }
5631
5632 static int clr_config_net_veth_ipv6_route(const char *key,
5633 struct lxc_conf *lxc_conf, void *data)
5634 {
5635 struct lxc_netdev *netdev = data;
5636 struct lxc_list *cur, *next;
5637
5638 if (!netdev)
5639 return -1;
5640
5641 lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) {
5642 lxc_list_del(cur);
5643 free(cur->elem);
5644 free(cur);
5645 }
5646
5647 return 0;
5648 }
5649
5650 static int get_config_net_nic(const char *key, char *retv, int inlen,
5651 struct lxc_conf *c, void *data)
5652 {
5653 int ret;
5654 const char *idxstring;
5655 struct lxc_config_t *config;
5656 struct lxc_netdev *netdev;
5657 ssize_t idx = -1;
5658 char *deindexed_key = NULL;
5659
5660 idxstring = key + 8;
5661 if (!isdigit(*idxstring))
5662 return -1;
5663
5664 config = get_network_config_ops(key, c, &idx, &deindexed_key);
5665 if (!config || idx < 0)
5666 return -1;
5667
5668 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
5669 if (!netdev) {
5670 free(deindexed_key);
5671 return -1;
5672 }
5673
5674 ret = config->get(deindexed_key, retv, inlen, c, netdev);
5675 free(deindexed_key);
5676
5677 return ret;
5678 }
5679
5680 static int get_config_net_type(const char *key, char *retv, int inlen,
5681 struct lxc_conf *c, void *data)
5682 {
5683 int len;
5684 int fulllen = 0;
5685 struct lxc_netdev *netdev = data;
5686
5687 if (!retv)
5688 inlen = 0;
5689 else
5690 memset(retv, 0, inlen);
5691
5692 if (!netdev)
5693 return -1;
5694
5695 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
5696
5697 return fulllen;
5698 }
5699
5700 static int get_config_net_flags(const char *key, char *retv, int inlen,
5701 struct lxc_conf *c, void *data)
5702 {
5703 int len;
5704 int fulllen = 0;
5705 struct lxc_netdev *netdev = data;
5706
5707 if (!retv)
5708 inlen = 0;
5709 else
5710 memset(retv, 0, inlen);
5711
5712 if (!netdev)
5713 return -1;
5714
5715 if (netdev->flags & IFF_UP)
5716 strprint(retv, inlen, "up");
5717
5718 return fulllen;
5719 }
5720
5721 static int get_config_net_link(const char *key, char *retv, int inlen,
5722 struct lxc_conf *c, void *data)
5723 {
5724 int len;
5725 int fulllen = 0;
5726 struct lxc_netdev *netdev = data;
5727
5728 if (!retv)
5729 inlen = 0;
5730 else
5731 memset(retv, 0, inlen);
5732
5733 if (!netdev)
5734 return -1;
5735
5736 if (netdev->link[0] != '\0')
5737 strprint(retv, inlen, "%s", netdev->link);
5738
5739 return fulllen;
5740 }
5741
5742 static int get_config_net_l2proxy(const char *key, char *retv, int inlen,
5743 struct lxc_conf *c, void *data)
5744 {
5745 struct lxc_netdev *netdev = data;
5746 return lxc_get_conf_bool(c, retv, inlen, netdev->l2proxy);
5747 }
5748
5749 static int get_config_net_name(const char *key, char *retv, int inlen,
5750 struct lxc_conf *c, void *data)
5751 {
5752 int len;
5753 int fulllen = 0;
5754 struct lxc_netdev *netdev = data;
5755
5756 if (!retv)
5757 inlen = 0;
5758 else
5759 memset(retv, 0, inlen);
5760
5761 if (!netdev)
5762 return -1;
5763
5764 if (netdev->name[0] != '\0')
5765 strprint(retv, inlen, "%s", netdev->name);
5766
5767 return fulllen;
5768 }
5769
5770 static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
5771 struct lxc_conf *c, void *data)
5772 {
5773 int len;
5774 int fulllen = 0;
5775 const char *mode;
5776 struct lxc_netdev *netdev = data;
5777
5778 if (!retv)
5779 inlen = 0;
5780 else
5781 memset(retv, 0, inlen);
5782
5783 if (!netdev)
5784 return -1;
5785
5786 if (netdev->type != LXC_NET_MACVLAN)
5787 return 0;
5788
5789 switch (netdev->priv.macvlan_attr.mode) {
5790 case MACVLAN_MODE_PRIVATE:
5791 mode = "private";
5792 break;
5793 case MACVLAN_MODE_VEPA:
5794 mode = "vepa";
5795 break;
5796 case MACVLAN_MODE_BRIDGE:
5797 mode = "bridge";
5798 break;
5799 case MACVLAN_MODE_PASSTHRU:
5800 mode = "passthru";
5801 break;
5802 default:
5803 mode = "(invalid)";
5804 break;
5805 }
5806
5807 strprint(retv, inlen, "%s", mode);
5808
5809 return fulllen;
5810 }
5811
5812 static int get_config_net_ipvlan_mode(const char *key, char *retv, int inlen,
5813 struct lxc_conf *c, void *data)
5814 {
5815 int len;
5816 int fulllen = 0;
5817 const char *mode;
5818 struct lxc_netdev *netdev = data;
5819
5820 if (!retv)
5821 inlen = 0;
5822 else
5823 memset(retv, 0, inlen);
5824
5825 if (!netdev)
5826 return ret_set_errno(-1, EINVAL);
5827
5828 if (netdev->type != LXC_NET_IPVLAN)
5829 return 0;
5830
5831 switch (netdev->priv.ipvlan_attr.mode) {
5832 case IPVLAN_MODE_L3:
5833 mode = "l3";
5834 break;
5835 case IPVLAN_MODE_L3S:
5836 mode = "l3s";
5837 break;
5838 case IPVLAN_MODE_L2:
5839 mode = "l2";
5840 break;
5841 default:
5842 mode = "(invalid)";
5843 break;
5844 }
5845
5846 strprint(retv, inlen, "%s", mode);
5847
5848 return fulllen;
5849 }
5850
5851 static int get_config_net_ipvlan_isolation(const char *key, char *retv, int inlen,
5852 struct lxc_conf *c, void *data)
5853 {
5854 int len;
5855 int fulllen = 0;
5856 const char *mode;
5857 struct lxc_netdev *netdev = data;
5858
5859 if (!retv)
5860 inlen = 0;
5861 else
5862 memset(retv, 0, inlen);
5863
5864 if (!netdev)
5865 return ret_set_errno(-1, EINVAL);
5866
5867 if (netdev->type != LXC_NET_IPVLAN)
5868 return 0;
5869
5870 switch (netdev->priv.ipvlan_attr.isolation) {
5871 case IPVLAN_ISOLATION_BRIDGE:
5872 mode = "bridge";
5873 break;
5874 case IPVLAN_ISOLATION_PRIVATE:
5875 mode = "private";
5876 break;
5877 case IPVLAN_ISOLATION_VEPA:
5878 mode = "vepa";
5879 break;
5880 default:
5881 mode = "(invalid)";
5882 break;
5883 }
5884
5885 strprint(retv, inlen, "%s", mode);
5886
5887 return fulllen;
5888 }
5889
5890 static int get_config_net_veth_mode(const char *key, char *retv, int inlen,
5891 struct lxc_conf *c, void *data)
5892 {
5893 int len;
5894 int fulllen = 0;
5895 const char *mode;
5896 struct lxc_netdev *netdev = data;
5897
5898 if (!retv)
5899 inlen = 0;
5900 else
5901 memset(retv, 0, inlen);
5902
5903 if (!netdev)
5904 return ret_set_errno(-1, EINVAL);
5905
5906 if (netdev->type != LXC_NET_VETH)
5907 return 0;
5908
5909 switch (netdev->priv.veth_attr.mode) {
5910 case VETH_MODE_BRIDGE:
5911 mode = "bridge";
5912 break;
5913 case VETH_MODE_ROUTER:
5914 mode = "router";
5915 break;
5916 default:
5917 mode = "(invalid)";
5918 break;
5919 }
5920
5921 strprint(retv, inlen, "%s", mode);
5922
5923 return fulllen;
5924 }
5925
5926 static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
5927 struct lxc_conf *c, void *data)
5928 {
5929 int len;
5930 int fulllen = 0;
5931 struct lxc_netdev *netdev = data;
5932
5933 if (!retv)
5934 inlen = 0;
5935 else
5936 memset(retv, 0, inlen);
5937
5938 if (!netdev)
5939 return -1;
5940
5941 if (netdev->type != LXC_NET_VETH)
5942 return 0;
5943
5944 strprint(retv, inlen, "%s",
5945 netdev->priv.veth_attr.pair[0] != '\0'
5946 ? netdev->priv.veth_attr.pair
5947 : netdev->priv.veth_attr.veth1);
5948
5949 return fulllen;
5950 }
5951
5952 static int get_config_net_veth_vlan_id(const char *key, char *retv, int inlen,
5953 struct lxc_conf *c, void *data)
5954 {
5955 int len;
5956 int fulllen = 0;
5957 struct lxc_netdev *netdev = data;
5958
5959 if (!netdev)
5960 return ret_errno(EINVAL);
5961
5962 if (netdev->type != LXC_NET_VETH)
5963 return 0;
5964
5965 if (!retv)
5966 inlen = 0;
5967 else
5968 memset(retv, 0, inlen);
5969
5970 strprint(retv, inlen, "%d", netdev->priv.veth_attr.vlan_id);
5971
5972 return fulllen;
5973 }
5974
5975 static int get_config_net_veth_vlan_tagged_id(const char *key, char *retv, int inlen,
5976 struct lxc_conf *c, void *data)
5977 {
5978 int len;
5979 size_t listlen;
5980 struct lxc_list *it;
5981 int fulllen = 0;
5982 struct lxc_netdev *netdev = data;
5983
5984 if (!netdev)
5985 return ret_errno(EINVAL);
5986
5987 if (netdev->type != LXC_NET_VETH)
5988 return 0;
5989
5990 if (!retv)
5991 inlen = 0;
5992 else
5993 memset(retv, 0, inlen);
5994
5995 listlen = lxc_list_len(&netdev->priv.veth_attr.vlan_tagged_ids);
5996
5997 lxc_list_for_each(it, &netdev->priv.veth_attr.vlan_tagged_ids) {
5998 unsigned short i = PTR_TO_USHORT(it->elem);
5999 strprint(retv, inlen, "%u%s", i,
6000 (listlen-- > 1) ? "\n" : "");
6001 }
6002
6003 return fulllen;
6004 }
6005
6006 static int get_config_net_script_up(const char *key, char *retv, int inlen,
6007 struct lxc_conf *c, void *data)
6008 {
6009 int len;
6010 int fulllen = 0;
6011 struct lxc_netdev *netdev = data;
6012
6013 if (!retv)
6014 inlen = 0;
6015 else
6016 memset(retv, 0, inlen);
6017
6018 if (!netdev)
6019 return -1;
6020
6021 if (netdev->upscript)
6022 strprint(retv, inlen, "%s", netdev->upscript);
6023
6024 return fulllen;
6025 }
6026
6027 static int get_config_net_script_down(const char *key, char *retv, int inlen,
6028 struct lxc_conf *c, void *data)
6029 {
6030 int len;
6031 int fulllen = 0;
6032 struct lxc_netdev *netdev = data;
6033
6034 if (!retv)
6035 inlen = 0;
6036 else
6037 memset(retv, 0, inlen);
6038
6039 if (!netdev)
6040 return -1;
6041
6042 if (netdev->downscript)
6043 strprint(retv, inlen, "%s", netdev->downscript);
6044
6045 return fulllen;
6046 }
6047
6048 static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
6049 struct lxc_conf *c, void *data)
6050 {
6051 int len;
6052 int fulllen = 0;
6053 struct lxc_netdev *netdev = data;
6054
6055 if (!retv)
6056 inlen = 0;
6057 else
6058 memset(retv, 0, inlen);
6059
6060 if (!netdev)
6061 return -1;
6062
6063 if (netdev->hwaddr)
6064 strprint(retv, inlen, "%s", netdev->hwaddr);
6065
6066 return fulllen;
6067 }
6068
6069 static int get_config_net_mtu(const char *key, char *retv, int inlen,
6070 struct lxc_conf *c, void *data)
6071 {
6072 int len;
6073 int fulllen = 0;
6074 struct lxc_netdev *netdev = data;
6075
6076 if (!retv)
6077 inlen = 0;
6078 else
6079 memset(retv, 0, inlen);
6080
6081 if (!netdev)
6082 return -1;
6083
6084 if (netdev->mtu)
6085 strprint(retv, inlen, "%s", netdev->mtu);
6086
6087 return fulllen;
6088 }
6089
6090 static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
6091 struct lxc_conf *c, void *data)
6092 {
6093 int len;
6094 int fulllen = 0;
6095 struct lxc_netdev *netdev = data;
6096
6097 if (!retv)
6098 inlen = 0;
6099 else
6100 memset(retv, 0, inlen);
6101
6102 if (!netdev)
6103 return -1;
6104
6105 if (netdev->type != LXC_NET_VLAN)
6106 return 0;
6107
6108 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
6109
6110 return fulllen;
6111 }
6112
6113 static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
6114 struct lxc_conf *c, void *data)
6115 {
6116 int len;
6117 char buf[INET_ADDRSTRLEN];
6118 int fulllen = 0;
6119 struct lxc_netdev *netdev = data;
6120
6121 if (!retv)
6122 inlen = 0;
6123 else
6124 memset(retv, 0, inlen);
6125
6126 if (!netdev)
6127 return -1;
6128
6129 if (netdev->ipv4_gateway_auto) {
6130 strprint(retv, inlen, "auto");
6131 } else if (netdev->ipv4_gateway_dev) {
6132 strprint(retv, inlen, "dev");
6133 } else if (netdev->ipv4_gateway) {
6134 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
6135 strprint(retv, inlen, "%s", buf);
6136 }
6137
6138 return fulllen;
6139 }
6140
6141 static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
6142 struct lxc_conf *c, void *data)
6143 {
6144 int len;
6145 size_t listlen;
6146 char buf[INET_ADDRSTRLEN];
6147 struct lxc_list *it;
6148 int fulllen = 0;
6149 struct lxc_netdev *netdev = data;
6150
6151 if (!retv)
6152 inlen = 0;
6153 else
6154 memset(retv, 0, inlen);
6155
6156 if (!netdev)
6157 return -1;
6158
6159 listlen = lxc_list_len(&netdev->ipv4);
6160
6161 lxc_list_for_each(it, &netdev->ipv4) {
6162 struct lxc_inetdev *i = it->elem;
6163 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6164 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6165 (listlen-- > 1) ? "\n" : "");
6166 }
6167
6168 return fulllen;
6169 }
6170
6171 static int get_config_net_veth_ipv4_route(const char *key, char *retv, int inlen,
6172 struct lxc_conf *c, void *data)
6173 {
6174 int len;
6175 size_t listlen;
6176 char buf[INET_ADDRSTRLEN];
6177 struct lxc_list *it;
6178 int fulllen = 0;
6179 struct lxc_netdev *netdev = data;
6180
6181 if (!retv)
6182 inlen = 0;
6183 else
6184 memset(retv, 0, inlen);
6185
6186 if (!netdev)
6187 return ret_set_errno(-1, EINVAL);
6188
6189 if (netdev->type != LXC_NET_VETH)
6190 return 0;
6191
6192 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv4_routes);
6193
6194 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv4_routes) {
6195 struct lxc_inetdev *i = it->elem;
6196 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
6197 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6198 (listlen-- > 1) ? "\n" : "");
6199 }
6200
6201 return fulllen;
6202 }
6203
6204 static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
6205 struct lxc_conf *c, void *data)
6206 {
6207 int len;
6208 char buf[INET6_ADDRSTRLEN];
6209 int fulllen = 0;
6210 struct lxc_netdev *netdev = data;
6211
6212 if (!retv)
6213 inlen = 0;
6214 else
6215 memset(retv, 0, inlen);
6216
6217 if (!netdev)
6218 return -1;
6219
6220 if (netdev->ipv6_gateway_auto) {
6221 strprint(retv, inlen, "auto");
6222 } else if (netdev->ipv6_gateway_dev) {
6223 strprint(retv, inlen, "dev");
6224 } else if (netdev->ipv6_gateway) {
6225 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
6226 strprint(retv, inlen, "%s", buf);
6227 }
6228
6229 return fulllen;
6230 }
6231
6232 static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
6233 struct lxc_conf *c, void *data)
6234 {
6235 int len;
6236 size_t listlen;
6237 char buf[INET6_ADDRSTRLEN];
6238 struct lxc_list *it;
6239 int fulllen = 0;
6240 struct lxc_netdev *netdev = data;
6241
6242 if (!retv)
6243 inlen = 0;
6244 else
6245 memset(retv, 0, inlen);
6246
6247 if (!netdev)
6248 return -1;
6249
6250 listlen = lxc_list_len(&netdev->ipv6);
6251
6252 lxc_list_for_each(it, &netdev->ipv6) {
6253 struct lxc_inet6dev *i = it->elem;
6254 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6255 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6256 (listlen-- > 1) ? "\n" : "");
6257 }
6258
6259 return fulllen;
6260 }
6261
6262 static int get_config_net_veth_ipv6_route(const char *key, char *retv, int inlen,
6263 struct lxc_conf *c, void *data)
6264 {
6265 int len;
6266 size_t listlen;
6267 char buf[INET6_ADDRSTRLEN];
6268 struct lxc_list *it;
6269 int fulllen = 0;
6270 struct lxc_netdev *netdev = data;
6271
6272 if (!retv)
6273 inlen = 0;
6274 else
6275 memset(retv, 0, inlen);
6276
6277 if (!netdev)
6278 return ret_set_errno(-1, EINVAL);
6279
6280 if (netdev->type != LXC_NET_VETH)
6281 return 0;
6282
6283 listlen = lxc_list_len(&netdev->priv.veth_attr.ipv6_routes);
6284
6285 lxc_list_for_each(it, &netdev->priv.veth_attr.ipv6_routes) {
6286 struct lxc_inet6dev *i = it->elem;
6287 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
6288 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
6289 (listlen-- > 1) ? "\n" : "");
6290 }
6291
6292 return fulllen;
6293 }
6294
6295 int lxc_list_config_items(char *retv, int inlen)
6296 {
6297 size_t i;
6298 int len;
6299 int fulllen = 0;
6300
6301 if (!retv)
6302 inlen = 0;
6303 else
6304 memset(retv, 0, inlen);
6305
6306 for (i = 0; i < config_jump_table_size; i++) {
6307 char *s = config_jump_table[i].name;
6308
6309 if (s[strlen(s) - 1] == '.')
6310 continue;
6311
6312 strprint(retv, inlen, "%s\n", s);
6313 }
6314
6315 return fulllen;
6316 }
6317
6318 int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
6319 int inlen)
6320 {
6321 int len;
6322 int fulllen = 0;
6323
6324 if (!retv)
6325 inlen = 0;
6326 else
6327 memset(retv, 0, inlen);
6328
6329 if (!strcmp(key, "lxc.apparmor")) {
6330 strprint(retv, inlen, "allow_incomplete\n");
6331 strprint(retv, inlen, "allow_nesting\n");
6332 strprint(retv, inlen, "profile\n");
6333 strprint(retv, inlen, "raw\n");
6334 } else if (!strcmp(key, "lxc.cgroup")) {
6335 strprint(retv, inlen, "dir\n");
6336 } else if (!strcmp(key, "lxc.selinux")) {
6337 strprint(retv, inlen, "context\n");
6338 strprint(retv, inlen, "context.keyring\n");
6339 } else if (!strcmp(key, "lxc.mount")) {
6340 strprint(retv, inlen, "auto\n");
6341 strprint(retv, inlen, "entry\n");
6342 strprint(retv, inlen, "fstab\n");
6343 } else if (!strcmp(key, "lxc.rootfs")) {
6344 strprint(retv, inlen, "mount\n");
6345 strprint(retv, inlen, "options\n");
6346 strprint(retv, inlen, "path\n");
6347 } else if (!strcmp(key, "lxc.uts")) {
6348 strprint(retv, inlen, "name\n");
6349 } else if (!strcmp(key, "lxc.hook")) {
6350 strprint(retv, inlen, "autodev\n");
6351 strprint(retv, inlen, "autodevtmpfssize\n");
6352 strprint(retv, inlen, "clone\n");
6353 strprint(retv, inlen, "destroy\n");
6354 strprint(retv, inlen, "mount\n");
6355 strprint(retv, inlen, "post-stop\n");
6356 strprint(retv, inlen, "pre-mount\n");
6357 strprint(retv, inlen, "pre-start\n");
6358 strprint(retv, inlen, "start-host\n");
6359 strprint(retv, inlen, "start\n");
6360 strprint(retv, inlen, "stop\n");
6361 } else if (!strcmp(key, "lxc.cap")) {
6362 strprint(retv, inlen, "drop\n");
6363 strprint(retv, inlen, "keep\n");
6364 } else if (!strcmp(key, "lxc.console")) {
6365 strprint(retv, inlen, "logfile\n");
6366 strprint(retv, inlen, "path\n");
6367 } else if (!strcmp(key, "lxc.seccomp")) {
6368 strprint(retv, inlen, "profile\n");
6369 } else if (!strcmp(key, "lxc.signal")) {
6370 strprint(retv, inlen, "halt\n");
6371 strprint(retv, inlen, "reboot\n");
6372 strprint(retv, inlen, "stop\n");
6373 } else if (!strcmp(key, "lxc.start")) {
6374 strprint(retv, inlen, "auto\n");
6375 strprint(retv, inlen, "delay\n");
6376 strprint(retv, inlen, "order\n");
6377 } else if (!strcmp(key, "lxc.monitor")) {
6378 strprint(retv, inlen, "unshare\n");
6379 } else if (!strcmp(key, "lxc.keyring")) {
6380 strprint(retv, inlen, "session\n");
6381 } else {
6382 fulllen = -1;
6383 }
6384
6385 return fulllen;
6386 }
6387
6388 int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
6389 {
6390 int len;
6391 const char *idxstring;
6392 struct lxc_netdev *netdev;
6393 int fulllen = 0;
6394 ssize_t idx = -1;
6395
6396 idxstring = key + 8;
6397 if (!isdigit(*idxstring))
6398 return -1;
6399
6400 (void)get_network_config_ops(key, c, &idx, NULL);
6401 if (idx < 0)
6402 return -1;
6403
6404 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
6405 if (!netdev)
6406 return -1;
6407
6408 if (!retv)
6409 inlen = 0;
6410 else
6411 memset(retv, 0, inlen);
6412
6413 strprint(retv, inlen, "type\n");
6414 strprint(retv, inlen, "script.up\n");
6415 strprint(retv, inlen, "script.down\n");
6416
6417 if (netdev->type != LXC_NET_EMPTY) {
6418 strprint(retv, inlen, "flags\n");
6419 strprint(retv, inlen, "link\n");
6420 strprint(retv, inlen, "name\n");
6421 strprint(retv, inlen, "hwaddr\n");
6422 strprint(retv, inlen, "mtu\n");
6423 strprint(retv, inlen, "ipv6.address\n");
6424 strprint(retv, inlen, "ipv6.gateway\n");
6425 strprint(retv, inlen, "ipv4.address\n");
6426 strprint(retv, inlen, "ipv4.gateway\n");
6427 }
6428
6429 switch (netdev->type) {
6430 case LXC_NET_VETH:
6431 strprint(retv, inlen, "veth.pair\n");
6432 strprint(retv, inlen, "veth.ipv4.route\n");
6433 strprint(retv, inlen, "veth.ipv6.route\n");
6434 strprint(retv, inlen, "veth.vlan.id\n");
6435 break;
6436 case LXC_NET_MACVLAN:
6437 strprint(retv, inlen, "macvlan.mode\n");
6438 break;
6439 case LXC_NET_IPVLAN:
6440 strprint(retv, inlen, "ipvlan.mode\n");
6441 strprint(retv, inlen, "ipvlan.isolation\n");
6442 break;
6443 case LXC_NET_VLAN:
6444 strprint(retv, inlen, "vlan.id\n");
6445 break;
6446 case LXC_NET_PHYS:
6447 break;
6448 }
6449
6450 return fulllen;
6451 }