1/* Copyright (C) 2015-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
17
18#include <nss.h>
19#include <string.h>
20
21/* Rewrite VALUE to a valid field value in the NSS database. Invalid
22 characters are replaced with a single space character ' '. If
23 VALUE is NULL, the empty string is returned. *TO_BE_FREED is
24 overwritten with a pointer the caller has to free if the function
25 returns successfully. On failure, return NULL. */
26const char *
27internal_function
28__nss_rewrite_field (const char *value, char **to_be_freed)
29{
30 *to_be_freed = NULL;
31 if (value == NULL)
32 return "";
33
34 /* Search for non-allowed characters. */
35 const char *p = strpbrk (value, __nss_invalid_field_characters);
36 if (p == NULL)
37 return value;
38 *to_be_freed = __strdup (value);
39 if (*to_be_freed == NULL)
40 return NULL;
41
42 /* Switch pointer to freshly-allocated buffer. */
43 char *bad = *to_be_freed + (p - value);
44 do
45 {
46 *bad = ' ';
47 bad = strpbrk (bad + 1, __nss_invalid_field_characters);
48 }
49 while (bad != NULL);
50
51 return *to_be_freed;
52}
53