1 /* Copyright (C) 1997 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/nis.h>
27 #include "nss-nisplus.h"
28 #include "nisplus-parser.h"
30 __libc_lock_define_initialized (static, lock
)
32 static nis_result
*result
= NULL
;
33 static nis_name tablename_val
= NULL
;
34 static u_long tablename_len
= 0;
36 static enum nss_status
37 _nss_create_tablename (int *errnop
)
39 if (tablename_val
== NULL
)
41 char buf
[40 + strlen (nis_local_directory ())];
44 p
= __stpcpy (buf
, "passwd.org_dir.");
45 p
= __stpcpy (p
, nis_local_directory ());
46 tablename_val
= __strdup (buf
);
47 if (tablename_val
== NULL
)
50 return NSS_STATUS_TRYAGAIN
;
52 tablename_len
= strlen (tablename_val
);
54 return NSS_STATUS_SUCCESS
;
58 _nss_nisplus_setspent (void)
60 enum nss_status status
= NSS_STATUS_SUCCESS
;
63 __libc_lock_lock (lock
);
66 nis_freeresult (result
);
69 if (tablename_val
== NULL
)
70 status
= _nss_create_tablename (&err
);
72 __libc_lock_unlock (lock
);
74 return NSS_STATUS_SUCCESS
;
78 _nss_nisplus_endspent (void)
80 __libc_lock_lock (lock
);
83 nis_freeresult (result
);
86 __libc_lock_unlock (lock
);
88 return NSS_STATUS_SUCCESS
;
91 static enum nss_status
92 internal_nisplus_getspent_r (struct spwd
*sp
, char *buffer
, size_t buflen
,
97 /* Get the next entry until we found a correct one. */
100 nis_result
*saved_res
;
106 if (tablename_val
== NULL
)
108 enum nss_status status
= _nss_create_tablename (errnop
);
110 if (status
!= NSS_STATUS_SUCCESS
)
114 result
= nis_first_entry (tablename_val
);
115 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
116 return niserr2nss (result
->status
);
123 res
= nis_next_entry (tablename_val
, &result
->cookie
);
125 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
127 nis_freeresult (saved_res
);
128 return niserr2nss (result
->status
);
132 parse_res
= _nss_nisplus_parse_spent (result
, sp
, buffer
,
136 nis_freeresult (result
);
139 return NSS_STATUS_TRYAGAIN
;
144 nis_freeresult (saved_res
);
146 } while (!parse_res
);
148 return NSS_STATUS_SUCCESS
;
152 _nss_nisplus_getspent_r (struct spwd
*result
, char *buffer
, size_t buflen
,
157 __libc_lock_lock (lock
);
159 status
= internal_nisplus_getspent_r (result
, buffer
, buflen
, errnop
);
161 __libc_lock_unlock (lock
);
167 _nss_nisplus_getspnam_r (const char *name
, struct spwd
*sp
,
168 char *buffer
, size_t buflen
, int *errnop
)
172 if (tablename_val
== NULL
)
174 enum nss_status status
= _nss_create_tablename (errnop
);
176 if (status
!= NSS_STATUS_SUCCESS
)
183 return NSS_STATUS_NOTFOUND
;
188 char buf
[strlen (name
) + 24 + tablename_len
];
190 sprintf (buf
, "[name=%s],%s", name
, tablename_val
);
192 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
194 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
196 enum nss_status status
= niserr2nss (result
->status
);
198 nis_freeresult (result
);
202 parse_res
= _nss_nisplus_parse_spent (result
, sp
, buffer
, buflen
,
204 nis_freeresult (result
);
211 return NSS_STATUS_TRYAGAIN
;
214 return NSS_STATUS_NOTFOUND
;
216 return NSS_STATUS_SUCCESS
;