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