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