]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/confile_utils.c
Merge pull request #2077 from lifeng68/Fix_segment_fault
[mirror_lxc.git] / src / lxc / confile_utils.c
CommitLineData
0b843d35
CB
1/* liblxcapi
2 *
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2017 Canonical Ltd.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
ce2f5ae8
CB
20#include "config.h"
21
f9373e40 22#include <ctype.h>
0b843d35 23#include <stdio.h>
ce2f5ae8 24#include <stdlib.h>
0b843d35 25#include <string.h>
9b0df30f 26#include <arpa/inet.h>
0b843d35 27
663e9916 28#include "conf.h"
ce2f5ae8
CB
29#include "confile.h"
30#include "confile_utils.h"
31#include "error.h"
ce2f5ae8 32#include "list.h"
28d9e29e
CB
33#include "log.h"
34#include "lxccontainer.h"
811ef482 35#include "network.h"
f9373e40 36#include "parse.h"
0b843d35
CB
37#include "utils.h"
38
ce2f5ae8
CB
39lxc_log_define(lxc_confile_utils, lxc);
40
0b843d35
CB
41int parse_idmaps(const char *idmap, char *type, unsigned long *nsid,
42 unsigned long *hostid, unsigned long *range)
43{
44 int ret = -1;
45 unsigned long tmp_hostid, tmp_nsid, tmp_range;
46 char tmp_type;
47 char *window, *slide;
48 char *dup = NULL;
49
50 /* Duplicate string. */
51 dup = strdup(idmap);
52 if (!dup)
53 goto on_error;
54
55 /* A prototypical idmap entry would be: "u 1000 1000000 65536" */
56
57 /* align */
58 slide = window = dup;
59 /* skip whitespace */
60 slide += strspn(slide, " \t\r");
61 if (slide != window && *slide == '\0')
62 goto on_error;
63
64 /* Validate type. */
65 if (*slide != 'u' && *slide != 'g')
66 goto on_error;
67 /* Assign type. */
68 tmp_type = *slide;
69
70 /* move beyond type */
71 slide++;
72 /* align */
73 window = slide;
74 /* Validate that only whitespace follows. */
75 slide += strspn(slide, " \t\r");
76 /* There must be whitespace. */
77 if (slide == window)
78 goto on_error;
79
80 /* Mark beginning of nsuid. */
81 window = slide;
82 /* Validate that non-whitespace follows. */
83 slide += strcspn(slide, " \t\r");
84 /* There must be non-whitespace. */
85 if (slide == window || *slide == '\0')
86 goto on_error;
87 /* Mark end of nsuid. */
88 *slide = '\0';
89
90 /* Parse nsuid. */
91 if (lxc_safe_ulong(window, &tmp_nsid) < 0)
92 goto on_error;
93
94 /* Move beyond \0. */
95 slide++;
0b843d35
CB
96 /* Validate that only whitespace follows. */
97 slide += strspn(slide, " \t\r");
98 /* If there was only one whitespace then we whiped it with our \0 above.
99 * So only ensure that we're not at the end of the string.
100 */
101 if (*slide == '\0')
102 goto on_error;
103
104 /* Mark beginning of hostid. */
105 window = slide;
106 /* Validate that non-whitespace follows. */
107 slide += strcspn(slide, " \t\r");
108 /* There must be non-whitespace. */
109 if (slide == window || *slide == '\0')
110 goto on_error;
111 /* Mark end of nsuid. */
112 *slide = '\0';
113
114 /* Parse hostid. */
115 if (lxc_safe_ulong(window, &tmp_hostid) < 0)
116 goto on_error;
117
118 /* Move beyond \0. */
119 slide++;
0b843d35
CB
120 /* Validate that only whitespace follows. */
121 slide += strspn(slide, " \t\r");
122 /* If there was only one whitespace then we whiped it with our \0 above.
123 * So only ensure that we're not at the end of the string.
124 */
125 if (*slide == '\0')
126 goto on_error;
127
128 /* Mark beginning of range. */
129 window = slide;
130 /* Validate that non-whitespace follows. */
131 slide += strcspn(slide, " \t\r");
132 /* There must be non-whitespace. */
133 if (slide == window)
134 goto on_error;
135
136 /* The range is the last valid entry we expect. So make sure that there
137 * is not trailing garbage and if there is, error out.
138 */
139 if (*(slide + strspn(slide, " \t\r\n")) != '\0')
140 goto on_error;
141 /* Mark end of range. */
142 *slide = '\0';
143
144 /* Parse range. */
145 if (lxc_safe_ulong(window, &tmp_range) < 0)
146 goto on_error;
147
148 *type = tmp_type;
149 *nsid = tmp_nsid;
150 *hostid = tmp_hostid;
151 *range = tmp_range;
152
153 /* Yay, we survived. */
154 ret = 0;
155
156on_error:
157 free(dup);
158
159 return ret;
160}
663e9916
CB
161
162bool lxc_config_value_empty(const char *value)
163{
164 if (value && strlen(value) > 0)
165 return false;
166
167 return true;
168}
ce2f5ae8 169
c302b476 170struct lxc_netdev *lxc_network_add(struct lxc_list *networks, int idx, bool tail)
ce2f5ae8
CB
171{
172 struct lxc_list *newlist;
173 struct lxc_netdev *netdev = NULL;
ce2f5ae8
CB
174
175 /* network does not exist */
176 netdev = malloc(sizeof(*netdev));
177 if (!netdev)
178 return NULL;
179
180 memset(netdev, 0, sizeof(*netdev));
181 lxc_list_init(&netdev->ipv4);
182 lxc_list_init(&netdev->ipv6);
183
184 /* give network a unique index */
185 netdev->idx = idx;
186
187 /* prepare new list */
188 newlist = malloc(sizeof(*newlist));
189 if (!newlist) {
190 free(netdev);
191 return NULL;
192 }
193
194 lxc_list_init(newlist);
195 newlist->elem = netdev;
196
c302b476
CB
197 if (tail)
198 lxc_list_add_tail(networks, newlist);
199 else
200 lxc_list_add(networks, newlist);
ce2f5ae8
CB
201 return netdev;
202}
1ed6ba91 203
c302b476
CB
204/* Takes care of finding the correct netdev struct in the networks list or
205 * allocates a new one if it couldn't be found.
206 */
207struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf,
208 unsigned int idx, bool allocate)
209{
210 struct lxc_netdev *netdev = NULL;
211 struct lxc_list *networks = &conf->network;
212 struct lxc_list *insert = networks;
213
214 /* lookup network */
215 if (!lxc_list_empty(networks)) {
216 lxc_list_for_each(insert, networks) {
217 netdev = insert->elem;
218 if (netdev->idx == idx)
219 return netdev;
220 else if (netdev->idx > idx)
221 break;
222 }
223 }
224
225 if (!allocate)
226 return NULL;
227
228 return lxc_network_add(insert, idx, true);
229}
230
1ed6ba91
CB
231void lxc_log_configured_netdevs(const struct lxc_conf *conf)
232{
233 struct lxc_netdev *netdev;
234 struct lxc_list *it = (struct lxc_list *)&conf->network;;
235
236 if ((conf->loglevel != LXC_LOG_LEVEL_TRACE) &&
237 (lxc_log_get_level() != LXC_LOG_LEVEL_TRACE))
238 return;
239
240 if (lxc_list_empty(it)) {
241 TRACE("container has no networks configured");
242 return;
243 }
244
245 lxc_list_for_each(it, &conf->network) {
9b0df30f
CB
246 struct lxc_list *cur, *next;
247 struct lxc_inetdev *inet4dev;
248 struct lxc_inet6dev *inet6dev;
249 char bufinet4[INET_ADDRSTRLEN], bufinet6[INET6_ADDRSTRLEN];
250
1ed6ba91
CB
251 netdev = it->elem;
252
c302b476 253 TRACE("index: %zd", netdev->idx);
7a582518 254 TRACE("ifindex: %d", netdev->ifindex);
1ed6ba91
CB
255 switch (netdev->type) {
256 case LXC_NET_VETH:
257 TRACE("type: veth");
de4855a8 258 if (netdev->priv.veth_attr.pair[0] != '\0')
9b0df30f
CB
259 TRACE("veth pair: %s",
260 netdev->priv.veth_attr.pair);
8ce727fc
CB
261 if (netdev->priv.veth_attr.veth1[0] != '\0')
262 TRACE("veth1 : %s",
263 netdev->priv.veth_attr.veth1);
d952b351
CB
264 if (netdev->priv.veth_attr.ifindex > 0)
265 TRACE("host side ifindex for veth device: %d",
266 netdev->priv.veth_attr.ifindex);
1ed6ba91
CB
267 break;
268 case LXC_NET_MACVLAN:
269 TRACE("type: macvlan");
9b0df30f
CB
270 if (netdev->priv.macvlan_attr.mode > 0) {
271 char *macvlan_mode;
272 macvlan_mode = lxc_macvlan_flag_to_mode(
273 netdev->priv.macvlan_attr.mode);
274 TRACE("macvlan mode: %s",
275 macvlan_mode ? macvlan_mode
276 : "(invalid mode)");
277 }
1ed6ba91
CB
278 break;
279 case LXC_NET_VLAN:
280 TRACE("type: vlan");
9b0df30f 281 TRACE("vlan id: %d", netdev->priv.vlan_attr.vid);
1ed6ba91
CB
282 break;
283 case LXC_NET_PHYS:
284 TRACE("type: phys");
b809f232
CB
285 if (netdev->priv.phys_attr.ifindex > 0) {
286 TRACE("host side ifindex for phys device: %d",
287 netdev->priv.phys_attr.ifindex);
288 }
1ed6ba91
CB
289 break;
290 case LXC_NET_EMPTY:
291 TRACE("type: empty");
292 break;
293 case LXC_NET_NONE:
294 TRACE("type: none");
295 break;
296 default:
297 ERROR("invalid network type %d", netdev->type);
298 return;
299 }
300
9b0df30f
CB
301 if (netdev->type != LXC_NET_EMPTY) {
302 TRACE("flags: %s",
303 netdev->flags == IFF_UP ? "up" : "none");
de4855a8 304 if (netdev->link[0] != '\0')
9b0df30f 305 TRACE("link: %s", netdev->link);
de4855a8 306 if (netdev->name[0] != '\0')
9b0df30f
CB
307 TRACE("name: %s", netdev->name);
308 if (netdev->hwaddr)
309 TRACE("hwaddr: %s", netdev->hwaddr);
310 if (netdev->mtu)
311 TRACE("mtu: %s", netdev->mtu);
312 if (netdev->upscript)
313 TRACE("upscript: %s", netdev->upscript);
314 if (netdev->downscript)
315 TRACE("downscript: %s", netdev->downscript);
316
317 TRACE("ipv4 gateway auto: %s",
318 netdev->ipv4_gateway_auto ? "true" : "false");
319
320 if (netdev->ipv4_gateway) {
321 inet_ntop(AF_INET, netdev->ipv4_gateway,
322 bufinet4, sizeof(bufinet4));
323 TRACE("ipv4 gateway: %s", bufinet4);
324 }
325
326 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
327 inet4dev = cur->elem;
328 inet_ntop(AF_INET, &inet4dev->addr, bufinet4,
329 sizeof(bufinet4));
330 TRACE("ipv4 addr: %s", bufinet4);
331 }
332
333 TRACE("ipv6 gateway auto: %s",
334 netdev->ipv6_gateway_auto ? "true" : "false");
335 if (netdev->ipv6_gateway) {
336 inet_ntop(AF_INET6, netdev->ipv6_gateway,
337 bufinet6, sizeof(bufinet6));
338 TRACE("ipv6 gateway: %s", bufinet6);
339 }
340 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
341 inet6dev = cur->elem;
342 inet_ntop(AF_INET6, &inet6dev->addr, bufinet6,
343 sizeof(bufinet6));
344 TRACE("ipv6 addr: %s", bufinet6);
345 }
346 }
1ed6ba91
CB
347 }
348}
519df1c1 349
e5d2fd7c
CB
350static void lxc_free_netdev(struct lxc_netdev *netdev)
351{
352 struct lxc_list *cur, *next;
353
e5d2fd7c
CB
354 free(netdev->upscript);
355 free(netdev->downscript);
356 free(netdev->hwaddr);
357 free(netdev->mtu);
358
359 free(netdev->ipv4_gateway);
360 lxc_list_for_each_safe(cur, &netdev->ipv4, next) {
361 lxc_list_del(cur);
362 free(cur->elem);
363 free(cur);
364 }
365
366 free(netdev->ipv6_gateway);
367 lxc_list_for_each_safe(cur, &netdev->ipv6, next) {
368 lxc_list_del(cur);
369 free(cur->elem);
370 free(cur);
371 }
372
373 free(netdev);
374}
375
519df1c1
CB
376bool lxc_remove_nic_by_idx(struct lxc_conf *conf, unsigned int idx)
377{
e5d2fd7c 378 struct lxc_list *cur, *next;
519df1c1
CB
379 struct lxc_netdev *netdev;
380 bool found = false;
381
382 lxc_list_for_each_safe(cur, &conf->network, next) {
383 netdev = cur->elem;
384 if (netdev->idx != idx)
385 continue;
386
387 lxc_list_del(cur);
388 found = true;
389 break;
390 }
391
392 if (!found)
393 return false;
394
e5d2fd7c 395 lxc_free_netdev(netdev);
519df1c1
CB
396 free(cur);
397
398 return true;
399}
e5d2fd7c 400
c302b476 401void lxc_free_networks(struct lxc_list *networks)
e5d2fd7c
CB
402{
403 struct lxc_list *cur, *next;
404 struct lxc_netdev *netdev;
405
c302b476 406 lxc_list_for_each_safe(cur, networks, next) {
e5d2fd7c
CB
407 netdev = cur->elem;
408 lxc_free_netdev(netdev);
409 free(cur);
410 }
411
412 /* prevent segfaults */
c302b476 413 lxc_list_init(networks);
e5d2fd7c 414}
9b0df30f
CB
415
416static struct macvlan_mode {
417 char *name;
418 int mode;
419} macvlan_mode[] = {
420 { "private", MACVLAN_MODE_PRIVATE },
421 { "vepa", MACVLAN_MODE_VEPA },
422 { "bridge", MACVLAN_MODE_BRIDGE },
423 { "passthru", MACVLAN_MODE_PASSTHRU },
424};
425
426int lxc_macvlan_mode_to_flag(int *mode, const char *value)
427{
428 size_t i;
429
430 for (i = 0; i < sizeof(macvlan_mode) / sizeof(macvlan_mode[0]); i++) {
431 if (strcmp(macvlan_mode[i].name, value))
432 continue;
433
434 *mode = macvlan_mode[i].mode;
435 return 0;
436 }
437
438 return -1;
439}
440
441char *lxc_macvlan_flag_to_mode(int mode)
442{
443 size_t i;
444
445 for (i = 0; i < sizeof(macvlan_mode) / sizeof(macvlan_mode[0]); i++) {
446 if (macvlan_mode[i].mode == mode)
447 continue;
448
449 return macvlan_mode[i].name;
450 }
451
452 return NULL;
453}
f9373e40
CB
454
455int set_config_string_item(char **conf_item, const char *value)
456{
457 char *new_value;
458
459 if (lxc_config_value_empty(value)) {
460 free(*conf_item);
461 *conf_item = NULL;
462 return 0;
463 }
464
465 new_value = strdup(value);
466 if (!new_value) {
467 SYSERROR("failed to duplicate string \"%s\"", value);
468 return -1;
469 }
470
471 free(*conf_item);
472 *conf_item = new_value;
473 return 0;
474}
475
476int set_config_string_item_max(char **conf_item, const char *value, size_t max)
477{
478 if (strlen(value) >= max) {
479 ERROR("%s is too long (>= %lu)", value, (unsigned long)max);
480 return -1;
481 }
482
483 return set_config_string_item(conf_item, value);
484}
485
486int set_config_path_item(char **conf_item, const char *value)
487{
488 return set_config_string_item_max(conf_item, value, PATH_MAX);
489}
490
491int config_ip_prefix(struct in_addr *addr)
492{
493 if (IN_CLASSA(addr->s_addr))
494 return 32 - IN_CLASSA_NSHIFT;
495 if (IN_CLASSB(addr->s_addr))
496 return 32 - IN_CLASSB_NSHIFT;
497 if (IN_CLASSC(addr->s_addr))
498 return 32 - IN_CLASSC_NSHIFT;
499
500 return 0;
501}
502
de4855a8 503int network_ifname(char *valuep, const char *value)
f9373e40 504{
de4855a8
CB
505 if (strlen(value) >= IFNAMSIZ) {
506 ERROR("Network devie name \"%s\" is too long (>= %zu)", value,
507 (size_t)IFNAMSIZ);
508 }
509
510 strcpy(valuep, value);
511 return 0;
f9373e40
CB
512}
513
514int rand_complete_hwaddr(char *hwaddr)
515{
516 const char hex[] = "0123456789abcdef";
517 char *curs = hwaddr;
518
519#ifndef HAVE_RAND_R
520 randseed(true);
521#else
522 unsigned int seed;
523
524 seed = randseed(false);
525#endif
526 while (*curs != '\0' && *curs != '\n') {
527 if (*curs == 'x' || *curs == 'X') {
528 if (curs - hwaddr == 1) {
529 /* ensure address is unicast */
530#ifdef HAVE_RAND_R
531 *curs = hex[rand_r(&seed) & 0x0E];
532 } else {
533 *curs = hex[rand_r(&seed) & 0x0F];
534#else
535 *curs = hex[rand() & 0x0E];
536 } else {
537 *curs = hex[rand() & 0x0F];
538#endif
539 }
540 }
541 curs++;
542 }
543 return 0;
544}
545
ce4be612 546bool lxc_config_net_hwaddr(const char *line)
547{
44047b2b
FA
548 unsigned index;
549 char tmp[7];
ce4be612 550
551 if (strncmp(line, "lxc.net", 7) != 0)
552 return false;
44047b2b 553 if (strncmp(line, "lxc.net.hwaddr", 14) == 0)
ce4be612 554 return true;
44047b2b 555 if (strncmp(line, "lxc.network.hwaddr", 18) == 0)
ce4be612 556 return true;
44047b2b
FA
557 if (sscanf(line, "lxc.net.%u.%6s", &index, tmp) == 2 || sscanf(line, "lxc.network.%u.%6s", &index, tmp) == 2)
558 return strncmp(tmp, "hwaddr", 6) == 0;
ce4be612 559
ce4be612 560 return false;
561}
562
f9373e40 563/*
ae1dc8b4 564 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
565 * file, we expand it in the unexpanded_config, so that after a save_config we
566 * store the hwaddr for re-use.
f9373e40
CB
567 * This is only called when reading the config file, not when executing a
568 * lxc.include.
569 * 'x' and 'X' are substituted in-place.
570 */
571void update_hwaddr(const char *line)
572{
573 char *p;
574
575 line += lxc_char_left_gc(line, strlen(line));
576 if (line[0] == '#')
577 return;
578
ae1dc8b4 579 if (!lxc_config_net_hwaddr(line))
f9373e40
CB
580 return;
581
582 /* Let config_net_hwaddr raise the error. */
583 p = strchr(line, '=');
584 if (!p)
585 return;
586 p++;
587
588 while (isblank(*p))
589 p++;
590
591 if (!*p)
592 return;
593
594 rand_complete_hwaddr(p);
595}
596
597bool new_hwaddr(char *hwaddr)
598{
599 int ret;
600
601 (void)randseed(true);
602
603 ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255,
604 rand() % 255, rand() % 255);
605 if (ret < 0 || ret >= 18) {
606 SYSERROR("Failed to call snprintf().");
607 return false;
608 }
609
610 return true;
611}
953fe44f
CB
612
613int lxc_get_conf_str(char *retv, int inlen, const char *value)
614{
615 if (!value)
616 return 0;
617 if (retv && inlen >= strlen(value) + 1)
618 strncpy(retv, value, strlen(value) + 1);
619
620 return strlen(value);
621}
622
623int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v)
624{
625 if (!retv)
626 inlen = 0;
627 else
628 memset(retv, 0, inlen);
629
630 return snprintf(retv, inlen, "%d", v);
631}
240d4b74 632
2ea479c9
CB
633int lxc_get_conf_uint64(struct lxc_conf *c, char *retv, int inlen, uint64_t v)
634{
635 if (!retv)
636 inlen = 0;
637 else
638 memset(retv, 0, inlen);
639
640 return snprintf(retv, inlen, "%"PRIu64, v);
641}
642
71460831 643bool parse_limit_value(const char **value, rlim_t *res)
240d4b74 644{
645 char *endptr = NULL;
646
647 if (strncmp(*value, "unlimited", sizeof("unlimited") - 1) == 0) {
648 *res = RLIM_INFINITY;
649 *value += sizeof("unlimited") - 1;
650 return true;
651 }
652
653 errno = 0;
71460831 654 *res = strtoull(*value, &endptr, 10);
240d4b74 655 if (errno || !endptr)
656 return false;
657 *value = endptr;
658
659 return true;
660}
28d9e29e
CB
661
662static int lxc_container_name_to_pid(const char *lxcname_or_pid,
663 const char *lxcpath)
664{
665 int ret;
666 signed long int pid;
667 char *err = NULL;
668
669 pid = strtol(lxcname_or_pid, &err, 10);
670 if (*err != '\0' || pid < 1) {
671 struct lxc_container *c;
672
673 c = lxc_container_new(lxcname_or_pid, lxcpath);
674 if (!c) {
675 ERROR("\"%s\" is not a valid pid nor a container name",
676 lxcname_or_pid);
677 return -1;
678 }
679
680 if (!c->may_control(c)) {
681 ERROR("Insufficient privileges to control container "
682 "\"%s\"", c->name);
683 lxc_container_put(c);
684 return -1;
685 }
686
687 pid = c->init_pid(c);
688 if (pid < 1) {
689 ERROR("Container \"%s\" is not running", c->name);
690 lxc_container_put(c);
691 return -1;
692 }
693
694 lxc_container_put(c);
695 }
696
697 ret = kill(pid, 0);
698 if (ret < 0) {
699 ERROR("%s - Failed to send signal to pid %d", strerror(errno),
700 (int)pid);
701 return -EPERM;
702 }
703
704 return pid;
705}
706
707int lxc_inherit_namespace(const char *lxcname_or_pid, const char *lxcpath,
708 const char *namespace)
709{
710 int fd, pid;
711 char *dup, *lastslash;
712
713 lastslash = strrchr(lxcname_or_pid, '/');
714 if (lastslash) {
715 dup = strdup(lxcname_or_pid);
716 if (!dup)
717 return -ENOMEM;
718
71649566
L
719 dup[lastslash - lxcname_or_pid] = '\0';
720 pid = lxc_container_name_to_pid(lastslash + 1, dup);
28d9e29e
CB
721 free(dup);
722 } else {
723 pid = lxc_container_name_to_pid(lxcname_or_pid, lxcpath);
724 }
725
726 if (pid < 0)
727 return -EINVAL;
728
729 fd = lxc_preserve_ns(pid, namespace);
730 if (fd < 0)
731 return -EINVAL;
732
733 return fd;
734}