]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/confile.c
d4fdaf23d9d40b9e9b303fbae788433b5580cfd8
[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 ERROR("AAAA: %s", host_path);
1807 ERROR("BBBB: %s", container_path);
1808
1809 lxc_conf->shmount.path_host = strdup(host_path);
1810 if (!lxc_conf->shmount.path_host) {
1811 SYSERROR("Failed to copy shmounts host path");
1812 goto on_error;
1813 }
1814
1815 lxc_conf->shmount.path_cont = strdup(container_path);
1816 if(!lxc_conf->shmount.path_cont) {
1817 SYSERROR("Failed to copy shmounts container path");
1818 goto on_error;
1819 }
1820 }
1821 }
1822
1823 ret = 0;
1824
1825 on_error:
1826 free(autos);
1827
1828 return ret;
1829 }
1830
1831 static int set_config_mount(const char *key, const char *value,
1832 struct lxc_conf *lxc_conf, void *data)
1833 {
1834 char *mntelem;
1835 struct lxc_list *mntlist;
1836
1837 if (lxc_config_value_empty(value))
1838 return lxc_clear_mount_entries(lxc_conf);
1839
1840 mntlist = malloc(sizeof(*mntlist));
1841 if (!mntlist)
1842 return -1;
1843
1844 mntelem = strdup(value);
1845 if (!mntelem) {
1846 free(mntlist);
1847 return -1;
1848 }
1849 mntlist->elem = mntelem;
1850
1851 lxc_list_add_tail(&lxc_conf->mount_list, mntlist);
1852
1853 return 0;
1854 }
1855
1856 int add_elem_to_mount_list(const char *value, struct lxc_conf *lxc_conf) {
1857 return set_config_mount(NULL, value, lxc_conf, NULL);
1858 }
1859
1860 static int set_config_cap_keep(const char *key, const char *value,
1861 struct lxc_conf *lxc_conf, void *data)
1862 {
1863 char *keepcaps, *token;
1864 struct lxc_list *keeplist;
1865 int ret = -1;
1866
1867 if (lxc_config_value_empty(value))
1868 return lxc_clear_config_keepcaps(lxc_conf);
1869
1870 keepcaps = strdup(value);
1871 if (!keepcaps)
1872 return -1;
1873
1874 /* In case several capability keep is specified in a single line
1875 * split these caps in a single element for the list.
1876 */
1877 lxc_iterate_parts(token, keepcaps, " \t") {
1878 if (!strcmp(token, "none"))
1879 lxc_clear_config_keepcaps(lxc_conf);
1880
1881 keeplist = malloc(sizeof(*keeplist));
1882 if (!keeplist)
1883 goto on_error;
1884
1885 keeplist->elem = strdup(token);
1886 if (!keeplist->elem) {
1887 free(keeplist);
1888 goto on_error;
1889 }
1890
1891 lxc_list_add_tail(&lxc_conf->keepcaps, keeplist);
1892 }
1893
1894 ret = 0;
1895
1896 on_error:
1897 free(keepcaps);
1898
1899 return ret;
1900 }
1901
1902 static int set_config_cap_drop(const char *key, const char *value,
1903 struct lxc_conf *lxc_conf, void *data)
1904 {
1905 char *dropcaps, *token;
1906 struct lxc_list *droplist;
1907 int ret = -1;
1908
1909 if (lxc_config_value_empty(value))
1910 return lxc_clear_config_caps(lxc_conf);
1911
1912 dropcaps = strdup(value);
1913 if (!dropcaps)
1914 return -1;
1915
1916 /* In case several capability drop is specified in a single line
1917 * split these caps in a single element for the list.
1918 */
1919 lxc_iterate_parts(token, dropcaps, " \t") {
1920 droplist = malloc(sizeof(*droplist));
1921 if (!droplist)
1922 goto on_error;
1923
1924 droplist->elem = strdup(token);
1925 if (!droplist->elem) {
1926 free(droplist);
1927 goto on_error;
1928 }
1929
1930 lxc_list_add_tail(&lxc_conf->caps, droplist);
1931 }
1932
1933 ret = 0;
1934
1935 on_error:
1936 free(dropcaps);
1937
1938 return ret;
1939 }
1940
1941 static int set_config_console_path(const char *key, const char *value,
1942 struct lxc_conf *lxc_conf, void *data)
1943 {
1944 return set_config_path_item(&lxc_conf->console.path, value);
1945 }
1946
1947 static int set_config_console_rotate(const char *key, const char *value,
1948 struct lxc_conf *lxc_conf, void *data)
1949 {
1950 if (lxc_config_value_empty(value)) {
1951 lxc_conf->console.log_rotate = 0;
1952 return 0;
1953 }
1954
1955 if (lxc_safe_uint(value, &lxc_conf->console.log_rotate) < 0)
1956 return -1;
1957
1958 if (lxc_conf->console.log_rotate > 1) {
1959 ERROR("The \"lxc.console.rotate\" config key can only be set "
1960 "to 0 or 1");
1961 return -1;
1962 }
1963
1964 return 0;
1965 }
1966
1967 static int set_config_console_logfile(const char *key, const char *value,
1968 struct lxc_conf *lxc_conf, void *data)
1969 {
1970 return set_config_path_item(&lxc_conf->console.log_path, value);
1971 }
1972
1973 static int set_config_console_buffer_size(const char *key, const char *value,
1974 struct lxc_conf *lxc_conf, void *data)
1975 {
1976 int ret;
1977 int64_t size;
1978 uint64_t buffer_size, pgsz;
1979
1980 if (lxc_config_value_empty(value)) {
1981 lxc_conf->console.buffer_size = 0;
1982 return 0;
1983 }
1984
1985 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
1986 if (!strcmp(value, "auto")) {
1987 lxc_conf->console.buffer_size = 1 << 17;
1988 return 0;
1989 }
1990
1991 ret = parse_byte_size_string(value, &size);
1992 if (ret < 0)
1993 return -1;
1994
1995 if (size < 0)
1996 return -EINVAL;
1997
1998 /* must be at least a page size */
1999 pgsz = lxc_getpagesize();
2000 if ((uint64_t)size < pgsz) {
2001 NOTICE("Requested ringbuffer size for the console is %" PRId64
2002 " but must be at least %" PRId64
2003 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
2004 size, pgsz, pgsz);
2005 size = pgsz;
2006 }
2007
2008 buffer_size = lxc_find_next_power2((uint64_t)size);
2009 if (buffer_size == 0)
2010 return -EINVAL;
2011
2012 if (buffer_size != size)
2013 NOTICE("Passed size was not a power of 2. Rounding log size to "
2014 "next power of two: %" PRIu64 " bytes", buffer_size);
2015
2016 lxc_conf->console.buffer_size = buffer_size;
2017
2018 return 0;
2019 }
2020
2021 static int set_config_console_size(const char *key, const char *value,
2022 struct lxc_conf *lxc_conf, void *data)
2023 {
2024 int ret;
2025 int64_t size;
2026 uint64_t log_size, pgsz;
2027
2028 if (lxc_config_value_empty(value)) {
2029 lxc_conf->console.log_size = 0;
2030 return 0;
2031 }
2032
2033 /* If the user specified "auto" the default log size is 2^17 = 128 Kib */
2034 if (!strcmp(value, "auto")) {
2035 lxc_conf->console.log_size = 1 << 17;
2036 return 0;
2037 }
2038
2039 ret = parse_byte_size_string(value, &size);
2040 if (ret < 0)
2041 return -1;
2042
2043 if (size < 0)
2044 return -EINVAL;
2045
2046 /* must be at least a page size */
2047 pgsz = lxc_getpagesize();
2048 if ((uint64_t)size < pgsz) {
2049 NOTICE("Requested ringbuffer size for the console is %" PRId64
2050 " but must be at least %" PRId64
2051 " bytes. Setting ringbuffer size to %" PRId64 " bytes",
2052 size, pgsz, pgsz);
2053 size = pgsz;
2054 }
2055
2056 log_size = lxc_find_next_power2((uint64_t)size);
2057 if (log_size == 0)
2058 return -EINVAL;
2059
2060 if (log_size != size)
2061 NOTICE("Passed size was not a power of 2. Rounding log size to "
2062 "next power of two: %" PRIu64 " bytes", log_size);
2063
2064 lxc_conf->console.log_size = log_size;
2065
2066 return 0;
2067 }
2068
2069 int append_unexp_config_line(const char *line, struct lxc_conf *conf)
2070 {
2071 size_t linelen;
2072 size_t len = conf->unexpanded_len;
2073
2074 update_hwaddr(line);
2075
2076 linelen = strlen(line);
2077 while (conf->unexpanded_alloced <= len + linelen + 2) {
2078 char *tmp = realloc(conf->unexpanded_config,
2079 conf->unexpanded_alloced + 1024);
2080 if (!tmp)
2081 return -1;
2082
2083 if (!conf->unexpanded_config)
2084 *tmp = '\0';
2085
2086 conf->unexpanded_config = tmp;
2087 conf->unexpanded_alloced += 1024;
2088 }
2089
2090 memcpy(conf->unexpanded_config + conf->unexpanded_len, line, linelen);
2091 conf->unexpanded_len += linelen;
2092 if (line[linelen - 1] != '\n')
2093 conf->unexpanded_config[conf->unexpanded_len++] = '\n';
2094 conf->unexpanded_config[conf->unexpanded_len] = '\0';
2095
2096 return 0;
2097 }
2098
2099 static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf)
2100 {
2101 struct dirent *direntp;
2102 DIR *dir;
2103 char path[PATH_MAX];
2104 int len;
2105 int ret = -1;
2106
2107 dir = opendir(dirp);
2108 if (!dir)
2109 return -1;
2110
2111 while ((direntp = readdir(dir))) {
2112 const char *fnam;
2113
2114 fnam = direntp->d_name;
2115 if (!strcmp(fnam, "."))
2116 continue;
2117
2118 if (!strcmp(fnam, ".."))
2119 continue;
2120
2121 len = strlen(fnam);
2122 if (len < 6 || strncmp(fnam + len - 5, ".conf", 5) != 0)
2123 continue;
2124
2125 len = snprintf(path, PATH_MAX, "%s/%s", dirp, fnam);
2126 if (len < 0 || len >= PATH_MAX) {
2127 ret = -1;
2128 goto out;
2129 }
2130
2131 ret = lxc_config_read(path, lxc_conf, true);
2132 if (ret < 0)
2133 goto out;
2134 }
2135 ret = 0;
2136
2137 out:
2138 closedir(dir);
2139
2140 return ret;
2141 }
2142
2143 static int set_config_includefiles(const char *key, const char *value,
2144 struct lxc_conf *lxc_conf, void *data)
2145 {
2146 if (lxc_config_value_empty(value)) {
2147 clr_config_includefiles(key, lxc_conf, NULL);
2148 return 0;
2149 }
2150
2151 if (is_dir(value))
2152 return do_includedir(value, lxc_conf);
2153
2154 return lxc_config_read(value, lxc_conf, true);
2155 }
2156
2157 static int set_config_rootfs_path(const char *key, const char *value,
2158 struct lxc_conf *lxc_conf, void *data)
2159 {
2160 int ret;
2161 char *dup, *tmp;
2162 const char *container_path;
2163
2164 if (lxc_config_value_empty(value)) {
2165 free(lxc_conf->rootfs.path);
2166 lxc_conf->rootfs.path = NULL;
2167 return 0;
2168 }
2169
2170 dup = strdup(value);
2171 if (!dup)
2172 return -1;
2173
2174 /* Split <storage type>:<container path> into <storage type> and
2175 * <container path>. Set "rootfs.bdev_type" to <storage type> and
2176 * "rootfs.path" to <container path>.
2177 */
2178 tmp = strchr(dup, ':');
2179 if (tmp) {
2180 *tmp = '\0';
2181
2182 ret = set_config_path_item(&lxc_conf->rootfs.bdev_type, dup);
2183 if (ret < 0) {
2184 free(dup);
2185 return -1;
2186 }
2187
2188 tmp++;
2189 container_path = tmp;
2190 } else {
2191 container_path = value;
2192 }
2193
2194 ret = set_config_path_item(&lxc_conf->rootfs.path, container_path);
2195 free(dup);
2196
2197 return ret;
2198 }
2199
2200 static int set_config_rootfs_managed(const char *key, const char *value,
2201 struct lxc_conf *lxc_conf, void *data)
2202 {
2203 unsigned int val = 0;
2204
2205 if (lxc_config_value_empty(value)) {
2206 lxc_conf->rootfs.managed = true;
2207 return 0;
2208 }
2209
2210 if (lxc_safe_uint(value, &val) < 0)
2211 return -EINVAL;
2212
2213 switch (val) {
2214 case 0:
2215 lxc_conf->rootfs.managed = false;
2216 return 0;
2217 case 1:
2218 lxc_conf->rootfs.managed = true;
2219 return 0;
2220 }
2221
2222 return -EINVAL;
2223 }
2224
2225 static int set_config_rootfs_mount(const char *key, const char *value,
2226 struct lxc_conf *lxc_conf, void *data)
2227 {
2228 return set_config_path_item(&lxc_conf->rootfs.mount, value);
2229 }
2230
2231 static int set_config_rootfs_options(const char *key, const char *value,
2232 struct lxc_conf *lxc_conf, void *data)
2233 {
2234 int ret;
2235 unsigned long mflags = 0, pflags = 0;
2236 char *mdata = NULL, *opts = NULL;
2237 struct lxc_rootfs *rootfs = &lxc_conf->rootfs;
2238
2239 ret = parse_mntopts(value, &mflags, &mdata);
2240 if (ret < 0)
2241 return -EINVAL;
2242
2243 ret = parse_propagationopts(value, &pflags);
2244 if (ret < 0) {
2245 free(mdata);
2246 return -EINVAL;
2247 }
2248
2249 ret = set_config_string_item(&opts, value);
2250 if (ret < 0) {
2251 free(mdata);
2252 return -ENOMEM;
2253 }
2254
2255 rootfs->mountflags = mflags | pflags;
2256 rootfs->options = opts;
2257 rootfs->data = mdata;
2258
2259 return 0;
2260 }
2261
2262 static int set_config_uts_name(const char *key, const char *value,
2263 struct lxc_conf *lxc_conf, void *data)
2264 {
2265 struct utsname *utsname;
2266
2267 if (lxc_config_value_empty(value)) {
2268 clr_config_uts_name(key, lxc_conf, NULL);
2269 return 0;
2270 }
2271
2272 utsname = malloc(sizeof(*utsname));
2273 if (!utsname)
2274 return -1;
2275
2276 if (strlen(value) >= sizeof(utsname->nodename)) {
2277 free(utsname);
2278 return -1;
2279 }
2280
2281 (void)strlcpy(utsname->nodename, value, sizeof(utsname->nodename));
2282 free(lxc_conf->utsname);
2283 lxc_conf->utsname = utsname;
2284
2285 return 0;
2286 }
2287
2288 static int set_config_namespace_clone(const char *key, const char *value,
2289 struct lxc_conf *lxc_conf, void *data)
2290 {
2291 char *ns, *token;
2292 int cloneflag = 0;
2293
2294 if (lxc_config_value_empty(value))
2295 return clr_config_namespace_clone(key, lxc_conf, data);
2296
2297 if (lxc_conf->ns_keep != 0) {
2298 errno = EINVAL;
2299 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2300 "\"lxc.namespace.keep\"");
2301 return -EINVAL;
2302 }
2303
2304 ns = strdup(value);
2305 if (!ns)
2306 return -1;
2307
2308 lxc_iterate_parts(token, ns, " \t") {
2309 token += lxc_char_left_gc(token, strlen(token));
2310 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2311 cloneflag = lxc_namespace_2_cloneflag(token);
2312 if (cloneflag < 0) {
2313 free(ns);
2314 return -EINVAL;
2315 }
2316 lxc_conf->ns_clone |= cloneflag;
2317 }
2318 free(ns);
2319
2320 return 0;
2321 }
2322
2323 static int set_config_namespace_keep(const char *key, const char *value,
2324 struct lxc_conf *lxc_conf, void *data)
2325 {
2326 char *ns, *token;
2327 int cloneflag = 0;
2328
2329 if (lxc_config_value_empty(value))
2330 return clr_config_namespace_keep(key, lxc_conf, data);
2331
2332 if (lxc_conf->ns_clone != 0) {
2333 errno = EINVAL;
2334 SYSERROR("Cannot set both \"lxc.namespace.clone\" and "
2335 "\"lxc.namespace.keep\"");
2336 return -EINVAL;
2337 }
2338
2339 ns = strdup(value);
2340 if (!ns)
2341 return -1;
2342
2343 lxc_iterate_parts(token, ns, " \t") {
2344 token += lxc_char_left_gc(token, strlen(token));
2345 token[lxc_char_right_gc(token, strlen(token))] = '\0';
2346 cloneflag = lxc_namespace_2_cloneflag(token);
2347 if (cloneflag < 0) {
2348 free(ns);
2349 return -EINVAL;
2350 }
2351 lxc_conf->ns_keep |= cloneflag;
2352 }
2353 free(ns);
2354
2355 return 0;
2356 }
2357
2358 static int set_config_namespace_share(const char *key, const char *value,
2359 struct lxc_conf *lxc_conf, void *data)
2360 {
2361 int ns_idx;
2362 const char *namespace;
2363
2364 if (lxc_config_value_empty(value))
2365 return clr_config_namespace_share(key, lxc_conf, data);
2366
2367 namespace = key + STRLITERALLEN("lxc.namespace.share.");
2368 ns_idx = lxc_namespace_2_ns_idx(namespace);
2369 if (ns_idx < 0)
2370 return ns_idx;
2371
2372 return set_config_string_item(&lxc_conf->ns_share[ns_idx], value);
2373 }
2374
2375 struct parse_line_conf {
2376 struct lxc_conf *conf;
2377 bool from_include;
2378 };
2379
2380 static int parse_line(char *buffer, void *data)
2381 {
2382 char *dot, *key, *line, *linep, *value;
2383 bool empty_line;
2384 struct lxc_config_t *config;
2385 int ret = 0;
2386 char *dup = buffer;
2387 struct parse_line_conf *plc = data;
2388
2389 /* If there are newlines in the config file we should keep them. */
2390 empty_line = lxc_is_line_empty(dup);
2391 if (empty_line)
2392 dup = "\n";
2393
2394 /* We have to dup the buffer otherwise, at the re-exec for reboot we
2395 * modified the original string on the stack by replacing '=' by '\0'
2396 * below.
2397 */
2398 linep = line = strdup(dup);
2399 if (!line)
2400 return -1;
2401
2402 if (!plc->from_include) {
2403 ret = append_unexp_config_line(line, plc->conf);
2404 if (ret < 0)
2405 goto on_error;
2406 }
2407
2408 if (empty_line)
2409 goto on_error;
2410
2411 line += lxc_char_left_gc(line, strlen(line));
2412
2413 /* ignore comments */
2414 if (line[0] == '#')
2415 goto on_error;
2416
2417 /* martian option - don't add it to the config itself */
2418 if (strncmp(line, "lxc.", 4))
2419 goto on_error;
2420
2421 ret = -1;
2422
2423 dot = strchr(line, '=');
2424 if (!dot) {
2425 ERROR("Invalid configuration line: %s", line);
2426 goto on_error;
2427 }
2428
2429 *dot = '\0';
2430 value = dot + 1;
2431
2432 key = line;
2433 key[lxc_char_right_gc(key, strlen(key))] = '\0';
2434
2435 value += lxc_char_left_gc(value, strlen(value));
2436 value[lxc_char_right_gc(value, strlen(value))] = '\0';
2437
2438 if (*value == '\'' || *value == '\"') {
2439 size_t len;
2440
2441 len = strlen(value);
2442 if (len > 1 && value[len - 1] == *value) {
2443 value[len - 1] = '\0';
2444 value++;
2445 }
2446 }
2447
2448 config = lxc_get_config(key);
2449 if (!config) {
2450 ERROR("Unknown configuration key \"%s\"", key);
2451 goto on_error;
2452 }
2453
2454 ret = config->set(key, value, plc->conf, NULL);
2455
2456 on_error:
2457 free(linep);
2458
2459 return ret;
2460 }
2461
2462 static struct new_config_item *parse_new_conf_line(char *buffer)
2463 {
2464 char *dot, *key, *line, *linep, *value;
2465 int ret = 0;
2466 char *dup = buffer;
2467 struct new_config_item *new = NULL;
2468
2469 linep = line = strdup(dup);
2470 if (!line)
2471 return NULL;
2472
2473 line += lxc_char_left_gc(line, strlen(line));
2474
2475 /* martian option - don't add it to the config itself */
2476 if (strncmp(line, "lxc.", 4))
2477 goto on_error;
2478
2479 ret = -1;
2480 dot = strchr(line, '=');
2481 if (!dot) {
2482 ERROR("Invalid configuration item: %s", line);
2483 goto on_error;
2484 }
2485
2486 *dot = '\0';
2487 value = dot + 1;
2488
2489 key = line;
2490 key[lxc_char_right_gc(key, strlen(key))] = '\0';
2491
2492 value += lxc_char_left_gc(value, strlen(value));
2493 value[lxc_char_right_gc(value, strlen(value))] = '\0';
2494
2495 if (*value == '\'' || *value == '\"') {
2496 size_t len;
2497
2498 len = strlen(value);
2499 if (len > 1 && value[len - 1] == *value) {
2500 value[len - 1] = '\0';
2501 value++;
2502 }
2503 }
2504
2505 ret = -1;
2506 new = malloc(sizeof(struct new_config_item));
2507 if (!new)
2508 goto on_error;
2509
2510 new->key = strdup(key);
2511 new->val = strdup(value);
2512 if (!new->val || !new->key)
2513 goto on_error;
2514
2515 ret = 0;
2516
2517 on_error:
2518 free(linep);
2519
2520 if (ret < 0 && new) {
2521 free(new->key);
2522 free(new->val);
2523 free(new);
2524 new = NULL;
2525 }
2526
2527 return new;
2528 }
2529
2530 int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include)
2531 {
2532 struct parse_line_conf c;
2533
2534 c.conf = conf;
2535 c.from_include = from_include;
2536
2537 /* Catch only the top level config file name in the structure. */
2538 if (!conf->rcfile)
2539 conf->rcfile = strdup(file);
2540
2541 return lxc_file_for_each_line_mmap(file, parse_line, &c);
2542 }
2543
2544 int lxc_config_define_add(struct lxc_list *defines, char *arg)
2545 {
2546 struct lxc_list *dent;
2547
2548 dent = malloc(sizeof(struct lxc_list));
2549 if (!dent)
2550 return -1;
2551
2552 dent->elem = parse_new_conf_line(arg);
2553 if (!dent->elem) {
2554 free(dent);
2555 return -1;
2556 }
2557
2558 lxc_list_add_tail(defines, dent);
2559
2560 return 0;
2561 }
2562
2563 bool lxc_config_define_load(struct lxc_list *defines, struct lxc_container *c)
2564 {
2565 struct lxc_list *it;
2566 bool bret = true;
2567
2568 lxc_list_for_each(it, defines) {
2569 struct new_config_item *new_item = it->elem;
2570 bret = c->set_config_item(c, new_item->key, new_item->val);
2571 if (!bret)
2572 break;
2573 }
2574
2575 lxc_config_define_free(defines);
2576
2577 return bret;
2578 }
2579
2580 void lxc_config_define_free(struct lxc_list *defines)
2581 {
2582 struct lxc_list *it, *next;
2583
2584 lxc_list_for_each_safe(it, defines, next) {
2585 struct new_config_item *new_item = it->elem;
2586 free(new_item->key);
2587 free(new_item->val);
2588 lxc_list_del(it);
2589 free(it);
2590 }
2591 }
2592
2593 signed long lxc_config_parse_arch(const char *arch)
2594 {
2595 #if HAVE_SYS_PERSONALITY_H
2596 size_t i;
2597 struct per_name {
2598 char *name;
2599 unsigned long per;
2600 } pername[] = {
2601 { "arm", PER_LINUX32 },
2602 { "armel", PER_LINUX32 },
2603 { "armhf", PER_LINUX32 },
2604 { "armv7l", PER_LINUX32 },
2605 { "athlon", PER_LINUX32 },
2606 { "i386", PER_LINUX32 },
2607 { "i486", PER_LINUX32 },
2608 { "i586", PER_LINUX32 },
2609 { "i686", PER_LINUX32 },
2610 { "linux32", PER_LINUX32 },
2611 { "mips", PER_LINUX32 },
2612 { "mipsel", PER_LINUX32 },
2613 { "ppc", PER_LINUX32 },
2614 { "powerpc", PER_LINUX32 },
2615 { "x86", PER_LINUX32 },
2616 { "amd64", PER_LINUX },
2617 { "arm64", PER_LINUX },
2618 { "linux64", PER_LINUX },
2619 { "mips64", PER_LINUX },
2620 { "mips64el", PER_LINUX },
2621 { "ppc64", PER_LINUX },
2622 { "ppc64el", PER_LINUX },
2623 { "ppc64le", PER_LINUX },
2624 { "powerpc64", PER_LINUX },
2625 { "s390x", PER_LINUX },
2626 { "x86_64", PER_LINUX },
2627 };
2628 size_t len = sizeof(pername) / sizeof(pername[0]);
2629
2630 for (i = 0; i < len; i++)
2631 if (!strcmp(pername[i].name, arch))
2632 return pername[i].per;
2633 #endif
2634
2635 return -1;
2636 }
2637
2638 int lxc_fill_elevated_privileges(char *flaglist, int *flags)
2639 {
2640 char *token;
2641 int i, aflag;
2642 struct {
2643 const char *token;
2644 int flag;
2645 } all_privs[] = {
2646 { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP },
2647 { "CAP", LXC_ATTACH_DROP_CAPABILITIES },
2648 { "LSM", LXC_ATTACH_LSM_EXEC },
2649 { NULL, 0 }
2650 };
2651
2652 if (!flaglist) {
2653 /* For the sake of backward compatibility, drop all privileges
2654 * if none is specified.
2655 */
2656 for (i = 0; all_privs[i].token; i++)
2657 *flags |= all_privs[i].flag;
2658
2659 return 0;
2660 }
2661
2662 lxc_iterate_parts(token, flaglist, "|") {
2663 aflag = -1;
2664
2665 for (i = 0; all_privs[i].token; i++)
2666 if (!strcmp(all_privs[i].token, token))
2667 aflag = all_privs[i].flag;
2668
2669 if (aflag < 0)
2670 return -1;
2671
2672 *flags |= aflag;
2673 }
2674
2675 return 0;
2676 }
2677
2678 /* Write out a configuration file. */
2679 int write_config(int fd, const struct lxc_conf *conf)
2680 {
2681 int ret;
2682 size_t len = conf->unexpanded_len;
2683
2684 if (len == 0)
2685 return 0;
2686
2687 ret = lxc_write_nointr(fd, conf->unexpanded_config, len);
2688 if (ret < 0) {
2689 SYSERROR("Failed to write configuration file");
2690 return -1;
2691 }
2692
2693 return 0;
2694 }
2695
2696 bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key,
2697 const char *v)
2698 {
2699 int ret;
2700 size_t len;
2701 char *tmp;
2702
2703 len = strlen(key) + strlen(v) + 4;
2704 tmp = alloca(len);
2705
2706 if (lxc_config_value_empty(v))
2707 ret = snprintf(tmp, len, "%s =", key);
2708 else
2709 ret = snprintf(tmp, len, "%s = %s", key, v);
2710 if (ret < 0 || ret >= len)
2711 return false;
2712
2713 /* Save the line verbatim into unexpanded_conf */
2714 if (append_unexp_config_line(tmp, conf))
2715 return false;
2716
2717 return true;
2718 }
2719
2720 void clear_unexp_config_line(struct lxc_conf *conf, const char *key,
2721 bool rm_subkeys)
2722 {
2723 char *lend;
2724 char *lstart = conf->unexpanded_config;
2725
2726 if (!conf->unexpanded_config)
2727 return;
2728
2729 while (*lstart) {
2730 lend = strchr(lstart, '\n');
2731 char v;
2732
2733 if (!lend)
2734 lend = lstart + strlen(lstart);
2735 else
2736 lend++;
2737
2738 if (strncmp(lstart, key, strlen(key)) != 0) {
2739 lstart = lend;
2740 continue;
2741 }
2742
2743 if (!rm_subkeys) {
2744 v = lstart[strlen(key)];
2745 if (!isspace(v) && v != '=') {
2746 lstart = lend;
2747 continue;
2748 }
2749 }
2750
2751 conf->unexpanded_len -= (lend - lstart);
2752
2753 if (*lend == '\0') {
2754 *lstart = '\0';
2755 return;
2756 }
2757
2758 memmove(lstart, lend, strlen(lend) + 1);
2759 }
2760 }
2761
2762 bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath,
2763 const char *newpath, const char *oldname,
2764 const char *newname, const char *ovldir)
2765 {
2766 int ret;
2767 char *lend, *newdir, *olddir, *p, *q;
2768 size_t newdirlen, olddirlen;
2769 char *lstart = conf->unexpanded_config;
2770 const char *key = "lxc.mount.entry";
2771
2772 olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2;
2773 olddir = alloca(olddirlen + 1);
2774 ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath,
2775 oldname);
2776 if (ret < 0 || ret >= olddirlen + 1)
2777 return false;
2778
2779 newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2;
2780 newdir = alloca(newdirlen + 1);
2781 ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath,
2782 newname);
2783 if (ret < 0 || ret >= newdirlen + 1)
2784 return false;
2785
2786 if (!conf->unexpanded_config)
2787 return true;
2788
2789 while (*lstart) {
2790 lend = strchr(lstart, '\n');
2791 if (!lend)
2792 lend = lstart + strlen(lstart);
2793 else
2794 lend++;
2795
2796 if (strncmp(lstart, key, strlen(key)) != 0)
2797 goto next;
2798
2799 p = strchr(lstart + strlen(key), '=');
2800 if (!p)
2801 goto next;
2802 p++;
2803
2804 while (isblank(*p))
2805 p++;
2806
2807 if (p >= lend)
2808 goto next;
2809
2810 /* Whenever a lxc.mount.entry entry is found in a line we check
2811 * if the substring "overlay" is present before doing any
2812 * further work. We check for "overlay" because substrings need
2813 * to have at least one space before them in a valid overlay
2814 * lxc.mount.entry (/A B overlay). When the space before is
2815 * missing it is very likely that these substrings are part of a
2816 * path or something else. (Checking q >= lend ensures that we
2817 * only count matches in the current line.) */
2818 q = strstr(p, " overlay");
2819 if (!q || q >= lend)
2820 goto next;
2821
2822 if (!(q = strstr(p, olddir)) || (q >= lend))
2823 goto next;
2824
2825 /* replace the olddir with newdir */
2826 if (olddirlen >= newdirlen) {
2827 size_t diff = olddirlen - newdirlen;
2828 memcpy(q, newdir, newdirlen);
2829
2830 if (olddirlen != newdirlen) {
2831 memmove(q + newdirlen, q + newdirlen + diff,
2832 strlen(q) - newdirlen - diff + 1);
2833 lend -= diff;
2834 conf->unexpanded_len -= diff;
2835 }
2836 } else {
2837 char *new;
2838 size_t diff = newdirlen - olddirlen;
2839 size_t oldlen = conf->unexpanded_len;
2840 size_t newlen = oldlen + diff;
2841 size_t poffset = q - conf->unexpanded_config;
2842
2843 new = realloc(conf->unexpanded_config, newlen + 1);
2844 if (!new)
2845 return false;
2846
2847 conf->unexpanded_len = newlen;
2848 conf->unexpanded_alloced = newlen + 1;
2849 new[newlen - 1] = '\0';
2850 lend = new + (lend - conf->unexpanded_config);
2851
2852 /* Move over the remainder to make room for the newdir.
2853 */
2854 memmove(new + poffset + newdirlen,
2855 new + poffset + olddirlen,
2856 oldlen - poffset - olddirlen + 1);
2857 conf->unexpanded_config = new;
2858
2859 memcpy(new + poffset, newdir, newdirlen);
2860 lend += diff;
2861 }
2862
2863 next:
2864 lstart = lend;
2865 }
2866
2867 return true;
2868 }
2869
2870 bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath,
2871 const char *newpath, const char *oldname,
2872 const char *newname)
2873 {
2874 int ret;
2875 char *lend, *newdir, *olddir, *p;
2876 char *lstart = conf->unexpanded_config;
2877 size_t newdirlen, olddirlen;
2878 const char *key = "lxc.hook";
2879
2880 olddirlen = strlen(oldpath) + strlen(oldname) + 1;
2881 olddir = alloca(olddirlen + 1);
2882 ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname);
2883 if (ret < 0 || ret >= olddirlen + 1)
2884 return false;
2885
2886 newdirlen = strlen(newpath) + strlen(newname) + 1;
2887 newdir = alloca(newdirlen + 1);
2888 ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname);
2889 if (ret < 0 || ret >= newdirlen + 1)
2890 return false;
2891
2892 if (!conf->unexpanded_config)
2893 return true;
2894
2895 while (*lstart) {
2896 lend = strchr(lstart, '\n');
2897 if (!lend)
2898 lend = lstart + strlen(lstart);
2899 else
2900 lend++;
2901
2902 if (strncmp(lstart, key, strlen(key)) != 0)
2903 goto next;
2904
2905 p = strchr(lstart + strlen(key), '=');
2906 if (!p)
2907 goto next;
2908 p++;
2909
2910 while (isblank(*p))
2911 p++;
2912
2913 if (p >= lend)
2914 goto next;
2915
2916 if (strncmp(p, olddir, strlen(olddir)) != 0)
2917 goto next;
2918
2919 /* replace the olddir with newdir */
2920 if (olddirlen >= newdirlen) {
2921 size_t diff = olddirlen - newdirlen;
2922 memcpy(p, newdir, newdirlen);
2923
2924 if (olddirlen != newdirlen) {
2925 memmove(p + newdirlen, p + newdirlen + diff,
2926 strlen(p) - newdirlen - diff + 1);
2927 lend -= diff;
2928 conf->unexpanded_len -= diff;
2929 }
2930 } else {
2931 char *new;
2932 size_t diff = newdirlen - olddirlen;
2933 size_t oldlen = conf->unexpanded_len;
2934 size_t newlen = oldlen + diff;
2935 size_t poffset = p - conf->unexpanded_config;
2936
2937 new = realloc(conf->unexpanded_config, newlen + 1);
2938 if (!new)
2939 return false;
2940
2941 conf->unexpanded_len = newlen;
2942 conf->unexpanded_alloced = newlen + 1;
2943 new[newlen - 1] = '\0';
2944 lend = new + (lend - conf->unexpanded_config);
2945
2946 /* Move over the remainder to make room for the newdir.
2947 */
2948 memmove(new + poffset + newdirlen,
2949 new + poffset + olddirlen,
2950 oldlen - poffset - olddirlen + 1);
2951 conf->unexpanded_config = new;
2952
2953 memcpy(new + poffset, newdir, newdirlen);
2954 lend += diff;
2955 }
2956
2957 next:
2958 lstart = lend;
2959 }
2960
2961 return true;
2962 }
2963
2964 #define DO(cmd) \
2965 { \
2966 if (!(cmd)) { \
2967 ERROR("Error writing to new config"); \
2968 return false; \
2969 } \
2970 }
2971
2972 /* This is called only from clone. We wish to update all hwaddrs in the
2973 * unexpanded config file. We can't/don't want to update any which come from
2974 * lxc.includes (there shouldn't be any).
2975 * We can't just walk the c->lxc-conf->network list because that includes netifs
2976 * from the include files. So we update the ones which we find in the unexp
2977 * config file, then find the original macaddr in the conf->network, and update
2978 * that to the same value.
2979 */
2980 bool network_new_hwaddrs(struct lxc_conf *conf)
2981 {
2982 char *lend, *p, *p2;
2983 struct lxc_list *it;
2984 char *lstart = conf->unexpanded_config;
2985
2986 if (!conf->unexpanded_config)
2987 return true;
2988
2989 while (*lstart) {
2990 char newhwaddr[18], oldhwaddr[17];
2991
2992 lend = strchr(lstart, '\n');
2993 if (!lend)
2994 lend = lstart + strlen(lstart);
2995 else
2996 lend++;
2997
2998 if (!lxc_config_net_hwaddr(lstart)) {
2999 lstart = lend;
3000 continue;
3001 }
3002
3003 p = strchr(lstart, '=');
3004 if (!p) {
3005 lstart = lend;
3006 continue;
3007 }
3008
3009 p++;
3010 while (isblank(*p))
3011 p++;
3012 if (!*p)
3013 return true;
3014
3015 p2 = p;
3016 while (*p2 && !isblank(*p2) && *p2 != '\n')
3017 p2++;
3018
3019 if ((p2 - p) != 17) {
3020 WARN("Bad hwaddr entry");
3021 lstart = lend;
3022 continue;
3023 }
3024
3025 memcpy(oldhwaddr, p, 17);
3026
3027 if (!new_hwaddr(newhwaddr))
3028 return false;
3029
3030 memcpy(p, newhwaddr, 17);
3031 lxc_list_for_each(it, &conf->network) {
3032 struct lxc_netdev *n = it->elem;
3033
3034 if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0)
3035 memcpy(n->hwaddr, newhwaddr, 17);
3036 }
3037
3038 lstart = lend;
3039 }
3040
3041 return true;
3042 }
3043
3044 static int set_config_ephemeral(const char *key, const char *value,
3045 struct lxc_conf *lxc_conf, void *data)
3046 {
3047 if (lxc_config_value_empty(value)) {
3048 lxc_conf->ephemeral = 0;
3049 return 0;
3050 }
3051
3052 if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0)
3053 return -1;
3054
3055 if (lxc_conf->ephemeral > 1)
3056 return -1;
3057
3058 return 0;
3059 }
3060
3061 static int set_config_log_syslog(const char *key, const char *value,
3062 struct lxc_conf *lxc_conf, void *data)
3063 {
3064 int facility;
3065
3066 if (lxc_conf->syslog) {
3067 free(lxc_conf->syslog);
3068 lxc_conf->syslog = NULL;
3069 }
3070
3071 if (lxc_config_value_empty(value))
3072 return 0;
3073
3074 facility = lxc_syslog_priority_to_int(value);
3075 if (facility == -EINVAL)
3076 return -1;
3077
3078 lxc_log_syslog(facility);
3079
3080 return set_config_string_item(&lxc_conf->syslog, value);
3081 }
3082
3083 static int set_config_no_new_privs(const char *key, const char *value,
3084 struct lxc_conf *lxc_conf, void *data)
3085 {
3086 unsigned int v;
3087
3088 if (lxc_config_value_empty(value)) {
3089 lxc_conf->no_new_privs = false;
3090 return 0;
3091 }
3092
3093 if (lxc_safe_uint(value, &v) < 0)
3094 return -1;
3095
3096 if (v > 1)
3097 return -1;
3098
3099 lxc_conf->no_new_privs = v ? true : false;
3100
3101 return 0;
3102 }
3103
3104 /* Callbacks to get configuration items. */
3105 static int get_config_personality(const char *key, char *retv, int inlen,
3106 struct lxc_conf *c, void *data)
3107 {
3108 int fulllen = 0;
3109
3110 if (!retv)
3111 inlen = 0;
3112 else
3113 memset(retv, 0, inlen);
3114
3115 #if HAVE_SYS_PERSONALITY_H
3116 int len = 0;
3117
3118 switch (c->personality) {
3119 case PER_LINUX32:
3120 strprint(retv, inlen, "i686");
3121 break;
3122 case PER_LINUX:
3123 strprint(retv, inlen, "x86_64");
3124 break;
3125 default:
3126 break;
3127 }
3128 #endif
3129
3130 return fulllen;
3131 }
3132
3133 static int get_config_pty_max(const char *key, char *retv, int inlen,
3134 struct lxc_conf *c, void *data)
3135 {
3136 return lxc_get_conf_size_t(c, retv, inlen, c->pty_max);
3137 }
3138
3139 static int get_config_tty_max(const char *key, char *retv, int inlen,
3140 struct lxc_conf *c, void *data)
3141 {
3142 return lxc_get_conf_size_t(c, retv, inlen, c->ttys.max);
3143 }
3144
3145 static int get_config_tty_dir(const char *key, char *retv, int inlen,
3146 struct lxc_conf *c, void *data)
3147 {
3148 return lxc_get_conf_str(retv, inlen, c->ttys.dir);
3149 }
3150
3151 static int get_config_apparmor_profile(const char *key, char *retv, int inlen,
3152 struct lxc_conf *c, void *data)
3153 {
3154 return lxc_get_conf_str(retv, inlen, c->lsm_aa_profile);
3155 }
3156
3157 static int get_config_apparmor_allow_incomplete(const char *key, char *retv,
3158 int inlen, struct lxc_conf *c,
3159 void *data)
3160 {
3161 return lxc_get_conf_int(c, retv, inlen,
3162 c->lsm_aa_allow_incomplete);
3163 }
3164
3165 static int get_config_apparmor_allow_nesting(const char *key, char *retv,
3166 int inlen, struct lxc_conf *c,
3167 void *data)
3168 {
3169 return lxc_get_conf_int(c, retv, inlen,
3170 c->lsm_aa_allow_nesting);
3171 }
3172
3173 static int get_config_apparmor_raw(const char *key, char *retv,
3174 int inlen, struct lxc_conf *c,
3175 void *data)
3176 {
3177 int len;
3178 struct lxc_list *it;
3179 int fulllen = 0;
3180
3181 if (!retv)
3182 inlen = 0;
3183 else
3184 memset(retv, 0, inlen);
3185
3186 lxc_list_for_each(it, &c->lsm_aa_raw) {
3187 strprint(retv, inlen, "%s\n", (char *)it->elem);
3188 }
3189
3190 return fulllen;
3191 }
3192
3193 static int get_config_selinux_context(const char *key, char *retv, int inlen,
3194 struct lxc_conf *c, void *data)
3195 {
3196 return lxc_get_conf_str(retv, inlen, c->lsm_se_context);
3197 }
3198
3199 /* If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, then
3200 * just the value(s) will be printed. Since there still could be more than one,
3201 * it is newline-separated.
3202 * (Maybe that's ambiguous, since some values, i.e. devices.list, will already
3203 * have newlines?)
3204 * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, in
3205 * 'lxc.cgroup.subsystem.key = value' format.
3206 */
3207 static int __get_config_cgroup_controller(const char *key, char *retv,
3208 int inlen, struct lxc_conf *c,
3209 int version)
3210 {
3211 int len;
3212 size_t namespaced_token_len;
3213 char *global_token, *namespaced_token;
3214 struct lxc_list *it;
3215 int fulllen = 0;
3216 bool get_all = false;
3217
3218 if (!retv)
3219 inlen = 0;
3220 else
3221 memset(retv, 0, inlen);
3222
3223 if (version == CGROUP2_SUPER_MAGIC) {
3224 global_token = "lxc.cgroup2";
3225 namespaced_token = "lxc.cgroup2.";
3226 namespaced_token_len = STRLITERALLEN("lxc.cgroup2.");
3227 } else if (version == CGROUP_SUPER_MAGIC) {
3228 global_token = "lxc.cgroup";
3229 namespaced_token = "lxc.cgroup.";
3230 namespaced_token_len = STRLITERALLEN("lxc.cgroup.");
3231 } else {
3232 return -1;
3233 }
3234
3235 if (strcmp(key, global_token) == 0)
3236 get_all = true;
3237 else if (strncmp(key, namespaced_token, namespaced_token_len) == 0)
3238 key += namespaced_token_len;
3239 else
3240 return -1;
3241
3242 lxc_list_for_each(it, &c->cgroup) {
3243 struct lxc_cgroup *cg = it->elem;
3244
3245 if (get_all) {
3246 if (version != cg->version)
3247 continue;
3248
3249 strprint(retv, inlen, "%s.%s = %s\n", global_token,
3250 cg->subsystem, cg->value);
3251 } else if (strcmp(cg->subsystem, key) == 0) {
3252 strprint(retv, inlen, "%s\n", cg->value);
3253 }
3254 }
3255
3256 return fulllen;
3257 }
3258
3259 static int get_config_cgroup_controller(const char *key, char *retv, int inlen,
3260 struct lxc_conf *c, void *data)
3261 {
3262 return __get_config_cgroup_controller(key, retv, inlen, c,
3263 CGROUP_SUPER_MAGIC);
3264 }
3265
3266 static int get_config_cgroup2_controller(const char *key, char *retv, int inlen,
3267 struct lxc_conf *c, void *data)
3268 {
3269 return __get_config_cgroup_controller(key, retv, inlen, c,
3270 CGROUP2_SUPER_MAGIC);
3271 }
3272
3273 static int get_config_cgroup_dir(const char *key, char *retv, int inlen,
3274 struct lxc_conf *lxc_conf, void *data)
3275 {
3276 int len;
3277 int fulllen = 0;
3278
3279 if (!retv)
3280 inlen = 0;
3281 else
3282 memset(retv, 0, inlen);
3283
3284 strprint(retv, inlen, "%s", lxc_conf->cgroup_meta.dir);
3285
3286 return fulllen;
3287 }
3288
3289 static inline int get_config_cgroup_relative(const char *key, char *retv,
3290 int inlen, struct lxc_conf *lxc_conf,
3291 void *data)
3292 {
3293 return lxc_get_conf_int(lxc_conf, retv, inlen,
3294 lxc_conf->cgroup_meta.relative);
3295 }
3296
3297 static int get_config_idmaps(const char *key, char *retv, int inlen,
3298 struct lxc_conf *c, void *data)
3299 {
3300 struct lxc_list *it;
3301 int len, listlen, ret;
3302 int fulllen = 0;
3303 /* "u 1000 1000000 65536"
3304 *
3305 * let's render this as
3306 *
3307 * sizeof(char)
3308 * +
3309 * sizeof(" ")
3310 * +
3311 * sizeof(uint32_t)
3312 * +
3313 * sizeof(" ")
3314 * +
3315 * sizeof(uint32_t)
3316 * +
3317 * sizeof(" ")
3318 * +
3319 * sizeof(uint32_t)
3320 * +
3321 * \0
3322 */
3323 #define __LXC_IDMAP_STR_BUF (3 * INTTYPE_TO_STRLEN(uint32_t) + 3 + 1 + 1)
3324 char buf[__LXC_IDMAP_STR_BUF];
3325
3326 if (!retv)
3327 inlen = 0;
3328 else
3329 memset(retv, 0, inlen);
3330
3331 listlen = lxc_list_len(&c->id_map);
3332 lxc_list_for_each(it, &c->id_map) {
3333 struct id_map *map = it->elem;
3334 ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu",
3335 (map->idtype == ID_TYPE_UID) ? 'u' : 'g',
3336 map->nsid, map->hostid, map->range);
3337 if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF)
3338 return -1;
3339
3340 strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : "");
3341 }
3342
3343 return fulllen;
3344 }
3345
3346 static int get_config_log_level(const char *key, char *retv, int inlen,
3347 struct lxc_conf *c, void *data)
3348 {
3349 const char *v;
3350 v = lxc_log_priority_to_string(c->loglevel);
3351 return lxc_get_conf_str(retv, inlen, v);
3352 }
3353
3354 static int get_config_log_file(const char *key, char *retv, int inlen,
3355 struct lxc_conf *c, void *data)
3356 {
3357 return lxc_get_conf_str(retv, inlen, c->logfile);
3358 }
3359
3360 static int get_config_mount_fstab(const char *key, char *retv, int inlen,
3361 struct lxc_conf *c, void *data)
3362 {
3363 return lxc_get_conf_str(retv, inlen, c->fstab);
3364 }
3365
3366 static int get_config_mount_auto(const char *key, char *retv, int inlen,
3367 struct lxc_conf *c, void *data)
3368 {
3369 int len, fulllen = 0;
3370 const char *sep = "";
3371
3372 if (!retv)
3373 inlen = 0;
3374 else
3375 memset(retv, 0, inlen);
3376
3377 if (!(c->auto_mounts & LXC_AUTO_ALL_MASK))
3378 return 0;
3379
3380 switch (c->auto_mounts & LXC_AUTO_PROC_MASK) {
3381 case LXC_AUTO_PROC_MIXED:
3382 strprint(retv, inlen, "%sproc:mixed", sep);
3383 sep = " ";
3384 break;
3385 case LXC_AUTO_PROC_RW:
3386 strprint(retv, inlen, "%sproc:rw", sep);
3387 sep = " ";
3388 break;
3389 default:
3390 break;
3391 }
3392
3393 switch (c->auto_mounts & LXC_AUTO_SYS_MASK) {
3394 case LXC_AUTO_SYS_RO:
3395 strprint(retv, inlen, "%ssys:ro", sep);
3396 sep = " ";
3397 break;
3398 case LXC_AUTO_SYS_RW:
3399 strprint(retv, inlen, "%ssys:rw", sep);
3400 sep = " ";
3401 break;
3402 case LXC_AUTO_SYS_MIXED:
3403 strprint(retv, inlen, "%ssys:mixed", sep);
3404 sep = " ";
3405 break;
3406 default:
3407 break;
3408 }
3409
3410 switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) {
3411 case LXC_AUTO_CGROUP_NOSPEC:
3412 strprint(retv, inlen, "%scgroup", sep);
3413 break;
3414 case LXC_AUTO_CGROUP_MIXED:
3415 strprint(retv, inlen, "%scgroup:mixed", sep);
3416 break;
3417 case LXC_AUTO_CGROUP_RO:
3418 strprint(retv, inlen, "%scgroup:ro", sep);
3419 break;
3420 case LXC_AUTO_CGROUP_RW:
3421 strprint(retv, inlen, "%scgroup:rw", sep);
3422 break;
3423 case LXC_AUTO_CGROUP_FULL_NOSPEC:
3424 strprint(retv, inlen, "%scgroup-full", sep);
3425 break;
3426 case LXC_AUTO_CGROUP_FULL_MIXED:
3427 strprint(retv, inlen, "%scgroup-full:mixed", sep);
3428 break;
3429 case LXC_AUTO_CGROUP_FULL_RO:
3430 strprint(retv, inlen, "%scgroup-full:ro", sep);
3431 break;
3432 case LXC_AUTO_CGROUP_FULL_RW:
3433 strprint(retv, inlen, "%scgroup-full:rw", sep);
3434 break;
3435 default:
3436 break;
3437 }
3438
3439 return fulllen;
3440 }
3441
3442 static int get_config_mount(const char *key, char *retv, int inlen,
3443 struct lxc_conf *c, void *data)
3444 {
3445 int len, fulllen = 0;
3446 struct lxc_list *it;
3447
3448 if (!retv)
3449 inlen = 0;
3450 else
3451 memset(retv, 0, inlen);
3452
3453 lxc_list_for_each(it, &c->mount_list) {
3454 strprint(retv, inlen, "%s\n", (char *)it->elem);
3455 }
3456
3457 return fulllen;
3458 }
3459
3460 static int get_config_rootfs_path(const char *key, char *retv, int inlen,
3461 struct lxc_conf *c, void *data)
3462 {
3463 return lxc_get_conf_str(retv, inlen, c->rootfs.path);
3464 }
3465
3466 static int get_config_rootfs_managed(const char *key, char *retv, int inlen,
3467 struct lxc_conf *c, void *data)
3468 {
3469 return lxc_get_conf_bool(c, retv, inlen, c->rootfs.managed);
3470 }
3471
3472 static int get_config_rootfs_mount(const char *key, char *retv, int inlen,
3473 struct lxc_conf *c, void *data)
3474 {
3475 return lxc_get_conf_str(retv, inlen, c->rootfs.mount);
3476 }
3477
3478 static int get_config_rootfs_options(const char *key, char *retv, int inlen,
3479 struct lxc_conf *c, void *data)
3480 {
3481 return lxc_get_conf_str(retv, inlen, c->rootfs.options);
3482 }
3483
3484 static int get_config_uts_name(const char *key, char *retv, int inlen,
3485 struct lxc_conf *c, void *data)
3486 {
3487 return lxc_get_conf_str(
3488 retv, inlen,
3489 c->utsname ? c->utsname->nodename : NULL);
3490 }
3491
3492 static int get_config_hooks(const char *key, char *retv, int inlen,
3493 struct lxc_conf *c, void *data)
3494 {
3495 char *subkey;
3496 int len, fulllen = 0, found = -1;
3497 struct lxc_list *it;
3498 int i;
3499
3500 subkey = strchr(key, '.');
3501 if (!subkey)
3502 return -1;
3503
3504 subkey = strchr(subkey + 1, '.');
3505 if (!subkey)
3506 return -1;
3507 subkey++;
3508 if (*subkey == '\0')
3509 return -1;
3510
3511 for (i = 0; i < NUM_LXC_HOOKS; i++) {
3512 if (strcmp(lxchook_names[i], subkey) == 0) {
3513 found = i;
3514 break;
3515 }
3516 }
3517
3518 if (found == -1)
3519 return -1;
3520
3521 if (!retv)
3522 inlen = 0;
3523 else
3524 memset(retv, 0, inlen);
3525
3526 lxc_list_for_each(it, &c->hooks[found]) {
3527 strprint(retv, inlen, "%s\n", (char *)it->elem);
3528 }
3529
3530 return fulllen;
3531 }
3532
3533 static int get_config_hooks_version(const char *key, char *retv, int inlen,
3534 struct lxc_conf *c, void *data)
3535 {
3536 return lxc_get_conf_int(c, retv, inlen, c->hooks_version);
3537 }
3538
3539 static int get_config_net(const char *key, char *retv, int inlen,
3540 struct lxc_conf *c, void *data)
3541 {
3542 int len, fulllen = 0;
3543 struct lxc_list *it;
3544
3545 if (!retv)
3546 inlen = 0;
3547 else
3548 memset(retv, 0, inlen);
3549
3550 lxc_list_for_each(it, &c->network) {
3551 struct lxc_netdev *n = it->elem;
3552 const char *t = lxc_net_type_to_str(n->type);
3553 strprint(retv, inlen, "%s\n", t ? t : "(invalid)");
3554 }
3555
3556 return fulllen;
3557 }
3558
3559 static int get_config_cap_drop(const char *key, char *retv, int inlen,
3560 struct lxc_conf *c, void *data)
3561 {
3562 int len, fulllen = 0;
3563 struct lxc_list *it;
3564
3565 if (!retv)
3566 inlen = 0;
3567 else
3568 memset(retv, 0, inlen);
3569
3570 lxc_list_for_each(it, &c->caps) {
3571 strprint(retv, inlen, "%s\n", (char *)it->elem);
3572 }
3573
3574 return fulllen;
3575 }
3576
3577 static int get_config_cap_keep(const char *key, char *retv, int inlen,
3578 struct lxc_conf *c, void *data)
3579 {
3580 int len, fulllen = 0;
3581 struct lxc_list *it;
3582
3583 if (!retv)
3584 inlen = 0;
3585 else
3586 memset(retv, 0, inlen);
3587
3588 lxc_list_for_each(it, &c->keepcaps) {
3589 strprint(retv, inlen, "%s\n", (char *)it->elem);
3590 }
3591
3592 return fulllen;
3593 }
3594
3595 static int get_config_console_path(const char *key, char *retv, int inlen,
3596 struct lxc_conf *c, void *data)
3597 {
3598 return lxc_get_conf_str(retv, inlen, c->console.path);
3599 }
3600
3601 static int get_config_console_logfile(const char *key, char *retv, int inlen,
3602 struct lxc_conf *c, void *data)
3603 {
3604 return lxc_get_conf_str(retv, inlen, c->console.log_path);
3605 }
3606
3607 static int get_config_console_rotate(const char *key, char *retv, int inlen,
3608 struct lxc_conf *c, void *data)
3609 {
3610 return lxc_get_conf_int(c, retv, inlen, c->console.log_rotate);
3611 }
3612
3613
3614 static int get_config_console_buffer_size(const char *key, char *retv,
3615 int inlen, struct lxc_conf *c,
3616 void *data)
3617 {
3618 return lxc_get_conf_uint64(c, retv, inlen, c->console.buffer_size);
3619 }
3620
3621 static int get_config_console_size(const char *key, char *retv, int inlen,
3622 struct lxc_conf *c, void *data)
3623 {
3624 return lxc_get_conf_uint64(c, retv, inlen, c->console.log_size);
3625 }
3626
3627
3628 static int get_config_seccomp_profile(const char *key, char *retv, int inlen,
3629 struct lxc_conf *c, void *data)
3630 {
3631 return lxc_get_conf_str(retv, inlen, c->seccomp);
3632 }
3633
3634 static int get_config_autodev(const char *key, char *retv, int inlen,
3635 struct lxc_conf *c, void *data)
3636 {
3637 return lxc_get_conf_int(c, retv, inlen, c->autodev);
3638 }
3639
3640 static int get_config_signal_halt(const char *key, char *retv, int inlen,
3641 struct lxc_conf *c, void *data)
3642 {
3643 return lxc_get_conf_int(c, retv, inlen, c->haltsignal);
3644 }
3645
3646 static int get_config_signal_reboot(const char *key, char *retv, int inlen,
3647 struct lxc_conf *c, void *data)
3648 {
3649 return lxc_get_conf_int(c, retv, inlen, c->rebootsignal);
3650 }
3651
3652 static int get_config_signal_stop(const char *key, char *retv, int inlen,
3653 struct lxc_conf *c, void *data)
3654 {
3655 return lxc_get_conf_int(c, retv, inlen, c->stopsignal);
3656 }
3657
3658 static int get_config_start(const char *key, char *retv, int inlen,
3659 struct lxc_conf *c, void *data)
3660 {
3661 if (strcmp(key + 10, "auto") == 0)
3662 return lxc_get_conf_int(c, retv, inlen, c->start_auto);
3663 else if (strcmp(key + 10, "delay") == 0)
3664 return lxc_get_conf_int(c, retv, inlen, c->start_delay);
3665 else if (strcmp(key + 10, "order") == 0)
3666 return lxc_get_conf_int(c, retv, inlen, c->start_order);
3667
3668 return -1;
3669 }
3670
3671 static int get_config_log_syslog(const char *key, char *retv, int inlen,
3672 struct lxc_conf *c, void *data)
3673 {
3674 return lxc_get_conf_str(retv, inlen, c->syslog);
3675 }
3676
3677 static int get_config_monitor(const char *key, char *retv, int inlen,
3678 struct lxc_conf *c, void *data)
3679 {
3680 return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare);
3681 }
3682
3683 static int get_config_monitor_signal_pdeath(const char *key, char *retv,
3684 int inlen, struct lxc_conf *c,
3685 void *data)
3686 {
3687 return lxc_get_conf_int(c, retv, inlen, c->monitor_signal_pdeath);
3688 }
3689
3690 static int get_config_group(const char *key, char *retv, int inlen,
3691 struct lxc_conf *c, void *data)
3692 {
3693 int len, fulllen = 0;
3694 struct lxc_list *it;
3695
3696 if (!retv)
3697 inlen = 0;
3698 else
3699 memset(retv, 0, inlen);
3700
3701 lxc_list_for_each(it, &c->groups) {
3702 strprint(retv, inlen, "%s\n", (char *)it->elem);
3703 }
3704
3705 return fulllen;
3706 }
3707
3708 static int get_config_environment(const char *key, char *retv, int inlen,
3709 struct lxc_conf *c, void *data)
3710 {
3711 int len, fulllen = 0;
3712 struct lxc_list *it;
3713
3714 if (!retv)
3715 inlen = 0;
3716 else
3717 memset(retv, 0, inlen);
3718
3719 lxc_list_for_each(it, &c->environment) {
3720 strprint(retv, inlen, "%s\n", (char *)it->elem);
3721 }
3722
3723 return fulllen;
3724 }
3725
3726 static int get_config_execute_cmd(const char *key, char *retv, int inlen,
3727 struct lxc_conf *c, void *data)
3728 {
3729 return lxc_get_conf_str(retv, inlen, c->execute_cmd);
3730 }
3731
3732 static int get_config_init_cmd(const char *key, char *retv, int inlen,
3733 struct lxc_conf *c, void *data)
3734 {
3735 return lxc_get_conf_str(retv, inlen, c->init_cmd);
3736 }
3737
3738 static int get_config_init_cwd(const char *key, char *retv, int inlen,
3739 struct lxc_conf *c, void *data)
3740 {
3741 return lxc_get_conf_str(retv, inlen, c->init_cwd);
3742 }
3743
3744 static int get_config_init_uid(const char *key, char *retv, int inlen,
3745 struct lxc_conf *c, void *data)
3746 {
3747 return lxc_get_conf_int(c, retv, inlen, c->init_uid);
3748 }
3749
3750 static int get_config_init_gid(const char *key, char *retv, int inlen,
3751 struct lxc_conf *c, void *data)
3752 {
3753 return lxc_get_conf_int(c, retv, inlen, c->init_gid);
3754 }
3755
3756 static int get_config_ephemeral(const char *key, char *retv, int inlen,
3757 struct lxc_conf *c, void *data)
3758 {
3759 return lxc_get_conf_int(c, retv, inlen, c->ephemeral);
3760 }
3761
3762 static int get_config_no_new_privs(const char *key, char *retv, int inlen,
3763 struct lxc_conf *c, void *data)
3764 {
3765 return lxc_get_conf_int(c, retv, inlen, c->no_new_privs);
3766 }
3767
3768 /* If you ask for a specific value, i.e. lxc.prlimit.nofile, then just the value
3769 * will be printed. If you ask for 'lxc.prlimit', then all limit entries will be
3770 * printed, in 'lxc.prlimit.resource = value' format.
3771 */
3772 static int get_config_prlimit(const char *key, char *retv, int inlen,
3773 struct lxc_conf *c, void *data)
3774 {
3775 int fulllen = 0, len;
3776 bool get_all = false;
3777 struct lxc_list *it;
3778
3779 if (!retv)
3780 inlen = 0;
3781 else
3782 memset(retv, 0, inlen);
3783
3784 if (!strcmp(key, "lxc.prlimit"))
3785 get_all = true;
3786 else if (strncmp(key, "lxc.prlimit.", 12) == 0)
3787 key += 12;
3788 else
3789 return -1;
3790
3791 lxc_list_for_each(it, &c->limits) {
3792 /* 2 colon separated 64 bit integers or the word 'unlimited' */
3793 char buf[INTTYPE_TO_STRLEN(uint64_t) * 2 + 2];
3794 int partlen;
3795 struct lxc_limit *lim = it->elem;
3796
3797 if (lim->limit.rlim_cur == RLIM_INFINITY) {
3798 memcpy(buf, "unlimited", STRLITERALLEN("unlimited") + 1);
3799 partlen = STRLITERALLEN("unlimited");
3800 } else {
3801 partlen = sprintf(buf, "%" PRIu64,
3802 (uint64_t)lim->limit.rlim_cur);
3803 }
3804
3805 if (lim->limit.rlim_cur != lim->limit.rlim_max) {
3806 if (lim->limit.rlim_max == RLIM_INFINITY)
3807 memcpy(buf + partlen, ":unlimited",
3808 STRLITERALLEN(":unlimited") + 1);
3809 else
3810 sprintf(buf + partlen, ":%" PRIu64,
3811 (uint64_t)lim->limit.rlim_max);
3812 }
3813
3814 if (get_all) {
3815 strprint(retv, inlen, "lxc.prlimit.%s = %s\n",
3816 lim->resource, buf);
3817 } else if (strcmp(lim->resource, key) == 0) {
3818 strprint(retv, inlen, "%s", buf);
3819 }
3820 }
3821
3822 return fulllen;
3823 }
3824
3825 /* If you ask for a specific value, i.e. lxc.sysctl.net.ipv4.ip_forward, then
3826 * just the value will be printed. If you ask for 'lxc.sysctl', then all sysctl
3827 * entries will be printed, in 'lxc.sysctl.key = value' format.
3828 */
3829 static int get_config_sysctl(const char *key, char *retv, int inlen,
3830 struct lxc_conf *c, void *data)
3831 {
3832 int len;
3833 struct lxc_list *it;
3834 int fulllen = 0;
3835 bool get_all = false;
3836
3837 if (!retv)
3838 inlen = 0;
3839 else
3840 memset(retv, 0, inlen);
3841
3842 if (strcmp(key, "lxc.sysctl") == 0)
3843 get_all = true;
3844 else if (strncmp(key, "lxc.sysctl.", STRLITERALLEN("lxc.sysctl.")) == 0)
3845 key += STRLITERALLEN("lxc.sysctl.");
3846 else
3847 return -1;
3848
3849 lxc_list_for_each(it, &c->sysctls) {
3850 struct lxc_sysctl *elem = it->elem;
3851 if (get_all) {
3852 strprint(retv, inlen, "lxc.sysctl.%s = %s\n", elem->key,
3853 elem->value);
3854 } else if (strcmp(elem->key, key) == 0) {
3855 strprint(retv, inlen, "%s", elem->value);
3856 }
3857 }
3858
3859 return fulllen;
3860 }
3861
3862 static int get_config_proc(const char *key, char *retv, int inlen,
3863 struct lxc_conf *c, void *data)
3864 {
3865 struct lxc_list *it;
3866 int len;
3867 int fulllen = 0;
3868 bool get_all = false;
3869
3870 if (!retv)
3871 inlen = 0;
3872 else
3873 memset(retv, 0, inlen);
3874
3875 if (strcmp(key, "lxc.proc") == 0)
3876 get_all = true;
3877 else if (strncmp(key, "lxc.proc.", STRLITERALLEN("lxc.proc.")) == 0)
3878 key += STRLITERALLEN("lxc.proc.");
3879 else
3880 return -1;
3881
3882 lxc_list_for_each(it, &c->procs) {
3883 struct lxc_proc *proc = it->elem;
3884
3885 if (get_all) {
3886 strprint(retv, inlen, "lxc.proc.%s = %s\n",
3887 proc->filename, proc->value);
3888 } else if (strcmp(proc->filename, key) == 0) {
3889 strprint(retv, inlen, "%s", proc->value);
3890 }
3891 }
3892
3893 return fulllen;
3894 }
3895
3896 static int get_config_namespace_clone(const char *key, char *retv, int inlen,
3897 struct lxc_conf *c, void *data)
3898 {
3899 int i, len;
3900 int fulllen = 0;
3901
3902 if (!retv)
3903 inlen = 0;
3904 else
3905 memset(retv, 0, inlen);
3906
3907 for (i = 0; i < LXC_NS_MAX; i++) {
3908 if (c->ns_clone & ns_info[i].clone_flag)
3909 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
3910 }
3911
3912 return fulllen;
3913 }
3914
3915 static int get_config_namespace_keep(const char *key, char *retv, int inlen,
3916 struct lxc_conf *c, void *data)
3917 {
3918 int i, len;
3919 int fulllen = 0;
3920
3921 if (!retv)
3922 inlen = 0;
3923 else
3924 memset(retv, 0, inlen);
3925
3926 for (i = 0; i < LXC_NS_MAX; i++) {
3927 if (c->ns_keep & ns_info[i].clone_flag)
3928 strprint(retv, inlen, "%s\n", ns_info[i].proc_name);
3929 }
3930
3931 return fulllen;
3932 }
3933
3934 static int get_config_namespace_share(const char *key, char *retv, int inlen,
3935 struct lxc_conf *c, void *data)
3936 {
3937 int len, ns_idx;
3938 const char *namespace;
3939 int fulllen = 0;
3940
3941 if (!retv)
3942 inlen = 0;
3943 else
3944 memset(retv, 0, inlen);
3945
3946 namespace = key + STRLITERALLEN("lxc.namespace.share.");
3947 ns_idx = lxc_namespace_2_ns_idx(namespace);
3948 if (ns_idx < 0)
3949 return ns_idx;
3950
3951 strprint(retv, inlen, "%s", c->ns_share[ns_idx]);
3952
3953 return fulllen;
3954 }
3955
3956 /* Callbacks to clear config items. */
3957 static inline int clr_config_personality(const char *key, struct lxc_conf *c,
3958 void *data)
3959 {
3960 c->personality = -1;
3961 return 0;
3962 }
3963
3964 static inline int clr_config_pty_max(const char *key, struct lxc_conf *c,
3965 void *data)
3966 {
3967 c->pty_max = 0;
3968 return 0;
3969 }
3970
3971 static inline int clr_config_tty_max(const char *key, struct lxc_conf *c,
3972 void *data)
3973 {
3974 c->ttys.tty = 0;
3975 return 0;
3976 }
3977
3978 static inline int clr_config_tty_dir(const char *key, struct lxc_conf *c,
3979 void *data)
3980 {
3981 free(c->ttys.dir);
3982 c->ttys.dir = NULL;
3983 return 0;
3984 }
3985
3986 static inline int clr_config_apparmor_profile(const char *key,
3987 struct lxc_conf *c, void *data)
3988 {
3989 free(c->lsm_aa_profile);
3990 c->lsm_aa_profile = NULL;
3991 return 0;
3992 }
3993
3994 static inline int clr_config_apparmor_allow_incomplete(const char *key,
3995 struct lxc_conf *c,
3996 void *data)
3997 {
3998 c->lsm_aa_allow_incomplete = 0;
3999 return 0;
4000 }
4001
4002 static inline int clr_config_apparmor_allow_nesting(const char *key,
4003 struct lxc_conf *c,
4004 void *data)
4005 {
4006 c->lsm_aa_allow_nesting = 0;
4007 return 0;
4008 }
4009
4010 static inline int clr_config_apparmor_raw(const char *key,
4011 struct lxc_conf *c,
4012 void *data)
4013 {
4014 return lxc_clear_apparmor_raw(c);
4015 }
4016
4017 static inline int clr_config_selinux_context(const char *key,
4018 struct lxc_conf *c, void *data)
4019 {
4020 free(c->lsm_se_context);
4021 c->lsm_se_context = NULL;
4022 return 0;
4023 }
4024
4025 static inline int clr_config_cgroup_controller(const char *key,
4026 struct lxc_conf *c, void *data)
4027 {
4028 return lxc_clear_cgroups(c, key, CGROUP_SUPER_MAGIC);
4029 }
4030
4031 static inline int clr_config_cgroup2_controller(const char *key,
4032 struct lxc_conf *c, void *data)
4033 {
4034 return lxc_clear_cgroups(c, key, CGROUP2_SUPER_MAGIC);
4035 }
4036
4037 static int clr_config_cgroup_dir(const char *key, struct lxc_conf *lxc_conf,
4038 void *data)
4039 {
4040 if (lxc_conf->cgroup_meta.dir) {
4041 free(lxc_conf->cgroup_meta.dir);
4042 lxc_conf->cgroup_meta.dir = NULL;
4043 }
4044
4045 return 0;
4046 }
4047
4048 static inline int clr_config_cgroup_relative(const char *key,
4049 struct lxc_conf *lxc_conf,
4050 void *data)
4051 {
4052 lxc_conf->cgroup_meta.relative = false;
4053 return 0;
4054 }
4055
4056 static inline int clr_config_idmaps(const char *key, struct lxc_conf *c,
4057 void *data)
4058 {
4059 return lxc_clear_idmaps(c);
4060 }
4061
4062 static inline int clr_config_log_level(const char *key, struct lxc_conf *c,
4063 void *data)
4064 {
4065 c->loglevel = LXC_LOG_LEVEL_NOTSET;
4066 return 0;
4067 }
4068
4069 static inline int clr_config_log_file(const char *key, struct lxc_conf *c,
4070 void *data)
4071 {
4072 free(c->logfile);
4073 c->logfile = NULL;
4074 return 0;
4075 }
4076
4077 static inline int clr_config_mount(const char *key, struct lxc_conf *c,
4078 void *data)
4079 {
4080 return lxc_clear_mount_entries(c);
4081 }
4082
4083 static inline int clr_config_mount_auto(const char *key, struct lxc_conf *c,
4084 void *data)
4085 {
4086 return lxc_clear_automounts(c);
4087 }
4088
4089 static inline int clr_config_mount_fstab(const char *key, struct lxc_conf *c,
4090 void *data)
4091 {
4092 free(c->fstab);
4093 c->fstab = NULL;
4094 return 0;
4095 }
4096
4097 static inline int clr_config_rootfs_path(const char *key, struct lxc_conf *c,
4098 void *data)
4099 {
4100 free(c->rootfs.path);
4101 c->rootfs.path = NULL;
4102 return 0;
4103 }
4104
4105 static inline int clr_config_rootfs_managed(const char *key, struct lxc_conf *c,
4106 void *data)
4107 {
4108 c->rootfs.managed = true;
4109 return 0;
4110 }
4111
4112 static inline int clr_config_rootfs_mount(const char *key, struct lxc_conf *c,
4113 void *data)
4114 {
4115 free(c->rootfs.mount);
4116 c->rootfs.mount = NULL;
4117 return 0;
4118 }
4119
4120 static inline int clr_config_rootfs_options(const char *key, struct lxc_conf *c,
4121 void *data)
4122 {
4123 free(c->rootfs.options);
4124 c->rootfs.options = NULL;
4125
4126 free(c->rootfs.data);
4127 c->rootfs.data = NULL;
4128
4129 return 0;
4130 }
4131
4132 static inline int clr_config_uts_name(const char *key, struct lxc_conf *c,
4133 void *data)
4134 {
4135 free(c->utsname);
4136 c->utsname = NULL;
4137 return 0;
4138 }
4139
4140 static inline int clr_config_hooks(const char *key, struct lxc_conf *c,
4141 void *data)
4142 {
4143 return lxc_clear_hooks(c, key);
4144 }
4145
4146 static inline int clr_config_hooks_version(const char *key, struct lxc_conf *c,
4147 void *data)
4148 {
4149 /* default to legacy hooks version */
4150 c->hooks_version = 0;
4151 return 0;
4152 }
4153
4154 static inline int clr_config_net(const char *key, struct lxc_conf *c,
4155 void *data)
4156 {
4157 lxc_free_networks(&c->network);
4158
4159 return 0;
4160 }
4161
4162 static inline int clr_config_cap_drop(const char *key, struct lxc_conf *c,
4163 void *data)
4164 {
4165 return lxc_clear_config_caps(c);
4166 }
4167
4168 static inline int clr_config_cap_keep(const char *key, struct lxc_conf *c,
4169 void *data)
4170 {
4171 return lxc_clear_config_keepcaps(c);
4172 }
4173
4174 static inline int clr_config_console_path(const char *key, struct lxc_conf *c,
4175 void *data)
4176 {
4177 free(c->console.path);
4178 c->console.path = NULL;
4179 return 0;
4180 }
4181
4182 static inline int clr_config_console_logfile(const char *key,
4183 struct lxc_conf *c, void *data)
4184 {
4185 free(c->console.log_path);
4186 c->console.log_path = NULL;
4187 return 0;
4188 }
4189
4190 static inline int clr_config_console_rotate(const char *key, struct lxc_conf *c,
4191 void *data)
4192 {
4193 c->console.log_rotate = 0;
4194 return 0;
4195 }
4196
4197 static inline int clr_config_console_buffer_size(const char *key,
4198 struct lxc_conf *c, void *data)
4199 {
4200 c->console.buffer_size = 0;
4201 return 0;
4202 }
4203
4204 static inline int clr_config_console_size(const char *key, struct lxc_conf *c,
4205 void *data)
4206 {
4207 c->console.log_size = 0;
4208 return 0;
4209 }
4210
4211 static inline int clr_config_seccomp_profile(const char *key,
4212 struct lxc_conf *c, void *data)
4213 {
4214 free(c->seccomp);
4215 c->seccomp = NULL;
4216 return 0;
4217 }
4218
4219 static inline int clr_config_autodev(const char *key, struct lxc_conf *c,
4220 void *data)
4221 {
4222 c->autodev = 1;
4223 return 0;
4224 }
4225
4226 static inline int clr_config_signal_halt(const char *key, struct lxc_conf *c,
4227 void *data)
4228 {
4229 c->haltsignal = 0;
4230 return 0;
4231 }
4232
4233 static inline int clr_config_signal_reboot(const char *key, struct lxc_conf *c,
4234 void *data)
4235 {
4236 c->rebootsignal = 0;
4237 return 0;
4238 }
4239
4240 static inline int clr_config_signal_stop(const char *key, struct lxc_conf *c,
4241 void *data)
4242 {
4243 c->stopsignal = 0;
4244 return 0;
4245 }
4246
4247 static inline int clr_config_start(const char *key, struct lxc_conf *c,
4248 void *data)
4249 {
4250 if (strcmp(key + 10, "auto") == 0)
4251 c->start_auto = 0;
4252 else if (strcmp(key + 10, "delay") == 0)
4253 c->start_delay = 0;
4254 else if (strcmp(key + 10, "order") == 0)
4255 c->start_order = 0;
4256
4257 return 0;
4258 }
4259
4260 static inline int clr_config_log_syslog(const char *key, struct lxc_conf *c,
4261 void *data)
4262 {
4263 free(c->syslog);
4264 c->syslog = NULL;
4265 return 0;
4266 }
4267
4268 static inline int clr_config_monitor(const char *key, struct lxc_conf *c,
4269 void *data)
4270 {
4271 c->monitor_unshare = 0;
4272 return 0;
4273 }
4274
4275 static inline int clr_config_monitor_signal_pdeath(const char *key,
4276 struct lxc_conf *c, void *data)
4277 {
4278 c->monitor_signal_pdeath = 0;
4279 return 0;
4280 }
4281
4282 static inline int clr_config_group(const char *key, struct lxc_conf *c,
4283 void *data)
4284 {
4285 return lxc_clear_groups(c);
4286 }
4287
4288 static inline int clr_config_environment(const char *key, struct lxc_conf *c,
4289 void *data)
4290 {
4291 return lxc_clear_environment(c);
4292 }
4293
4294 static inline int clr_config_execute_cmd(const char *key, struct lxc_conf *c,
4295 void *data)
4296 {
4297 free(c->execute_cmd);
4298 c->execute_cmd = NULL;
4299 return 0;
4300 }
4301
4302 static inline int clr_config_init_cmd(const char *key, struct lxc_conf *c,
4303 void *data)
4304 {
4305 free(c->init_cmd);
4306 c->init_cmd = NULL;
4307 return 0;
4308 }
4309
4310 static inline int clr_config_init_cwd(const char *key, struct lxc_conf *c,
4311 void *data)
4312 {
4313 free(c->init_cwd);
4314 c->init_cwd = NULL;
4315 return 0;
4316 }
4317
4318 static inline int clr_config_init_uid(const char *key, struct lxc_conf *c,
4319 void *data)
4320 {
4321 c->init_uid = 0;
4322 return 0;
4323 }
4324
4325 static inline int clr_config_init_gid(const char *key, struct lxc_conf *c,
4326 void *data)
4327 {
4328 c->init_gid = 0;
4329 return 0;
4330 }
4331
4332 static inline int clr_config_ephemeral(const char *key, struct lxc_conf *c,
4333 void *data)
4334 {
4335 c->ephemeral = 0;
4336 return 0;
4337 }
4338
4339 static inline int clr_config_no_new_privs(const char *key, struct lxc_conf *c,
4340 void *data)
4341 {
4342 c->no_new_privs = false;
4343 return 0;
4344 }
4345
4346 static inline int clr_config_prlimit(const char *key, struct lxc_conf *c,
4347 void *data)
4348 {
4349 return lxc_clear_limits(c, key);
4350 }
4351
4352 static inline int clr_config_sysctl(const char *key, struct lxc_conf *c,
4353 void *data)
4354 {
4355 return lxc_clear_sysctls(c, key);
4356 }
4357
4358 static inline int clr_config_proc(const char *key, struct lxc_conf *c,
4359 void *data)
4360 {
4361 return lxc_clear_procs(c, key);
4362 }
4363
4364 static inline int clr_config_includefiles(const char *key, struct lxc_conf *c,
4365 void *data)
4366 {
4367 lxc_clear_includes(c);
4368 return 0;
4369 }
4370
4371 static int clr_config_namespace_clone(const char *key,
4372 struct lxc_conf *lxc_conf, void *data)
4373 {
4374 lxc_conf->ns_clone = 0;
4375 return 0;
4376 }
4377
4378 static int clr_config_namespace_keep(const char *key, struct lxc_conf *lxc_conf,
4379 void *data)
4380 {
4381 lxc_conf->ns_keep = 0;
4382 return 0;
4383 }
4384
4385 static int clr_config_namespace_share(const char *key,
4386 struct lxc_conf *lxc_conf, void *data)
4387 {
4388 int ns_idx;
4389 const char *namespace;
4390
4391 namespace = key + STRLITERALLEN("lxc.namespace.share.");
4392 ns_idx = lxc_namespace_2_ns_idx(namespace);
4393 if (ns_idx < 0)
4394 return ns_idx;
4395
4396 free(lxc_conf->ns_share[ns_idx]);
4397 lxc_conf->ns_share[ns_idx] = NULL;
4398
4399 return 0;
4400 }
4401
4402 static int get_config_includefiles(const char *key, char *retv, int inlen,
4403 struct lxc_conf *c, void *data)
4404 {
4405 return -ENOSYS;
4406 }
4407
4408 static struct lxc_config_t *get_network_config_ops(const char *key,
4409 struct lxc_conf *lxc_conf,
4410 ssize_t *idx,
4411 char **deindexed_key)
4412 {
4413 int ret;
4414 unsigned int tmpidx;
4415 size_t numstrlen;
4416 char *copy, *idx_start, *idx_end;
4417 struct lxc_config_t *config = NULL;
4418
4419 /* check that this is a sensible network key */
4420 if (strncmp("lxc.net.", key, 8)) {
4421 ERROR("Invalid network configuration key \"%s\"", key);
4422 return NULL;
4423 }
4424
4425 copy = strdup(key);
4426 if (!copy) {
4427 ERROR("Failed to duplicate string \"%s\"", key);
4428 return NULL;
4429 }
4430
4431 /* lxc.net.<n> */
4432 if (!isdigit(*(key + 8))) {
4433 ERROR("Failed to detect digit in string \"%s\"", key + 8);
4434 goto on_error;
4435 }
4436
4437 /* beginning of index string */
4438 idx_start = (copy + 7);
4439 *idx_start = '\0';
4440
4441 /* end of index string */
4442 idx_end = strchr((copy + 8), '.');
4443 if (idx_end)
4444 *idx_end = '\0';
4445
4446 /* parse current index */
4447 ret = lxc_safe_uint((idx_start + 1), &tmpidx);
4448 if (ret < 0) {
4449 errno = -ret;
4450 SYSERROR("Failed to parse unsigned integer from string \"%s\"",
4451 idx_start + 1);
4452 *idx = ret;
4453 goto on_error;
4454 }
4455
4456 /* This, of course is utterly nonsensical on so many levels, but
4457 * better safe than sorry.
4458 * (Checking for INT_MAX here is intentional.)
4459 */
4460 if (tmpidx == INT_MAX) {
4461 SYSERROR("Number of configured networks would overflow the "
4462 "counter");
4463 goto on_error;
4464 }
4465 *idx = tmpidx;
4466
4467 numstrlen = strlen((idx_start + 1));
4468
4469 /* repair configuration key */
4470 *idx_start = '.';
4471
4472 /* lxc.net.<idx>.<subkey> */
4473 if (idx_end) {
4474 *idx_end = '.';
4475 if (strlen(idx_end + 1) == 0) {
4476 ERROR("No subkey in network configuration key \"%s\"", key);
4477 goto on_error;
4478 }
4479
4480 memmove(copy + 8, idx_end + 1, strlen(idx_end + 1));
4481 copy[strlen(key) - numstrlen + 1] = '\0';
4482
4483 config = lxc_get_config(copy);
4484 if (!config) {
4485 ERROR("Unknown network configuration key \"%s\"", key);
4486 goto on_error;
4487 }
4488 }
4489
4490 if (deindexed_key)
4491 *deindexed_key = copy;
4492
4493 return config;
4494
4495 on_error:
4496 free(copy);
4497 return NULL;
4498 }
4499
4500 /* Config entry is something like "lxc.net.0.ipv4" the key 'lxc.net.' was
4501 * found. So we make sure next comes an integer, find the right callback (by
4502 * rewriting the key), and call it.
4503 */
4504 static int set_config_net_nic(const char *key, const char *value,
4505 struct lxc_conf *lxc_conf, void *data)
4506 {
4507 int ret;
4508 const char *idxstring;
4509 struct lxc_config_t *config;
4510 struct lxc_netdev *netdev;
4511 ssize_t idx = -1;
4512 char *deindexed_key = NULL;
4513
4514 idxstring = key + 8;
4515 if (!isdigit(*idxstring))
4516 return -1;
4517
4518 if (lxc_config_value_empty(value))
4519 return clr_config_net_nic(key, lxc_conf, data);
4520
4521 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
4522 if (!config || idx < 0)
4523 return -1;
4524
4525 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, true);
4526 if (!netdev) {
4527 free(deindexed_key);
4528 return -1;
4529 }
4530
4531 ret = config->set(deindexed_key, value, lxc_conf, netdev);
4532 free(deindexed_key);
4533
4534 return ret;
4535 }
4536
4537 static int clr_config_net_nic(const char *key, struct lxc_conf *lxc_conf,
4538 void *data)
4539 {
4540 int ret;
4541 const char *idxstring;
4542 struct lxc_config_t *config;
4543 struct lxc_netdev *netdev;
4544 ssize_t idx = -1;
4545 char *deindexed_key = NULL;
4546
4547 idxstring = key + 8;
4548 if (!isdigit(*idxstring))
4549 return -1;
4550
4551 /* The left conjunct is pretty self-explanatory. The right conjunct
4552 * checks whether the two pointers are equal. If they are we know that
4553 * this is not a key that is namespaced any further and so we are
4554 * supposed to clear the whole network.
4555 */
4556 if (isdigit(*idxstring) && (strrchr(key, '.') == (idxstring - 1))) {
4557 unsigned int rmnetdevidx;
4558
4559 if (lxc_safe_uint(idxstring, &rmnetdevidx) < 0)
4560 return -1;
4561
4562 /* Remove network from network list. */
4563 lxc_remove_nic_by_idx(lxc_conf, rmnetdevidx);
4564 return 0;
4565 }
4566
4567 config = get_network_config_ops(key, lxc_conf, &idx, &deindexed_key);
4568 if (!config || idx < 0)
4569 return -1;
4570
4571 netdev = lxc_get_netdev_by_idx(lxc_conf, (unsigned int)idx, false);
4572 if (!netdev) {
4573 free(deindexed_key);
4574 return -1;
4575 }
4576
4577 ret = config->clr(deindexed_key, lxc_conf, netdev);
4578 free(deindexed_key);
4579
4580 return ret;
4581 }
4582
4583 static int clr_config_net_type(const char *key, struct lxc_conf *lxc_conf,
4584 void *data)
4585 {
4586 struct lxc_netdev *netdev = data;
4587
4588 if (!netdev)
4589 return -1;
4590
4591 netdev->type = -1;
4592
4593 return 0;
4594 }
4595
4596 static int clr_config_net_name(const char *key, struct lxc_conf *lxc_conf,
4597 void *data)
4598 {
4599 struct lxc_netdev *netdev = data;
4600
4601 if (!netdev)
4602 return -1;
4603
4604 netdev->name[0] = '\0';
4605
4606 return 0;
4607 }
4608
4609 static int clr_config_net_flags(const char *key, struct lxc_conf *lxc_conf,
4610 void *data)
4611 {
4612 struct lxc_netdev *netdev = data;
4613
4614 if (!netdev)
4615 return -1;
4616
4617 netdev->flags = 0;
4618
4619 return 0;
4620 }
4621
4622 static int clr_config_net_link(const char *key, struct lxc_conf *lxc_conf,
4623 void *data)
4624 {
4625 struct lxc_netdev *netdev = data;
4626
4627 if (!netdev)
4628 return -1;
4629
4630 netdev->link[0] = '\0';
4631
4632 return 0;
4633 }
4634
4635 static int clr_config_net_macvlan_mode(const char *key,
4636 struct lxc_conf *lxc_conf, void *data)
4637 {
4638 struct lxc_netdev *netdev = data;
4639
4640 if (!netdev)
4641 return -1;
4642
4643 if (netdev->type != LXC_NET_MACVLAN)
4644 return 0;
4645
4646 netdev->priv.macvlan_attr.mode = -1;
4647
4648 return 0;
4649 }
4650
4651 static int clr_config_net_veth_pair(const char *key, struct lxc_conf *lxc_conf,
4652 void *data)
4653 {
4654 struct lxc_netdev *netdev = data;
4655
4656 if (!netdev)
4657 return -1;
4658
4659 netdev->priv.veth_attr.pair[0] = '\0';
4660
4661 return 0;
4662 }
4663
4664 static int clr_config_net_script_up(const char *key, struct lxc_conf *lxc_conf,
4665 void *data)
4666 {
4667 struct lxc_netdev *netdev = data;
4668
4669 if (!netdev)
4670 return -1;
4671
4672 free(netdev->upscript);
4673 netdev->upscript = NULL;
4674
4675 return 0;
4676 }
4677
4678 static int clr_config_net_script_down(const char *key,
4679 struct lxc_conf *lxc_conf, void *data)
4680 {
4681 struct lxc_netdev *netdev = data;
4682
4683 if (!netdev)
4684 return -1;
4685
4686 free(netdev->downscript);
4687 netdev->downscript = NULL;
4688
4689 return 0;
4690 }
4691
4692 static int clr_config_net_hwaddr(const char *key, struct lxc_conf *lxc_conf,
4693 void *data)
4694 {
4695 struct lxc_netdev *netdev = data;
4696
4697 if (!netdev)
4698 return -1;
4699
4700 free(netdev->hwaddr);
4701 netdev->hwaddr = NULL;
4702
4703 return 0;
4704 }
4705
4706 static int clr_config_net_mtu(const char *key, struct lxc_conf *lxc_conf,
4707 void *data)
4708 {
4709 struct lxc_netdev *netdev = data;
4710
4711 if (!netdev)
4712 return -1;
4713
4714 free(netdev->mtu);
4715 netdev->mtu = NULL;
4716
4717 return 0;
4718 }
4719
4720 static int clr_config_net_vlan_id(const char *key, struct lxc_conf *lxc_conf,
4721 void *data)
4722 {
4723 struct lxc_netdev *netdev = data;
4724
4725 if (!netdev)
4726 return -1;
4727
4728 netdev->priv.vlan_attr.vid = 0;
4729
4730 return 0;
4731 }
4732
4733 static int clr_config_net_ipv4_gateway(const char *key,
4734 struct lxc_conf *lxc_conf, void *data)
4735 {
4736 struct lxc_netdev *netdev = data;
4737
4738 if (!netdev)
4739 return -1;
4740
4741 free(netdev->ipv4_gateway);
4742 netdev->ipv4_gateway = NULL;
4743
4744 return 0;
4745 }
4746
4747 static int clr_config_net_ipv4_address(const char *key,
4748 struct lxc_conf *lxc_conf, void *data)
4749 {
4750 struct lxc_netdev *netdev = data;
4751 struct lxc_list *cur, *next;
4752
4753 if (!netdev)
4754 return -1;
4755
4756 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
4757 lxc_list_del(cur);
4758 free(cur->elem);
4759 free(cur);
4760 }
4761
4762 return 0;
4763 }
4764
4765 static int clr_config_net_ipv6_gateway(const char *key,
4766 struct lxc_conf *lxc_conf, void *data)
4767 {
4768 struct lxc_netdev *netdev = data;
4769
4770 if (!netdev)
4771 return -1;
4772
4773 free(netdev->ipv6_gateway);
4774 netdev->ipv6_gateway = NULL;
4775
4776 return 0;
4777 }
4778
4779 static int clr_config_net_ipv6_address(const char *key,
4780 struct lxc_conf *lxc_conf, void *data)
4781 {
4782 struct lxc_netdev *netdev = data;
4783 struct lxc_list *cur, *next;
4784
4785 if (!netdev)
4786 return -1;
4787
4788 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
4789 lxc_list_del(cur);
4790 free(cur->elem);
4791 free(cur);
4792 }
4793
4794 return 0;
4795 }
4796
4797 static int get_config_net_nic(const char *key, char *retv, int inlen,
4798 struct lxc_conf *c, void *data)
4799 {
4800 int ret;
4801 const char *idxstring;
4802 struct lxc_config_t *config;
4803 struct lxc_netdev *netdev;
4804 ssize_t idx = -1;
4805 char *deindexed_key = NULL;
4806
4807 idxstring = key + 8;
4808 if (!isdigit(*idxstring))
4809 return -1;
4810
4811 config = get_network_config_ops(key, c, &idx, &deindexed_key);
4812 if (!config || idx < 0)
4813 return -1;
4814
4815 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
4816 if (!netdev) {
4817 free(deindexed_key);
4818 return -1;
4819 }
4820
4821 ret = config->get(deindexed_key, retv, inlen, c, netdev);
4822 free(deindexed_key);
4823
4824 return ret;
4825 }
4826
4827 static int get_config_net_type(const char *key, char *retv, int inlen,
4828 struct lxc_conf *c, void *data)
4829 {
4830 int len;
4831 int fulllen = 0;
4832 struct lxc_netdev *netdev = data;
4833
4834 if (!retv)
4835 inlen = 0;
4836 else
4837 memset(retv, 0, inlen);
4838
4839 if (!netdev)
4840 return -1;
4841
4842 strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type));
4843
4844 return fulllen;
4845 }
4846
4847 static int get_config_net_flags(const char *key, char *retv, int inlen,
4848 struct lxc_conf *c, void *data)
4849 {
4850 int len;
4851 int fulllen = 0;
4852 struct lxc_netdev *netdev = data;
4853
4854 if (!retv)
4855 inlen = 0;
4856 else
4857 memset(retv, 0, inlen);
4858
4859 if (!netdev)
4860 return -1;
4861
4862 if (netdev->flags & IFF_UP)
4863 strprint(retv, inlen, "up");
4864
4865 return fulllen;
4866 }
4867
4868 static int get_config_net_link(const char *key, char *retv, int inlen,
4869 struct lxc_conf *c, void *data)
4870 {
4871 int len;
4872 int fulllen = 0;
4873 struct lxc_netdev *netdev = data;
4874
4875 if (!retv)
4876 inlen = 0;
4877 else
4878 memset(retv, 0, inlen);
4879
4880 if (!netdev)
4881 return -1;
4882
4883 if (netdev->link[0] != '\0')
4884 strprint(retv, inlen, "%s", netdev->link);
4885
4886 return fulllen;
4887 }
4888
4889 static int get_config_net_name(const char *key, char *retv, int inlen,
4890 struct lxc_conf *c, void *data)
4891 {
4892 int len;
4893 int fulllen = 0;
4894 struct lxc_netdev *netdev = data;
4895
4896 if (!retv)
4897 inlen = 0;
4898 else
4899 memset(retv, 0, inlen);
4900
4901 if (!netdev)
4902 return -1;
4903
4904 if (netdev->name[0] != '\0')
4905 strprint(retv, inlen, "%s", netdev->name);
4906
4907 return fulllen;
4908 }
4909
4910 static int get_config_net_macvlan_mode(const char *key, char *retv, int inlen,
4911 struct lxc_conf *c, void *data)
4912 {
4913 int len;
4914 int fulllen = 0;
4915 const char *mode;
4916 struct lxc_netdev *netdev = data;
4917
4918 if (!retv)
4919 inlen = 0;
4920 else
4921 memset(retv, 0, inlen);
4922
4923 if (!netdev)
4924 return -1;
4925
4926 if (netdev->type != LXC_NET_MACVLAN)
4927 return 0;
4928
4929 switch (netdev->priv.macvlan_attr.mode) {
4930 case MACVLAN_MODE_PRIVATE:
4931 mode = "private";
4932 break;
4933 case MACVLAN_MODE_VEPA:
4934 mode = "vepa";
4935 break;
4936 case MACVLAN_MODE_BRIDGE:
4937 mode = "bridge";
4938 break;
4939 case MACVLAN_MODE_PASSTHRU:
4940 mode = "passthru";
4941 break;
4942 default:
4943 mode = "(invalid)";
4944 break;
4945 }
4946
4947 strprint(retv, inlen, "%s", mode);
4948
4949 return fulllen;
4950 }
4951
4952 static int get_config_net_veth_pair(const char *key, char *retv, int inlen,
4953 struct lxc_conf *c, void *data)
4954 {
4955 int len;
4956 int fulllen = 0;
4957 struct lxc_netdev *netdev = data;
4958
4959 if (!retv)
4960 inlen = 0;
4961 else
4962 memset(retv, 0, inlen);
4963
4964 if (!netdev)
4965 return -1;
4966
4967 if (netdev->type != LXC_NET_VETH)
4968 return 0;
4969
4970 strprint(retv, inlen, "%s",
4971 netdev->priv.veth_attr.pair[0] != '\0'
4972 ? netdev->priv.veth_attr.pair
4973 : netdev->priv.veth_attr.veth1);
4974
4975 return fulllen;
4976 }
4977
4978 static int get_config_net_script_up(const char *key, char *retv, int inlen,
4979 struct lxc_conf *c, void *data)
4980 {
4981 int len;
4982 int fulllen = 0;
4983 struct lxc_netdev *netdev = data;
4984
4985 if (!retv)
4986 inlen = 0;
4987 else
4988 memset(retv, 0, inlen);
4989
4990 if (!netdev)
4991 return -1;
4992
4993 if (netdev->upscript)
4994 strprint(retv, inlen, "%s", netdev->upscript);
4995
4996 return fulllen;
4997 }
4998
4999 static int get_config_net_script_down(const char *key, char *retv, int inlen,
5000 struct lxc_conf *c, void *data)
5001 {
5002 int len;
5003 int fulllen = 0;
5004 struct lxc_netdev *netdev = data;
5005
5006 if (!retv)
5007 inlen = 0;
5008 else
5009 memset(retv, 0, inlen);
5010
5011 if (!netdev)
5012 return -1;
5013
5014 if (netdev->downscript)
5015 strprint(retv, inlen, "%s", netdev->downscript);
5016
5017 return fulllen;
5018 }
5019
5020 static int get_config_net_hwaddr(const char *key, char *retv, int inlen,
5021 struct lxc_conf *c, void *data)
5022 {
5023 int len;
5024 int fulllen = 0;
5025 struct lxc_netdev *netdev = data;
5026
5027 if (!retv)
5028 inlen = 0;
5029 else
5030 memset(retv, 0, inlen);
5031
5032 if (!netdev)
5033 return -1;
5034
5035 if (netdev->hwaddr)
5036 strprint(retv, inlen, "%s", netdev->hwaddr);
5037
5038 return fulllen;
5039 }
5040
5041 static int get_config_net_mtu(const char *key, char *retv, int inlen,
5042 struct lxc_conf *c, void *data)
5043 {
5044 int len;
5045 int fulllen = 0;
5046 struct lxc_netdev *netdev = data;
5047
5048 if (!retv)
5049 inlen = 0;
5050 else
5051 memset(retv, 0, inlen);
5052
5053 if (!netdev)
5054 return -1;
5055
5056 if (netdev->mtu)
5057 strprint(retv, inlen, "%s", netdev->mtu);
5058
5059 return fulllen;
5060 }
5061
5062 static int get_config_net_vlan_id(const char *key, char *retv, int inlen,
5063 struct lxc_conf *c, void *data)
5064 {
5065 int len;
5066 int fulllen = 0;
5067 struct lxc_netdev *netdev = data;
5068
5069 if (!retv)
5070 inlen = 0;
5071 else
5072 memset(retv, 0, inlen);
5073
5074 if (!netdev)
5075 return -1;
5076
5077 if (netdev->type != LXC_NET_VLAN)
5078 return 0;
5079
5080 strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid);
5081
5082 return fulllen;
5083 }
5084
5085 static int get_config_net_ipv4_gateway(const char *key, char *retv, int inlen,
5086 struct lxc_conf *c, void *data)
5087 {
5088 int len;
5089 char buf[INET_ADDRSTRLEN];
5090 int fulllen = 0;
5091 struct lxc_netdev *netdev = data;
5092
5093 if (!retv)
5094 inlen = 0;
5095 else
5096 memset(retv, 0, inlen);
5097
5098 if (!netdev)
5099 return -1;
5100
5101 if (netdev->ipv4_gateway_auto) {
5102 strprint(retv, inlen, "auto");
5103 } else if (netdev->ipv4_gateway) {
5104 inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf));
5105 strprint(retv, inlen, "%s", buf);
5106 }
5107
5108 return fulllen;
5109 }
5110
5111 static int get_config_net_ipv4_address(const char *key, char *retv, int inlen,
5112 struct lxc_conf *c, void *data)
5113 {
5114 int len;
5115 size_t listlen;
5116 char buf[INET_ADDRSTRLEN];
5117 struct lxc_list *it;
5118 int fulllen = 0;
5119 struct lxc_netdev *netdev = data;
5120
5121 if (!retv)
5122 inlen = 0;
5123 else
5124 memset(retv, 0, inlen);
5125
5126 if (!netdev)
5127 return -1;
5128
5129 listlen = lxc_list_len(&netdev->ipv4);
5130
5131 lxc_list_for_each(it, &netdev->ipv4) {
5132 struct lxc_inetdev *i = it->elem;
5133 inet_ntop(AF_INET, &i->addr, buf, sizeof(buf));
5134 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
5135 (listlen-- > 1) ? "\n" : "");
5136 }
5137
5138 return fulllen;
5139 }
5140
5141 static int get_config_net_ipv6_gateway(const char *key, char *retv, int inlen,
5142 struct lxc_conf *c, void *data)
5143 {
5144 int len;
5145 char buf[INET6_ADDRSTRLEN];
5146 int fulllen = 0;
5147 struct lxc_netdev *netdev = data;
5148
5149 if (!retv)
5150 inlen = 0;
5151 else
5152 memset(retv, 0, inlen);
5153
5154 if (!netdev)
5155 return -1;
5156
5157 if (netdev->ipv6_gateway_auto) {
5158 strprint(retv, inlen, "auto");
5159 } else if (netdev->ipv6_gateway) {
5160 inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf));
5161 strprint(retv, inlen, "%s", buf);
5162 }
5163
5164 return fulllen;
5165 }
5166
5167 static int get_config_net_ipv6_address(const char *key, char *retv, int inlen,
5168 struct lxc_conf *c, void *data)
5169 {
5170 int len;
5171 size_t listlen;
5172 char buf[INET6_ADDRSTRLEN];
5173 struct lxc_list *it;
5174 int fulllen = 0;
5175 struct lxc_netdev *netdev = data;
5176
5177 if (!retv)
5178 inlen = 0;
5179 else
5180 memset(retv, 0, inlen);
5181
5182 if (!netdev)
5183 return -1;
5184
5185 listlen = lxc_list_len(&netdev->ipv6);
5186
5187 lxc_list_for_each(it, &netdev->ipv6) {
5188 struct lxc_inet6dev *i = it->elem;
5189 inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf));
5190 strprint(retv, inlen, "%s/%u%s", buf, i->prefix,
5191 (listlen-- > 1) ? "\n" : "");
5192 }
5193
5194 return fulllen;
5195 }
5196
5197 int lxc_list_config_items(char *retv, int inlen)
5198 {
5199 size_t i;
5200 int len;
5201 int fulllen = 0;
5202
5203 if (!retv)
5204 inlen = 0;
5205 else
5206 memset(retv, 0, inlen);
5207
5208 for (i = 0; i < config_jump_table_size; i++) {
5209 char *s = config_jump_table[i].name;
5210
5211 if (s[strlen(s) - 1] == '.')
5212 continue;
5213
5214 strprint(retv, inlen, "%s\n", s);
5215 }
5216
5217 return fulllen;
5218 }
5219
5220 int lxc_list_subkeys(struct lxc_conf *conf, const char *key, char *retv,
5221 int inlen)
5222 {
5223 int len;
5224 int fulllen = 0;
5225
5226 if (!retv)
5227 inlen = 0;
5228 else
5229 memset(retv, 0, inlen);
5230
5231 if (!strcmp(key, "lxc.apparmor")) {
5232 strprint(retv, inlen, "allow_incomplete\n");
5233 strprint(retv, inlen, "allow_nesting\n");
5234 strprint(retv, inlen, "profile\n");
5235 strprint(retv, inlen, "raw\n");
5236 } else if (!strcmp(key, "lxc.cgroup")) {
5237 strprint(retv, inlen, "dir\n");
5238 } else if (!strcmp(key, "lxc.selinux")) {
5239 strprint(retv, inlen, "context\n");
5240 } else if (!strcmp(key, "lxc.mount")) {
5241 strprint(retv, inlen, "auto\n");
5242 strprint(retv, inlen, "entry\n");
5243 strprint(retv, inlen, "fstab\n");
5244 } else if (!strcmp(key, "lxc.rootfs")) {
5245 strprint(retv, inlen, "mount\n");
5246 strprint(retv, inlen, "options\n");
5247 strprint(retv, inlen, "path\n");
5248 } else if (!strcmp(key, "lxc.uts")) {
5249 strprint(retv, inlen, "name\n");
5250 } else if (!strcmp(key, "lxc.hook")) {
5251 strprint(retv, inlen, "autodev\n");
5252 strprint(retv, inlen, "clone\n");
5253 strprint(retv, inlen, "destroy\n");
5254 strprint(retv, inlen, "mount\n");
5255 strprint(retv, inlen, "post-stop\n");
5256 strprint(retv, inlen, "pre-mount\n");
5257 strprint(retv, inlen, "pre-start\n");
5258 strprint(retv, inlen, "start-host\n");
5259 strprint(retv, inlen, "start\n");
5260 strprint(retv, inlen, "stop\n");
5261 } else if (!strcmp(key, "lxc.cap")) {
5262 strprint(retv, inlen, "drop\n");
5263 strprint(retv, inlen, "keep\n");
5264 } else if (!strcmp(key, "lxc.console")) {
5265 strprint(retv, inlen, "logfile\n");
5266 strprint(retv, inlen, "path\n");
5267 } else if (!strcmp(key, "lxc.seccomp")) {
5268 strprint(retv, inlen, "profile\n");
5269 } else if (!strcmp(key, "lxc.signal")) {
5270 strprint(retv, inlen, "halt\n");
5271 strprint(retv, inlen, "reboot\n");
5272 strprint(retv, inlen, "stop\n");
5273 } else if (!strcmp(key, "lxc.start")) {
5274 strprint(retv, inlen, "auto\n");
5275 strprint(retv, inlen, "delay\n");
5276 strprint(retv, inlen, "order\n");
5277 } else if (!strcmp(key, "lxc.monitor")) {
5278 strprint(retv, inlen, "unshare\n");
5279 } else {
5280 fulllen = -1;
5281 }
5282
5283 return fulllen;
5284 }
5285
5286 int lxc_list_net(struct lxc_conf *c, const char *key, char *retv, int inlen)
5287 {
5288 int len;
5289 const char *idxstring;
5290 struct lxc_netdev *netdev;
5291 int fulllen = 0;
5292 ssize_t idx = -1;
5293
5294 idxstring = key + 8;
5295 if (!isdigit(*idxstring))
5296 return -1;
5297
5298 (void)get_network_config_ops(key, c, &idx, NULL);
5299 if (idx < 0)
5300 return -1;
5301
5302 netdev = lxc_get_netdev_by_idx(c, (unsigned int)idx, false);
5303 if (!netdev)
5304 return -1;
5305
5306 if (!retv)
5307 inlen = 0;
5308 else
5309 memset(retv, 0, inlen);
5310
5311 strprint(retv, inlen, "type\n");
5312 strprint(retv, inlen, "script.up\n");
5313 strprint(retv, inlen, "script.down\n");
5314
5315 if (netdev->type != LXC_NET_EMPTY) {
5316 strprint(retv, inlen, "flags\n");
5317 strprint(retv, inlen, "link\n");
5318 strprint(retv, inlen, "name\n");
5319 strprint(retv, inlen, "hwaddr\n");
5320 strprint(retv, inlen, "mtu\n");
5321 strprint(retv, inlen, "ipv6.address\n");
5322 strprint(retv, inlen, "ipv6.gateway\n");
5323 strprint(retv, inlen, "ipv4.address\n");
5324 strprint(retv, inlen, "ipv4.gateway\n");
5325 }
5326
5327 switch (netdev->type) {
5328 case LXC_NET_VETH:
5329 strprint(retv, inlen, "veth.pair\n");
5330 break;
5331 case LXC_NET_MACVLAN:
5332 strprint(retv, inlen, "macvlan.mode\n");
5333 break;
5334 case LXC_NET_VLAN:
5335 strprint(retv, inlen, "vlan.id\n");
5336 break;
5337 case LXC_NET_PHYS:
5338 break;
5339 }
5340
5341 return fulllen;
5342 }