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