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