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