]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/confile.c
conf: simplify tty handling
[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_buffer_size);
86 lxc_config_define(console_logfile);
87 lxc_config_define(console_path);
88 lxc_config_define(console_rotate);
89 lxc_config_define(console_size);
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.size", set_config_console_buffer_size, get_config_console_buffer_size, clr_config_console_buffer_size, },
159 { "lxc.console.logfile", set_config_console_logfile, get_config_console_logfile, clr_config_console_logfile, },
160 { "lxc.console.path", set_config_console_path, get_config_console_path, clr_config_console_path, },
161 { "lxc.console.rotate", set_config_console_rotate, get_config_console_rotate, clr_config_console_rotate, },
162 { "lxc.console.size", set_config_console_size, get_config_console_size, clr_config_console_size, },
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 if (!strchr(value, '=')) {
1100 const char *env_val;
1101 const char *env_key = value;
1102 const char *env_var[3] = {0};
1103
1104 env_val = getenv(env_key);
1105 if (!env_val)
1106 goto on_error;
1107
1108 env_var[0] = env_key;
1109 env_var[1] = env_val;
1110 list_item->elem = lxc_string_join("=", env_var, false);
1111 } else {
1112 list_item->elem = strdup(value);
1113 }
1114
1115 if (!list_item->elem)
1116 goto on_error;
1117
1118 lxc_list_add_tail(&lxc_conf->environment, list_item);
1119
1120 return 0;
1121
1122 on_error:
1123 free(list_item);
1124 return -1;
1125 }
1126
1127 static int set_config_tty_max(const char *key, const char *value,
1128 struct lxc_conf *lxc_conf, void *data)
1129 {
1130 int ret;
1131 unsigned int nbtty = 0;
1132
1133 if (lxc_config_value_empty(value)) {
1134 lxc_conf->ttys.max = 0;
1135 return 0;
1136 }
1137
1138 ret = lxc_safe_uint(value, &nbtty);
1139 if (ret < 0)
1140 return -1;
1141
1142 lxc_conf->ttys.max = nbtty;
1143 return 0;
1144 }
1145
1146 static int set_config_tty_dir(const char *key, const char *value,
1147 struct lxc_conf *lxc_conf, void *data)
1148 {
1149 return set_config_string_item_max(&lxc_conf->ttys.dir, value,
1150 NAME_MAX + 1);
1151 }
1152
1153 static int set_config_apparmor_profile(const char *key, const char *value,
1154 struct lxc_conf *lxc_conf, void *data)
1155 {
1156 return set_config_string_item(&lxc_conf->lsm_aa_profile, value);
1157 }
1158
1159 static int set_config_apparmor_allow_incomplete(const char *key,
1160 const char *value,
1161 struct lxc_conf *lxc_conf,
1162 void *data)
1163 {
1164 if (lxc_config_value_empty(value)) {
1165 lxc_conf->lsm_aa_allow_incomplete = 0;
1166 return 0;
1167 }
1168
1169 if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete) < 0)
1170 return -1;
1171
1172 if (lxc_conf->lsm_aa_allow_incomplete > 1)
1173 return -1;
1174
1175 return 0;
1176 }
1177
1178 static int set_config_selinux_context(const char *key, const char *value,
1179 struct lxc_conf *lxc_conf, void *data)
1180 {
1181 return set_config_string_item(&lxc_conf->lsm_se_context, value);
1182 }
1183
1184 static int set_config_log_file(const char *key, const char *value,
1185 struct lxc_conf *c, void *data)
1186 {
1187 int ret;
1188
1189 if (lxc_config_value_empty(value)) {
1190 free(c->logfile);
1191 c->logfile = NULL;
1192 return 0;
1193 }
1194
1195 /* Store these values in the lxc_conf, and then try to set for actual
1196 * current logging.
1197 */
1198 ret = set_config_path_item(&c->logfile, value);
1199 if (ret == 0)
1200 ret = lxc_log_set_file(&c->logfd, c->logfile);
1201
1202 return ret;
1203 }
1204
1205 static int set_config_log_level(const char *key, const char *value,
1206 struct lxc_conf *lxc_conf, void *data)
1207 {
1208 int newlevel;
1209
1210 if (lxc_config_value_empty(value)) {
1211 lxc_conf->loglevel = LXC_LOG_LEVEL_NOTSET;
1212 return 0;
1213 }
1214
1215 if (value[0] >= '0' && value[0] <= '9') {
1216 if (lxc_safe_int(value, &newlevel) < 0)
1217 return -1;
1218 } else {
1219 newlevel = lxc_log_priority_to_int(value);
1220 }
1221
1222 /* Store these values in the lxc_conf, and then try to set for actual
1223 * current logging.
1224 */
1225 lxc_conf->loglevel = newlevel;
1226 return lxc_log_set_level(&lxc_conf->loglevel, newlevel);
1227 }
1228
1229 static int set_config_autodev(const char *key, const char *value,
1230 struct lxc_conf *lxc_conf, void *data)
1231 {
1232 if (lxc_config_value_empty(value)) {
1233 lxc_conf->autodev = 0;
1234 return 0;
1235 }
1236
1237 if (lxc_safe_uint(value, &lxc_conf->autodev) < 0)
1238 return -1;
1239
1240 if (lxc_conf->autodev > 1)
1241 return -1;
1242
1243 return 0;
1244 }
1245
1246 static int sig_num(const char *sig)
1247 {
1248 unsigned int signum;
1249
1250 if (lxc_safe_uint(sig, &signum) < 0)
1251 return -1;
1252
1253 return signum;
1254 }
1255
1256 static int rt_sig_num(const char *signame)
1257 {
1258 int rtmax = 0, sig_n = 0;
1259
1260 if (strncasecmp(signame, "max-", 4) == 0) {
1261 rtmax = 1;
1262 }
1263
1264 signame += 4;
1265 if (!isdigit(*signame))
1266 return -1;
1267
1268 sig_n = sig_num(signame);
1269 sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n;
1270 if (sig_n > SIGRTMAX || sig_n < SIGRTMIN)
1271 return -1;
1272
1273 return sig_n;
1274 }
1275
1276 static int sig_parse(const char *signame)
1277 {
1278 size_t n;
1279
1280 if (isdigit(*signame)) {
1281 return sig_num(signame);
1282 } else if (strncasecmp(signame, "sig", 3) == 0) {
1283 signame += 3;
1284 if (strncasecmp(signame, "rt", 2) == 0)
1285 return rt_sig_num(signame + 2);
1286 for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++) {
1287 if (strcasecmp(signames[n].name, signame) == 0)
1288 return signames[n].num;
1289 }
1290 }
1291
1292 return -1;
1293 }
1294
1295 static int set_config_signal_halt(const char *key, const char *value,
1296 struct lxc_conf *lxc_conf, void *data)
1297 {
1298 int sig_n;
1299
1300 if (lxc_config_value_empty(value)) {
1301 lxc_conf->haltsignal = 0;
1302 return 0;
1303 }
1304
1305 sig_n = sig_parse(value);
1306 if (sig_n < 0)
1307 return -1;
1308
1309 lxc_conf->haltsignal = sig_n;
1310
1311 return 0;
1312 }
1313
1314 static int set_config_signal_reboot(const char *key, const char *value,
1315 struct lxc_conf *lxc_conf, void *data)
1316 {
1317 int sig_n;
1318
1319 if (lxc_config_value_empty(value)) {
1320 lxc_conf->rebootsignal = 0;
1321 return 0;
1322 }
1323
1324 sig_n = sig_parse(value);
1325 if (sig_n < 0)
1326 return -1;
1327
1328 lxc_conf->rebootsignal = sig_n;
1329
1330 return 0;
1331 }
1332
1333 static int set_config_signal_stop(const char *key, const char *value,
1334 struct lxc_conf *lxc_conf, void *data)
1335 {
1336 int sig_n;
1337
1338 if (lxc_config_value_empty(value)) {
1339 lxc_conf->stopsignal = 0;
1340 return 0;
1341 }
1342
1343 sig_n = sig_parse(value);
1344 if (sig_n < 0)
1345 return -1;
1346
1347 lxc_conf->stopsignal = sig_n;
1348
1349 return 0;
1350 }
1351
1352 static int __set_config_cgroup_controller(const char *key, const char *value,
1353 struct lxc_conf *lxc_conf, int version)
1354 {
1355 const char *subkey, *token;
1356 size_t token_len;
1357 struct lxc_list *cglist = NULL;
1358 struct lxc_cgroup *cgelem = NULL;
1359
1360 if (lxc_config_value_empty(value))
1361 return lxc_clear_cgroups(lxc_conf, key, version);
1362
1363 if (version == CGROUP2_SUPER_MAGIC) {
1364 token = "lxc.cgroup2.";
1365 token_len = 12;
1366 } else if (version == CGROUP_SUPER_MAGIC) {
1367 token = "lxc.cgroup.";
1368 token_len = 11;
1369 } else {
1370 return -EINVAL;
1371 }
1372
1373 if (strncmp(key, token, token_len) != 0)
1374 return -EINVAL;
1375
1376 subkey = key + token_len;
1377 if (*subkey == '\0')
1378 return -EINVAL;
1379
1380 cglist = malloc(sizeof(*cglist));
1381 if (!cglist)
1382 goto out;
1383
1384 cgelem = malloc(sizeof(*cgelem));
1385 if (!cgelem)
1386 goto out;
1387 memset(cgelem, 0, sizeof(*cgelem));
1388
1389 cgelem->subsystem = strdup(subkey);
1390 if (!cgelem->subsystem)
1391 goto out;
1392
1393 cgelem->value = strdup(value);
1394 if (!cgelem->value)
1395 goto out;
1396
1397 cgelem->version = version;
1398
1399 lxc_list_add_elem(cglist, cgelem);
1400
1401 if (version == CGROUP2_SUPER_MAGIC)
1402 lxc_list_add_tail(&lxc_conf->cgroup2, cglist);
1403 else
1404 lxc_list_add_tail(&lxc_conf->cgroup, cglist);
1405
1406 return 0;
1407
1408 out:
1409 free(cglist);
1410 if (cgelem) {
1411 free(cgelem->subsystem);
1412 free(cgelem->value);
1413 free(cgelem);
1414 }
1415
1416 return -1;
1417 }
1418
1419 static int set_config_cgroup_controller(const char *key, const char *value,
1420 struct lxc_conf *lxc_conf, void *data)
1421 {
1422 return __set_config_cgroup_controller(key, value, lxc_conf,
1423 CGROUP_SUPER_MAGIC);
1424 }
1425
1426 static int set_config_cgroup2_controller(const char *key, const char *value,
1427 struct lxc_conf *lxc_conf, void *data)
1428 {
1429 return __set_config_cgroup_controller(key, value, lxc_conf,
1430 CGROUP2_SUPER_MAGIC);
1431 }
1432
1433
1434 static int set_config_cgroup_dir(const char *key, const char *value,
1435 struct lxc_conf *lxc_conf, void *data)
1436 {
1437 if (lxc_config_value_empty(value))
1438 return clr_config_cgroup_dir(key, lxc_conf, NULL);
1439
1440 return set_config_string_item(&lxc_conf->cgroup_meta.dir, value);
1441 }
1442
1443 static int set_config_prlimit(const char *key, const char *value,
1444 struct lxc_conf *lxc_conf, void *data)
1445 {
1446 struct lxc_list *iter;
1447 struct rlimit limit;
1448 rlim_t limit_value;
1449 struct lxc_list *limlist = NULL;
1450 struct lxc_limit *limelem = NULL;
1451
1452 if (lxc_config_value_empty(value))
1453 return lxc_clear_limits(lxc_conf, key);
1454
1455 if (strncmp(key, "lxc.prlimit.", sizeof("lxc.prlimit.") - 1) != 0)
1456 return -1;
1457
1458 key += sizeof("lxc.prlimit.") - 1;
1459
1460 /* soft limit comes first in the value */
1461 if (!parse_limit_value(&value, &limit_value))
1462 return -1;
1463 limit.rlim_cur = limit_value;
1464
1465 /* skip spaces and a colon */
1466 while (isspace(*value))
1467 ++value;
1468
1469 if (*value == ':')
1470 ++value;
1471 else if (*value) /* any other character is an error here */
1472 return -1;
1473
1474 while (isspace(*value))
1475 ++value;
1476
1477 /* optional hard limit */
1478 if (*value) {
1479 if (!parse_limit_value(&value, &limit_value))
1480 return -1;
1481 limit.rlim_max = limit_value;
1482
1483 /* check for trailing garbage */
1484 while (isspace(*value))
1485 ++value;
1486
1487 if (*value)
1488 return -1;
1489 } else {
1490 /* a single value sets both hard and soft limit */
1491 limit.rlim_max = limit.rlim_cur;
1492 }
1493
1494 /* find existing list element */
1495 lxc_list_for_each(iter, &lxc_conf->limits) {
1496 limelem = iter->elem;
1497 if (!strcmp(key, limelem->resource)) {
1498 limelem->limit = limit;
1499 return 0;
1500 }
1501 }
1502
1503 /* allocate list element */
1504 limlist = malloc(sizeof(*limlist));
1505 if (!limlist)
1506 goto on_error;
1507
1508 limelem = malloc(sizeof(*limelem));
1509 if (!limelem)
1510 goto on_error;
1511 memset(limelem, 0, sizeof(*limelem));
1512
1513 limelem->resource = strdup(key);
1514 if (!limelem->resource)
1515 goto on_error;
1516 limelem->limit = limit;
1517
1518 lxc_list_add_elem(limlist, limelem);;
1519
1520 lxc_list_add_tail(&lxc_conf->limits, limlist);
1521
1522 return 0;
1523
1524 on_error:
1525 free(limlist);
1526 if (limelem) {
1527 free(limelem->resource);
1528 free(limelem);
1529 }
1530 return -1;
1531 }
1532
1533 static int set_config_sysctl(const char *key, const char *value,
1534 struct lxc_conf *lxc_conf, void *data)
1535 {
1536 struct lxc_list *iter;
1537 char *replace_value = NULL;
1538 struct lxc_list *sysctl_list = NULL;
1539 struct lxc_sysctl *sysctl_elem = NULL;
1540
1541 if (lxc_config_value_empty(value))
1542 return clr_config_sysctl(key, lxc_conf, NULL);
1543
1544 if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) != 0)
1545 return -1;
1546
1547 key += sizeof("lxc.sysctl.") - 1;
1548
1549 /* find existing list element */
1550 lxc_list_for_each(iter, &lxc_conf->sysctls) {
1551 sysctl_elem = iter->elem;
1552
1553 if (strcmp(key, sysctl_elem->key) != 0)
1554 continue;
1555
1556 replace_value = strdup(value);
1557 if (!replace_value)
1558 return -1;
1559
1560 free(sysctl_elem->value);
1561 sysctl_elem->value = replace_value;
1562 return 0;
1563 }
1564
1565 /* allocate list element */
1566 sysctl_list = malloc(sizeof(*sysctl_list));
1567 if (!sysctl_list)
1568 goto on_error;
1569
1570 sysctl_elem = malloc(sizeof(*sysctl_elem));
1571 if (!sysctl_elem)
1572 goto on_error;
1573 memset(sysctl_elem, 0, sizeof(*sysctl_elem));
1574
1575 sysctl_elem->key = strdup(key);
1576 if (!sysctl_elem->key)
1577 goto on_error;
1578
1579 sysctl_elem->value = strdup(value);
1580 if (!sysctl_elem->value)
1581 goto on_error;
1582
1583 lxc_list_add_elem(sysctl_list, sysctl_elem);
1584
1585 lxc_list_add_tail(&lxc_conf->sysctls, sysctl_list);
1586
1587 return 0;
1588
1589 on_error:
1590 free(sysctl_list);
1591 if (sysctl_elem) {
1592 free(sysctl_elem->key);
1593 free(sysctl_elem->value);
1594 free(sysctl_elem);
1595 }
1596 return -1;
1597 }
1598
1599 static int set_config_proc(const char *key, const char *value,
1600 struct lxc_conf *lxc_conf, void *data)
1601 {
1602 const char *subkey;
1603 struct lxc_list *proclist = NULL;
1604 struct lxc_proc *procelem = NULL;
1605
1606 if (lxc_config_value_empty(value))
1607 return clr_config_proc(key, lxc_conf, NULL);
1608
1609 if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") -1) != 0)
1610 return -1;
1611
1612 subkey = key + sizeof("lxc.proc.") - 1;
1613 if (*subkey == '\0')
1614 return -EINVAL;
1615
1616 proclist = malloc(sizeof(*proclist));
1617 if (!proclist)
1618 goto on_error;
1619
1620 procelem = malloc(sizeof(*procelem));
1621 if (!procelem)
1622 goto on_error;
1623 memset(procelem, 0, sizeof(*procelem));
1624
1625 procelem->filename = strdup(subkey);
1626 procelem->value = strdup(value);
1627
1628 if (!procelem->filename || !procelem->value)
1629 goto on_error;
1630
1631 proclist->elem = procelem;
1632
1633 lxc_list_add_tail(&lxc_conf->procs, proclist);
1634
1635 return 0;
1636
1637 on_error:
1638 free(proclist);
1639 if (procelem) {
1640 free(procelem->filename);
1641 free(procelem->value);
1642 free(procelem);
1643 }
1644
1645 return -1;
1646 }
1647
1648 static int set_config_idmaps(const char *key, const char *value,
1649 struct lxc_conf *lxc_conf, void *data)
1650 {
1651 unsigned long hostid, nsid, range;
1652 char type;
1653 int ret;
1654 struct lxc_list *idmaplist = NULL;
1655 struct id_map *idmap = NULL;
1656
1657 if (lxc_config_value_empty(value))
1658 return lxc_clear_idmaps(lxc_conf);
1659
1660 idmaplist = malloc(sizeof(*idmaplist));
1661 if (!idmaplist)
1662 goto on_error;
1663
1664 idmap = malloc(sizeof(*idmap));
1665 if (!idmap)
1666 goto on_error;
1667 memset(idmap, 0, sizeof(*idmap));
1668
1669 ret = parse_idmaps(value, &type, &nsid, &hostid, &range);
1670 if (ret < 0) {
1671 ERROR("Failed to parse id mappings");
1672 goto on_error;
1673 }
1674
1675 INFO("Read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range);
1676 if (type == 'u')
1677 idmap->idtype = ID_TYPE_UID;
1678 else if (type == 'g')
1679 idmap->idtype = ID_TYPE_GID;
1680 else
1681 goto on_error;
1682
1683 idmap->hostid = hostid;
1684 idmap->nsid = nsid;
1685 idmap->range = range;
1686 idmaplist->elem = idmap;
1687 lxc_list_add_tail(&lxc_conf->id_map, idmaplist);
1688
1689 if (!lxc_conf->root_nsuid_map && idmap->idtype == ID_TYPE_UID)
1690 if (idmap->nsid == 0)
1691 lxc_conf->root_nsuid_map = idmap;
1692
1693
1694 if (!lxc_conf->root_nsgid_map && idmap->idtype == ID_TYPE_GID)
1695 if (idmap->nsid == 0)
1696 lxc_conf->root_nsgid_map = idmap;
1697
1698 idmap = NULL;
1699
1700 return 0;
1701
1702 on_error:
1703 free(idmaplist);
1704 free(idmap);
1705
1706 return -1;
1707 }
1708
1709 static int set_config_mount_fstab(const char *key, const char *value,
1710 struct lxc_conf *lxc_conf, void *data)
1711 {
1712 if (lxc_config_value_empty(value)) {
1713 clr_config_mount_fstab(key, lxc_conf, NULL);
1714 return -1;
1715 }
1716
1717 return set_config_path_item(&lxc_conf->fstab, value);
1718 }
1719
1720 static int set_config_mount_auto(const char *key, const char *value,
1721 struct lxc_conf *lxc_conf, void *data)
1722 {
1723 char *autos, *autoptr, *sptr, *token;
1724 int i;
1725 int ret = -1;
1726 static struct {
1727 const char *token;
1728 int mask;
1729 int flag;
1730 } allowed_auto_mounts[] = {
1731 { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
1732 { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED },
1733 { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW },
1734 { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
1735 { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO },
1736 { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED },
1737 { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW },
1738 { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC },
1739 { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED },
1740 { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO },
1741 { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW },
1742 { "cgroup:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC | LXC_AUTO_CGROUP_FORCE },
1743 { "cgroup:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED | LXC_AUTO_CGROUP_FORCE },
1744 { "cgroup:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO | LXC_AUTO_CGROUP_FORCE },
1745 { "cgroup:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW | LXC_AUTO_CGROUP_FORCE },
1746 { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC },
1747 { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED },
1748 { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO },
1749 { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW },
1750 { "cgroup-full:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC | LXC_AUTO_CGROUP_FORCE },
1751 { "cgroup-full:mixed:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED | LXC_AUTO_CGROUP_FORCE },
1752 { "cgroup-full:ro:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO | LXC_AUTO_CGROUP_FORCE },
1753 { "cgroup-full:rw:force", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW | LXC_AUTO_CGROUP_FORCE },
1754 /* For adding anything that is just a single on/off, but has no
1755 * options: keep mask and flag identical and just define the enum
1756 * value as an unused bit so far
1757 */
1758 { NULL, 0, 0 }
1759 };
1760
1761 if (lxc_config_value_empty(value)) {
1762 lxc_conf->auto_mounts = 0;
1763 return 0;
1764 }
1765
1766 autos = strdup(value);
1767 if (!autos)
1768 return -1;
1769
1770 for (autoptr = autos;; autoptr = NULL) {
1771 token = strtok_r(autoptr, " \t", &sptr);
1772 if (!token) {
1773 ret = 0;
1774 break;
1775 }
1776
1777 for (i = 0; allowed_auto_mounts[i].token; i++) {
1778 if (!strcmp(allowed_auto_mounts[i].token, token))
1779 break;
1780 }
1781
1782 if (!allowed_auto_mounts[i].token) {
1783 ERROR("Invalid filesystem to automount \"%s\"", token);
1784 break;
1785 }
1786
1787 lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask;
1788 lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag;
1789 }
1790
1791 free(autos);
1792 return ret;
1793 }
1794
1795 static int set_config_mount(const char *key, const char *value,
1796 struct lxc_conf *lxc_conf, void *data)
1797 {
1798 char *mntelem;
1799 struct lxc_list *mntlist;
1800
1801 if (lxc_config_value_empty(value))
1802 return lxc_clear_mount_entries(lxc_conf);
1803
1804 mntlist = malloc(sizeof(*mntlist));
1805 if (!mntlist)
1806 return -1;
1807
1808 mntelem = strdup(value);
1809 if (!mntelem) {
1810 free(mntlist);
1811 return -1;
1812 }
1813 mntlist->elem = mntelem;
1814
1815 lxc_list_add_tail(&lxc_conf->mount_list, mntlist);
1816
1817 return 0;
1818 }
1819
1820 static int set_config_cap_keep(const char *key, const char *value,
1821 struct lxc_conf *lxc_conf, void *data)
1822 {
1823 char *keepcaps, *keepptr, *sptr, *token;
1824 struct lxc_list *keeplist;
1825 int ret = -1;
1826
1827 if (lxc_config_value_empty(value))
1828 return lxc_clear_config_keepcaps(lxc_conf);
1829
1830 keepcaps = strdup(value);
1831 if (!keepcaps)
1832 return -1;
1833
1834 /* In case several capability keep is specified in a single line
1835 * split these caps in a single element for the list.
1836 */
1837 for (keepptr = keepcaps;; keepptr = NULL) {
1838 token = strtok_r(keepptr, " \t", &sptr);
1839 if (!token) {
1840 ret = 0;
1841 break;
1842 }
1843
1844 if (!strcmp(token, "none"))
1845 lxc_clear_config_keepcaps(lxc_conf);
1846
1847 keeplist = malloc(sizeof(*keeplist));
1848 if (!keeplist)
1849 break;
1850
1851 keeplist->elem = strdup(token);
1852 if (!keeplist->elem) {
1853 free(keeplist);
1854 break;
1855 }
1856
1857 lxc_list_add_tail(&lxc_conf->keepcaps, keeplist);
1858 }
1859
1860 free(keepcaps);
1861
1862 return ret;
1863 }
1864
1865 static int set_config_cap_drop(const char *key, const char *value,
1866 struct lxc_conf *lxc_conf, void *data)
1867 {
1868 char *dropcaps, *dropptr, *sptr, *token;
1869 struct lxc_list *droplist;
1870 int ret = -1;
1871
1872 if (lxc_config_value_empty(value))
1873 return lxc_clear_config_caps(lxc_conf);
1874
1875 dropcaps = strdup(value);
1876 if (!dropcaps)
1877 return -1;
1878
1879 /* In case several capability drop is specified in a single line
1880 * split these caps in a single element for the list.
1881 */
1882 for (dropptr = dropcaps;; dropptr = NULL) {
1883 token = strtok_r(dropptr, " \t", &sptr);
1884 if (!token) {
1885 ret = 0;
1886 break;
1887 }
1888
1889 droplist = malloc(sizeof(*droplist));
1890 if (!droplist)
1891 break;
1892
1893 droplist->elem = strdup(token);
1894 if (!droplist->elem) {
1895 free(droplist);
1896 break;
1897 }
1898
1899 lxc_list_add_tail(&lxc_conf->caps, droplist);
1900 }
1901
1902 free(dropcaps);
1903
1904 return ret;
1905 }
1906
1907 static int set_config_console_path(const char *key, const char *value,
1908 struct lxc_conf *lxc_conf, void *data)
1909 {
1910 return set_config_path_item(&lxc_conf->console.path, value);
1911 }
1912
1913 static int set_config_console_rotate(const char *key, const char *value,
1914 struct lxc_conf *lxc_conf, void *data)
1915 {
1916 if (lxc_config_value_empty(value)) {
1917 lxc_conf->console.log_rotate = 0;
1918 return 0;
1919 }
1920
1921 if (lxc_safe_uint(value, &lxc_conf->console.log_rotate) < 0)
1922 return -1;
1923
1924 if (lxc_conf->console.log_rotate > 1) {
1925 ERROR("The \"lxc.console.rotate\" config key can only be set "
1926 "to 0 or 1");
1927 return -1;
1928 }
1929
1930 return 0;
1931 }
1932
1933 static int set_config_console_logfile(const char *key, const char *value,
1934 struct lxc_conf *lxc_conf, void *data)
1935 {
1936 return set_config_path_item(&lxc_conf->console.log_path, value);
1937 }
1938
1939 static int set_config_console_buffer_size(const char *key, const char *value,
1940 struct lxc_conf *lxc_conf, void *data)
1941 {
1942 int ret;
1943 int64_t size;
1944 uint64_t buffer_size, pgsz;
1945
1946 if (lxc_config_value_empty(value)) {
1947 lxc_conf->console.buffer_size = 0;
1948 return 0;
1949 }
1950
1951 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1952 if (!strcmp(value, "auto")) {
1953 lxc_conf->console.buffer_size = 1 << 17;
1954 return 0;
1955 }
1956
1957 ret = parse_byte_size_string(value, &size);
1958 if (ret < 0)
1959 return -1;
1960
1961 if (size < 0)
1962 return -EINVAL;
1963
1964 /* must be at least a page size */
1965 pgsz = lxc_getpagesize();
1966 if ((uint64_t)size < pgsz) {
1967 NOTICE("Requested ringbuffer size for the console is %" PRId64
1968 " but must be at least %" PRId64
1969 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
1970 size, pgsz, pgsz);
1971 size = pgsz;
1972 }
1973
1974 buffer_size = lxc_find_next_power2((uint64_t)size);
1975 if (buffer_size == 0)
1976 return -EINVAL;
1977
1978 if (buffer_size != size)
1979 NOTICE("Passed size was not a power of 2. Rounding log size to "
1980 "next power of two: %" PRIu64 " bytes", buffer_size);
1981
1982 lxc_conf->console.buffer_size = buffer_size;
1983 return 0;
1984 }
1985
1986 static int set_config_console_size(const char *key, const char *value,
1987 struct lxc_conf *lxc_conf, void *data)
1988 {
1989 int ret;
1990 int64_t size;
1991 uint64_t log_size, pgsz;
1992
1993 if (lxc_config_value_empty(value)) {
1994 lxc_conf->console.log_size = 0;
1995 return 0;
1996 }
1997
1998 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1999 if (!strcmp(value, "auto")) {
2000 lxc_conf->console.log_size = 1 << 17;
2001 return 0;
2002 }
2003
2004 ret = parse_byte_size_string(value, &size);
2005 if (ret < 0)
2006 return -1;
2007
2008 if (size < 0)
2009 return -EINVAL;
2010
2011 /* must be at least a page size */
2012 pgsz = lxc_getpagesize();
2013 if ((uint64_t)size < pgsz) {
2014 NOTICE("Requested ringbuffer size for the console is %" PRId64
2015 " but must be at least %" PRId64
2016 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
2017 size, pgsz, pgsz);
2018 size = pgsz;
2019 }
2020
2021 log_size = lxc_find_next_power2((uint64_t)size);
2022 if (log_size == 0)
2023 return -EINVAL;
2024
2025 if (log_size != size)
2026 NOTICE("Passed size was not a power of 2. Rounding log size to "
2027 "next power of two: %" PRIu64 " bytes", log_size);
2028
2029 lxc_conf->console.log_size = log_size;
2030 return 0;
2031 }
2032
2033 int append_unexp_config_line(const char *line, struct lxc_conf *conf)
2034 {
2035 size_t len = conf->unexpanded_len, linelen = strlen(line);
2036
2037 update_hwaddr(line);
2038
2039 while (conf->unexpanded_alloced <= len + linelen + 2) {
2040 char *tmp = realloc(conf->unexpanded_config,
2041 conf->unexpanded_alloced + 1024);
2042 if (!tmp)
2043 return -1;
2044
2045 if (!conf->unexpanded_config)
2046 *tmp = '\0';
2047 conf->unexpanded_config = tmp;
2048 conf->unexpanded_alloced += 1024;
2049 }
2050 strcat(conf->unexpanded_config, line);
2051 conf->unexpanded_len += linelen;
2052 if (line[linelen - 1] != '\n') {
2053 strcat(conf->unexpanded_config, "\n");
2054 conf->unexpanded_len++;
2055 }
2056
2057 return 0;
2058 }
2059
2060 static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf)
2061 {
2062 struct dirent *direntp;
2063 DIR *dir;
2064 char path[MAXPATHLEN];
2065 int len;
2066 int ret = -1;
2067
2068 dir = opendir(dirp);
2069 if (!dir)
2070 return -1;
2071
2072 while ((direntp = readdir(dir))) {
2073 const char *fnam;
2074 if (!direntp)
2075 break;
2076
2077 fnam = direntp->d_name;
2078 if (!strcmp(fnam, "."))
2079 continue;
2080
2081 if (!strcmp(fnam, ".."))
2082 continue;
2083
2084 len = strlen(fnam);
2085 if (len < 6 || strncmp(fnam + len - 5, ".conf", 5) != 0)
2086 continue;
2087
2088 len = snprintf(path, MAXPATHLEN, "%s/%s", dirp, fnam);
2089 if (len < 0 || len >= MAXPATHLEN) {
2090 ret = -1;
2091 goto out;
2092 }
2093
2094 ret = lxc_config_read(path, lxc_conf, true);
2095 if (ret < 0)
2096 goto out;
2097 }
2098 ret = 0;
2099
2100 out:
2101 closedir(dir);
2102
2103 return ret;
2104 }
2105
2106 static int set_config_includefiles(const char *key, const char *value,
2107 struct lxc_conf *lxc_conf, void *data)
2108 {
2109 if (lxc_config_value_empty(value)) {
2110 clr_config_includefiles(key, lxc_conf, NULL);
2111 return 0;
2112 }
2113
2114 if (is_dir(value))
2115 return do_includedir(value, lxc_conf);
2116
2117 return lxc_config_read(value, lxc_conf, true);
2118 }
2119
2120 static int set_config_rootfs_path(const char *key, const char *value,
2121 struct lxc_conf *lxc_conf, void *data)
2122 {
2123 int ret;
2124 char *dup, *tmp;
2125 const char *container_path;
2126
2127 if (lxc_config_value_empty(value)) {
2128 free(lxc_conf->rootfs.path);
2129 lxc_conf->rootfs.path = NULL;
2130 return 0;
2131 }
2132
2133 dup = strdup(value);
2134 if (!dup)
2135 return -1;
2136
2137 /* Split <storage type>:<container path> into <storage type> and
2138 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2139 * "rootfs.path" to <container path>.
2140 */
2141 tmp = strchr(dup, ':');
2142 if (tmp) {
2143 *tmp = '\0';
2144 ret = set_config_path_item(&lxc_conf->rootfs.bdev_type, dup);
2145 if (ret < 0) {
2146 free(dup);
2147 return -1;
2148 }
2149 tmp++;
2150 container_path = tmp;
2151 } else {
2152 container_path = value;
2153 }
2154
2155 ret = set_config_path_item(&lxc_conf->rootfs.path, container_path);
2156 free(dup);
2157 return ret;
2158 }
2159
2160 static int set_config_rootfs_mount(const char *key, const char *value,
2161 struct lxc_conf *lxc_conf, void *data)
2162 {
2163 return set_config_path_item(&lxc_conf->rootfs.mount, value);
2164 }
2165
2166 static int set_config_rootfs_options(const char *key, const char *value,
2167 struct lxc_conf *lxc_conf, void *data)
2168 {
2169 return set_config_string_item(&lxc_conf->rootfs.options, value);
2170 }
2171
2172 static int set_config_uts_name(const char *key, const char *value,
2173 struct lxc_conf *lxc_conf, void *data)
2174 {
2175 struct utsname *utsname;
2176
2177 if (lxc_config_value_empty(value)) {
2178 clr_config_uts_name(key, lxc_conf, NULL);
2179 return 0;
2180 }
2181
2182 utsname = malloc(sizeof(*utsname));
2183 if (!utsname)
2184 return -1;
2185
2186 if (strlen(value) >= sizeof(utsname->nodename)) {
2187 free(utsname);
2188 return -1;
2189 }
2190
2191 strcpy(utsname->nodename, value);
2192 free(lxc_conf->utsname);
2193 lxc_conf->utsname = utsname;
2194
2195 return 0;
2196 }
2197
2198 static int set_config_namespace_clone(const char *key, const char *value,
2199 struct lxc_conf *lxc_conf, void *data)
2200 {
2201 char *ns, *nsptr, *token;
2202 int cloneflag = 0;
2203 char *saveptr = NULL;
2204
2205 if (lxc_config_value_empty(value))
2206 return clr_config_namespace_clone(key, lxc_conf, data);
2207
2208 if (lxc_conf->ns_keep != 0) {
2209 ERROR("%s - Cannot set both \"lxc.namespace.clone\" and "
2210 "\"lxc.namespace.keep\"", strerror(EINVAL));
2211 return -EINVAL;
2212 }
2213
2214 ns = strdup(value);
2215 if (!ns)
2216 return -1;
2217 nsptr = ns;
2218
2219 for (; (token = strtok_r(nsptr, " \t", &saveptr)); nsptr = NULL) {
2220 token += lxc_char_left_gc(token, strlen(token));
2221 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2222 cloneflag = lxc_namespace_2_cloneflag(token);
2223 if (cloneflag < 0) {
2224 free(ns);
2225 return -EINVAL;
2226 }
2227 lxc_conf->ns_clone |= cloneflag;
2228 }
2229 free(ns);
2230
2231 return 0;
2232 }
2233
2234 static int set_config_namespace_keep(const char *key, const char *value,
2235 struct lxc_conf *lxc_conf, void *data)
2236 {
2237 char *ns, *nsptr, *token;
2238 int cloneflag = 0;
2239 char *saveptr = NULL;
2240
2241 if (lxc_config_value_empty(value))
2242 return clr_config_namespace_keep(key, lxc_conf, data);
2243
2244 if (lxc_conf->ns_clone != 0) {
2245 ERROR("%s - Cannot set both \"lxc.namespace.clone\" and "
2246 "\"lxc.namespace.keep\"", strerror(EINVAL));
2247 return -EINVAL;
2248 }
2249
2250 ns = strdup(value);
2251 if (!ns)
2252 return -1;
2253 nsptr = ns;
2254
2255 for (; (token = strtok_r(nsptr, " \t", &saveptr)); nsptr = NULL) {
2256 token += lxc_char_left_gc(token, strlen(token));
2257 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2258 cloneflag = lxc_namespace_2_cloneflag(token);
2259 if (cloneflag < 0) {
2260 free(ns);
2261 return -EINVAL;
2262 }
2263 lxc_conf->ns_keep |= cloneflag;
2264 }
2265 free(ns);
2266
2267 return 0;
2268 }
2269
2270 static int set_config_namespace_share(const char *key, const char *value,
2271 struct lxc_conf *lxc_conf, void *data)
2272 {
2273 int ns_idx;
2274 const char *namespace;
2275
2276 if (lxc_config_value_empty(value))
2277 return clr_config_namespace_share(key, lxc_conf, data);
2278
2279 namespace = key + sizeof("lxc.namespace.share.") - 1;
2280 ns_idx = lxc_namespace_2_ns_idx(namespace);
2281 if (ns_idx < 0)
2282 return ns_idx;
2283
2284 return set_config_string_item(&lxc_conf->ns_share[ns_idx], value);
2285 }
2286
2287 struct parse_line_conf {
2288 struct lxc_conf *conf;
2289 bool from_include;
2290 };
2291
2292 static int parse_line(char *buffer, void *data)
2293 {
2294 char *dot, *key, *line, *linep, *value;
2295 bool empty_line;
2296 struct lxc_config_t *config;
2297 int ret = 0;
2298 char *dup = buffer;
2299 struct parse_line_conf *plc = data;
2300
2301 /* If there are newlines in the config file we should keep them. */
2302 empty_line = lxc_is_line_empty(dup);
2303 if (empty_line)
2304 dup = "\n";
2305
2306 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2307 * modified the original string on the stack by replacing '=' by '\0'
2308 * below.
2309 */
2310 linep = line = strdup(dup);
2311 if (!line)
2312 return -1;
2313
2314 if (!plc->from_include) {
2315 ret = append_unexp_config_line(line, plc->conf);
2316 if (ret < 0)
2317 goto on_error;
2318 }
2319
2320 if (empty_line)
2321 goto on_error;
2322
2323 line += lxc_char_left_gc(line, strlen(line));
2324
2325 /* ignore comments */
2326 if (line[0] == '#')
2327 goto on_error;
2328
2329 /* martian option - don't add it to the config itself */
2330 if (strncmp(line, "lxc.", 4))
2331 goto on_error;
2332
2333 ret = -1;
2334
2335 dot = strchr(line, '=');
2336 if (!dot) {
2337 ERROR("Invalid configuration line: %s", line);
2338 goto on_error;
2339 }
2340
2341 *dot = '\0';
2342 value = dot + 1;
2343
2344 key = line;
2345 key[lxc_char_right_gc(key, strlen(key))] = '\0';
2346
2347 value += lxc_char_left_gc(value, strlen(value));
2348 value[lxc_char_right_gc(value, strlen(value))] = '\0';
2349
2350 if (*value == '\'' || *value == '\"') {
2351 size_t len;
2352
2353 len = strlen(value);
2354 if (len > 1 && value[len - 1] == *value) {
2355 value[len - 1] = '\0';
2356 value++;
2357 }
2358 }
2359
2360 config = lxc_get_config(key);
2361 if (!config) {
2362 ERROR("Unknown configuration key \"%s\"", key);
2363 goto on_error;
2364 }
2365
2366 ret = config->set(key, value, plc->conf, NULL);
2367
2368 on_error:
2369 free(linep);
2370 return ret;
2371 }
2372
2373 static int lxc_config_readline(char *buffer, struct lxc_conf *conf)
2374 {
2375 struct parse_line_conf c;
2376
2377 c.conf = conf;
2378 c.from_include = false;
2379
2380 return parse_line(buffer, &c);
2381 }
2382
2383 int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
2384 {
2385 int ret;
2386 struct parse_line_conf c;
2387
2388 c.conf = conf;
2389 c.from_include = from_include;
2390
2391 ret = access(file, R_OK);
2392 if (ret < 0)
2393 return -1;
2394
2395 /* Catch only the top level config file name in the structure. */
2396 if (!conf->rcfile)
2397 conf->rcfile = strdup(file);
2398
2399 return lxc_file_for_each_line_mmap(file, parse_line, &c);
2400 }
2401
2402 int lxc_config_define_add(struct lxc_list *defines, char *arg)
2403 {
2404 struct lxc_list *dent;
2405
2406 dent = malloc(sizeof(struct lxc_list));
2407 if (!dent)
2408 return -1;
2409
2410 dent->elem = arg;
2411 lxc_list_add_tail(defines, dent);
2412 return 0;
2413 }
2414
2415 int lxc_config_define_load(struct lxc_list *defines, struct lxc_conf *conf)
2416 {
2417 struct lxc_list *it, *next;
2418 int ret = 0;
2419
2420 lxc_list_for_each(it, defines) {
2421 ret = lxc_config_readline(it->elem, conf);
2422 if (ret)
2423 break;
2424 }
2425
2426 lxc_list_for_each_safe(it, defines, next) {
2427 lxc_list_del(it);
2428 free(it);
2429 }
2430
2431 return ret;
2432 }
2433
2434 signed long lxc_config_parse_arch(const char *arch)
2435 {
2436 #if HAVE_SYS_PERSONALITY_H
2437 size_t i;
2438 struct per_name {
2439 char *name;
2440 unsigned long per;
2441 } pername[] = {
2442 { "arm", PER_LINUX32 },
2443 { "armel", PER_LINUX32 },
2444 { "armhf", PER_LINUX32 },
2445 { "armv7l", PER_LINUX32 },
2446 { "athlon", PER_LINUX32 },
2447 { "i386", PER_LINUX32 },
2448 { "i486", PER_LINUX32 },
2449 { "i586", PER_LINUX32 },
2450 { "i686", PER_LINUX32 },
2451 { "linux32", PER_LINUX32 },
2452 { "mips", PER_LINUX32 },
2453 { "mipsel", PER_LINUX32 },
2454 { "ppc", PER_LINUX32 },
2455 { "powerpc", PER_LINUX32 },
2456 { "x86", PER_LINUX32 },
2457 { "amd64", PER_LINUX },
2458 { "arm64", PER_LINUX },
2459 { "linux64", PER_LINUX },
2460 { "mips64", PER_LINUX },
2461 { "mips64el", PER_LINUX },
2462 { "ppc64", PER_LINUX },
2463 { "ppc64el", PER_LINUX },
2464 { "ppc64le", PER_LINUX },
2465 { "powerpc64", PER_LINUX },
2466 { "s390x", PER_LINUX },
2467 { "x86_64", PER_LINUX },
2468 };
2469 size_t len = sizeof(pername) / sizeof(pername[0]);
2470
2471 for (i = 0; i < len; i++)
2472 if (!strcmp(pername[i].name, arch))
2473 return pername[i].per;
2474 #endif
2475
2476 return -1;
2477 }
2478
2479 /* Write out a configuration file. */
2480 int write_config(int fd, const struct lxc_conf *conf)
2481 {
2482 int ret;
2483 size_t len = conf->unexpanded_len;
2484
2485 if (len == 0)
2486 return 0;
2487
2488 ret = lxc_write_nointr(fd, conf->unexpanded_config, len);
2489 if (ret < 0) {
2490 SYSERROR("Failed to write configuration file");
2491 return -1;
2492 }
2493
2494 return 0;
2495 }
2496
2497 bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key,
2498 const char *v)
2499 {
2500 int ret;
2501 size_t len;
2502 char *tmp;
2503
2504 len = strlen(key) + strlen(v) + 4;
2505 tmp = alloca(len);
2506
2507 if (lxc_config_value_empty(v))
2508 ret = snprintf(tmp, len, "%s =", key);
2509 else
2510 ret = snprintf(tmp, len, "%s = %s", key, v);
2511 if (ret < 0 || ret >= len)
2512 return false;
2513
2514 /* Save the line verbatim into unexpanded_conf */
2515 if (append_unexp_config_line(tmp, conf))
2516 return false;
2517
2518 return true;
2519 }
2520
2521 void clear_unexp_config_line(struct lxc_conf *conf, const char *key,
2522 bool rm_subkeys)
2523 {
2524 char *lend;
2525 char *lstart = conf->unexpanded_config;
2526
2527 if (!conf->unexpanded_config)
2528 return;
2529
2530 while (*lstart) {
2531 lend = strchr(lstart, '\n');
2532 char v;
2533 if (!lend)
2534 lend = lstart + strlen(lstart);
2535 else
2536 lend++;
2537 if (strncmp(lstart, key, strlen(key)) != 0) {
2538 lstart = lend;
2539 continue;
2540 }
2541 if (!rm_subkeys) {
2542 v = lstart[strlen(key)];
2543 if (!isspace(v) && v != '=') {
2544 lstart = lend;
2545 continue;
2546 }
2547 }
2548 conf->unexpanded_len -= (lend - lstart);
2549 if (*lend == '\0') {
2550 *lstart = '\0';
2551 return;
2552 }
2553 memmove(lstart, lend, strlen(lend) + 1);
2554 }
2555 }
2556
2557 bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
2558 const char *newpath, const char *oldname,
2559 const char *newname, const char *ovldir)
2560 {
2561 int ret;
2562 char *lend, *newdir, *olddir, *p, *q;
2563 size_t newdirlen, olddirlen;
2564 char *lstart = conf->unexpanded_config;
2565 const char *key = "lxc.mount.entry";
2566
2567 olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
2568 olddir = alloca(olddirlen + 1);
2569 ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath,
2570 oldname);
2571 if (ret < 0 || ret >= olddirlen + 1)
2572 return false;
2573
2574 newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
2575 newdir = alloca(newdirlen + 1);
2576 ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath,
2577 newname);
2578 if (ret < 0 || ret >= newdirlen + 1)
2579 return false;
2580
2581 if (!conf->unexpanded_config)
2582 return true;
2583
2584 while (*lstart) {
2585 lend = strchr(lstart, '\n');
2586 if (!lend)
2587 lend = lstart + strlen(lstart);
2588 else
2589 lend++;
2590
2591 if (strncmp(lstart, key, strlen(key)) != 0)
2592 goto next;
2593
2594 p = strchr(lstart + strlen(key), '=');
2595 if (!p)
2596 goto next;
2597 p++;
2598
2599 while (isblank(*p))
2600 p++;
2601
2602 if (p >= lend)
2603 goto next;
2604
2605 /* Whenever a lxc.mount.entry entry is found in a line we check
2606 * if the substring "overlay" is present before doing any
2607 * further work. We check for "overlay" because substrings need
2608 * to have at least one space before them in a valid overlay
2609 * lxc.mount.entry (/A B overlay). When the space before is
2610 * missing it is very likely that these substrings are part of a
2611 * path or something else. (Checking q >= lend ensures that we
2612 * only count matches in the current line.) */
2613 q = strstr(p, " overlay");
2614 if (!q || q >= lend)
2615 goto next;
2616
2617 if (!(q = strstr(p, olddir)) || (q >= lend))
2618 goto next;
2619
2620 /* replace the olddir with newdir */
2621 if (olddirlen >= newdirlen) {
2622 size_t diff = olddirlen - newdirlen;
2623 memcpy(q, newdir, newdirlen);
2624 if (olddirlen != newdirlen) {
2625 memmove(q + newdirlen, q + newdirlen + diff,
2626 strlen(q) - newdirlen - diff + 1);
2627 lend -= diff;
2628 conf->unexpanded_len -= diff;
2629 }
2630 } else {
2631 char *new;
2632 size_t diff = newdirlen - olddirlen;
2633 size_t oldlen = conf->unexpanded_len;
2634 size_t newlen = oldlen + diff;
2635 size_t poffset = q - conf->unexpanded_config;
2636
2637 new = realloc(conf->unexpanded_config, newlen + 1);
2638 if (!new)
2639 return false;
2640
2641 conf->unexpanded_len = newlen;
2642 conf->unexpanded_alloced = newlen + 1;
2643 new[newlen - 1] = '\0';
2644 lend = new + (lend - conf->unexpanded_config);
2645 /* Move over the remainder to make room for the newdir.
2646 */
2647 memmove(new + poffset + newdirlen,
2648 new + poffset + olddirlen,
2649 oldlen - poffset - olddirlen + 1);
2650 conf->unexpanded_config = new;
2651 memcpy(new + poffset, newdir, newdirlen);
2652 lend += diff;
2653 }
2654 next:
2655 lstart = lend;
2656 }
2657
2658 return true;
2659 }
2660
2661 bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
2662 const char *newpath, const char *oldname,
2663 const char *newname)
2664 {
2665 int ret;
2666 char *lend, *newdir, *olddir, *p;
2667 char *lstart = conf->unexpanded_config;
2668 size_t newdirlen, olddirlen;
2669 const char *key = "lxc.hook";
2670
2671 olddirlen = strlen(oldpath) + strlen(oldname) + 1;
2672 olddir = alloca(olddirlen + 1);
2673 ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
2674 if (ret < 0 || ret >= olddirlen + 1)
2675 return false;
2676
2677 newdirlen = strlen(newpath) + strlen(newname) + 1;
2678 newdir = alloca(newdirlen + 1);
2679 ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
2680 if (ret < 0 || ret >= newdirlen + 1)
2681 return false;
2682
2683 if (!conf->unexpanded_config)
2684 return true;
2685
2686 while (*lstart) {
2687 lend = strchr(lstart, '\n');
2688 if (!lend)
2689 lend = lstart + strlen(lstart);
2690 else
2691 lend++;
2692
2693 if (strncmp(lstart, key, strlen(key)) != 0)
2694 goto next;
2695
2696 p = strchr(lstart + strlen(key), '=');
2697 if (!p)
2698 goto next;
2699 p++;
2700
2701 while (isblank(*p))
2702 p++;
2703
2704 if (p >= lend)
2705 goto next;
2706
2707 if (strncmp(p, olddir, strlen(olddir)) != 0)
2708 goto next;
2709
2710 /* replace the olddir with newdir */
2711 if (olddirlen >= newdirlen) {
2712 size_t diff = olddirlen - newdirlen;
2713 memcpy(p, newdir, newdirlen);
2714 if (olddirlen != newdirlen) {
2715 memmove(p + newdirlen, p + newdirlen + diff,
2716 strlen(p) - newdirlen - diff + 1);
2717 lend -= diff;
2718 conf->unexpanded_len -= diff;
2719 }
2720 } else {
2721 char *new;
2722 size_t diff = newdirlen - olddirlen;
2723 size_t oldlen = conf->unexpanded_len;
2724 size_t newlen = oldlen + diff;
2725 size_t poffset = p - conf->unexpanded_config;
2726
2727 new = realloc(conf->unexpanded_config, newlen + 1);
2728 if (!new)
2729 return false;
2730
2731 conf->unexpanded_len = newlen;
2732 conf->unexpanded_alloced = newlen + 1;
2733 new[newlen - 1] = '\0';
2734 lend = new + (lend - conf->unexpanded_config);
2735 /* Move over the remainder to make room for the newdir.
2736 */
2737 memmove(new + poffset + newdirlen,
2738 new + poffset + olddirlen,
2739 oldlen - poffset - olddirlen + 1);
2740 conf->unexpanded_config = new;
2741 memcpy(new + poffset, newdir, newdirlen);
2742 lend += diff;
2743 }
2744 next:
2745 lstart = lend;
2746 }
2747
2748 return true;
2749 }
2750
2751 #define DO(cmd) \
2752 { \
2753 if (!(cmd)) { \
2754 ERROR("Error writing to new config"); \
2755 return false; \
2756 } \
2757 }
2758
2759 /* This is called only from clone. We wish to update all hwaddrs in the
2760 * unexpanded config file. We can't/don't want to update any which come from
2761 * lxc.includes (there shouldn't be any).
2762 * We can't just walk the c->lxc-conf->network list because that includes netifs
2763 * from the include files. So we update the ones which we find in the unexp
2764 * config file, then find the original macaddr in the conf->network, and update
2765 * that to the same value.
2766 */
2767 bool network_new_hwaddrs(struct lxc_conf *conf)
2768 {
2769 char *lend, *p, *p2;
2770 struct lxc_list *it;
2771 char *lstart = conf->unexpanded_config;
2772
2773 if (!conf->unexpanded_config)
2774 return true;
2775
2776 while (*lstart) {
2777 char newhwaddr[18], oldhwaddr[17];
2778
2779 lend = strchr(lstart, '\n');
2780 if (!lend)
2781 lend = lstart + strlen(lstart);
2782 else
2783 lend++;
2784
2785 if (!lxc_config_net_hwaddr(lstart)) {
2786 lstart = lend;
2787 continue;
2788 }
2789
2790 p = strchr(lstart, '=');
2791 if (!p) {
2792 lstart = lend;
2793 continue;
2794 }
2795
2796 p++;
2797 while (isblank(*p))
2798 p++;
2799 if (!*p)
2800 return true;
2801
2802 p2 = p;
2803 while (*p2 && !isblank(*p2) && *p2 != '\n')
2804 p2++;
2805
2806 if ((p2 - p) != 17) {
2807 WARN("Bad hwaddr entry");
2808 lstart = lend;
2809 continue;
2810 }
2811
2812 memcpy(oldhwaddr, p, 17);
2813
2814 if (!new_hwaddr(newhwaddr))
2815 return false;
2816
2817 memcpy(p, newhwaddr, 17);
2818 lxc_list_for_each(it, &conf->network) {
2819 struct lxc_netdev *n = it->elem;
2820
2821 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
2822 memcpy(n->hwaddr, newhwaddr, 17);
2823 }
2824
2825 lstart = lend;
2826 }
2827
2828 return true;
2829 }
2830
2831 static int set_config_ephemeral(const char *key, const char *value,
2832 struct lxc_conf *lxc_conf, void *data)
2833 {
2834 if (lxc_config_value_empty(value)) {
2835 lxc_conf->ephemeral = 0;
2836 return 0;
2837 }
2838
2839 if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0)
2840 return -1;
2841
2842 if (lxc_conf->ephemeral > 1)
2843 return -1;
2844
2845 return 0;
2846 }
2847
2848 static int set_config_log_syslog(const char *key, const char *value,
2849 struct lxc_conf *lxc_conf, void *data)
2850 {
2851 int facility;
2852
2853 if (lxc_conf->syslog) {
2854 free(lxc_conf->syslog);
2855 lxc_conf->syslog = NULL;
2856 }
2857
2858 if (lxc_config_value_empty(value))
2859 return 0;
2860
2861 facility = lxc_syslog_priority_to_int(value);
2862 if (facility == -EINVAL)
2863 return -1;
2864
2865 lxc_log_syslog(facility);
2866 return set_config_string_item(&lxc_conf->syslog, value);
2867 }
2868
2869 static int set_config_no_new_privs(const char *key, const char *value,
2870 struct lxc_conf *lxc_conf, void *data)
2871 {
2872 unsigned int v;
2873
2874 if (lxc_config_value_empty(value)) {
2875 lxc_conf->no_new_privs = false;
2876 return 0;
2877 }
2878
2879 if (lxc_safe_uint(value, &v) < 0)
2880 return -1;
2881
2882 if (v > 1)
2883 return -1;
2884
2885 lxc_conf->no_new_privs = v ? true : false;
2886
2887 return 0;
2888 }
2889
2890 /* Callbacks to get configuration items. */
2891 static int get_config_personality(const char *key, char *retv, int inlen,
2892 struct lxc_conf *c, void *data)
2893 {
2894 int fulllen = 0;
2895
2896 if (!retv)
2897 inlen = 0;
2898 else
2899 memset(retv, 0, inlen);
2900
2901 #if HAVE_SYS_PERSONALITY_H
2902 int len = 0;
2903
2904 switch (c->personality) {
2905 case PER_LINUX32:
2906 strprint(retv, inlen, "i686");
2907 break;
2908 case PER_LINUX:
2909 strprint(retv, inlen, "x86_64");
2910 break;
2911 default:
2912 break;
2913 }
2914 #endif
2915
2916 return fulllen;
2917 }
2918
2919 static int get_config_pty_max(const char *key, char *retv, int inlen,
2920 struct lxc_conf *c, void *data)
2921 {
2922 return lxc_get_conf_int(c, retv, inlen, c->pts);
2923 }
2924
2925 static int get_config_tty_max(const char *key, char *retv, int inlen,
2926 struct lxc_conf *c, void *data)
2927 {
2928 return lxc_get_conf_size_t(c, retv, inlen, c->ttys.max);
2929 }
2930
2931 static int get_config_tty_dir(const char *key, char *retv, int inlen,
2932 struct lxc_conf *c, void *data)
2933 {
2934 return lxc_get_conf_str(retv, inlen, c->ttys.dir);
2935 }
2936
2937 static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
2938 struct lxc_conf *c, void *data)
2939 {
2940 return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile);
2941 }
2942
2943 static int get_config_apparmor_allow_incomplete(const char *key, char *retv,
2944 int inlen, struct lxc_conf *c,
2945 void *data)
2946 {
2947 return lxc_get_conf_int(c, retv, inlen,
2948 c->lsm_aa_allow_incomplete);
2949 }
2950
2951 static int get_config_selinux_context(const char *key, char *retv, int inlen,
2952 struct lxc_conf *c, void *data)
2953 {
2954 return lxc_get_conf_str(retv, inlen, c->lsm_se_context);
2955 }
2956
2957 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
2958 * just the value(s) will be printed. Since there still could be more than one,
2959 * it is newline-separated.
2960 * (Maybe that's ambigous, since some values, i.e. devices.list, will already
2961 * have newlines?)
2962 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
2963 * 'lxc.cgroup.subsystem.key = value' format.
2964 */
2965 static int __get_config_cgroup_controller(const char *key, char *retv,
2966 int inlen, struct lxc_conf *c,
2967 int version)
2968 {
2969 int len;
2970 size_t namespaced_token_len;
2971 char *global_token, *namespaced_token;
2972 struct lxc_list *it;
2973 int fulllen = 0;
2974 bool get_all = false;
2975
2976 if (!retv)
2977 inlen = 0;
2978 else
2979 memset(retv, 0, inlen);
2980
2981 if (version == CGROUP2_SUPER_MAGIC) {
2982 global_token = "lxc.cgroup2";
2983 namespaced_token = "lxc.cgroup2.";
2984 namespaced_token_len = sizeof("lxc.cgroup2.") - 1;;
2985 } else if (version == CGROUP_SUPER_MAGIC) {
2986 global_token = "lxc.cgroup";
2987 namespaced_token = "lxc.cgroup.";
2988 namespaced_token_len = sizeof("lxc.cgroup.") - 1;;
2989 } else {
2990 return -1;
2991 }
2992
2993 if (strcmp(key, global_token) == 0)
2994 get_all = true;
2995 else if (strncmp(key, namespaced_token, namespaced_token_len) == 0)
2996 key += namespaced_token_len;
2997 else
2998 return -1;
2999
3000 lxc_list_for_each(it, &c->cgroup) {
3001 struct lxc_cgroup *cg = it->elem;
3002
3003 if (get_all) {
3004 if (version != cg->version)
3005 continue;
3006
3007 strprint(retv, inlen, "%s.%s = %s\n",
3008 global_token, cg->subsystem, cg->value);
3009 } else if (!strcmp(cg->subsystem, key)) {
3010 strprint(retv, inlen, "%s\n", cg->value);
3011 }
3012 }
3013
3014 return fulllen;
3015 }
3016
3017 static int get_config_cgroup_controller(const char *key, char *retv, int inlen,
3018 struct lxc_conf *c, void *data)
3019 {
3020 return __get_config_cgroup_controller(key, retv, inlen, c,
3021 CGROUP_SUPER_MAGIC);
3022 }
3023
3024 static int get_config_cgroup2_controller(const char *key, char *retv, int inlen,
3025 struct lxc_conf *c, void *data)
3026 {
3027 return __get_config_cgroup_controller(key, retv, inlen, c,
3028 CGROUP2_SUPER_MAGIC);
3029 }
3030
3031 static int get_config_cgroup_dir(const char *key, char *retv, int inlen,
3032 struct lxc_conf *lxc_conf, void *data)
3033 {
3034 int len;
3035 int fulllen = 0;
3036
3037 if (!retv)
3038 inlen = 0;
3039 else
3040 memset(retv, 0, inlen);
3041
3042 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir);
3043
3044 return fulllen;
3045 }
3046
3047 static int get_config_idmaps(const char *key, char *retv, int inlen,
3048 struct lxc_conf *c, void *data)
3049 {
3050 struct lxc_list *it;
3051 int len, listlen, ret;
3052 int fulllen = 0;
3053 /* "u 1000 1000000 65536"
3054 *
3055 * let's render this as
3056 *
3057 * sizeof(char)
3058 * +
3059 * sizeof(" ")
3060 * +
3061 * sizeof(uint64_t)
3062 * +
3063 * sizeof(" ")
3064 * +
3065 * sizeof(uint64_t)
3066 * +
3067 * sizeof(" ")
3068 * +
3069 * sizeof(uint64_t)
3070 * +
3071 * \0
3072 */
3073 #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1)
3074 char buf[__LXC_IDMAP_STR_BUF];
3075
3076 if (!retv)
3077 inlen = 0;
3078 else
3079 memset(retv, 0, inlen);
3080
3081 listlen = lxc_list_len(&c->id_map);
3082 lxc_list_for_each(it, &c->id_map)
3083 {
3084 struct id_map *map = it->elem;
3085 ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu",
3086 (map->idtype == ID_TYPE_UID) ? 'u' : 'g',
3087 map->nsid, map->hostid, map->range);
3088 if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF)
3089 return -1;
3090
3091 strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : "");
3092 }
3093 return fulllen;
3094 }
3095
3096 static int get_config_log_level(const char *key, char *retv, int inlen,
3097 struct lxc_conf *c, void *data)
3098 {
3099 const char *v;
3100 v = lxc_log_priority_to_string(c->loglevel);
3101 return lxc_get_conf_str(retv, inlen, v);
3102 }
3103
3104 static int get_config_log_file(const char *key, char *retv, int inlen,
3105 struct lxc_conf *c, void *data)
3106 {
3107 return lxc_get_conf_str(retv, inlen, c->logfile);
3108 }
3109
3110 static int get_config_mount_fstab(const char *key, char *retv, int inlen,
3111 struct lxc_conf *c, void *data)
3112 {
3113 return lxc_get_conf_str(retv, inlen, c->fstab);
3114 }
3115
3116 static int get_config_mount_auto(const char *key, char *retv, int inlen,
3117 struct lxc_conf *c, void *data)
3118 {
3119 int len, fulllen = 0;
3120 const char *sep = "";
3121
3122 if (!retv)
3123 inlen = 0;
3124 else
3125 memset(retv, 0, inlen);
3126
3127 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
3128 return 0;
3129
3130 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
3131 case LXC_AUTO_PROC_MIXED:
3132 strprint(retv, inlen, "%sproc:mixed", sep);
3133 sep = " ";
3134 break;
3135 case LXC_AUTO_PROC_RW:
3136 strprint(retv, inlen, "%sproc:rw", sep);
3137 sep = " ";
3138 break;
3139 default:
3140 break;
3141 }
3142
3143 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
3144 case LXC_AUTO_SYS_RO:
3145 strprint(retv, inlen, "%ssys:ro", sep);
3146 sep = " ";
3147 break;
3148 case LXC_AUTO_SYS_RW:
3149 strprint(retv, inlen, "%ssys:rw", sep);
3150 sep = " ";
3151 break;
3152 case LXC_AUTO_SYS_MIXED:
3153 strprint(retv, inlen, "%ssys:mixed", sep);
3154 sep = " ";
3155 break;
3156 default:
3157 break;
3158 }
3159
3160 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
3161 case LXC_AUTO_CGROUP_NOSPEC:
3162 strprint(retv, inlen, "%scgroup", sep);
3163 break;
3164 case LXC_AUTO_CGROUP_MIXED:
3165 strprint(retv, inlen, "%scgroup:mixed", sep);
3166 break;
3167 case LXC_AUTO_CGROUP_RO:
3168 strprint(retv, inlen, "%scgroup:ro", sep);
3169 break;
3170 case LXC_AUTO_CGROUP_RW:
3171 strprint(retv, inlen, "%scgroup:rw", sep);
3172 break;
3173 case LXC_AUTO_CGROUP_FULL_NOSPEC:
3174 strprint(retv, inlen, "%scgroup-full", sep);
3175 break;
3176 case LXC_AUTO_CGROUP_FULL_MIXED:
3177 strprint(retv, inlen, "%scgroup-full:mixed", sep);
3178 break;
3179 case LXC_AUTO_CGROUP_FULL_RO:
3180 strprint(retv, inlen, "%scgroup-full:ro", sep);
3181 break;
3182 case LXC_AUTO_CGROUP_FULL_RW:
3183 strprint(retv, inlen, "%scgroup-full:rw", sep);
3184 break;
3185 default:
3186 break;
3187 }
3188
3189 return fulllen;
3190 }
3191
3192 static int get_config_mount(const char *key, char *retv, int inlen,
3193 struct lxc_conf *c, void *data)
3194 {
3195 int len, fulllen = 0;
3196 struct lxc_list *it;
3197
3198 if (!retv)
3199 inlen = 0;
3200 else
3201 memset(retv, 0, inlen);
3202
3203 lxc_list_for_each(it, &c->mount_list) {
3204 strprint(retv, inlen, "%s\n", (char *)it->elem);
3205 }
3206
3207 return fulllen;
3208 }
3209
3210 static int get_config_rootfs_path(const char *key, char *retv, int inlen,
3211 struct lxc_conf *c, void *data)
3212 {
3213 return lxc_get_conf_str(retv, inlen, c->rootfs.path);
3214 }
3215
3216 static int get_config_rootfs_mount(const char *key, char *retv, int inlen,
3217 struct lxc_conf *c, void *data)
3218 {
3219 return lxc_get_conf_str(retv, inlen, c->rootfs.mount);
3220 }
3221
3222 static int get_config_rootfs_options(const char *key, char *retv, int inlen,
3223 struct lxc_conf *c, void *data)
3224 {
3225 return lxc_get_conf_str(retv, inlen, c->rootfs.options);
3226 }
3227
3228 static int get_config_uts_name(const char *key, char *retv, int inlen,
3229 struct lxc_conf *c, void *data)
3230 {
3231 return lxc_get_conf_str(
3232 retv, inlen,
3233 c->utsname ? c->utsname->nodename : NULL);
3234 }
3235
3236 static int get_config_hooks(const char *key, char *retv, int inlen,
3237 struct lxc_conf *c, void *data)
3238 {
3239 char *subkey;
3240 int len, fulllen = 0, found = -1;
3241 struct lxc_list *it;
3242 int i;
3243
3244 subkey = strchr(key, '.');
3245 if (subkey)
3246 subkey = strchr(subkey + 1, '.');
3247 if (!subkey)
3248 return -1;
3249 subkey++;
3250 if (!*subkey)
3251 return -1;
3252 for (i = 0; i < NUM_LXC_HOOKS; i++) {
3253 if (strcmp(lxchook_names[i], subkey) == 0) {
3254 found = i;
3255 break;
3256 }
3257 }
3258 if (found == -1)
3259 return -1;
3260
3261 if (!retv)
3262 inlen = 0;
3263 else
3264 memset(retv, 0, inlen);
3265
3266 lxc_list_for_each(it, &c->hooks[found]) {
3267 strprint(retv, inlen, "%s\n", (char *)it->elem);
3268 }
3269 return fulllen;
3270 }
3271
3272 static int get_config_hooks_version(const char *key, char *retv, int inlen,
3273 struct lxc_conf *c, void *data)
3274 {
3275 return lxc_get_conf_int(c, retv, inlen, c->hooks_version);
3276 }
3277
3278 static int get_config_net(const char *key, char *retv, int inlen,
3279 struct lxc_conf *c, void *data)
3280 {
3281 int len, fulllen = 0;
3282 struct lxc_list *it;
3283
3284 if (!retv)
3285 inlen = 0;
3286 else
3287 memset(retv, 0, inlen);
3288
3289 lxc_list_for_each(it, &c->network) {
3290 struct lxc_netdev *n = it->elem;
3291 const char *t = lxc_net_type_to_str(n->type);
3292 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
3293 }
3294
3295 return fulllen;
3296 }
3297
3298 static int get_config_cap_drop(const char *key, char *retv, int inlen,
3299 struct lxc_conf *c, void *data)
3300 {
3301 int len, fulllen = 0;
3302 struct lxc_list *it;
3303
3304 if (!retv)
3305 inlen = 0;
3306 else
3307 memset(retv, 0, inlen);
3308
3309 lxc_list_for_each(it, &c->caps) {
3310 strprint(retv, inlen, "%s\n", (char *)it->elem);
3311 }
3312
3313 return fulllen;
3314 }
3315
3316 static int get_config_cap_keep(const char *key, char *retv, int inlen,
3317 struct lxc_conf *c, void *data)
3318 {
3319 int len, fulllen = 0;
3320 struct lxc_list *it;
3321
3322 if (!retv)
3323 inlen = 0;
3324 else
3325 memset(retv, 0, inlen);
3326
3327 lxc_list_for_each(it, &c->keepcaps) {
3328 strprint(retv, inlen, "%s\n", (char *)it->elem);
3329 }
3330
3331 return fulllen;
3332 }
3333
3334 static int get_config_console_path(const char *key, char *retv, int inlen,
3335 struct lxc_conf *c, void *data)
3336 {
3337 return lxc_get_conf_str(retv, inlen, c->console.path);
3338 }
3339
3340 static int get_config_console_logfile(const char *key, char *retv, int inlen,
3341 struct lxc_conf *c, void *data)
3342 {
3343 return lxc_get_conf_str(retv, inlen, c->console.log_path);
3344 }
3345
3346 static int get_config_console_rotate(const char *key, char *retv, int inlen,
3347 struct lxc_conf *c, void *data)
3348 {
3349 return lxc_get_conf_int(c, retv, inlen, c->console.log_rotate);
3350 }
3351
3352
3353 static int get_config_console_buffer_size(const char *key, char *retv,
3354 int inlen, struct lxc_conf *c,
3355 void *data)
3356 {
3357 return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size);
3358 }
3359
3360 static int get_config_console_size(const char *key, char *retv, int inlen,
3361 struct lxc_conf *c, void *data)
3362 {
3363 return lxc_get_conf_uint64(c, retv, inlen, c->console.log_size);
3364 }
3365
3366
3367 static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
3368 struct lxc_conf *c, void *data)
3369 {
3370 return lxc_get_conf_str(retv, inlen, c->seccomp);
3371 }
3372
3373 static int get_config_autodev(const char *key, char *retv, int inlen,
3374 struct lxc_conf *c, void *data)
3375 {
3376 return lxc_get_conf_int(c, retv, inlen, c->autodev);
3377 }
3378
3379 static int get_config_signal_halt(const char *key, char *retv, int inlen,
3380 struct lxc_conf *c, void *data)
3381 {
3382 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
3383 }
3384
3385 static int get_config_signal_reboot(const char *key, char *retv, int inlen,
3386 struct lxc_conf *c, void *data)
3387 {
3388 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
3389 }
3390
3391 static int get_config_signal_stop(const char *key, char *retv, int inlen,
3392 struct lxc_conf *c, void *data)
3393 {
3394 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
3395 }
3396
3397 static int get_config_start(const char *key, char *retv, int inlen,
3398 struct lxc_conf *c, void *data)
3399 {
3400 if (strcmp(key + 10, "auto") == 0)
3401 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
3402 else if (strcmp(key + 10, "delay") == 0)
3403 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
3404 else if (strcmp(key + 10, "order") == 0)
3405 return lxc_get_conf_int(c, retv, inlen, c->start_order);
3406
3407 return -1;
3408 }
3409
3410 static int get_config_log_syslog(const char *key, char *retv, int inlen,
3411 struct lxc_conf *c, void *data)
3412 {
3413 return lxc_get_conf_str(retv, inlen, c->syslog);
3414 }
3415
3416 static int get_config_monitor(const char *key, char *retv, int inlen,
3417 struct lxc_conf *c, void *data)
3418 {
3419 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
3420 }
3421
3422 static int get_config_group(const char *key, char *retv, int inlen,
3423 struct lxc_conf *c, void *data)
3424 {
3425 int len, fulllen = 0;
3426 struct lxc_list *it;
3427
3428 if (!retv)
3429 inlen = 0;
3430 else
3431 memset(retv, 0, inlen);
3432
3433 lxc_list_for_each(it, &c->groups) {
3434 strprint(retv, inlen, "%s\n", (char *)it->elem);
3435 }
3436
3437 return fulllen;
3438 }
3439
3440 static int get_config_environment(const char *key, char *retv, int inlen,
3441 struct lxc_conf *c, void *data)
3442 {
3443 int len, fulllen = 0;
3444 struct lxc_list *it;
3445
3446 if (!retv)
3447 inlen = 0;
3448 else
3449 memset(retv, 0, inlen);
3450
3451 lxc_list_for_each(it, &c->environment) {
3452 strprint(retv, inlen, "%s\n", (char *)it->elem);
3453 }
3454
3455 return fulllen;
3456 }
3457
3458 static int get_config_execute_cmd(const char *key, char *retv, int inlen,
3459 struct lxc_conf *c, void *data)
3460 {
3461 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
3462 }
3463
3464 static int get_config_init_cmd(const char *key, char *retv, int inlen,
3465 struct lxc_conf *c, void *data)
3466 {
3467 return lxc_get_conf_str(retv, inlen, c->init_cmd);
3468 }
3469
3470 static int get_config_init_cwd(const char *key, char *retv, int inlen,
3471 struct lxc_conf *c, void *data)
3472 {
3473 return lxc_get_conf_str(retv, inlen, c->init_cwd);
3474 }
3475
3476 static int get_config_init_uid(const char *key, char *retv, int inlen,
3477 struct lxc_conf *c, void *data)
3478 {
3479 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
3480 }
3481
3482 static int get_config_init_gid(const char *key, char *retv, int inlen,
3483 struct lxc_conf *c, void *data)
3484 {
3485 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
3486 }
3487
3488 static int get_config_ephemeral(const char *key, char *retv, int inlen,
3489 struct lxc_conf *c, void *data)
3490 {
3491 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
3492 }
3493
3494 static int get_config_no_new_privs(const char *key, char *retv, int inlen,
3495 struct lxc_conf *c, void *data)
3496 {
3497 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
3498 }
3499
3500 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3501 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3502 * printed, in 'lxc.prlimit.resource = value' format.
3503 */
3504 static int get_config_prlimit(const char *key, char *retv, int inlen,
3505 struct lxc_conf *c, void *data)
3506 {
3507 int fulllen = 0, len;
3508 bool get_all = false;
3509 struct lxc_list *it;
3510
3511 if (!retv)
3512 inlen = 0;
3513 else
3514 memset(retv, 0, inlen);
3515
3516 if (!strcmp(key, "lxc.prlimit"))
3517 get_all = true;
3518 else if (strncmp(key, "lxc.prlimit.", 12) == 0)
3519 key += 12;
3520 else
3521 return -1;
3522
3523 lxc_list_for_each(it, &c->limits) {
3524 char buf[LXC_NUMSTRLEN64 * 2 + 2]; /* 2 colon separated 64 bit
3525 integers or the word
3526 'unlimited' */
3527 int partlen;
3528 struct lxc_limit *lim = it->elem;
3529
3530 if (lim->limit.rlim_cur == RLIM_INFINITY) {
3531 memcpy(buf, "unlimited", sizeof("unlimited"));
3532 partlen = sizeof("unlimited") - 1;
3533 } else {
3534 partlen = sprintf(buf, "%" PRIu64,
3535 (uint64_t)lim->limit.rlim_cur);
3536 }
3537 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
3538 if (lim->limit.rlim_max == RLIM_INFINITY)
3539 memcpy(buf + partlen, ":unlimited",
3540 sizeof(":unlimited"));
3541 else
3542 sprintf(buf + partlen, ":%" PRIu64,
3543 (uint64_t)lim->limit.rlim_max);
3544 }
3545
3546 if (get_all) {
3547 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
3548 lim->resource, buf);
3549 } else if (!strcmp(lim->resource, key)) {
3550 strprint(retv, inlen, "%s", buf);
3551 }
3552 }
3553
3554 return fulllen;
3555 }
3556
3557 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3558 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3559 * entries will be printed, in 'lxc.sysctl.key = value' format.
3560 */
3561 static int get_config_sysctl(const char *key, char *retv, int inlen,
3562 struct lxc_conf *c, void *data)
3563 {
3564 int len;
3565 struct lxc_list *it;
3566 int fulllen = 0;
3567 bool get_all = false;
3568
3569 if (!retv)
3570 inlen = 0;
3571 else
3572 memset(retv, 0, inlen);
3573
3574 if (strcmp(key, "lxc.sysctl") == 0)
3575 get_all = true;
3576 else if (strncmp(key, "lxc.sysctl.", sizeof("lxc.sysctl.") - 1) == 0)
3577 key += sizeof("lxc.sysctl.") - 1;
3578 else
3579 return -1;
3580
3581 lxc_list_for_each(it, &c->sysctls) {
3582 struct lxc_sysctl *elem = it->elem;
3583 if (get_all) {
3584 strprint(retv, inlen, "lxc.sysctl.%s = %s\n",
3585 elem->key, elem->value);
3586 } else if (strcmp(elem->key, key) == 0) {
3587 strprint(retv, inlen, "%s", elem->value);
3588 }
3589 }
3590
3591 return fulllen;
3592 }
3593
3594 static int get_config_proc(const char *key, char *retv, int inlen,
3595 struct lxc_conf *c, void *data)
3596 {
3597 struct lxc_list *it;
3598 int len;
3599 int fulllen = 0;
3600 bool get_all = false;
3601
3602 if (!retv)
3603 inlen = 0;
3604 else
3605 memset(retv, 0, inlen);
3606
3607 if (strcmp(key, "lxc.proc") == 0)
3608 get_all = true;
3609 else if (strncmp(key, "lxc.proc.", sizeof("lxc.proc.") - 1) == 0)
3610 key += sizeof("lxc.proc.") - 1;
3611 else
3612 return -1;
3613
3614 lxc_list_for_each(it, &c->procs) {
3615 struct lxc_proc *proc = it->elem;
3616
3617 if (get_all) {
3618 strprint(retv, inlen, "lxc.proc.%s = %s\n",
3619 proc->filename, proc->value);
3620 } else if (strcmp(proc->filename, key) == 0) {
3621 strprint(retv, inlen, "%s", proc->value);
3622 }
3623 }
3624
3625 return fulllen;
3626 }
3627
3628 static int get_config_namespace_clone(const char *key, char *retv, int inlen,
3629 struct lxc_conf *c, void *data)
3630 {
3631 int i, len;
3632 int fulllen = 0;
3633
3634 if (!retv)
3635 inlen = 0;
3636 else
3637 memset(retv, 0, inlen);
3638
3639 for (i = 0; i < LXC_NS_MAX; i++) {
3640 if (c->ns_clone & ns_info[i].clone_flag)
3641 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
3642 }
3643
3644 return fulllen;
3645 }
3646
3647 static int get_config_namespace_keep(const char *key, char *retv, int inlen,
3648 struct lxc_conf *c, void *data)
3649 {
3650 int i, len;
3651 int fulllen = 0;
3652
3653 if (!retv)
3654 inlen = 0;
3655 else
3656 memset(retv, 0, inlen);
3657
3658 for (i = 0; i < LXC_NS_MAX; i++) {
3659 if (c->ns_keep & ns_info[i].clone_flag)
3660 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
3661 }
3662
3663 return fulllen;
3664 }
3665
3666 static int get_config_namespace_share(const char *key, char *retv, int inlen,
3667 struct lxc_conf *c, void *data)
3668 {
3669 int len, ns_idx;
3670 const char *namespace;
3671 int fulllen = 0;
3672
3673 if (!retv)
3674 inlen = 0;
3675 else
3676 memset(retv, 0, inlen);
3677
3678 namespace = key + sizeof("lxc.namespace.share.") - 1;
3679 ns_idx = lxc_namespace_2_ns_idx(namespace);
3680 if (ns_idx < 0)
3681 return ns_idx;
3682
3683 strprint(retv, inlen, "%s", c->ns_share[ns_idx]);
3684
3685 return fulllen;
3686 }
3687
3688 /* Callbacks to clear config items. */
3689 static inline int clr_config_personality(const char *key, struct lxc_conf *c,
3690 void *data)
3691 {
3692 c->personality = -1;
3693 return 0;
3694 }
3695
3696 static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
3697 void *data)
3698 {
3699 c->pts = 0;
3700 return 0;
3701 }
3702
3703 static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
3704 void *data)
3705 {
3706 c->ttys.tty = 0;
3707 return 0;
3708 }
3709
3710 static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
3711 void *data)
3712 {
3713 free(c->ttys.dir);
3714 c->ttys.dir = NULL;
3715 return 0;
3716 }
3717
3718 static inline int clr_config_apparmor_profile(const char *key,
3719 struct lxc_conf *c, void *data)
3720 {
3721 free(c->lsm_aa_profile);
3722 c->lsm_aa_profile = NULL;
3723 return 0;
3724 }
3725
3726 static inline int clr_config_apparmor_allow_incomplete(const char *key,
3727 struct lxc_conf *c,
3728 void *data)
3729 {
3730 c->lsm_aa_allow_incomplete = 0;
3731 return 0;
3732 }
3733
3734 static inline int clr_config_selinux_context(const char *key,
3735 struct lxc_conf *c, void *data)
3736 {
3737 free(c->lsm_se_context);
3738 c->lsm_se_context = NULL;
3739 return 0;
3740 }
3741
3742 static inline int clr_config_cgroup_controller(const char *key,
3743 struct lxc_conf *c, void *data)
3744 {
3745 return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC);
3746 }
3747
3748 static inline int clr_config_cgroup2_controller(const char *key,
3749 struct lxc_conf *c, void *data)
3750 {
3751 return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC);
3752 }
3753
3754 static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
3755 void *data)
3756 {
3757 if (lxc_conf->cgroup_meta.dir) {
3758 free(lxc_conf->cgroup_meta.dir);
3759 lxc_conf->cgroup_meta.dir = NULL;
3760 }
3761
3762 return 0;
3763 }
3764
3765 static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
3766 void *data)
3767 {
3768 return lxc_clear_idmaps(c);
3769 }
3770
3771 static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
3772 void *data)
3773 {
3774 c->loglevel = LXC_LOG_LEVEL_NOTSET;
3775 return 0;
3776 }
3777
3778 static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
3779 void *data)
3780 {
3781 free(c->logfile);
3782 c->logfile = NULL;
3783 return 0;
3784 }
3785
3786 static inline int clr_config_mount(const char *key, struct lxc_conf *c,
3787 void *data)
3788 {
3789 return lxc_clear_mount_entries(c);
3790 }
3791
3792 static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
3793 void *data)
3794 {
3795 return lxc_clear_automounts(c);
3796 }
3797
3798 static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
3799 void *data)
3800 {
3801 free(c->fstab);
3802 c->fstab = NULL;
3803 return 0;
3804 }
3805
3806 static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
3807 void *data)
3808 {
3809 free(c->rootfs.path);
3810 c->rootfs.path = NULL;
3811 return 0;
3812 }
3813
3814 static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
3815 void *data)
3816 {
3817 free(c->rootfs.mount);
3818 c->rootfs.mount = NULL;
3819 return 0;
3820 }
3821
3822 static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
3823 void *data)
3824 {
3825 free(c->rootfs.options);
3826 c->rootfs.options = NULL;
3827 return 0;
3828 }
3829
3830 static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
3831 void *data)
3832 {
3833 free(c->utsname);
3834 c->utsname = NULL;
3835 return 0;
3836 }
3837
3838 static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
3839 void *data)
3840 {
3841 return lxc_clear_hooks(c, key);
3842 }
3843
3844 static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c,
3845 void *data)
3846 {
3847 /* default to legacy hooks version */
3848 c->hooks_version = 0;
3849 return 0;
3850 }
3851
3852 static inline int clr_config_net(const char *key, struct lxc_conf *c,
3853 void *data)
3854 {
3855 lxc_free_networks(&c->network);
3856
3857 return 0;
3858 }
3859
3860 static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
3861 void *data)
3862 {
3863 return lxc_clear_config_caps(c);
3864 }
3865
3866 static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
3867 void *data)
3868 {
3869 return lxc_clear_config_keepcaps(c);
3870 }
3871
3872 static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
3873 void *data)
3874 {
3875 free(c->console.path);
3876 c->console.path = NULL;
3877 return 0;
3878 }
3879
3880 static inline int clr_config_console_logfile(const char *key,
3881 struct lxc_conf *c, void *data)
3882 {
3883 free(c->console.log_path);
3884 c->console.log_path = NULL;
3885 return 0;
3886 }
3887
3888 static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c,
3889 void *data)
3890 {
3891 c->console.log_rotate = 0;
3892 return 0;
3893 }
3894
3895 static inline int clr_config_console_buffer_size(const char *key,
3896 struct lxc_conf *c, void *data)
3897 {
3898 c->console.buffer_size = 0;
3899 return 0;
3900 }
3901
3902 static inline int clr_config_console_size(const char *key, struct lxc_conf *c,
3903 void *data)
3904 {
3905 c->console.log_size = 0;
3906 return 0;
3907 }
3908
3909 static inline int clr_config_seccomp_profile(const char *key,
3910 struct lxc_conf *c, void *data)
3911 {
3912 free(c->seccomp);
3913 c->seccomp = NULL;
3914 return 0;
3915 }
3916
3917 static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
3918 void *data)
3919 {
3920 c->autodev = 1;
3921 return 0;
3922 }
3923
3924 static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
3925 void *data)
3926 {
3927 c->haltsignal = 0;
3928 return 0;
3929 }
3930
3931 static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
3932 void *data)
3933 {
3934 c->rebootsignal = 0;
3935 return 0;
3936 }
3937
3938 static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
3939 void *data)
3940 {
3941 c->stopsignal = 0;
3942 return 0;
3943 }
3944
3945 static inline int clr_config_start(const char *key, struct lxc_conf *c,
3946 void *data)
3947 {
3948 if (strcmp(key + 10, "auto") == 0)
3949 c->start_auto = 0;
3950 else if (strcmp(key + 10, "delay") == 0)
3951 c->start_delay = 0;
3952 else if (strcmp(key + 10, "order") == 0)
3953 c->start_order = 0;
3954
3955 return 0;
3956 }
3957
3958 static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
3959 void *data)
3960 {
3961 free(c->syslog);
3962 c->syslog = NULL;
3963 return 0;
3964 }
3965
3966 static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
3967 void *data)
3968 {
3969 c->monitor_unshare = 0;
3970 return 0;
3971 }
3972
3973 static inline int clr_config_group(const char *key, struct lxc_conf *c,
3974 void *data)
3975 {
3976 return lxc_clear_groups(c);
3977 }
3978
3979 static inline int clr_config_environment(const char *key, struct lxc_conf *c,
3980 void *data)
3981 {
3982 return lxc_clear_environment(c);
3983 }
3984
3985 static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
3986 void *data)
3987 {
3988 free(c->execute_cmd);
3989 c->execute_cmd = NULL;
3990 return 0;
3991 }
3992
3993 static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
3994 void *data)
3995 {
3996 free(c->init_cmd);
3997 c->init_cmd = NULL;
3998 return 0;
3999 }
4000
4001 static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
4002 void *data)
4003 {
4004 free(c->init_cwd);
4005 c->init_cwd = NULL;
4006 return 0;
4007 }
4008
4009 static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
4010 void *data)
4011 {
4012 c->init_uid = 0;
4013 return 0;
4014 }
4015
4016 static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
4017 void *data)
4018 {
4019 c->init_gid = 0;
4020 return 0;
4021 }
4022
4023 static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
4024 void *data)
4025 {
4026 c->ephemeral = 0;
4027 return 0;
4028 }
4029
4030 static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
4031 void *data)
4032 {
4033 c->no_new_privs = false;
4034 return 0;
4035 }
4036
4037 static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
4038 void *data)
4039 {
4040 return lxc_clear_limits(c, key);
4041 }
4042
4043 static inline int clr_config_sysctl(const char *key, struct lxc_conf *c,
4044 void *data)
4045 {
4046 return lxc_clear_sysctls(c, key);
4047 }
4048
4049 static inline int clr_config_proc(const char *key, struct lxc_conf *c,
4050 void *data)
4051 {
4052 return lxc_clear_procs(c, key);
4053 }
4054
4055 static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
4056 void *data)
4057 {
4058 lxc_clear_includes(c);
4059 return 0;
4060 }
4061
4062 static int clr_config_namespace_clone(const char *key,
4063 struct lxc_conf *lxc_conf, void *data)
4064 {
4065 lxc_conf->ns_clone = 0;
4066 return 0;
4067 }
4068
4069 static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf,
4070 void *data)
4071 {
4072 lxc_conf->ns_keep = 0;
4073 return 0;
4074 }
4075
4076 static int clr_config_namespace_share(const char *key,
4077 struct lxc_conf *lxc_conf, void *data)
4078 {
4079 int ns_idx;
4080 const char *namespace;
4081
4082 namespace = key + sizeof("lxc.namespace.share.") - 1;
4083 ns_idx = lxc_namespace_2_ns_idx(namespace);
4084 if (ns_idx < 0)
4085 return ns_idx;
4086
4087 free(lxc_conf->ns_share[ns_idx]);
4088 lxc_conf->ns_share[ns_idx] = NULL;
4089
4090 return 0;
4091 }
4092
4093 static int get_config_includefiles(const char *key, char *retv, int inlen,
4094 struct lxc_conf *c, void *data)
4095 {
4096 return -ENOSYS;
4097 }
4098
4099 static struct lxc_config_t *get_network_config_ops(const char *key,
4100 struct lxc_conf *lxc_conf,
4101 ssize_t *idx,
4102 char **deindexed_key)
4103 {
4104 int ret;
4105 unsigned int tmpidx;
4106 size_t numstrlen;
4107 char *copy, *idx_start, *idx_end;
4108 struct lxc_config_t *config = NULL;
4109
4110 /* check that this is a sensible network key */
4111 if (strncmp("lxc.net.", key, 8)) {
4112 ERROR("Invalid network configuration key \"%s\"", key);
4113 return NULL;
4114 }
4115
4116 copy = strdup(key);
4117 if (!copy) {
4118 ERROR("Failed to duplicate string \"%s\"", key);
4119 return NULL;
4120 }
4121
4122 /* lxc.net.<n> */
4123 if (!isdigit(*(key + 8))) {
4124 ERROR("Failed to detect digit in string \"%s\"", key + 8);
4125 goto on_error;
4126 }
4127
4128 /* beginning of index string */
4129 idx_start = (copy + 7);
4130 *idx_start = '\0';
4131
4132 /* end of index string */
4133 idx_end = strchr((copy + 8), '.');
4134 if (idx_end)
4135 *idx_end = '\0';
4136
4137 /* parse current index */
4138 ret = lxc_safe_uint((idx_start + 1), &tmpidx);
4139 if (ret < 0) {
4140 ERROR("Failed to parse usigned integer from string \"%s\": %s",
4141 idx_start + 1, strerror(-ret));
4142 *idx = ret;
4143 goto on_error;
4144 }
4145
4146 /* This, of course is utterly nonsensical on so many levels, but
4147 * better safe than sorry.
4148 * (Checking for INT_MAX here is intentional.)
4149 */
4150 if (tmpidx == INT_MAX) {
4151 SYSERROR("Number of configured networks would overflow the "
4152 "counter");
4153 goto on_error;
4154 }
4155 *idx = tmpidx;
4156
4157 numstrlen = strlen((idx_start + 1));
4158
4159 /* repair configuration key */
4160 *idx_start = '.';
4161
4162 /* lxc.net.<idx>.<subkey> */
4163 if (idx_end) {
4164 *idx_end = '.';
4165 if (strlen(idx_end + 1) == 0) {
4166 ERROR("No subkey in network configuration key \"%s\"", key);
4167 goto on_error;
4168 }
4169
4170 memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
4171 copy[strlen(key) - numstrlen + 1] = '\0';
4172
4173 config = lxc_get_config(copy);
4174 if (!config) {
4175 ERROR("Unknown network configuration key \"%s\"", key);
4176 goto on_error;
4177 }
4178 }
4179
4180 if (deindexed_key)
4181 *deindexed_key = copy;
4182
4183 return config;
4184
4185 on_error:
4186 free(copy);
4187 return NULL;
4188 }
4189
4190 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4191 * found. So we make sure next comes an integer, find the right callback (by
4192 * rewriting the key), and call it.
4193 */
4194 static int set_config_net_nic(const char *key, const char *value,
4195 struct lxc_conf *lxc_conf, void *data)
4196 {
4197 int ret;
4198 const char *idxstring;
4199 struct lxc_config_t *config;
4200 struct lxc_netdev *netdev;
4201 ssize_t idx = -1;
4202 char *deindexed_key = NULL;
4203
4204 idxstring = key + 8;
4205 if (!isdigit(*idxstring))
4206 return -1;
4207
4208 if (lxc_config_value_empty(value))
4209 return clr_config_net_nic(key, lxc_conf, data);
4210
4211 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
4212 if (!config || idx < 0)
4213 return -1;
4214
4215 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
4216 if (!netdev) {
4217 free(deindexed_key);
4218 return -1;
4219 }
4220
4221 ret = config->set(deindexed_key, value, lxc_conf, netdev);
4222 free(deindexed_key);
4223 return ret;
4224 }
4225
4226 static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
4227 void *data)
4228 {
4229 int ret;
4230 const char *idxstring;
4231 struct lxc_config_t *config;
4232 struct lxc_netdev *netdev;
4233 ssize_t idx = -1;
4234 char *deindexed_key = NULL;
4235
4236 idxstring = key + 8;
4237 if (!isdigit(*idxstring))
4238 return -1;
4239
4240 /* The left conjunct is pretty self-explanatory. The right conjunct
4241 * checks whether the two pointers are equal. If they are we know that
4242 * this is not a key that is namespaced any further and so we are
4243 * supposed to clear the whole network.
4244 */
4245 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
4246 unsigned int rmnetdevidx;
4247
4248 if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0)
4249 return -1;
4250
4251 /* Remove network from network list. */
4252 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
4253 return 0;
4254 }
4255
4256 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
4257 if (!config || idx < 0)
4258 return -1;
4259
4260 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false);
4261 if (!netdev) {
4262 free(deindexed_key);
4263 return -1;
4264 }
4265
4266 ret = config->clr(deindexed_key, lxc_conf, netdev);
4267 free(deindexed_key);
4268 return ret;
4269 }
4270
4271 static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
4272 void *data)
4273 {
4274 struct lxc_netdev *netdev = data;
4275
4276 if (!netdev)
4277 return -1;
4278
4279 netdev->type = -1;
4280
4281 return 0;
4282 }
4283
4284 static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
4285 void *data)
4286 {
4287 struct lxc_netdev *netdev = data;
4288
4289 if (!netdev)
4290 return -1;
4291
4292 netdev->name[0] = '\0';
4293
4294 return 0;
4295 }
4296
4297 static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
4298 void *data)
4299 {
4300 struct lxc_netdev *netdev = data;
4301
4302 if (!netdev)
4303 return -1;
4304
4305 netdev->flags = 0;
4306
4307 return 0;
4308 }
4309
4310 static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
4311 void *data)
4312 {
4313 struct lxc_netdev *netdev = data;
4314
4315 if (!netdev)
4316 return -1;
4317
4318 netdev->link[0] = '\0';
4319
4320 return 0;
4321 }
4322
4323 static int clr_config_net_macvlan_mode(const char *key,
4324 struct lxc_conf *lxc_conf, void *data)
4325 {
4326 struct lxc_netdev *netdev = data;
4327
4328 if (!netdev)
4329 return -1;
4330
4331 if (netdev->type != LXC_NET_MACVLAN)
4332 return 0;
4333
4334 netdev->priv.macvlan_attr.mode = -1;
4335
4336 return 0;
4337 }
4338
4339 static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
4340 void *data)
4341 {
4342 struct lxc_netdev *netdev = data;
4343
4344 if (!netdev)
4345 return -1;
4346
4347 netdev->priv.veth_attr.pair[0] = '\0';
4348
4349 return 0;
4350 }
4351
4352 static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
4353 void *data)
4354 {
4355 struct lxc_netdev *netdev = data;
4356
4357 if (!netdev)
4358 return -1;
4359
4360 free(netdev->upscript);
4361 netdev->upscript = NULL;
4362
4363 return 0;
4364 }
4365
4366 static int clr_config_net_script_down(const char *key,
4367 struct lxc_conf *lxc_conf, void *data)
4368 {
4369 struct lxc_netdev *netdev = data;
4370
4371 if (!netdev)
4372 return -1;
4373
4374 free(netdev->downscript);
4375 netdev->downscript = NULL;
4376
4377 return 0;
4378 }
4379
4380 static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
4381 void *data)
4382 {
4383 struct lxc_netdev *netdev = data;
4384
4385 if (!netdev)
4386 return -1;
4387
4388 free(netdev->hwaddr);
4389 netdev->hwaddr = NULL;
4390
4391 return 0;
4392 }
4393
4394 static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
4395 void *data)
4396 {
4397 struct lxc_netdev *netdev = data;
4398
4399 if (!netdev)
4400 return -1;
4401
4402 free(netdev->mtu);
4403 netdev->mtu = NULL;
4404
4405 return 0;
4406 }
4407
4408 static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
4409 void *data)
4410 {
4411 struct lxc_netdev *netdev = data;
4412
4413 if (!netdev)
4414 return -1;
4415
4416 netdev->priv.vlan_attr.vid = 0;
4417
4418 return 0;
4419 }
4420
4421 static int clr_config_net_ipv4_gateway(const char *key,
4422 struct lxc_conf *lxc_conf, void *data)
4423 {
4424 struct lxc_netdev *netdev = data;
4425
4426 if (!netdev)
4427 return -1;
4428
4429 free(netdev->ipv4_gateway);
4430 netdev->ipv4_gateway = NULL;
4431
4432 return 0;
4433 }
4434
4435 static int clr_config_net_ipv4_address(const char *key,
4436 struct lxc_conf *lxc_conf, void *data)
4437 {
4438 struct lxc_netdev *netdev = data;
4439 struct lxc_list *cur, *next;
4440
4441 if (!netdev)
4442 return -1;
4443
4444 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
4445 lxc_list_del(cur);
4446 free(cur->elem);
4447 free(cur);
4448 }
4449
4450 return 0;
4451 }
4452
4453 static int clr_config_net_ipv6_gateway(const char *key,
4454 struct lxc_conf *lxc_conf, void *data)
4455 {
4456 struct lxc_netdev *netdev = data;
4457
4458 if (!netdev)
4459 return -1;
4460
4461 free(netdev->ipv6_gateway);
4462 netdev->ipv6_gateway = NULL;
4463
4464 return 0;
4465 }
4466
4467 static int clr_config_net_ipv6_address(const char *key,
4468 struct lxc_conf *lxc_conf, void *data)
4469 {
4470 struct lxc_netdev *netdev = data;
4471 struct lxc_list *cur, *next;
4472
4473 if (!netdev)
4474 return -1;
4475
4476 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
4477 lxc_list_del(cur);
4478 free(cur->elem);
4479 free(cur);
4480 }
4481
4482 return 0;
4483 }
4484
4485 static int get_config_net_nic(const char *key, char *retv, int inlen,
4486 struct lxc_conf *c, void *data)
4487 {
4488 int ret;
4489 const char *idxstring;
4490 struct lxc_config_t *config;
4491 struct lxc_netdev *netdev;
4492 ssize_t idx = -1;
4493 char *deindexed_key = NULL;
4494
4495 idxstring = key + 8;
4496 if (!isdigit(*idxstring))
4497 return -1;
4498
4499 config = get_network_config_ops(key, c, &idx, &deindexed_key);
4500 if (!config || idx < 0)
4501 return -1;
4502
4503 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
4504 if (!netdev) {
4505 free(deindexed_key);
4506 return -1;
4507 }
4508
4509 ret = config->get(deindexed_key, retv, inlen, c, netdev);
4510 free(deindexed_key);
4511 return ret;
4512 }
4513
4514 static int get_config_net_type(const char *key, char *retv, int inlen,
4515 struct lxc_conf *c, void *data)
4516 {
4517 int len;
4518 int fulllen = 0;
4519 struct lxc_netdev *netdev = data;
4520
4521 if (!retv)
4522 inlen = 0;
4523 else
4524 memset(retv, 0, inlen);
4525
4526 if (!netdev)
4527 return -1;
4528
4529 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
4530
4531 return fulllen;
4532 }
4533
4534 static int get_config_net_flags(const char *key, char *retv, int inlen,
4535 struct lxc_conf *c, void *data)
4536 {
4537 int len;
4538 int fulllen = 0;
4539 struct lxc_netdev *netdev = data;
4540
4541 if (!retv)
4542 inlen = 0;
4543 else
4544 memset(retv, 0, inlen);
4545
4546 if (!netdev)
4547 return -1;
4548
4549 if (netdev->flags & IFF_UP)
4550 strprint(retv, inlen, "up");
4551
4552 return fulllen;
4553 }
4554
4555 static int get_config_net_link(const char *key, char *retv, int inlen,
4556 struct lxc_conf *c, void *data)
4557 {
4558 int len;
4559 int fulllen = 0;
4560 struct lxc_netdev *netdev = data;
4561
4562 if (!retv)
4563 inlen = 0;
4564 else
4565 memset(retv, 0, inlen);
4566
4567 if (!netdev)
4568 return -1;
4569
4570 if (netdev->link[0] != '\0')
4571 strprint(retv, inlen, "%s", netdev->link);
4572
4573 return fulllen;
4574 }
4575
4576 static int get_config_net_name(const char *key, char *retv, int inlen,
4577 struct lxc_conf *c, void *data)
4578 {
4579 int len;
4580 int fulllen = 0;
4581 struct lxc_netdev *netdev = data;
4582
4583 if (!retv)
4584 inlen = 0;
4585 else
4586 memset(retv, 0, inlen);
4587
4588 if (!netdev)
4589 return -1;
4590
4591 if (netdev->name[0] != '\0')
4592 strprint(retv, inlen, "%s", netdev->name);
4593
4594 return fulllen;
4595 }
4596
4597 static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
4598 struct lxc_conf *c, void *data)
4599 {
4600 int len;
4601 int fulllen = 0;
4602 const char *mode;
4603 struct lxc_netdev *netdev = data;
4604
4605 if (!retv)
4606 inlen = 0;
4607 else
4608 memset(retv, 0, inlen);
4609
4610 if (!netdev)
4611 return -1;
4612
4613 if (netdev->type != LXC_NET_MACVLAN)
4614 return 0;
4615
4616 switch (netdev->priv.macvlan_attr.mode) {
4617 case MACVLAN_MODE_PRIVATE:
4618 mode = "private";
4619 break;
4620 case MACVLAN_MODE_VEPA:
4621 mode = "vepa";
4622 break;
4623 case MACVLAN_MODE_BRIDGE:
4624 mode = "bridge";
4625 break;
4626 case MACVLAN_MODE_PASSTHRU:
4627 mode = "passthru";
4628 break;
4629 default:
4630 mode = "(invalid)";
4631 break;
4632 }
4633
4634 strprint(retv, inlen, "%s", mode);
4635
4636 return fulllen;
4637 }
4638
4639 static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
4640 struct lxc_conf *c, void *data)
4641 {
4642 int len;
4643 int fulllen = 0;
4644 struct lxc_netdev *netdev = data;
4645
4646 if (!retv)
4647 inlen = 0;
4648 else
4649 memset(retv, 0, inlen);
4650
4651 if (!netdev)
4652 return -1;
4653
4654 if (netdev->type != LXC_NET_VETH)
4655 return 0;
4656
4657 strprint(retv, inlen, "%s",
4658 netdev->priv.veth_attr.pair[0] != '\0'
4659 ? netdev->priv.veth_attr.pair
4660 : netdev->priv.veth_attr.veth1);
4661
4662 return fulllen;
4663 }
4664
4665 static int get_config_net_script_up(const char *key, char *retv, int inlen,
4666 struct lxc_conf *c, void *data)
4667 {
4668 int len;
4669 int fulllen = 0;
4670 struct lxc_netdev *netdev = data;
4671
4672 if (!retv)
4673 inlen = 0;
4674 else
4675 memset(retv, 0, inlen);
4676
4677 if (!netdev)
4678 return -1;
4679
4680 if (netdev->upscript)
4681 strprint(retv, inlen, "%s", netdev->upscript);
4682
4683 return fulllen;
4684 }
4685
4686 static int get_config_net_script_down(const char *key, char *retv, int inlen,
4687 struct lxc_conf *c, void *data)
4688 {
4689 int len;
4690 int fulllen = 0;
4691 struct lxc_netdev *netdev = data;
4692
4693 if (!retv)
4694 inlen = 0;
4695 else
4696 memset(retv, 0, inlen);
4697
4698 if (!netdev)
4699 return -1;
4700
4701 if (netdev->downscript)
4702 strprint(retv, inlen, "%s", netdev->downscript);
4703
4704 return fulllen;
4705 }
4706
4707 static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
4708 struct lxc_conf *c, void *data)
4709 {
4710 int len;
4711 int fulllen = 0;
4712 struct lxc_netdev *netdev = data;
4713
4714 if (!retv)
4715 inlen = 0;
4716 else
4717 memset(retv, 0, inlen);
4718
4719 if (!netdev)
4720 return -1;
4721
4722 if (netdev->hwaddr)
4723 strprint(retv, inlen, "%s", netdev->hwaddr);
4724
4725 return fulllen;
4726 }
4727
4728 static int get_config_net_mtu(const char *key, char *retv, int inlen,
4729 struct lxc_conf *c, void *data)
4730 {
4731 int len;
4732 int fulllen = 0;
4733 struct lxc_netdev *netdev = data;
4734
4735 if (!retv)
4736 inlen = 0;
4737 else
4738 memset(retv, 0, inlen);
4739
4740 if (!netdev)
4741 return -1;
4742
4743 if (netdev->mtu)
4744 strprint(retv, inlen, "%s", netdev->mtu);
4745
4746 return fulllen;
4747 }
4748
4749 static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
4750 struct lxc_conf *c, void *data)
4751 {
4752 int len;
4753 int fulllen = 0;
4754 struct lxc_netdev *netdev = data;
4755
4756 if (!retv)
4757 inlen = 0;
4758 else
4759 memset(retv, 0, inlen);
4760
4761 if (!netdev)
4762 return -1;
4763
4764 if (netdev->type != LXC_NET_VLAN)
4765 return 0;
4766
4767 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
4768
4769 return fulllen;
4770 }
4771
4772 static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
4773 struct lxc_conf *c, void *data)
4774 {
4775 int len;
4776 char buf[INET_ADDRSTRLEN];
4777 int fulllen = 0;
4778 struct lxc_netdev *netdev = data;
4779
4780 if (!retv)
4781 inlen = 0;
4782 else
4783 memset(retv, 0, inlen);
4784
4785 if (!netdev)
4786 return -1;
4787
4788 if (netdev->ipv4_gateway_auto) {
4789 strprint(retv, inlen, "auto");
4790 } else if (netdev->ipv4_gateway) {
4791 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
4792 strprint(retv, inlen, "%s", buf);
4793 }
4794
4795 return fulllen;
4796 }
4797
4798 static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
4799 struct lxc_conf *c, void *data)
4800 {
4801 int len;
4802 size_t listlen;
4803 char buf[INET_ADDRSTRLEN];
4804 struct lxc_list *it;
4805 int fulllen = 0;
4806 struct lxc_netdev *netdev = data;
4807
4808 if (!retv)
4809 inlen = 0;
4810 else
4811 memset(retv, 0, inlen);
4812
4813 if (!netdev)
4814 return -1;
4815
4816 listlen = lxc_list_len(&netdev->ipv4);
4817 lxc_list_for_each(it, &netdev->ipv4) {
4818 struct lxc_inetdev *i = it->elem;
4819 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
4820 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
4821 (listlen-- > 1) ? "\n" : "");
4822 }
4823
4824 return fulllen;
4825 }
4826
4827 static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
4828 struct lxc_conf *c, void *data)
4829 {
4830 int len;
4831 char buf[INET6_ADDRSTRLEN];
4832 int fulllen = 0;
4833 struct lxc_netdev *netdev = data;
4834
4835 if (!retv)
4836 inlen = 0;
4837 else
4838 memset(retv, 0, inlen);
4839
4840 if (!netdev)
4841 return -1;
4842
4843 if (netdev->ipv6_gateway_auto) {
4844 strprint(retv, inlen, "auto");
4845 } else if (netdev->ipv6_gateway) {
4846 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
4847 strprint(retv, inlen, "%s", buf);
4848 }
4849
4850 return fulllen;
4851 }
4852
4853 static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
4854 struct lxc_conf *c, void *data)
4855 {
4856 int len;
4857 size_t listlen;
4858 char buf[INET6_ADDRSTRLEN];
4859 struct lxc_list *it;
4860 int fulllen = 0;
4861 struct lxc_netdev *netdev = data;
4862
4863 if (!retv)
4864 inlen = 0;
4865 else
4866 memset(retv, 0, inlen);
4867
4868 if (!netdev)
4869 return -1;
4870
4871 listlen = lxc_list_len(&netdev->ipv6);
4872 lxc_list_for_each(it, &netdev->ipv6) {
4873 struct lxc_inet6dev *i = it->elem;
4874 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
4875 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
4876 (listlen-- > 1) ? "\n" : "");
4877 }
4878
4879 return fulllen;
4880 }
4881
4882 int lxc_list_config_items(char *retv, int inlen)
4883 {
4884 size_t i;
4885 int len;
4886 int fulllen = 0;
4887
4888 if (!retv)
4889 inlen = 0;
4890 else
4891 memset(retv, 0, inlen);
4892
4893 for (i = 0; i < config_size; i++) {
4894 char *s = config[i].name;
4895
4896 if (s[strlen(s) - 1] == '.')
4897 continue;
4898
4899 strprint(retv, inlen, "%s\n", s);
4900 }
4901
4902 return fulllen;
4903 }
4904
4905 int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
4906 int inlen)
4907 {
4908 int len;
4909 int fulllen = 0;
4910
4911 if (!retv)
4912 inlen = 0;
4913 else
4914 memset(retv, 0, inlen);
4915
4916 if (!strcmp(key, "lxc.apparmor")) {
4917 strprint(retv, inlen, "allow_incomplete\n");
4918 strprint(retv, inlen, "profile\n");
4919 } else if (!strcmp(key, "lxc.cgroup")) {
4920 strprint(retv, inlen, "dir\n");
4921 } else if (!strcmp(key, "lxc.selinux")) {
4922 strprint(retv, inlen, "context\n");
4923 } else if (!strcmp(key, "lxc.mount")) {
4924 strprint(retv, inlen, "auto\n");
4925 strprint(retv, inlen, "entry\n");
4926 strprint(retv, inlen, "fstab\n");
4927 } else if (!strcmp(key, "lxc.rootfs")) {
4928 strprint(retv, inlen, "mount\n");
4929 strprint(retv, inlen, "options\n");
4930 strprint(retv, inlen, "path\n");
4931 } else if (!strcmp(key, "lxc.uts")) {
4932 strprint(retv, inlen, "name\n");
4933 } else if (!strcmp(key, "lxc.hook")) {
4934 strprint(retv, inlen, "autodev\n");
4935 strprint(retv, inlen, "clone\n");
4936 strprint(retv, inlen, "destroy\n");
4937 strprint(retv, inlen, "mount\n");
4938 strprint(retv, inlen, "post-stop\n");
4939 strprint(retv, inlen, "pre-mount\n");
4940 strprint(retv, inlen, "pre-start\n");
4941 strprint(retv, inlen, "start-host\n");
4942 strprint(retv, inlen, "start\n");
4943 strprint(retv, inlen, "stop\n");
4944 } else if (!strcmp(key, "lxc.cap")) {
4945 strprint(retv, inlen, "drop\n");
4946 strprint(retv, inlen, "keep\n");
4947 } else if (!strcmp(key, "lxc.console")) {
4948 strprint(retv, inlen, "logfile\n");
4949 strprint(retv, inlen, "path\n");
4950 } else if (!strcmp(key, "lxc.seccomp")) {
4951 strprint(retv, inlen, "profile\n");
4952 } else if (!strcmp(key, "lxc.signal")) {
4953 strprint(retv, inlen, "halt\n");
4954 strprint(retv, inlen, "reboot\n");
4955 strprint(retv, inlen, "stop\n");
4956 } else if (!strcmp(key, "lxc.start")) {
4957 strprint(retv, inlen, "auto\n");
4958 strprint(retv, inlen, "delay\n");
4959 strprint(retv, inlen, "order\n");
4960 } else if (!strcmp(key, "lxc.monitor")) {
4961 strprint(retv, inlen, "unshare\n");
4962 } else {
4963 fulllen = -1;
4964 }
4965
4966 return fulllen;
4967 }
4968
4969 int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
4970 {
4971 int len;
4972 const char *idxstring;
4973 struct lxc_netdev *netdev;
4974 int fulllen = 0;
4975 ssize_t idx = -1;
4976
4977 idxstring = key + 8;
4978 if (!isdigit(*idxstring))
4979 return -1;
4980
4981 (void)get_network_config_ops(key, c, &idx, NULL);
4982 if (idx < 0)
4983 return -1;
4984
4985 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
4986 if (!netdev)
4987 return -1;
4988
4989 if (!retv)
4990 inlen = 0;
4991 else
4992 memset(retv, 0, inlen);
4993
4994 strprint(retv, inlen, "type\n");
4995 strprint(retv, inlen, "script.up\n");
4996 strprint(retv, inlen, "script.down\n");
4997 if (netdev->type != LXC_NET_EMPTY) {
4998 strprint(retv, inlen, "flags\n");
4999 strprint(retv, inlen, "link\n");
5000 strprint(retv, inlen, "name\n");
5001 strprint(retv, inlen, "hwaddr\n");
5002 strprint(retv, inlen, "mtu\n");
5003 strprint(retv, inlen, "ipv6.address\n");
5004 strprint(retv, inlen, "ipv6.gateway\n");
5005 strprint(retv, inlen, "ipv4.address\n");
5006 strprint(retv, inlen, "ipv4.gateway\n");
5007 }
5008
5009 switch (netdev->type) {
5010 case LXC_NET_VETH:
5011 strprint(retv, inlen, "veth.pair\n");
5012 break;
5013 case LXC_NET_MACVLAN:
5014 strprint(retv, inlen, "macvlan.mode\n");
5015 break;
5016 case LXC_NET_VLAN:
5017 strprint(retv, inlen, "vlan.id\n");
5018 break;
5019 case LXC_NET_PHYS:
5020 break;
5021 }
5022
5023 return fulllen;
5024 }