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