From: Christian Brauner Date: Thu, 1 Jun 2017 21:43:16 +0000 (+0200) Subject: confile_utils: add new file X-Git-Tag: lxc-2.1.0~106^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0b843d358a41628ea4bdcc4b1dfcb59b3e191fd8;p=thirdparty%2Flxc.git confile_utils: add new file This adds confile_utils.{c,h} which will contain a helpers to parse lxc configuration files. Signed-off-by: Christian Brauner --- diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am index a6a69decf..9f0c7a743 100644 --- a/src/lxc/Makefile.am +++ b/src/lxc/Makefile.am @@ -20,6 +20,8 @@ noinst_HEADERS = \ cgroups/cgroup.h \ caps.h \ conf.h \ + confile.h \ + confile_utils.h \ console.h \ error.h \ initutils.h \ @@ -101,6 +103,7 @@ liblxc_la_SOURCES = \ namespace.h namespace.c \ conf.c conf.h \ confile.c confile.h \ + confile_utils.c confile_utils.h \ list.h \ state.c state.h \ log.c log.h \ diff --git a/src/lxc/confile.c b/src/lxc/confile.c index 4186d6347..be6c3b1d5 100644 --- a/src/lxc/confile.c +++ b/src/lxc/confile.c @@ -45,6 +45,7 @@ #include "parse.h" #include "config.h" #include "confile.h" +#include "confile_utils.h" #include "utils.h" #include "log.h" #include "conf.h" @@ -1984,8 +1985,7 @@ static int set_config_idmaps(const char *key, const char *value, { unsigned long hostid, nsid, range; char type; - char *window, *slide; - char *dup = NULL; + int ret; struct lxc_list *idmaplist = NULL; struct id_map *idmap = NULL; @@ -2001,109 +2001,10 @@ static int set_config_idmaps(const char *key, const char *value, goto on_error; memset(idmap, 0, sizeof(*idmap)); - /* Duplicate string. */ - dup = strdup(value); - if (!dup) + ret = parse_idmaps(value, &type, &nsid, &hostid, &range); + if (ret < 0) goto on_error; - /* A prototypical idmap entry would be: "u 1000 1000000 65536" */ - - /* align */ - slide = window = dup; - /* skip whitespace */ - slide += strspn(slide, " \t\r"); - if (slide != window && *slide == '\0') - goto on_error; - - /* Validate type. */ - if (*slide != 'u' && *slide != 'g') - goto on_error; - /* Assign type. */ - type = *slide; - - /* move beyond type */ - slide++; - /* align */ - window = slide; - /* Validate that only whitespace follows. */ - slide += strspn(slide, " \t\r"); - /* There must be whitespace. */ - if (slide == window) - goto on_error; - - /* Mark beginning of nsuid. */ - window = slide; - /* Validate that non-whitespace follows. */ - slide += strcspn(slide, " \t\r"); - /* There must be non-whitespace. */ - if (slide == window || *slide == '\0') - goto on_error; - /* Mark end of nsuid. */ - *slide = '\0'; - - /* Parse nsuid. */ - if (lxc_safe_ulong(window, &nsid) < 0) - goto on_error; - - /* Move beyond \0. */ - slide++; - /* align */ - window = slide; - /* Validate that only whitespace follows. */ - slide += strspn(slide, " \t\r"); - /* If there was only one whitespace then we whiped it with our \0 above. - * So only ensure that we're not at the end of the string. - */ - if (*slide == '\0') - goto on_error; - - /* Mark beginning of hostid. */ - window = slide; - /* Validate that non-whitespace follows. */ - slide += strcspn(slide, " \t\r"); - /* There must be non-whitespace. */ - if (slide == window || *slide == '\0') - goto on_error; - /* Mark end of nsuid. */ - *slide = '\0'; - - /* Parse hostid. */ - if (lxc_safe_ulong(window, &hostid) < 0) - goto on_error; - - /* Move beyond \0. */ - slide++; - /* align */ - window = slide; - /* Validate that only whitespace follows. */ - slide += strspn(slide, " \t\r"); - /* If there was only one whitespace then we whiped it with our \0 above. - * So only ensure that we're not at the end of the string. - */ - if (*slide == '\0') - goto on_error; - - /* Mark beginning of range. */ - window = slide; - /* Validate that non-whitespace follows. */ - slide += strcspn(slide, " \t\r"); - /* There must be non-whitespace. */ - if (slide == window) - goto on_error; - - /* The range is the last valid entry we expect. So make sure that there - * is not trailing garbage and if there is, error out. - */ - if (*(slide + strspn(slide, " \t\r\n")) != '\0') - goto on_error; - /* Mark end of range. */ - *slide = '\0'; - - /* Parse range. */ - if (lxc_safe_ulong(window, &range) < 0) - goto on_error; - - /* Yay, we survived. */ INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range); if (type == 'u') idmap->idtype = ID_TYPE_UID; @@ -2124,7 +2025,6 @@ static int set_config_idmaps(const char *key, const char *value, on_error: free(idmaplist); free(idmap); - free(dup); return -1; } diff --git a/src/lxc/confile_utils.c b/src/lxc/confile_utils.c new file mode 100644 index 000000000..2018f8e9e --- /dev/null +++ b/src/lxc/confile_utils.c @@ -0,0 +1,148 @@ +/* liblxcapi + * + * Copyright © 2017 Christian Brauner . + * Copyright © 2017 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include + +#include "utils.h" + +int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, + unsigned long *hostid, unsigned long *range) +{ + int ret = -1; + unsigned long tmp_hostid, tmp_nsid, tmp_range; + char tmp_type; + char *window, *slide; + char *dup = NULL; + + /* Duplicate string. */ + dup = strdup(idmap); + if (!dup) + goto on_error; + + /* A prototypical idmap entry would be: "u 1000 1000000 65536" */ + + /* align */ + slide = window = dup; + /* skip whitespace */ + slide += strspn(slide, " \t\r"); + if (slide != window && *slide == '\0') + goto on_error; + + /* Validate type. */ + if (*slide != 'u' && *slide != 'g') + goto on_error; + /* Assign type. */ + tmp_type = *slide; + + /* move beyond type */ + slide++; + /* align */ + window = slide; + /* Validate that only whitespace follows. */ + slide += strspn(slide, " \t\r"); + /* There must be whitespace. */ + if (slide == window) + goto on_error; + + /* Mark beginning of nsuid. */ + window = slide; + /* Validate that non-whitespace follows. */ + slide += strcspn(slide, " \t\r"); + /* There must be non-whitespace. */ + if (slide == window || *slide == '\0') + goto on_error; + /* Mark end of nsuid. */ + *slide = '\0'; + + /* Parse nsuid. */ + if (lxc_safe_ulong(window, &tmp_nsid) < 0) + goto on_error; + + /* Move beyond \0. */ + slide++; + /* align */ + window = slide; + /* Validate that only whitespace follows. */ + slide += strspn(slide, " \t\r"); + /* If there was only one whitespace then we whiped it with our \0 above. + * So only ensure that we're not at the end of the string. + */ + if (*slide == '\0') + goto on_error; + + /* Mark beginning of hostid. */ + window = slide; + /* Validate that non-whitespace follows. */ + slide += strcspn(slide, " \t\r"); + /* There must be non-whitespace. */ + if (slide == window || *slide == '\0') + goto on_error; + /* Mark end of nsuid. */ + *slide = '\0'; + + /* Parse hostid. */ + if (lxc_safe_ulong(window, &tmp_hostid) < 0) + goto on_error; + + /* Move beyond \0. */ + slide++; + /* align */ + window = slide; + /* Validate that only whitespace follows. */ + slide += strspn(slide, " \t\r"); + /* If there was only one whitespace then we whiped it with our \0 above. + * So only ensure that we're not at the end of the string. + */ + if (*slide == '\0') + goto on_error; + + /* Mark beginning of range. */ + window = slide; + /* Validate that non-whitespace follows. */ + slide += strcspn(slide, " \t\r"); + /* There must be non-whitespace. */ + if (slide == window) + goto on_error; + + /* The range is the last valid entry we expect. So make sure that there + * is not trailing garbage and if there is, error out. + */ + if (*(slide + strspn(slide, " \t\r\n")) != '\0') + goto on_error; + /* Mark end of range. */ + *slide = '\0'; + + /* Parse range. */ + if (lxc_safe_ulong(window, &tmp_range) < 0) + goto on_error; + + *type = tmp_type; + *nsid = tmp_nsid; + *hostid = tmp_hostid; + *range = tmp_range; + + /* Yay, we survived. */ + ret = 0; + +on_error: + free(dup); + + return ret; +} diff --git a/src/lxc/confile_utils.h b/src/lxc/confile_utils.h new file mode 100644 index 000000000..758d96ad6 --- /dev/null +++ b/src/lxc/confile_utils.h @@ -0,0 +1,26 @@ +/* liblxcapi + * + * Copyright © 2017 Christian Brauner . + * Copyright © 2017 Canonical Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2, as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef __LXC_CONFILE_UTILS_H +#define __LXC_CONFILE_UTILS_H + +extern int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, + unsigned long *hostid, unsigned long *range); + +#endif /* __LXC_CONFILE_UTILS_H */