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