confile_utils: add new file

This adds confile_utils.{c,h} which will contain a helpers to parse lxc configuration files. Signed-off-by: 's avatarChristian Brauner <christian.brauner@ubuntu.com>
parent a9849a06
...@@ -20,6 +20,8 @@ noinst_HEADERS = \ ...@@ -20,6 +20,8 @@ noinst_HEADERS = \
cgroups/cgroup.h \ cgroups/cgroup.h \
caps.h \ caps.h \
conf.h \ conf.h \
confile.h \
confile_utils.h \
console.h \ console.h \
error.h \ error.h \
initutils.h \ initutils.h \
...@@ -101,6 +103,7 @@ liblxc_la_SOURCES = \ ...@@ -101,6 +103,7 @@ liblxc_la_SOURCES = \
namespace.h namespace.c \ namespace.h namespace.c \
conf.c conf.h \ conf.c conf.h \
confile.c confile.h \ confile.c confile.h \
confile_utils.c confile_utils.h \
list.h \ list.h \
state.c state.h \ state.c state.h \
log.c log.h \ log.c log.h \
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
#include "parse.h" #include "parse.h"
#include "config.h" #include "config.h"
#include "confile.h" #include "confile.h"
#include "confile_utils.h"
#include "utils.h" #include "utils.h"
#include "log.h" #include "log.h"
#include "conf.h" #include "conf.h"
...@@ -1984,8 +1985,7 @@ static int set_config_idmaps(const char *key, const char *value, ...@@ -1984,8 +1985,7 @@ static int set_config_idmaps(const char *key, const char *value,
{ {
unsigned long hostid, nsid, range; unsigned long hostid, nsid, range;
char type; char type;
char *window, *slide; int ret;
char *dup = NULL;
struct lxc_list *idmaplist = NULL; struct lxc_list *idmaplist = NULL;
struct id_map *idmap = NULL; struct id_map *idmap = NULL;
...@@ -2001,109 +2001,10 @@ static int set_config_idmaps(const char *key, const char *value, ...@@ -2001,109 +2001,10 @@ static int set_config_idmaps(const char *key, const char *value,
goto on_error; goto on_error;
memset(idmap, 0, sizeof(*idmap)); memset(idmap, 0, sizeof(*idmap));
/* Duplicate string. */ ret = parse_idmaps(value, &type, &nsid, &hostid, &range);
dup = strdup(value); if (ret < 0)
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. */
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; goto on_error;
/* Yay, we survived. */
INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range); INFO("read uid map: type %c nsid %lu hostid %lu range %lu", type, nsid, hostid, range);
if (type == 'u') if (type == 'u')
idmap->idtype = ID_TYPE_UID; idmap->idtype = ID_TYPE_UID;
...@@ -2124,7 +2025,6 @@ static int set_config_idmaps(const char *key, const char *value, ...@@ -2124,7 +2025,6 @@ static int set_config_idmaps(const char *key, const char *value,
on_error: on_error:
free(idmaplist); free(idmaplist);
free(idmap); free(idmap);
free(dup);
return -1; return -1;
} }
......
/* liblxcapi
*
* Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
* 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 <stdio.h>
#include <string.h>
#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;
}
/* liblxcapi
*
* Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
* 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 */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment