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