]>
Commit | Line | Data |
---|---|---|
c2cc9f0a | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
c2cc9f0a | 8 | * |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
c2cc9f0a | 22 | */ |
12a50cc6 | 23 | #define _GNU_SOURCE |
2e6e3feb | 24 | #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */ |
c2cc9f0a | 25 | #include <stdio.h> |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <unistd.h> | |
29 | #include <errno.h> | |
63376d7d | 30 | #include <fcntl.h> |
a84b9932 | 31 | #include <ctype.h> |
2e6e3feb | 32 | #include <inttypes.h> /* Required for PRIu64 to work. */ |
a84b9932 | 33 | #include <signal.h> |
63376d7d | 34 | #include <sys/stat.h> |
c2cc9f0a | 35 | #include <sys/types.h> |
36 | #include <sys/param.h> | |
37 | #include <sys/utsname.h> | |
38 | #include <arpa/inet.h> | |
39 | #include <netinet/in.h> | |
40 | #include <net/if.h> | |
67702c21 | 41 | #include <time.h> |
e1daebd9 | 42 | #include <dirent.h> |
64c57ea1 | 43 | #include <syslog.h> |
c2cc9f0a | 44 | |
d8e48992 | 45 | #include "bdev.h" |
b2718c72 | 46 | #include "parse.h" |
6ff05e18 | 47 | #include "config.h" |
525f0002 | 48 | #include "confile.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 | 65 | lxc_log_define(lxc_confile, lxc); |
576f946d | 66 | |
713046e3 | 67 | static int set_config_personality(const char *, const char *, struct lxc_conf *); |
7b992a3e | 68 | static int get_config_personality(struct lxc_container *, const char *, char *, int); |
bdf91ab4 | 69 | |
713046e3 | 70 | static int set_config_pts(const char *, const char *, struct lxc_conf *); |
bdf91ab4 CB |
71 | static int get_config_pts(struct lxc_container *, const char *, char *, int); |
72 | ||
713046e3 | 73 | static int set_config_tty(const char *, const char *, struct lxc_conf *); |
5485782f CB |
74 | static int get_config_tty(struct lxc_container *, const char *, char *, int); |
75 | ||
713046e3 | 76 | static int set_config_ttydir(const char *, const char *, struct lxc_conf *); |
8015e018 CB |
77 | static int get_config_ttydir(struct lxc_container *, const char *, char *, int); |
78 | ||
713046e3 | 79 | static int set_config_kmsg(const char *, const char *, struct lxc_conf *); |
de1ede69 CB |
80 | static int get_config_kmsg(struct lxc_container *, const char *, char *, int); |
81 | ||
713046e3 CB |
82 | static int set_config_lsm_aa_profile(const char *, const char *, struct lxc_conf *); |
83 | static int set_config_lsm_aa_incomplete(const char *, const char *, struct lxc_conf *); | |
84 | static int set_config_lsm_se_context(const char *, const char *, struct lxc_conf *); | |
85 | static int set_config_cgroup(const char *, const char *, struct lxc_conf *); | |
86 | static int set_config_idmap(const char *, const char *, struct lxc_conf *); | |
87 | static int set_config_loglevel(const char *, const char *, struct lxc_conf *); | |
88 | static int set_config_logfile(const char *, const char *, struct lxc_conf *); | |
89 | static int set_config_mount(const char *, const char *, struct lxc_conf *); | |
90 | static int set_config_mount_auto(const char *, const char *, struct lxc_conf *); | |
91 | static int set_config_fstab(const char *, const char *, struct lxc_conf *); | |
92 | static int set_config_rootfs(const char *, const char *, struct lxc_conf *); | |
93 | static int set_config_rootfs_mount(const char *, const char *, struct lxc_conf *); | |
94 | static int set_config_rootfs_options(const char *, const char *, struct lxc_conf *); | |
95 | static int set_config_rootfs_backend(const char *, const char *, struct lxc_conf *); | |
96 | static int set_config_pivotdir(const char *, const char *, struct lxc_conf *); | |
97 | static int set_config_utsname(const char *, const char *, struct lxc_conf *); | |
98 | static int set_config_hook(const char *, const char *, struct lxc_conf *lxc_conf); | |
99 | static int set_config_network(const char *, const char *, struct lxc_conf *); | |
100 | static int set_config_network_type(const char *, const char *, struct lxc_conf *); | |
101 | static int set_config_network_flags(const char *, const char *, struct lxc_conf *); | |
102 | static int set_config_network_link(const char *, const char *, struct lxc_conf *); | |
103 | static int set_config_network_name(const char *, const char *, struct lxc_conf *); | |
104 | static int set_config_network_veth_pair(const char *, const char *, struct lxc_conf *); | |
105 | static int set_config_network_macvlan_mode(const char *, const char *, struct lxc_conf *); | |
106 | static int set_config_network_hwaddr(const char *, const char *, struct lxc_conf *); | |
107 | static int set_config_network_vlan_id(const char *, const char *, struct lxc_conf *); | |
108 | static int set_config_network_mtu(const char *, const char *, struct lxc_conf *); | |
109 | static int set_config_network_ipv4(const char *, const char *, struct lxc_conf *); | |
110 | static int set_config_network_ipv4_gateway(const char *, const char *, struct lxc_conf *); | |
111 | static int set_config_network_script_up(const char *, const char *, struct lxc_conf *); | |
112 | static int set_config_network_script_down(const char *, const char *, struct lxc_conf *); | |
113 | static int set_config_network_ipv6(const char *, const char *, struct lxc_conf *); | |
114 | static int set_config_network_ipv6_gateway(const char *, const char *, struct lxc_conf *); | |
115 | static int set_config_cap_drop(const char *, const char *, struct lxc_conf *); | |
116 | static int set_config_cap_keep(const char *, const char *, struct lxc_conf *); | |
117 | static int set_config_console(const char *, const char *, struct lxc_conf *); | |
118 | static int set_config_console_logfile(const char *, const char *, struct lxc_conf *); | |
119 | static int set_config_seccomp(const char *, const char *, struct lxc_conf *); | |
120 | static int set_config_includefile(const char *, const char *, struct lxc_conf *); | |
121 | static int set_config_network_nic(const char *, const char *, struct lxc_conf *); | |
122 | static int set_config_autodev(const char *, const char *, struct lxc_conf *); | |
123 | static int set_config_haltsignal(const char *, const char *, struct lxc_conf *); | |
124 | static int set_config_rebootsignal(const char *, const char *, struct lxc_conf *); | |
125 | static int set_config_stopsignal(const char *, const char *, struct lxc_conf *); | |
126 | static int set_config_start(const char *, const char *, struct lxc_conf *); | |
127 | static int set_config_syslog(const char *, const char *, struct lxc_conf *); | |
128 | static int set_config_monitor(const char *, const char *, struct lxc_conf *); | |
129 | static int set_config_group(const char *, const char *, struct lxc_conf *); | |
130 | static int set_config_environment(const char *, const char *, struct lxc_conf *); | |
131 | static int set_config_init_cmd(const char *, const char *, struct lxc_conf *); | |
132 | static int set_config_init_uid(const char *, const char *, struct lxc_conf *); | |
133 | static int set_config_init_gid(const char *, const char *, struct lxc_conf *); | |
134 | static int set_config_ephemeral(const char *, const char *, struct lxc_conf *); | |
135 | static int set_config_no_new_privs(const char *, const char *, struct lxc_conf *); | |
136 | static int set_config_limit(const char *, const char *, struct lxc_conf *); | |
c2cc9f0a | 137 | |
72d0e1cb | 138 | static struct lxc_config_t config[] = { |
7b992a3e | 139 | { "lxc.arch", set_config_personality, get_config_personality, NULL}, |
bdf91ab4 | 140 | { "lxc.pts", set_config_pts, get_config_pts, NULL}, |
5485782f | 141 | { "lxc.tty", set_config_tty, get_config_tty, NULL}, |
8015e018 | 142 | { "lxc.devttydir", set_config_ttydir, get_config_ttydir, NULL}, |
de1ede69 | 143 | { "lxc.kmsg", set_config_kmsg, get_config_kmsg, NULL}, |
713046e3 CB |
144 | { "lxc.aa_profile", set_config_lsm_aa_profile, NULL, NULL}, |
145 | { "lxc.aa_allow_incomplete", set_config_lsm_aa_incomplete, NULL, NULL}, | |
146 | { "lxc.se_context", set_config_lsm_se_context, NULL, NULL}, | |
147 | { "lxc.cgroup", set_config_cgroup, NULL, NULL}, | |
148 | { "lxc.id_map", set_config_idmap, NULL, NULL}, | |
149 | { "lxc.loglevel", set_config_loglevel, NULL, NULL}, | |
150 | { "lxc.logfile", set_config_logfile, NULL, NULL}, | |
151 | { "lxc.mount.entry", set_config_mount, NULL, NULL}, | |
152 | { "lxc.mount.auto", set_config_mount_auto, NULL, NULL}, | |
153 | { "lxc.mount", set_config_fstab, NULL, NULL}, | |
154 | { "lxc.rootfs.mount", set_config_rootfs_mount, NULL, NULL}, | |
155 | { "lxc.rootfs.options", set_config_rootfs_options, NULL, NULL}, | |
156 | { "lxc.rootfs.backend", set_config_rootfs_backend, NULL, NULL}, | |
157 | { "lxc.rootfs", set_config_rootfs, NULL, NULL}, | |
158 | { "lxc.pivotdir", set_config_pivotdir, NULL, NULL}, | |
159 | { "lxc.utsname", set_config_utsname, NULL, NULL}, | |
160 | { "lxc.hook.pre-start", set_config_hook, NULL, NULL}, | |
161 | { "lxc.hook.pre-mount", set_config_hook, NULL, NULL}, | |
162 | { "lxc.hook.mount", set_config_hook, NULL, NULL}, | |
163 | { "lxc.hook.autodev", set_config_hook, NULL, NULL}, | |
164 | { "lxc.hook.start", set_config_hook, NULL, NULL}, | |
165 | { "lxc.hook.stop", set_config_hook, NULL, NULL}, | |
166 | { "lxc.hook.post-stop", set_config_hook, NULL, NULL}, | |
167 | { "lxc.hook.clone", set_config_hook, NULL, NULL}, | |
168 | { "lxc.hook.destroy", set_config_hook, NULL, NULL}, | |
169 | { "lxc.hook", set_config_hook, NULL, NULL}, | |
170 | { "lxc.network.type", set_config_network_type, NULL, NULL}, | |
171 | { "lxc.network.flags", set_config_network_flags, NULL, NULL}, | |
172 | { "lxc.network.link", set_config_network_link, NULL, NULL}, | |
173 | { "lxc.network.name", set_config_network_name, NULL, NULL}, | |
174 | { "lxc.network.macvlan.mode", set_config_network_macvlan_mode, NULL, NULL}, | |
175 | { "lxc.network.veth.pair", set_config_network_veth_pair, NULL, NULL}, | |
176 | { "lxc.network.script.up", set_config_network_script_up, NULL, NULL}, | |
177 | { "lxc.network.script.down", set_config_network_script_down, NULL, NULL}, | |
178 | { "lxc.network.hwaddr", set_config_network_hwaddr, NULL, NULL}, | |
179 | { "lxc.network.mtu", set_config_network_mtu, NULL, NULL}, | |
180 | { "lxc.network.vlan.id", set_config_network_vlan_id, NULL, NULL}, | |
181 | { "lxc.network.ipv4.gateway", set_config_network_ipv4_gateway, NULL, NULL}, | |
182 | { "lxc.network.ipv4", set_config_network_ipv4, NULL, NULL}, | |
183 | { "lxc.network.ipv6.gateway", set_config_network_ipv6_gateway, NULL, NULL}, | |
184 | { "lxc.network.ipv6", set_config_network_ipv6, NULL, NULL}, | |
185 | { "lxc.network.", set_config_network_nic, NULL, NULL}, | |
186 | { "lxc.network", set_config_network, NULL, NULL}, | |
187 | { "lxc.cap.drop", set_config_cap_drop, NULL, NULL}, | |
188 | { "lxc.cap.keep", set_config_cap_keep, NULL, NULL}, | |
189 | { "lxc.console.logfile", set_config_console_logfile, NULL, NULL}, | |
190 | { "lxc.console", set_config_console, NULL, NULL}, | |
191 | { "lxc.seccomp", set_config_seccomp, NULL, NULL}, | |
192 | { "lxc.include", set_config_includefile, NULL, NULL}, | |
193 | { "lxc.autodev", set_config_autodev, NULL, NULL}, | |
194 | { "lxc.haltsignal", set_config_haltsignal, NULL, NULL}, | |
195 | { "lxc.rebootsignal", set_config_rebootsignal, NULL, NULL}, | |
196 | { "lxc.stopsignal", set_config_stopsignal, NULL, NULL}, | |
197 | { "lxc.start.auto", set_config_start, NULL, NULL}, | |
198 | { "lxc.start.delay", set_config_start, NULL, NULL}, | |
199 | { "lxc.start.order", set_config_start, NULL, NULL}, | |
200 | { "lxc.monitor.unshare", set_config_monitor, NULL, NULL}, | |
201 | { "lxc.group", set_config_group, NULL, NULL}, | |
202 | { "lxc.environment", set_config_environment, NULL, NULL}, | |
203 | { "lxc.init_cmd", set_config_init_cmd, NULL, NULL}, | |
204 | { "lxc.init_uid", set_config_init_uid, NULL, NULL}, | |
205 | { "lxc.init_gid", set_config_init_gid, NULL, NULL}, | |
206 | { "lxc.ephemeral", set_config_ephemeral, NULL, NULL}, | |
207 | { "lxc.syslog", set_config_syslog, NULL, NULL}, | |
208 | { "lxc.no_new_privs", set_config_no_new_privs, NULL, NULL}, | |
209 | { "lxc.limit", set_config_limit, NULL, NULL}, | |
a84b9932 AV |
210 | }; |
211 | ||
212 | struct signame { | |
213 | int num; | |
74a3920a | 214 | const char *name; |
a84b9932 AV |
215 | }; |
216 | ||
74a3920a | 217 | static const struct signame signames[] = { |
a84b9932 AV |
218 | { SIGHUP, "HUP" }, |
219 | { SIGINT, "INT" }, | |
220 | { SIGQUIT, "QUIT" }, | |
221 | { SIGILL, "ILL" }, | |
222 | { SIGABRT, "ABRT" }, | |
223 | { SIGFPE, "FPE" }, | |
224 | { SIGKILL, "KILL" }, | |
225 | { SIGSEGV, "SEGV" }, | |
226 | { SIGPIPE, "PIPE" }, | |
227 | { SIGALRM, "ALRM" }, | |
228 | { SIGTERM, "TERM" }, | |
229 | { SIGUSR1, "USR1" }, | |
230 | { SIGUSR2, "USR2" }, | |
231 | { SIGCHLD, "CHLD" }, | |
232 | { SIGCONT, "CONT" }, | |
233 | { SIGSTOP, "STOP" }, | |
234 | { SIGTSTP, "TSTP" }, | |
235 | { SIGTTIN, "TTIN" }, | |
236 | { SIGTTOU, "TTOU" }, | |
89dfc302 SY |
237 | #ifdef SIGTRAP |
238 | { SIGTRAP, "TRAP" }, | |
239 | #endif | |
240 | #ifdef SIGIOT | |
241 | { SIGIOT, "IOT" }, | |
242 | #endif | |
243 | #ifdef SIGEMT | |
244 | { SIGEMT, "EMT" }, | |
245 | #endif | |
246 | #ifdef SIGBUS | |
247 | { SIGBUS, "BUS" }, | |
248 | #endif | |
249 | #ifdef SIGSTKFLT | |
250 | { SIGSTKFLT, "STKFLT" }, | |
251 | #endif | |
252 | #ifdef SIGCLD | |
253 | { SIGCLD, "CLD" }, | |
254 | #endif | |
255 | #ifdef SIGURG | |
256 | { SIGURG, "URG" }, | |
257 | #endif | |
258 | #ifdef SIGXCPU | |
259 | { SIGXCPU, "XCPU" }, | |
260 | #endif | |
261 | #ifdef SIGXFSZ | |
262 | { SIGXFSZ, "XFSZ" }, | |
263 | #endif | |
264 | #ifdef SIGVTALRM | |
265 | { SIGVTALRM, "VTALRM" }, | |
266 | #endif | |
267 | #ifdef SIGPROF | |
268 | { SIGPROF, "PROF" }, | |
269 | #endif | |
270 | #ifdef SIGWINCH | |
271 | { SIGWINCH, "WINCH" }, | |
272 | #endif | |
273 | #ifdef SIGIO | |
274 | { SIGIO, "IO" }, | |
275 | #endif | |
276 | #ifdef SIGPOLL | |
277 | { SIGPOLL, "POLL" }, | |
278 | #endif | |
279 | #ifdef SIGINFO | |
280 | { SIGINFO, "INFO" }, | |
281 | #endif | |
282 | #ifdef SIGLOST | |
283 | { SIGLOST, "LOST" }, | |
284 | #endif | |
285 | #ifdef SIGPWR | |
286 | { SIGPWR, "PWR" }, | |
287 | #endif | |
288 | #ifdef SIGUNUSED | |
289 | { SIGUNUSED, "UNUSED" }, | |
290 | #endif | |
291 | #ifdef SIGSYS | |
292 | { SIGSYS, "SYS" }, | |
293 | #endif | |
c2cc9f0a | 294 | }; |
295 | ||
72d0e1cb | 296 | static const size_t config_size = sizeof(config)/sizeof(struct lxc_config_t); |
c2cc9f0a | 297 | |
72d0e1cb | 298 | extern struct lxc_config_t *lxc_getconfig(const char *key) |
c2cc9f0a | 299 | { |
84760c11 | 300 | size_t i; |
c2cc9f0a | 301 | |
302 | for (i = 0; i < config_size; i++) | |
a871ff6b | 303 | if (!strncmp(config[i].name, key, |
c2cc9f0a | 304 | strlen(config[i].name))) |
305 | return &config[i]; | |
306 | return NULL; | |
307 | } | |
308 | ||
72d0e1cb SG |
309 | #define strprint(str, inlen, ...) \ |
310 | do { \ | |
311 | len = snprintf(str, inlen, ##__VA_ARGS__); \ | |
312 | if (len < 0) { SYSERROR("snprintf"); return -1; }; \ | |
313 | fulllen += len; \ | |
314 | if (inlen > 0) { \ | |
315 | if (str) str += len; \ | |
316 | inlen -= len; \ | |
317 | if (inlen < 0) inlen = 0; \ | |
318 | } \ | |
319 | } while (0); | |
320 | ||
321 | int lxc_listconfigs(char *retv, int inlen) | |
322 | { | |
84760c11 | 323 | size_t i; |
324 | int fulllen = 0, len; | |
72d0e1cb SG |
325 | |
326 | if (!retv) | |
327 | inlen = 0; | |
328 | else | |
329 | memset(retv, 0, inlen); | |
330 | for (i = 0; i < config_size; i++) { | |
331 | char *s = config[i].name; | |
332 | if (s[strlen(s)-1] == '.') | |
333 | continue; | |
334 | strprint(retv, inlen, "%s\n", s); | |
335 | } | |
336 | return fulllen; | |
337 | } | |
338 | ||
fe8d7be7 CB |
339 | static inline bool config_value_empty(const char *value) |
340 | { | |
341 | if (value && strlen(value) > 0) | |
342 | return false; | |
343 | ||
344 | return true; | |
345 | } | |
346 | ||
713046e3 | 347 | static int set_config_string_item(char **conf_item, const char *value) |
6d03d92a DE |
348 | { |
349 | char *new_value; | |
350 | ||
e70b9db5 | 351 | if (config_value_empty(value)) { |
f10fad2f | 352 | free(*conf_item); |
d6eca240 | 353 | *conf_item = NULL; |
6d03d92a | 354 | return 0; |
d6eca240 | 355 | } |
6d03d92a DE |
356 | |
357 | new_value = strdup(value); | |
358 | if (!new_value) { | |
e70b9db5 | 359 | SYSERROR("failed to duplicate string \"%s\"", value); |
6d03d92a DE |
360 | return -1; |
361 | } | |
362 | ||
f10fad2f | 363 | free(*conf_item); |
6d03d92a DE |
364 | *conf_item = new_value; |
365 | return 0; | |
366 | } | |
367 | ||
713046e3 | 368 | static int set_config_string_item_max(char **conf_item, const char *value, |
6d03d92a DE |
369 | size_t max) |
370 | { | |
371 | if (strlen(value) >= max) { | |
a5a82508 | 372 | ERROR("%s is too long (>= %lu)", value, (unsigned long)max); |
6d03d92a DE |
373 | return -1; |
374 | } | |
375 | ||
713046e3 | 376 | return set_config_string_item(conf_item, value); |
6d03d92a DE |
377 | } |
378 | ||
713046e3 | 379 | static int set_config_path_item(char **conf_item, const char *value) |
6d03d92a | 380 | { |
713046e3 | 381 | return set_config_string_item_max(conf_item, value, PATH_MAX); |
6d03d92a DE |
382 | } |
383 | ||
72d0e1cb SG |
384 | /* |
385 | * config entry is something like "lxc.network.0.ipv4" | |
386 | * the key 'lxc.network.' was found. So we make sure next | |
387 | * comes an integer, find the right callback (by rewriting | |
388 | * the key), and call it. | |
389 | */ | |
713046e3 | 390 | static int set_config_network_nic(const char *key, const char *value, |
72d0e1cb SG |
391 | struct lxc_conf *lxc_conf) |
392 | { | |
393 | char *copy = strdup(key), *p; | |
394 | int ret = -1; | |
395 | struct lxc_config_t *config; | |
396 | ||
397 | if (!copy) { | |
398 | SYSERROR("failed to allocate memory"); | |
399 | return -1; | |
400 | } | |
401 | /* | |
402 | * ok we know that to get here we've got "lxc.network." | |
403 | * and it isn't any of the other network entries. So | |
404 | * after the second . should come an integer (# of defined | |
405 | * nic) followed by a valid entry. | |
406 | */ | |
407 | if (*(key+12) < '0' || *(key+12) > '9') | |
408 | goto out; | |
46cd2845 | 409 | p = strchr(key+12, '.'); |
72d0e1cb SG |
410 | if (!p) |
411 | goto out; | |
412 | strcpy(copy+12, p+1); | |
413 | config = lxc_getconfig(copy); | |
414 | if (!config) { | |
415 | ERROR("unknown key %s", key); | |
416 | goto out; | |
417 | } | |
d37f7cd7 | 418 | ret = config->set(key, value, lxc_conf); |
72d0e1cb SG |
419 | |
420 | out: | |
421 | free(copy); | |
422 | return ret; | |
423 | } | |
424 | ||
713046e3 | 425 | static int set_config_network(const char *key, const char *value, |
61924323 | 426 | struct lxc_conf *lxc_conf) |
6b0d5538 | 427 | { |
61924323 | 428 | if (!config_value_empty(value)) { |
6b0d5538 SH |
429 | ERROR("lxc.network must not have a value"); |
430 | return -1; | |
431 | } | |
432 | ||
433 | return lxc_clear_config_network(lxc_conf); | |
434 | } | |
435 | ||
261658e8 BP |
436 | static int macvlan_mode(int *valuep, const char *value); |
437 | ||
713046e3 | 438 | static int set_config_network_type(const char *key, const char *value, |
e892973e | 439 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 440 | { |
5f4535a3 | 441 | struct lxc_list *network = &lxc_conf->network; |
c2cc9f0a | 442 | struct lxc_netdev *netdev; |
443 | struct lxc_list *list; | |
c2cc9f0a | 444 | |
d4ba45b3 | 445 | if (config_value_empty(value)) |
7d0eb87e SH |
446 | return lxc_clear_config_network(lxc_conf); |
447 | ||
c2cc9f0a | 448 | netdev = malloc(sizeof(*netdev)); |
449 | if (!netdev) { | |
36eb9bde | 450 | SYSERROR("failed to allocate memory"); |
c2cc9f0a | 451 | return -1; |
452 | } | |
453 | ||
82d5ae15 | 454 | memset(netdev, 0, sizeof(*netdev)); |
c2cc9f0a | 455 | lxc_list_init(&netdev->ipv4); |
456 | lxc_list_init(&netdev->ipv6); | |
c2cc9f0a | 457 | |
458 | list = malloc(sizeof(*list)); | |
459 | if (!list) { | |
36eb9bde | 460 | SYSERROR("failed to allocate memory"); |
022de5f3 | 461 | free(netdev); |
c2cc9f0a | 462 | return -1; |
463 | } | |
464 | ||
465 | lxc_list_init(list); | |
5f4535a3 | 466 | list->elem = netdev; |
c2cc9f0a | 467 | |
bac89583 | 468 | lxc_list_add_tail(network, list); |
a871ff6b | 469 | |
c2cc9f0a | 470 | if (!strcmp(value, "veth")) |
24654103 | 471 | netdev->type = LXC_NET_VETH; |
261658e8 | 472 | else if (!strcmp(value, "macvlan")) { |
24654103 | 473 | netdev->type = LXC_NET_MACVLAN; |
261658e8 BP |
474 | macvlan_mode(&netdev->priv.macvlan_attr.mode, "private"); |
475 | } | |
26c39028 | 476 | else if (!strcmp(value, "vlan")) |
24654103 | 477 | netdev->type = LXC_NET_VLAN; |
c2cc9f0a | 478 | else if (!strcmp(value, "phys")) |
24654103 | 479 | netdev->type = LXC_NET_PHYS; |
5f58350a | 480 | else if (!strcmp(value, "empty")) |
24654103 | 481 | netdev->type = LXC_NET_EMPTY; |
26b797f3 SH |
482 | else if (!strcmp(value, "none")) |
483 | netdev->type = LXC_NET_NONE; | |
c2cc9f0a | 484 | else { |
36eb9bde | 485 | ERROR("invalid network type %s", value); |
c2cc9f0a | 486 | return -1; |
487 | } | |
488 | return 0; | |
489 | } | |
490 | ||
a059591e DL |
491 | static int config_ip_prefix(struct in_addr *addr) |
492 | { | |
493 | if (IN_CLASSA(addr->s_addr)) | |
494 | return 32 - IN_CLASSA_NSHIFT; | |
495 | if (IN_CLASSB(addr->s_addr)) | |
496 | return 32 - IN_CLASSB_NSHIFT; | |
497 | if (IN_CLASSC(addr->s_addr)) | |
498 | return 32 - IN_CLASSC_NSHIFT; | |
499 | ||
500 | return 0; | |
501 | } | |
502 | ||
72d0e1cb SG |
503 | /* |
504 | * if you have p="lxc.network.0.link", pass it p+12 | |
505 | * to get back '0' (the index of the nic) | |
506 | */ | |
507 | static int get_network_netdev_idx(const char *key) | |
508 | { | |
509 | int ret, idx; | |
510 | ||
511 | if (*key < '0' || *key > '9') | |
512 | return -1; | |
513 | ret = sscanf(key, "%d", &idx); | |
514 | if (ret != 1) | |
515 | return -1; | |
516 | return idx; | |
517 | } | |
518 | ||
519 | /* | |
520 | * if you have p="lxc.network.0", pass this p+12 and it will return | |
521 | * the netdev of the first configured nic | |
522 | */ | |
523 | static struct lxc_netdev *get_netdev_from_key(const char *key, | |
524 | struct lxc_list *network) | |
525 | { | |
526 | int i = 0, idx = get_network_netdev_idx(key); | |
527 | struct lxc_netdev *netdev = NULL; | |
528 | struct lxc_list *it; | |
529 | if (idx == -1) | |
530 | return NULL; | |
531 | lxc_list_for_each(it, network) { | |
532 | if (idx == i++) { | |
533 | netdev = it->elem; | |
534 | break; | |
535 | } | |
536 | } | |
537 | return netdev; | |
538 | } | |
539 | ||
12a50cc6 DE |
540 | extern int lxc_list_nicconfigs(struct lxc_conf *c, const char *key, |
541 | char *retv, int inlen) | |
72d0e1cb SG |
542 | { |
543 | struct lxc_netdev *netdev; | |
544 | int fulllen = 0, len; | |
545 | ||
546 | netdev = get_netdev_from_key(key+12, &c->network); | |
547 | if (!netdev) | |
548 | return -1; | |
549 | ||
550 | if (!retv) | |
551 | inlen = 0; | |
552 | else | |
553 | memset(retv, 0, inlen); | |
554 | ||
74836992 | 555 | strprint(retv, inlen, "type\n"); |
72d0e1cb | 556 | strprint(retv, inlen, "script.up\n"); |
8fc8295a | 557 | strprint(retv, inlen, "script.down\n"); |
72d0e1cb SG |
558 | if (netdev->type != LXC_NET_EMPTY) { |
559 | strprint(retv, inlen, "flags\n"); | |
560 | strprint(retv, inlen, "link\n"); | |
561 | strprint(retv, inlen, "name\n"); | |
562 | strprint(retv, inlen, "hwaddr\n"); | |
563 | strprint(retv, inlen, "mtu\n"); | |
564 | strprint(retv, inlen, "ipv6\n"); | |
9eaf8a59 | 565 | strprint(retv, inlen, "ipv6.gateway\n"); |
72d0e1cb | 566 | strprint(retv, inlen, "ipv4\n"); |
9eaf8a59 | 567 | strprint(retv, inlen, "ipv4.gateway\n"); |
72d0e1cb SG |
568 | } |
569 | switch(netdev->type) { | |
570 | case LXC_NET_VETH: | |
571 | strprint(retv, inlen, "veth.pair\n"); | |
572 | break; | |
573 | case LXC_NET_MACVLAN: | |
574 | strprint(retv, inlen, "macvlan.mode\n"); | |
575 | break; | |
576 | case LXC_NET_VLAN: | |
577 | strprint(retv, inlen, "vlan.id\n"); | |
578 | break; | |
579 | case LXC_NET_PHYS: | |
580 | break; | |
581 | } | |
582 | return fulllen; | |
583 | } | |
584 | ||
16950ecb DL |
585 | static struct lxc_netdev *network_netdev(const char *key, const char *value, |
586 | struct lxc_list *network) | |
c2cc9f0a | 587 | { |
72d0e1cb | 588 | struct lxc_netdev *netdev = NULL; |
c2cc9f0a | 589 | |
5f4535a3 | 590 | if (lxc_list_empty(network)) { |
16950ecb DL |
591 | ERROR("network is not created for '%s' = '%s' option", |
592 | key, value); | |
33c945e0 | 593 | return NULL; |
c2cc9f0a | 594 | } |
595 | ||
72d0e1cb SG |
596 | if (get_network_netdev_idx(key+12) == -1) |
597 | netdev = lxc_list_last_elem(network); | |
598 | else | |
599 | netdev = get_netdev_from_key(key+12, network); | |
600 | ||
5f4535a3 | 601 | if (!netdev) { |
16950ecb DL |
602 | ERROR("no network device defined for '%s' = '%s' option", |
603 | key, value); | |
33c945e0 | 604 | return NULL; |
c2cc9f0a | 605 | } |
606 | ||
33c945e0 | 607 | return netdev; |
c2cc9f0a | 608 | } |
609 | ||
12a50cc6 | 610 | static int network_ifname(char **valuep, const char *value) |
c2cc9f0a | 611 | { |
713046e3 | 612 | return set_config_string_item_max(valuep, value, IFNAMSIZ); |
c2cc9f0a | 613 | } |
614 | ||
e892973e DL |
615 | #ifndef MACVLAN_MODE_PRIVATE |
616 | # define MACVLAN_MODE_PRIVATE 1 | |
617 | #endif | |
618 | ||
619 | #ifndef MACVLAN_MODE_VEPA | |
620 | # define MACVLAN_MODE_VEPA 2 | |
621 | #endif | |
622 | ||
623 | #ifndef MACVLAN_MODE_BRIDGE | |
624 | # define MACVLAN_MODE_BRIDGE 4 | |
625 | #endif | |
626 | ||
99854161 EL |
627 | #ifndef MACVLAN_MODE_PASSTHRU |
628 | # define MACVLAN_MODE_PASSTHRU 8 | |
629 | #endif | |
630 | ||
12a50cc6 | 631 | static int macvlan_mode(int *valuep, const char *value) |
e892973e DL |
632 | { |
633 | struct mc_mode { | |
634 | char *name; | |
635 | int mode; | |
636 | } m[] = { | |
637 | { "private", MACVLAN_MODE_PRIVATE }, | |
638 | { "vepa", MACVLAN_MODE_VEPA }, | |
639 | { "bridge", MACVLAN_MODE_BRIDGE }, | |
99854161 | 640 | { "passthru", MACVLAN_MODE_PASSTHRU }, |
e892973e DL |
641 | }; |
642 | ||
84760c11 | 643 | size_t i; |
e892973e DL |
644 | |
645 | for (i = 0; i < sizeof(m)/sizeof(m[0]); i++) { | |
646 | if (strcmp(m[i].name, value)) | |
647 | continue; | |
648 | ||
649 | *valuep = m[i].mode; | |
650 | return 0; | |
651 | } | |
652 | ||
653 | return -1; | |
654 | } | |
655 | ||
508c263e SH |
656 | static int rand_complete_hwaddr(char *hwaddr) |
657 | { | |
658 | const char hex[] = "0123456789abcdef"; | |
659 | char *curs = hwaddr; | |
660 | ||
661 | #ifndef HAVE_RAND_R | |
662 | randseed(true); | |
663 | #else | |
664 | unsigned int seed=randseed(false); | |
665 | #endif | |
e6744e9b | 666 | while (*curs != '\0' && *curs != '\n') |
508c263e SH |
667 | { |
668 | if ( *curs == 'x' || *curs == 'X' ) { | |
669 | if (curs - hwaddr == 1) { | |
670 | //ensure address is unicast | |
671 | #ifdef HAVE_RAND_R | |
672 | *curs = hex[rand_r(&seed) & 0x0E]; | |
673 | } else { | |
674 | *curs = hex[rand_r(&seed) & 0x0F]; | |
675 | #else | |
676 | *curs = hex[rand() & 0x0E]; | |
677 | } else { | |
678 | *curs = hex[rand() & 0x0F]; | |
679 | #endif | |
680 | } | |
681 | } | |
682 | curs++; | |
683 | } | |
684 | return 0; | |
685 | } | |
686 | ||
713046e3 | 687 | static int set_config_network_flags(const char *key, const char *value, |
33c945e0 | 688 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 689 | { |
c2cc9f0a | 690 | struct lxc_netdev *netdev; |
691 | ||
16950ecb | 692 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 693 | if (!netdev) |
c2cc9f0a | 694 | return -1; |
c2cc9f0a | 695 | |
33c945e0 | 696 | netdev->flags |= IFF_UP; |
c2cc9f0a | 697 | |
33c945e0 MT |
698 | return 0; |
699 | } | |
700 | ||
576400e5 | 701 | static int set_network_link(const char *key, const char *value, struct lxc_conf *lxc_conf) |
702 | { | |
703 | struct lxc_netdev *netdev; | |
704 | ||
705 | netdev = network_netdev(key, value, &lxc_conf->network); | |
706 | if (!netdev) | |
707 | return -1; | |
708 | ||
709 | return network_ifname(&netdev->link, value); | |
710 | } | |
711 | ||
712 | static int create_matched_ifnames(const char *value, struct lxc_conf *lxc_conf) | |
713 | { | |
714 | struct ifaddrs *ifaddr, *ifa; | |
715 | const char *type_key = "lxc.network.type"; | |
716 | const char *link_key = "lxc.network.link"; | |
717 | const char *tmpvalue = "phys"; | |
718 | int n, ret = 0; | |
719 | ||
720 | if (getifaddrs(&ifaddr) == -1) { | |
721 | SYSERROR("Get network interfaces failed"); | |
722 | return -1; | |
723 | } | |
724 | ||
725 | for (ifa = ifaddr, n = 0; ifa != NULL; ifa = ifa->ifa_next, n++) { | |
726 | if (!ifa->ifa_addr) | |
727 | continue; | |
728 | if (ifa->ifa_addr->sa_family != AF_PACKET) | |
729 | continue; | |
730 | ||
731 | if (!strncmp(value, ifa->ifa_name, strlen(value)-1)) { | |
713046e3 | 732 | ret = set_config_network_type(type_key, tmpvalue, lxc_conf); |
576400e5 | 733 | if (!ret) { |
734 | ret = set_network_link(link_key, ifa->ifa_name, lxc_conf); | |
735 | if (ret) { | |
736 | ERROR("failed to create matched ifnames"); | |
737 | break; | |
738 | } | |
739 | } else { | |
740 | ERROR("failed to create matched ifnames"); | |
741 | break; | |
742 | } | |
743 | } | |
744 | } | |
745 | ||
746 | freeifaddrs(ifaddr); /* free the dynamic memory */ | |
747 | ifaddr = NULL; /* prevent use after free */ | |
091045f8 | 748 | |
576400e5 | 749 | return ret; |
750 | } | |
751 | ||
713046e3 | 752 | static int set_config_network_link(const char *key, const char *value, |
33c945e0 MT |
753 | struct lxc_conf *lxc_conf) |
754 | { | |
755 | struct lxc_netdev *netdev; | |
576400e5 | 756 | struct lxc_list * it; |
757 | int ret = 0; | |
33c945e0 | 758 | |
16950ecb | 759 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 760 | if (!netdev) |
c2cc9f0a | 761 | return -1; |
c2cc9f0a | 762 | |
576400e5 | 763 | if (value[strlen(value) - 1] == '+' && netdev->type == LXC_NET_PHYS) { |
764 | //get the last network list and remove it. | |
765 | it = lxc_conf->network.prev; | |
766 | if (((struct lxc_netdev *)(it->elem))->type != LXC_NET_PHYS) { | |
767 | ERROR("lxc config cannot support string pattern matching for this link type"); | |
768 | return -1; | |
769 | } | |
770 | ||
771 | lxc_list_del(it); | |
772 | free(it); | |
773 | ret = create_matched_ifnames(value, lxc_conf); | |
774 | ||
775 | } else { | |
776 | ret = network_ifname(&netdev->link, value); | |
777 | } | |
778 | ||
779 | return ret; | |
c2cc9f0a | 780 | } |
781 | ||
713046e3 | 782 | static int set_config_network_name(const char *key, const char *value, |
33c945e0 | 783 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 784 | { |
c2cc9f0a | 785 | struct lxc_netdev *netdev; |
786 | ||
16950ecb | 787 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 788 | if (!netdev) |
c2cc9f0a | 789 | return -1; |
c2cc9f0a | 790 | |
33c945e0 MT |
791 | return network_ifname(&netdev->name, value); |
792 | } | |
793 | ||
713046e3 | 794 | static int set_config_network_veth_pair(const char *key, const char *value, |
e892973e DL |
795 | struct lxc_conf *lxc_conf) |
796 | { | |
797 | struct lxc_netdev *netdev; | |
798 | ||
799 | netdev = network_netdev(key, value, &lxc_conf->network); | |
800 | if (!netdev) | |
801 | return -1; | |
802 | ||
c5316d60 | 803 | if (netdev->type != LXC_NET_VETH) { |
128d327a | 804 | ERROR("Invalid veth pair for a non-veth netdev"); |
c5316d60 SH |
805 | return -1; |
806 | } | |
e892973e DL |
807 | return network_ifname(&netdev->priv.veth_attr.pair, value); |
808 | } | |
809 | ||
713046e3 | 810 | static int set_config_network_macvlan_mode(const char *key, const char *value, |
e892973e | 811 | struct lxc_conf *lxc_conf) |
8634bc19 MT |
812 | { |
813 | struct lxc_netdev *netdev; | |
814 | ||
815 | netdev = network_netdev(key, value, &lxc_conf->network); | |
816 | if (!netdev) | |
817 | return -1; | |
818 | ||
c5316d60 | 819 | if (netdev->type != LXC_NET_MACVLAN) { |
128d327a | 820 | ERROR("Invalid macvlan.mode for a non-macvlan netdev"); |
c5316d60 SH |
821 | return -1; |
822 | } | |
e892973e | 823 | return macvlan_mode(&netdev->priv.macvlan_attr.mode, value); |
8634bc19 MT |
824 | } |
825 | ||
713046e3 | 826 | static int set_config_network_hwaddr(const char *key, const char *value, |
33c945e0 MT |
827 | struct lxc_conf *lxc_conf) |
828 | { | |
829 | struct lxc_netdev *netdev; | |
830 | ||
508c263e SH |
831 | char *new_value = strdup(value); |
832 | if (!new_value) { | |
833 | SYSERROR("failed to strdup '%s': %m", value); | |
c2cc9f0a | 834 | return -1; |
508c263e SH |
835 | } |
836 | rand_complete_hwaddr(new_value); | |
c2cc9f0a | 837 | |
508c263e SH |
838 | netdev = network_netdev(key, new_value, &lxc_conf->network); |
839 | if (!netdev) { | |
840 | free(new_value); | |
841 | return -1; | |
842 | }; | |
843 | ||
9763f881 | 844 | if (config_value_empty(new_value)) { |
508c263e SH |
845 | free(new_value); |
846 | netdev->hwaddr = NULL; | |
847 | return 0; | |
848 | } | |
849 | ||
850 | netdev->hwaddr = new_value; | |
851 | return 0; | |
c2cc9f0a | 852 | } |
853 | ||
713046e3 | 854 | static int set_config_network_vlan_id(const char *key, const char *value, |
26c39028 JHS |
855 | struct lxc_conf *lxc_conf) |
856 | { | |
857 | struct lxc_netdev *netdev; | |
858 | ||
859 | netdev = network_netdev(key, value, &lxc_conf->network); | |
860 | if (!netdev) | |
861 | return -1; | |
862 | ||
c5316d60 | 863 | if (netdev->type != LXC_NET_VLAN) { |
128d327a | 864 | ERROR("Invalid vlan.id for a non-macvlan netdev"); |
c5316d60 SH |
865 | return -1; |
866 | } | |
f6cc1de1 | 867 | if (get_u16(&netdev->priv.vlan_attr.vid, value, 0)) |
26c39028 JHS |
868 | return -1; |
869 | ||
870 | return 0; | |
871 | } | |
872 | ||
713046e3 | 873 | static int set_config_network_mtu(const char *key, const char *value, |
33c945e0 | 874 | struct lxc_conf *lxc_conf) |
442cbbe6 | 875 | { |
442cbbe6 TR |
876 | struct lxc_netdev *netdev; |
877 | ||
16950ecb | 878 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 879 | if (!netdev) |
442cbbe6 | 880 | return -1; |
442cbbe6 | 881 | |
713046e3 | 882 | return set_config_string_item(&netdev->mtu, value); |
442cbbe6 TR |
883 | } |
884 | ||
713046e3 | 885 | static int set_config_network_ipv4(const char *key, const char *value, |
33c945e0 | 886 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 887 | { |
c2cc9f0a | 888 | struct lxc_netdev *netdev; |
33c945e0 | 889 | struct lxc_inetdev *inetdev; |
c2cc9f0a | 890 | struct lxc_list *list; |
891 | char *cursor, *slash, *addr = NULL, *bcast = NULL, *prefix = NULL; | |
892 | ||
c3760156 | 893 | if (config_value_empty(value)) |
0797e123 CB |
894 | return lxc_clear_config_item(lxc_conf, key); |
895 | ||
16950ecb | 896 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 897 | if (!netdev) |
c2cc9f0a | 898 | return -1; |
c2cc9f0a | 899 | |
900 | inetdev = malloc(sizeof(*inetdev)); | |
901 | if (!inetdev) { | |
36eb9bde | 902 | SYSERROR("failed to allocate ipv4 address"); |
c2cc9f0a | 903 | return -1; |
904 | } | |
905 | memset(inetdev, 0, sizeof(*inetdev)); | |
906 | ||
907 | list = malloc(sizeof(*list)); | |
908 | if (!list) { | |
36eb9bde | 909 | SYSERROR("failed to allocate memory"); |
53719062 | 910 | free(inetdev); |
c2cc9f0a | 911 | return -1; |
912 | } | |
913 | ||
914 | lxc_list_init(list); | |
915 | list->elem = inetdev; | |
916 | ||
956edc54 SG |
917 | addr = strdup(value); |
918 | if (!addr) { | |
919 | ERROR("no address specified"); | |
53719062 SH |
920 | free(inetdev); |
921 | free(list); | |
956edc54 SG |
922 | return -1; |
923 | } | |
c2cc9f0a | 924 | |
925 | cursor = strstr(addr, " "); | |
926 | if (cursor) { | |
927 | *cursor = '\0'; | |
928 | bcast = cursor + 1; | |
929 | } | |
930 | ||
931 | slash = strstr(addr, "/"); | |
932 | if (slash) { | |
933 | *slash = '\0'; | |
934 | prefix = slash + 1; | |
935 | } | |
936 | ||
c2cc9f0a | 937 | if (!inet_pton(AF_INET, addr, &inetdev->addr)) { |
36eb9bde | 938 | SYSERROR("invalid ipv4 address: %s", value); |
53719062 | 939 | free(inetdev); |
956edc54 | 940 | free(addr); |
53719062 | 941 | free(list); |
c2cc9f0a | 942 | return -1; |
943 | } | |
944 | ||
0093bb8c DL |
945 | if (bcast && !inet_pton(AF_INET, bcast, &inetdev->bcast)) { |
946 | SYSERROR("invalid ipv4 broadcast address: %s", value); | |
53719062 SH |
947 | free(inetdev); |
948 | free(list); | |
956edc54 | 949 | free(addr); |
0093bb8c DL |
950 | return -1; |
951 | } | |
c2cc9f0a | 952 | |
a059591e | 953 | /* no prefix specified, determine it from the network class */ |
1c633398 CB |
954 | if (prefix) { |
955 | if (lxc_safe_uint(prefix, &inetdev->prefix) < 0) | |
956 | return -1; | |
957 | } else { | |
958 | inetdev->prefix = config_ip_prefix(&inetdev->addr); | |
959 | } | |
a059591e | 960 | |
b3df193c | 961 | /* if no broadcast address, let compute one from the |
0093bb8c DL |
962 | * prefix and address |
963 | */ | |
964 | if (!bcast) { | |
1b7d4743 DL |
965 | inetdev->bcast.s_addr = inetdev->addr.s_addr; |
966 | inetdev->bcast.s_addr |= | |
967 | htonl(INADDR_BROADCAST >> inetdev->prefix); | |
0093bb8c | 968 | } |
c2cc9f0a | 969 | |
8538f388 | 970 | lxc_list_add_tail(&netdev->ipv4, list); |
c2cc9f0a | 971 | |
956edc54 | 972 | free(addr); |
c2cc9f0a | 973 | return 0; |
974 | } | |
975 | ||
713046e3 | 976 | static int set_config_network_ipv4_gateway(const char *key, const char *value, |
f8fee0e2 MK |
977 | struct lxc_conf *lxc_conf) |
978 | { | |
979 | struct lxc_netdev *netdev; | |
f8fee0e2 MK |
980 | |
981 | netdev = network_netdev(key, value, &lxc_conf->network); | |
982 | if (!netdev) | |
983 | return -1; | |
984 | ||
e088e926 | 985 | free(netdev->ipv4_gateway); |
f8fee0e2 | 986 | |
10da55e4 | 987 | if (config_value_empty(value)) { |
e088e926 SG |
988 | netdev->ipv4_gateway = NULL; |
989 | } else if (!strcmp(value, "auto")) { | |
19a26f82 MK |
990 | netdev->ipv4_gateway = NULL; |
991 | netdev->ipv4_gateway_auto = true; | |
992 | } else { | |
e088e926 SG |
993 | struct in_addr *gw; |
994 | ||
995 | gw = malloc(sizeof(*gw)); | |
996 | if (!gw) { | |
997 | SYSERROR("failed to allocate ipv4 gateway address"); | |
998 | return -1; | |
999 | } | |
1000 | ||
19a26f82 MK |
1001 | if (!inet_pton(AF_INET, value, gw)) { |
1002 | SYSERROR("invalid ipv4 gateway address: %s", value); | |
53719062 | 1003 | free(gw); |
19a26f82 MK |
1004 | return -1; |
1005 | } | |
1006 | ||
1007 | netdev->ipv4_gateway = gw; | |
1008 | netdev->ipv4_gateway_auto = false; | |
f8fee0e2 MK |
1009 | } |
1010 | ||
f8fee0e2 MK |
1011 | return 0; |
1012 | } | |
1013 | ||
713046e3 | 1014 | static int set_config_network_ipv6(const char *key, const char *value, |
e892973e | 1015 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 1016 | { |
c2cc9f0a | 1017 | struct lxc_netdev *netdev; |
1018 | struct lxc_inet6dev *inet6dev; | |
1019 | struct lxc_list *list; | |
12a50cc6 | 1020 | char *slash,*valdup; |
c2cc9f0a | 1021 | char *netmask; |
1022 | ||
cae8a0f0 | 1023 | if (config_value_empty(value)) |
0797e123 CB |
1024 | return lxc_clear_config_item(lxc_conf, key); |
1025 | ||
16950ecb | 1026 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 1027 | if (!netdev) |
c2cc9f0a | 1028 | return -1; |
c2cc9f0a | 1029 | |
1030 | inet6dev = malloc(sizeof(*inet6dev)); | |
1031 | if (!inet6dev) { | |
36eb9bde | 1032 | SYSERROR("failed to allocate ipv6 address"); |
c2cc9f0a | 1033 | return -1; |
1034 | } | |
1035 | memset(inet6dev, 0, sizeof(*inet6dev)); | |
1036 | ||
1037 | list = malloc(sizeof(*list)); | |
1038 | if (!list) { | |
36eb9bde | 1039 | SYSERROR("failed to allocate memory"); |
28027320 | 1040 | free(inet6dev); |
c2cc9f0a | 1041 | return -1; |
1042 | } | |
1043 | ||
1044 | lxc_list_init(list); | |
1045 | list->elem = inet6dev; | |
1046 | ||
956edc54 SG |
1047 | valdup = strdup(value); |
1048 | if (!valdup) { | |
1049 | ERROR("no address specified"); | |
28027320 SH |
1050 | free(list); |
1051 | free(inet6dev); | |
956edc54 SG |
1052 | return -1; |
1053 | } | |
1054 | ||
a059591e | 1055 | inet6dev->prefix = 64; |
12a50cc6 | 1056 | slash = strstr(valdup, "/"); |
c2cc9f0a | 1057 | if (slash) { |
1058 | *slash = '\0'; | |
1059 | netmask = slash + 1; | |
1c633398 CB |
1060 | if (lxc_safe_uint(netmask, &inet6dev->prefix) < 0) |
1061 | return -1; | |
c2cc9f0a | 1062 | } |
1063 | ||
5acccf95 SH |
1064 | if (!inet_pton(AF_INET6, valdup, &inet6dev->addr)) { |
1065 | SYSERROR("invalid ipv6 address: %s", valdup); | |
28027320 SH |
1066 | free(list); |
1067 | free(inet6dev); | |
956edc54 | 1068 | free(valdup); |
c2cc9f0a | 1069 | return -1; |
1070 | } | |
1071 | ||
8538f388 | 1072 | lxc_list_add_tail(&netdev->ipv6, list); |
c2cc9f0a | 1073 | |
956edc54 | 1074 | free(valdup); |
c2cc9f0a | 1075 | return 0; |
1076 | } | |
1077 | ||
713046e3 | 1078 | static int set_config_network_ipv6_gateway(const char *key, const char *value, |
f8fee0e2 MK |
1079 | struct lxc_conf *lxc_conf) |
1080 | { | |
1081 | struct lxc_netdev *netdev; | |
f8fee0e2 MK |
1082 | |
1083 | netdev = network_netdev(key, value, &lxc_conf->network); | |
1084 | if (!netdev) | |
1085 | return -1; | |
1086 | ||
e088e926 | 1087 | free(netdev->ipv6_gateway); |
f8fee0e2 | 1088 | |
114df19e | 1089 | if (config_value_empty(value)) { |
ffe34437 | 1090 | netdev->ipv6_gateway = NULL; |
e088e926 | 1091 | } else if (!strcmp(value, "auto")) { |
19a26f82 MK |
1092 | netdev->ipv6_gateway = NULL; |
1093 | netdev->ipv6_gateway_auto = true; | |
1094 | } else { | |
8fb86a37 SH |
1095 | struct in6_addr *gw; |
1096 | ||
bec695f3 DE |
1097 | gw = malloc(sizeof(*gw)); |
1098 | if (!gw) { | |
1099 | SYSERROR("failed to allocate ipv6 gateway address"); | |
1100 | return -1; | |
1101 | } | |
1102 | ||
19a26f82 MK |
1103 | if (!inet_pton(AF_INET6, value, gw)) { |
1104 | SYSERROR("invalid ipv6 gateway address: %s", value); | |
28027320 | 1105 | free(gw); |
19a26f82 MK |
1106 | return -1; |
1107 | } | |
1108 | ||
1109 | netdev->ipv6_gateway = gw; | |
1110 | netdev->ipv6_gateway_auto = false; | |
f8fee0e2 MK |
1111 | } |
1112 | ||
f8fee0e2 MK |
1113 | return 0; |
1114 | } | |
1115 | ||
713046e3 | 1116 | static int set_config_network_script_up(const char *key, const char *value, |
8fc8295a | 1117 | struct lxc_conf *lxc_conf) |
e3b4c4c4 ST |
1118 | { |
1119 | struct lxc_netdev *netdev; | |
1120 | ||
1121 | netdev = network_netdev(key, value, &lxc_conf->network); | |
1122 | if (!netdev) | |
8fc8295a | 1123 | return -1; |
e3b4c4c4 | 1124 | |
713046e3 | 1125 | return set_config_string_item(&netdev->upscript, value); |
8fc8295a DE |
1126 | } |
1127 | ||
713046e3 | 1128 | static int set_config_network_script_down(const char *key, const char *value, |
8fc8295a DE |
1129 | struct lxc_conf *lxc_conf) |
1130 | { | |
1131 | struct lxc_netdev *netdev; | |
1132 | ||
1133 | netdev = network_netdev(key, value, &lxc_conf->network); | |
1134 | if (!netdev) | |
1135 | return -1; | |
1136 | ||
713046e3 | 1137 | return set_config_string_item(&netdev->downscript, value); |
e3b4c4c4 ST |
1138 | } |
1139 | ||
26ddeedd SH |
1140 | static int add_hook(struct lxc_conf *lxc_conf, int which, char *hook) |
1141 | { | |
1142 | struct lxc_list *hooklist; | |
1143 | ||
1144 | hooklist = malloc(sizeof(*hooklist)); | |
1145 | if (!hooklist) { | |
1146 | free(hook); | |
1147 | return -1; | |
1148 | } | |
1149 | hooklist->elem = hook; | |
1150 | lxc_list_add_tail(&lxc_conf->hooks[which], hooklist); | |
1151 | return 0; | |
1152 | } | |
1153 | ||
713046e3 | 1154 | static int set_config_seccomp(const char *key, const char *value, |
8f2c3a70 SH |
1155 | struct lxc_conf *lxc_conf) |
1156 | { | |
713046e3 | 1157 | return set_config_path_item(&lxc_conf->seccomp, value); |
8f2c3a70 SH |
1158 | } |
1159 | ||
713046e3 | 1160 | static int set_config_init_cmd(const char *key, const char *value, |
67c660d0 SG |
1161 | struct lxc_conf *lxc_conf) |
1162 | { | |
713046e3 | 1163 | return set_config_path_item(&lxc_conf->init_cmd, value); |
67c660d0 SG |
1164 | } |
1165 | ||
713046e3 | 1166 | static int set_config_init_uid(const char *key, const char *value, |
2e7cde40 | 1167 | struct lxc_conf *lxc_conf) |
72bb04e4 | 1168 | { |
d1e5d636 CB |
1169 | unsigned int init_uid; |
1170 | ||
2e7cde40 CB |
1171 | /* Set config value to default. */ |
1172 | if (config_value_empty(value)) { | |
1173 | lxc_conf->init_uid = 0; | |
fee80911 | 1174 | return 0; |
2e7cde40 | 1175 | } |
fee80911 | 1176 | |
2e7cde40 | 1177 | /* Parse new config value. */ |
d1e5d636 CB |
1178 | if (lxc_safe_uint(value, &init_uid) < 0) |
1179 | return -1; | |
1180 | lxc_conf->init_uid = init_uid; | |
1181 | ||
72bb04e4 PT |
1182 | return 0; |
1183 | } | |
1184 | ||
713046e3 | 1185 | static int set_config_init_gid(const char *key, const char *value, |
2debb6e6 | 1186 | struct lxc_conf *lxc_conf) |
72bb04e4 | 1187 | { |
d1e5d636 CB |
1188 | unsigned int init_gid; |
1189 | ||
2debb6e6 CB |
1190 | /* Set config value to default. */ |
1191 | if (config_value_empty(value)) { | |
1192 | lxc_conf->init_gid = 0; | |
a757cc7d | 1193 | return 0; |
2debb6e6 | 1194 | } |
a757cc7d | 1195 | |
2debb6e6 | 1196 | /* Parse new config value. */ |
d1e5d636 CB |
1197 | if (lxc_safe_uint(value, &init_gid) < 0) |
1198 | return -1; | |
1199 | lxc_conf->init_gid = init_gid; | |
1200 | ||
72bb04e4 PT |
1201 | return 0; |
1202 | } | |
1203 | ||
713046e3 | 1204 | static int set_config_hook(const char *key, const char *value, |
26ddeedd SH |
1205 | struct lxc_conf *lxc_conf) |
1206 | { | |
7d0eb87e | 1207 | char *copy; |
72bb04e4 | 1208 | |
03d88f7c | 1209 | if (config_value_empty(value)) |
7d0eb87e SH |
1210 | return lxc_clear_hooks(lxc_conf, key); |
1211 | ||
6b0d5538 SH |
1212 | if (strcmp(key, "lxc.hook") == 0) { |
1213 | ERROR("lxc.hook cannot take a value"); | |
1214 | return -1; | |
1215 | } | |
7d0eb87e | 1216 | copy = strdup(value); |
26ddeedd SH |
1217 | if (!copy) { |
1218 | SYSERROR("failed to dup string '%s'", value); | |
1219 | return -1; | |
1220 | } | |
1221 | if (strcmp(key, "lxc.hook.pre-start") == 0) | |
1222 | return add_hook(lxc_conf, LXCHOOK_PRESTART, copy); | |
5ea6163a SH |
1223 | else if (strcmp(key, "lxc.hook.pre-mount") == 0) |
1224 | return add_hook(lxc_conf, LXCHOOK_PREMOUNT, copy); | |
f7bee6c6 MW |
1225 | else if (strcmp(key, "lxc.hook.autodev") == 0) |
1226 | return add_hook(lxc_conf, LXCHOOK_AUTODEV, copy); | |
26ddeedd SH |
1227 | else if (strcmp(key, "lxc.hook.mount") == 0) |
1228 | return add_hook(lxc_conf, LXCHOOK_MOUNT, copy); | |
1229 | else if (strcmp(key, "lxc.hook.start") == 0) | |
1230 | return add_hook(lxc_conf, LXCHOOK_START, copy); | |
52492063 WB |
1231 | else if (strcmp(key, "lxc.hook.stop") == 0) |
1232 | return add_hook(lxc_conf, LXCHOOK_STOP, copy); | |
26ddeedd SH |
1233 | else if (strcmp(key, "lxc.hook.post-stop") == 0) |
1234 | return add_hook(lxc_conf, LXCHOOK_POSTSTOP, copy); | |
148e91f5 SH |
1235 | else if (strcmp(key, "lxc.hook.clone") == 0) |
1236 | return add_hook(lxc_conf, LXCHOOK_CLONE, copy); | |
37cf711b SY |
1237 | else if (strcmp(key, "lxc.hook.destroy") == 0) |
1238 | return add_hook(lxc_conf, LXCHOOK_DESTROY, copy); | |
26ddeedd SH |
1239 | SYSERROR("Unknown key: %s", key); |
1240 | free(copy); | |
1241 | return -1; | |
1242 | } | |
1243 | ||
713046e3 | 1244 | static int set_config_personality(const char *key, const char *value, |
cccc74b5 DL |
1245 | struct lxc_conf *lxc_conf) |
1246 | { | |
525f0002 | 1247 | signed long personality = lxc_config_parse_arch(value); |
cccc74b5 | 1248 | |
525f0002 CS |
1249 | if (personality >= 0) |
1250 | lxc_conf->personality = personality; | |
1251 | else | |
1252 | WARN("unsupported personality '%s'", value); | |
970ab589 DL |
1253 | |
1254 | return 0; | |
cccc74b5 DL |
1255 | } |
1256 | ||
713046e3 | 1257 | static int set_config_pts(const char *key, const char *value, |
12a50cc6 | 1258 | struct lxc_conf *lxc_conf) |
10db618d | 1259 | { |
ec200ce9 CB |
1260 | /* Set config value to default. */ |
1261 | if (config_value_empty(value)) { | |
1262 | lxc_conf->pts = 0; | |
884a4580 | 1263 | return 0; |
ec200ce9 | 1264 | } |
884a4580 | 1265 | |
ec200ce9 | 1266 | /* Parse new config value. */ |
17919969 CB |
1267 | if (lxc_safe_uint(value, &lxc_conf->pts) < 0) |
1268 | return -1; | |
10db618d | 1269 | |
1270 | return 0; | |
1271 | } | |
1272 | ||
713046e3 | 1273 | static int set_config_start(const char *key, const char *value, |
ebb80f95 | 1274 | struct lxc_conf *lxc_conf) |
ee1e7aa0 | 1275 | { |
ebb80f95 CB |
1276 | bool is_empty; |
1277 | ||
1278 | is_empty = config_value_empty(value); | |
1279 | ||
1280 | if (strcmp(key, "lxc.start.auto") == 0) { | |
1281 | /* Set config value to default. */ | |
1282 | if (is_empty) { | |
1283 | lxc_conf->start_auto = 0; | |
1284 | return 0; | |
1285 | } | |
61ff8fc8 | 1286 | |
ebb80f95 | 1287 | /* Parse new config value. */ |
3590152f CB |
1288 | if (lxc_safe_uint(value, &lxc_conf->start_auto) < 0) |
1289 | return -1; | |
ebb80f95 | 1290 | |
3590152f CB |
1291 | if (lxc_conf->start_auto > 1) |
1292 | return -1; | |
ebb80f95 | 1293 | |
ee1e7aa0 | 1294 | return 0; |
ebb80f95 CB |
1295 | } else if (strcmp(key, "lxc.start.delay") == 0) { |
1296 | /* Set config value to default. */ | |
1297 | if (is_empty) { | |
1298 | lxc_conf->start_delay = 0; | |
1299 | return 0; | |
1300 | } | |
1301 | ||
1302 | /* Parse new config value. */ | |
1303 | return lxc_safe_uint(value, &lxc_conf->start_delay); | |
1304 | } else if (strcmp(key, "lxc.start.order") == 0) { | |
1305 | /* Set config value to default. */ | |
1306 | if (is_empty) { | |
1307 | lxc_conf->start_order = 0; | |
1308 | return 0; | |
1309 | } | |
1310 | ||
1311 | /* Parse new config value. */ | |
1312 | return lxc_safe_int(value, &lxc_conf->start_order); | |
ee1e7aa0 | 1313 | } |
ebb80f95 | 1314 | |
ee1e7aa0 SG |
1315 | SYSERROR("Unknown key: %s", key); |
1316 | return -1; | |
1317 | } | |
1318 | ||
713046e3 | 1319 | static int set_config_monitor(const char *key, const char *value, |
a8dfe4e0 WB |
1320 | struct lxc_conf *lxc_conf) |
1321 | { | |
4ad9cd26 CB |
1322 | /* Set config value to default. */ |
1323 | if (config_value_empty(value)) { | |
1324 | lxc_conf->monitor_unshare = 0; | |
a8dfe4e0 WB |
1325 | return 0; |
1326 | } | |
4ad9cd26 CB |
1327 | |
1328 | /* Parse new config value. */ | |
1329 | if (strcmp(key, "lxc.monitor.unshare") == 0) | |
1330 | return lxc_safe_uint(value, &lxc_conf->monitor_unshare); | |
1331 | ||
a8dfe4e0 WB |
1332 | SYSERROR("Unknown key: %s", key); |
1333 | return -1; | |
1334 | } | |
1335 | ||
713046e3 | 1336 | static int set_config_group(const char *key, const char *value, |
ee1e7aa0 SG |
1337 | struct lxc_conf *lxc_conf) |
1338 | { | |
1339 | char *groups, *groupptr, *sptr, *token; | |
1340 | struct lxc_list *grouplist; | |
1341 | int ret = -1; | |
1342 | ||
7584546d | 1343 | if (config_value_empty(value)) |
ee1e7aa0 SG |
1344 | return lxc_clear_groups(lxc_conf); |
1345 | ||
1346 | groups = strdup(value); | |
1347 | if (!groups) { | |
1348 | SYSERROR("failed to dup '%s'", value); | |
1349 | return -1; | |
1350 | } | |
1351 | ||
1352 | /* in case several groups are specified in a single line | |
1353 | * split these groups in a single element for the list */ | |
1354 | for (groupptr = groups;;groupptr = NULL) { | |
d028235d SG |
1355 | token = strtok_r(groupptr, " \t", &sptr); |
1356 | if (!token) { | |
ee1e7aa0 | 1357 | ret = 0; |
d028235d | 1358 | break; |
ee1e7aa0 SG |
1359 | } |
1360 | ||
1361 | grouplist = malloc(sizeof(*grouplist)); | |
1362 | if (!grouplist) { | |
1363 | SYSERROR("failed to allocate groups list"); | |
1364 | break; | |
1365 | } | |
1366 | ||
1367 | grouplist->elem = strdup(token); | |
1368 | if (!grouplist->elem) { | |
1369 | SYSERROR("failed to dup '%s'", token); | |
1370 | free(grouplist); | |
1371 | break; | |
1372 | } | |
1373 | ||
1374 | lxc_list_add_tail(&lxc_conf->groups, grouplist); | |
d028235d | 1375 | } |
ee1e7aa0 SG |
1376 | |
1377 | free(groups); | |
1378 | ||
1379 | return ret; | |
1380 | } | |
1381 | ||
713046e3 | 1382 | static int set_config_environment(const char *key, const char *value, |
7c661726 MP |
1383 | struct lxc_conf *lxc_conf) |
1384 | { | |
1385 | struct lxc_list *list_item = NULL; | |
1386 | ||
cdee76e3 | 1387 | if (config_value_empty(value)) |
ab799c0b SG |
1388 | return lxc_clear_environment(lxc_conf); |
1389 | ||
7c661726 MP |
1390 | list_item = malloc(sizeof(*list_item)); |
1391 | if (!list_item) | |
1392 | goto freak_out; | |
1393 | ||
1394 | list_item->elem = strdup(value); | |
1395 | ||
1396 | if (!list_item->elem) | |
1397 | goto freak_out; | |
1398 | ||
1399 | lxc_list_add_tail(&lxc_conf->environment, list_item); | |
1400 | ||
1401 | return 0; | |
1402 | ||
1403 | freak_out: | |
f10fad2f | 1404 | free(list_item); |
7c661726 MP |
1405 | |
1406 | return -1; | |
1407 | } | |
1408 | ||
713046e3 | 1409 | static int set_config_tty(const char *key, const char *value, |
12a50cc6 | 1410 | struct lxc_conf *lxc_conf) |
b0a33c1e | 1411 | { |
cb508ee8 CB |
1412 | /* Set config value to default. */ |
1413 | if (config_value_empty(value)) { | |
1414 | lxc_conf->tty = 0; | |
fb12b12a | 1415 | return 0; |
cb508ee8 | 1416 | } |
fb12b12a | 1417 | |
cb508ee8 CB |
1418 | /* Parse new config value. */ |
1419 | return lxc_safe_uint(value, &lxc_conf->tty); | |
b0a33c1e | 1420 | } |
1421 | ||
713046e3 | 1422 | static int set_config_ttydir(const char *key, const char *value, |
7c6ef2a2 SH |
1423 | struct lxc_conf *lxc_conf) |
1424 | { | |
713046e3 | 1425 | return set_config_string_item_max(&lxc_conf->ttydir, value, NAME_MAX+1); |
7c6ef2a2 SH |
1426 | } |
1427 | ||
713046e3 | 1428 | static int set_config_kmsg(const char *key, const char *value, |
3d6b7fdc | 1429 | struct lxc_conf *lxc_conf) |
7e0e1d94 | 1430 | { |
3d6b7fdc CB |
1431 | /* Set config value to default. */ |
1432 | if (config_value_empty(value)) { | |
1433 | lxc_conf->kmsg = 0; | |
5767e9ba | 1434 | return 0; |
3d6b7fdc | 1435 | } |
5767e9ba | 1436 | |
3d6b7fdc | 1437 | /* Parse new config value. */ |
91863d36 CB |
1438 | if (lxc_safe_uint(value, &lxc_conf->kmsg) < 0) |
1439 | return -1; | |
7e0e1d94 | 1440 | |
91863d36 CB |
1441 | if (lxc_conf->kmsg > 1) |
1442 | return -1; | |
7e0e1d94 AV |
1443 | |
1444 | return 0; | |
1445 | } | |
1446 | ||
713046e3 | 1447 | static int set_config_lsm_aa_profile(const char *key, const char *value, |
fe4de9a6 | 1448 | struct lxc_conf *lxc_conf) |
e075f5d9 | 1449 | { |
713046e3 | 1450 | return set_config_string_item(&lxc_conf->lsm_aa_profile, value); |
fe4de9a6 DE |
1451 | } |
1452 | ||
713046e3 | 1453 | static int set_config_lsm_aa_incomplete(const char *key, const char *value, |
cccfa758 | 1454 | struct lxc_conf *lxc_conf) |
7aff4f43 | 1455 | { |
cccfa758 CB |
1456 | /* Set config value to default. */ |
1457 | if (config_value_empty(value)) { | |
1458 | lxc_conf->lsm_aa_allow_incomplete = 0; | |
a678e9fa | 1459 | return 0; |
cccfa758 | 1460 | } |
a678e9fa | 1461 | |
cccfa758 | 1462 | /* Parse new config value. */ |
a56e2df9 CB |
1463 | if (lxc_safe_uint(value, &lxc_conf->lsm_aa_allow_incomplete) < 0) |
1464 | return -1; | |
7aff4f43 | 1465 | |
a56e2df9 | 1466 | if (lxc_conf->lsm_aa_allow_incomplete > 1) { |
cccfa758 CB |
1467 | ERROR("Wrong value for lxc.lsm_aa_allow_incomplete. Can only " |
1468 | "be set to 0 or 1"); | |
a56e2df9 CB |
1469 | return -1; |
1470 | } | |
7aff4f43 SH |
1471 | |
1472 | return 0; | |
1473 | } | |
1474 | ||
713046e3 | 1475 | static int set_config_lsm_se_context(const char *key, const char *value, |
fe4de9a6 DE |
1476 | struct lxc_conf *lxc_conf) |
1477 | { | |
713046e3 | 1478 | return set_config_string_item(&lxc_conf->lsm_se_context, value); |
e075f5d9 | 1479 | } |
e075f5d9 | 1480 | |
713046e3 | 1481 | static int set_config_logfile(const char *key, const char *value, |
858377e4 | 1482 | struct lxc_conf *c) |
4a85ce2a | 1483 | { |
6d03d92a DE |
1484 | int ret; |
1485 | ||
b40a606e SH |
1486 | // store these values in the lxc_conf, and then try to set for |
1487 | // actual current logging. | |
713046e3 | 1488 | ret = set_config_path_item(&c->logfile, value); |
6d03d92a | 1489 | if (ret == 0) |
858377e4 | 1490 | ret = lxc_log_set_file(&c->logfd, c->logfile); |
6d03d92a | 1491 | return ret; |
4a85ce2a SH |
1492 | } |
1493 | ||
713046e3 | 1494 | static int set_config_loglevel(const char *key, const char *value, |
575b9745 | 1495 | struct lxc_conf *lxc_conf) |
4a85ce2a | 1496 | { |
9ea87d5d SH |
1497 | int newlevel; |
1498 | ||
575b9745 CB |
1499 | /* Set config value to default. */ |
1500 | if (config_value_empty(value)) { | |
1501 | lxc_conf->loglevel = LXC_LOG_PRIORITY_NOTSET; | |
4a85ce2a | 1502 | return 0; |
575b9745 | 1503 | } |
4a85ce2a | 1504 | |
575b9745 | 1505 | /* Parse new config value. */ |
a56e2df9 CB |
1506 | if (value[0] >= '0' && value[0] <= '9') { |
1507 | if (lxc_safe_int(value, &newlevel) < 0) | |
1508 | return -1; | |
1509 | } else { | |
9ea87d5d | 1510 | newlevel = lxc_log_priority_to_int(value); |
a56e2df9 | 1511 | } |
575b9745 CB |
1512 | |
1513 | /* store these values in the lxc_conf, and then try to set for actual | |
1514 | * current logging. | |
1515 | */ | |
b40a606e | 1516 | lxc_conf->loglevel = newlevel; |
858377e4 | 1517 | return lxc_log_set_level(&lxc_conf->loglevel, newlevel); |
4a85ce2a SH |
1518 | } |
1519 | ||
713046e3 | 1520 | static int set_config_autodev(const char *key, const char *value, |
c6883f38 SH |
1521 | struct lxc_conf *lxc_conf) |
1522 | { | |
1045031e CB |
1523 | /* Set config value to default. */ |
1524 | if (config_value_empty(value)) { | |
1525 | lxc_conf->autodev = 0; | |
180abbc0 | 1526 | return 0; |
1045031e | 1527 | } |
180abbc0 | 1528 | |
1045031e | 1529 | /* Parse new config value. */ |
ff6cb4ed CB |
1530 | if (lxc_safe_uint(value, &lxc_conf->autodev) < 0) |
1531 | return -1; | |
c6883f38 | 1532 | |
ff6cb4ed CB |
1533 | if (lxc_conf->autodev > 1) { |
1534 | ERROR("Wrong value for lxc.autodev. Can only be set to 0 or 1"); | |
1535 | return -1; | |
1536 | } | |
c6883f38 SH |
1537 | |
1538 | return 0; | |
1539 | } | |
1540 | ||
a84b9932 AV |
1541 | static int sig_num(const char *sig) |
1542 | { | |
f2e539b3 | 1543 | unsigned int signum; |
a84b9932 | 1544 | |
f2e539b3 | 1545 | if (lxc_safe_uint(sig, &signum) < 0) |
a84b9932 | 1546 | return -1; |
f2e539b3 CB |
1547 | |
1548 | return signum; | |
a84b9932 AV |
1549 | } |
1550 | ||
1551 | static int rt_sig_num(const char *signame) | |
1552 | { | |
1553 | int sig_n = 0; | |
1554 | int rtmax = 0; | |
1555 | ||
1556 | if (strncasecmp(signame, "max-", 4) == 0) { | |
1557 | rtmax = 1; | |
1558 | } | |
1559 | signame += 4; | |
1560 | if (!isdigit(*signame)) | |
1561 | return -1; | |
1562 | sig_n = sig_num(signame); | |
1563 | sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n; | |
1564 | if (sig_n > SIGRTMAX || sig_n < SIGRTMIN) | |
1565 | return -1; | |
1566 | return sig_n; | |
1567 | } | |
1568 | ||
1569 | static int sig_parse(const char *signame) { | |
84760c11 | 1570 | size_t n; |
a84b9932 AV |
1571 | |
1572 | if (isdigit(*signame)) { | |
1573 | return sig_num(signame); | |
1574 | } else if (strncasecmp(signame, "sig", 3) == 0) { | |
1575 | signame += 3; | |
1576 | if (strncasecmp(signame, "rt", 2) == 0) | |
1577 | return rt_sig_num(signame + 2); | |
1578 | for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++) { | |
1579 | if (strcasecmp (signames[n].name, signame) == 0) | |
1580 | return signames[n].num; | |
1581 | } | |
1582 | } | |
1583 | return -1; | |
1584 | } | |
1585 | ||
713046e3 | 1586 | static int set_config_haltsignal(const char *key, const char *value, |
f0f1d8c0 DE |
1587 | struct lxc_conf *lxc_conf) |
1588 | { | |
62a085fb | 1589 | int sig_n; |
f0f1d8c0 | 1590 | |
c1a64603 CB |
1591 | /* Set config value to default. */ |
1592 | if (config_value_empty(value)) { | |
1593 | lxc_conf->haltsignal = 0; | |
955912f0 | 1594 | return 0; |
c1a64603 | 1595 | } |
955912f0 | 1596 | |
c1a64603 | 1597 | /* Parse new config value. */ |
62a085fb CB |
1598 | sig_n = sig_parse(value); |
1599 | ||
f0f1d8c0 DE |
1600 | if (sig_n < 0) |
1601 | return -1; | |
1602 | lxc_conf->haltsignal = sig_n; | |
1603 | ||
1604 | return 0; | |
1605 | } | |
1606 | ||
713046e3 | 1607 | static int set_config_rebootsignal(const char *key, const char *value, |
9d7e7587 | 1608 | struct lxc_conf *lxc_conf) |
dd267776 | 1609 | { |
9d7e7587 | 1610 | int sig_n; |
dd267776 | 1611 | |
18fcee44 CB |
1612 | /* Set config value to default. */ |
1613 | if (config_value_empty(value)) { | |
1614 | lxc_conf->rebootsignal = 0; | |
9d7e7587 | 1615 | return 0; |
18fcee44 | 1616 | } |
9d7e7587 | 1617 | |
18fcee44 | 1618 | /* Parse new config value. */ |
9d7e7587 | 1619 | sig_n = sig_parse(value); |
dd267776 BP |
1620 | if (sig_n < 0) |
1621 | return -1; | |
1622 | lxc_conf->rebootsignal = sig_n; | |
1623 | ||
1624 | return 0; | |
1625 | } | |
1626 | ||
713046e3 | 1627 | static int set_config_stopsignal(const char *key, const char *value, |
6ca6aedd | 1628 | struct lxc_conf *lxc_conf) |
a84b9932 | 1629 | { |
6ca6aedd | 1630 | int sig_n; |
a84b9932 | 1631 | |
4100d1a7 CB |
1632 | /* Set config value to default. */ |
1633 | if (config_value_empty(value)) { | |
1634 | lxc_conf->stopsignal = 0; | |
6ca6aedd | 1635 | return 0; |
4100d1a7 | 1636 | } |
6ca6aedd | 1637 | |
4100d1a7 | 1638 | /* Parse new config value. */ |
6ca6aedd | 1639 | sig_n = sig_parse(value); |
a84b9932 AV |
1640 | if (sig_n < 0) |
1641 | return -1; | |
1642 | lxc_conf->stopsignal = sig_n; | |
1643 | ||
1644 | return 0; | |
1645 | } | |
1646 | ||
713046e3 | 1647 | static int set_config_cgroup(const char *key, const char *value, |
12a50cc6 | 1648 | struct lxc_conf *lxc_conf) |
576f946d | 1649 | { |
1650 | char *token = "lxc.cgroup."; | |
1651 | char *subkey; | |
bf83c5b9 MS |
1652 | struct lxc_list *cglist = NULL; |
1653 | struct lxc_cgroup *cgelem = NULL; | |
576f946d | 1654 | |
fb0752be | 1655 | if (config_value_empty(value)) |
7d0eb87e SH |
1656 | return lxc_clear_cgroups(lxc_conf, key); |
1657 | ||
576f946d | 1658 | subkey = strstr(key, token); |
1659 | ||
1660 | if (!subkey) | |
1661 | return -1; | |
1662 | ||
1663 | if (!strlen(subkey)) | |
1664 | return -1; | |
1665 | ||
1666 | if (strlen(subkey) == strlen(token)) | |
1667 | return -1; | |
a871ff6b | 1668 | |
576f946d | 1669 | subkey += strlen(token); |
1670 | ||
1671 | cglist = malloc(sizeof(*cglist)); | |
1672 | if (!cglist) | |
bf83c5b9 | 1673 | goto out; |
576f946d | 1674 | |
1675 | cgelem = malloc(sizeof(*cgelem)); | |
bf83c5b9 MS |
1676 | if (!cgelem) |
1677 | goto out; | |
1678 | memset(cgelem, 0, sizeof(*cgelem)); | |
576f946d | 1679 | |
1680 | cgelem->subsystem = strdup(subkey); | |
1681 | cgelem->value = strdup(value); | |
bf83c5b9 MS |
1682 | |
1683 | if (!cgelem->subsystem || !cgelem->value) | |
1684 | goto out; | |
1685 | ||
576f946d | 1686 | cglist->elem = cgelem; |
1687 | ||
94d12f0a | 1688 | lxc_list_add_tail(&lxc_conf->cgroup, cglist); |
576f946d | 1689 | |
1690 | return 0; | |
bf83c5b9 MS |
1691 | |
1692 | out: | |
f10fad2f | 1693 | free(cglist); |
bf83c5b9 MS |
1694 | |
1695 | if (cgelem) { | |
f10fad2f | 1696 | free(cgelem->subsystem); |
bf83c5b9 | 1697 | |
f10fad2f | 1698 | free(cgelem->value); |
bf83c5b9 MS |
1699 | |
1700 | free(cgelem); | |
1701 | } | |
1702 | ||
1703 | return -1; | |
576f946d | 1704 | } |
1705 | ||
c6d09e15 WB |
1706 | static bool parse_limit_value(const char **value, unsigned long *res) { |
1707 | char *endptr = NULL; | |
1708 | ||
1709 | if (strncmp(*value, "unlimited", sizeof("unlimited")-1) == 0) { | |
1710 | *res = RLIM_INFINITY; | |
1711 | *value += sizeof("unlimited")-1; | |
1712 | return true; | |
1713 | } | |
1714 | ||
1715 | errno = 0; | |
1716 | *res = strtoul(*value, &endptr, 10); | |
1717 | if (errno || !endptr) | |
1718 | return false; | |
1719 | *value = endptr; | |
1720 | ||
1721 | return true; | |
1722 | } | |
1723 | ||
713046e3 | 1724 | static int set_config_limit(const char *key, const char *value, |
c6d09e15 WB |
1725 | struct lxc_conf *lxc_conf) |
1726 | { | |
1727 | struct lxc_list *limlist = NULL; | |
1728 | struct lxc_limit *limelem = NULL; | |
1729 | struct lxc_list *iter; | |
1730 | struct rlimit limit; | |
1731 | unsigned long limit_value; | |
1732 | ||
7a18f483 | 1733 | if (config_value_empty(value)) |
c6d09e15 WB |
1734 | return lxc_clear_limits(lxc_conf, key); |
1735 | ||
1736 | if (strncmp(key, "lxc.limit.", sizeof("lxc.limit.")-1) != 0) | |
1737 | return -1; | |
1738 | ||
1739 | key += sizeof("lxc.limit.")-1; | |
1740 | ||
1741 | /* soft limit comes first in the value */ | |
1742 | if (!parse_limit_value(&value, &limit_value)) | |
1743 | return -1; | |
1744 | limit.rlim_cur = limit_value; | |
1745 | ||
1746 | /* skip spaces and a colon */ | |
1747 | while (isspace(*value)) | |
1748 | ++value; | |
1749 | if (*value == ':') | |
1750 | ++value; | |
1751 | else if (*value) /* any other character is an error here */ | |
1752 | return -1; | |
1753 | while (isspace(*value)) | |
1754 | ++value; | |
1755 | ||
1756 | /* optional hard limit */ | |
1757 | if (*value) { | |
1758 | if (!parse_limit_value(&value, &limit_value)) | |
1759 | return -1; | |
1760 | limit.rlim_max = limit_value; | |
1761 | /* check for trailing garbage */ | |
1762 | while (isspace(*value)) | |
1763 | ++value; | |
1764 | if (*value) | |
1765 | return -1; | |
1766 | } else { | |
1767 | /* a single value sets both hard and soft limit */ | |
1768 | limit.rlim_max = limit.rlim_cur; | |
1769 | } | |
1770 | ||
1771 | /* find existing list element */ | |
1772 | lxc_list_for_each(iter, &lxc_conf->limits) { | |
1773 | limelem = iter->elem; | |
1774 | if (!strcmp(key, limelem->resource)) { | |
1775 | limelem->limit = limit; | |
1776 | return 0; | |
1777 | } | |
1778 | } | |
1779 | ||
1780 | /* allocate list element */ | |
1781 | limlist = malloc(sizeof(*limlist)); | |
1782 | if (!limlist) | |
1783 | goto out; | |
2e6e3feb | 1784 | |
c6d09e15 WB |
1785 | limelem = malloc(sizeof(*limelem)); |
1786 | if (!limelem) | |
1787 | goto out; | |
1788 | memset(limelem, 0, sizeof(*limelem)); | |
1789 | ||
1790 | limelem->resource = strdup(key); | |
1791 | if (!limelem->resource) | |
1792 | goto out; | |
1793 | limelem->limit = limit; | |
1794 | ||
1795 | limlist->elem = limelem; | |
1796 | ||
1797 | lxc_list_add_tail(&lxc_conf->limits, limlist); | |
1798 | ||
1799 | return 0; | |
1800 | ||
1801 | out: | |
1802 | free(limlist); | |
1803 | if (limelem) { | |
1804 | free(limelem->resource); | |
1805 | free(limelem); | |
1806 | } | |
1807 | return -1; | |
1808 | } | |
1809 | ||
713046e3 | 1810 | static int set_config_idmap(const char *key, const char *value, struct lxc_conf *lxc_conf) |
f6d3e3e4 | 1811 | { |
251d0d2a | 1812 | unsigned long hostid, nsid, range; |
f6d3e3e4 | 1813 | char type; |
34a7a4c6 CB |
1814 | char *window, *slide; |
1815 | char *dup = NULL; | |
1816 | struct lxc_list *idmaplist = NULL; | |
1817 | struct id_map *idmap = NULL; | |
f6d3e3e4 | 1818 | |
ab8d8307 | 1819 | if (config_value_empty(value)) |
7d0eb87e SH |
1820 | return lxc_clear_idmaps(lxc_conf); |
1821 | ||
f6d3e3e4 SH |
1822 | idmaplist = malloc(sizeof(*idmaplist)); |
1823 | if (!idmaplist) | |
34a7a4c6 | 1824 | goto on_error; |
f6d3e3e4 SH |
1825 | |
1826 | idmap = malloc(sizeof(*idmap)); | |
1827 | if (!idmap) | |
34a7a4c6 | 1828 | goto on_error; |
f6d3e3e4 SH |
1829 | memset(idmap, 0, sizeof(*idmap)); |
1830 | ||
34a7a4c6 CB |
1831 | /* Duplicate string. */ |
1832 | dup = strdup(value); | |
1833 | if (!dup) | |
1834 | goto on_error; | |
1835 | ||
1836 | /* A prototypical idmap entry would be: "0 1000 1000000 65536" */ | |
1837 | ||
1838 | /* align */ | |
1839 | slide = window = dup; | |
1840 | /* skip whitespace */ | |
1841 | slide += strspn(slide, " \t\r"); | |
1842 | if (slide != window && *slide == '\0') | |
1843 | goto on_error; | |
1844 | ||
1845 | /* Validate type. */ | |
1846 | if (*slide != 'u' && *slide != 'g') | |
1847 | goto on_error; | |
1848 | /* Assign type. */ | |
1849 | type = *slide; | |
1850 | ||
1851 | /* move beyond type */ | |
1852 | slide++; | |
1853 | /* align */ | |
1854 | window = slide; | |
1855 | /* Validate that only whitespace follows. */ | |
1856 | slide += strspn(slide, " \t\r"); | |
1857 | /* There must be whitespace. */ | |
1858 | if (slide == window) | |
1859 | goto on_error; | |
1860 | ||
1861 | /* Mark beginning of nsuid. */ | |
1862 | window = slide; | |
1863 | /* Validate that non-whitespace follows. */ | |
1864 | slide += strcspn(slide, " \t\r"); | |
1865 | /* There must be non-whitespace. */ | |
1866 | if (slide == window || *slide == '\0') | |
1867 | goto on_error; | |
1868 | /* Mark end of nsuid. */ | |
1869 | *slide = '\0'; | |
1870 | ||
1871 | /* Parse nsuid. */ | |
1872 | if (lxc_safe_ulong(window, &nsid) < 0) | |
1873 | goto on_error; | |
1874 | ||
1875 | /* Move beyond \0. */ | |
1876 | slide++; | |
1877 | /* align */ | |
1878 | window = slide; | |
1879 | /* Validate that only whitespace follows. */ | |
1880 | slide += strspn(slide, " \t\r"); | |
1881 | /* If there was only one whitespace then we whiped it with our \0 above. | |
1882 | * So only ensure that we're not at the end of the string. | |
1883 | */ | |
1884 | if (*slide == '\0') | |
1885 | goto on_error; | |
1886 | ||
1887 | /* Mark beginning of hostid. */ | |
1888 | window = slide; | |
1889 | /* Validate that non-whitespace follows. */ | |
1890 | slide += strcspn(slide, " \t\r"); | |
1891 | /* There must be non-whitespace. */ | |
1892 | if (slide == window || *slide == '\0') | |
1893 | goto on_error; | |
1894 | /* Mark end of nsuid. */ | |
1895 | *slide = '\0'; | |
1896 | ||
1897 | /* Parse hostid. */ | |
1898 | if (lxc_safe_ulong(window, &hostid) < 0) | |
1899 | goto on_error; | |
1900 | ||
1901 | /* Move beyond \0. */ | |
1902 | slide++; | |
1903 | /* align */ | |
1904 | window = slide; | |
1905 | /* Validate that only whitespace follows. */ | |
1906 | slide += strspn(slide, " \t\r"); | |
1907 | /* If there was only one whitespace then we whiped it with our \0 above. | |
1908 | * So only ensure that we're not at the end of the string. | |
1909 | */ | |
1910 | if (*slide == '\0') | |
1911 | goto on_error; | |
1912 | ||
1913 | /* Mark beginning of range. */ | |
1914 | window = slide; | |
1915 | /* Validate that non-whitespace follows. */ | |
1916 | slide += strcspn(slide, " \t\r"); | |
1917 | /* There must be non-whitespace. */ | |
1918 | if (slide == window) | |
1919 | goto on_error; | |
1920 | ||
1921 | /* The range is the last valid entry we expect. So make sure that there | |
1922 | * is not trailing garbage and if there is, error out. | |
1923 | */ | |
1924 | if (*(slide + strspn(slide, " \t\r\n")) != '\0') | |
1925 | goto on_error; | |
1926 | /* Mark end of range. */ | |
1927 | *slide = '\0'; | |
7e60c3f0 | 1928 | |
34a7a4c6 CB |
1929 | /* Parse range. */ |
1930 | if (lxc_safe_ulong(window, &range) < 0) | |
1931 | goto on_error; | |
1932 | ||
1933 | /* Yay, we survived. */ | |
251d0d2a | 1934 | INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range); |
ac7725e7 | 1935 | if (type == 'u') |
f6d3e3e4 | 1936 | idmap->idtype = ID_TYPE_UID; |
ac7725e7 | 1937 | else if (type == 'g') |
f6d3e3e4 SH |
1938 | idmap->idtype = ID_TYPE_GID; |
1939 | else | |
34a7a4c6 | 1940 | goto on_error; |
7e60c3f0 | 1941 | |
f6d3e3e4 SH |
1942 | idmap->hostid = hostid; |
1943 | idmap->nsid = nsid; | |
1944 | idmap->range = range; | |
7e60c3f0 SG |
1945 | idmaplist->elem = idmap; |
1946 | lxc_list_add_tail(&lxc_conf->id_map, idmaplist); | |
34a7a4c6 | 1947 | idmap = NULL; |
7e60c3f0 | 1948 | |
f6d3e3e4 SH |
1949 | return 0; |
1950 | ||
34a7a4c6 | 1951 | on_error: |
f10fad2f | 1952 | free(idmaplist); |
34a7a4c6 CB |
1953 | free(idmap); |
1954 | free(dup); | |
f6d3e3e4 SH |
1955 | |
1956 | return -1; | |
1957 | } | |
1958 | ||
713046e3 | 1959 | static int set_config_fstab(const char *key, const char *value, |
d95db067 DE |
1960 | struct lxc_conf *lxc_conf) |
1961 | { | |
46f3de30 CB |
1962 | if (config_value_empty(value)) { |
1963 | lxc_clear_config_item(lxc_conf, key); | |
d9192f5d | 1964 | return -1; |
46f3de30 | 1965 | } |
6f5685f0 | 1966 | |
713046e3 | 1967 | return set_config_path_item(&lxc_conf->fstab, value); |
d95db067 DE |
1968 | } |
1969 | ||
713046e3 | 1970 | static int set_config_mount_auto(const char *key, const char *value, |
368bbc02 CS |
1971 | struct lxc_conf *lxc_conf) |
1972 | { | |
1973 | char *autos, *autoptr, *sptr, *token; | |
b06b8511 | 1974 | static struct { const char *token; int mask; int flag; } allowed_auto_mounts[] = { |
0769b82a CS |
1975 | { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED }, |
1976 | { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED }, | |
1977 | { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW }, | |
f24a52d5 | 1978 | { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED }, |
0769b82a | 1979 | { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO }, |
f24a52d5 | 1980 | { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED }, |
0769b82a CS |
1981 | { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW }, |
1982 | { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC }, | |
1983 | { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED }, | |
1984 | { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO }, | |
1985 | { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW }, | |
1986 | { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC }, | |
1987 | { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED }, | |
1988 | { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO }, | |
1989 | { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW }, | |
70f55bfd | 1990 | /* NB: For adding anything that is just a single on/off, but has |
b06b8511 CS |
1991 | * no options: keep mask and flag identical and just define the |
1992 | * enum value as an unused bit so far | |
1993 | */ | |
368bbc02 CS |
1994 | { NULL, 0 } |
1995 | }; | |
1996 | int i; | |
1997 | int ret = -1; | |
1998 | ||
ede57f73 | 1999 | if (config_value_empty(value)) { |
d9192f5d SH |
2000 | lxc_conf->auto_mounts = 0; |
2001 | return 0; | |
2002 | } | |
368bbc02 CS |
2003 | |
2004 | autos = strdup(value); | |
2005 | if (!autos) { | |
2006 | SYSERROR("failed to dup '%s'", value); | |
2007 | return -1; | |
2008 | } | |
2009 | ||
2010 | for (autoptr = autos; ; autoptr = NULL) { | |
d028235d SG |
2011 | token = strtok_r(autoptr, " \t", &sptr); |
2012 | if (!token) { | |
368bbc02 | 2013 | ret = 0; |
d028235d | 2014 | break; |
368bbc02 CS |
2015 | } |
2016 | ||
2017 | for (i = 0; allowed_auto_mounts[i].token; i++) { | |
2018 | if (!strcmp(allowed_auto_mounts[i].token, token)) | |
2019 | break; | |
2020 | } | |
2021 | ||
2022 | if (!allowed_auto_mounts[i].token) { | |
2023 | ERROR("Invalid filesystem to automount: %s", token); | |
2024 | break; | |
2025 | } | |
2026 | ||
b06b8511 | 2027 | lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask; |
368bbc02 | 2028 | lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag; |
d028235d | 2029 | } |
368bbc02 CS |
2030 | |
2031 | free(autos); | |
2032 | ||
2033 | return ret; | |
2034 | } | |
2035 | ||
713046e3 | 2036 | static int set_config_mount(const char *key, const char *value, |
12a50cc6 | 2037 | struct lxc_conf *lxc_conf) |
e7938e9e | 2038 | { |
e7938e9e MN |
2039 | char *mntelem; |
2040 | struct lxc_list *mntlist; | |
2041 | ||
67cbe21a | 2042 | if (config_value_empty(value)) |
d9192f5d | 2043 | return lxc_clear_mount_entries(lxc_conf); |
e7938e9e MN |
2044 | |
2045 | mntlist = malloc(sizeof(*mntlist)); | |
2046 | if (!mntlist) | |
2047 | return -1; | |
2048 | ||
2049 | mntelem = strdup(value); | |
00b6be44 SH |
2050 | if (!mntelem) { |
2051 | free(mntlist); | |
bf83c5b9 | 2052 | return -1; |
00b6be44 | 2053 | } |
e7938e9e MN |
2054 | mntlist->elem = mntelem; |
2055 | ||
2056 | lxc_list_add_tail(&lxc_conf->mount_list, mntlist); | |
2057 | ||
2058 | return 0; | |
2059 | } | |
2060 | ||
713046e3 | 2061 | static int set_config_cap_keep(const char *key, const char *value, |
1fb86a7c SH |
2062 | struct lxc_conf *lxc_conf) |
2063 | { | |
2064 | char *keepcaps, *keepptr, *sptr, *token; | |
2065 | struct lxc_list *keeplist; | |
2066 | int ret = -1; | |
2067 | ||
99feb7f1 | 2068 | if (config_value_empty(value)) |
7d0eb87e | 2069 | return lxc_clear_config_keepcaps(lxc_conf); |
1fb86a7c SH |
2070 | |
2071 | keepcaps = strdup(value); | |
2072 | if (!keepcaps) { | |
2073 | SYSERROR("failed to dup '%s'", value); | |
2074 | return -1; | |
2075 | } | |
2076 | ||
2077 | /* in case several capability keep is specified in a single line | |
2078 | * split these caps in a single element for the list */ | |
2079 | for (keepptr = keepcaps;;keepptr = NULL) { | |
d028235d SG |
2080 | token = strtok_r(keepptr, " \t", &sptr); |
2081 | if (!token) { | |
1fb86a7c | 2082 | ret = 0; |
d028235d | 2083 | break; |
1fb86a7c SH |
2084 | } |
2085 | ||
7035407c DE |
2086 | if (!strcmp(token, "none")) |
2087 | lxc_clear_config_keepcaps(lxc_conf); | |
2088 | ||
1fb86a7c SH |
2089 | keeplist = malloc(sizeof(*keeplist)); |
2090 | if (!keeplist) { | |
2091 | SYSERROR("failed to allocate keepcap list"); | |
2092 | break; | |
2093 | } | |
2094 | ||
2095 | keeplist->elem = strdup(token); | |
2096 | if (!keeplist->elem) { | |
2097 | SYSERROR("failed to dup '%s'", token); | |
2098 | free(keeplist); | |
2099 | break; | |
2100 | } | |
2101 | ||
2102 | lxc_list_add_tail(&lxc_conf->keepcaps, keeplist); | |
d028235d | 2103 | } |
1fb86a7c SH |
2104 | |
2105 | free(keepcaps); | |
2106 | ||
2107 | return ret; | |
2108 | } | |
2109 | ||
713046e3 | 2110 | static int set_config_cap_drop(const char *key, const char *value, |
81810dd1 DL |
2111 | struct lxc_conf *lxc_conf) |
2112 | { | |
d95db067 | 2113 | char *dropcaps, *dropptr, *sptr, *token; |
81810dd1 DL |
2114 | struct lxc_list *droplist; |
2115 | int ret = -1; | |
2116 | ||
b2fae748 | 2117 | if (config_value_empty(value)) |
7d0eb87e | 2118 | return lxc_clear_config_caps(lxc_conf); |
81810dd1 DL |
2119 | |
2120 | dropcaps = strdup(value); | |
2121 | if (!dropcaps) { | |
2122 | SYSERROR("failed to dup '%s'", value); | |
2123 | return -1; | |
2124 | } | |
2125 | ||
2126 | /* in case several capability drop is specified in a single line | |
2127 | * split these caps in a single element for the list */ | |
d95db067 | 2128 | for (dropptr = dropcaps;;dropptr = NULL) { |
d028235d SG |
2129 | token = strtok_r(dropptr, " \t", &sptr); |
2130 | if (!token) { | |
81810dd1 | 2131 | ret = 0; |
d028235d | 2132 | break; |
81810dd1 | 2133 | } |
81810dd1 DL |
2134 | |
2135 | droplist = malloc(sizeof(*droplist)); | |
2136 | if (!droplist) { | |
2137 | SYSERROR("failed to allocate drop list"); | |
2138 | break; | |
2139 | } | |
2140 | ||
2141 | droplist->elem = strdup(token); | |
2142 | if (!droplist->elem) { | |
2143 | SYSERROR("failed to dup '%s'", token); | |
2144 | free(droplist); | |
2145 | break; | |
2146 | } | |
2147 | ||
2148 | lxc_list_add_tail(&lxc_conf->caps, droplist); | |
d028235d | 2149 | } |
81810dd1 DL |
2150 | |
2151 | free(dropcaps); | |
2152 | ||
2153 | return ret; | |
2154 | } | |
2155 | ||
713046e3 | 2156 | static int set_config_console(const char *key, const char *value, |
28a4b0e5 DL |
2157 | struct lxc_conf *lxc_conf) |
2158 | { | |
713046e3 | 2159 | return set_config_path_item(&lxc_conf->console.path, value); |
28a4b0e5 DL |
2160 | } |
2161 | ||
713046e3 | 2162 | static int set_config_console_logfile(const char *key, const char *value, |
96f15ca1 SH |
2163 | struct lxc_conf *lxc_conf) |
2164 | { | |
713046e3 | 2165 | return set_config_path_item(&lxc_conf->console.log_path, value); |
96f15ca1 SH |
2166 | } |
2167 | ||
e6744e9b SH |
2168 | /* |
2169 | * If we find a lxc.network.hwaddr in the original config file, | |
2170 | * we expand it in the unexpanded_config, so that after a save_config | |
2171 | * we store the hwaddr for re-use. | |
2172 | * This is only called when reading the config file, not when executing | |
2173 | * a lxc.include. | |
2174 | * 'x' and 'X' are substituted in-place. | |
2175 | */ | |
2176 | static void update_hwaddr(const char *line) | |
2177 | { | |
2178 | char *p; | |
2179 | ||
2180 | line += lxc_char_left_gc(line, strlen(line)); | |
2181 | if (line[0] == '#') | |
2182 | return; | |
2183 | if (strncmp(line, "lxc.network.hwaddr", 18) != 0) | |
2184 | return; | |
2185 | p = strchr(line, '='); | |
2186 | if (!p) | |
2187 | return; // let config_network_hwaddr raise the error | |
2188 | p++; | |
2189 | while (isblank(*p)) | |
2190 | p++; | |
2191 | if (!*p) | |
2192 | return; | |
2193 | ||
2194 | rand_complete_hwaddr(p); | |
2195 | } | |
2196 | ||
6b0d5538 | 2197 | int append_unexp_config_line(const char *line, struct lxc_conf *conf) |
f979ac15 | 2198 | { |
6b0d5538 | 2199 | size_t len = conf->unexpanded_len, linelen = strlen(line); |
f979ac15 | 2200 | |
e6744e9b SH |
2201 | update_hwaddr(line); |
2202 | ||
6b0d5538 SH |
2203 | while (conf->unexpanded_alloced <= len + linelen + 2) { |
2204 | char *tmp = realloc(conf->unexpanded_config, conf->unexpanded_alloced + 1024); | |
2205 | if (!tmp) | |
2206 | return -1; | |
2207 | if (!conf->unexpanded_config) | |
2208 | *tmp = '\0'; | |
2209 | conf->unexpanded_config = tmp; | |
2210 | conf->unexpanded_alloced += 1024; | |
2211 | } | |
2212 | strcat(conf->unexpanded_config, line); | |
2213 | conf->unexpanded_len += linelen; | |
2214 | if (line[linelen-1] != '\n') { | |
2215 | strcat(conf->unexpanded_config, "\n"); | |
2216 | conf->unexpanded_len++; | |
f979ac15 | 2217 | } |
f979ac15 SH |
2218 | return 0; |
2219 | } | |
2220 | ||
e1daebd9 SH |
2221 | static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf) |
2222 | { | |
74f96976 | 2223 | struct dirent *direntp; |
e1daebd9 SH |
2224 | DIR *dir; |
2225 | char path[MAXPATHLEN]; | |
2226 | int ret = -1, len; | |
2227 | ||
2228 | dir = opendir(dirp); | |
2229 | if (!dir) { | |
2230 | SYSERROR("failed to open '%s'", dirp); | |
2231 | return -1; | |
2232 | } | |
2233 | ||
74f96976 | 2234 | while ((direntp = readdir(dir))) { |
e1daebd9 SH |
2235 | const char *fnam; |
2236 | if (!direntp) | |
2237 | break; | |
2238 | ||
2239 | fnam = direntp->d_name; | |
2240 | if (!strcmp(fnam, ".")) | |
2241 | continue; | |
2242 | ||
2243 | if (!strcmp(fnam, "..")) | |
2244 | continue; | |
2245 | ||
2246 | len = strlen(fnam); | |
2247 | if (len < 6 || strncmp(fnam+len-5, ".conf", 5) != 0) | |
2248 | continue; | |
2249 | len = snprintf(path, MAXPATHLEN, "%s/%s", dirp, fnam); | |
2250 | if (len < 0 || len >= MAXPATHLEN) { | |
2251 | ERROR("lxc.include filename too long under '%s'", dirp); | |
2252 | ret = -1; | |
2253 | goto out; | |
2254 | } | |
2255 | ||
2256 | ret = lxc_config_read(path, lxc_conf, true); | |
2257 | if (ret < 0) | |
2258 | goto out; | |
2259 | } | |
2260 | ret = 0; | |
2261 | ||
2262 | out: | |
2263 | if (closedir(dir)) | |
2264 | WARN("lxc.include dir: failed to close directory"); | |
2265 | ||
2266 | return ret; | |
2267 | } | |
2268 | ||
713046e3 | 2269 | static int set_config_includefile(const char *key, const char *value, |
355c5701 | 2270 | struct lxc_conf *lxc_conf) |
09ad6246 | 2271 | { |
355c5701 CB |
2272 | /* Set config value to default. */ |
2273 | if (config_value_empty(value)) { | |
2274 | lxc_clear_config_item(lxc_conf, key); | |
616422f1 | 2275 | return 0; |
355c5701 | 2276 | } |
616422f1 | 2277 | |
355c5701 | 2278 | /* Parse new config value. */ |
e1daebd9 SH |
2279 | if (is_dir(value)) |
2280 | return do_includedir(value, lxc_conf); | |
2281 | ||
6b0d5538 | 2282 | return lxc_config_read(value, lxc_conf, true); |
09ad6246 SH |
2283 | } |
2284 | ||
713046e3 | 2285 | static int set_config_rootfs(const char *key, const char *value, |
12a50cc6 | 2286 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 2287 | { |
713046e3 | 2288 | return set_config_path_item(&lxc_conf->rootfs.path, value); |
c2cc9f0a | 2289 | } |
2290 | ||
713046e3 | 2291 | static int set_config_rootfs_mount(const char *key, const char *value, |
12a50cc6 | 2292 | struct lxc_conf *lxc_conf) |
23b7ea69 | 2293 | { |
713046e3 | 2294 | return set_config_path_item(&lxc_conf->rootfs.mount, value); |
23b7ea69 DL |
2295 | } |
2296 | ||
713046e3 | 2297 | static int set_config_rootfs_options(const char *key, const char *value, |
a17b1e65 SG |
2298 | struct lxc_conf *lxc_conf) |
2299 | { | |
713046e3 | 2300 | return set_config_string_item(&lxc_conf->rootfs.options, value); |
a17b1e65 SG |
2301 | } |
2302 | ||
713046e3 | 2303 | static int set_config_rootfs_backend(const char *key, const char *value, |
b8223439 | 2304 | struct lxc_conf *lxc_conf) |
bfd77214 | 2305 | { |
b8223439 | 2306 | if (config_value_empty(value)) { |
6a21d4ae SH |
2307 | free(lxc_conf->rootfs.bdev_type); |
2308 | lxc_conf->rootfs.bdev_type = NULL; | |
b8223439 | 2309 | return 0; |
6a21d4ae | 2310 | } |
b8223439 | 2311 | |
bfd77214 | 2312 | if (!is_valid_bdev_type(value)) { |
327a1e78 | 2313 | ERROR("Bad rootfs.backend: '%s'", value); |
bfd77214 SH |
2314 | return -1; |
2315 | } | |
2316 | ||
713046e3 | 2317 | return set_config_string_item(&lxc_conf->rootfs.bdev_type, value); |
bfd77214 SH |
2318 | } |
2319 | ||
713046e3 | 2320 | static int set_config_pivotdir(const char *key, const char *value, |
12a50cc6 | 2321 | struct lxc_conf *lxc_conf) |
bf601689 | 2322 | { |
2d489f9e | 2323 | WARN("lxc.pivotdir is ignored. It will soon become an error."); |
59bb8698 | 2324 | return 0; |
bf601689 MH |
2325 | } |
2326 | ||
713046e3 | 2327 | static int set_config_utsname(const char *key, const char *value, |
12a50cc6 | 2328 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 2329 | { |
2330 | struct utsname *utsname; | |
2331 | ||
00cd8039 CB |
2332 | if (config_value_empty(value)) { |
2333 | lxc_clear_config_item(lxc_conf, key); | |
1939e73d | 2334 | return 0; |
00cd8039 | 2335 | } |
1939e73d | 2336 | |
c2cc9f0a | 2337 | utsname = malloc(sizeof(*utsname)); |
2338 | if (!utsname) { | |
36eb9bde | 2339 | SYSERROR("failed to allocate memory"); |
c2cc9f0a | 2340 | return -1; |
2341 | } | |
2342 | ||
2343 | if (strlen(value) >= sizeof(utsname->nodename)) { | |
00cd8039 | 2344 | ERROR("node name '%s' is too long", value); |
b6f24d54 | 2345 | free(utsname); |
c2cc9f0a | 2346 | return -1; |
2347 | } | |
2348 | ||
2349 | strcpy(utsname->nodename, value); | |
f10fad2f | 2350 | free(lxc_conf->utsname); |
c2cc9f0a | 2351 | lxc_conf->utsname = utsname; |
2352 | ||
2353 | return 0; | |
2354 | } | |
2355 | ||
6b0d5538 SH |
2356 | struct parse_line_conf { |
2357 | struct lxc_conf *conf; | |
2358 | bool from_include; | |
2359 | }; | |
4184c3e1 | 2360 | |
7a7ff0c6 | 2361 | static int parse_line(char *buffer, void *data) |
c2cc9f0a | 2362 | { |
72d0e1cb | 2363 | struct lxc_config_t *config; |
81192358 | 2364 | char *line, *linep; |
c2cc9f0a | 2365 | char *dot; |
2366 | char *key; | |
2367 | char *value; | |
476d4cf1 | 2368 | int ret = 0; |
6b0d5538 | 2369 | struct parse_line_conf *plc = data; |
c2cc9f0a | 2370 | |
91480a0f | 2371 | if (lxc_is_line_empty(buffer)) |
c2cc9f0a | 2372 | return 0; |
2373 | ||
81192358 DL |
2374 | /* we have to dup the buffer otherwise, at the re-exec for |
2375 | * reboot we modified the original string on the stack by | |
2376 | * replacing '=' by '\0' below | |
91480a0f | 2377 | */ |
81192358 | 2378 | linep = line = strdup(buffer); |
91480a0f DL |
2379 | if (!line) { |
2380 | SYSERROR("failed to allocate memory for '%s'", buffer); | |
81192358 | 2381 | return -1; |
91480a0f DL |
2382 | } |
2383 | ||
6b0d5538 SH |
2384 | if (!plc->from_include) |
2385 | if ((ret = append_unexp_config_line(line, plc->conf))) | |
2386 | goto out; | |
2387 | ||
b2718c72 | 2388 | line += lxc_char_left_gc(line, strlen(line)); |
476d4cf1 | 2389 | |
4184c3e1 SH |
2390 | /* ignore comments */ |
2391 | if (line[0] == '#') | |
91480a0f | 2392 | goto out; |
476d4cf1 | 2393 | |
6b0d5538 SH |
2394 | /* martian option - don't add it to the config itself */ |
2395 | if (strncmp(line, "lxc.", 4)) | |
4184c3e1 | 2396 | goto out; |
4184c3e1 | 2397 | |
476d4cf1 | 2398 | ret = -1; |
c2cc9f0a | 2399 | |
b2718c72 | 2400 | dot = strstr(line, "="); |
c2cc9f0a | 2401 | if (!dot) { |
36eb9bde | 2402 | ERROR("invalid configuration line: %s", line); |
91480a0f | 2403 | goto out; |
c2cc9f0a | 2404 | } |
a871ff6b | 2405 | |
c2cc9f0a | 2406 | *dot = '\0'; |
2407 | value = dot + 1; | |
2408 | ||
b2718c72 | 2409 | key = line; |
2410 | key[lxc_char_right_gc(key, strlen(key))] = '\0'; | |
c2cc9f0a | 2411 | |
b2718c72 | 2412 | value += lxc_char_left_gc(value, strlen(value)); |
2413 | value[lxc_char_right_gc(value, strlen(value))] = '\0'; | |
c2cc9f0a | 2414 | |
bd878dee SB |
2415 | if (*value == '\'' || *value == '\"') { |
2416 | size_t len = strlen(value); | |
2417 | if (len > 1 && value[len-1] == *value) { | |
2418 | value[len-1] = '\0'; | |
2419 | value++; | |
2420 | } | |
2421 | } | |
2422 | ||
72d0e1cb | 2423 | config = lxc_getconfig(key); |
c2cc9f0a | 2424 | if (!config) { |
6e1d9b94 | 2425 | ERROR("unknown key %s", key); |
91480a0f | 2426 | goto out; |
c2cc9f0a | 2427 | } |
2428 | ||
d37f7cd7 | 2429 | ret = config->set(key, value, plc->conf); |
91480a0f DL |
2430 | |
2431 | out: | |
81192358 | 2432 | free(linep); |
91480a0f | 2433 | return ret; |
c2cc9f0a | 2434 | } |
2435 | ||
74a3920a | 2436 | static int lxc_config_readline(char *buffer, struct lxc_conf *conf) |
af5b0155 | 2437 | { |
6b0d5538 SH |
2438 | struct parse_line_conf c; |
2439 | ||
2440 | c.conf = conf; | |
2441 | c.from_include = false; | |
2442 | ||
2443 | return parse_line(buffer, &c); | |
af5b0155 CLG |
2444 | } |
2445 | ||
6b0d5538 | 2446 | int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include) |
c2cc9f0a | 2447 | { |
6b0d5538 SH |
2448 | struct parse_line_conf c; |
2449 | ||
2450 | c.conf = conf; | |
2451 | c.from_include = from_include; | |
f979ac15 | 2452 | |
f3ca99fd SH |
2453 | if( access(file, R_OK) == -1 ) { |
2454 | return -1; | |
2455 | } | |
6b0d5538 | 2456 | |
f7bee6c6 | 2457 | /* Catch only the top level config file name in the structure */ |
76d0127f CB |
2458 | if(!conf->rcfile) |
2459 | conf->rcfile = strdup(file); | |
f979ac15 | 2460 | |
6b0d5538 | 2461 | return lxc_file_for_each_line(file, parse_line, &c); |
c2cc9f0a | 2462 | } |
62e46035 CLG |
2463 | |
2464 | int lxc_config_define_add(struct lxc_list *defines, char* arg) | |
2465 | { | |
2466 | struct lxc_list *dent; | |
2467 | ||
2468 | dent = malloc(sizeof(struct lxc_list)); | |
2469 | if (!dent) | |
2470 | return -1; | |
2471 | ||
2472 | dent->elem = arg; | |
2473 | lxc_list_add_tail(defines, dent); | |
2474 | return 0; | |
2475 | } | |
2476 | ||
226a18d6 | 2477 | int lxc_config_define_load(struct lxc_list *defines, struct lxc_conf *conf) |
62e46035 | 2478 | { |
9ebb03ad | 2479 | struct lxc_list *it,*next; |
62e46035 CLG |
2480 | int ret = 0; |
2481 | ||
2482 | lxc_list_for_each(it, defines) { | |
2483 | ret = lxc_config_readline(it->elem, conf); | |
2484 | if (ret) | |
2485 | break; | |
2486 | } | |
2487 | ||
9ebb03ad | 2488 | lxc_list_for_each_safe(it, defines, next) { |
62e46035 CLG |
2489 | lxc_list_del(it); |
2490 | free(it); | |
2491 | } | |
2492 | ||
2493 | return ret; | |
2494 | } | |
525f0002 CS |
2495 | |
2496 | signed long lxc_config_parse_arch(const char *arch) | |
2497 | { | |
6ff05e18 | 2498 | #if HAVE_SYS_PERSONALITY_H |
525f0002 CS |
2499 | struct per_name { |
2500 | char *name; | |
2501 | unsigned long per; | |
bb8d8207 | 2502 | } pername[] = { |
525f0002 | 2503 | { "x86", PER_LINUX32 }, |
bb8d8207 DE |
2504 | { "linux32", PER_LINUX32 }, |
2505 | { "i386", PER_LINUX32 }, | |
2506 | { "i486", PER_LINUX32 }, | |
2507 | { "i586", PER_LINUX32 }, | |
525f0002 | 2508 | { "i686", PER_LINUX32 }, |
bb8d8207 | 2509 | { "athlon", PER_LINUX32 }, |
e319eb34 JC |
2510 | { "mips", PER_LINUX32 }, |
2511 | { "mipsel", PER_LINUX32 }, | |
08245e8c CB |
2512 | { "ppc", PER_LINUX32 }, |
2513 | { "arm", PER_LINUX32 }, | |
2514 | { "armv7l", PER_LINUX32 }, | |
2515 | { "armhf", PER_LINUX32 }, | |
2516 | { "armel", PER_LINUX32 }, | |
2517 | { "powerpc", PER_LINUX32 }, | |
bb8d8207 | 2518 | { "linux64", PER_LINUX }, |
525f0002 CS |
2519 | { "x86_64", PER_LINUX }, |
2520 | { "amd64", PER_LINUX }, | |
e319eb34 JC |
2521 | { "mips64", PER_LINUX }, |
2522 | { "mips64el", PER_LINUX }, | |
08245e8c CB |
2523 | { "ppc64", PER_LINUX }, |
2524 | { "ppc64le", PER_LINUX }, | |
2525 | { "ppc64el", PER_LINUX }, | |
2526 | { "powerpc64", PER_LINUX }, | |
2527 | { "s390x", PER_LINUX }, | |
2528 | { "aarch64", PER_LINUX }, | |
2529 | { "arm64", PER_LINUX }, | |
525f0002 CS |
2530 | }; |
2531 | size_t len = sizeof(pername) / sizeof(pername[0]); | |
2532 | ||
84760c11 | 2533 | size_t i; |
525f0002 CS |
2534 | |
2535 | for (i = 0; i < len; i++) { | |
2536 | if (!strcmp(pername[i].name, arch)) | |
2537 | return pername[i].per; | |
2538 | } | |
6ff05e18 | 2539 | #endif |
525f0002 CS |
2540 | |
2541 | return -1; | |
2542 | } | |
72d0e1cb | 2543 | |
4d69b293 NK |
2544 | int lxc_fill_elevated_privileges(char *flaglist, int *flags) |
2545 | { | |
2546 | char *token, *saveptr = NULL; | |
2547 | int i, aflag; | |
2548 | struct { const char *token; int flag; } all_privs[] = { | |
2549 | { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP }, | |
2550 | { "CAP", LXC_ATTACH_DROP_CAPABILITIES }, | |
2551 | { "LSM", LXC_ATTACH_LSM_EXEC }, | |
2552 | { NULL, 0 } | |
2553 | }; | |
2554 | ||
2555 | if (!flaglist) { | |
2556 | /* for the sake of backward compatibility, drop all privileges | |
2557 | if none is specified */ | |
2558 | for (i = 0; all_privs[i].token; i++) { | |
d028235d | 2559 | *flags |= all_privs[i].flag; |
4d69b293 NK |
2560 | } |
2561 | return 0; | |
2562 | } | |
2563 | ||
2564 | token = strtok_r(flaglist, "|", &saveptr); | |
2565 | while (token) { | |
2566 | aflag = -1; | |
2567 | for (i = 0; all_privs[i].token; i++) { | |
2568 | if (!strcmp(all_privs[i].token, token)) | |
2569 | aflag = all_privs[i].flag; | |
2570 | } | |
2571 | if (aflag < 0) | |
2572 | return -1; | |
2573 | ||
2574 | *flags |= aflag; | |
2575 | ||
2576 | token = strtok_r(NULL, "|", &saveptr); | |
2577 | } | |
2578 | return 0; | |
2579 | } | |
2580 | ||
bdf91ab4 CB |
2581 | static inline int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, |
2582 | int v) | |
72d0e1cb SG |
2583 | { |
2584 | if (!retv) | |
2585 | inlen = 0; | |
2586 | else | |
2587 | memset(retv, 0, inlen); | |
bdf91ab4 | 2588 | |
72d0e1cb SG |
2589 | return snprintf(retv, inlen, "%d", v); |
2590 | } | |
2591 | ||
72d0e1cb SG |
2592 | /* |
2593 | * If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, | |
2594 | * then just the value(s) will be printed. Since there still could be | |
2595 | * more than one, it is newline-separated. | |
2596 | * (Maybe that's ambigous, since some values, i.e. devices.list, will | |
2597 | * already have newlines?) | |
2598 | * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, | |
2599 | * in 'lxc.cgroup.subsystem.key = value' format. | |
2600 | */ | |
12a50cc6 DE |
2601 | static int lxc_get_cgroup_entry(struct lxc_conf *c, char *retv, int inlen, |
2602 | const char *key) | |
72d0e1cb SG |
2603 | { |
2604 | int fulllen = 0, len; | |
2605 | int all = 0; | |
2606 | struct lxc_list *it; | |
2607 | ||
2608 | if (!retv) | |
2609 | inlen = 0; | |
2610 | else | |
2611 | memset(retv, 0, inlen); | |
2612 | ||
2613 | if (strcmp(key, "all") == 0) | |
2614 | all = 1; | |
2615 | ||
2616 | lxc_list_for_each(it, &c->cgroup) { | |
2617 | struct lxc_cgroup *cg = it->elem; | |
2618 | if (all) { | |
2619 | strprint(retv, inlen, "lxc.cgroup.%s = %s\n", cg->subsystem, cg->value); | |
2620 | } else if (strcmp(cg->subsystem, key) == 0) { | |
2621 | strprint(retv, inlen, "%s\n", cg->value); | |
2622 | } | |
2623 | } | |
2624 | return fulllen; | |
2625 | } | |
2626 | ||
c6d09e15 WB |
2627 | /* |
2628 | * If you ask for a specific value, i.e. lxc.limit.nofile, then just the value | |
2629 | * will be printed. If you ask for 'lxc.limit', then all limit entries will be | |
2630 | * printed, in 'lxc.limit.resource = value' format. | |
2631 | */ | |
2632 | static int lxc_get_limit_entry(struct lxc_conf *c, char *retv, int inlen, | |
2633 | const char *key) | |
2634 | { | |
2635 | int fulllen = 0, len; | |
2636 | int all = 0; | |
2637 | struct lxc_list *it; | |
2638 | ||
2639 | if (!retv) | |
2640 | inlen = 0; | |
2641 | else | |
2642 | memset(retv, 0, inlen); | |
2643 | ||
2644 | if (strcmp(key, "all") == 0) | |
2645 | all = 1; | |
2646 | ||
2647 | lxc_list_for_each(it, &c->limits) { | |
2648 | char buf[LXC_NUMSTRLEN64*2+2]; /* 2 colon separated 64 bit integers or the word 'unlimited' */ | |
2649 | int partlen; | |
2650 | struct lxc_limit *lim = it->elem; | |
2651 | ||
2652 | if (lim->limit.rlim_cur == RLIM_INFINITY) { | |
2653 | memcpy(buf, "unlimited", sizeof("unlimited")); | |
2654 | partlen = sizeof("unlimited")-1; | |
2655 | } else { | |
2e6e3feb | 2656 | partlen = sprintf(buf, "%"PRIu64, (uint64_t)lim->limit.rlim_cur); |
c6d09e15 WB |
2657 | } |
2658 | if (lim->limit.rlim_cur != lim->limit.rlim_max) { | |
2659 | if (lim->limit.rlim_max == RLIM_INFINITY) { | |
2660 | memcpy(buf+partlen, ":unlimited", sizeof(":unlimited")); | |
2661 | } else { | |
2e6e3feb | 2662 | sprintf(buf+partlen, ":%"PRIu64, (uint64_t)lim->limit.rlim_max); |
c6d09e15 WB |
2663 | } |
2664 | } | |
2665 | ||
2666 | if (all) { | |
2667 | strprint(retv, inlen, "lxc.limit.%s = %s\n", lim->resource, buf); | |
2668 | } else if (strcmp(lim->resource, key) == 0) { | |
2669 | strprint(retv, inlen, "%s", buf); | |
2670 | } | |
2671 | } | |
2672 | ||
2673 | return fulllen; | |
2674 | } | |
2675 | ||
12a50cc6 DE |
2676 | static int lxc_get_item_hooks(struct lxc_conf *c, char *retv, int inlen, |
2677 | const char *key) | |
72d0e1cb SG |
2678 | { |
2679 | char *subkey; | |
2680 | int len, fulllen = 0, found = -1; | |
2681 | struct lxc_list *it; | |
2682 | int i; | |
2683 | ||
2684 | /* "lxc.hook.mount" */ | |
46cd2845 PL |
2685 | subkey = strchr(key, '.'); |
2686 | if (subkey) subkey = strchr(subkey+1, '.'); | |
72d0e1cb SG |
2687 | if (!subkey) |
2688 | return -1; | |
2689 | subkey++; | |
2690 | if (!*subkey) | |
2691 | return -1; | |
2692 | for (i=0; i<NUM_LXC_HOOKS; i++) { | |
2693 | if (strcmp(lxchook_names[i], subkey) == 0) { | |
2694 | found=i; | |
2695 | break; | |
2696 | } | |
2697 | } | |
2698 | if (found == -1) | |
2699 | return -1; | |
2700 | ||
2701 | if (!retv) | |
2702 | inlen = 0; | |
2703 | else | |
2704 | memset(retv, 0, inlen); | |
2705 | ||
2706 | lxc_list_for_each(it, &c->hooks[found]) { | |
2707 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2708 | } | |
2709 | return fulllen; | |
2710 | } | |
2711 | ||
ee1e7aa0 SG |
2712 | static int lxc_get_item_groups(struct lxc_conf *c, char *retv, int inlen) |
2713 | { | |
2714 | int len, fulllen = 0; | |
2715 | struct lxc_list *it; | |
2716 | ||
2717 | if (!retv) | |
2718 | inlen = 0; | |
2719 | else | |
2720 | memset(retv, 0, inlen); | |
2721 | ||
2722 | lxc_list_for_each(it, &c->groups) { | |
2723 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2724 | } | |
2725 | return fulllen; | |
2726 | } | |
2727 | ||
ab799c0b SG |
2728 | static int lxc_get_item_environment(struct lxc_conf *c, char *retv, int inlen) |
2729 | { | |
2730 | int len, fulllen = 0; | |
2731 | struct lxc_list *it; | |
2732 | ||
2733 | if (!retv) | |
2734 | inlen = 0; | |
2735 | else | |
2736 | memset(retv, 0, inlen); | |
2737 | ||
2738 | lxc_list_for_each(it, &c->environment) { | |
2739 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2740 | } | |
2741 | return fulllen; | |
2742 | } | |
2743 | ||
72d0e1cb SG |
2744 | static int lxc_get_item_cap_drop(struct lxc_conf *c, char *retv, int inlen) |
2745 | { | |
2746 | int len, fulllen = 0; | |
2747 | struct lxc_list *it; | |
2748 | ||
2749 | if (!retv) | |
2750 | inlen = 0; | |
2751 | else | |
2752 | memset(retv, 0, inlen); | |
2753 | ||
2754 | lxc_list_for_each(it, &c->caps) { | |
2755 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2756 | } | |
2757 | return fulllen; | |
2758 | } | |
2759 | ||
1fb86a7c SH |
2760 | static int lxc_get_item_cap_keep(struct lxc_conf *c, char *retv, int inlen) |
2761 | { | |
2762 | int len, fulllen = 0; | |
2763 | struct lxc_list *it; | |
2764 | ||
2765 | if (!retv) | |
2766 | inlen = 0; | |
2767 | else | |
2768 | memset(retv, 0, inlen); | |
2769 | ||
2770 | lxc_list_for_each(it, &c->keepcaps) { | |
2771 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2772 | } | |
2773 | return fulllen; | |
2774 | } | |
2775 | ||
72d0e1cb SG |
2776 | static int lxc_get_mount_entries(struct lxc_conf *c, char *retv, int inlen) |
2777 | { | |
2778 | int len, fulllen = 0; | |
2779 | struct lxc_list *it; | |
2780 | ||
2781 | if (!retv) | |
2782 | inlen = 0; | |
2783 | else | |
2784 | memset(retv, 0, inlen); | |
2785 | ||
2786 | lxc_list_for_each(it, &c->mount_list) { | |
2787 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2788 | } | |
2789 | return fulllen; | |
2790 | } | |
2791 | ||
b099e9e9 SH |
2792 | static int lxc_get_auto_mounts(struct lxc_conf *c, char *retv, int inlen) |
2793 | { | |
2794 | int len, fulllen = 0; | |
0769b82a | 2795 | const char *sep = ""; |
b099e9e9 SH |
2796 | |
2797 | if (!retv) | |
2798 | inlen = 0; | |
2799 | else | |
2800 | memset(retv, 0, inlen); | |
2801 | ||
2802 | if (!(c->auto_mounts & LXC_AUTO_ALL_MASK)) | |
2803 | return 0; | |
2804 | ||
2805 | switch (c->auto_mounts & LXC_AUTO_PROC_MASK) { | |
0769b82a CS |
2806 | case LXC_AUTO_PROC_MIXED: strprint(retv, inlen, "%sproc:mixed", sep); sep = " "; break; |
2807 | case LXC_AUTO_PROC_RW: strprint(retv, inlen, "%sproc:rw", sep); sep = " "; break; | |
b099e9e9 SH |
2808 | default: break; |
2809 | } | |
2810 | switch (c->auto_mounts & LXC_AUTO_SYS_MASK) { | |
0769b82a CS |
2811 | case LXC_AUTO_SYS_RO: strprint(retv, inlen, "%ssys:ro", sep); sep = " "; break; |
2812 | case LXC_AUTO_SYS_RW: strprint(retv, inlen, "%ssys:rw", sep); sep = " "; break; | |
10f27710 | 2813 | case LXC_AUTO_SYS_MIXED: strprint(retv, inlen, "%ssys:mixed", sep); sep = " "; break; |
b099e9e9 SH |
2814 | default: break; |
2815 | } | |
2816 | switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) { | |
0769b82a CS |
2817 | case LXC_AUTO_CGROUP_NOSPEC: strprint(retv, inlen, "%scgroup", sep); sep = " "; break; |
2818 | case LXC_AUTO_CGROUP_MIXED: strprint(retv, inlen, "%scgroup:mixed", sep); sep = " "; break; | |
2819 | case LXC_AUTO_CGROUP_RO: strprint(retv, inlen, "%scgroup:ro", sep); sep = " "; break; | |
2820 | case LXC_AUTO_CGROUP_RW: strprint(retv, inlen, "%scgroup:rw", sep); sep = " "; break; | |
2821 | case LXC_AUTO_CGROUP_FULL_NOSPEC: strprint(retv, inlen, "%scgroup-full", sep); sep = " "; break; | |
2822 | case LXC_AUTO_CGROUP_FULL_MIXED: strprint(retv, inlen, "%scgroup-full:mixed", sep); sep = " "; break; | |
2823 | case LXC_AUTO_CGROUP_FULL_RO: strprint(retv, inlen, "%scgroup-full:ro", sep); sep = " "; break; | |
2824 | case LXC_AUTO_CGROUP_FULL_RW: strprint(retv, inlen, "%scgroup-full:rw", sep); sep = " "; break; | |
b099e9e9 SH |
2825 | default: break; |
2826 | } | |
0769b82a | 2827 | |
b099e9e9 SH |
2828 | return fulllen; |
2829 | } | |
2830 | ||
72d0e1cb SG |
2831 | /* |
2832 | * lxc.network.0.XXX, where XXX can be: name, type, link, flags, type, | |
8fc8295a | 2833 | * macvlan.mode, veth.pair, vlan, ipv4, ipv6, script.up, hwaddr, mtu, |
9eaf8a59 | 2834 | * ipv4.gateway, ipv6.gateway. ipvX.gateway can return 'auto' instead |
72d0e1cb SG |
2835 | * of an address. ipv4 and ipv6 return lists (newline-separated). |
2836 | * things like veth.pair return '' if invalid (i.e. if called for vlan | |
2837 | * type). | |
2838 | */ | |
12a50cc6 DE |
2839 | static int lxc_get_item_nic(struct lxc_conf *c, char *retv, int inlen, |
2840 | const char *key) | |
72d0e1cb SG |
2841 | { |
2842 | char *p1; | |
fe88b9d2 | 2843 | int len, fulllen = 0; |
72d0e1cb SG |
2844 | struct lxc_netdev *netdev; |
2845 | ||
2846 | if (!retv) | |
2847 | inlen = 0; | |
2848 | else | |
2849 | memset(retv, 0, inlen); | |
2850 | ||
46cd2845 | 2851 | p1 = strchr(key, '.'); |
72d0e1cb SG |
2852 | if (!p1 || *(p1+1) == '\0') return -1; |
2853 | p1++; | |
2854 | ||
2855 | netdev = get_netdev_from_key(key, &c->network); | |
2856 | if (!netdev) | |
2857 | return -1; | |
2858 | if (strcmp(p1, "name") == 0) { | |
2859 | if (netdev->name) | |
2860 | strprint(retv, inlen, "%s", netdev->name); | |
2861 | } else if (strcmp(p1, "type") == 0) { | |
2862 | strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type)); | |
2863 | } else if (strcmp(p1, "link") == 0) { | |
2864 | if (netdev->link) | |
2865 | strprint(retv, inlen, "%s", netdev->link); | |
2866 | } else if (strcmp(p1, "flags") == 0) { | |
2867 | if (netdev->flags & IFF_UP) | |
2868 | strprint(retv, inlen, "up"); | |
8fc8295a | 2869 | } else if (strcmp(p1, "script.up") == 0) { |
72d0e1cb SG |
2870 | if (netdev->upscript) |
2871 | strprint(retv, inlen, "%s", netdev->upscript); | |
8fc8295a DE |
2872 | } else if (strcmp(p1, "script.down") == 0) { |
2873 | if (netdev->downscript) | |
2874 | strprint(retv, inlen, "%s", netdev->downscript); | |
72d0e1cb SG |
2875 | } else if (strcmp(p1, "hwaddr") == 0) { |
2876 | if (netdev->hwaddr) | |
2877 | strprint(retv, inlen, "%s", netdev->hwaddr); | |
2878 | } else if (strcmp(p1, "mtu") == 0) { | |
2879 | if (netdev->mtu) | |
2880 | strprint(retv, inlen, "%s", netdev->mtu); | |
2881 | } else if (strcmp(p1, "macvlan.mode") == 0) { | |
2882 | if (netdev->type == LXC_NET_MACVLAN) { | |
2883 | const char *mode; | |
2884 | switch (netdev->priv.macvlan_attr.mode) { | |
2885 | case MACVLAN_MODE_PRIVATE: mode = "private"; break; | |
2886 | case MACVLAN_MODE_VEPA: mode = "vepa"; break; | |
2887 | case MACVLAN_MODE_BRIDGE: mode = "bridge"; break; | |
99854161 | 2888 | case MACVLAN_MODE_PASSTHRU: mode = "passthru"; break; |
72d0e1cb SG |
2889 | default: mode = "(invalid)"; break; |
2890 | } | |
2891 | strprint(retv, inlen, "%s", mode); | |
2892 | } | |
2893 | } else if (strcmp(p1, "veth.pair") == 0) { | |
11029c02 DE |
2894 | if (netdev->type == LXC_NET_VETH) { |
2895 | strprint(retv, inlen, "%s", | |
2896 | netdev->priv.veth_attr.pair ? | |
2897 | netdev->priv.veth_attr.pair : | |
2898 | netdev->priv.veth_attr.veth1); | |
2899 | } | |
72d0e1cb SG |
2900 | } else if (strcmp(p1, "vlan") == 0) { |
2901 | if (netdev->type == LXC_NET_VLAN) { | |
2902 | strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid); | |
2903 | } | |
9eaf8a59 | 2904 | } else if (strcmp(p1, "ipv4.gateway") == 0) { |
72d0e1cb SG |
2905 | if (netdev->ipv4_gateway_auto) { |
2906 | strprint(retv, inlen, "auto"); | |
2907 | } else if (netdev->ipv4_gateway) { | |
2908 | char buf[INET_ADDRSTRLEN]; | |
2909 | inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf)); | |
2910 | strprint(retv, inlen, "%s", buf); | |
2911 | } | |
2912 | } else if (strcmp(p1, "ipv4") == 0) { | |
2913 | struct lxc_list *it2; | |
2914 | lxc_list_for_each(it2, &netdev->ipv4) { | |
2915 | struct lxc_inetdev *i = it2->elem; | |
2916 | char buf[INET_ADDRSTRLEN]; | |
2917 | inet_ntop(AF_INET, &i->addr, buf, sizeof(buf)); | |
28417b5a | 2918 | strprint(retv, inlen, "%s/%d\n", buf, i->prefix); |
72d0e1cb | 2919 | } |
9eaf8a59 | 2920 | } else if (strcmp(p1, "ipv6.gateway") == 0) { |
72d0e1cb SG |
2921 | if (netdev->ipv6_gateway_auto) { |
2922 | strprint(retv, inlen, "auto"); | |
2923 | } else if (netdev->ipv6_gateway) { | |
6afb165d AN |
2924 | char buf[INET6_ADDRSTRLEN]; |
2925 | inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf)); | |
72d0e1cb SG |
2926 | strprint(retv, inlen, "%s", buf); |
2927 | } | |
2928 | } else if (strcmp(p1, "ipv6") == 0) { | |
2929 | struct lxc_list *it2; | |
2930 | lxc_list_for_each(it2, &netdev->ipv6) { | |
6afb165d AN |
2931 | struct lxc_inet6dev *i = it2->elem; |
2932 | char buf[INET6_ADDRSTRLEN]; | |
72d0e1cb | 2933 | inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf)); |
28417b5a | 2934 | strprint(retv, inlen, "%s/%d\n", buf, i->prefix); |
72d0e1cb SG |
2935 | } |
2936 | } | |
2937 | return fulllen; | |
2938 | } | |
2939 | ||
2940 | static int lxc_get_item_network(struct lxc_conf *c, char *retv, int inlen) | |
2941 | { | |
2942 | int len, fulllen = 0; | |
2943 | struct lxc_list *it; | |
2944 | ||
2945 | if (!retv) | |
2946 | inlen = 0; | |
2947 | else | |
2948 | memset(retv, 0, inlen); | |
2949 | ||
2950 | lxc_list_for_each(it, &c->network) { | |
2951 | struct lxc_netdev *n = it->elem; | |
2952 | const char *t = lxc_net_type_to_str(n->type); | |
2953 | strprint(retv, inlen, "%s\n", t ? t : "(invalid)"); | |
2954 | } | |
2955 | return fulllen; | |
2956 | } | |
2957 | ||
0df888ba CB |
2958 | static int lxc_get_idmaps(struct lxc_conf *c, char *retv, int inlen) |
2959 | { | |
2960 | struct lxc_list *it; | |
2961 | int len, listlen, ret; | |
2962 | int fulllen = 0; | |
2963 | /* "u 1000 1000000 65536" | |
2964 | * | |
2965 | * let's render this as | |
2966 | * | |
2967 | * sizeof(char) | |
2968 | * + | |
2969 | * sizeof(" ") | |
2970 | * + | |
2971 | * sizeof(uint64_t) | |
2972 | * + | |
2973 | * sizeof(" ") | |
2974 | * + | |
2975 | * sizeof(uint64_t) | |
2976 | * + | |
2977 | * sizeof(" ") | |
2978 | * + | |
2979 | * sizeof(uint64_t) | |
2980 | * + | |
2981 | * \0 | |
2982 | */ | |
2983 | #define __LXC_IDMAP_STR_BUF (3 * LXC_NUMSTRLEN64 + 3 + 1 + 1) | |
2984 | char buf[__LXC_IDMAP_STR_BUF]; | |
2985 | ||
2986 | if (!retv) | |
2987 | inlen = 0; | |
2988 | else | |
2989 | memset(retv, 0, inlen); | |
2990 | ||
2991 | listlen = lxc_list_len(&c->id_map); | |
2992 | lxc_list_for_each(it, &c->id_map) | |
2993 | { | |
2994 | struct id_map *map = it->elem; | |
2995 | ret = snprintf(buf, __LXC_IDMAP_STR_BUF, "%c %lu %lu %lu", | |
2996 | (map->idtype == ID_TYPE_UID) ? 'u' : 'g', | |
2997 | map->nsid, map->hostid, map->range); | |
2998 | if (ret < 0 || ret >= __LXC_IDMAP_STR_BUF) | |
2999 | return -1; | |
3000 | ||
3001 | strprint(retv, inlen, "%s%s", buf, (listlen-- > 1) ? "\n" : ""); | |
3002 | } | |
3003 | return fulllen; | |
3004 | } | |
3005 | ||
12a50cc6 DE |
3006 | int lxc_get_config_item(struct lxc_conf *c, const char *key, char *retv, |
3007 | int inlen) | |
72d0e1cb | 3008 | { |
4a85ce2a | 3009 | const char *v = NULL; |
72d0e1cb SG |
3010 | |
3011 | if (strcmp(key, "lxc.mount.entry") == 0) | |
3012 | return lxc_get_mount_entries(c, retv, inlen); | |
b099e9e9 SH |
3013 | else if (strcmp(key, "lxc.mount.auto") == 0) |
3014 | return lxc_get_auto_mounts(c, retv, inlen); | |
72d0e1cb SG |
3015 | else if (strcmp(key, "lxc.mount") == 0) |
3016 | v = c->fstab; | |
72d0e1cb | 3017 | else if (strcmp(key, "lxc.aa_profile") == 0) |
fe4de9a6 | 3018 | v = c->lsm_aa_profile; |
7aff4f43 SH |
3019 | else if (strcmp(key, "lxc.aa_allow_incomplete") == 0) |
3020 | return lxc_get_conf_int(c, retv, inlen, c->lsm_aa_allow_incomplete); | |
fe4de9a6 DE |
3021 | else if (strcmp(key, "lxc.se_context") == 0) |
3022 | v = c->lsm_se_context; | |
4a85ce2a | 3023 | else if (strcmp(key, "lxc.logfile") == 0) |
858377e4 | 3024 | v = c->logfile; |
4a85ce2a | 3025 | else if (strcmp(key, "lxc.loglevel") == 0) |
858377e4 | 3026 | v = lxc_log_priority_to_string(c->loglevel); |
72d0e1cb SG |
3027 | else if (strcmp(key, "lxc.cgroup") == 0) // all cgroup info |
3028 | return lxc_get_cgroup_entry(c, retv, inlen, "all"); | |
3029 | else if (strncmp(key, "lxc.cgroup.", 11) == 0) // specific cgroup info | |
3030 | return lxc_get_cgroup_entry(c, retv, inlen, key + 11); | |
3031 | else if (strcmp(key, "lxc.utsname") == 0) | |
64fca455 | 3032 | v = c->utsname ? c->utsname->nodename : NULL; |
96f15ca1 SH |
3033 | else if (strcmp(key, "lxc.console.logfile") == 0) |
3034 | v = c->console.log_path; | |
72d0e1cb SG |
3035 | else if (strcmp(key, "lxc.console") == 0) |
3036 | v = c->console.path; | |
3037 | else if (strcmp(key, "lxc.rootfs.mount") == 0) | |
3038 | v = c->rootfs.mount; | |
327a1e78 | 3039 | else if (strcmp(key, "lxc.rootfs.backend") == 0) |
6a21d4ae | 3040 | v = c->rootfs.bdev_type; |
a17b1e65 SG |
3041 | else if (strcmp(key, "lxc.rootfs.options") == 0) |
3042 | v = c->rootfs.options; | |
72d0e1cb SG |
3043 | else if (strcmp(key, "lxc.rootfs") == 0) |
3044 | v = c->rootfs.path; | |
72d0e1cb SG |
3045 | else if (strcmp(key, "lxc.cap.drop") == 0) |
3046 | return lxc_get_item_cap_drop(c, retv, inlen); | |
1fb86a7c SH |
3047 | else if (strcmp(key, "lxc.cap.keep") == 0) |
3048 | return lxc_get_item_cap_keep(c, retv, inlen); | |
72d0e1cb SG |
3049 | else if (strncmp(key, "lxc.hook", 8) == 0) |
3050 | return lxc_get_item_hooks(c, retv, inlen, key); | |
3051 | else if (strcmp(key, "lxc.network") == 0) | |
3052 | return lxc_get_item_network(c, retv, inlen); | |
3053 | else if (strncmp(key, "lxc.network.", 12) == 0) | |
3054 | return lxc_get_item_nic(c, retv, inlen, key + 12); | |
ee1e7aa0 SG |
3055 | else if (strcmp(key, "lxc.start.auto") == 0) |
3056 | return lxc_get_conf_int(c, retv, inlen, c->start_auto); | |
3057 | else if (strcmp(key, "lxc.start.delay") == 0) | |
3058 | return lxc_get_conf_int(c, retv, inlen, c->start_delay); | |
3059 | else if (strcmp(key, "lxc.start.order") == 0) | |
3060 | return lxc_get_conf_int(c, retv, inlen, c->start_order); | |
a8dfe4e0 WB |
3061 | else if (strcmp(key, "lxc.monitor.unshare") == 0) |
3062 | return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare); | |
ee1e7aa0 SG |
3063 | else if (strcmp(key, "lxc.group") == 0) |
3064 | return lxc_get_item_groups(c, retv, inlen); | |
58e0f57d SH |
3065 | else if (strcmp(key, "lxc.seccomp") == 0) |
3066 | v = c->seccomp; | |
ab799c0b SG |
3067 | else if (strcmp(key, "lxc.environment") == 0) |
3068 | return lxc_get_item_environment(c, retv, inlen); | |
67c660d0 SG |
3069 | else if (strcmp(key, "lxc.init_cmd") == 0) |
3070 | v = c->init_cmd; | |
68d18db8 PT |
3071 | else if (strcmp(key, "lxc.init_uid") == 0) |
3072 | return lxc_get_conf_int(c, retv, inlen, c->init_uid); | |
3073 | else if (strcmp(key, "lxc.init_gid") == 0) | |
3074 | return lxc_get_conf_int(c, retv, inlen, c->init_gid); | |
8796becf CB |
3075 | else if (strcmp(key, "lxc.ephemeral") == 0) |
3076 | return lxc_get_conf_int(c, retv, inlen, c->ephemeral); | |
76d0127f CB |
3077 | else if (strcmp(key, "lxc.syslog") == 0) |
3078 | v = c->syslog; | |
5a46f283 CB |
3079 | else if (strcmp(key, "lxc.no_new_privs") == 0) |
3080 | return lxc_get_conf_int(c, retv, inlen, c->no_new_privs); | |
c6d09e15 WB |
3081 | else if (strcmp(key, "lxc.limit") == 0) // all limits |
3082 | return lxc_get_limit_entry(c, retv, inlen, "all"); | |
3083 | else if (strncmp(key, "lxc.limit.", 10) == 0) // specific limit | |
3084 | return lxc_get_limit_entry(c, retv, inlen, key + 10); | |
0df888ba CB |
3085 | else if (strcmp(key, "lxc.id_map") == 0) |
3086 | return lxc_get_idmaps(c, retv, inlen); | |
8beda89e CB |
3087 | else if (strcmp(key, "lxc.haltsignal") == 0) |
3088 | return lxc_get_conf_int(c, retv, inlen, c->haltsignal); | |
bbb8adb1 CB |
3089 | else if (strcmp(key, "lxc.rebootsignal") == 0) |
3090 | return lxc_get_conf_int(c, retv, inlen, c->rebootsignal); | |
d577bad1 CB |
3091 | else if (strcmp(key, "lxc.stopsignal") == 0) |
3092 | return lxc_get_conf_int(c, retv, inlen, c->stopsignal); | |
1a4c89ac CB |
3093 | else if (strcmp(key, "lxc.autodev") == 0) |
3094 | return lxc_get_conf_int(c, retv, inlen, c->autodev); | |
72d0e1cb SG |
3095 | else return -1; |
3096 | ||
3097 | if (!v) | |
3098 | return 0; | |
3099 | if (retv && inlen >= strlen(v) + 1) | |
3100 | strncpy(retv, v, strlen(v)+1); | |
3101 | return strlen(v); | |
3102 | } | |
3103 | ||
12a50cc6 | 3104 | int lxc_clear_config_item(struct lxc_conf *c, const char *key) |
72d0e1cb | 3105 | { |
c7b15d1e CB |
3106 | int ret = 0; |
3107 | ||
3108 | if (strcmp(key, "lxc.network") == 0) { | |
3109 | ret = lxc_clear_config_network(c); | |
3110 | ||
3111 | } else if (strncmp(key, "lxc.network.", 12) == 0) { | |
3112 | ret = lxc_clear_nic(c, key + 12); | |
3113 | ||
3114 | } else if (strcmp(key, "lxc.cap.drop") == 0) { | |
3115 | ret = lxc_clear_config_caps(c); | |
3116 | ||
3117 | } else if (strcmp(key, "lxc.cap.keep") == 0) { | |
3118 | ret = lxc_clear_config_keepcaps(c); | |
3119 | ||
3120 | } else if (strncmp(key, "lxc.cgroup", 10) == 0) { | |
3121 | ret = lxc_clear_cgroups(c, key); | |
3122 | ||
3123 | } else if (strcmp(key, "lxc.mount.entry") == 0) { | |
3124 | ret = lxc_clear_mount_entries(c); | |
3125 | ||
3126 | } else if (strcmp(key, "lxc.mount.auto") == 0) { | |
3127 | ret = lxc_clear_automounts(c); | |
3128 | ||
3129 | } else if (strncmp(key, "lxc.hook", 8) == 0) { | |
3130 | ret = lxc_clear_hooks(c, key); | |
3131 | ||
3132 | } else if (strncmp(key, "lxc.group", 9) == 0) { | |
3133 | ret = lxc_clear_groups(c); | |
3134 | ||
3135 | } else if (strncmp(key, "lxc.environment", 15) == 0) { | |
3136 | ret = lxc_clear_environment(c); | |
3137 | ||
3138 | } else if (strncmp(key, "lxc.id_map", 10) == 0) { | |
3139 | ret = lxc_clear_idmaps(c); | |
3140 | ||
3141 | } else if (strncmp(key, "lxc.limit", 9) == 0) { | |
3142 | ret = lxc_clear_limits(c, key); | |
3143 | ||
3144 | } else if (strcmp(key, "lxc.utsname") == 0) { | |
3145 | free(c->utsname); | |
3146 | c->utsname = NULL; | |
3147 | ||
3148 | } else if (strcmp(key, "lxc.arch") == 0) { | |
3149 | c->personality = -1; | |
3150 | ||
3151 | } else if (strcmp(key, "lxc.haltsignal") == 0) { | |
3152 | c->haltsignal = 0; | |
3153 | ||
3154 | } else if (strcmp(key, "lxc.rebootsignal") == 0) { | |
3155 | c->rebootsignal = 0; | |
3156 | ||
3157 | } else if (strcmp(key, "lxc.stopsignal") == 0) { | |
3158 | c->stopsignal = 0; | |
3159 | ||
3160 | } else if (strcmp(key, "lxc.init_cmd") == 0) { | |
3161 | free(c->init_cmd); | |
3162 | c->init_cmd = NULL; | |
3163 | ||
3164 | } else if (strcmp(key, "lxc.init_uid") == 0) { | |
3165 | c->init_uid = 0; | |
3166 | ||
3167 | } else if (strcmp(key, "lxc.init_gid") == 0) { | |
3168 | c->init_gid = 0; | |
3169 | ||
3170 | } else if (strcmp(key, "lxc.ephemeral") == 0) { | |
3171 | c->ephemeral = 0; | |
3172 | ||
3173 | } else if (strcmp(key, "lxc.console.logfile") == 0) { | |
3174 | free(c->console.log_path); | |
3175 | c->console.log_path = NULL; | |
3176 | ||
3177 | } else if (strcmp(key, "lxc.console") == 0) { | |
3178 | free(c->console.path); | |
3179 | c->console.path = NULL; | |
3180 | ||
3181 | } else if (strcmp(key, "lxc.tty") == 0) { | |
3182 | c->tty = 0; | |
3183 | ||
3184 | } else if (strcmp(key, "lxc.devttydir") == 0) { | |
3185 | free(c->ttydir); | |
3186 | c->ttydir = NULL; | |
3187 | ||
3188 | } else if (strcmp(key, "lxc.autodev") == 0) { | |
3189 | c->autodev = 1; | |
3190 | ||
3191 | } else if (strcmp(key, "lxc.kmsg") == 0) { | |
3192 | c->kmsg = 0; | |
3193 | ||
3194 | } else if (strcmp(key, "lxc.mount") == 0) { | |
3195 | free(c->fstab); | |
3196 | c->fstab = NULL; | |
3197 | ||
3198 | } else if (strcmp(key, "lxc.rootfs") == 0) { | |
3199 | free(c->rootfs.path); | |
3200 | c->rootfs.path = NULL; | |
3201 | ||
3202 | } else if (strcmp(key, "lxc.rootfs.mount") == 0) { | |
3203 | free(c->rootfs.mount); | |
3204 | c->rootfs.mount = NULL; | |
3205 | ||
3206 | } else if (strcmp(key, "lxc.rootfs.options") == 0) { | |
3207 | free(c->rootfs.options); | |
3208 | c->rootfs.options = NULL; | |
3209 | ||
3210 | } else if (strcmp(key, "lxc.rootfs.backend") == 0) { | |
3211 | free(c->rootfs.bdev_type); | |
3212 | c->rootfs.bdev_type = NULL; | |
3213 | ||
3214 | } else if (strcmp(key, "lxc.aa_profile") == 0) { | |
3215 | free(c->lsm_aa_profile); | |
3216 | c->lsm_aa_profile = NULL; | |
3217 | ||
3218 | } else if (strcmp(key, "lxc.aa_allow_incomplete") == 0) { | |
3219 | c->lsm_aa_allow_incomplete = 0; | |
3220 | ||
3221 | } else if (strcmp(key, "lxc.se_context") == 0) { | |
3222 | free(c->lsm_se_context); | |
3223 | c->lsm_se_context = NULL; | |
3224 | ||
3225 | } else if (strcmp(key, "lxc.seccomp") == 0) { | |
3226 | free(c->seccomp); | |
3227 | c->seccomp = NULL; | |
3228 | ||
3229 | } else if (strcmp(key, "lxc.loglevel") == 0) { | |
3230 | c->loglevel = LXC_LOG_PRIORITY_NOTSET; | |
3231 | ||
3232 | } else if (strcmp(key, "lxc.logfile") == 0) { | |
3233 | free(c->logfile); | |
3234 | c->logfile = NULL; | |
3235 | ||
3236 | } else if (strcmp(key, "lxc.monitor.unshare") == 0) { | |
3237 | c->monitor_unshare = 0; | |
3238 | ||
3239 | } else if (strcmp(key, "lxc.pts") == 0) { | |
3240 | c->pts = 0; | |
3241 | ||
3242 | } else if (strcmp(key, "lxc.include") == 0) { | |
3243 | lxc_clear_includes(c); | |
3244 | ||
3245 | } else if (strcmp(key, "lxc.syslog") == 0) { | |
3246 | free(c->syslog); | |
3247 | c->syslog = NULL; | |
3248 | } else { | |
3249 | ret = -1; | |
3250 | } | |
3251 | ||
3252 | return ret; | |
72d0e1cb SG |
3253 | } |
3254 | ||
3255 | /* | |
3256 | * writing out a confile. | |
3257 | */ | |
3258 | void write_config(FILE *fout, struct lxc_conf *c) | |
3259 | { | |
6b0d5538 SH |
3260 | size_t len = c->unexpanded_len; |
3261 | int ret; | |
72d0e1cb | 3262 | |
6b0d5538 SH |
3263 | if (!len) |
3264 | return; | |
3265 | ret = fwrite(c->unexpanded_config, 1, len, fout); | |
3266 | if (ret != len) | |
3267 | SYSERROR("Error writing configuration file"); | |
3268 | } | |
f979ac15 | 3269 | |
6b0d5538 SH |
3270 | bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key, const char *v) |
3271 | { | |
3272 | int ret; | |
151d2da2 CB |
3273 | size_t len; |
3274 | char *tmp; | |
4184c3e1 | 3275 | |
151d2da2 CB |
3276 | len = strlen(key) + strlen(v) + 4; |
3277 | tmp = alloca(len); | |
3278 | ||
3279 | if (config_value_empty(v)) | |
3280 | ret = snprintf(tmp, len, "%s =", key); | |
3281 | else | |
3282 | ret = snprintf(tmp, len, "%s = %s", key, v); | |
6b0d5538 SH |
3283 | if (ret < 0 || ret >= len) |
3284 | return false; | |
3285 | ||
3286 | /* Save the line verbatim into unexpanded_conf */ | |
3287 | if (append_unexp_config_line(tmp, conf)) | |
3288 | return false; | |
3289 | ||
3290 | return true; | |
3291 | } | |
3292 | ||
3293 | void clear_unexp_config_line(struct lxc_conf *conf, const char *key, bool rm_subkeys) | |
3294 | { | |
3295 | char *lstart = conf->unexpanded_config, *lend; | |
3296 | ||
3297 | if (!conf->unexpanded_config) | |
3298 | return; | |
3299 | while (*lstart) { | |
3300 | lend = strchr(lstart, '\n'); | |
3301 | char v; | |
3302 | if (!lend) | |
3303 | lend = lstart + strlen(lstart); | |
3304 | else | |
3305 | lend++; | |
3306 | if (strncmp(lstart, key, strlen(key)) != 0) { | |
3307 | lstart = lend; | |
3308 | continue; | |
5f62730e | 3309 | } |
6b0d5538 SH |
3310 | if (!rm_subkeys) { |
3311 | v = lstart[strlen(key)]; | |
3312 | if (!isspace(v) && v != '=') { | |
3313 | lstart = lend; | |
3314 | continue; | |
3315 | } | |
5f62730e | 3316 | } |
6b0d5538 SH |
3317 | conf->unexpanded_len -= (lend - lstart); |
3318 | if (*lend == '\0') { | |
3319 | *lstart = '\0'; | |
3320 | return; | |
fd986e08 | 3321 | } |
6b0d5538 | 3322 | memmove(lstart, lend, strlen(lend)+1); |
fd986e08 | 3323 | } |
6b0d5538 SH |
3324 | } |
3325 | ||
329b3625 CB |
3326 | bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath, |
3327 | const char *newpath, const char *oldname, | |
3328 | const char *newname, const char *ovldir) | |
3329 | { | |
3330 | const char *key = "lxc.mount.entry"; | |
3331 | int ret; | |
3332 | char *lstart = conf->unexpanded_config; | |
3333 | char *lend; | |
3334 | char *p; | |
3335 | char *q; | |
3336 | size_t newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2; | |
3337 | size_t olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2; | |
3338 | char *olddir = alloca(olddirlen + 1); | |
3339 | char *newdir = alloca(newdirlen + 1); | |
3340 | ||
3341 | ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath, oldname); | |
3342 | if (ret < 0 || ret >= olddirlen + 1) { | |
3343 | ERROR("Bug in %s", __func__); | |
3344 | return false; | |
3345 | } | |
3346 | ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath, newname); | |
3347 | if (ret < 0 || ret >= newdirlen + 1) { | |
3348 | ERROR("Bug in %s", __func__); | |
3349 | return false; | |
3350 | } | |
3351 | if (!conf->unexpanded_config) | |
3352 | return true; | |
3353 | while (*lstart) { | |
3354 | lend = strchr(lstart, '\n'); | |
3355 | if (!lend) | |
3356 | lend = lstart + strlen(lstart); | |
3357 | else | |
3358 | lend++; | |
3359 | if (strncmp(lstart, key, strlen(key)) != 0) | |
3360 | goto next; | |
3361 | p = strchr(lstart + strlen(key), '='); | |
3362 | if (!p) | |
3363 | goto next; | |
3364 | p++; | |
3365 | while (isblank(*p)) | |
3366 | p++; | |
3367 | if (p >= lend) | |
3368 | goto next; | |
3369 | /* Whenever an lxc.mount.entry entry is found in a line we check | |
3370 | * if the substring " overlay" or the substring " aufs" is | |
3371 | * present before doing any further work. We check for " | |
3372 | * overlay" and " aufs" since both substrings need to have at | |
3373 | * least one space before them in a valid overlay | |
3374 | * lxc.mount.entry (/A B overlay). When the space before is | |
3375 | * missing it is very likely that these substrings are part of a | |
3376 | * path or something else. (Checking q >= lend ensures that we | |
3377 | * only count matches in the current line.) */ | |
3378 | if ((!(q = strstr(p, " overlay")) || q >= lend) && (!(q = strstr(p, " aufs")) || q >= lend)) | |
3379 | goto next; | |
3380 | if (!(q = strstr(p, olddir)) || (q >= lend)) | |
3381 | goto next; | |
3382 | ||
3383 | /* replace the olddir with newdir */ | |
3384 | if (olddirlen >= newdirlen) { | |
3385 | size_t diff = olddirlen - newdirlen; | |
3386 | memcpy(q, newdir, newdirlen); | |
3387 | if (olddirlen != newdirlen) { | |
3388 | memmove(q + newdirlen, q + newdirlen + diff, | |
3389 | strlen(q) - newdirlen - diff + 1); | |
3390 | lend -= diff; | |
3391 | conf->unexpanded_len -= diff; | |
3392 | } | |
3393 | } else { | |
3394 | char *new; | |
3395 | size_t diff = newdirlen - olddirlen; | |
3396 | size_t oldlen = conf->unexpanded_len; | |
3397 | size_t newlen = oldlen + diff; | |
3398 | size_t poffset = q - conf->unexpanded_config; | |
3399 | new = realloc(conf->unexpanded_config, newlen + 1); | |
3400 | if (!new) { | |
3401 | ERROR("Out of memory"); | |
3402 | return false; | |
3403 | } | |
3404 | conf->unexpanded_len = newlen; | |
3405 | conf->unexpanded_alloced = newlen + 1; | |
3406 | new[newlen - 1] = '\0'; | |
3407 | lend = new + (lend - conf->unexpanded_config); | |
3408 | /* move over the remainder to make room for the newdir */ | |
3409 | memmove(new + poffset + newdirlen, | |
3410 | new + poffset + olddirlen, | |
3411 | oldlen - poffset - olddirlen + 1); | |
3412 | conf->unexpanded_config = new; | |
3413 | memcpy(new + poffset, newdir, newdirlen); | |
3414 | lend += diff; | |
3415 | } | |
3416 | next: | |
3417 | lstart = lend; | |
3418 | } | |
3419 | return true; | |
3420 | } | |
3421 | ||
67702c21 | 3422 | bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath, |
d546aa0e CB |
3423 | const char *newpath, const char *oldname, |
3424 | const char *newname) | |
6b0d5538 | 3425 | { |
67702c21 SH |
3426 | const char *key = "lxc.hook"; |
3427 | int ret; | |
3428 | char *lstart = conf->unexpanded_config, *lend, *p; | |
3429 | size_t newdirlen = strlen(newpath) + strlen(newname) + 1; | |
3430 | size_t olddirlen = strlen(oldpath) + strlen(oldname) + 1; | |
3431 | char *olddir = alloca(olddirlen + 1); | |
3432 | char *newdir = alloca(newdirlen + 1); | |
3433 | ||
d546aa0e CB |
3434 | ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname); |
3435 | if (ret < 0 || ret >= olddirlen + 1) { | |
67702c21 SH |
3436 | ERROR("Bug in %s", __func__); |
3437 | return false; | |
3438 | } | |
d546aa0e CB |
3439 | ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname); |
3440 | if (ret < 0 || ret >= newdirlen + 1) { | |
67702c21 SH |
3441 | ERROR("Bug in %s", __func__); |
3442 | return false; | |
3443 | } | |
3444 | if (!conf->unexpanded_config) | |
3445 | return true; | |
3446 | while (*lstart) { | |
3447 | lend = strchr(lstart, '\n'); | |
3448 | if (!lend) | |
3449 | lend = lstart + strlen(lstart); | |
3450 | else | |
3451 | lend++; | |
d546aa0e CB |
3452 | if (strncmp(lstart, key, strlen(key)) != 0) |
3453 | goto next; | |
3454 | p = strchr(lstart + strlen(key), '='); | |
3455 | if (!p) | |
3456 | goto next; | |
67702c21 SH |
3457 | p++; |
3458 | while (isblank(*p)) | |
3459 | p++; | |
d546aa0e CB |
3460 | if (p >= lend) |
3461 | goto next; | |
3462 | if (strncmp(p, olddir, strlen(olddir)) != 0) | |
3463 | goto next; | |
67702c21 SH |
3464 | /* replace the olddir with newdir */ |
3465 | if (olddirlen >= newdirlen) { | |
3466 | size_t diff = olddirlen - newdirlen; | |
3467 | memcpy(p, newdir, newdirlen); | |
3468 | if (olddirlen != newdirlen) { | |
d546aa0e CB |
3469 | memmove(p + newdirlen, p + newdirlen + diff, |
3470 | strlen(p) - newdirlen - diff + 1); | |
67702c21 SH |
3471 | lend -= diff; |
3472 | conf->unexpanded_len -= diff; | |
3473 | } | |
67702c21 SH |
3474 | } else { |
3475 | char *new; | |
3476 | size_t diff = newdirlen - olddirlen; | |
3477 | size_t oldlen = conf->unexpanded_len; | |
3478 | size_t newlen = oldlen + diff; | |
3479 | size_t poffset = p - conf->unexpanded_config; | |
d546aa0e | 3480 | new = realloc(conf->unexpanded_config, newlen + 1); |
67702c21 SH |
3481 | if (!new) { |
3482 | ERROR("Out of memory"); | |
6b0d5538 | 3483 | return false; |
67702c21 SH |
3484 | } |
3485 | conf->unexpanded_len = newlen; | |
d546aa0e CB |
3486 | conf->unexpanded_alloced = newlen + 1; |
3487 | new[newlen - 1] = '\0'; | |
67702c21 | 3488 | lend = new + (lend - conf->unexpanded_config); |
d546aa0e CB |
3489 | /* move over the remainder to make room for the newdir */ |
3490 | memmove(new + poffset + newdirlen, | |
3491 | new + poffset + olddirlen, | |
3492 | oldlen - poffset - olddirlen + 1); | |
67702c21 | 3493 | conf->unexpanded_config = new; |
d546aa0e CB |
3494 | memcpy(new + poffset, newdir, newdirlen); |
3495 | lend += diff; | |
fd986e08 | 3496 | } |
d546aa0e CB |
3497 | next: |
3498 | lstart = lend; | |
fd986e08 | 3499 | } |
6b0d5538 SH |
3500 | return true; |
3501 | } | |
3502 | ||
3503 | #define DO(cmd) { \ | |
3504 | if (!(cmd)) { \ | |
3505 | ERROR("Error writing to new config"); \ | |
3506 | return false; \ | |
3507 | } \ | |
3508 | } | |
3509 | ||
091045f8 | 3510 | static bool new_hwaddr(char *hwaddr) |
67702c21 | 3511 | { |
091045f8 CB |
3512 | int ret; |
3513 | ||
091045f8 CB |
3514 | (void)randseed(true); |
3515 | ||
3516 | ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255, | |
3517 | rand() % 255, rand() % 255); | |
3518 | if (ret < 0 || ret >= 18) { | |
3519 | SYSERROR("Failed to call snprintf()."); | |
3520 | return false; | |
3521 | } | |
3522 | ||
3523 | return true; | |
67702c21 SH |
3524 | } |
3525 | ||
3526 | /* | |
3527 | * This is called only from clone. | |
3528 | * We wish to update all hwaddrs in the unexpanded config file. We | |
3529 | * can't/don't want to update any which come from lxc.includes (there | |
3530 | * shouldn't be any). | |
3531 | * We can't just walk the c->lxc-conf->network list because that includes | |
3532 | * netifs from the include files. So we update the ones which we find in | |
3533 | * the unexp config file, then find the original macaddr in the | |
3534 | * conf->network, and update that to the same value. | |
3535 | */ | |
3536 | bool network_new_hwaddrs(struct lxc_conf *conf) | |
6b0d5538 SH |
3537 | { |
3538 | struct lxc_list *it; | |
3539 | ||
67702c21 SH |
3540 | const char *key = "lxc.network.hwaddr"; |
3541 | char *lstart = conf->unexpanded_config, *lend, *p, *p2; | |
6b0d5538 | 3542 | |
67702c21 SH |
3543 | if (!conf->unexpanded_config) |
3544 | return true; | |
091045f8 | 3545 | |
67702c21 SH |
3546 | while (*lstart) { |
3547 | char newhwaddr[18], oldhwaddr[17]; | |
091045f8 | 3548 | |
67702c21 SH |
3549 | lend = strchr(lstart, '\n'); |
3550 | if (!lend) | |
3551 | lend = lstart + strlen(lstart); | |
3552 | else | |
3553 | lend++; | |
091045f8 | 3554 | |
67702c21 SH |
3555 | if (strncmp(lstart, key, strlen(key)) != 0) { |
3556 | lstart = lend; | |
3557 | continue; | |
72d0e1cb | 3558 | } |
091045f8 | 3559 | |
67702c21 SH |
3560 | p = strchr(lstart+strlen(key), '='); |
3561 | if (!p) { | |
3562 | lstart = lend; | |
3563 | continue; | |
72d0e1cb | 3564 | } |
091045f8 | 3565 | |
67702c21 SH |
3566 | p++; |
3567 | while (isblank(*p)) | |
3568 | p++; | |
3569 | if (!*p) | |
3570 | return true; | |
091045f8 | 3571 | |
67702c21 SH |
3572 | p2 = p; |
3573 | while (*p2 && !isblank(*p2) && *p2 != '\n') | |
3574 | p2++; | |
3575 | if (p2-p != 17) { | |
3576 | WARN("Bad hwaddr entry"); | |
3577 | lstart = lend; | |
3578 | continue; | |
72d0e1cb | 3579 | } |
091045f8 | 3580 | |
67702c21 | 3581 | memcpy(oldhwaddr, p, 17); |
091045f8 CB |
3582 | |
3583 | if (!new_hwaddr(newhwaddr)) | |
3584 | return false; | |
3585 | ||
67702c21 SH |
3586 | memcpy(p, newhwaddr, 17); |
3587 | lxc_list_for_each(it, &conf->network) { | |
3588 | struct lxc_netdev *n = it->elem; | |
3589 | if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0) | |
3590 | memcpy(n->hwaddr, newhwaddr, 17); | |
72d0e1cb | 3591 | } |
67702c21 SH |
3592 | |
3593 | lstart = lend; | |
72d0e1cb | 3594 | } |
091045f8 | 3595 | |
6b0d5538 | 3596 | return true; |
72d0e1cb | 3597 | } |
8796becf | 3598 | |
713046e3 | 3599 | static int set_config_ephemeral(const char *key, const char *value, |
8796becf CB |
3600 | struct lxc_conf *lxc_conf) |
3601 | { | |
3c6cf53a CB |
3602 | /* Set config value to default. */ |
3603 | if (config_value_empty(value)) { | |
3604 | lxc_conf->ephemeral = 0; | |
78304622 | 3605 | return 0; |
3c6cf53a | 3606 | } |
78304622 | 3607 | |
3c6cf53a | 3608 | /* Parse new config value. */ |
66ffdb1a CB |
3609 | if (lxc_safe_uint(value, &lxc_conf->ephemeral) < 0) |
3610 | return -1; | |
8796becf | 3611 | |
66ffdb1a | 3612 | if (lxc_conf->ephemeral > 1) { |
8796becf CB |
3613 | ERROR("Wrong value for lxc.ephemeral. Can only be set to 0 or 1"); |
3614 | return -1; | |
8796becf CB |
3615 | } |
3616 | ||
3617 | return 0; | |
3618 | } | |
3619 | ||
713046e3 | 3620 | static int set_config_syslog(const char *key, const char *value, |
76d0127f | 3621 | struct lxc_conf *lxc_conf) |
64c57ea1 | 3622 | { |
76d0127f | 3623 | int facility; |
7ca56b84 | 3624 | |
ee10a69c CB |
3625 | /* Clear any previously set value. */ |
3626 | if (lxc_conf->syslog) { | |
3627 | free(lxc_conf->syslog); | |
3628 | lxc_conf->syslog = NULL; | |
3629 | } | |
3630 | ||
3631 | /* Check if value is empty. */ | |
7ca56b84 CB |
3632 | if (config_value_empty(value)) |
3633 | return 0; | |
3634 | ||
ee10a69c | 3635 | /* Parse value. */ |
76d0127f CB |
3636 | facility = lxc_syslog_priority_to_int(value); |
3637 | if (facility == -EINVAL) { | |
d47f1b43 | 3638 | ERROR("Wrong value for lxc.syslog."); |
76d0127f | 3639 | return -1; |
64c57ea1 BD |
3640 | } |
3641 | ||
76d0127f | 3642 | lxc_log_syslog(facility); |
713046e3 | 3643 | return set_config_string_item(&lxc_conf->syslog, value); |
64c57ea1 | 3644 | } |
5a46f283 | 3645 | |
713046e3 | 3646 | static int set_config_no_new_privs(const char *key, const char *value, |
cf3f8bf6 | 3647 | struct lxc_conf *lxc_conf) |
5a46f283 | 3648 | { |
e8ec7c9e | 3649 | unsigned int v; |
5a46f283 | 3650 | |
cf3f8bf6 CB |
3651 | /* Set config value to default. */ |
3652 | if (config_value_empty(value)) { | |
3653 | lxc_conf->no_new_privs = false; | |
80926845 | 3654 | return 0; |
cf3f8bf6 | 3655 | } |
80926845 | 3656 | |
cf3f8bf6 | 3657 | /* Parse new config value. */ |
e8ec7c9e CB |
3658 | if (lxc_safe_uint(value, &v) < 0) |
3659 | return -1; | |
3660 | ||
3661 | if (v > 1) { | |
5a46f283 CB |
3662 | ERROR("Wrong value for lxc.no_new_privs. Can only be set to 0 or 1"); |
3663 | return -1; | |
3664 | } | |
e8ec7c9e | 3665 | |
5a46f283 CB |
3666 | lxc_conf->no_new_privs = v ? true : false; |
3667 | ||
3668 | return 0; | |
3669 | } | |
7b992a3e CB |
3670 | |
3671 | /* Callbacks to get configuration items. */ | |
3672 | static int get_config_personality(struct lxc_container *c, const char *key, | |
3673 | char *retv, int inlen) | |
3674 | { | |
3675 | int fulllen = 0; | |
3676 | ||
3677 | if (!retv) | |
3678 | inlen = 0; | |
3679 | else | |
3680 | memset(retv, 0, inlen); | |
3681 | ||
3682 | #if HAVE_SYS_PERSONALITY_H | |
3683 | int len = 0; | |
3684 | ||
3685 | switch (c->lxc_conf->personality) { | |
3686 | case PER_LINUX32: | |
3687 | strprint(retv, inlen, "i686"); | |
3688 | break; | |
3689 | case PER_LINUX: | |
3690 | strprint(retv, inlen, "x86_64"); | |
3691 | break; | |
3692 | default: | |
3693 | break; | |
3694 | } | |
3695 | #endif | |
3696 | ||
3697 | return fulllen; | |
3698 | } | |
bdf91ab4 CB |
3699 | |
3700 | static int get_config_pts(struct lxc_container *c, const char *key, char *retv, | |
3701 | int inlen) | |
3702 | { | |
3703 | return lxc_get_conf_int(c->lxc_conf, retv, inlen, c->lxc_conf->pts); | |
3704 | } | |
5485782f CB |
3705 | |
3706 | static int get_config_tty(struct lxc_container *c, const char *key, char *retv, | |
3707 | int inlen) | |
3708 | { | |
3709 | return lxc_get_conf_int(c->lxc_conf, retv, inlen, c->lxc_conf->tty); | |
3710 | } | |
8015e018 CB |
3711 | |
3712 | static inline int lxc_get_conf_str(char *retv, int inlen, char *value) | |
3713 | { | |
3714 | if (!value) | |
3715 | return 0; | |
3716 | if (retv && inlen >= strlen(value) + 1) | |
3717 | strncpy(retv, value, strlen(value) + 1); | |
3718 | ||
3719 | return strlen(value); | |
3720 | } | |
3721 | ||
3722 | static int get_config_ttydir(struct lxc_container *c, const char *key, | |
3723 | char *retv, int inlen) | |
3724 | { | |
3725 | return lxc_get_conf_str(retv, inlen, c->lxc_conf->ttydir); | |
3726 | } | |
de1ede69 CB |
3727 | |
3728 | static int get_config_kmsg(struct lxc_container *c, const char *key, char *retv, | |
3729 | int inlen) | |
3730 | { | |
3731 | return lxc_get_conf_int(c->lxc_conf, retv, inlen, c->lxc_conf->kmsg); | |
3732 | } |