]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nis/nis-grp.c
Update.
[thirdparty/glibc.git] / nis / nss_nis / nis-grp.c
1 /* Copyright (C) 1996,1997,1998,1999,2001,2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
4
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.
9
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.
14
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. */
19
20 #include <nss.h>
21 #include <grp.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
28
29 #include "nss-nis.h"
30
31 /* Get the declaration of the parser function. */
32 #define ENTNAME grent
33 #define STRUCTURE group
34 #define EXTERN_PARSER
35 #include <nss/nss_files/files-parse.c>
36
37 /* Protect global state against multiple changers */
38 __libc_lock_define_initialized (static, lock)
39
40 static bool_t new_start = 1;
41 static char *oldkey;
42 static int oldkeylen;
43
44 enum nss_status
45 _nss_nis_setgrent (int stayopen)
46 {
47 __libc_lock_lock (lock);
48
49 new_start = 1;
50 if (oldkey != NULL)
51 {
52 free (oldkey);
53 oldkey = NULL;
54 oldkeylen = 0;
55 }
56
57 __libc_lock_unlock (lock);
58
59 return NSS_STATUS_SUCCESS;
60 }
61
62 enum nss_status
63 _nss_nis_endgrent (void)
64 {
65 __libc_lock_lock (lock);
66
67 new_start = 1;
68 if (oldkey != NULL)
69 {
70 free (oldkey);
71 oldkey = NULL;
72 oldkeylen = 0;
73 }
74
75 __libc_lock_unlock (lock);
76
77 return NSS_STATUS_SUCCESS;
78 }
79
80 static enum nss_status
81 internal_nis_getgrent_r (struct group *grp, char *buffer, size_t buflen,
82 int *errnop)
83 {
84 struct parser_data *data = (void *) buffer;
85 char *domain, *result, *outkey;
86 int len, keylen, parse_res;
87
88 if (yp_get_default_domain (&domain))
89 return NSS_STATUS_UNAVAIL;
90
91 /* Get the next entry until we found a correct one. */
92 do
93 {
94 enum nss_status retval;
95 char *p;
96
97 if (new_start)
98 retval = yperr2nss (yp_first (domain, "group.byname",
99 &outkey, &keylen, &result, &len));
100 else
101 retval = yperr2nss ( yp_next (domain, "group.byname",
102 oldkey, oldkeylen,
103 &outkey, &keylen, &result, &len));
104
105 if (retval != NSS_STATUS_SUCCESS)
106 {
107 if (retval == NSS_STATUS_TRYAGAIN)
108 *errnop = errno;
109 return retval;
110 }
111
112 if ((size_t) (len + 1) > buflen)
113 {
114 free (result);
115 *errnop = ERANGE;
116 return NSS_STATUS_TRYAGAIN;
117 }
118
119 p = strncpy (buffer, result, len);
120 buffer[len] = '\0';
121 while (isspace (*p))
122 ++p;
123 free (result);
124
125 parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
126 if (parse_res == -1)
127 {
128 free (outkey);
129 *errnop = ERANGE;
130 return NSS_STATUS_TRYAGAIN;
131 }
132
133 free (oldkey);
134 oldkey = outkey;
135 oldkeylen = keylen;
136 new_start = 0;
137 }
138 while (parse_res < 1);
139
140 return NSS_STATUS_SUCCESS;
141 }
142
143 enum nss_status
144 _nss_nis_getgrent_r (struct group *result, char *buffer, size_t buflen,
145 int *errnop)
146 {
147 int status;
148
149 __libc_lock_lock (lock);
150
151 status = internal_nis_getgrent_r (result, buffer, buflen, errnop);
152
153 __libc_lock_unlock (lock);
154
155 return status;
156 }
157
158 enum nss_status
159 _nss_nis_getgrnam_r (const char *name, struct group *grp,
160 char *buffer, size_t buflen, int *errnop)
161 {
162 struct parser_data *data = (void *) buffer;
163 enum nss_status retval;
164 char *domain, *result, *p;
165 int len, parse_res;
166
167 if (name == NULL)
168 {
169 *errnop = EINVAL;
170 return NSS_STATUS_UNAVAIL;
171 }
172
173 if (yp_get_default_domain (&domain))
174 return NSS_STATUS_UNAVAIL;
175
176 retval = yperr2nss (yp_match (domain, "group.byname", name,
177 strlen (name), &result, &len));
178
179 if (retval != NSS_STATUS_SUCCESS)
180 {
181 if (retval == NSS_STATUS_NOTFOUND)
182 *errnop = EAGAIN;
183 else if (retval == NSS_STATUS_TRYAGAIN)
184 *errnop = errno;
185 return retval;
186 }
187
188 if ((size_t) (len + 1) > buflen)
189 {
190 free (result);
191 *errnop = ERANGE;
192 return NSS_STATUS_TRYAGAIN;
193 }
194
195 p = strncpy (buffer, result, len);
196 buffer[len] = '\0';
197 while (isspace (*p))
198 ++p;
199 free (result);
200
201 parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
202 if (parse_res < 1)
203 {
204 if (parse_res == -1)
205 return NSS_STATUS_TRYAGAIN;
206 else
207 return NSS_STATUS_NOTFOUND;
208 }
209 return NSS_STATUS_SUCCESS;
210 }
211
212 enum nss_status
213 _nss_nis_getgrgid_r (gid_t gid, struct group *grp,
214 char *buffer, size_t buflen, int *errnop)
215 {
216 struct parser_data *data = (void *) buffer;
217 enum nss_status retval;
218 char *domain, *result, *p;
219 int len, nlen, parse_res;
220 char buf[32];
221
222 if (yp_get_default_domain (&domain))
223 return NSS_STATUS_UNAVAIL;
224
225 nlen = sprintf (buf, "%lu", (unsigned long int) gid);
226
227 retval = yperr2nss (yp_match (domain, "group.bygid", buf,
228 nlen, &result, &len));
229
230 if (retval != NSS_STATUS_SUCCESS)
231 {
232 if (retval == NSS_STATUS_TRYAGAIN)
233 *errnop = errno;
234 return retval;
235 }
236
237 if ((size_t) (len + 1) > buflen)
238 {
239 free (result);
240 *errnop = ERANGE;
241 return NSS_STATUS_TRYAGAIN;
242 }
243
244 p = strncpy (buffer, result, len);
245 buffer[len] = '\0';
246 while (isspace (*p))
247 ++p;
248 free (result);
249
250 parse_res = _nss_files_parse_grent (p, grp, data, buflen, errnop);
251 if (parse_res < 1)
252 {
253 if (parse_res == -1)
254 return NSS_STATUS_TRYAGAIN;
255 else
256 return NSS_STATUS_NOTFOUND;
257 }
258 return NSS_STATUS_SUCCESS;
259 }