]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-pwd.c
f7bc8321bd2e0dd5cbafa0b41416fe32bb3c652c
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-pwd.c
1 /* Copyright (C) 1997, 1999, 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.
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 <errno.h>
22 #include <pwd.h>
23 #include <string.h>
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/nis.h>
26
27 #include "nss-nisplus.h"
28 #include "nisplus-parser.h"
29
30 __libc_lock_define_initialized (static, lock)
31
32 static nis_result *result;
33 static nis_name tablename_val;
34 static u_long tablename_len;
35
36 static enum nss_status
37 _nss_create_tablename (int *errnop)
38 {
39 if (tablename_val == NULL)
40 {
41 char buf [40 + strlen (nis_local_directory ())];
42 char *p;
43
44 p = __stpcpy (buf, "passwd.org_dir.");
45 p = __stpcpy (p, nis_local_directory ());
46 tablename_val = __strdup (buf);
47 if (tablename_val == NULL)
48 {
49 *errnop = errno;
50 return NSS_STATUS_TRYAGAIN;
51 }
52 tablename_len = strlen (tablename_val);
53 }
54 return NSS_STATUS_SUCCESS;
55 }
56
57
58 enum nss_status
59 _nss_nisplus_setpwent (int stayopen)
60 {
61 enum nss_status status = NSS_STATUS_SUCCESS;
62 int err;
63
64 __libc_lock_lock (lock);
65
66 if (result)
67 nis_freeresult (result);
68 result = NULL;
69
70 if (tablename_val == NULL)
71 status = _nss_create_tablename (&err);
72
73 __libc_lock_unlock (lock);
74
75 return status;
76 }
77
78 enum nss_status
79 _nss_nisplus_endpwent (void)
80 {
81 __libc_lock_lock (lock);
82
83 if (result)
84 nis_freeresult (result);
85 result = NULL;
86
87 __libc_lock_unlock (lock);
88
89 return NSS_STATUS_SUCCESS;
90 }
91
92 static enum nss_status
93 internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen,
94 int *errnop)
95 {
96 int parse_res;
97
98 /* Get the next entry until we found a correct one. */
99 do
100 {
101 nis_result *saved_res;
102
103 if (result == NULL)
104 {
105 saved_res = NULL;
106 if (tablename_val == NULL)
107 {
108 enum nss_status status = _nss_create_tablename (errnop);
109
110 if (status != NSS_STATUS_SUCCESS)
111 return status;
112 }
113
114 result = nis_first_entry (tablename_val);
115 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
116 return niserr2nss (result->status);
117 }
118 else
119 {
120 nis_result *res;
121
122 saved_res = result;
123 res = nis_next_entry (tablename_val, &result->cookie);
124 result = res;
125 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
126 {
127 nis_freeresult (saved_res);
128 return niserr2nss (result->status);
129 }
130 }
131
132 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer,
133 buflen, errnop);
134 if (parse_res == -1)
135 {
136 nis_freeresult (result);
137 result = saved_res;
138 *errnop = ERANGE;
139 return NSS_STATUS_TRYAGAIN;
140 }
141 else
142 {
143 if (saved_res)
144 nis_freeresult (saved_res);
145 }
146 } while (!parse_res);
147
148 return NSS_STATUS_SUCCESS;
149 }
150
151 enum nss_status
152 _nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen,
153 int *errnop)
154 {
155 int status;
156
157 __libc_lock_lock (lock);
158
159 status = internal_nisplus_getpwent_r (result, buffer, buflen, errnop);
160
161 __libc_lock_unlock (lock);
162
163 return status;
164 }
165
166 enum nss_status
167 _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
168 char *buffer, size_t buflen, int *errnop)
169 {
170 int parse_res;
171
172 if (tablename_val == NULL)
173 {
174 enum nss_status status = _nss_create_tablename (errnop);
175
176 if (status != NSS_STATUS_SUCCESS)
177 return status;
178 }
179
180 if (name == NULL)
181 {
182 *errnop = EINVAL;
183 return NSS_STATUS_UNAVAIL;
184 }
185 else
186 {
187 nis_result *result;
188 char buf[strlen (name) + 24 + tablename_len];
189
190 sprintf (buf, "[name=%s],%s", name, tablename_val);
191
192 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
193
194 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
195 {
196 enum nss_status status = niserr2nss (result->status);
197
198 nis_freeresult (result);
199 return status;
200 }
201
202 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen,
203 errnop);
204
205 nis_freeresult (result);
206
207 if (parse_res < 1)
208 {
209 if (parse_res == -1)
210 {
211 *errnop = ERANGE;
212 return NSS_STATUS_TRYAGAIN;
213 }
214 else
215 return NSS_STATUS_NOTFOUND;
216 }
217 return NSS_STATUS_SUCCESS;
218 }
219 }
220
221 enum nss_status
222 _nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw,
223 char *buffer, size_t buflen, int *errnop)
224 {
225 if (tablename_val == NULL)
226 {
227 enum nss_status status = _nss_create_tablename (errnop);
228
229 if (status != NSS_STATUS_SUCCESS)
230 return status;
231 }
232
233 {
234 int parse_res;
235 nis_result *result;
236 char buf[100 + tablename_len];
237
238 sprintf (buf, "[uid=%lu],%s", (unsigned long int) uid, tablename_val);
239
240 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
241
242 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
243 {
244 enum nss_status status = niserr2nss (result->status);
245
246 nis_freeresult (result);
247 return status;
248 }
249
250 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen, errnop);
251
252 nis_freeresult (result);
253
254 if (parse_res < 1)
255 {
256 if (parse_res == -1)
257 {
258 *errnop = ERANGE;
259 return NSS_STATUS_TRYAGAIN;
260 }
261 else
262 return NSS_STATUS_NOTFOUND;
263 }
264 return NSS_STATUS_SUCCESS;
265 }
266 }