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