]>
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"
40 #include "include/strlcpy.h"
43 lxc_log_define(lxc_confile_utils
, lxc
);
45 int parse_idmaps(const char *idmap
, char *type
, unsigned long *nsid
,
46 unsigned long *hostid
, unsigned long *range
)
49 unsigned long tmp_hostid
, tmp_nsid
, tmp_range
;
54 /* Duplicate string. */
59 /* A prototypical idmap entry would be: "u 1000 1000000 65536" */
64 slide
+= strspn(slide
, " \t\r");
65 if (slide
!= window
&& *slide
== '\0')
69 if (*slide
!= 'u' && *slide
!= 'g') {
70 ERROR("Invalid id mapping type: %c", *slide
);
77 /* move beyond type */
81 /* Validate that only whitespace follows. */
82 slide
+= strspn(slide
, " \t\r");
83 /* There must be whitespace. */
87 /* Mark beginning of nsid. */
89 /* Validate that non-whitespace follows. */
90 slide
+= strcspn(slide
, " \t\r");
91 /* There must be non-whitespace. */
92 if (slide
== window
|| *slide
== '\0')
94 /* Mark end of nsid. */
98 if (lxc_safe_ulong(window
, &tmp_nsid
) < 0) {
99 ERROR("Failed to parse nsid: %s", window
);
103 /* Move beyond \0. */
105 /* Validate that only whitespace follows. */
106 slide
+= strspn(slide
, " \t\r");
107 /* If there was only one whitespace then we whiped it with our \0 above.
108 * So only ensure that we're not at the end of the string.
113 /* Mark beginning of hostid. */
115 /* Validate that non-whitespace follows. */
116 slide
+= strcspn(slide
, " \t\r");
117 /* There must be non-whitespace. */
118 if (slide
== window
|| *slide
== '\0')
120 /* Mark end of nsid. */
124 if (lxc_safe_ulong(window
, &tmp_hostid
) < 0) {
125 ERROR("Failed to parse hostid: %s", window
);
129 /* Move beyond \0. */
131 /* Validate that only whitespace follows. */
132 slide
+= strspn(slide
, " \t\r");
133 /* If there was only one whitespace then we whiped it with our \0 above.
134 * So only ensure that we're not at the end of the string.
139 /* Mark beginning of range. */
141 /* Validate that non-whitespace follows. */
142 slide
+= strcspn(slide
, " \t\r");
143 /* There must be non-whitespace. */
147 /* The range is the last valid entry we expect. So make sure that there
148 * is no trailing garbage and if there is, error out.
150 if (*(slide
+ strspn(slide
, " \t\r\n")) != '\0')
152 /* Mark end of range. */
156 if (lxc_safe_ulong(window
, &tmp_range
) < 0) {
157 ERROR("Failed to parse id mapping range: %s", window
);
163 *hostid
= tmp_hostid
;
166 /* Yay, we survived. */
175 bool lxc_config_value_empty(const char *value
)
177 if (value
&& strlen(value
) > 0)
183 struct lxc_netdev
*lxc_network_add(struct lxc_list
*networks
, int idx
, bool tail
)
185 struct lxc_list
*newlist
;
186 struct lxc_netdev
*netdev
= NULL
;
188 /* network does not exist */
189 netdev
= malloc(sizeof(*netdev
));
193 memset(netdev
, 0, sizeof(*netdev
));
194 lxc_list_init(&netdev
->ipv4
);
195 lxc_list_init(&netdev
->ipv6
);
197 /* give network a unique index */
200 /* prepare new list */
201 newlist
= malloc(sizeof(*newlist
));
207 lxc_list_init(newlist
);
208 newlist
->elem
= netdev
;
211 lxc_list_add_tail(networks
, newlist
);
213 lxc_list_add(networks
, newlist
);
217 /* Takes care of finding the correct netdev struct in the networks list or
218 * allocates a new one if it couldn't be found.
220 struct lxc_netdev
*lxc_get_netdev_by_idx(struct lxc_conf
*conf
,
221 unsigned int idx
, bool allocate
)
223 struct lxc_netdev
*netdev
= NULL
;
224 struct lxc_list
*networks
= &conf
->network
;
225 struct lxc_list
*insert
= networks
;
228 if (!lxc_list_empty(networks
)) {
229 lxc_list_for_each(insert
, networks
) {
230 netdev
= insert
->elem
;
231 if (netdev
->idx
== idx
)
233 else if (netdev
->idx
> idx
)
241 return lxc_network_add(insert
, idx
, true);
244 void lxc_log_configured_netdevs(const struct lxc_conf
*conf
)
246 struct lxc_netdev
*netdev
;
247 struct lxc_list
*it
= (struct lxc_list
*)&conf
->network
;;
249 if ((conf
->loglevel
!= LXC_LOG_LEVEL_TRACE
) &&
250 (lxc_log_get_level() != LXC_LOG_LEVEL_TRACE
))
253 if (lxc_list_empty(it
)) {
254 TRACE("container has no networks configured");
258 lxc_list_for_each(it
, &conf
->network
) {
259 struct lxc_list
*cur
, *next
;
260 struct lxc_inetdev
*inet4dev
;
261 struct lxc_inet6dev
*inet6dev
;
262 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
266 TRACE("index: %zd", netdev
->idx
);
267 TRACE("ifindex: %d", netdev
->ifindex
);
268 switch (netdev
->type
) {
271 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
272 TRACE("veth pair: %s",
273 netdev
->priv
.veth_attr
.pair
);
274 if (netdev
->priv
.veth_attr
.veth1
[0] != '\0')
276 netdev
->priv
.veth_attr
.veth1
);
277 if (netdev
->priv
.veth_attr
.ifindex
> 0)
278 TRACE("host side ifindex for veth device: %d",
279 netdev
->priv
.veth_attr
.ifindex
);
281 case LXC_NET_MACVLAN
:
282 TRACE("type: macvlan");
283 if (netdev
->priv
.macvlan_attr
.mode
> 0) {
285 macvlan_mode
= lxc_macvlan_flag_to_mode(
286 netdev
->priv
.macvlan_attr
.mode
);
287 TRACE("macvlan mode: %s",
288 macvlan_mode
? macvlan_mode
294 TRACE("vlan id: %d", netdev
->priv
.vlan_attr
.vid
);
298 if (netdev
->priv
.phys_attr
.ifindex
> 0) {
299 TRACE("host side ifindex for phys device: %d",
300 netdev
->priv
.phys_attr
.ifindex
);
304 TRACE("type: empty");
310 ERROR("invalid network type %d", netdev
->type
);
314 if (netdev
->type
!= LXC_NET_EMPTY
) {
316 netdev
->flags
== IFF_UP
? "up" : "none");
317 if (netdev
->link
[0] != '\0')
318 TRACE("link: %s", netdev
->link
);
319 if (netdev
->name
[0] != '\0')
320 TRACE("name: %s", netdev
->name
);
322 TRACE("hwaddr: %s", netdev
->hwaddr
);
324 TRACE("mtu: %s", netdev
->mtu
);
325 if (netdev
->upscript
)
326 TRACE("upscript: %s", netdev
->upscript
);
327 if (netdev
->downscript
)
328 TRACE("downscript: %s", netdev
->downscript
);
330 TRACE("ipv4 gateway auto: %s",
331 netdev
->ipv4_gateway_auto
? "true" : "false");
333 if (netdev
->ipv4_gateway
) {
334 inet_ntop(AF_INET
, netdev
->ipv4_gateway
,
335 bufinet4
, sizeof(bufinet4
));
336 TRACE("ipv4 gateway: %s", bufinet4
);
339 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
340 inet4dev
= cur
->elem
;
341 inet_ntop(AF_INET
, &inet4dev
->addr
, bufinet4
,
343 TRACE("ipv4 addr: %s", bufinet4
);
346 TRACE("ipv6 gateway auto: %s",
347 netdev
->ipv6_gateway_auto
? "true" : "false");
348 if (netdev
->ipv6_gateway
) {
349 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
,
350 bufinet6
, sizeof(bufinet6
));
351 TRACE("ipv6 gateway: %s", bufinet6
);
353 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
354 inet6dev
= cur
->elem
;
355 inet_ntop(AF_INET6
, &inet6dev
->addr
, bufinet6
,
357 TRACE("ipv6 addr: %s", bufinet6
);
363 static void lxc_free_netdev(struct lxc_netdev
*netdev
)
365 struct lxc_list
*cur
, *next
;
367 free(netdev
->upscript
);
368 free(netdev
->downscript
);
369 free(netdev
->hwaddr
);
372 free(netdev
->ipv4_gateway
);
373 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
379 free(netdev
->ipv6_gateway
);
380 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
389 bool lxc_remove_nic_by_idx(struct lxc_conf
*conf
, unsigned int idx
)
391 struct lxc_list
*cur
, *next
;
392 struct lxc_netdev
*netdev
;
395 lxc_list_for_each_safe(cur
, &conf
->network
, next
) {
397 if (netdev
->idx
!= idx
)
408 lxc_free_netdev(netdev
);
414 void lxc_free_networks(struct lxc_list
*networks
)
416 struct lxc_list
*cur
, *next
;
417 struct lxc_netdev
*netdev
;
419 lxc_list_for_each_safe(cur
, networks
, next
) {
421 lxc_free_netdev(netdev
);
425 /* prevent segfaults */
426 lxc_list_init(networks
);
429 static struct macvlan_mode
{
433 { "private", MACVLAN_MODE_PRIVATE
},
434 { "vepa", MACVLAN_MODE_VEPA
},
435 { "bridge", MACVLAN_MODE_BRIDGE
},
436 { "passthru", MACVLAN_MODE_PASSTHRU
},
439 int lxc_macvlan_mode_to_flag(int *mode
, const char *value
)
443 for (i
= 0; i
< sizeof(macvlan_mode
) / sizeof(macvlan_mode
[0]); i
++) {
444 if (strcmp(macvlan_mode
[i
].name
, value
))
447 *mode
= macvlan_mode
[i
].mode
;
454 char *lxc_macvlan_flag_to_mode(int mode
)
458 for (i
= 0; i
< sizeof(macvlan_mode
) / sizeof(macvlan_mode
[0]); i
++) {
459 if (macvlan_mode
[i
].mode
== mode
)
462 return macvlan_mode
[i
].name
;
468 int set_config_string_item(char **conf_item
, const char *value
)
472 if (lxc_config_value_empty(value
)) {
478 new_value
= strdup(value
);
480 SYSERROR("failed to duplicate string \"%s\"", value
);
485 *conf_item
= new_value
;
489 int set_config_string_item_max(char **conf_item
, const char *value
, size_t max
)
491 if (strlen(value
) >= max
) {
492 ERROR("%s is too long (>= %lu)", value
, (unsigned long)max
);
496 return set_config_string_item(conf_item
, value
);
499 int set_config_path_item(char **conf_item
, const char *value
)
501 return set_config_string_item_max(conf_item
, value
, PATH_MAX
);
504 int config_ip_prefix(struct in_addr
*addr
)
506 if (IN_CLASSA(addr
->s_addr
))
507 return 32 - IN_CLASSA_NSHIFT
;
508 if (IN_CLASSB(addr
->s_addr
))
509 return 32 - IN_CLASSB_NSHIFT
;
510 if (IN_CLASSC(addr
->s_addr
))
511 return 32 - IN_CLASSC_NSHIFT
;
516 int network_ifname(char *valuep
, const char *value
, size_t size
)
520 if (!valuep
|| !value
)
523 retlen
= strlcpy(valuep
, value
, size
);
524 if (retlen
>= size
) {
525 ERROR("Network devie name \"%s\" is too long (>= %zu)", value
,
532 int rand_complete_hwaddr(char *hwaddr
)
534 const char hex
[] = "0123456789abcdef";
542 seed
= randseed(false);
544 while (*curs
!= '\0' && *curs
!= '\n') {
545 if (*curs
== 'x' || *curs
== 'X') {
546 if (curs
- hwaddr
== 1) {
547 /* ensure address is unicast */
549 *curs
= hex
[rand_r(&seed
) & 0x0E];
551 *curs
= hex
[rand_r(&seed
) & 0x0F];
553 *curs
= hex
[rand() & 0x0E];
555 *curs
= hex
[rand() & 0x0F];
564 bool lxc_config_net_hwaddr(const char *line
)
569 if (strncmp(line
, "lxc.net", 7) != 0)
571 if (strncmp(line
, "lxc.net.hwaddr", 14) == 0)
573 if (strncmp(line
, "lxc.network.hwaddr", 18) == 0)
575 if (sscanf(line
, "lxc.net.%u.%6s", &index
, tmp
) == 2 || sscanf(line
, "lxc.network.%u.%6s", &index
, tmp
) == 2)
576 return strncmp(tmp
, "hwaddr", 6) == 0;
582 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
583 * file, we expand it in the unexpanded_config, so that after a save_config we
584 * store the hwaddr for re-use.
585 * This is only called when reading the config file, not when executing a
587 * 'x' and 'X' are substituted in-place.
589 void update_hwaddr(const char *line
)
593 line
+= lxc_char_left_gc(line
, strlen(line
));
597 if (!lxc_config_net_hwaddr(line
))
600 /* Let config_net_hwaddr raise the error. */
601 p
= strchr(line
, '=');
612 rand_complete_hwaddr(p
);
615 bool new_hwaddr(char *hwaddr
)
619 (void)randseed(true);
621 ret
= snprintf(hwaddr
, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255,
622 rand() % 255, rand() % 255);
623 if (ret
< 0 || ret
>= 18) {
624 SYSERROR("Failed to call snprintf().");
631 int lxc_get_conf_str(char *retv
, int inlen
, const char *value
)
638 value_len
= strlen(value
);
639 if (retv
&& inlen
>= value_len
+ 1)
640 memcpy(retv
, value
, value_len
+ 1);
642 return strlen(value
);
645 int lxc_get_conf_int(struct lxc_conf
*c
, char *retv
, int inlen
, int v
)
653 memset(retv
, 0, inlen
);
655 strprint(retv
, inlen
, "%d", v
);
660 int lxc_get_conf_size_t(struct lxc_conf
*c
, char *retv
, int inlen
, size_t v
)
668 memset(retv
, 0, inlen
);
670 strprint(retv
, inlen
, "%zu", v
);
675 int lxc_get_conf_uint64(struct lxc_conf
*c
, char *retv
, int inlen
, uint64_t v
)
683 memset(retv
, 0, inlen
);
685 strprint(retv
, inlen
, "%"PRIu64
, v
);
690 bool parse_limit_value(const char **value
, rlim_t
*res
)
694 if (strncmp(*value
, "unlimited", sizeof("unlimited") - 1) == 0) {
695 *res
= RLIM_INFINITY
;
696 *value
+= sizeof("unlimited") - 1;
701 *res
= strtoull(*value
, &endptr
, 10);
702 if (errno
|| !endptr
)
709 static int lxc_container_name_to_pid(const char *lxcname_or_pid
,
716 pid
= strtol(lxcname_or_pid
, &err
, 10);
717 if (*err
!= '\0' || pid
< 1) {
718 struct lxc_container
*c
;
720 c
= lxc_container_new(lxcname_or_pid
, lxcpath
);
722 ERROR("\"%s\" is not a valid pid nor a container name",
727 if (!c
->may_control(c
)) {
728 ERROR("Insufficient privileges to control container "
730 lxc_container_put(c
);
734 pid
= c
->init_pid(c
);
736 ERROR("Container \"%s\" is not running", c
->name
);
737 lxc_container_put(c
);
741 lxc_container_put(c
);
746 SYSERROR("Failed to send signal to pid %d", (int)pid
);
753 int lxc_inherit_namespace(const char *lxcname_or_pid
, const char *lxcpath
,
754 const char *namespace)
757 char *dup
, *lastslash
;
759 lastslash
= strrchr(lxcname_or_pid
, '/');
761 dup
= strdup(lxcname_or_pid
);
765 dup
[lastslash
- lxcname_or_pid
] = '\0';
766 pid
= lxc_container_name_to_pid(lastslash
+ 1, dup
);
769 pid
= lxc_container_name_to_pid(lxcname_or_pid
, lxcpath
);
775 fd
= lxc_preserve_ns(pid
, namespace);