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