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