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