Fix iov[] size in SH register_dump()
[glibc.git] / nis / nss_nisplus / nisplus-initgroups.c
blob6588ec2533a2c45df544dbdcb4d31e49a3e6be62
1 /* Copyright (C) 1997, 2001, 2002, 2003, 2005, 2006
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <atomic.h>
21 #include <nss.h>
22 #include <grp.h>
23 #include <ctype.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
29 #include "nss-nisplus.h"
30 #include "nisplus-parser.h"
31 #include <libnsl.h>
32 #include <nis_intern.h>
33 #include <nis_xdr.h>
35 #define NISOBJVAL(col, obj) \
36 ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
38 #define NISOBJLEN(col, obj) \
39 ((obj)->EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
41 extern nis_name grp_tablename_val attribute_hidden;
42 extern size_t grp_tablename_len attribute_hidden;
43 extern enum nss_status _nss_grp_create_tablename (int *errnop);
46 enum nss_status
47 _nss_nisplus_initgroups_dyn (const char *user, gid_t group, long int *start,
48 long int *size, gid_t **groupsp, long int limit,
49 int *errnop)
51 if (grp_tablename_val == NULL)
53 enum nss_status status = _nss_grp_create_tablename (errnop);
55 if (status != NSS_STATUS_SUCCESS)
56 return status;
59 nis_result *result;
60 char buf[strlen (user) + 12 + grp_tablename_len];
62 snprintf (buf, sizeof (buf), "[members=%s],%s", user, grp_tablename_val);
64 result = nis_list (buf, FOLLOW_LINKS | FOLLOW_PATH | ALL_RESULTS, NULL, NULL);
66 if (result == NULL)
68 *errnop = ENOMEM;
69 return NSS_STATUS_TRYAGAIN;
72 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
74 enum nss_status status = niserr2nss (result->status);
76 nis_freeresult (result);
77 return status;
80 if (NIS_RES_NUMOBJ (result) == 0)
82 errout:
83 nis_freeresult (result);
84 return NSS_STATUS_NOTFOUND;
87 gid_t *groups = *groupsp;
88 nis_object *obj = NIS_RES_OBJECT (result);
89 for (unsigned int cnt = 0; cnt < NIS_RES_NUMOBJ (result); ++cnt, ++obj)
91 if (__type_of (obj) != NIS_ENTRY_OBJ
92 || strcmp (obj->EN_data.en_type, "group_tbl") != 0
93 || obj->EN_data.en_cols.en_cols_len < 4)
94 continue;
96 char *numstr = NISOBJVAL (2, obj);
97 size_t len = NISOBJLEN (2, obj);
98 if (len == 0 || numstr[0] == '\0')
99 continue;
101 gid_t gid;
102 char *endp;
103 if (__builtin_expect (numstr[len - 1] != '\0', 0))
105 char numstrbuf[len + 1];
106 memcpy (numstrbuf, numstr, len);
107 numstrbuf[len] = '\0';
108 gid = strtoul (numstrbuf, &endp, 10);
109 if (*endp)
110 continue;
112 else
114 gid = strtoul (numstr, &endp, 10);
115 if (*endp)
116 continue;
119 if (gid == group)
120 continue;
122 /* Insert this group. */
123 if (*start == *size)
125 /* Need a bigger buffer. */
126 long int newsize;
128 if (limit > 0 && *size == limit)
129 /* We reached the maximum. */
130 break;
132 if (limit <= 0)
133 newsize = 2 * *size;
134 else
135 newsize = MIN (limit, 2 * *size);
137 gid_t *newgroups = realloc (groups, newsize * sizeof (*groups));
138 if (newgroups == NULL)
139 goto errout;
140 *groupsp = groups = newgroups;
141 *size = newsize;
144 groups[*start] = gid;
145 *start += 1;
148 nis_freeresult (result);
149 return NSS_STATUS_SUCCESS;