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