]>
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 |
c2cc9f0a | 24 | #include <stdio.h> |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | #include <unistd.h> | |
28 | #include <errno.h> | |
63376d7d | 29 | #include <fcntl.h> |
a84b9932 AV |
30 | #include <ctype.h> |
31 | #include <signal.h> | |
63376d7d | 32 | #include <sys/stat.h> |
c2cc9f0a | 33 | #include <sys/types.h> |
34 | #include <sys/param.h> | |
35 | #include <sys/utsname.h> | |
36 | #include <arpa/inet.h> | |
37 | #include <netinet/in.h> | |
38 | #include <net/if.h> | |
67702c21 | 39 | #include <time.h> |
e1daebd9 | 40 | #include <dirent.h> |
64c57ea1 | 41 | #include <syslog.h> |
c2cc9f0a | 42 | |
d8e48992 | 43 | #include "bdev.h" |
b2718c72 | 44 | #include "parse.h" |
6ff05e18 | 45 | #include "config.h" |
525f0002 | 46 | #include "confile.h" |
26c39028 | 47 | #include "utils.h" |
f2363e38 ÇO |
48 | #include "log.h" |
49 | #include "conf.h" | |
72d0e1cb | 50 | #include "network.h" |
58e0f57d | 51 | #include "lxcseccomp.h" |
36eb9bde | 52 | |
6ff05e18 SG |
53 | #if HAVE_SYS_PERSONALITY_H |
54 | #include <sys/personality.h> | |
55 | #endif | |
56 | ||
36eb9bde | 57 | lxc_log_define(lxc_confile, lxc); |
576f946d | 58 | |
12a50cc6 DE |
59 | static int config_personality(const char *, const char *, struct lxc_conf *); |
60 | static int config_pts(const char *, const char *, struct lxc_conf *); | |
61 | static int config_tty(const char *, const char *, struct lxc_conf *); | |
62 | static int config_ttydir(const char *, const char *, struct lxc_conf *); | |
7e0e1d94 | 63 | static int config_kmsg(const char *, const char *, struct lxc_conf *); |
fe4de9a6 | 64 | static int config_lsm_aa_profile(const char *, const char *, struct lxc_conf *); |
7aff4f43 | 65 | static int config_lsm_aa_incomplete(const char *, const char *, struct lxc_conf *); |
fe4de9a6 | 66 | static int config_lsm_se_context(const char *, const char *, struct lxc_conf *); |
12a50cc6 | 67 | static int config_cgroup(const char *, const char *, struct lxc_conf *); |
f6d3e3e4 | 68 | static int config_idmap(const char *, const char *, struct lxc_conf *); |
4a85ce2a SH |
69 | static int config_loglevel(const char *, const char *, struct lxc_conf *); |
70 | static int config_logfile(const char *, const char *, struct lxc_conf *); | |
12a50cc6 | 71 | static int config_mount(const char *, const char *, struct lxc_conf *); |
70f55bfd NC |
72 | static int config_mount_auto(const char *, const char *, struct lxc_conf *); |
73 | static int config_fstab(const char *, const char *, struct lxc_conf *); | |
12a50cc6 DE |
74 | static int config_rootfs(const char *, const char *, struct lxc_conf *); |
75 | static int config_rootfs_mount(const char *, const char *, struct lxc_conf *); | |
a17b1e65 | 76 | static int config_rootfs_options(const char *, const char *, struct lxc_conf *); |
327a1e78 | 77 | static int config_rootfs_backend(const char *, const char *, struct lxc_conf *); |
12a50cc6 DE |
78 | static int config_pivotdir(const char *, const char *, struct lxc_conf *); |
79 | static int config_utsname(const char *, const char *, struct lxc_conf *); | |
80 | static int config_hook(const char *, const char *, struct lxc_conf *lxc_conf); | |
6b0d5538 | 81 | static int config_network(const char *, const char *, struct lxc_conf *); |
12a50cc6 DE |
82 | static int config_network_type(const char *, const char *, struct lxc_conf *); |
83 | static int config_network_flags(const char *, const char *, struct lxc_conf *); | |
84 | static int config_network_link(const char *, const char *, struct lxc_conf *); | |
85 | static int config_network_name(const char *, const char *, struct lxc_conf *); | |
86 | static int config_network_veth_pair(const char *, const char *, struct lxc_conf *); | |
87 | static int config_network_macvlan_mode(const char *, const char *, struct lxc_conf *); | |
88 | static int config_network_hwaddr(const char *, const char *, struct lxc_conf *); | |
89 | static int config_network_vlan_id(const char *, const char *, struct lxc_conf *); | |
90 | static int config_network_mtu(const char *, const char *, struct lxc_conf *); | |
91 | static int config_network_ipv4(const char *, const char *, struct lxc_conf *); | |
92 | static int config_network_ipv4_gateway(const char *, const char *, struct lxc_conf *); | |
8fc8295a DE |
93 | static int config_network_script_up(const char *, const char *, struct lxc_conf *); |
94 | static int config_network_script_down(const char *, const char *, struct lxc_conf *); | |
12a50cc6 DE |
95 | static int config_network_ipv6(const char *, const char *, struct lxc_conf *); |
96 | static int config_network_ipv6_gateway(const char *, const char *, struct lxc_conf *); | |
97 | static int config_cap_drop(const char *, const char *, struct lxc_conf *); | |
1fb86a7c | 98 | static int config_cap_keep(const char *, const char *, struct lxc_conf *); |
12a50cc6 | 99 | static int config_console(const char *, const char *, struct lxc_conf *); |
96f15ca1 | 100 | static int config_console_logfile(const char *, const char *, struct lxc_conf *); |
12a50cc6 DE |
101 | static int config_seccomp(const char *, const char *, struct lxc_conf *); |
102 | static int config_includefile(const char *, const char *, struct lxc_conf *); | |
103 | static int config_network_nic(const char *, const char *, struct lxc_conf *); | |
104 | static int config_autodev(const char *, const char *, struct lxc_conf *); | |
f0f1d8c0 | 105 | static int config_haltsignal(const char *, const char *, struct lxc_conf *); |
dd267776 | 106 | static int config_rebootsignal(const char *, const char *, struct lxc_conf *); |
a84b9932 | 107 | static int config_stopsignal(const char *, const char *, struct lxc_conf *); |
ee1e7aa0 | 108 | static int config_start(const char *, const char *, struct lxc_conf *); |
64c57ea1 | 109 | static int config_syslog(const char *, const char *, struct lxc_conf *); |
a8dfe4e0 | 110 | static int config_monitor(const char *, const char *, struct lxc_conf *); |
ee1e7aa0 | 111 | static int config_group(const char *, const char *, struct lxc_conf *); |
7c661726 | 112 | static int config_environment(const char *, const char *, struct lxc_conf *); |
67c660d0 | 113 | static int config_init_cmd(const char *, const char *, struct lxc_conf *); |
72bb04e4 PT |
114 | static int config_init_uid(const char *, const char *, struct lxc_conf *); |
115 | static int config_init_gid(const char *, const char *, struct lxc_conf *); | |
8796becf | 116 | static int config_ephemeral(const char *, const char *, struct lxc_conf *); |
5a46f283 | 117 | static int config_no_new_privs(const char *, const char *, struct lxc_conf *); |
c2cc9f0a | 118 | |
72d0e1cb | 119 | static struct lxc_config_t config[] = { |
576f946d | 120 | |
cccc74b5 | 121 | { "lxc.arch", config_personality }, |
e892973e DL |
122 | { "lxc.pts", config_pts }, |
123 | { "lxc.tty", config_tty }, | |
7c6ef2a2 | 124 | { "lxc.devttydir", config_ttydir }, |
7e0e1d94 | 125 | { "lxc.kmsg", config_kmsg }, |
fe4de9a6 | 126 | { "lxc.aa_profile", config_lsm_aa_profile }, |
7aff4f43 | 127 | { "lxc.aa_allow_incomplete", config_lsm_aa_incomplete }, |
fe4de9a6 | 128 | { "lxc.se_context", config_lsm_se_context }, |
e892973e | 129 | { "lxc.cgroup", config_cgroup }, |
f6d3e3e4 | 130 | { "lxc.id_map", config_idmap }, |
4a85ce2a SH |
131 | { "lxc.loglevel", config_loglevel }, |
132 | { "lxc.logfile", config_logfile }, | |
70f55bfd NC |
133 | { "lxc.mount.entry", config_mount }, |
134 | { "lxc.mount.auto", config_mount_auto }, | |
135 | { "lxc.mount", config_fstab }, | |
23b7ea69 | 136 | { "lxc.rootfs.mount", config_rootfs_mount }, |
a17b1e65 | 137 | { "lxc.rootfs.options", config_rootfs_options }, |
327a1e78 | 138 | { "lxc.rootfs.backend", config_rootfs_backend }, |
e892973e | 139 | { "lxc.rootfs", config_rootfs }, |
bf601689 | 140 | { "lxc.pivotdir", config_pivotdir }, |
e892973e | 141 | { "lxc.utsname", config_utsname }, |
26ddeedd | 142 | { "lxc.hook.pre-start", config_hook }, |
12a50cc6 | 143 | { "lxc.hook.pre-mount", config_hook }, |
26ddeedd | 144 | { "lxc.hook.mount", config_hook }, |
f7bee6c6 | 145 | { "lxc.hook.autodev", config_hook }, |
26ddeedd | 146 | { "lxc.hook.start", config_hook }, |
52492063 | 147 | { "lxc.hook.stop", config_hook }, |
26ddeedd | 148 | { "lxc.hook.post-stop", config_hook }, |
148e91f5 | 149 | { "lxc.hook.clone", config_hook }, |
37cf711b | 150 | { "lxc.hook.destroy", config_hook }, |
6b0d5538 | 151 | { "lxc.hook", config_hook }, |
e892973e DL |
152 | { "lxc.network.type", config_network_type }, |
153 | { "lxc.network.flags", config_network_flags }, | |
154 | { "lxc.network.link", config_network_link }, | |
155 | { "lxc.network.name", config_network_name }, | |
156 | { "lxc.network.macvlan.mode", config_network_macvlan_mode }, | |
157 | { "lxc.network.veth.pair", config_network_veth_pair }, | |
8fc8295a DE |
158 | { "lxc.network.script.up", config_network_script_up }, |
159 | { "lxc.network.script.down", config_network_script_down }, | |
e892973e DL |
160 | { "lxc.network.hwaddr", config_network_hwaddr }, |
161 | { "lxc.network.mtu", config_network_mtu }, | |
162 | { "lxc.network.vlan.id", config_network_vlan_id }, | |
f8fee0e2 | 163 | { "lxc.network.ipv4.gateway", config_network_ipv4_gateway }, |
e892973e | 164 | { "lxc.network.ipv4", config_network_ipv4 }, |
f8fee0e2 | 165 | { "lxc.network.ipv6.gateway", config_network_ipv6_gateway }, |
e892973e | 166 | { "lxc.network.ipv6", config_network_ipv6 }, |
72d0e1cb SG |
167 | /* config_network_nic must come after all other 'lxc.network.*' entries */ |
168 | { "lxc.network.", config_network_nic }, | |
6b0d5538 | 169 | { "lxc.network", config_network }, |
81810dd1 | 170 | { "lxc.cap.drop", config_cap_drop }, |
1fb86a7c | 171 | { "lxc.cap.keep", config_cap_keep }, |
96f15ca1 | 172 | { "lxc.console.logfile", config_console_logfile }, |
63376d7d | 173 | { "lxc.console", config_console }, |
8f2c3a70 | 174 | { "lxc.seccomp", config_seccomp }, |
09ad6246 | 175 | { "lxc.include", config_includefile }, |
c6883f38 | 176 | { "lxc.autodev", config_autodev }, |
f0f1d8c0 | 177 | { "lxc.haltsignal", config_haltsignal }, |
dd267776 | 178 | { "lxc.rebootsignal", config_rebootsignal }, |
a84b9932 | 179 | { "lxc.stopsignal", config_stopsignal }, |
ee1e7aa0 SG |
180 | { "lxc.start.auto", config_start }, |
181 | { "lxc.start.delay", config_start }, | |
182 | { "lxc.start.order", config_start }, | |
a8dfe4e0 | 183 | { "lxc.monitor.unshare", config_monitor }, |
ee1e7aa0 | 184 | { "lxc.group", config_group }, |
7c661726 | 185 | { "lxc.environment", config_environment }, |
67c660d0 | 186 | { "lxc.init_cmd", config_init_cmd }, |
72bb04e4 PT |
187 | { "lxc.init_uid", config_init_uid }, |
188 | { "lxc.init_gid", config_init_gid }, | |
8796becf | 189 | { "lxc.ephemeral", config_ephemeral }, |
64c57ea1 | 190 | { "lxc.syslog", config_syslog }, |
5a46f283 | 191 | { "lxc.no_new_privs", config_no_new_privs }, |
a84b9932 AV |
192 | }; |
193 | ||
194 | struct signame { | |
195 | int num; | |
74a3920a | 196 | const char *name; |
a84b9932 AV |
197 | }; |
198 | ||
74a3920a | 199 | static const struct signame signames[] = { |
a84b9932 AV |
200 | { SIGHUP, "HUP" }, |
201 | { SIGINT, "INT" }, | |
202 | { SIGQUIT, "QUIT" }, | |
203 | { SIGILL, "ILL" }, | |
204 | { SIGABRT, "ABRT" }, | |
205 | { SIGFPE, "FPE" }, | |
206 | { SIGKILL, "KILL" }, | |
207 | { SIGSEGV, "SEGV" }, | |
208 | { SIGPIPE, "PIPE" }, | |
209 | { SIGALRM, "ALRM" }, | |
210 | { SIGTERM, "TERM" }, | |
211 | { SIGUSR1, "USR1" }, | |
212 | { SIGUSR2, "USR2" }, | |
213 | { SIGCHLD, "CHLD" }, | |
214 | { SIGCONT, "CONT" }, | |
215 | { SIGSTOP, "STOP" }, | |
216 | { SIGTSTP, "TSTP" }, | |
217 | { SIGTTIN, "TTIN" }, | |
218 | { SIGTTOU, "TTOU" }, | |
89dfc302 SY |
219 | #ifdef SIGTRAP |
220 | { SIGTRAP, "TRAP" }, | |
221 | #endif | |
222 | #ifdef SIGIOT | |
223 | { SIGIOT, "IOT" }, | |
224 | #endif | |
225 | #ifdef SIGEMT | |
226 | { SIGEMT, "EMT" }, | |
227 | #endif | |
228 | #ifdef SIGBUS | |
229 | { SIGBUS, "BUS" }, | |
230 | #endif | |
231 | #ifdef SIGSTKFLT | |
232 | { SIGSTKFLT, "STKFLT" }, | |
233 | #endif | |
234 | #ifdef SIGCLD | |
235 | { SIGCLD, "CLD" }, | |
236 | #endif | |
237 | #ifdef SIGURG | |
238 | { SIGURG, "URG" }, | |
239 | #endif | |
240 | #ifdef SIGXCPU | |
241 | { SIGXCPU, "XCPU" }, | |
242 | #endif | |
243 | #ifdef SIGXFSZ | |
244 | { SIGXFSZ, "XFSZ" }, | |
245 | #endif | |
246 | #ifdef SIGVTALRM | |
247 | { SIGVTALRM, "VTALRM" }, | |
248 | #endif | |
249 | #ifdef SIGPROF | |
250 | { SIGPROF, "PROF" }, | |
251 | #endif | |
252 | #ifdef SIGWINCH | |
253 | { SIGWINCH, "WINCH" }, | |
254 | #endif | |
255 | #ifdef SIGIO | |
256 | { SIGIO, "IO" }, | |
257 | #endif | |
258 | #ifdef SIGPOLL | |
259 | { SIGPOLL, "POLL" }, | |
260 | #endif | |
261 | #ifdef SIGINFO | |
262 | { SIGINFO, "INFO" }, | |
263 | #endif | |
264 | #ifdef SIGLOST | |
265 | { SIGLOST, "LOST" }, | |
266 | #endif | |
267 | #ifdef SIGPWR | |
268 | { SIGPWR, "PWR" }, | |
269 | #endif | |
270 | #ifdef SIGUNUSED | |
271 | { SIGUNUSED, "UNUSED" }, | |
272 | #endif | |
273 | #ifdef SIGSYS | |
274 | { SIGSYS, "SYS" }, | |
275 | #endif | |
c2cc9f0a | 276 | }; |
277 | ||
72d0e1cb | 278 | static const size_t config_size = sizeof(config)/sizeof(struct lxc_config_t); |
c2cc9f0a | 279 | |
72d0e1cb | 280 | extern struct lxc_config_t *lxc_getconfig(const char *key) |
c2cc9f0a | 281 | { |
84760c11 | 282 | size_t i; |
c2cc9f0a | 283 | |
284 | for (i = 0; i < config_size; i++) | |
a871ff6b | 285 | if (!strncmp(config[i].name, key, |
c2cc9f0a | 286 | strlen(config[i].name))) |
287 | return &config[i]; | |
288 | return NULL; | |
289 | } | |
290 | ||
72d0e1cb SG |
291 | #define strprint(str, inlen, ...) \ |
292 | do { \ | |
293 | len = snprintf(str, inlen, ##__VA_ARGS__); \ | |
294 | if (len < 0) { SYSERROR("snprintf"); return -1; }; \ | |
295 | fulllen += len; \ | |
296 | if (inlen > 0) { \ | |
297 | if (str) str += len; \ | |
298 | inlen -= len; \ | |
299 | if (inlen < 0) inlen = 0; \ | |
300 | } \ | |
301 | } while (0); | |
302 | ||
303 | int lxc_listconfigs(char *retv, int inlen) | |
304 | { | |
84760c11 | 305 | size_t i; |
306 | int fulllen = 0, len; | |
72d0e1cb SG |
307 | |
308 | if (!retv) | |
309 | inlen = 0; | |
310 | else | |
311 | memset(retv, 0, inlen); | |
312 | for (i = 0; i < config_size; i++) { | |
313 | char *s = config[i].name; | |
314 | if (s[strlen(s)-1] == '.') | |
315 | continue; | |
316 | strprint(retv, inlen, "%s\n", s); | |
317 | } | |
318 | return fulllen; | |
319 | } | |
320 | ||
6d03d92a DE |
321 | static int config_string_item(char **conf_item, const char *value) |
322 | { | |
323 | char *new_value; | |
324 | ||
d6eca240 | 325 | if (!value || strlen(value) == 0) { |
f10fad2f | 326 | free(*conf_item); |
d6eca240 | 327 | *conf_item = NULL; |
6d03d92a | 328 | return 0; |
d6eca240 | 329 | } |
6d03d92a DE |
330 | |
331 | new_value = strdup(value); | |
332 | if (!new_value) { | |
333 | SYSERROR("failed to strdup '%s': %m", value); | |
334 | return -1; | |
335 | } | |
336 | ||
f10fad2f | 337 | free(*conf_item); |
6d03d92a DE |
338 | *conf_item = new_value; |
339 | return 0; | |
340 | } | |
341 | ||
342 | static int config_string_item_max(char **conf_item, const char *value, | |
343 | size_t max) | |
344 | { | |
345 | if (strlen(value) >= max) { | |
a5a82508 | 346 | ERROR("%s is too long (>= %lu)", value, (unsigned long)max); |
6d03d92a DE |
347 | return -1; |
348 | } | |
349 | ||
350 | return config_string_item(conf_item, value); | |
351 | } | |
352 | ||
353 | static int config_path_item(char **conf_item, const char *value) | |
354 | { | |
355 | return config_string_item_max(conf_item, value, PATH_MAX); | |
356 | } | |
357 | ||
72d0e1cb SG |
358 | /* |
359 | * config entry is something like "lxc.network.0.ipv4" | |
360 | * the key 'lxc.network.' was found. So we make sure next | |
361 | * comes an integer, find the right callback (by rewriting | |
362 | * the key), and call it. | |
363 | */ | |
12a50cc6 | 364 | static int config_network_nic(const char *key, const char *value, |
72d0e1cb SG |
365 | struct lxc_conf *lxc_conf) |
366 | { | |
367 | char *copy = strdup(key), *p; | |
368 | int ret = -1; | |
369 | struct lxc_config_t *config; | |
370 | ||
371 | if (!copy) { | |
372 | SYSERROR("failed to allocate memory"); | |
373 | return -1; | |
374 | } | |
375 | /* | |
376 | * ok we know that to get here we've got "lxc.network." | |
377 | * and it isn't any of the other network entries. So | |
378 | * after the second . should come an integer (# of defined | |
379 | * nic) followed by a valid entry. | |
380 | */ | |
381 | if (*(key+12) < '0' || *(key+12) > '9') | |
382 | goto out; | |
46cd2845 | 383 | p = strchr(key+12, '.'); |
72d0e1cb SG |
384 | if (!p) |
385 | goto out; | |
386 | strcpy(copy+12, p+1); | |
387 | config = lxc_getconfig(copy); | |
388 | if (!config) { | |
389 | ERROR("unknown key %s", key); | |
390 | goto out; | |
391 | } | |
392 | ret = config->cb(key, value, lxc_conf); | |
393 | ||
394 | out: | |
395 | free(copy); | |
396 | return ret; | |
397 | } | |
398 | ||
6b0d5538 SH |
399 | static int config_network(const char *key, const char *value, |
400 | struct lxc_conf *lxc_conf) | |
401 | { | |
402 | if (value && strlen(value)) { | |
403 | ERROR("lxc.network must not have a value"); | |
404 | return -1; | |
405 | } | |
406 | ||
407 | return lxc_clear_config_network(lxc_conf); | |
408 | } | |
409 | ||
261658e8 BP |
410 | static int macvlan_mode(int *valuep, const char *value); |
411 | ||
12a50cc6 | 412 | static int config_network_type(const char *key, const char *value, |
e892973e | 413 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 414 | { |
5f4535a3 | 415 | struct lxc_list *network = &lxc_conf->network; |
c2cc9f0a | 416 | struct lxc_netdev *netdev; |
417 | struct lxc_list *list; | |
c2cc9f0a | 418 | |
7d0eb87e SH |
419 | if (!value || strlen(value) == 0) |
420 | return lxc_clear_config_network(lxc_conf); | |
421 | ||
c2cc9f0a | 422 | netdev = malloc(sizeof(*netdev)); |
423 | if (!netdev) { | |
36eb9bde | 424 | SYSERROR("failed to allocate memory"); |
c2cc9f0a | 425 | return -1; |
426 | } | |
427 | ||
82d5ae15 | 428 | memset(netdev, 0, sizeof(*netdev)); |
c2cc9f0a | 429 | lxc_list_init(&netdev->ipv4); |
430 | lxc_list_init(&netdev->ipv6); | |
c2cc9f0a | 431 | |
432 | list = malloc(sizeof(*list)); | |
433 | if (!list) { | |
36eb9bde | 434 | SYSERROR("failed to allocate memory"); |
022de5f3 | 435 | free(netdev); |
c2cc9f0a | 436 | return -1; |
437 | } | |
438 | ||
439 | lxc_list_init(list); | |
5f4535a3 | 440 | list->elem = netdev; |
c2cc9f0a | 441 | |
bac89583 | 442 | lxc_list_add_tail(network, list); |
a871ff6b | 443 | |
c2cc9f0a | 444 | if (!strcmp(value, "veth")) |
24654103 | 445 | netdev->type = LXC_NET_VETH; |
261658e8 | 446 | else if (!strcmp(value, "macvlan")) { |
24654103 | 447 | netdev->type = LXC_NET_MACVLAN; |
261658e8 BP |
448 | macvlan_mode(&netdev->priv.macvlan_attr.mode, "private"); |
449 | } | |
26c39028 | 450 | else if (!strcmp(value, "vlan")) |
24654103 | 451 | netdev->type = LXC_NET_VLAN; |
c2cc9f0a | 452 | else if (!strcmp(value, "phys")) |
24654103 | 453 | netdev->type = LXC_NET_PHYS; |
5f58350a | 454 | else if (!strcmp(value, "empty")) |
24654103 | 455 | netdev->type = LXC_NET_EMPTY; |
26b797f3 SH |
456 | else if (!strcmp(value, "none")) |
457 | netdev->type = LXC_NET_NONE; | |
c2cc9f0a | 458 | else { |
36eb9bde | 459 | ERROR("invalid network type %s", value); |
c2cc9f0a | 460 | return -1; |
461 | } | |
462 | return 0; | |
463 | } | |
464 | ||
a059591e DL |
465 | static int config_ip_prefix(struct in_addr *addr) |
466 | { | |
467 | if (IN_CLASSA(addr->s_addr)) | |
468 | return 32 - IN_CLASSA_NSHIFT; | |
469 | if (IN_CLASSB(addr->s_addr)) | |
470 | return 32 - IN_CLASSB_NSHIFT; | |
471 | if (IN_CLASSC(addr->s_addr)) | |
472 | return 32 - IN_CLASSC_NSHIFT; | |
473 | ||
474 | return 0; | |
475 | } | |
476 | ||
72d0e1cb SG |
477 | /* |
478 | * if you have p="lxc.network.0.link", pass it p+12 | |
479 | * to get back '0' (the index of the nic) | |
480 | */ | |
481 | static int get_network_netdev_idx(const char *key) | |
482 | { | |
483 | int ret, idx; | |
484 | ||
485 | if (*key < '0' || *key > '9') | |
486 | return -1; | |
487 | ret = sscanf(key, "%d", &idx); | |
488 | if (ret != 1) | |
489 | return -1; | |
490 | return idx; | |
491 | } | |
492 | ||
493 | /* | |
494 | * if you have p="lxc.network.0", pass this p+12 and it will return | |
495 | * the netdev of the first configured nic | |
496 | */ | |
497 | static struct lxc_netdev *get_netdev_from_key(const char *key, | |
498 | struct lxc_list *network) | |
499 | { | |
500 | int i = 0, idx = get_network_netdev_idx(key); | |
501 | struct lxc_netdev *netdev = NULL; | |
502 | struct lxc_list *it; | |
503 | if (idx == -1) | |
504 | return NULL; | |
505 | lxc_list_for_each(it, network) { | |
506 | if (idx == i++) { | |
507 | netdev = it->elem; | |
508 | break; | |
509 | } | |
510 | } | |
511 | return netdev; | |
512 | } | |
513 | ||
12a50cc6 DE |
514 | extern int lxc_list_nicconfigs(struct lxc_conf *c, const char *key, |
515 | char *retv, int inlen) | |
72d0e1cb SG |
516 | { |
517 | struct lxc_netdev *netdev; | |
518 | int fulllen = 0, len; | |
519 | ||
520 | netdev = get_netdev_from_key(key+12, &c->network); | |
521 | if (!netdev) | |
522 | return -1; | |
523 | ||
524 | if (!retv) | |
525 | inlen = 0; | |
526 | else | |
527 | memset(retv, 0, inlen); | |
528 | ||
74836992 | 529 | strprint(retv, inlen, "type\n"); |
72d0e1cb | 530 | strprint(retv, inlen, "script.up\n"); |
8fc8295a | 531 | strprint(retv, inlen, "script.down\n"); |
72d0e1cb SG |
532 | if (netdev->type != LXC_NET_EMPTY) { |
533 | strprint(retv, inlen, "flags\n"); | |
534 | strprint(retv, inlen, "link\n"); | |
535 | strprint(retv, inlen, "name\n"); | |
536 | strprint(retv, inlen, "hwaddr\n"); | |
537 | strprint(retv, inlen, "mtu\n"); | |
538 | strprint(retv, inlen, "ipv6\n"); | |
9eaf8a59 | 539 | strprint(retv, inlen, "ipv6.gateway\n"); |
72d0e1cb | 540 | strprint(retv, inlen, "ipv4\n"); |
9eaf8a59 | 541 | strprint(retv, inlen, "ipv4.gateway\n"); |
72d0e1cb SG |
542 | } |
543 | switch(netdev->type) { | |
544 | case LXC_NET_VETH: | |
545 | strprint(retv, inlen, "veth.pair\n"); | |
546 | break; | |
547 | case LXC_NET_MACVLAN: | |
548 | strprint(retv, inlen, "macvlan.mode\n"); | |
549 | break; | |
550 | case LXC_NET_VLAN: | |
551 | strprint(retv, inlen, "vlan.id\n"); | |
552 | break; | |
553 | case LXC_NET_PHYS: | |
554 | break; | |
555 | } | |
556 | return fulllen; | |
557 | } | |
558 | ||
16950ecb DL |
559 | static struct lxc_netdev *network_netdev(const char *key, const char *value, |
560 | struct lxc_list *network) | |
c2cc9f0a | 561 | { |
72d0e1cb | 562 | struct lxc_netdev *netdev = NULL; |
c2cc9f0a | 563 | |
5f4535a3 | 564 | if (lxc_list_empty(network)) { |
16950ecb DL |
565 | ERROR("network is not created for '%s' = '%s' option", |
566 | key, value); | |
33c945e0 | 567 | return NULL; |
c2cc9f0a | 568 | } |
569 | ||
72d0e1cb SG |
570 | if (get_network_netdev_idx(key+12) == -1) |
571 | netdev = lxc_list_last_elem(network); | |
572 | else | |
573 | netdev = get_netdev_from_key(key+12, network); | |
574 | ||
5f4535a3 | 575 | if (!netdev) { |
16950ecb DL |
576 | ERROR("no network device defined for '%s' = '%s' option", |
577 | key, value); | |
33c945e0 | 578 | return NULL; |
c2cc9f0a | 579 | } |
580 | ||
33c945e0 | 581 | return netdev; |
c2cc9f0a | 582 | } |
583 | ||
12a50cc6 | 584 | static int network_ifname(char **valuep, const char *value) |
c2cc9f0a | 585 | { |
c9bb9a85 | 586 | return config_string_item_max(valuep, value, IFNAMSIZ); |
c2cc9f0a | 587 | } |
588 | ||
e892973e DL |
589 | #ifndef MACVLAN_MODE_PRIVATE |
590 | # define MACVLAN_MODE_PRIVATE 1 | |
591 | #endif | |
592 | ||
593 | #ifndef MACVLAN_MODE_VEPA | |
594 | # define MACVLAN_MODE_VEPA 2 | |
595 | #endif | |
596 | ||
597 | #ifndef MACVLAN_MODE_BRIDGE | |
598 | # define MACVLAN_MODE_BRIDGE 4 | |
599 | #endif | |
600 | ||
99854161 EL |
601 | #ifndef MACVLAN_MODE_PASSTHRU |
602 | # define MACVLAN_MODE_PASSTHRU 8 | |
603 | #endif | |
604 | ||
12a50cc6 | 605 | static int macvlan_mode(int *valuep, const char *value) |
e892973e DL |
606 | { |
607 | struct mc_mode { | |
608 | char *name; | |
609 | int mode; | |
610 | } m[] = { | |
611 | { "private", MACVLAN_MODE_PRIVATE }, | |
612 | { "vepa", MACVLAN_MODE_VEPA }, | |
613 | { "bridge", MACVLAN_MODE_BRIDGE }, | |
99854161 | 614 | { "passthru", MACVLAN_MODE_PASSTHRU }, |
e892973e DL |
615 | }; |
616 | ||
84760c11 | 617 | size_t i; |
e892973e DL |
618 | |
619 | for (i = 0; i < sizeof(m)/sizeof(m[0]); i++) { | |
620 | if (strcmp(m[i].name, value)) | |
621 | continue; | |
622 | ||
623 | *valuep = m[i].mode; | |
624 | return 0; | |
625 | } | |
626 | ||
627 | return -1; | |
628 | } | |
629 | ||
508c263e SH |
630 | static int rand_complete_hwaddr(char *hwaddr) |
631 | { | |
632 | const char hex[] = "0123456789abcdef"; | |
633 | char *curs = hwaddr; | |
634 | ||
635 | #ifndef HAVE_RAND_R | |
636 | randseed(true); | |
637 | #else | |
638 | unsigned int seed=randseed(false); | |
639 | #endif | |
e6744e9b | 640 | while (*curs != '\0' && *curs != '\n') |
508c263e SH |
641 | { |
642 | if ( *curs == 'x' || *curs == 'X' ) { | |
643 | if (curs - hwaddr == 1) { | |
644 | //ensure address is unicast | |
645 | #ifdef HAVE_RAND_R | |
646 | *curs = hex[rand_r(&seed) & 0x0E]; | |
647 | } else { | |
648 | *curs = hex[rand_r(&seed) & 0x0F]; | |
649 | #else | |
650 | *curs = hex[rand() & 0x0E]; | |
651 | } else { | |
652 | *curs = hex[rand() & 0x0F]; | |
653 | #endif | |
654 | } | |
655 | } | |
656 | curs++; | |
657 | } | |
658 | return 0; | |
659 | } | |
660 | ||
12a50cc6 | 661 | static int config_network_flags(const char *key, const char *value, |
33c945e0 | 662 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 663 | { |
c2cc9f0a | 664 | struct lxc_netdev *netdev; |
665 | ||
16950ecb | 666 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 667 | if (!netdev) |
c2cc9f0a | 668 | return -1; |
c2cc9f0a | 669 | |
33c945e0 | 670 | netdev->flags |= IFF_UP; |
c2cc9f0a | 671 | |
33c945e0 MT |
672 | return 0; |
673 | } | |
674 | ||
12a50cc6 | 675 | static int config_network_link(const char *key, const char *value, |
33c945e0 MT |
676 | struct lxc_conf *lxc_conf) |
677 | { | |
678 | struct lxc_netdev *netdev; | |
679 | ||
16950ecb | 680 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 681 | if (!netdev) |
c2cc9f0a | 682 | return -1; |
c2cc9f0a | 683 | |
33c945e0 | 684 | return network_ifname(&netdev->link, value); |
c2cc9f0a | 685 | } |
686 | ||
12a50cc6 | 687 | static int config_network_name(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 MT |
696 | return network_ifname(&netdev->name, value); |
697 | } | |
698 | ||
12a50cc6 | 699 | static int config_network_veth_pair(const char *key, const char *value, |
e892973e DL |
700 | struct lxc_conf *lxc_conf) |
701 | { | |
702 | struct lxc_netdev *netdev; | |
703 | ||
704 | netdev = network_netdev(key, value, &lxc_conf->network); | |
705 | if (!netdev) | |
706 | return -1; | |
707 | ||
c5316d60 | 708 | if (netdev->type != LXC_NET_VETH) { |
128d327a | 709 | ERROR("Invalid veth pair for a non-veth netdev"); |
c5316d60 SH |
710 | return -1; |
711 | } | |
e892973e DL |
712 | return network_ifname(&netdev->priv.veth_attr.pair, value); |
713 | } | |
714 | ||
12a50cc6 | 715 | static int config_network_macvlan_mode(const char *key, const char *value, |
e892973e | 716 | struct lxc_conf *lxc_conf) |
8634bc19 MT |
717 | { |
718 | struct lxc_netdev *netdev; | |
719 | ||
720 | netdev = network_netdev(key, value, &lxc_conf->network); | |
721 | if (!netdev) | |
722 | return -1; | |
723 | ||
c5316d60 | 724 | if (netdev->type != LXC_NET_MACVLAN) { |
128d327a | 725 | ERROR("Invalid macvlan.mode for a non-macvlan netdev"); |
c5316d60 SH |
726 | return -1; |
727 | } | |
e892973e | 728 | return macvlan_mode(&netdev->priv.macvlan_attr.mode, value); |
8634bc19 MT |
729 | } |
730 | ||
12a50cc6 | 731 | static int config_network_hwaddr(const char *key, const char *value, |
33c945e0 MT |
732 | struct lxc_conf *lxc_conf) |
733 | { | |
734 | struct lxc_netdev *netdev; | |
735 | ||
508c263e SH |
736 | char *new_value = strdup(value); |
737 | if (!new_value) { | |
738 | SYSERROR("failed to strdup '%s': %m", value); | |
c2cc9f0a | 739 | return -1; |
508c263e SH |
740 | } |
741 | rand_complete_hwaddr(new_value); | |
c2cc9f0a | 742 | |
508c263e SH |
743 | netdev = network_netdev(key, new_value, &lxc_conf->network); |
744 | if (!netdev) { | |
745 | free(new_value); | |
746 | return -1; | |
747 | }; | |
748 | ||
749 | if (!new_value || strlen(new_value) == 0) { | |
750 | free(new_value); | |
751 | netdev->hwaddr = NULL; | |
752 | return 0; | |
753 | } | |
754 | ||
755 | netdev->hwaddr = new_value; | |
756 | return 0; | |
c2cc9f0a | 757 | } |
758 | ||
12a50cc6 | 759 | static int config_network_vlan_id(const char *key, const char *value, |
26c39028 JHS |
760 | struct lxc_conf *lxc_conf) |
761 | { | |
762 | struct lxc_netdev *netdev; | |
763 | ||
764 | netdev = network_netdev(key, value, &lxc_conf->network); | |
765 | if (!netdev) | |
766 | return -1; | |
767 | ||
c5316d60 | 768 | if (netdev->type != LXC_NET_VLAN) { |
128d327a | 769 | ERROR("Invalid vlan.id for a non-macvlan netdev"); |
c5316d60 SH |
770 | return -1; |
771 | } | |
f6cc1de1 | 772 | if (get_u16(&netdev->priv.vlan_attr.vid, value, 0)) |
26c39028 JHS |
773 | return -1; |
774 | ||
775 | return 0; | |
776 | } | |
777 | ||
12a50cc6 | 778 | static int config_network_mtu(const char *key, const char *value, |
33c945e0 | 779 | struct lxc_conf *lxc_conf) |
442cbbe6 | 780 | { |
442cbbe6 TR |
781 | struct lxc_netdev *netdev; |
782 | ||
16950ecb | 783 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 784 | if (!netdev) |
442cbbe6 | 785 | return -1; |
442cbbe6 | 786 | |
6d03d92a | 787 | return config_string_item(&netdev->mtu, value); |
442cbbe6 TR |
788 | } |
789 | ||
12a50cc6 | 790 | static int config_network_ipv4(const char *key, const char *value, |
33c945e0 | 791 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 792 | { |
c2cc9f0a | 793 | struct lxc_netdev *netdev; |
33c945e0 | 794 | struct lxc_inetdev *inetdev; |
c2cc9f0a | 795 | struct lxc_list *list; |
796 | char *cursor, *slash, *addr = NULL, *bcast = NULL, *prefix = NULL; | |
797 | ||
16950ecb | 798 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 799 | if (!netdev) |
c2cc9f0a | 800 | return -1; |
c2cc9f0a | 801 | |
802 | inetdev = malloc(sizeof(*inetdev)); | |
803 | if (!inetdev) { | |
36eb9bde | 804 | SYSERROR("failed to allocate ipv4 address"); |
c2cc9f0a | 805 | return -1; |
806 | } | |
807 | memset(inetdev, 0, sizeof(*inetdev)); | |
808 | ||
809 | list = malloc(sizeof(*list)); | |
810 | if (!list) { | |
36eb9bde | 811 | SYSERROR("failed to allocate memory"); |
53719062 | 812 | free(inetdev); |
c2cc9f0a | 813 | return -1; |
814 | } | |
815 | ||
816 | lxc_list_init(list); | |
817 | list->elem = inetdev; | |
818 | ||
956edc54 SG |
819 | addr = strdup(value); |
820 | if (!addr) { | |
821 | ERROR("no address specified"); | |
53719062 SH |
822 | free(inetdev); |
823 | free(list); | |
956edc54 SG |
824 | return -1; |
825 | } | |
c2cc9f0a | 826 | |
827 | cursor = strstr(addr, " "); | |
828 | if (cursor) { | |
829 | *cursor = '\0'; | |
830 | bcast = cursor + 1; | |
831 | } | |
832 | ||
833 | slash = strstr(addr, "/"); | |
834 | if (slash) { | |
835 | *slash = '\0'; | |
836 | prefix = slash + 1; | |
837 | } | |
838 | ||
c2cc9f0a | 839 | if (!inet_pton(AF_INET, addr, &inetdev->addr)) { |
36eb9bde | 840 | SYSERROR("invalid ipv4 address: %s", value); |
53719062 | 841 | free(inetdev); |
956edc54 | 842 | free(addr); |
53719062 | 843 | free(list); |
c2cc9f0a | 844 | return -1; |
845 | } | |
846 | ||
0093bb8c DL |
847 | if (bcast && !inet_pton(AF_INET, bcast, &inetdev->bcast)) { |
848 | SYSERROR("invalid ipv4 broadcast address: %s", value); | |
53719062 SH |
849 | free(inetdev); |
850 | free(list); | |
956edc54 | 851 | free(addr); |
0093bb8c DL |
852 | return -1; |
853 | } | |
c2cc9f0a | 854 | |
a059591e | 855 | /* no prefix specified, determine it from the network class */ |
1c633398 CB |
856 | if (prefix) { |
857 | if (lxc_safe_uint(prefix, &inetdev->prefix) < 0) | |
858 | return -1; | |
859 | } else { | |
860 | inetdev->prefix = config_ip_prefix(&inetdev->addr); | |
861 | } | |
a059591e | 862 | |
b3df193c | 863 | /* if no broadcast address, let compute one from the |
0093bb8c DL |
864 | * prefix and address |
865 | */ | |
866 | if (!bcast) { | |
1b7d4743 DL |
867 | inetdev->bcast.s_addr = inetdev->addr.s_addr; |
868 | inetdev->bcast.s_addr |= | |
869 | htonl(INADDR_BROADCAST >> inetdev->prefix); | |
0093bb8c | 870 | } |
c2cc9f0a | 871 | |
8538f388 | 872 | lxc_list_add_tail(&netdev->ipv4, list); |
c2cc9f0a | 873 | |
956edc54 | 874 | free(addr); |
c2cc9f0a | 875 | return 0; |
876 | } | |
877 | ||
12a50cc6 | 878 | static int config_network_ipv4_gateway(const char *key, const char *value, |
f8fee0e2 MK |
879 | struct lxc_conf *lxc_conf) |
880 | { | |
881 | struct lxc_netdev *netdev; | |
f8fee0e2 MK |
882 | |
883 | netdev = network_netdev(key, value, &lxc_conf->network); | |
884 | if (!netdev) | |
885 | return -1; | |
886 | ||
e088e926 | 887 | free(netdev->ipv4_gateway); |
f8fee0e2 | 888 | |
e088e926 SG |
889 | if (!value || strlen(value) == 0) { |
890 | netdev->ipv4_gateway = NULL; | |
891 | } else if (!strcmp(value, "auto")) { | |
19a26f82 MK |
892 | netdev->ipv4_gateway = NULL; |
893 | netdev->ipv4_gateway_auto = true; | |
894 | } else { | |
e088e926 SG |
895 | struct in_addr *gw; |
896 | ||
897 | gw = malloc(sizeof(*gw)); | |
898 | if (!gw) { | |
899 | SYSERROR("failed to allocate ipv4 gateway address"); | |
900 | return -1; | |
901 | } | |
902 | ||
19a26f82 MK |
903 | if (!inet_pton(AF_INET, value, gw)) { |
904 | SYSERROR("invalid ipv4 gateway address: %s", value); | |
53719062 | 905 | free(gw); |
19a26f82 MK |
906 | return -1; |
907 | } | |
908 | ||
909 | netdev->ipv4_gateway = gw; | |
910 | netdev->ipv4_gateway_auto = false; | |
f8fee0e2 MK |
911 | } |
912 | ||
f8fee0e2 MK |
913 | return 0; |
914 | } | |
915 | ||
12a50cc6 | 916 | static int config_network_ipv6(const char *key, const char *value, |
e892973e | 917 | struct lxc_conf *lxc_conf) |
c2cc9f0a | 918 | { |
c2cc9f0a | 919 | struct lxc_netdev *netdev; |
920 | struct lxc_inet6dev *inet6dev; | |
921 | struct lxc_list *list; | |
12a50cc6 | 922 | char *slash,*valdup; |
c2cc9f0a | 923 | char *netmask; |
924 | ||
16950ecb | 925 | netdev = network_netdev(key, value, &lxc_conf->network); |
33c945e0 | 926 | if (!netdev) |
c2cc9f0a | 927 | return -1; |
c2cc9f0a | 928 | |
929 | inet6dev = malloc(sizeof(*inet6dev)); | |
930 | if (!inet6dev) { | |
36eb9bde | 931 | SYSERROR("failed to allocate ipv6 address"); |
c2cc9f0a | 932 | return -1; |
933 | } | |
934 | memset(inet6dev, 0, sizeof(*inet6dev)); | |
935 | ||
936 | list = malloc(sizeof(*list)); | |
937 | if (!list) { | |
36eb9bde | 938 | SYSERROR("failed to allocate memory"); |
28027320 | 939 | free(inet6dev); |
c2cc9f0a | 940 | return -1; |
941 | } | |
942 | ||
943 | lxc_list_init(list); | |
944 | list->elem = inet6dev; | |
945 | ||
956edc54 SG |
946 | valdup = strdup(value); |
947 | if (!valdup) { | |
948 | ERROR("no address specified"); | |
28027320 SH |
949 | free(list); |
950 | free(inet6dev); | |
956edc54 SG |
951 | return -1; |
952 | } | |
953 | ||
a059591e | 954 | inet6dev->prefix = 64; |
12a50cc6 | 955 | slash = strstr(valdup, "/"); |
c2cc9f0a | 956 | if (slash) { |
957 | *slash = '\0'; | |
958 | netmask = slash + 1; | |
1c633398 CB |
959 | if (lxc_safe_uint(netmask, &inet6dev->prefix) < 0) |
960 | return -1; | |
c2cc9f0a | 961 | } |
962 | ||
5acccf95 SH |
963 | if (!inet_pton(AF_INET6, valdup, &inet6dev->addr)) { |
964 | SYSERROR("invalid ipv6 address: %s", valdup); | |
28027320 SH |
965 | free(list); |
966 | free(inet6dev); | |
956edc54 | 967 | free(valdup); |
c2cc9f0a | 968 | return -1; |
969 | } | |
970 | ||
8538f388 | 971 | lxc_list_add_tail(&netdev->ipv6, list); |
c2cc9f0a | 972 | |
956edc54 | 973 | free(valdup); |
c2cc9f0a | 974 | return 0; |
975 | } | |
976 | ||
12a50cc6 | 977 | static int config_network_ipv6_gateway(const char *key, const char *value, |
f8fee0e2 MK |
978 | struct lxc_conf *lxc_conf) |
979 | { | |
980 | struct lxc_netdev *netdev; | |
f8fee0e2 MK |
981 | |
982 | netdev = network_netdev(key, value, &lxc_conf->network); | |
983 | if (!netdev) | |
984 | return -1; | |
985 | ||
e088e926 | 986 | free(netdev->ipv6_gateway); |
f8fee0e2 | 987 | |
e088e926 | 988 | if (!value || strlen(value) == 0) { |
ffe34437 | 989 | netdev->ipv6_gateway = NULL; |
e088e926 | 990 | } else if (!strcmp(value, "auto")) { |
19a26f82 MK |
991 | netdev->ipv6_gateway = NULL; |
992 | netdev->ipv6_gateway_auto = true; | |
993 | } else { | |
8fb86a37 SH |
994 | struct in6_addr *gw; |
995 | ||
bec695f3 DE |
996 | gw = malloc(sizeof(*gw)); |
997 | if (!gw) { | |
998 | SYSERROR("failed to allocate ipv6 gateway address"); | |
999 | return -1; | |
1000 | } | |
1001 | ||
19a26f82 MK |
1002 | if (!inet_pton(AF_INET6, value, gw)) { |
1003 | SYSERROR("invalid ipv6 gateway address: %s", value); | |
28027320 | 1004 | free(gw); |
19a26f82 MK |
1005 | return -1; |
1006 | } | |
1007 | ||
1008 | netdev->ipv6_gateway = gw; | |
1009 | netdev->ipv6_gateway_auto = false; | |
f8fee0e2 MK |
1010 | } |
1011 | ||
f8fee0e2 MK |
1012 | return 0; |
1013 | } | |
1014 | ||
8fc8295a DE |
1015 | static int config_network_script_up(const char *key, const char *value, |
1016 | struct lxc_conf *lxc_conf) | |
e3b4c4c4 ST |
1017 | { |
1018 | struct lxc_netdev *netdev; | |
1019 | ||
1020 | netdev = network_netdev(key, value, &lxc_conf->network); | |
1021 | if (!netdev) | |
8fc8295a | 1022 | return -1; |
e3b4c4c4 | 1023 | |
8fc8295a DE |
1024 | return config_string_item(&netdev->upscript, value); |
1025 | } | |
1026 | ||
1027 | static int config_network_script_down(const char *key, const char *value, | |
1028 | struct lxc_conf *lxc_conf) | |
1029 | { | |
1030 | struct lxc_netdev *netdev; | |
1031 | ||
1032 | netdev = network_netdev(key, value, &lxc_conf->network); | |
1033 | if (!netdev) | |
1034 | return -1; | |
1035 | ||
1036 | return config_string_item(&netdev->downscript, value); | |
e3b4c4c4 ST |
1037 | } |
1038 | ||
26ddeedd SH |
1039 | static int add_hook(struct lxc_conf *lxc_conf, int which, char *hook) |
1040 | { | |
1041 | struct lxc_list *hooklist; | |
1042 | ||
1043 | hooklist = malloc(sizeof(*hooklist)); | |
1044 | if (!hooklist) { | |
1045 | free(hook); | |
1046 | return -1; | |
1047 | } | |
1048 | hooklist->elem = hook; | |
1049 | lxc_list_add_tail(&lxc_conf->hooks[which], hooklist); | |
1050 | return 0; | |
1051 | } | |
1052 | ||
12a50cc6 | 1053 | static int config_seccomp(const char *key, const char *value, |
8f2c3a70 SH |
1054 | struct lxc_conf *lxc_conf) |
1055 | { | |
6d03d92a | 1056 | return config_path_item(&lxc_conf->seccomp, value); |
8f2c3a70 SH |
1057 | } |
1058 | ||
67c660d0 SG |
1059 | static int config_init_cmd(const char *key, const char *value, |
1060 | struct lxc_conf *lxc_conf) | |
1061 | { | |
1062 | return config_path_item(&lxc_conf->init_cmd, value); | |
1063 | } | |
1064 | ||
72bb04e4 PT |
1065 | static int config_init_uid(const char *key, const char *value, |
1066 | struct lxc_conf *lxc_conf) | |
1067 | { | |
d1e5d636 CB |
1068 | unsigned int init_uid; |
1069 | ||
1070 | if (lxc_safe_uint(value, &init_uid) < 0) | |
1071 | return -1; | |
1072 | lxc_conf->init_uid = init_uid; | |
1073 | ||
72bb04e4 PT |
1074 | return 0; |
1075 | } | |
1076 | ||
1077 | static int config_init_gid(const char *key, const char *value, | |
1078 | struct lxc_conf *lxc_conf) | |
1079 | { | |
d1e5d636 CB |
1080 | unsigned int init_gid; |
1081 | ||
1082 | if (lxc_safe_uint(value, &init_gid) < 0) | |
1083 | return -1; | |
1084 | lxc_conf->init_gid = init_gid; | |
1085 | ||
72bb04e4 PT |
1086 | return 0; |
1087 | } | |
1088 | ||
12a50cc6 | 1089 | static int config_hook(const char *key, const char *value, |
26ddeedd SH |
1090 | struct lxc_conf *lxc_conf) |
1091 | { | |
7d0eb87e | 1092 | char *copy; |
72bb04e4 | 1093 | |
7d0eb87e SH |
1094 | if (!value || strlen(value) == 0) |
1095 | return lxc_clear_hooks(lxc_conf, key); | |
1096 | ||
6b0d5538 SH |
1097 | if (strcmp(key, "lxc.hook") == 0) { |
1098 | ERROR("lxc.hook cannot take a value"); | |
1099 | return -1; | |
1100 | } | |
7d0eb87e | 1101 | copy = strdup(value); |
26ddeedd SH |
1102 | if (!copy) { |
1103 | SYSERROR("failed to dup string '%s'", value); | |
1104 | return -1; | |
1105 | } | |
1106 | if (strcmp(key, "lxc.hook.pre-start") == 0) | |
1107 | return add_hook(lxc_conf, LXCHOOK_PRESTART, copy); | |
5ea6163a SH |
1108 | else if (strcmp(key, "lxc.hook.pre-mount") == 0) |
1109 | return add_hook(lxc_conf, LXCHOOK_PREMOUNT, copy); | |
f7bee6c6 MW |
1110 | else if (strcmp(key, "lxc.hook.autodev") == 0) |
1111 | return add_hook(lxc_conf, LXCHOOK_AUTODEV, copy); | |
26ddeedd SH |
1112 | else if (strcmp(key, "lxc.hook.mount") == 0) |
1113 | return add_hook(lxc_conf, LXCHOOK_MOUNT, copy); | |
1114 | else if (strcmp(key, "lxc.hook.start") == 0) | |
1115 | return add_hook(lxc_conf, LXCHOOK_START, copy); | |
52492063 WB |
1116 | else if (strcmp(key, "lxc.hook.stop") == 0) |
1117 | return add_hook(lxc_conf, LXCHOOK_STOP, copy); | |
26ddeedd SH |
1118 | else if (strcmp(key, "lxc.hook.post-stop") == 0) |
1119 | return add_hook(lxc_conf, LXCHOOK_POSTSTOP, copy); | |
148e91f5 SH |
1120 | else if (strcmp(key, "lxc.hook.clone") == 0) |
1121 | return add_hook(lxc_conf, LXCHOOK_CLONE, copy); | |
37cf711b SY |
1122 | else if (strcmp(key, "lxc.hook.destroy") == 0) |
1123 | return add_hook(lxc_conf, LXCHOOK_DESTROY, copy); | |
26ddeedd SH |
1124 | SYSERROR("Unknown key: %s", key); |
1125 | free(copy); | |
1126 | return -1; | |
1127 | } | |
1128 | ||
8f47bc3f | 1129 | static int config_personality(const char *key, const char *value, |
cccc74b5 DL |
1130 | struct lxc_conf *lxc_conf) |
1131 | { | |
525f0002 | 1132 | signed long personality = lxc_config_parse_arch(value); |
cccc74b5 | 1133 | |
525f0002 CS |
1134 | if (personality >= 0) |
1135 | lxc_conf->personality = personality; | |
1136 | else | |
1137 | WARN("unsupported personality '%s'", value); | |
970ab589 DL |
1138 | |
1139 | return 0; | |
cccc74b5 DL |
1140 | } |
1141 | ||
12a50cc6 DE |
1142 | static int config_pts(const char *key, const char *value, |
1143 | struct lxc_conf *lxc_conf) | |
10db618d | 1144 | { |
1145 | int maxpts = atoi(value); | |
1146 | ||
1147 | lxc_conf->pts = maxpts; | |
1148 | ||
1149 | return 0; | |
1150 | } | |
1151 | ||
ee1e7aa0 SG |
1152 | static int config_start(const char *key, const char *value, |
1153 | struct lxc_conf *lxc_conf) | |
1154 | { | |
1155 | if(strcmp(key, "lxc.start.auto") == 0) { | |
1156 | lxc_conf->start_auto = atoi(value); | |
1157 | return 0; | |
1158 | } | |
1159 | else if (strcmp(key, "lxc.start.delay") == 0) { | |
1160 | lxc_conf->start_delay = atoi(value); | |
1161 | return 0; | |
1162 | } | |
1163 | else if (strcmp(key, "lxc.start.order") == 0) { | |
1164 | lxc_conf->start_order = atoi(value); | |
1165 | return 0; | |
1166 | } | |
1167 | SYSERROR("Unknown key: %s", key); | |
1168 | return -1; | |
1169 | } | |
1170 | ||
a8dfe4e0 WB |
1171 | static int config_monitor(const char *key, const char *value, |
1172 | struct lxc_conf *lxc_conf) | |
1173 | { | |
1174 | if(strcmp(key, "lxc.monitor.unshare") == 0) { | |
1175 | lxc_conf->monitor_unshare = atoi(value); | |
1176 | return 0; | |
1177 | } | |
1178 | SYSERROR("Unknown key: %s", key); | |
1179 | return -1; | |
1180 | } | |
1181 | ||
ee1e7aa0 SG |
1182 | static int config_group(const char *key, const char *value, |
1183 | struct lxc_conf *lxc_conf) | |
1184 | { | |
1185 | char *groups, *groupptr, *sptr, *token; | |
1186 | struct lxc_list *grouplist; | |
1187 | int ret = -1; | |
1188 | ||
1189 | if (!strlen(value)) | |
1190 | return lxc_clear_groups(lxc_conf); | |
1191 | ||
1192 | groups = strdup(value); | |
1193 | if (!groups) { | |
1194 | SYSERROR("failed to dup '%s'", value); | |
1195 | return -1; | |
1196 | } | |
1197 | ||
1198 | /* in case several groups are specified in a single line | |
1199 | * split these groups in a single element for the list */ | |
1200 | for (groupptr = groups;;groupptr = NULL) { | |
d028235d SG |
1201 | token = strtok_r(groupptr, " \t", &sptr); |
1202 | if (!token) { | |
ee1e7aa0 | 1203 | ret = 0; |
d028235d | 1204 | break; |
ee1e7aa0 SG |
1205 | } |
1206 | ||
1207 | grouplist = malloc(sizeof(*grouplist)); | |
1208 | if (!grouplist) { | |
1209 | SYSERROR("failed to allocate groups list"); | |
1210 | break; | |
1211 | } | |
1212 | ||
1213 | grouplist->elem = strdup(token); | |
1214 | if (!grouplist->elem) { | |
1215 | SYSERROR("failed to dup '%s'", token); | |
1216 | free(grouplist); | |
1217 | break; | |
1218 | } | |
1219 | ||
1220 | lxc_list_add_tail(&lxc_conf->groups, grouplist); | |
d028235d | 1221 | } |
ee1e7aa0 SG |
1222 | |
1223 | free(groups); | |
1224 | ||
1225 | return ret; | |
1226 | } | |
1227 | ||
7c661726 MP |
1228 | static int config_environment(const char *key, const char *value, |
1229 | struct lxc_conf *lxc_conf) | |
1230 | { | |
1231 | struct lxc_list *list_item = NULL; | |
1232 | ||
ab799c0b SG |
1233 | if (!strlen(value)) |
1234 | return lxc_clear_environment(lxc_conf); | |
1235 | ||
7c661726 MP |
1236 | list_item = malloc(sizeof(*list_item)); |
1237 | if (!list_item) | |
1238 | goto freak_out; | |
1239 | ||
1240 | list_item->elem = strdup(value); | |
1241 | ||
1242 | if (!list_item->elem) | |
1243 | goto freak_out; | |
1244 | ||
1245 | lxc_list_add_tail(&lxc_conf->environment, list_item); | |
1246 | ||
1247 | return 0; | |
1248 | ||
1249 | freak_out: | |
f10fad2f | 1250 | free(list_item); |
7c661726 MP |
1251 | |
1252 | return -1; | |
1253 | } | |
1254 | ||
12a50cc6 DE |
1255 | static int config_tty(const char *key, const char *value, |
1256 | struct lxc_conf *lxc_conf) | |
b0a33c1e | 1257 | { |
1258 | int nbtty = atoi(value); | |
1259 | ||
1260 | lxc_conf->tty = nbtty; | |
1261 | ||
1262 | return 0; | |
1263 | } | |
1264 | ||
12a50cc6 | 1265 | static int config_ttydir(const char *key, const char *value, |
7c6ef2a2 SH |
1266 | struct lxc_conf *lxc_conf) |
1267 | { | |
6d03d92a | 1268 | return config_string_item_max(&lxc_conf->ttydir, value, NAME_MAX+1); |
7c6ef2a2 SH |
1269 | } |
1270 | ||
7e0e1d94 AV |
1271 | static int config_kmsg(const char *key, const char *value, |
1272 | struct lxc_conf *lxc_conf) | |
1273 | { | |
1274 | int v = atoi(value); | |
1275 | ||
1276 | lxc_conf->kmsg = v; | |
1277 | ||
1278 | return 0; | |
1279 | } | |
1280 | ||
fe4de9a6 DE |
1281 | static int config_lsm_aa_profile(const char *key, const char *value, |
1282 | struct lxc_conf *lxc_conf) | |
e075f5d9 | 1283 | { |
6d03d92a | 1284 | return config_string_item(&lxc_conf->lsm_aa_profile, value); |
fe4de9a6 DE |
1285 | } |
1286 | ||
7aff4f43 SH |
1287 | static int config_lsm_aa_incomplete(const char *key, const char *value, |
1288 | struct lxc_conf *lxc_conf) | |
1289 | { | |
1290 | int v = atoi(value); | |
1291 | ||
1292 | lxc_conf->lsm_aa_allow_incomplete = v == 1 ? 1 : 0; | |
1293 | ||
1294 | return 0; | |
1295 | } | |
1296 | ||
fe4de9a6 DE |
1297 | static int config_lsm_se_context(const char *key, const char *value, |
1298 | struct lxc_conf *lxc_conf) | |
1299 | { | |
6d03d92a | 1300 | return config_string_item(&lxc_conf->lsm_se_context, value); |
e075f5d9 | 1301 | } |
e075f5d9 | 1302 | |
4a85ce2a | 1303 | static int config_logfile(const char *key, const char *value, |
858377e4 | 1304 | struct lxc_conf *c) |
4a85ce2a | 1305 | { |
6d03d92a DE |
1306 | int ret; |
1307 | ||
b40a606e SH |
1308 | // store these values in the lxc_conf, and then try to set for |
1309 | // actual current logging. | |
858377e4 | 1310 | ret = config_path_item(&c->logfile, value); |
6d03d92a | 1311 | if (ret == 0) |
858377e4 | 1312 | ret = lxc_log_set_file(&c->logfd, c->logfile); |
6d03d92a | 1313 | return ret; |
4a85ce2a SH |
1314 | } |
1315 | ||
1316 | static int config_loglevel(const char *key, const char *value, | |
1317 | struct lxc_conf *lxc_conf) | |
1318 | { | |
9ea87d5d SH |
1319 | int newlevel; |
1320 | ||
4a85ce2a SH |
1321 | if (!value || strlen(value) == 0) |
1322 | return 0; | |
1323 | ||
1324 | if (value[0] >= '0' && value[0] <= '9') | |
9ea87d5d | 1325 | newlevel = atoi(value); |
4a85ce2a | 1326 | else |
9ea87d5d | 1327 | newlevel = lxc_log_priority_to_int(value); |
b40a606e SH |
1328 | // store these values in the lxc_conf, and then try to set for |
1329 | // actual current logging. | |
1330 | lxc_conf->loglevel = newlevel; | |
858377e4 | 1331 | return lxc_log_set_level(&lxc_conf->loglevel, newlevel); |
4a85ce2a SH |
1332 | } |
1333 | ||
12a50cc6 | 1334 | static int config_autodev(const char *key, const char *value, |
c6883f38 SH |
1335 | struct lxc_conf *lxc_conf) |
1336 | { | |
1337 | int v = atoi(value); | |
1338 | ||
1339 | lxc_conf->autodev = v; | |
1340 | ||
1341 | return 0; | |
1342 | } | |
1343 | ||
a84b9932 AV |
1344 | static int sig_num(const char *sig) |
1345 | { | |
1346 | int n; | |
1347 | char *endp = NULL; | |
1348 | ||
1349 | errno = 0; | |
1350 | n = strtol(sig, &endp, 10); | |
1351 | if (sig == endp || n < 0 || errno != 0) | |
1352 | return -1; | |
1353 | return n; | |
1354 | } | |
1355 | ||
1356 | static int rt_sig_num(const char *signame) | |
1357 | { | |
1358 | int sig_n = 0; | |
1359 | int rtmax = 0; | |
1360 | ||
1361 | if (strncasecmp(signame, "max-", 4) == 0) { | |
1362 | rtmax = 1; | |
1363 | } | |
1364 | signame += 4; | |
1365 | if (!isdigit(*signame)) | |
1366 | return -1; | |
1367 | sig_n = sig_num(signame); | |
1368 | sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n; | |
1369 | if (sig_n > SIGRTMAX || sig_n < SIGRTMIN) | |
1370 | return -1; | |
1371 | return sig_n; | |
1372 | } | |
1373 | ||
1374 | static int sig_parse(const char *signame) { | |
84760c11 | 1375 | size_t n; |
a84b9932 AV |
1376 | |
1377 | if (isdigit(*signame)) { | |
1378 | return sig_num(signame); | |
1379 | } else if (strncasecmp(signame, "sig", 3) == 0) { | |
1380 | signame += 3; | |
1381 | if (strncasecmp(signame, "rt", 2) == 0) | |
1382 | return rt_sig_num(signame + 2); | |
1383 | for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++) { | |
1384 | if (strcasecmp (signames[n].name, signame) == 0) | |
1385 | return signames[n].num; | |
1386 | } | |
1387 | } | |
1388 | return -1; | |
1389 | } | |
1390 | ||
f0f1d8c0 DE |
1391 | static int config_haltsignal(const char *key, const char *value, |
1392 | struct lxc_conf *lxc_conf) | |
1393 | { | |
1394 | int sig_n = sig_parse(value); | |
1395 | ||
1396 | if (sig_n < 0) | |
1397 | return -1; | |
1398 | lxc_conf->haltsignal = sig_n; | |
1399 | ||
1400 | return 0; | |
1401 | } | |
1402 | ||
dd267776 BP |
1403 | static int config_rebootsignal(const char *key, const char *value, |
1404 | struct lxc_conf *lxc_conf) | |
1405 | { | |
1406 | int sig_n = sig_parse(value); | |
1407 | ||
1408 | if (sig_n < 0) | |
1409 | return -1; | |
1410 | lxc_conf->rebootsignal = sig_n; | |
1411 | ||
1412 | return 0; | |
1413 | } | |
1414 | ||
a84b9932 AV |
1415 | static int config_stopsignal(const char *key, const char *value, |
1416 | struct lxc_conf *lxc_conf) | |
1417 | { | |
1418 | int sig_n = sig_parse(value); | |
1419 | ||
1420 | if (sig_n < 0) | |
1421 | return -1; | |
1422 | lxc_conf->stopsignal = sig_n; | |
1423 | ||
1424 | return 0; | |
1425 | } | |
1426 | ||
12a50cc6 DE |
1427 | static int config_cgroup(const char *key, const char *value, |
1428 | struct lxc_conf *lxc_conf) | |
576f946d | 1429 | { |
1430 | char *token = "lxc.cgroup."; | |
1431 | char *subkey; | |
bf83c5b9 MS |
1432 | struct lxc_list *cglist = NULL; |
1433 | struct lxc_cgroup *cgelem = NULL; | |
576f946d | 1434 | |
7d0eb87e SH |
1435 | if (!value || strlen(value) == 0) |
1436 | return lxc_clear_cgroups(lxc_conf, key); | |
1437 | ||
576f946d | 1438 | subkey = strstr(key, token); |
1439 | ||
1440 | if (!subkey) | |
1441 | return -1; | |
1442 | ||
1443 | if (!strlen(subkey)) | |
1444 | return -1; | |
1445 | ||
1446 | if (strlen(subkey) == strlen(token)) | |
1447 | return -1; | |
a871ff6b | 1448 | |
576f946d | 1449 | subkey += strlen(token); |
1450 | ||
1451 | cglist = malloc(sizeof(*cglist)); | |
1452 | if (!cglist) | |
bf83c5b9 | 1453 | goto out; |
576f946d | 1454 | |
1455 | cgelem = malloc(sizeof(*cgelem)); | |
bf83c5b9 MS |
1456 | if (!cgelem) |
1457 | goto out; | |
1458 | memset(cgelem, 0, sizeof(*cgelem)); | |
576f946d | 1459 | |
1460 | cgelem->subsystem = strdup(subkey); | |
1461 | cgelem->value = strdup(value); | |
bf83c5b9 MS |
1462 | |
1463 | if (!cgelem->subsystem || !cgelem->value) | |
1464 | goto out; | |
1465 | ||
576f946d | 1466 | cglist->elem = cgelem; |
1467 | ||
94d12f0a | 1468 | lxc_list_add_tail(&lxc_conf->cgroup, cglist); |
576f946d | 1469 | |
1470 | return 0; | |
bf83c5b9 MS |
1471 | |
1472 | out: | |
f10fad2f | 1473 | free(cglist); |
bf83c5b9 MS |
1474 | |
1475 | if (cgelem) { | |
f10fad2f | 1476 | free(cgelem->subsystem); |
bf83c5b9 | 1477 | |
f10fad2f | 1478 | free(cgelem->value); |
bf83c5b9 MS |
1479 | |
1480 | free(cgelem); | |
1481 | } | |
1482 | ||
1483 | return -1; | |
576f946d | 1484 | } |
1485 | ||
f6d3e3e4 SH |
1486 | static int config_idmap(const char *key, const char *value, struct lxc_conf *lxc_conf) |
1487 | { | |
1488 | char *token = "lxc.id_map"; | |
1489 | char *subkey; | |
1490 | struct lxc_list *idmaplist = NULL; | |
1491 | struct id_map *idmap = NULL; | |
251d0d2a | 1492 | unsigned long hostid, nsid, range; |
f6d3e3e4 SH |
1493 | char type; |
1494 | int ret; | |
1495 | ||
7d0eb87e SH |
1496 | if (!value || strlen(value) == 0) |
1497 | return lxc_clear_idmaps(lxc_conf); | |
1498 | ||
f6d3e3e4 SH |
1499 | subkey = strstr(key, token); |
1500 | ||
1501 | if (!subkey) | |
1502 | return -1; | |
1503 | ||
1504 | if (!strlen(subkey)) | |
1505 | return -1; | |
1506 | ||
1507 | idmaplist = malloc(sizeof(*idmaplist)); | |
1508 | if (!idmaplist) | |
1509 | goto out; | |
1510 | ||
1511 | idmap = malloc(sizeof(*idmap)); | |
1512 | if (!idmap) | |
1513 | goto out; | |
1514 | memset(idmap, 0, sizeof(*idmap)); | |
1515 | ||
251d0d2a | 1516 | ret = sscanf(value, "%c %lu %lu %lu", &type, &nsid, &hostid, &range); |
f6d3e3e4 SH |
1517 | if (ret != 4) |
1518 | goto out; | |
7e60c3f0 | 1519 | |
251d0d2a | 1520 | INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range); |
ac7725e7 | 1521 | if (type == 'u') |
f6d3e3e4 | 1522 | idmap->idtype = ID_TYPE_UID; |
ac7725e7 | 1523 | else if (type == 'g') |
f6d3e3e4 SH |
1524 | idmap->idtype = ID_TYPE_GID; |
1525 | else | |
1526 | goto out; | |
7e60c3f0 | 1527 | |
f6d3e3e4 SH |
1528 | idmap->hostid = hostid; |
1529 | idmap->nsid = nsid; | |
1530 | idmap->range = range; | |
1531 | ||
7e60c3f0 SG |
1532 | idmaplist->elem = idmap; |
1533 | lxc_list_add_tail(&lxc_conf->id_map, idmaplist); | |
1534 | ||
f6d3e3e4 SH |
1535 | return 0; |
1536 | ||
1537 | out: | |
f10fad2f | 1538 | free(idmaplist); |
f6d3e3e4 SH |
1539 | |
1540 | if (idmap) { | |
1541 | free(idmap); | |
1542 | } | |
1543 | ||
1544 | return -1; | |
1545 | } | |
1546 | ||
d95db067 DE |
1547 | static int config_fstab(const char *key, const char *value, |
1548 | struct lxc_conf *lxc_conf) | |
1549 | { | |
d9192f5d SH |
1550 | if (!value || strlen(value) == 0) |
1551 | return -1; | |
6d03d92a | 1552 | return config_path_item(&lxc_conf->fstab, value); |
d95db067 DE |
1553 | } |
1554 | ||
368bbc02 CS |
1555 | static int config_mount_auto(const char *key, const char *value, |
1556 | struct lxc_conf *lxc_conf) | |
1557 | { | |
1558 | char *autos, *autoptr, *sptr, *token; | |
b06b8511 | 1559 | static struct { const char *token; int mask; int flag; } allowed_auto_mounts[] = { |
0769b82a CS |
1560 | { "proc", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED }, |
1561 | { "proc:mixed", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_MIXED }, | |
1562 | { "proc:rw", LXC_AUTO_PROC_MASK, LXC_AUTO_PROC_RW }, | |
f24a52d5 | 1563 | { "sys", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED }, |
0769b82a | 1564 | { "sys:ro", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RO }, |
f24a52d5 | 1565 | { "sys:mixed", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_MIXED }, |
0769b82a CS |
1566 | { "sys:rw", LXC_AUTO_SYS_MASK, LXC_AUTO_SYS_RW }, |
1567 | { "cgroup", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_NOSPEC }, | |
1568 | { "cgroup:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_MIXED }, | |
1569 | { "cgroup:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RO }, | |
1570 | { "cgroup:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_RW }, | |
1571 | { "cgroup-full", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_NOSPEC }, | |
1572 | { "cgroup-full:mixed", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_MIXED }, | |
1573 | { "cgroup-full:ro", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RO }, | |
1574 | { "cgroup-full:rw", LXC_AUTO_CGROUP_MASK, LXC_AUTO_CGROUP_FULL_RW }, | |
70f55bfd | 1575 | /* NB: For adding anything that is just a single on/off, but has |
b06b8511 CS |
1576 | * no options: keep mask and flag identical and just define the |
1577 | * enum value as an unused bit so far | |
1578 | */ | |
368bbc02 CS |
1579 | { NULL, 0 } |
1580 | }; | |
1581 | int i; | |
1582 | int ret = -1; | |
1583 | ||
d9192f5d SH |
1584 | if (!value || strlen(value) == 0) { |
1585 | lxc_conf->auto_mounts = 0; | |
1586 | return 0; | |
1587 | } | |
368bbc02 CS |
1588 | |
1589 | autos = strdup(value); | |
1590 | if (!autos) { | |
1591 | SYSERROR("failed to dup '%s'", value); | |
1592 | return -1; | |
1593 | } | |
1594 | ||
1595 | for (autoptr = autos; ; autoptr = NULL) { | |
d028235d SG |
1596 | token = strtok_r(autoptr, " \t", &sptr); |
1597 | if (!token) { | |
368bbc02 | 1598 | ret = 0; |
d028235d | 1599 | break; |
368bbc02 CS |
1600 | } |
1601 | ||
1602 | for (i = 0; allowed_auto_mounts[i].token; i++) { | |
1603 | if (!strcmp(allowed_auto_mounts[i].token, token)) | |
1604 | break; | |
1605 | } | |
1606 | ||
1607 | if (!allowed_auto_mounts[i].token) { | |
1608 | ERROR("Invalid filesystem to automount: %s", token); | |
1609 | break; | |
1610 | } | |
1611 | ||
b06b8511 | 1612 | lxc_conf->auto_mounts &= ~allowed_auto_mounts[i].mask; |
368bbc02 | 1613 | lxc_conf->auto_mounts |= allowed_auto_mounts[i].flag; |
d028235d | 1614 | } |
368bbc02 CS |
1615 | |
1616 | free(autos); | |
1617 | ||
1618 | return ret; | |
1619 | } | |
1620 | ||
12a50cc6 DE |
1621 | static int config_mount(const char *key, const char *value, |
1622 | struct lxc_conf *lxc_conf) | |
e7938e9e | 1623 | { |
e7938e9e MN |
1624 | char *mntelem; |
1625 | struct lxc_list *mntlist; | |
1626 | ||
d9192f5d SH |
1627 | if (!value || strlen(value) == 0) |
1628 | return lxc_clear_mount_entries(lxc_conf); | |
e7938e9e MN |
1629 | |
1630 | mntlist = malloc(sizeof(*mntlist)); | |
1631 | if (!mntlist) | |
1632 | return -1; | |
1633 | ||
1634 | mntelem = strdup(value); | |
00b6be44 SH |
1635 | if (!mntelem) { |
1636 | free(mntlist); | |
bf83c5b9 | 1637 | return -1; |
00b6be44 | 1638 | } |
e7938e9e MN |
1639 | mntlist->elem = mntelem; |
1640 | ||
1641 | lxc_list_add_tail(&lxc_conf->mount_list, mntlist); | |
1642 | ||
1643 | return 0; | |
1644 | } | |
1645 | ||
1fb86a7c SH |
1646 | static int config_cap_keep(const char *key, const char *value, |
1647 | struct lxc_conf *lxc_conf) | |
1648 | { | |
1649 | char *keepcaps, *keepptr, *sptr, *token; | |
1650 | struct lxc_list *keeplist; | |
1651 | int ret = -1; | |
1652 | ||
1653 | if (!strlen(value)) | |
7d0eb87e | 1654 | return lxc_clear_config_keepcaps(lxc_conf); |
1fb86a7c SH |
1655 | |
1656 | keepcaps = strdup(value); | |
1657 | if (!keepcaps) { | |
1658 | SYSERROR("failed to dup '%s'", value); | |
1659 | return -1; | |
1660 | } | |
1661 | ||
1662 | /* in case several capability keep is specified in a single line | |
1663 | * split these caps in a single element for the list */ | |
1664 | for (keepptr = keepcaps;;keepptr = NULL) { | |
d028235d SG |
1665 | token = strtok_r(keepptr, " \t", &sptr); |
1666 | if (!token) { | |
1fb86a7c | 1667 | ret = 0; |
d028235d | 1668 | break; |
1fb86a7c SH |
1669 | } |
1670 | ||
7035407c DE |
1671 | if (!strcmp(token, "none")) |
1672 | lxc_clear_config_keepcaps(lxc_conf); | |
1673 | ||
1fb86a7c SH |
1674 | keeplist = malloc(sizeof(*keeplist)); |
1675 | if (!keeplist) { | |
1676 | SYSERROR("failed to allocate keepcap list"); | |
1677 | break; | |
1678 | } | |
1679 | ||
1680 | keeplist->elem = strdup(token); | |
1681 | if (!keeplist->elem) { | |
1682 | SYSERROR("failed to dup '%s'", token); | |
1683 | free(keeplist); | |
1684 | break; | |
1685 | } | |
1686 | ||
1687 | lxc_list_add_tail(&lxc_conf->keepcaps, keeplist); | |
d028235d | 1688 | } |
1fb86a7c SH |
1689 | |
1690 | free(keepcaps); | |
1691 | ||
1692 | return ret; | |
1693 | } | |
1694 | ||
12a50cc6 | 1695 | static int config_cap_drop(const char *key, const char *value, |
81810dd1 DL |
1696 | struct lxc_conf *lxc_conf) |
1697 | { | |
d95db067 | 1698 | char *dropcaps, *dropptr, *sptr, *token; |
81810dd1 DL |
1699 | struct lxc_list *droplist; |
1700 | int ret = -1; | |
1701 | ||
1702 | if (!strlen(value)) | |
7d0eb87e | 1703 | return lxc_clear_config_caps(lxc_conf); |
81810dd1 DL |
1704 | |
1705 | dropcaps = strdup(value); | |
1706 | if (!dropcaps) { | |
1707 | SYSERROR("failed to dup '%s'", value); | |
1708 | return -1; | |
1709 | } | |
1710 | ||
1711 | /* in case several capability drop is specified in a single line | |
1712 | * split these caps in a single element for the list */ | |
d95db067 | 1713 | for (dropptr = dropcaps;;dropptr = NULL) { |
d028235d SG |
1714 | token = strtok_r(dropptr, " \t", &sptr); |
1715 | if (!token) { | |
81810dd1 | 1716 | ret = 0; |
d028235d | 1717 | break; |
81810dd1 | 1718 | } |
81810dd1 DL |
1719 | |
1720 | droplist = malloc(sizeof(*droplist)); | |
1721 | if (!droplist) { | |
1722 | SYSERROR("failed to allocate drop list"); | |
1723 | break; | |
1724 | } | |
1725 | ||
1726 | droplist->elem = strdup(token); | |
1727 | if (!droplist->elem) { | |
1728 | SYSERROR("failed to dup '%s'", token); | |
1729 | free(droplist); | |
1730 | break; | |
1731 | } | |
1732 | ||
1733 | lxc_list_add_tail(&lxc_conf->caps, droplist); | |
d028235d | 1734 | } |
81810dd1 DL |
1735 | |
1736 | free(dropcaps); | |
1737 | ||
1738 | return ret; | |
1739 | } | |
1740 | ||
12a50cc6 | 1741 | static int config_console(const char *key, const char *value, |
28a4b0e5 DL |
1742 | struct lxc_conf *lxc_conf) |
1743 | { | |
6d03d92a | 1744 | return config_path_item(&lxc_conf->console.path, value); |
28a4b0e5 DL |
1745 | } |
1746 | ||
96f15ca1 SH |
1747 | static int config_console_logfile(const char *key, const char *value, |
1748 | struct lxc_conf *lxc_conf) | |
1749 | { | |
1750 | return config_path_item(&lxc_conf->console.log_path, value); | |
1751 | } | |
1752 | ||
e6744e9b SH |
1753 | /* |
1754 | * If we find a lxc.network.hwaddr in the original config file, | |
1755 | * we expand it in the unexpanded_config, so that after a save_config | |
1756 | * we store the hwaddr for re-use. | |
1757 | * This is only called when reading the config file, not when executing | |
1758 | * a lxc.include. | |
1759 | * 'x' and 'X' are substituted in-place. | |
1760 | */ | |
1761 | static void update_hwaddr(const char *line) | |
1762 | { | |
1763 | char *p; | |
1764 | ||
1765 | line += lxc_char_left_gc(line, strlen(line)); | |
1766 | if (line[0] == '#') | |
1767 | return; | |
1768 | if (strncmp(line, "lxc.network.hwaddr", 18) != 0) | |
1769 | return; | |
1770 | p = strchr(line, '='); | |
1771 | if (!p) | |
1772 | return; // let config_network_hwaddr raise the error | |
1773 | p++; | |
1774 | while (isblank(*p)) | |
1775 | p++; | |
1776 | if (!*p) | |
1777 | return; | |
1778 | ||
1779 | rand_complete_hwaddr(p); | |
1780 | } | |
1781 | ||
6b0d5538 | 1782 | int append_unexp_config_line(const char *line, struct lxc_conf *conf) |
f979ac15 | 1783 | { |
6b0d5538 | 1784 | size_t len = conf->unexpanded_len, linelen = strlen(line); |
f979ac15 | 1785 | |
e6744e9b SH |
1786 | update_hwaddr(line); |
1787 | ||
6b0d5538 SH |
1788 | while (conf->unexpanded_alloced <= len + linelen + 2) { |
1789 | char *tmp = realloc(conf->unexpanded_config, conf->unexpanded_alloced + 1024); | |
1790 | if (!tmp) | |
1791 | return -1; | |
1792 | if (!conf->unexpanded_config) | |
1793 | *tmp = '\0'; | |
1794 | conf->unexpanded_config = tmp; | |
1795 | conf->unexpanded_alloced += 1024; | |
1796 | } | |
1797 | strcat(conf->unexpanded_config, line); | |
1798 | conf->unexpanded_len += linelen; | |
1799 | if (line[linelen-1] != '\n') { | |
1800 | strcat(conf->unexpanded_config, "\n"); | |
1801 | conf->unexpanded_len++; | |
f979ac15 | 1802 | } |
f979ac15 SH |
1803 | return 0; |
1804 | } | |
1805 | ||
e1daebd9 SH |
1806 | static int do_includedir(const char *dirp, struct lxc_conf *lxc_conf) |
1807 | { | |
74f96976 | 1808 | struct dirent *direntp; |
e1daebd9 SH |
1809 | DIR *dir; |
1810 | char path[MAXPATHLEN]; | |
1811 | int ret = -1, len; | |
1812 | ||
1813 | dir = opendir(dirp); | |
1814 | if (!dir) { | |
1815 | SYSERROR("failed to open '%s'", dirp); | |
1816 | return -1; | |
1817 | } | |
1818 | ||
74f96976 | 1819 | while ((direntp = readdir(dir))) { |
e1daebd9 SH |
1820 | const char *fnam; |
1821 | if (!direntp) | |
1822 | break; | |
1823 | ||
1824 | fnam = direntp->d_name; | |
1825 | if (!strcmp(fnam, ".")) | |
1826 | continue; | |
1827 | ||
1828 | if (!strcmp(fnam, "..")) | |
1829 | continue; | |
1830 | ||
1831 | len = strlen(fnam); | |
1832 | if (len < 6 || strncmp(fnam+len-5, ".conf", 5) != 0) | |
1833 | continue; | |
1834 | len = snprintf(path, MAXPATHLEN, "%s/%s", dirp, fnam); | |
1835 | if (len < 0 || len >= MAXPATHLEN) { | |
1836 | ERROR("lxc.include filename too long under '%s'", dirp); | |
1837 | ret = -1; | |
1838 | goto out; | |
1839 | } | |
1840 | ||
1841 | ret = lxc_config_read(path, lxc_conf, true); | |
1842 | if (ret < 0) | |
1843 | goto out; | |
1844 | } | |
1845 | ret = 0; | |
1846 | ||
1847 | out: | |
1848 | if (closedir(dir)) | |
1849 | WARN("lxc.include dir: failed to close directory"); | |
1850 | ||
1851 | return ret; | |
1852 | } | |
1853 | ||
12a50cc6 | 1854 | static int config_includefile(const char *key, const char *value, |
09ad6246 SH |
1855 | struct lxc_conf *lxc_conf) |
1856 | { | |
e1daebd9 SH |
1857 | if (is_dir(value)) |
1858 | return do_includedir(value, lxc_conf); | |
1859 | ||
6b0d5538 | 1860 | return lxc_config_read(value, lxc_conf, true); |
09ad6246 SH |
1861 | } |
1862 | ||
12a50cc6 DE |
1863 | static int config_rootfs(const char *key, const char *value, |
1864 | struct lxc_conf *lxc_conf) | |
c2cc9f0a | 1865 | { |
6d03d92a | 1866 | return config_path_item(&lxc_conf->rootfs.path, value); |
c2cc9f0a | 1867 | } |
1868 | ||
12a50cc6 DE |
1869 | static int config_rootfs_mount(const char *key, const char *value, |
1870 | struct lxc_conf *lxc_conf) | |
23b7ea69 | 1871 | { |
6d03d92a | 1872 | return config_path_item(&lxc_conf->rootfs.mount, value); |
23b7ea69 DL |
1873 | } |
1874 | ||
a17b1e65 SG |
1875 | static int config_rootfs_options(const char *key, const char *value, |
1876 | struct lxc_conf *lxc_conf) | |
1877 | { | |
1878 | return config_string_item(&lxc_conf->rootfs.options, value); | |
1879 | } | |
1880 | ||
327a1e78 | 1881 | static int config_rootfs_backend(const char *key, const char *value, |
bfd77214 SH |
1882 | struct lxc_conf *lxc_conf) |
1883 | { | |
6a21d4ae SH |
1884 | if (strlen(value) == 0) { |
1885 | free(lxc_conf->rootfs.bdev_type); | |
1886 | lxc_conf->rootfs.bdev_type = NULL; | |
1887 | } | |
bfd77214 | 1888 | if (!is_valid_bdev_type(value)) { |
327a1e78 | 1889 | ERROR("Bad rootfs.backend: '%s'", value); |
bfd77214 SH |
1890 | return -1; |
1891 | } | |
1892 | ||
b3b8c97f | 1893 | return config_string_item(&lxc_conf->rootfs.bdev_type, value); |
bfd77214 SH |
1894 | } |
1895 | ||
12a50cc6 DE |
1896 | static int config_pivotdir(const char *key, const char *value, |
1897 | struct lxc_conf *lxc_conf) | |
bf601689 | 1898 | { |
2d489f9e | 1899 | WARN("lxc.pivotdir is ignored. It will soon become an error."); |
59bb8698 | 1900 | return 0; |
bf601689 MH |
1901 | } |
1902 | ||
12a50cc6 DE |
1903 | static int config_utsname(const char *key, const char *value, |
1904 | struct lxc_conf *lxc_conf) | |
c2cc9f0a | 1905 | { |
1906 | struct utsname *utsname; | |
1907 | ||
1908 | utsname = malloc(sizeof(*utsname)); | |
1909 | if (!utsname) { | |
36eb9bde | 1910 | SYSERROR("failed to allocate memory"); |
c2cc9f0a | 1911 | return -1; |
1912 | } | |
1913 | ||
1914 | if (strlen(value) >= sizeof(utsname->nodename)) { | |
36eb9bde | 1915 | ERROR("node name '%s' is too long", |
16e29c91 | 1916 | value); |
b6f24d54 | 1917 | free(utsname); |
c2cc9f0a | 1918 | return -1; |
1919 | } | |
1920 | ||
1921 | strcpy(utsname->nodename, value); | |
f10fad2f | 1922 | free(lxc_conf->utsname); |
c2cc9f0a | 1923 | lxc_conf->utsname = utsname; |
1924 | ||
1925 | return 0; | |
1926 | } | |
1927 | ||
6b0d5538 SH |
1928 | struct parse_line_conf { |
1929 | struct lxc_conf *conf; | |
1930 | bool from_include; | |
1931 | }; | |
4184c3e1 | 1932 | |
7a7ff0c6 | 1933 | static int parse_line(char *buffer, void *data) |
c2cc9f0a | 1934 | { |
72d0e1cb | 1935 | struct lxc_config_t *config; |
81192358 | 1936 | char *line, *linep; |
c2cc9f0a | 1937 | char *dot; |
1938 | char *key; | |
1939 | char *value; | |
476d4cf1 | 1940 | int ret = 0; |
6b0d5538 | 1941 | struct parse_line_conf *plc = data; |
c2cc9f0a | 1942 | |
91480a0f | 1943 | if (lxc_is_line_empty(buffer)) |
c2cc9f0a | 1944 | return 0; |
1945 | ||
81192358 DL |
1946 | /* we have to dup the buffer otherwise, at the re-exec for |
1947 | * reboot we modified the original string on the stack by | |
1948 | * replacing '=' by '\0' below | |
91480a0f | 1949 | */ |
81192358 | 1950 | linep = line = strdup(buffer); |
91480a0f DL |
1951 | if (!line) { |
1952 | SYSERROR("failed to allocate memory for '%s'", buffer); | |
81192358 | 1953 | return -1; |
91480a0f DL |
1954 | } |
1955 | ||
6b0d5538 SH |
1956 | if (!plc->from_include) |
1957 | if ((ret = append_unexp_config_line(line, plc->conf))) | |
1958 | goto out; | |
1959 | ||
b2718c72 | 1960 | line += lxc_char_left_gc(line, strlen(line)); |
476d4cf1 | 1961 | |
4184c3e1 SH |
1962 | /* ignore comments */ |
1963 | if (line[0] == '#') | |
91480a0f | 1964 | goto out; |
476d4cf1 | 1965 | |
6b0d5538 SH |
1966 | /* martian option - don't add it to the config itself */ |
1967 | if (strncmp(line, "lxc.", 4)) | |
4184c3e1 | 1968 | goto out; |
4184c3e1 | 1969 | |
476d4cf1 | 1970 | ret = -1; |
c2cc9f0a | 1971 | |
b2718c72 | 1972 | dot = strstr(line, "="); |
c2cc9f0a | 1973 | if (!dot) { |
36eb9bde | 1974 | ERROR("invalid configuration line: %s", line); |
91480a0f | 1975 | goto out; |
c2cc9f0a | 1976 | } |
a871ff6b | 1977 | |
c2cc9f0a | 1978 | *dot = '\0'; |
1979 | value = dot + 1; | |
1980 | ||
b2718c72 | 1981 | key = line; |
1982 | key[lxc_char_right_gc(key, strlen(key))] = '\0'; | |
c2cc9f0a | 1983 | |
b2718c72 | 1984 | value += lxc_char_left_gc(value, strlen(value)); |
1985 | value[lxc_char_right_gc(value, strlen(value))] = '\0'; | |
c2cc9f0a | 1986 | |
bd878dee SB |
1987 | if (*value == '\'' || *value == '\"') { |
1988 | size_t len = strlen(value); | |
1989 | if (len > 1 && value[len-1] == *value) { | |
1990 | value[len-1] = '\0'; | |
1991 | value++; | |
1992 | } | |
1993 | } | |
1994 | ||
72d0e1cb | 1995 | config = lxc_getconfig(key); |
c2cc9f0a | 1996 | if (!config) { |
6e1d9b94 | 1997 | ERROR("unknown key %s", key); |
91480a0f | 1998 | goto out; |
c2cc9f0a | 1999 | } |
2000 | ||
6b0d5538 | 2001 | ret = config->cb(key, value, plc->conf); |
91480a0f DL |
2002 | |
2003 | out: | |
81192358 | 2004 | free(linep); |
91480a0f | 2005 | return ret; |
c2cc9f0a | 2006 | } |
2007 | ||
74a3920a | 2008 | static int lxc_config_readline(char *buffer, struct lxc_conf *conf) |
af5b0155 | 2009 | { |
6b0d5538 SH |
2010 | struct parse_line_conf c; |
2011 | ||
2012 | c.conf = conf; | |
2013 | c.from_include = false; | |
2014 | ||
2015 | return parse_line(buffer, &c); | |
af5b0155 CLG |
2016 | } |
2017 | ||
6b0d5538 | 2018 | int lxc_config_read(const char *file, struct lxc_conf *conf, bool from_include) |
c2cc9f0a | 2019 | { |
6b0d5538 SH |
2020 | struct parse_line_conf c; |
2021 | ||
2022 | c.conf = conf; | |
2023 | c.from_include = from_include; | |
f979ac15 | 2024 | |
f3ca99fd SH |
2025 | if( access(file, R_OK) == -1 ) { |
2026 | return -1; | |
2027 | } | |
6b0d5538 | 2028 | |
f7bee6c6 | 2029 | /* Catch only the top level config file name in the structure */ |
76d0127f CB |
2030 | if(!conf->rcfile) |
2031 | conf->rcfile = strdup(file); | |
f979ac15 | 2032 | |
6b0d5538 | 2033 | return lxc_file_for_each_line(file, parse_line, &c); |
c2cc9f0a | 2034 | } |
62e46035 CLG |
2035 | |
2036 | int lxc_config_define_add(struct lxc_list *defines, char* arg) | |
2037 | { | |
2038 | struct lxc_list *dent; | |
2039 | ||
2040 | dent = malloc(sizeof(struct lxc_list)); | |
2041 | if (!dent) | |
2042 | return -1; | |
2043 | ||
2044 | dent->elem = arg; | |
2045 | lxc_list_add_tail(defines, dent); | |
2046 | return 0; | |
2047 | } | |
2048 | ||
226a18d6 | 2049 | int lxc_config_define_load(struct lxc_list *defines, struct lxc_conf *conf) |
62e46035 | 2050 | { |
9ebb03ad | 2051 | struct lxc_list *it,*next; |
62e46035 CLG |
2052 | int ret = 0; |
2053 | ||
2054 | lxc_list_for_each(it, defines) { | |
2055 | ret = lxc_config_readline(it->elem, conf); | |
2056 | if (ret) | |
2057 | break; | |
2058 | } | |
2059 | ||
9ebb03ad | 2060 | lxc_list_for_each_safe(it, defines, next) { |
62e46035 CLG |
2061 | lxc_list_del(it); |
2062 | free(it); | |
2063 | } | |
2064 | ||
2065 | return ret; | |
2066 | } | |
525f0002 CS |
2067 | |
2068 | signed long lxc_config_parse_arch(const char *arch) | |
2069 | { | |
6ff05e18 | 2070 | #if HAVE_SYS_PERSONALITY_H |
525f0002 CS |
2071 | struct per_name { |
2072 | char *name; | |
2073 | unsigned long per; | |
bb8d8207 | 2074 | } pername[] = { |
525f0002 | 2075 | { "x86", PER_LINUX32 }, |
bb8d8207 DE |
2076 | { "linux32", PER_LINUX32 }, |
2077 | { "i386", PER_LINUX32 }, | |
2078 | { "i486", PER_LINUX32 }, | |
2079 | { "i586", PER_LINUX32 }, | |
525f0002 | 2080 | { "i686", PER_LINUX32 }, |
bb8d8207 | 2081 | { "athlon", PER_LINUX32 }, |
e319eb34 JC |
2082 | { "mips", PER_LINUX32 }, |
2083 | { "mipsel", PER_LINUX32 }, | |
08245e8c CB |
2084 | { "ppc", PER_LINUX32 }, |
2085 | { "arm", PER_LINUX32 }, | |
2086 | { "armv7l", PER_LINUX32 }, | |
2087 | { "armhf", PER_LINUX32 }, | |
2088 | { "armel", PER_LINUX32 }, | |
2089 | { "powerpc", PER_LINUX32 }, | |
bb8d8207 | 2090 | { "linux64", PER_LINUX }, |
525f0002 CS |
2091 | { "x86_64", PER_LINUX }, |
2092 | { "amd64", PER_LINUX }, | |
e319eb34 JC |
2093 | { "mips64", PER_LINUX }, |
2094 | { "mips64el", PER_LINUX }, | |
08245e8c CB |
2095 | { "ppc64", PER_LINUX }, |
2096 | { "ppc64le", PER_LINUX }, | |
2097 | { "ppc64el", PER_LINUX }, | |
2098 | { "powerpc64", PER_LINUX }, | |
2099 | { "s390x", PER_LINUX }, | |
2100 | { "aarch64", PER_LINUX }, | |
2101 | { "arm64", PER_LINUX }, | |
525f0002 CS |
2102 | }; |
2103 | size_t len = sizeof(pername) / sizeof(pername[0]); | |
2104 | ||
84760c11 | 2105 | size_t i; |
525f0002 CS |
2106 | |
2107 | for (i = 0; i < len; i++) { | |
2108 | if (!strcmp(pername[i].name, arch)) | |
2109 | return pername[i].per; | |
2110 | } | |
6ff05e18 | 2111 | #endif |
525f0002 CS |
2112 | |
2113 | return -1; | |
2114 | } | |
72d0e1cb | 2115 | |
4d69b293 NK |
2116 | int lxc_fill_elevated_privileges(char *flaglist, int *flags) |
2117 | { | |
2118 | char *token, *saveptr = NULL; | |
2119 | int i, aflag; | |
2120 | struct { const char *token; int flag; } all_privs[] = { | |
2121 | { "CGROUP", LXC_ATTACH_MOVE_TO_CGROUP }, | |
2122 | { "CAP", LXC_ATTACH_DROP_CAPABILITIES }, | |
2123 | { "LSM", LXC_ATTACH_LSM_EXEC }, | |
2124 | { NULL, 0 } | |
2125 | }; | |
2126 | ||
2127 | if (!flaglist) { | |
2128 | /* for the sake of backward compatibility, drop all privileges | |
2129 | if none is specified */ | |
2130 | for (i = 0; all_privs[i].token; i++) { | |
d028235d | 2131 | *flags |= all_privs[i].flag; |
4d69b293 NK |
2132 | } |
2133 | return 0; | |
2134 | } | |
2135 | ||
2136 | token = strtok_r(flaglist, "|", &saveptr); | |
2137 | while (token) { | |
2138 | aflag = -1; | |
2139 | for (i = 0; all_privs[i].token; i++) { | |
2140 | if (!strcmp(all_privs[i].token, token)) | |
2141 | aflag = all_privs[i].flag; | |
2142 | } | |
2143 | if (aflag < 0) | |
2144 | return -1; | |
2145 | ||
2146 | *flags |= aflag; | |
2147 | ||
2148 | token = strtok_r(NULL, "|", &saveptr); | |
2149 | } | |
2150 | return 0; | |
2151 | } | |
2152 | ||
72d0e1cb SG |
2153 | static int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v) |
2154 | { | |
2155 | if (!retv) | |
2156 | inlen = 0; | |
2157 | else | |
2158 | memset(retv, 0, inlen); | |
2159 | return snprintf(retv, inlen, "%d", v); | |
2160 | } | |
2161 | ||
2162 | static int lxc_get_arch_entry(struct lxc_conf *c, char *retv, int inlen) | |
2163 | { | |
6ff05e18 | 2164 | int fulllen = 0; |
72d0e1cb SG |
2165 | |
2166 | if (!retv) | |
2167 | inlen = 0; | |
2168 | else | |
2169 | memset(retv, 0, inlen); | |
2170 | ||
6ff05e18 SG |
2171 | #if HAVE_SYS_PERSONALITY_H |
2172 | int len = 0; | |
2173 | ||
72d0e1cb | 2174 | switch(c->personality) { |
14622799 | 2175 | case PER_LINUX32: strprint(retv, inlen, "i686"); break; |
72d0e1cb SG |
2176 | case PER_LINUX: strprint(retv, inlen, "x86_64"); break; |
2177 | default: break; | |
2178 | } | |
6ff05e18 | 2179 | #endif |
72d0e1cb SG |
2180 | |
2181 | return fulllen; | |
2182 | } | |
2183 | ||
2184 | /* | |
2185 | * If you ask for a specific cgroup value, i.e. lxc.cgroup.devices.list, | |
2186 | * then just the value(s) will be printed. Since there still could be | |
2187 | * more than one, it is newline-separated. | |
2188 | * (Maybe that's ambigous, since some values, i.e. devices.list, will | |
2189 | * already have newlines?) | |
2190 | * If you ask for 'lxc.cgroup", then all cgroup entries will be printed, | |
2191 | * in 'lxc.cgroup.subsystem.key = value' format. | |
2192 | */ | |
12a50cc6 DE |
2193 | static int lxc_get_cgroup_entry(struct lxc_conf *c, char *retv, int inlen, |
2194 | const char *key) | |
72d0e1cb SG |
2195 | { |
2196 | int fulllen = 0, len; | |
2197 | int all = 0; | |
2198 | struct lxc_list *it; | |
2199 | ||
2200 | if (!retv) | |
2201 | inlen = 0; | |
2202 | else | |
2203 | memset(retv, 0, inlen); | |
2204 | ||
2205 | if (strcmp(key, "all") == 0) | |
2206 | all = 1; | |
2207 | ||
2208 | lxc_list_for_each(it, &c->cgroup) { | |
2209 | struct lxc_cgroup *cg = it->elem; | |
2210 | if (all) { | |
2211 | strprint(retv, inlen, "lxc.cgroup.%s = %s\n", cg->subsystem, cg->value); | |
2212 | } else if (strcmp(cg->subsystem, key) == 0) { | |
2213 | strprint(retv, inlen, "%s\n", cg->value); | |
2214 | } | |
2215 | } | |
2216 | return fulllen; | |
2217 | } | |
2218 | ||
12a50cc6 DE |
2219 | static int lxc_get_item_hooks(struct lxc_conf *c, char *retv, int inlen, |
2220 | const char *key) | |
72d0e1cb SG |
2221 | { |
2222 | char *subkey; | |
2223 | int len, fulllen = 0, found = -1; | |
2224 | struct lxc_list *it; | |
2225 | int i; | |
2226 | ||
2227 | /* "lxc.hook.mount" */ | |
46cd2845 PL |
2228 | subkey = strchr(key, '.'); |
2229 | if (subkey) subkey = strchr(subkey+1, '.'); | |
72d0e1cb SG |
2230 | if (!subkey) |
2231 | return -1; | |
2232 | subkey++; | |
2233 | if (!*subkey) | |
2234 | return -1; | |
2235 | for (i=0; i<NUM_LXC_HOOKS; i++) { | |
2236 | if (strcmp(lxchook_names[i], subkey) == 0) { | |
2237 | found=i; | |
2238 | break; | |
2239 | } | |
2240 | } | |
2241 | if (found == -1) | |
2242 | return -1; | |
2243 | ||
2244 | if (!retv) | |
2245 | inlen = 0; | |
2246 | else | |
2247 | memset(retv, 0, inlen); | |
2248 | ||
2249 | lxc_list_for_each(it, &c->hooks[found]) { | |
2250 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2251 | } | |
2252 | return fulllen; | |
2253 | } | |
2254 | ||
ee1e7aa0 SG |
2255 | static int lxc_get_item_groups(struct lxc_conf *c, char *retv, int inlen) |
2256 | { | |
2257 | int len, fulllen = 0; | |
2258 | struct lxc_list *it; | |
2259 | ||
2260 | if (!retv) | |
2261 | inlen = 0; | |
2262 | else | |
2263 | memset(retv, 0, inlen); | |
2264 | ||
2265 | lxc_list_for_each(it, &c->groups) { | |
2266 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2267 | } | |
2268 | return fulllen; | |
2269 | } | |
2270 | ||
ab799c0b SG |
2271 | static int lxc_get_item_environment(struct lxc_conf *c, char *retv, int inlen) |
2272 | { | |
2273 | int len, fulllen = 0; | |
2274 | struct lxc_list *it; | |
2275 | ||
2276 | if (!retv) | |
2277 | inlen = 0; | |
2278 | else | |
2279 | memset(retv, 0, inlen); | |
2280 | ||
2281 | lxc_list_for_each(it, &c->environment) { | |
2282 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2283 | } | |
2284 | return fulllen; | |
2285 | } | |
2286 | ||
72d0e1cb SG |
2287 | static int lxc_get_item_cap_drop(struct lxc_conf *c, char *retv, int inlen) |
2288 | { | |
2289 | int len, fulllen = 0; | |
2290 | struct lxc_list *it; | |
2291 | ||
2292 | if (!retv) | |
2293 | inlen = 0; | |
2294 | else | |
2295 | memset(retv, 0, inlen); | |
2296 | ||
2297 | lxc_list_for_each(it, &c->caps) { | |
2298 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2299 | } | |
2300 | return fulllen; | |
2301 | } | |
2302 | ||
1fb86a7c SH |
2303 | static int lxc_get_item_cap_keep(struct lxc_conf *c, char *retv, int inlen) |
2304 | { | |
2305 | int len, fulllen = 0; | |
2306 | struct lxc_list *it; | |
2307 | ||
2308 | if (!retv) | |
2309 | inlen = 0; | |
2310 | else | |
2311 | memset(retv, 0, inlen); | |
2312 | ||
2313 | lxc_list_for_each(it, &c->keepcaps) { | |
2314 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2315 | } | |
2316 | return fulllen; | |
2317 | } | |
2318 | ||
72d0e1cb SG |
2319 | static int lxc_get_mount_entries(struct lxc_conf *c, char *retv, int inlen) |
2320 | { | |
2321 | int len, fulllen = 0; | |
2322 | struct lxc_list *it; | |
2323 | ||
2324 | if (!retv) | |
2325 | inlen = 0; | |
2326 | else | |
2327 | memset(retv, 0, inlen); | |
2328 | ||
2329 | lxc_list_for_each(it, &c->mount_list) { | |
2330 | strprint(retv, inlen, "%s\n", (char *)it->elem); | |
2331 | } | |
2332 | return fulllen; | |
2333 | } | |
2334 | ||
b099e9e9 SH |
2335 | static int lxc_get_auto_mounts(struct lxc_conf *c, char *retv, int inlen) |
2336 | { | |
2337 | int len, fulllen = 0; | |
0769b82a | 2338 | const char *sep = ""; |
b099e9e9 SH |
2339 | |
2340 | if (!retv) | |
2341 | inlen = 0; | |
2342 | else | |
2343 | memset(retv, 0, inlen); | |
2344 | ||
2345 | if (!(c->auto_mounts & LXC_AUTO_ALL_MASK)) | |
2346 | return 0; | |
2347 | ||
2348 | switch (c->auto_mounts & LXC_AUTO_PROC_MASK) { | |
0769b82a CS |
2349 | case LXC_AUTO_PROC_MIXED: strprint(retv, inlen, "%sproc:mixed", sep); sep = " "; break; |
2350 | case LXC_AUTO_PROC_RW: strprint(retv, inlen, "%sproc:rw", sep); sep = " "; break; | |
b099e9e9 SH |
2351 | default: break; |
2352 | } | |
2353 | switch (c->auto_mounts & LXC_AUTO_SYS_MASK) { | |
0769b82a CS |
2354 | case LXC_AUTO_SYS_RO: strprint(retv, inlen, "%ssys:ro", sep); sep = " "; break; |
2355 | case LXC_AUTO_SYS_RW: strprint(retv, inlen, "%ssys:rw", sep); sep = " "; break; | |
10f27710 | 2356 | case LXC_AUTO_SYS_MIXED: strprint(retv, inlen, "%ssys:mixed", sep); sep = " "; break; |
b099e9e9 SH |
2357 | default: break; |
2358 | } | |
2359 | switch (c->auto_mounts & LXC_AUTO_CGROUP_MASK) { | |
0769b82a CS |
2360 | case LXC_AUTO_CGROUP_NOSPEC: strprint(retv, inlen, "%scgroup", sep); sep = " "; break; |
2361 | case LXC_AUTO_CGROUP_MIXED: strprint(retv, inlen, "%scgroup:mixed", sep); sep = " "; break; | |
2362 | case LXC_AUTO_CGROUP_RO: strprint(retv, inlen, "%scgroup:ro", sep); sep = " "; break; | |
2363 | case LXC_AUTO_CGROUP_RW: strprint(retv, inlen, "%scgroup:rw", sep); sep = " "; break; | |
2364 | case LXC_AUTO_CGROUP_FULL_NOSPEC: strprint(retv, inlen, "%scgroup-full", sep); sep = " "; break; | |
2365 | case LXC_AUTO_CGROUP_FULL_MIXED: strprint(retv, inlen, "%scgroup-full:mixed", sep); sep = " "; break; | |
2366 | case LXC_AUTO_CGROUP_FULL_RO: strprint(retv, inlen, "%scgroup-full:ro", sep); sep = " "; break; | |
2367 | case LXC_AUTO_CGROUP_FULL_RW: strprint(retv, inlen, "%scgroup-full:rw", sep); sep = " "; break; | |
b099e9e9 SH |
2368 | default: break; |
2369 | } | |
0769b82a | 2370 | |
b099e9e9 SH |
2371 | return fulllen; |
2372 | } | |
2373 | ||
72d0e1cb SG |
2374 | /* |
2375 | * lxc.network.0.XXX, where XXX can be: name, type, link, flags, type, | |
8fc8295a | 2376 | * macvlan.mode, veth.pair, vlan, ipv4, ipv6, script.up, hwaddr, mtu, |
9eaf8a59 | 2377 | * ipv4.gateway, ipv6.gateway. ipvX.gateway can return 'auto' instead |
72d0e1cb SG |
2378 | * of an address. ipv4 and ipv6 return lists (newline-separated). |
2379 | * things like veth.pair return '' if invalid (i.e. if called for vlan | |
2380 | * type). | |
2381 | */ | |
12a50cc6 DE |
2382 | static int lxc_get_item_nic(struct lxc_conf *c, char *retv, int inlen, |
2383 | const char *key) | |
72d0e1cb SG |
2384 | { |
2385 | char *p1; | |
fe88b9d2 | 2386 | int len, fulllen = 0; |
72d0e1cb SG |
2387 | struct lxc_netdev *netdev; |
2388 | ||
2389 | if (!retv) | |
2390 | inlen = 0; | |
2391 | else | |
2392 | memset(retv, 0, inlen); | |
2393 | ||
46cd2845 | 2394 | p1 = strchr(key, '.'); |
72d0e1cb SG |
2395 | if (!p1 || *(p1+1) == '\0') return -1; |
2396 | p1++; | |
2397 | ||
2398 | netdev = get_netdev_from_key(key, &c->network); | |
2399 | if (!netdev) | |
2400 | return -1; | |
2401 | if (strcmp(p1, "name") == 0) { | |
2402 | if (netdev->name) | |
2403 | strprint(retv, inlen, "%s", netdev->name); | |
2404 | } else if (strcmp(p1, "type") == 0) { | |
2405 | strprint(retv, inlen, "%s", lxc_net_type_to_str(netdev->type)); | |
2406 | } else if (strcmp(p1, "link") == 0) { | |
2407 | if (netdev->link) | |
2408 | strprint(retv, inlen, "%s", netdev->link); | |
2409 | } else if (strcmp(p1, "flags") == 0) { | |
2410 | if (netdev->flags & IFF_UP) | |
2411 | strprint(retv, inlen, "up"); | |
8fc8295a | 2412 | } else if (strcmp(p1, "script.up") == 0) { |
72d0e1cb SG |
2413 | if (netdev->upscript) |
2414 | strprint(retv, inlen, "%s", netdev->upscript); | |
8fc8295a DE |
2415 | } else if (strcmp(p1, "script.down") == 0) { |
2416 | if (netdev->downscript) | |
2417 | strprint(retv, inlen, "%s", netdev->downscript); | |
72d0e1cb SG |
2418 | } else if (strcmp(p1, "hwaddr") == 0) { |
2419 | if (netdev->hwaddr) | |
2420 | strprint(retv, inlen, "%s", netdev->hwaddr); | |
2421 | } else if (strcmp(p1, "mtu") == 0) { | |
2422 | if (netdev->mtu) | |
2423 | strprint(retv, inlen, "%s", netdev->mtu); | |
2424 | } else if (strcmp(p1, "macvlan.mode") == 0) { | |
2425 | if (netdev->type == LXC_NET_MACVLAN) { | |
2426 | const char *mode; | |
2427 | switch (netdev->priv.macvlan_attr.mode) { | |
2428 | case MACVLAN_MODE_PRIVATE: mode = "private"; break; | |
2429 | case MACVLAN_MODE_VEPA: mode = "vepa"; break; | |
2430 | case MACVLAN_MODE_BRIDGE: mode = "bridge"; break; | |
99854161 | 2431 | case MACVLAN_MODE_PASSTHRU: mode = "passthru"; break; |
72d0e1cb SG |
2432 | default: mode = "(invalid)"; break; |
2433 | } | |
2434 | strprint(retv, inlen, "%s", mode); | |
2435 | } | |
2436 | } else if (strcmp(p1, "veth.pair") == 0) { | |
11029c02 DE |
2437 | if (netdev->type == LXC_NET_VETH) { |
2438 | strprint(retv, inlen, "%s", | |
2439 | netdev->priv.veth_attr.pair ? | |
2440 | netdev->priv.veth_attr.pair : | |
2441 | netdev->priv.veth_attr.veth1); | |
2442 | } | |
72d0e1cb SG |
2443 | } else if (strcmp(p1, "vlan") == 0) { |
2444 | if (netdev->type == LXC_NET_VLAN) { | |
2445 | strprint(retv, inlen, "%d", netdev->priv.vlan_attr.vid); | |
2446 | } | |
9eaf8a59 | 2447 | } else if (strcmp(p1, "ipv4.gateway") == 0) { |
72d0e1cb SG |
2448 | if (netdev->ipv4_gateway_auto) { |
2449 | strprint(retv, inlen, "auto"); | |
2450 | } else if (netdev->ipv4_gateway) { | |
2451 | char buf[INET_ADDRSTRLEN]; | |
2452 | inet_ntop(AF_INET, netdev->ipv4_gateway, buf, sizeof(buf)); | |
2453 | strprint(retv, inlen, "%s", buf); | |
2454 | } | |
2455 | } else if (strcmp(p1, "ipv4") == 0) { | |
2456 | struct lxc_list *it2; | |
2457 | lxc_list_for_each(it2, &netdev->ipv4) { | |
2458 | struct lxc_inetdev *i = it2->elem; | |
2459 | char buf[INET_ADDRSTRLEN]; | |
2460 | inet_ntop(AF_INET, &i->addr, buf, sizeof(buf)); | |
28417b5a | 2461 | strprint(retv, inlen, "%s/%d\n", buf, i->prefix); |
72d0e1cb | 2462 | } |
9eaf8a59 | 2463 | } else if (strcmp(p1, "ipv6.gateway") == 0) { |
72d0e1cb SG |
2464 | if (netdev->ipv6_gateway_auto) { |
2465 | strprint(retv, inlen, "auto"); | |
2466 | } else if (netdev->ipv6_gateway) { | |
6afb165d AN |
2467 | char buf[INET6_ADDRSTRLEN]; |
2468 | inet_ntop(AF_INET6, netdev->ipv6_gateway, buf, sizeof(buf)); | |
72d0e1cb SG |
2469 | strprint(retv, inlen, "%s", buf); |
2470 | } | |
2471 | } else if (strcmp(p1, "ipv6") == 0) { | |
2472 | struct lxc_list *it2; | |
2473 | lxc_list_for_each(it2, &netdev->ipv6) { | |
6afb165d AN |
2474 | struct lxc_inet6dev *i = it2->elem; |
2475 | char buf[INET6_ADDRSTRLEN]; | |
72d0e1cb | 2476 | inet_ntop(AF_INET6, &i->addr, buf, sizeof(buf)); |
28417b5a | 2477 | strprint(retv, inlen, "%s/%d\n", buf, i->prefix); |
72d0e1cb SG |
2478 | } |
2479 | } | |
2480 | return fulllen; | |
2481 | } | |
2482 | ||
2483 | static int lxc_get_item_network(struct lxc_conf *c, char *retv, int inlen) | |
2484 | { | |
2485 | int len, fulllen = 0; | |
2486 | struct lxc_list *it; | |
2487 | ||
2488 | if (!retv) | |
2489 | inlen = 0; | |
2490 | else | |
2491 | memset(retv, 0, inlen); | |
2492 | ||
2493 | lxc_list_for_each(it, &c->network) { | |
2494 | struct lxc_netdev *n = it->elem; | |
2495 | const char *t = lxc_net_type_to_str(n->type); | |
2496 | strprint(retv, inlen, "%s\n", t ? t : "(invalid)"); | |
2497 | } | |
2498 | return fulllen; | |
2499 | } | |
2500 | ||
12a50cc6 DE |
2501 | int lxc_get_config_item(struct lxc_conf *c, const char *key, char *retv, |
2502 | int inlen) | |
72d0e1cb | 2503 | { |
4a85ce2a | 2504 | const char *v = NULL; |
72d0e1cb SG |
2505 | |
2506 | if (strcmp(key, "lxc.mount.entry") == 0) | |
2507 | return lxc_get_mount_entries(c, retv, inlen); | |
b099e9e9 SH |
2508 | else if (strcmp(key, "lxc.mount.auto") == 0) |
2509 | return lxc_get_auto_mounts(c, retv, inlen); | |
72d0e1cb SG |
2510 | else if (strcmp(key, "lxc.mount") == 0) |
2511 | v = c->fstab; | |
2512 | else if (strcmp(key, "lxc.tty") == 0) | |
2513 | return lxc_get_conf_int(c, retv, inlen, c->tty); | |
2514 | else if (strcmp(key, "lxc.pts") == 0) | |
2515 | return lxc_get_conf_int(c, retv, inlen, c->pts); | |
2516 | else if (strcmp(key, "lxc.devttydir") == 0) | |
2517 | v = c->ttydir; | |
2518 | else if (strcmp(key, "lxc.arch") == 0) | |
2519 | return lxc_get_arch_entry(c, retv, inlen); | |
2520 | else if (strcmp(key, "lxc.aa_profile") == 0) | |
fe4de9a6 | 2521 | v = c->lsm_aa_profile; |
7aff4f43 SH |
2522 | else if (strcmp(key, "lxc.aa_allow_incomplete") == 0) |
2523 | return lxc_get_conf_int(c, retv, inlen, c->lsm_aa_allow_incomplete); | |
fe4de9a6 DE |
2524 | else if (strcmp(key, "lxc.se_context") == 0) |
2525 | v = c->lsm_se_context; | |
4a85ce2a | 2526 | else if (strcmp(key, "lxc.logfile") == 0) |
858377e4 | 2527 | v = c->logfile; |
4a85ce2a | 2528 | else if (strcmp(key, "lxc.loglevel") == 0) |
858377e4 | 2529 | v = lxc_log_priority_to_string(c->loglevel); |
72d0e1cb SG |
2530 | else if (strcmp(key, "lxc.cgroup") == 0) // all cgroup info |
2531 | return lxc_get_cgroup_entry(c, retv, inlen, "all"); | |
2532 | else if (strncmp(key, "lxc.cgroup.", 11) == 0) // specific cgroup info | |
2533 | return lxc_get_cgroup_entry(c, retv, inlen, key + 11); | |
2534 | else if (strcmp(key, "lxc.utsname") == 0) | |
64fca455 | 2535 | v = c->utsname ? c->utsname->nodename : NULL; |
96f15ca1 SH |
2536 | else if (strcmp(key, "lxc.console.logfile") == 0) |
2537 | v = c->console.log_path; | |
72d0e1cb SG |
2538 | else if (strcmp(key, "lxc.console") == 0) |
2539 | v = c->console.path; | |
2540 | else if (strcmp(key, "lxc.rootfs.mount") == 0) | |
2541 | v = c->rootfs.mount; | |
327a1e78 | 2542 | else if (strcmp(key, "lxc.rootfs.backend") == 0) |
6a21d4ae | 2543 | v = c->rootfs.bdev_type; |
a17b1e65 SG |
2544 | else if (strcmp(key, "lxc.rootfs.options") == 0) |
2545 | v = c->rootfs.options; | |
72d0e1cb SG |
2546 | else if (strcmp(key, "lxc.rootfs") == 0) |
2547 | v = c->rootfs.path; | |
72d0e1cb SG |
2548 | else if (strcmp(key, "lxc.cap.drop") == 0) |
2549 | return lxc_get_item_cap_drop(c, retv, inlen); | |
1fb86a7c SH |
2550 | else if (strcmp(key, "lxc.cap.keep") == 0) |
2551 | return lxc_get_item_cap_keep(c, retv, inlen); | |
72d0e1cb SG |
2552 | else if (strncmp(key, "lxc.hook", 8) == 0) |
2553 | return lxc_get_item_hooks(c, retv, inlen, key); | |
2554 | else if (strcmp(key, "lxc.network") == 0) | |
2555 | return lxc_get_item_network(c, retv, inlen); | |
2556 | else if (strncmp(key, "lxc.network.", 12) == 0) | |
2557 | return lxc_get_item_nic(c, retv, inlen, key + 12); | |
ee1e7aa0 SG |
2558 | else if (strcmp(key, "lxc.start.auto") == 0) |
2559 | return lxc_get_conf_int(c, retv, inlen, c->start_auto); | |
2560 | else if (strcmp(key, "lxc.start.delay") == 0) | |
2561 | return lxc_get_conf_int(c, retv, inlen, c->start_delay); | |
2562 | else if (strcmp(key, "lxc.start.order") == 0) | |
2563 | return lxc_get_conf_int(c, retv, inlen, c->start_order); | |
a8dfe4e0 WB |
2564 | else if (strcmp(key, "lxc.monitor.unshare") == 0) |
2565 | return lxc_get_conf_int(c, retv, inlen, c->monitor_unshare); | |
ee1e7aa0 SG |
2566 | else if (strcmp(key, "lxc.group") == 0) |
2567 | return lxc_get_item_groups(c, retv, inlen); | |
58e0f57d SH |
2568 | else if (strcmp(key, "lxc.seccomp") == 0) |
2569 | v = c->seccomp; | |
ab799c0b SG |
2570 | else if (strcmp(key, "lxc.environment") == 0) |
2571 | return lxc_get_item_environment(c, retv, inlen); | |
67c660d0 SG |
2572 | else if (strcmp(key, "lxc.init_cmd") == 0) |
2573 | v = c->init_cmd; | |
68d18db8 PT |
2574 | else if (strcmp(key, "lxc.init_uid") == 0) |
2575 | return lxc_get_conf_int(c, retv, inlen, c->init_uid); | |
2576 | else if (strcmp(key, "lxc.init_gid") == 0) | |
2577 | return lxc_get_conf_int(c, retv, inlen, c->init_gid); | |
8796becf CB |
2578 | else if (strcmp(key, "lxc.ephemeral") == 0) |
2579 | return lxc_get_conf_int(c, retv, inlen, c->ephemeral); | |
76d0127f CB |
2580 | else if (strcmp(key, "lxc.syslog") == 0) |
2581 | v = c->syslog; | |
5a46f283 CB |
2582 | else if (strcmp(key, "lxc.no_new_privs") == 0) |
2583 | return lxc_get_conf_int(c, retv, inlen, c->no_new_privs); | |
72d0e1cb SG |
2584 | else return -1; |
2585 | ||
2586 | if (!v) | |
2587 | return 0; | |
2588 | if (retv && inlen >= strlen(v) + 1) | |
2589 | strncpy(retv, v, strlen(v)+1); | |
2590 | return strlen(v); | |
2591 | } | |
2592 | ||
12a50cc6 | 2593 | int lxc_clear_config_item(struct lxc_conf *c, const char *key) |
72d0e1cb SG |
2594 | { |
2595 | if (strcmp(key, "lxc.network") == 0) | |
2596 | return lxc_clear_config_network(c); | |
2597 | else if (strncmp(key, "lxc.network.", 12) == 0) | |
2598 | return lxc_clear_nic(c, key + 12); | |
2599 | else if (strcmp(key, "lxc.cap.drop") == 0) | |
2600 | return lxc_clear_config_caps(c); | |
1fb86a7c SH |
2601 | else if (strcmp(key, "lxc.cap.keep") == 0) |
2602 | return lxc_clear_config_keepcaps(c); | |
72d0e1cb SG |
2603 | else if (strncmp(key, "lxc.cgroup", 10) == 0) |
2604 | return lxc_clear_cgroups(c, key); | |
a7c6b8c7 | 2605 | else if (strcmp(key, "lxc.mount.entry") == 0) |
72d0e1cb | 2606 | return lxc_clear_mount_entries(c); |
b099e9e9 SH |
2607 | else if (strcmp(key, "lxc.mount.auto") == 0) |
2608 | return lxc_clear_automounts(c); | |
17ed13a3 SH |
2609 | else if (strncmp(key, "lxc.hook", 8) == 0) |
2610 | return lxc_clear_hooks(c, key); | |
ee1e7aa0 SG |
2611 | else if (strncmp(key, "lxc.group", 9) == 0) |
2612 | return lxc_clear_groups(c); | |
fcdc3e50 | 2613 | else if (strncmp(key, "lxc.environment", 15) == 0) |
ab799c0b | 2614 | return lxc_clear_environment(c); |
fcdc3e50 | 2615 | else if (strncmp(key, "lxc.id_map", 10) == 0) |
26f1b390 | 2616 | return lxc_clear_idmaps(c); |
72d0e1cb SG |
2617 | return -1; |
2618 | } | |
2619 | ||
2620 | /* | |
2621 | * writing out a confile. | |
2622 | */ | |
2623 | void write_config(FILE *fout, struct lxc_conf *c) | |
2624 | { | |
6b0d5538 SH |
2625 | size_t len = c->unexpanded_len; |
2626 | int ret; | |
72d0e1cb | 2627 | |
6b0d5538 SH |
2628 | if (!len) |
2629 | return; | |
2630 | ret = fwrite(c->unexpanded_config, 1, len, fout); | |
2631 | if (ret != len) | |
2632 | SYSERROR("Error writing configuration file"); | |
2633 | } | |
f979ac15 | 2634 | |
6b0d5538 SH |
2635 | bool do_append_unexp_config_line(struct lxc_conf *conf, const char *key, const char *v) |
2636 | { | |
2637 | int ret; | |
2638 | size_t len = strlen(key) + strlen(v) + 4; | |
2639 | char *tmp = alloca(len); | |
4184c3e1 | 2640 | |
6b0d5538 SH |
2641 | ret = snprintf(tmp, len, "%s = %s", key, v); |
2642 | if (ret < 0 || ret >= len) | |
2643 | return false; | |
2644 | ||
2645 | /* Save the line verbatim into unexpanded_conf */ | |
2646 | if (append_unexp_config_line(tmp, conf)) | |
2647 | return false; | |
2648 | ||
2649 | return true; | |
2650 | } | |
2651 | ||
2652 | void clear_unexp_config_line(struct lxc_conf *conf, const char *key, bool rm_subkeys) | |
2653 | { | |
2654 | char *lstart = conf->unexpanded_config, *lend; | |
2655 | ||
2656 | if (!conf->unexpanded_config) | |
2657 | return; | |
2658 | while (*lstart) { | |
2659 | lend = strchr(lstart, '\n'); | |
2660 | char v; | |
2661 | if (!lend) | |
2662 | lend = lstart + strlen(lstart); | |
2663 | else | |
2664 | lend++; | |
2665 | if (strncmp(lstart, key, strlen(key)) != 0) { | |
2666 | lstart = lend; | |
2667 | continue; | |
5f62730e | 2668 | } |
6b0d5538 SH |
2669 | if (!rm_subkeys) { |
2670 | v = lstart[strlen(key)]; | |
2671 | if (!isspace(v) && v != '=') { | |
2672 | lstart = lend; | |
2673 | continue; | |
2674 | } | |
5f62730e | 2675 | } |
6b0d5538 SH |
2676 | conf->unexpanded_len -= (lend - lstart); |
2677 | if (*lend == '\0') { | |
2678 | *lstart = '\0'; | |
2679 | return; | |
fd986e08 | 2680 | } |
6b0d5538 | 2681 | memmove(lstart, lend, strlen(lend)+1); |
fd986e08 | 2682 | } |
6b0d5538 SH |
2683 | } |
2684 | ||
329b3625 CB |
2685 | bool clone_update_unexp_ovl_paths(struct lxc_conf *conf, const char *oldpath, |
2686 | const char *newpath, const char *oldname, | |
2687 | const char *newname, const char *ovldir) | |
2688 | { | |
2689 | const char *key = "lxc.mount.entry"; | |
2690 | int ret; | |
2691 | char *lstart = conf->unexpanded_config; | |
2692 | char *lend; | |
2693 | char *p; | |
2694 | char *q; | |
2695 | size_t newdirlen = strlen(ovldir) + strlen(newpath) + strlen(newname) + 2; | |
2696 | size_t olddirlen = strlen(ovldir) + strlen(oldpath) + strlen(oldname) + 2; | |
2697 | char *olddir = alloca(olddirlen + 1); | |
2698 | char *newdir = alloca(newdirlen + 1); | |
2699 | ||
2700 | ret = snprintf(olddir, olddirlen + 1, "%s=%s/%s", ovldir, oldpath, oldname); | |
2701 | if (ret < 0 || ret >= olddirlen + 1) { | |
2702 | ERROR("Bug in %s", __func__); | |
2703 | return false; | |
2704 | } | |
2705 | ret = snprintf(newdir, newdirlen + 1, "%s=%s/%s", ovldir, newpath, newname); | |
2706 | if (ret < 0 || ret >= newdirlen + 1) { | |
2707 | ERROR("Bug in %s", __func__); | |
2708 | return false; | |
2709 | } | |
2710 | if (!conf->unexpanded_config) | |
2711 | return true; | |
2712 | while (*lstart) { | |
2713 | lend = strchr(lstart, '\n'); | |
2714 | if (!lend) | |
2715 | lend = lstart + strlen(lstart); | |
2716 | else | |
2717 | lend++; | |
2718 | if (strncmp(lstart, key, strlen(key)) != 0) | |
2719 | goto next; | |
2720 | p = strchr(lstart + strlen(key), '='); | |
2721 | if (!p) | |
2722 | goto next; | |
2723 | p++; | |
2724 | while (isblank(*p)) | |
2725 | p++; | |
2726 | if (p >= lend) | |
2727 | goto next; | |
2728 | /* Whenever an lxc.mount.entry entry is found in a line we check | |
2729 | * if the substring " overlay" or the substring " aufs" is | |
2730 | * present before doing any further work. We check for " | |
2731 | * overlay" and " aufs" since both substrings need to have at | |
2732 | * least one space before them in a valid overlay | |
2733 | * lxc.mount.entry (/A B overlay). When the space before is | |
2734 | * missing it is very likely that these substrings are part of a | |
2735 | * path or something else. (Checking q >= lend ensures that we | |
2736 | * only count matches in the current line.) */ | |
2737 | if ((!(q = strstr(p, " overlay")) || q >= lend) && (!(q = strstr(p, " aufs")) || q >= lend)) | |
2738 | goto next; | |
2739 | if (!(q = strstr(p, olddir)) || (q >= lend)) | |
2740 | goto next; | |
2741 | ||
2742 | /* replace the olddir with newdir */ | |
2743 | if (olddirlen >= newdirlen) { | |
2744 | size_t diff = olddirlen - newdirlen; | |
2745 | memcpy(q, newdir, newdirlen); | |
2746 | if (olddirlen != newdirlen) { | |
2747 | memmove(q + newdirlen, q + newdirlen + diff, | |
2748 | strlen(q) - newdirlen - diff + 1); | |
2749 | lend -= diff; | |
2750 | conf->unexpanded_len -= diff; | |
2751 | } | |
2752 | } else { | |
2753 | char *new; | |
2754 | size_t diff = newdirlen - olddirlen; | |
2755 | size_t oldlen = conf->unexpanded_len; | |
2756 | size_t newlen = oldlen + diff; | |
2757 | size_t poffset = q - conf->unexpanded_config; | |
2758 | new = realloc(conf->unexpanded_config, newlen + 1); | |
2759 | if (!new) { | |
2760 | ERROR("Out of memory"); | |
2761 | return false; | |
2762 | } | |
2763 | conf->unexpanded_len = newlen; | |
2764 | conf->unexpanded_alloced = newlen + 1; | |
2765 | new[newlen - 1] = '\0'; | |
2766 | lend = new + (lend - conf->unexpanded_config); | |
2767 | /* move over the remainder to make room for the newdir */ | |
2768 | memmove(new + poffset + newdirlen, | |
2769 | new + poffset + olddirlen, | |
2770 | oldlen - poffset - olddirlen + 1); | |
2771 | conf->unexpanded_config = new; | |
2772 | memcpy(new + poffset, newdir, newdirlen); | |
2773 | lend += diff; | |
2774 | } | |
2775 | next: | |
2776 | lstart = lend; | |
2777 | } | |
2778 | return true; | |
2779 | } | |
2780 | ||
67702c21 | 2781 | bool clone_update_unexp_hooks(struct lxc_conf *conf, const char *oldpath, |
d546aa0e CB |
2782 | const char *newpath, const char *oldname, |
2783 | const char *newname) | |
6b0d5538 | 2784 | { |
67702c21 SH |
2785 | const char *key = "lxc.hook"; |
2786 | int ret; | |
2787 | char *lstart = conf->unexpanded_config, *lend, *p; | |
2788 | size_t newdirlen = strlen(newpath) + strlen(newname) + 1; | |
2789 | size_t olddirlen = strlen(oldpath) + strlen(oldname) + 1; | |
2790 | char *olddir = alloca(olddirlen + 1); | |
2791 | char *newdir = alloca(newdirlen + 1); | |
2792 | ||
d546aa0e CB |
2793 | ret = snprintf(olddir, olddirlen + 1, "%s/%s", oldpath, oldname); |
2794 | if (ret < 0 || ret >= olddirlen + 1) { | |
67702c21 SH |
2795 | ERROR("Bug in %s", __func__); |
2796 | return false; | |
2797 | } | |
d546aa0e CB |
2798 | ret = snprintf(newdir, newdirlen + 1, "%s/%s", newpath, newname); |
2799 | if (ret < 0 || ret >= newdirlen + 1) { | |
67702c21 SH |
2800 | ERROR("Bug in %s", __func__); |
2801 | return false; | |
2802 | } | |
2803 | if (!conf->unexpanded_config) | |
2804 | return true; | |
2805 | while (*lstart) { | |
2806 | lend = strchr(lstart, '\n'); | |
2807 | if (!lend) | |
2808 | lend = lstart + strlen(lstart); | |
2809 | else | |
2810 | lend++; | |
d546aa0e CB |
2811 | if (strncmp(lstart, key, strlen(key)) != 0) |
2812 | goto next; | |
2813 | p = strchr(lstart + strlen(key), '='); | |
2814 | if (!p) | |
2815 | goto next; | |
67702c21 SH |
2816 | p++; |
2817 | while (isblank(*p)) | |
2818 | p++; | |
d546aa0e CB |
2819 | if (p >= lend) |
2820 | goto next; | |
2821 | if (strncmp(p, olddir, strlen(olddir)) != 0) | |
2822 | goto next; | |
67702c21 SH |
2823 | /* replace the olddir with newdir */ |
2824 | if (olddirlen >= newdirlen) { | |
2825 | size_t diff = olddirlen - newdirlen; | |
2826 | memcpy(p, newdir, newdirlen); | |
2827 | if (olddirlen != newdirlen) { | |
d546aa0e CB |
2828 | memmove(p + newdirlen, p + newdirlen + diff, |
2829 | strlen(p) - newdirlen - diff + 1); | |
67702c21 SH |
2830 | lend -= diff; |
2831 | conf->unexpanded_len -= diff; | |
2832 | } | |
67702c21 SH |
2833 | } else { |
2834 | char *new; | |
2835 | size_t diff = newdirlen - olddirlen; | |
2836 | size_t oldlen = conf->unexpanded_len; | |
2837 | size_t newlen = oldlen + diff; | |
2838 | size_t poffset = p - conf->unexpanded_config; | |
d546aa0e | 2839 | new = realloc(conf->unexpanded_config, newlen + 1); |
67702c21 SH |
2840 | if (!new) { |
2841 | ERROR("Out of memory"); | |
6b0d5538 | 2842 | return false; |
67702c21 SH |
2843 | } |
2844 | conf->unexpanded_len = newlen; | |
d546aa0e CB |
2845 | conf->unexpanded_alloced = newlen + 1; |
2846 | new[newlen - 1] = '\0'; | |
67702c21 | 2847 | lend = new + (lend - conf->unexpanded_config); |
d546aa0e CB |
2848 | /* move over the remainder to make room for the newdir */ |
2849 | memmove(new + poffset + newdirlen, | |
2850 | new + poffset + olddirlen, | |
2851 | oldlen - poffset - olddirlen + 1); | |
67702c21 | 2852 | conf->unexpanded_config = new; |
d546aa0e CB |
2853 | memcpy(new + poffset, newdir, newdirlen); |
2854 | lend += diff; | |
fd986e08 | 2855 | } |
d546aa0e CB |
2856 | next: |
2857 | lstart = lend; | |
fd986e08 | 2858 | } |
6b0d5538 SH |
2859 | return true; |
2860 | } | |
2861 | ||
2862 | #define DO(cmd) { \ | |
2863 | if (!(cmd)) { \ | |
2864 | ERROR("Error writing to new config"); \ | |
2865 | return false; \ | |
2866 | } \ | |
2867 | } | |
2868 | ||
67702c21 SH |
2869 | static void new_hwaddr(char *hwaddr) |
2870 | { | |
2871 | FILE *f; | |
2872 | f = fopen("/dev/urandom", "r"); | |
2873 | if (f) { | |
2874 | unsigned int seed; | |
2875 | int ret = fread(&seed, sizeof(seed), 1, f); | |
2876 | if (ret != 1) | |
2877 | seed = time(NULL); | |
2878 | fclose(f); | |
2879 | srand(seed); | |
2880 | } else | |
2881 | srand(time(NULL)); | |
2882 | snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", | |
2883 | rand() % 255, rand() % 255, rand() % 255); | |
2884 | } | |
2885 | ||
2886 | /* | |
2887 | * This is called only from clone. | |
2888 | * We wish to update all hwaddrs in the unexpanded config file. We | |
2889 | * can't/don't want to update any which come from lxc.includes (there | |
2890 | * shouldn't be any). | |
2891 | * We can't just walk the c->lxc-conf->network list because that includes | |
2892 | * netifs from the include files. So we update the ones which we find in | |
2893 | * the unexp config file, then find the original macaddr in the | |
2894 | * conf->network, and update that to the same value. | |
2895 | */ | |
2896 | bool network_new_hwaddrs(struct lxc_conf *conf) | |
6b0d5538 SH |
2897 | { |
2898 | struct lxc_list *it; | |
2899 | ||
67702c21 SH |
2900 | const char *key = "lxc.network.hwaddr"; |
2901 | char *lstart = conf->unexpanded_config, *lend, *p, *p2; | |
6b0d5538 | 2902 | |
67702c21 SH |
2903 | if (!conf->unexpanded_config) |
2904 | return true; | |
2905 | while (*lstart) { | |
2906 | char newhwaddr[18], oldhwaddr[17]; | |
2907 | lend = strchr(lstart, '\n'); | |
2908 | if (!lend) | |
2909 | lend = lstart + strlen(lstart); | |
2910 | else | |
2911 | lend++; | |
2912 | if (strncmp(lstart, key, strlen(key)) != 0) { | |
2913 | lstart = lend; | |
2914 | continue; | |
72d0e1cb | 2915 | } |
67702c21 SH |
2916 | p = strchr(lstart+strlen(key), '='); |
2917 | if (!p) { | |
2918 | lstart = lend; | |
2919 | continue; | |
72d0e1cb | 2920 | } |
67702c21 SH |
2921 | p++; |
2922 | while (isblank(*p)) | |
2923 | p++; | |
2924 | if (!*p) | |
2925 | return true; | |
2926 | p2 = p; | |
2927 | while (*p2 && !isblank(*p2) && *p2 != '\n') | |
2928 | p2++; | |
2929 | if (p2-p != 17) { | |
2930 | WARN("Bad hwaddr entry"); | |
2931 | lstart = lend; | |
2932 | continue; | |
72d0e1cb | 2933 | } |
67702c21 SH |
2934 | memcpy(oldhwaddr, p, 17); |
2935 | new_hwaddr(newhwaddr); | |
2936 | memcpy(p, newhwaddr, 17); | |
2937 | lxc_list_for_each(it, &conf->network) { | |
2938 | struct lxc_netdev *n = it->elem; | |
2939 | if (n->hwaddr && memcmp(oldhwaddr, n->hwaddr, 17) == 0) | |
2940 | memcpy(n->hwaddr, newhwaddr, 17); | |
72d0e1cb | 2941 | } |
67702c21 SH |
2942 | |
2943 | lstart = lend; | |
72d0e1cb | 2944 | } |
6b0d5538 | 2945 | return true; |
72d0e1cb | 2946 | } |
8796becf CB |
2947 | |
2948 | static int config_ephemeral(const char *key, const char *value, | |
2949 | struct lxc_conf *lxc_conf) | |
2950 | { | |
2951 | int v = atoi(value); | |
2952 | ||
2953 | if (v != 0 && v != 1) { | |
2954 | ERROR("Wrong value for lxc.ephemeral. Can only be set to 0 or 1"); | |
2955 | return -1; | |
2956 | } else { | |
2957 | lxc_conf->ephemeral = v; | |
2958 | } | |
2959 | ||
2960 | return 0; | |
2961 | } | |
2962 | ||
64c57ea1 | 2963 | static int config_syslog(const char *key, const char *value, |
76d0127f | 2964 | struct lxc_conf *lxc_conf) |
64c57ea1 | 2965 | { |
76d0127f CB |
2966 | int facility; |
2967 | facility = lxc_syslog_priority_to_int(value); | |
2968 | if (facility == -EINVAL) { | |
2969 | ERROR("Wrong value for lxc.syslog"); | |
2970 | return -1; | |
64c57ea1 BD |
2971 | } |
2972 | ||
76d0127f CB |
2973 | lxc_log_syslog(facility); |
2974 | return config_string_item(&lxc_conf->syslog, value); | |
64c57ea1 | 2975 | } |
5a46f283 CB |
2976 | |
2977 | static int config_no_new_privs(const char *key, const char *value, | |
2978 | struct lxc_conf *lxc_conf) | |
2979 | { | |
2980 | int v = atoi(value); | |
2981 | ||
2982 | if (v != 0 && v != 1) { | |
2983 | ERROR("Wrong value for lxc.no_new_privs. Can only be set to 0 or 1"); | |
2984 | return -1; | |
2985 | } | |
2986 | lxc_conf->no_new_privs = v ? true : false; | |
2987 | ||
2988 | return 0; | |
2989 | } |