|
Packit |
6c4009 |
/* Common code for file-based database parsers in nss_files module.
|
|
Packit |
6c4009 |
Copyright (C) 1996-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <ctype.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <stdint.h>
|
|
Packit Service |
0793fb |
#include <nss_files.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* These symbols are defined by the including source file:
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ENTNAME -- database name of the structure and functions (hostent, pwent).
|
|
Packit |
6c4009 |
STRUCTURE -- struct name, define only if not ENTNAME (passwd, group).
|
|
Packit |
6c4009 |
DATABASE -- string of the database file's name ("hosts", "passwd").
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
ENTDATA -- if defined, `struct ENTDATA' is used by the parser to store
|
|
Packit |
6c4009 |
things pointed to by the resultant `struct STRUCTURE'.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
NEED_H_ERRNO - defined iff an arg `int *herrnop' is used.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
EXTRA_ARGS -- defined iff extra parameters must be passed to the parser
|
|
Packit |
6c4009 |
EXTRA_ARGS_DECL -- declaration for these extra parameters
|
|
Packit |
6c4009 |
EXTRA_ARGS_VALUE -- values to be passed for these parameters
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
Also see files-XXX.c. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef EXTRA_ARGS
|
|
Packit |
6c4009 |
# define EXTRA_ARGS
|
|
Packit |
6c4009 |
# define EXTRA_ARGS_DECL
|
|
Packit |
6c4009 |
# define EXTRA_ARGS_VALUE
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define CONCAT(a,b) CONCAT1(a,b)
|
|
Packit |
6c4009 |
#define CONCAT1(a,b) a##b
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifndef STRUCTURE
|
|
Packit |
6c4009 |
# define STRUCTURE ENTNAME
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
struct parser_data
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
#ifdef ENTDATA
|
|
Packit |
6c4009 |
struct ENTDATA entdata;
|
|
Packit |
6c4009 |
# define ENTDATA_DECL(data) struct ENTDATA *const entdata = &data->entdata;
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
# define ENTDATA_DECL(data)
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
char linebuffer[0];
|
|
Packit |
6c4009 |
};
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifdef ENTDATA
|
|
Packit |
6c4009 |
/* The function can't be exported, because the entdata structure
|
|
Packit |
6c4009 |
is defined only in files-foo.c. */
|
|
Packit |
6c4009 |
# define parser_stclass static
|
|
Packit |
6c4009 |
# define nss_files_parse_hidden_def(name)
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
/* Export the line parser function so it can be used in nss_db. */
|
|
Packit |
6c4009 |
# define parser_stclass /* Global */
|
|
Packit |
6c4009 |
# define parse_line CONCAT(_nss_files_parse_,ENTNAME)
|
|
Packit |
6c4009 |
# if IS_IN (libc)
|
|
Packit |
6c4009 |
/* We are defining one of the functions that actually lives in libc
|
|
Packit |
6c4009 |
because it is used to implement fget*ent and suchlike. */
|
|
Packit |
6c4009 |
# define nss_files_parse_hidden_def(name) libc_hidden_def (name)
|
|
Packit |
6c4009 |
# else
|
|
Packit |
6c4009 |
# define nss_files_parse_hidden_def(name) libnss_files_hidden_def (name)
|
|
Packit |
6c4009 |
# endif
|
|
Packit |
6c4009 |
#endif
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#ifdef EXTERN_PARSER
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* The parser is defined in a different module. */
|
|
Packit Service |
3e830d |
extern int parse_line (char *line, struct STRUCTURE *result,
|
|
Packit |
6c4009 |
struct parser_data *data, size_t datalen, int *errnop
|
|
Packit |
6c4009 |
EXTRA_ARGS_DECL);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# define LINE_PARSER(EOLSET, BODY) /* Do nothing */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#else
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Define a line parsing function. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# define LINE_PARSER(EOLSET, BODY) \
|
|
Packit |
6c4009 |
parser_stclass int \
|
|
Packit Service |
3e830d |
parse_line (char *line, struct STRUCTURE *result, \
|
|
Packit |
6c4009 |
struct parser_data *data, size_t datalen, int *errnop \
|
|
Packit |
6c4009 |
EXTRA_ARGS_DECL) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
ENTDATA_DECL (data) \
|
|
Packit |
6c4009 |
BUFFER_PREPARE \
|
|
Packit |
6c4009 |
char *p = strpbrk (line, EOLSET "\n"); \
|
|
Packit |
6c4009 |
if (p != NULL) \
|
|
Packit |
6c4009 |
*p = '\0'; \
|
|
Packit |
6c4009 |
BODY; \
|
|
Packit |
6c4009 |
TRAILING_LIST_PARSER; \
|
|
Packit |
6c4009 |
return 1; \
|
|
Packit |
6c4009 |
} \
|
|
Packit |
6c4009 |
nss_files_parse_hidden_def (parse_line)
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# define STRING_FIELD(variable, terminator_p, swallow) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
variable = line; \
|
|
Packit |
6c4009 |
while (*line != '\0' && !terminator_p (*line)) \
|
|
Packit |
6c4009 |
++line; \
|
|
Packit |
6c4009 |
if (*line != '\0') \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
*line = '\0'; \
|
|
Packit |
6c4009 |
do \
|
|
Packit |
6c4009 |
++line; \
|
|
Packit |
6c4009 |
while (swallow && terminator_p (*line)); \
|
|
Packit |
6c4009 |
} \
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# define STRING_LIST(variable, terminator_c) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
char **list = parse_list (&line, buf_start, buf_end, terminator_c, \
|
|
Packit |
6c4009 |
errnop); \
|
|
Packit |
6c4009 |
if (list) \
|
|
Packit |
6c4009 |
variable = list; \
|
|
Packit |
6c4009 |
else \
|
|
Packit |
6c4009 |
return -1; /* -1 indicates we ran out of space. */ \
|
|
Packit |
6c4009 |
\
|
|
Packit |
6c4009 |
/* Determine the new end of the buffer. */ \
|
|
Packit |
6c4009 |
while (*list != NULL) \
|
|
Packit |
6c4009 |
++list; \
|
|
Packit |
6c4009 |
buf_start = (char *) (list + 1); \
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Helper function. */
|
|
Packit |
6c4009 |
static inline uint32_t
|
|
Packit |
6c4009 |
__attribute__ ((always_inline))
|
|
Packit |
6c4009 |
strtou32 (const char *nptr, char **endptr, int base)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
unsigned long int val = strtoul (nptr, endptr, base);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Match the 32-bit behavior on 64-bit platforms. */
|
|
Packit |
6c4009 |
if (sizeof (long int) > 4 && val > 0xffffffff)
|
|
Packit |
6c4009 |
val = 0xffffffff;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return val;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# define INT_FIELD(variable, terminator_p, swallow, base, convert) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
char *endp; \
|
|
Packit |
6c4009 |
variable = convert (strtou32 (line, &endp, base)); \
|
|
Packit |
6c4009 |
if (endp == line) \
|
|
Packit |
6c4009 |
return 0; \
|
|
Packit |
6c4009 |
else if (terminator_p (*endp)) \
|
|
Packit |
6c4009 |
do \
|
|
Packit |
6c4009 |
++endp; \
|
|
Packit |
6c4009 |
while (swallow && terminator_p (*endp)); \
|
|
Packit |
6c4009 |
else if (*endp != '\0') \
|
|
Packit |
6c4009 |
return 0; \
|
|
Packit |
6c4009 |
line = endp; \
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# define INT_FIELD_MAYBE_NULL(variable, terminator_p, swallow, base, convert, default) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
char *endp; \
|
|
Packit |
6c4009 |
if (*line == '\0') \
|
|
Packit |
6c4009 |
/* We expect some more input, so don't allow the string to end here. */ \
|
|
Packit |
6c4009 |
return 0; \
|
|
Packit |
6c4009 |
variable = convert (strtou32 (line, &endp, base)); \
|
|
Packit |
6c4009 |
if (endp == line) \
|
|
Packit |
6c4009 |
variable = default; \
|
|
Packit |
6c4009 |
if (terminator_p (*endp)) \
|
|
Packit |
6c4009 |
do \
|
|
Packit |
6c4009 |
++endp; \
|
|
Packit |
6c4009 |
while (swallow && terminator_p (*endp)); \
|
|
Packit |
6c4009 |
else if (*endp != '\0') \
|
|
Packit |
6c4009 |
return 0; \
|
|
Packit |
6c4009 |
line = endp; \
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# define ISCOLON(c) ((c) == ':')
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# ifndef TRAILING_LIST_MEMBER
|
|
Packit |
6c4009 |
# define BUFFER_PREPARE /* Nothing to do. */
|
|
Packit |
6c4009 |
# define TRAILING_LIST_PARSER /* Nothing to do. */
|
|
Packit |
6c4009 |
# else
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# define BUFFER_PREPARE \
|
|
Packit |
6c4009 |
char *buf_start = NULL; \
|
|
Packit |
6c4009 |
char *buf_end = (char *) data + datalen; \
|
|
Packit |
6c4009 |
if (line >= data->linebuffer && line < buf_end) \
|
|
Packit |
6c4009 |
/* Find the end of the line buffer, we will use the space in \
|
|
Packit |
6c4009 |
DATA after it for storing the vector of pointers. */ \
|
|
Packit |
6c4009 |
buf_start = strchr (line, '\0') + 1; \
|
|
Packit |
6c4009 |
else \
|
|
Packit |
6c4009 |
/* LINE does not point within DATA->linebuffer, so that space is \
|
|
Packit |
6c4009 |
not being used for scratch space right now. We can use all of \
|
|
Packit |
6c4009 |
it for the pointer vector storage. */ \
|
|
Packit |
6c4009 |
buf_start = data->linebuffer; \
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# define TRAILING_LIST_PARSER \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
if (buf_start == NULL) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
if (line >= data->linebuffer && line < buf_end) \
|
|
Packit |
6c4009 |
/* Find the end of the line buffer, we will use the space in \
|
|
Packit |
6c4009 |
DATA after it for storing the vector of pointers. */ \
|
|
Packit |
6c4009 |
buf_start = strchr (line, '\0') + 1; \
|
|
Packit |
6c4009 |
else \
|
|
Packit |
6c4009 |
/* LINE does not point within DATA->linebuffer, so that space is \
|
|
Packit |
6c4009 |
not being used for scratch space right now. We can use all of \
|
|
Packit |
6c4009 |
it for the pointer vector storage. */ \
|
|
Packit |
6c4009 |
buf_start = data->linebuffer; \
|
|
Packit |
6c4009 |
} \
|
|
Packit |
6c4009 |
\
|
|
Packit |
6c4009 |
char **list = parse_list (&line, buf_start, buf_end, '\0', errnop); \
|
|
Packit |
6c4009 |
if (list) \
|
|
Packit |
6c4009 |
result->TRAILING_LIST_MEMBER = list; \
|
|
Packit |
6c4009 |
else \
|
|
Packit |
6c4009 |
return -1; /* -1 indicates we ran out of space. */ \
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static inline char **
|
|
Packit |
6c4009 |
__attribute ((always_inline))
|
|
Packit |
6c4009 |
parse_list (char **linep, char *eol, char *buf_end, int terminator_c,
|
|
Packit |
6c4009 |
int *errnop)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char *line = *linep;
|
|
Packit |
6c4009 |
char **list, **p;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Adjust the pointer so it is aligned for storing pointers. */
|
|
Packit |
6c4009 |
eol += __alignof__ (char *) - 1;
|
|
Packit |
6c4009 |
eol -= (eol - (char *) 0) % __alignof__ (char *);
|
|
Packit |
6c4009 |
/* We will start the storage here for the vector of pointers. */
|
|
Packit |
6c4009 |
list = (char **) eol;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
p = list;
|
|
Packit |
6c4009 |
while (1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if ((char *) (p + 2) > buf_end)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* We cannot fit another pointer in the buffer. */
|
|
Packit |
6c4009 |
*errnop = ERANGE;
|
|
Packit |
6c4009 |
return NULL;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (*line == '\0')
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
if (*line == terminator_c)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
++line;
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Skip leading white space. This might not be portable but useful. */
|
|
Packit |
6c4009 |
while (isspace (*line))
|
|
Packit |
6c4009 |
++line;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
char *elt = line;
|
|
Packit |
6c4009 |
while (1)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
if (*line == '\0' || *line == terminator_c
|
|
Packit |
6c4009 |
|| TRAILING_LIST_SEPARATOR_P (*line))
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
/* End of the next entry. */
|
|
Packit |
6c4009 |
if (line > elt)
|
|
Packit |
6c4009 |
/* We really found some data. */
|
|
Packit |
6c4009 |
*p++ = elt;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* Terminate string if necessary. */
|
|
Packit |
6c4009 |
if (*line != '\0')
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
char endc = *line;
|
|
Packit |
6c4009 |
*line++ = '\0';
|
|
Packit |
6c4009 |
if (endc == terminator_c)
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
break;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
++line;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
out:
|
|
Packit |
6c4009 |
*p = NULL;
|
|
Packit |
6c4009 |
*linep = line;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return list;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
# endif /* TRAILING_LIST_MEMBER */
|
|
Packit |
6c4009 |
#endif /* EXTERN_PARSER */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define LOOKUP_NAME(nameelt, aliaselt) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
char **ap; \
|
|
Packit |
6c4009 |
if (! strcmp (name, result->nameelt)) \
|
|
Packit |
6c4009 |
break; \
|
|
Packit |
6c4009 |
for (ap = result->aliaselt; *ap; ++ap) \
|
|
Packit |
6c4009 |
if (! strcmp (name, *ap)) \
|
|
Packit |
6c4009 |
break; \
|
|
Packit |
6c4009 |
if (*ap) \
|
|
Packit |
6c4009 |
break; \
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define LOOKUP_NAME_CASE(nameelt, aliaselt) \
|
|
Packit |
6c4009 |
{ \
|
|
Packit |
6c4009 |
char **ap; \
|
|
Packit |
6c4009 |
if (! __strcasecmp (name, result->nameelt)) \
|
|
Packit |
6c4009 |
break; \
|
|
Packit |
6c4009 |
for (ap = result->aliaselt; *ap; ++ap) \
|
|
Packit |
6c4009 |
if (! __strcasecmp (name, *ap)) \
|
|
Packit |
6c4009 |
break; \
|
|
Packit |
6c4009 |
if (*ap) \
|
|
Packit |
6c4009 |
break; \
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* This is defined by db-*.c to include "../nss_db/db-XXX.c" instead. */
|
|
Packit |
6c4009 |
#ifndef GENERIC
|
|
Packit |
6c4009 |
# define GENERIC "files-XXX.c"
|
|
Packit |
6c4009 |
#endif
|