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