Update copyright notices with scripts/update-copyrights.
[glibc.git] / nss / nss_db / db-initgroups.c
blob3038b2ed17d8d8d158d3aad98bca44a79ae623af
1 /* Initgroups handling in nss_db module.
2 Copyright (C) 2011-2013 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 <limits.h>
24 #include <paths.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/param.h>
29 #include "nss_db.h"
31 /* The hashing function we use. */
32 #include "../intl/hash-string.h"
35 enum nss_status
36 _nss_db_initgroups_dyn (const char *user, gid_t group, long int *start,
37 long int *size, gid_t **groupsp, long int limit,
38 int *errnop)
40 struct nss_db_map state = { NULL, 0 };
41 enum nss_status status = internal_setent (_PATH_VARDB "group.db", &state);
42 if (status != NSS_STATUS_SUCCESS)
44 *errnop = errno;
45 return status;
48 const struct nss_db_header *header = state.header;
49 int i;
50 for (i = 0; i < header->ndbs; ++i)
51 if (header->dbs[i].id == ':')
52 break;
53 if (i == header->ndbs)
55 status = NSS_STATUS_UNAVAIL;
56 goto out;
59 const stridx_t *hashtable
60 = (const stridx_t *) ((const char *) header
61 + header->dbs[i].hashoffset);
62 const char *valstrtab = (const char *) header + header->valstroffset;
63 size_t userlen = strlen (user);
64 uint32_t hashval = __hash_string (user);
65 size_t hidx = hashval % header->dbs[i].hashsize;
66 size_t hval2 = 1 + hashval % (header->dbs[i].hashsize - 2);
68 gid_t *groups = *groupsp;
70 status = NSS_STATUS_NOTFOUND;
71 while (hashtable[hidx] != ~((stridx_t) 0))
73 const char *valstr = valstrtab + hashtable[hidx];
74 while (isblank (*valstr))
75 ++valstr;
77 if (strncmp (valstr, user, userlen) == 0 && isblank (valstr[userlen]))
79 valstr += userlen + 1;
80 while (isblank (*valstr))
81 ++valstr;
83 while (*valstr != '\0')
85 errno = 0;
86 char *endp;
87 unsigned long int n = strtoul (valstr, &endp, 10);
88 if (*endp != ',' && *endp != '\0')
89 break;
90 valstr = *endp == '\0' ? endp : endp + 1;
92 if (n != ULONG_MAX || errno != ERANGE)
94 /* Insert the group. */
95 if (*start == *size)
97 /* Need a bigger buffer. */
98 if (limit > 0 && *size == limit)
100 /* We reached the maximum. */
101 status = NSS_STATUS_SUCCESS;
102 goto out;
105 long int newsize;
106 if (limit <= 0)
107 newsize = 2 * *size;
108 else
109 newsize = MIN (limit, 2 * *size);
111 gid_t *newgroups = realloc (groups,
112 newsize * sizeof (*groups));
113 if (newgroups == NULL)
115 *errnop = ENOMEM;
116 status = NSS_STATUS_TRYAGAIN;
117 goto out;
120 *groupsp = groups = newgroups;
121 *size = newsize;
124 groups[*start] = n;
125 *start += 1;
129 status = NSS_STATUS_SUCCESS;
130 break;
133 if ((hidx += hval2) >= header->dbs[i].hashsize)
134 hidx -= header->dbs[i].hashsize;
137 out:
138 internal_endent (&state);
140 return status;