]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/confile_utils.c
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2017 Canonical Ltd.
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.
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.
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.
26 #include <arpa/inet.h>
30 #include "confile_utils.h"
34 #include "lxccontainer.h"
39 lxc_log_define(lxc_confile_utils
, lxc
);
41 int parse_idmaps(const char *idmap
, char *type
, unsigned long *nsid
,
42 unsigned long *hostid
, unsigned long *range
)
45 unsigned long tmp_hostid
, tmp_nsid
, tmp_range
;
50 /* Duplicate string. */
55 /* A prototypical idmap entry would be: "u 1000 1000000 65536" */
60 slide
+= strspn(slide
, " \t\r");
61 if (slide
!= window
&& *slide
== '\0')
65 if (*slide
!= 'u' && *slide
!= 'g')
70 /* move beyond type */
74 /* Validate that only whitespace follows. */
75 slide
+= strspn(slide
, " \t\r");
76 /* There must be whitespace. */
80 /* Mark beginning of nsuid. */
82 /* Validate that non-whitespace follows. */
83 slide
+= strcspn(slide
, " \t\r");
84 /* There must be non-whitespace. */
85 if (slide
== window
|| *slide
== '\0')
87 /* Mark end of nsuid. */
91 if (lxc_safe_ulong(window
, &tmp_nsid
) < 0)
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.
104 /* Mark beginning of hostid. */
106 /* Validate that non-whitespace follows. */
107 slide
+= strcspn(slide
, " \t\r");
108 /* There must be non-whitespace. */
109 if (slide
== window
|| *slide
== '\0')
111 /* Mark end of nsuid. */
115 if (lxc_safe_ulong(window
, &tmp_hostid
) < 0)
118 /* Move beyond \0. */
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.
128 /* Mark beginning of range. */
130 /* Validate that non-whitespace follows. */
131 slide
+= strcspn(slide
, " \t\r");
132 /* There must be non-whitespace. */
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.
139 if (*(slide
+ strspn(slide
, " \t\r\n")) != '\0')
141 /* Mark end of range. */
145 if (lxc_safe_ulong(window
, &tmp_range
) < 0)
150 *hostid
= tmp_hostid
;
153 /* Yay, we survived. */
162 bool lxc_config_value_empty(const char *value
)
164 if (value
&& strlen(value
) > 0)
170 struct lxc_netdev
*lxc_network_add(struct lxc_list
*networks
, int idx
, bool tail
)
172 struct lxc_list
*newlist
;
173 struct lxc_netdev
*netdev
= NULL
;
175 /* network does not exist */
176 netdev
= malloc(sizeof(*netdev
));
180 memset(netdev
, 0, sizeof(*netdev
));
181 lxc_list_init(&netdev
->ipv4
);
182 lxc_list_init(&netdev
->ipv6
);
184 /* give network a unique index */
187 /* prepare new list */
188 newlist
= malloc(sizeof(*newlist
));
194 lxc_list_init(newlist
);
195 newlist
->elem
= netdev
;
198 lxc_list_add_tail(networks
, newlist
);
200 lxc_list_add(networks
, newlist
);
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.
207 struct lxc_netdev
*lxc_get_netdev_by_idx(struct lxc_conf
*conf
,
208 unsigned int idx
, bool allocate
)
210 struct lxc_netdev
*netdev
= NULL
;
211 struct lxc_list
*networks
= &conf
->network
;
212 struct lxc_list
*insert
= networks
;
215 if (!lxc_list_empty(networks
)) {
216 lxc_list_for_each(insert
, networks
) {
217 netdev
= insert
->elem
;
218 if (netdev
->idx
== idx
)
220 else if (netdev
->idx
> idx
)
228 return lxc_network_add(insert
, idx
, true);
231 void lxc_log_configured_netdevs(const struct lxc_conf
*conf
)
233 struct lxc_netdev
*netdev
;
234 struct lxc_list
*it
= (struct lxc_list
*)&conf
->network
;;
236 if ((conf
->loglevel
!= LXC_LOG_LEVEL_TRACE
) &&
237 (lxc_log_get_level() != LXC_LOG_LEVEL_TRACE
))
240 if (lxc_list_empty(it
)) {
241 TRACE("container has no networks configured");
245 lxc_list_for_each(it
, &conf
->network
) {
246 struct lxc_list
*cur
, *next
;
247 struct lxc_inetdev
*inet4dev
;
248 struct lxc_inet6dev
*inet6dev
;
249 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
253 TRACE("index: %zd", netdev
->idx
);
254 TRACE("ifindex: %d", netdev
->ifindex
);
255 switch (netdev
->type
) {
258 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
259 TRACE("veth pair: %s",
260 netdev
->priv
.veth_attr
.pair
);
261 if (netdev
->priv
.veth_attr
.veth1
[0] != '\0')
263 netdev
->priv
.veth_attr
.veth1
);
264 if (netdev
->priv
.veth_attr
.ifindex
> 0)
265 TRACE("host side ifindex for veth device: %d",
266 netdev
->priv
.veth_attr
.ifindex
);
268 case LXC_NET_MACVLAN
:
269 TRACE("type: macvlan");
270 if (netdev
->priv
.macvlan_attr
.mode
> 0) {
272 macvlan_mode
= lxc_macvlan_flag_to_mode(
273 netdev
->priv
.macvlan_attr
.mode
);
274 TRACE("macvlan mode: %s",
275 macvlan_mode
? macvlan_mode
281 TRACE("vlan id: %d", netdev
->priv
.vlan_attr
.vid
);
285 if (netdev
->priv
.phys_attr
.ifindex
> 0) {
286 TRACE("host side ifindex for phys device: %d",
287 netdev
->priv
.phys_attr
.ifindex
);
291 TRACE("type: empty");
297 ERROR("invalid network type %d", netdev
->type
);
301 if (netdev
->type
!= LXC_NET_EMPTY
) {
303 netdev
->flags
== IFF_UP
? "up" : "none");
304 if (netdev
->link
[0] != '\0')
305 TRACE("link: %s", netdev
->link
);
306 if (netdev
->name
[0] != '\0')
307 TRACE("name: %s", netdev
->name
);
309 TRACE("hwaddr: %s", netdev
->hwaddr
);
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
);
317 TRACE("ipv4 gateway auto: %s",
318 netdev
->ipv4_gateway_auto
? "true" : "false");
320 if (netdev
->ipv4_gateway
) {
321 inet_ntop(AF_INET
, netdev
->ipv4_gateway
,
322 bufinet4
, sizeof(bufinet4
));
323 TRACE("ipv4 gateway: %s", bufinet4
);
326 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
327 inet4dev
= cur
->elem
;
328 inet_ntop(AF_INET
, &inet4dev
->addr
, bufinet4
,
330 TRACE("ipv4 addr: %s", bufinet4
);
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
);
340 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
341 inet6dev
= cur
->elem
;
342 inet_ntop(AF_INET6
, &inet6dev
->addr
, bufinet6
,
344 TRACE("ipv6 addr: %s", bufinet6
);
350 static void lxc_free_netdev(struct lxc_netdev
*netdev
)
352 struct lxc_list
*cur
, *next
;
354 free(netdev
->upscript
);
355 free(netdev
->downscript
);
356 free(netdev
->hwaddr
);
359 free(netdev
->ipv4_gateway
);
360 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
366 free(netdev
->ipv6_gateway
);
367 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
376 bool lxc_remove_nic_by_idx(struct lxc_conf
*conf
, unsigned int idx
)
378 struct lxc_list
*cur
, *next
;
379 struct lxc_netdev
*netdev
;
382 lxc_list_for_each_safe(cur
, &conf
->network
, next
) {
384 if (netdev
->idx
!= idx
)
395 lxc_free_netdev(netdev
);
401 void lxc_free_networks(struct lxc_list
*networks
)
403 struct lxc_list
*cur
, *next
;
404 struct lxc_netdev
*netdev
;
406 lxc_list_for_each_safe(cur
, networks
, next
) {
408 lxc_free_netdev(netdev
);
412 /* prevent segfaults */
413 lxc_list_init(networks
);
416 static struct macvlan_mode
{
420 { "private", MACVLAN_MODE_PRIVATE
},
421 { "vepa", MACVLAN_MODE_VEPA
},
422 { "bridge", MACVLAN_MODE_BRIDGE
},
423 { "passthru", MACVLAN_MODE_PASSTHRU
},
426 int lxc_macvlan_mode_to_flag(int *mode
, const char *value
)
430 for (i
= 0; i
< sizeof(macvlan_mode
) / sizeof(macvlan_mode
[0]); i
++) {
431 if (strcmp(macvlan_mode
[i
].name
, value
))
434 *mode
= macvlan_mode
[i
].mode
;
441 char *lxc_macvlan_flag_to_mode(int mode
)
445 for (i
= 0; i
< sizeof(macvlan_mode
) / sizeof(macvlan_mode
[0]); i
++) {
446 if (macvlan_mode
[i
].mode
== mode
)
449 return macvlan_mode
[i
].name
;
455 int set_config_string_item(char **conf_item
, const char *value
)
459 if (lxc_config_value_empty(value
)) {
465 new_value
= strdup(value
);
467 SYSERROR("failed to duplicate string \"%s\"", value
);
472 *conf_item
= new_value
;
476 int set_config_string_item_max(char **conf_item
, const char *value
, size_t max
)
478 if (strlen(value
) >= max
) {
479 ERROR("%s is too long (>= %lu)", value
, (unsigned long)max
);
483 return set_config_string_item(conf_item
, value
);
486 int set_config_path_item(char **conf_item
, const char *value
)
488 return set_config_string_item_max(conf_item
, value
, PATH_MAX
);
491 int config_ip_prefix(struct in_addr
*addr
)
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
;
503 int network_ifname(char *valuep
, const char *value
)
505 if (strlen(value
) >= IFNAMSIZ
) {
506 ERROR("Network devie name \"%s\" is too long (>= %zu)", value
,
510 strcpy(valuep
, value
);
514 int rand_complete_hwaddr(char *hwaddr
)
516 const char hex
[] = "0123456789abcdef";
524 seed
= randseed(false);
526 while (*curs
!= '\0' && *curs
!= '\n') {
527 if (*curs
== 'x' || *curs
== 'X') {
528 if (curs
- hwaddr
== 1) {
529 /* ensure address is unicast */
531 *curs
= hex
[rand_r(&seed
) & 0x0E];
533 *curs
= hex
[rand_r(&seed
) & 0x0F];
535 *curs
= hex
[rand() & 0x0E];
537 *curs
= hex
[rand() & 0x0F];
546 bool lxc_config_net_hwaddr(const char *line
)
550 if (strncmp(line
, "lxc.net", 7) != 0)
552 if (strncmp(line
, "lxc.network.hwaddr", 18) == 0)
555 /* We have to dup the line, if line is something like
556 * "lxc.net.[i].xxx = xxxxx ", we need to remove
557 * '[i]' and compare its key with 'lxc.net.hwaddr'*/
560 SYSERROR("failed to allocate memory");
563 if (*(copy
+ 8) >= '0' && *(copy
+ 8) <= '9') {
564 p
= strchr(copy
+ 8, '.');
569 /* strlen("hwaddr") = 6 */
570 if (strlen(p
+ 1) >= 6)
571 memmove(copy
+ 8, p
+ 1, 6);
574 if (strncmp(copy
, "lxc.net.hwaddr", 14) == 0) {
580 /* We have to dup the line second time, if line is something like
581 * "lxc.network.[i].xxx = xxxxx ", we need to remove
582 * '[i]' and compare its key with 'lxc.network.hwaddr'*/
585 SYSERROR("failed to allocate memory");
588 if (*(copy
+ 12) >= '0' && *(copy
+ 12) <= '9') {
589 p
= strchr(copy
+ 12, '.');
594 /* strlen("hwaddr") = 6 */
595 if (strlen(p
+ 1) >= 6)
596 memmove(copy
+ 12, p
+ 1, 6);
599 if (strncmp(copy
, "lxc.network.hwaddr", 18) == 0) {
609 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
610 * file, we expand it in the unexpanded_config, so that after a save_config we
611 * store the hwaddr for re-use.
612 * This is only called when reading the config file, not when executing a
614 * 'x' and 'X' are substituted in-place.
616 void update_hwaddr(const char *line
)
620 line
+= lxc_char_left_gc(line
, strlen(line
));
624 if (!lxc_config_net_hwaddr(line
))
627 /* Let config_net_hwaddr raise the error. */
628 p
= strchr(line
, '=');
639 rand_complete_hwaddr(p
);
642 bool new_hwaddr(char *hwaddr
)
646 (void)randseed(true);
648 ret
= snprintf(hwaddr
, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255,
649 rand() % 255, rand() % 255);
650 if (ret
< 0 || ret
>= 18) {
651 SYSERROR("Failed to call snprintf().");
658 int lxc_get_conf_str(char *retv
, int inlen
, const char *value
)
662 if (retv
&& inlen
>= strlen(value
) + 1)
663 strncpy(retv
, value
, strlen(value
) + 1);
665 return strlen(value
);
668 int lxc_get_conf_int(struct lxc_conf
*c
, char *retv
, int inlen
, int v
)
673 memset(retv
, 0, inlen
);
675 return snprintf(retv
, inlen
, "%d", v
);
678 int lxc_get_conf_uint64(struct lxc_conf
*c
, char *retv
, int inlen
, uint64_t v
)
683 memset(retv
, 0, inlen
);
685 return snprintf(retv
, inlen
, "%"PRIu64
, v
);
688 bool parse_limit_value(const char **value
, rlim_t
*res
)
692 if (strncmp(*value
, "unlimited", sizeof("unlimited") - 1) == 0) {
693 *res
= RLIM_INFINITY
;
694 *value
+= sizeof("unlimited") - 1;
699 *res
= strtoull(*value
, &endptr
, 10);
700 if (errno
|| !endptr
)
707 static int lxc_container_name_to_pid(const char *lxcname_or_pid
,
714 pid
= strtol(lxcname_or_pid
, &err
, 10);
715 if (*err
!= '\0' || pid
< 1) {
716 struct lxc_container
*c
;
718 c
= lxc_container_new(lxcname_or_pid
, lxcpath
);
720 ERROR("\"%s\" is not a valid pid nor a container name",
725 if (!c
->may_control(c
)) {
726 ERROR("Insufficient privileges to control container "
728 lxc_container_put(c
);
732 pid
= c
->init_pid(c
);
734 ERROR("Container \"%s\" is not running", c
->name
);
735 lxc_container_put(c
);
739 lxc_container_put(c
);
744 ERROR("%s - Failed to send signal to pid %d", strerror(errno
),
752 int lxc_inherit_namespace(const char *lxcname_or_pid
, const char *lxcpath
,
753 const char *namespace)
756 char *dup
, *lastslash
;
758 lastslash
= strrchr(lxcname_or_pid
, '/');
760 dup
= strdup(lxcname_or_pid
);
764 dup
[lastslash
- lxcname_or_pid
] = '\0';
765 pid
= lxc_container_name_to_pid(lastslash
+ 1, dup
);
768 pid
= lxc_container_name_to_pid(lxcname_or_pid
, lxcpath
);
774 fd
= lxc_preserve_ns(pid
, namespace);