Support C11 __STDC_SOURCE__ and _ISOC11_SOURCE
[glibc.git] / nss / nss_db / db-initgroups.c
blobaa8163b5b0b5317a9caaec055226d286fe743d47
1 /* Initgroups handling in nss_db module.
2 Copyright (C) 2011 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@gmail.com>.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If not,
18 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include <ctype.h>
22 #include <errno.h>
23 #include <grp.h>
24 #include <paths.h>
26 #include "nss_db.h"
28 /* The hashing function we use. */
29 #include "../intl/hash-string.h"
32 enum nss_status
33 _nss_db_initgroups_dyn (const char *user, gid_t group, long int *start,
34 long int *size, gid_t **groupsp, long int limit,
35 int *errnop)
37 struct nss_db_map state = { NULL, 0 };
38 enum nss_status status = internal_setent (_PATH_VARDB "group.db", &state);
39 if (status != NSS_STATUS_SUCCESS)
41 *errnop = errno;
42 return status;
45 const struct nss_db_header *header = state.header;
46 int i;
47 for (i = 0; i < header->ndbs; ++i)
48 if (header->dbs[i].id == ':')
49 break;
50 if (i == header->ndbs)
52 status = NSS_STATUS_UNAVAIL;
53 goto out;
56 const stridx_t *hashtable
57 = (const stridx_t *) ((const char *) header
58 + header->dbs[i].hashoffset);
59 const char *valstrtab = (const char *) header + header->valstroffset;
60 size_t userlen = strlen (user);
61 uint32_t hashval = __hash_string (user);
62 size_t hidx = hashval % header->dbs[i].hashsize;
63 size_t hval2 = 1 + hashval % (header->dbs[i].hashsize - 2);
65 gid_t *groups = *groupsp;
67 status = NSS_STATUS_NOTFOUND;
68 while (hashtable[hidx] != ~((stridx_t) 0))
70 const char *valstr = valstrtab + hashtable[hidx];
71 while (isblank (*valstr))
72 ++valstr;
74 if (strncmp (valstr, user, userlen) == 0 && isblank (valstr[userlen]))
76 valstr += userlen + 1;
77 while (isblank (*valstr))
78 ++valstr;
80 while (*valstr != '\0')
82 errno = 0;
83 char *endp;
84 unsigned long int n = strtoul (valstr, &endp, 10);
85 if (*endp != ',' && *endp != '\0')
86 break;
87 valstr = *endp == '\0' ? endp : endp + 1;
89 if (n != ULONG_MAX || errno != ERANGE)
91 /* Insert the group. */
92 if (*start == *size)
94 /* Need a bigger buffer. */
95 if (limit > 0 && *size == limit)
97 /* We reached the maximum. */
98 status = NSS_STATUS_SUCCESS;
99 goto out;
102 long int newsize;
103 if (limit <= 0)
104 newsize = 2 * *size;
105 else
106 newsize = MIN (limit, 2 * *size);
108 gid_t *newgroups = realloc (groups,
109 newsize * sizeof (*groups));
110 if (newgroups == NULL)
112 *errnop = ENOMEM;
113 status = NSS_STATUS_TRYAGAIN;
114 goto out;
117 *groupsp = groups = newgroups;
118 *size = newsize;
121 groups[*start] = n;
122 *start += 1;
126 status = NSS_STATUS_SUCCESS;
127 break;
130 if ((hidx += hval2) >= header->dbs[i].hashsize)
131 hidx -= header->dbs[i].hashsize;
134 out:
135 internal_endent (&state);
137 return status;