1 /* Copyright (C) 1997, 2001 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
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
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
29 #include "nisplus-parser.h"
31 __libc_lock_define_initialized (static, lock
);
33 static nis_result
*result
;
34 static unsigned long next_entry
;
35 static nis_name tablename_val
;
36 static u_long tablename_len
;
38 static enum nss_status
39 _nss_create_tablename (int *errnop
)
41 if (tablename_val
== NULL
)
43 char buf
[40 + strlen (nis_local_directory ())];
46 p
= __stpcpy (buf
, "group.org_dir.");
47 p
= __stpcpy (p
, nis_local_directory ());
48 tablename_val
= __strdup (buf
);
49 if (tablename_val
== NULL
)
52 return NSS_STATUS_TRYAGAIN
;
54 tablename_len
= strlen (tablename_val
);
56 return NSS_STATUS_SUCCESS
;
59 static enum nss_status
60 internal_setgrent (void)
62 enum nss_status status
;
66 nis_freeresult (result
);
70 if (tablename_val
== NULL
)
71 if (_nss_create_tablename (&err
) != NSS_STATUS_SUCCESS
)
72 return NSS_STATUS_UNAVAIL
;
74 result
= nis_list (tablename_val
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
75 status
= niserr2nss (result
->status
);
76 if (status
!= NSS_STATUS_SUCCESS
)
78 nis_freeresult (result
);
85 _nss_nisplus_setgrent (int stayopen
)
87 enum nss_status status
;
89 __libc_lock_lock (lock
);
91 status
= internal_setgrent ();
93 __libc_lock_unlock (lock
);
99 _nss_nisplus_endgrent (void)
101 __libc_lock_lock (lock
);
104 nis_freeresult (result
);
107 __libc_lock_unlock (lock
);
109 return NSS_STATUS_SUCCESS
;
112 static enum nss_status
113 internal_nisplus_getgrent_r (struct group
*gr
, char *buffer
, size_t buflen
,
120 enum nss_status status
;
122 status
= internal_setgrent ();
123 if (result
== NULL
|| status
!= NSS_STATUS_SUCCESS
)
127 /* Get the next entry until we found a correct one. */
130 if (next_entry
>= result
->objects
.objects_len
)
131 return NSS_STATUS_NOTFOUND
;
133 parse_res
= _nss_nisplus_parse_grent (result
, next_entry
, gr
,
134 buffer
, buflen
, errnop
);
136 return NSS_STATUS_TRYAGAIN
;
142 return NSS_STATUS_SUCCESS
;
146 _nss_nisplus_getgrent_r (struct group
*result
, char *buffer
, size_t buflen
,
151 __libc_lock_lock (lock
);
153 status
= internal_nisplus_getgrent_r (result
, buffer
, buflen
, errnop
);
155 __libc_lock_unlock (lock
);
161 _nss_nisplus_getgrnam_r (const char *name
, struct group
*gr
,
162 char *buffer
, size_t buflen
, int *errnop
)
166 if (tablename_val
== NULL
)
168 enum nss_status status
= _nss_create_tablename (errnop
);
170 if (status
!= NSS_STATUS_SUCCESS
)
177 return NSS_STATUS_NOTFOUND
;
182 char buf
[strlen (name
) + 24 + tablename_len
];
184 sprintf (buf
, "[name=%s],%s", name
, tablename_val
);
186 result
= nis_list (buf
, FOLLOW_LINKS
| FOLLOW_PATH
, NULL
, NULL
);
188 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
190 enum nss_status status
= niserr2nss (result
->status
);
192 nis_freeresult (result
);
196 parse_res
= _nss_nisplus_parse_grent (result
, 0, gr
, buffer
, buflen
,
198 nis_freeresult (result
);
204 return NSS_STATUS_TRYAGAIN
;
207 return NSS_STATUS_NOTFOUND
;
209 return NSS_STATUS_SUCCESS
;
214 _nss_nisplus_getgrgid_r (const gid_t gid
, struct group
*gr
,
215 char *buffer
, size_t buflen
, int *errnop
)
217 if (tablename_val
== NULL
)
219 enum nss_status status
= _nss_create_tablename (errnop
);
221 if (status
!= NSS_STATUS_SUCCESS
)
228 char buf
[36 + tablename_len
];
230 sprintf (buf
, "[gid=%d],%s", gid
, tablename_val
);
232 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
234 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
236 enum nss_status status
= niserr2nss (result
->status
);
238 nis_freeresult (result
);
242 parse_res
= _nss_nisplus_parse_grent (result
, 0, gr
, buffer
, buflen
,
245 nis_freeresult (result
);
251 return NSS_STATUS_TRYAGAIN
;
254 return NSS_STATUS_NOTFOUND
;
256 return NSS_STATUS_SUCCESS
;