1 /* Copyright (C) 1996-1999, 2001-2003, 2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.de>, 1996.
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
21 /* The following is an ugly trick to avoid a prototype declaration for
23 #define _nss_nis_endgrent _nss_nis_endgrent_XXX
25 #undef _nss_nis_endgrent
29 #include <bits/libc-lock.h>
30 #include <rpcsvc/yp.h>
31 #include <rpcsvc/ypclnt.h>
35 /* Get the declaration of the parser function. */
37 #define STRUCTURE group
39 #include <nss/nss_files/files-parse.c>
41 /* Protect global state against multiple changers */
42 __libc_lock_define_initialized (static, lock
)
44 static bool_t new_start
= 1;
49 _nss_nis_setgrent (int stayopen
)
51 __libc_lock_lock (lock
);
61 __libc_lock_unlock (lock
);
63 return NSS_STATUS_SUCCESS
;
65 /* Make _nss_nis_endgrent an alias of _nss_nis_setgrent. We do this
66 even though the prototypes don't match. The argument of setgrent
67 is not used so this makes no difference. */
68 strong_alias (_nss_nis_setgrent
, _nss_nis_endgrent
)
70 static enum nss_status
71 internal_nis_getgrent_r (struct group
*grp
, char *buffer
, size_t buflen
,
74 struct parser_data
*data
= (void *) buffer
;
75 char *domain
, *result
, *outkey
;
76 int len
, keylen
, parse_res
;
78 if (yp_get_default_domain (&domain
))
79 return NSS_STATUS_UNAVAIL
;
81 /* Get the next entry until we found a correct one. */
84 enum nss_status retval
;
88 retval
= yperr2nss (yp_first (domain
, "group.byname",
89 &outkey
, &keylen
, &result
, &len
));
91 retval
= yperr2nss ( yp_next (domain
, "group.byname",
93 &outkey
, &keylen
, &result
, &len
));
95 if (retval
!= NSS_STATUS_SUCCESS
)
97 if (retval
== NSS_STATUS_TRYAGAIN
)
102 if ((size_t) (len
+ 1) > buflen
)
106 return NSS_STATUS_TRYAGAIN
;
109 p
= strncpy (buffer
, result
, len
);
115 parse_res
= _nss_files_parse_grent (p
, grp
, data
, buflen
, errnop
);
120 return NSS_STATUS_TRYAGAIN
;
128 while (parse_res
< 1);
130 return NSS_STATUS_SUCCESS
;
134 _nss_nis_getgrent_r (struct group
*result
, char *buffer
, size_t buflen
,
139 __libc_lock_lock (lock
);
141 status
= internal_nis_getgrent_r (result
, buffer
, buflen
, errnop
);
143 __libc_lock_unlock (lock
);
149 _nss_nis_getgrnam_r (const char *name
, struct group
*grp
,
150 char *buffer
, size_t buflen
, int *errnop
)
152 struct parser_data
*data
= (void *) buffer
;
153 enum nss_status retval
;
154 char *domain
, *result
, *p
;
160 return NSS_STATUS_UNAVAIL
;
163 if (yp_get_default_domain (&domain
))
164 return NSS_STATUS_UNAVAIL
;
166 retval
= yperr2nss (yp_match (domain
, "group.byname", name
,
167 strlen (name
), &result
, &len
));
169 if (retval
!= NSS_STATUS_SUCCESS
)
171 if (retval
== NSS_STATUS_TRYAGAIN
)
176 if ((size_t) (len
+ 1) > buflen
)
180 return NSS_STATUS_TRYAGAIN
;
183 p
= strncpy (buffer
, result
, len
);
189 parse_res
= _nss_files_parse_grent (p
, grp
, data
, buflen
, errnop
);
193 return NSS_STATUS_TRYAGAIN
;
195 return NSS_STATUS_NOTFOUND
;
197 return NSS_STATUS_SUCCESS
;
201 _nss_nis_getgrgid_r (gid_t gid
, struct group
*grp
,
202 char *buffer
, size_t buflen
, int *errnop
)
204 struct parser_data
*data
= (void *) buffer
;
205 enum nss_status retval
;
206 char *domain
, *result
, *p
;
207 int len
, nlen
, parse_res
;
210 if (yp_get_default_domain (&domain
))
211 return NSS_STATUS_UNAVAIL
;
213 nlen
= sprintf (buf
, "%lu", (unsigned long int) gid
);
215 retval
= yperr2nss (yp_match (domain
, "group.bygid", buf
,
216 nlen
, &result
, &len
));
218 if (retval
!= NSS_STATUS_SUCCESS
)
220 if (retval
== NSS_STATUS_TRYAGAIN
)
225 if ((size_t) (len
+ 1) > buflen
)
229 return NSS_STATUS_TRYAGAIN
;
232 p
= strncpy (buffer
, result
, len
);
238 parse_res
= _nss_files_parse_grent (p
, grp
, data
, buflen
, errnop
);
242 return NSS_STATUS_TRYAGAIN
;
244 return NSS_STATUS_NOTFOUND
;
246 return NSS_STATUS_SUCCESS
;