Replace FSF snail mail address with URLs.
[glibc.git] / nss / nss_db / db-initgroups.c
blob0d44e00b1b3a7c826aa7f7d4c453bca50697790f
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
18 not, see <http://www.gnu.org/licenses/>. */
20 #include <ctype.h>
21 #include <errno.h>
22 #include <grp.h>
23 #include <paths.h>
25 #include "nss_db.h"
27 /* The hashing function we use. */
28 #include "../intl/hash-string.h"
31 enum nss_status
32 _nss_db_initgroups_dyn (const char *user, gid_t group, long int *start,
33 long int *size, gid_t **groupsp, long int limit,
34 int *errnop)
36 struct nss_db_map state = { NULL, 0 };
37 enum nss_status status = internal_setent (_PATH_VARDB "group.db", &state);
38 if (status != NSS_STATUS_SUCCESS)
40 *errnop = errno;
41 return status;
44 const struct nss_db_header *header = state.header;
45 int i;
46 for (i = 0; i < header->ndbs; ++i)
47 if (header->dbs[i].id == ':')
48 break;
49 if (i == header->ndbs)
51 status = NSS_STATUS_UNAVAIL;
52 goto out;
55 const stridx_t *hashtable
56 = (const stridx_t *) ((const char *) header
57 + header->dbs[i].hashoffset);
58 const char *valstrtab = (const char *) header + header->valstroffset;
59 size_t userlen = strlen (user);
60 uint32_t hashval = __hash_string (user);
61 size_t hidx = hashval % header->dbs[i].hashsize;
62 size_t hval2 = 1 + hashval % (header->dbs[i].hashsize - 2);
64 gid_t *groups = *groupsp;
66 status = NSS_STATUS_NOTFOUND;
67 while (hashtable[hidx] != ~((stridx_t) 0))
69 const char *valstr = valstrtab + hashtable[hidx];
70 while (isblank (*valstr))
71 ++valstr;
73 if (strncmp (valstr, user, userlen) == 0 && isblank (valstr[userlen]))
75 valstr += userlen + 1;
76 while (isblank (*valstr))
77 ++valstr;
79 while (*valstr != '\0')
81 errno = 0;
82 char *endp;
83 unsigned long int n = strtoul (valstr, &endp, 10);
84 if (*endp != ',' && *endp != '\0')
85 break;
86 valstr = *endp == '\0' ? endp : endp + 1;
88 if (n != ULONG_MAX || errno != ERANGE)
90 /* Insert the group. */
91 if (*start == *size)
93 /* Need a bigger buffer. */
94 if (limit > 0 && *size == limit)
96 /* We reached the maximum. */
97 status = NSS_STATUS_SUCCESS;
98 goto out;
101 long int newsize;
102 if (limit <= 0)
103 newsize = 2 * *size;
104 else
105 newsize = MIN (limit, 2 * *size);
107 gid_t *newgroups = realloc (groups,
108 newsize * sizeof (*groups));
109 if (newgroups == NULL)
111 *errnop = ENOMEM;
112 status = NSS_STATUS_TRYAGAIN;
113 goto out;
116 *groupsp = groups = newgroups;
117 *size = newsize;
120 groups[*start] = n;
121 *start += 1;
125 status = NSS_STATUS_SUCCESS;
126 break;
129 if ((hidx += hval2) >= header->dbs[i].hashsize)
130 hidx -= header->dbs[i].hashsize;
133 out:
134 internal_endent (&state);
136 return status;