Fix csqrt spurious underflows (bug 18371).
[glibc.git] / nis / nss_nisplus / nisplus-initgroups.c
blob0fc9b7e358d8c43dd7e9935ad92a9329d16e50ad
1 /* Copyright (C) 1997-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
18 #include <atomic.h>
19 #include <nss.h>
20 #include <grp.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/nis.h>
27 #include "nss-nisplus.h"
28 #include "nisplus-parser.h"
29 #include <libnsl.h>
30 #include <nis_intern.h>
31 #include <nis_xdr.h>
33 #define NISOBJVAL(col, obj) \
34 ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
36 #define NISOBJLEN(col, obj) \
37 ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
39 extern nis_name grp_tablename_val attribute_hidden;
40 extern size_t grp_tablename_len attribute_hidden;
41 extern enum nss_status _nss_grp_create_tablename (int *errnop);
44 enum nss_status
45 _nss_nisplus_initgroups_dyn (const char *user, gid_t group, long int *start,
46 long int *size, gid_t **groupsp, long int limit,
47 int *errnop)
49 if (grp_tablename_val == NULL)
51 enum nss_status status = _nss_grp_create_tablename (errnop);
53 if (status != NSS_STATUS_SUCCESS)
54 return status;
57 nis_result *result;
58 char buf[strlen (user) + 12 + grp_tablename_len];
60 snprintf (buf, sizeof (buf), "[members=%s],%s", user, grp_tablename_val);
62 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | ALL_RESULTS, NULL, NULL);
64 if (result == NULL)
66 *errnop = ENOMEM;
67 return NSS_STATUS_TRYAGAIN;
70 if (__glibc_unlikely (niserr2nss (result->status) != NSS_STATUS_SUCCESS))
72 enum nss_status status = niserr2nss (result->status);
74 nis_freeresult (result);
75 return status;
78 if (NIS_RES_NUMOBJ (result) == 0)
80 errout:
81 nis_freeresult (result);
82 return NSS_STATUS_NOTFOUND;
85 gid_t *groups = *groupsp;
86 nis_object *obj = NIS_RES_OBJECT (result);
87 for (unsigned int cnt = 0; cnt < NIS_RES_NUMOBJ (result); ++cnt, ++obj)
89 if (__type_of (obj) != NIS_ENTRY_OBJ
90 || strcmp (obj->EN_data.en_type, "group_tbl") != 0
91 || obj->EN_data.en_cols.en_cols_len < 4)
92 continue;
94 char *numstr = NISOBJVAL (2, obj);
95 size_t len = NISOBJLEN (2, obj);
96 if (len == 0 || numstr[0] == '\0')
97 continue;
99 gid_t gid;
100 char *endp;
101 if (__glibc_unlikely (numstr[len - 1] != '\0'))
103 char numstrbuf[len + 1];
104 memcpy (numstrbuf, numstr, len);
105 numstrbuf[len] = '\0';
106 gid = strtoul (numstrbuf, &endp, 10);
107 if (*endp)
108 continue;
110 else
112 gid = strtoul (numstr, &endp, 10);
113 if (*endp)
114 continue;
117 if (gid == group)
118 continue;
120 /* Insert this group. */
121 if (*start == *size)
123 /* Need a bigger buffer. */
124 long int newsize;
126 if (limit > 0 && *size == limit)
127 /* We reached the maximum. */
128 break;
130 if (limit <= 0)
131 newsize = 2 * *size;
132 else
133 newsize = MIN (limit, 2 * *size);
135 gid_t *newgroups = realloc (groups, newsize * sizeof (*groups));
136 if (newgroups == NULL)
137 goto errout;
138 *groupsp = groups = newgroups;
139 *size = newsize;
142 groups[*start] = gid;
143 *start += 1;
146 nis_freeresult (result);
147 return NSS_STATUS_SUCCESS;