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