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