]> git.ipfire.org Git - thirdparty/glibc.git/blame - nis/nss_nisplus/nisplus-pwd.c
Update.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-pwd.c
CommitLineData
e61abf83
UD
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.
4
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.
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 Library General Public License for more details.
14
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. */
19
20#include <nss.h>
21#include <errno.h>
22#include <pwd.h>
23#include <string.h>
24#include <libc-lock.h>
25#include <rpcsvc/nis.h>
26#include <rpcsvc/nislib.h>
27
28#include "nss-nisplus.h"
29
30__libc_lock_define_initialized (static, lock)
31
32static nis_result *result = NULL;
33static nis_name *names = NULL;
34
35#define NISENTRYVAL(idx,col,res) \
36 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
37
38#define NISENTRYLEN(idx,col,res) \
39 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
40
26dee9c4 41int
e61abf83
UD
42_nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw,
43 char *buffer, size_t buflen)
44{
45 char *first_unused = buffer;
46 size_t room_left = buflen;
47
48 if (result == NULL)
26dee9c4 49 return 0;
e61abf83
UD
50
51 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
52 result->objects.objects_len != 1 ||
53 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
54 strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
55 "passwd_tbl") != 0 ||
56 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 7)
26dee9c4 57 return 0;
e61abf83
UD
58
59 if (NISENTRYLEN(0, 0, result) >= room_left)
60 {
61 /* The line is too long for our buffer. */
62 no_more_room:
63 __set_errno (ERANGE);
26dee9c4 64 return 0;
e61abf83
UD
65 }
66
67 strncpy (first_unused, NISENTRYVAL(0, 0, result),
68 NISENTRYLEN (0, 0, result));
69 first_unused[NISENTRYLEN(0, 0, result)] = '\0';
70 pw->pw_name = first_unused;
71 room_left -= (strlen (first_unused) +1);
72 first_unused += strlen (first_unused) +1;
73
74 if (NISENTRYLEN(0, 1, result) >= room_left)
75 goto no_more_room;
76
77 strncpy (first_unused, NISENTRYVAL(0, 1, result),
78 NISENTRYLEN (0, 1, result));
79 first_unused[NISENTRYLEN(0, 1, result)] = '\0';
80 pw->pw_passwd = first_unused;
81 room_left -= (strlen (first_unused) +1);
82 first_unused += strlen (first_unused) +1;
83
84 if (NISENTRYLEN(0, 2, result) >= room_left)
85 goto no_more_room;
86
87 strncpy (first_unused, NISENTRYVAL (0, 2, result),
88 NISENTRYLEN (0, 2, result));
89 first_unused[NISENTRYLEN(0, 2, result)] = '\0';
90 pw->pw_uid = atoi (first_unused);
91 room_left -= (strlen (first_unused) +1);
92 first_unused += strlen (first_unused) +1;
93
94 if (NISENTRYLEN(0, 3, result) >= room_left)
95 goto no_more_room;
96
97 strncpy (first_unused, NISENTRYVAL(0, 3, result),
98 NISENTRYLEN (0, 3, result));
99 first_unused[NISENTRYLEN(0, 3, result)] = '\0';
100 pw->pw_gid = atoi (first_unused);
101 room_left -= (strlen (first_unused) +1);
102 first_unused += strlen (first_unused) +1;
103
104 if (NISENTRYLEN(0, 4, result) >= room_left)
105 goto no_more_room;
106
107 strncpy (first_unused, NISENTRYVAL(0, 4, result),
108 NISENTRYLEN (0, 4, result));
109 first_unused[NISENTRYLEN(0, 4, result)] = '\0';
110 pw->pw_gecos = first_unused;
111 room_left -= (strlen (first_unused) +1);
112 first_unused += strlen (first_unused) +1;
113
114 if (NISENTRYLEN(0, 5, result) >= room_left)
115 goto no_more_room;
116
117 strncpy (first_unused, NISENTRYVAL (0, 5, result),
118 NISENTRYLEN (0, 5, result));
119 first_unused[NISENTRYLEN(0, 5, result)] = '\0';
120 pw->pw_dir = first_unused;
121 room_left -= (strlen (first_unused) +1);
122 first_unused += strlen (first_unused) +1;
123
124 if (NISENTRYLEN(0, 6, result) >= room_left)
125 goto no_more_room;
126
127 strncpy (first_unused, NISENTRYVAL (0, 6, result),
128 NISENTRYLEN (0, 6, result));
129 first_unused[NISENTRYLEN (0, 6, result)] = '\0';
130 pw->pw_shell = first_unused;
131 room_left -= (strlen (first_unused) +1);
132 first_unused += strlen (first_unused) +1;
133
134 return 1;
135}
136
137enum nss_status
138_nss_nisplus_setpwent (void)
139{
140 __libc_lock_lock (lock);
141
142 if (result)
143 nis_freeresult (result);
144 result = NULL;
145 if (names)
146 {
147 nis_freenames (names);
148 names = NULL;
149 }
150
151 __libc_lock_unlock (lock);
152
153 return NSS_STATUS_SUCCESS;
154}
155
156enum nss_status
157_nss_nisplus_endpwent (void)
158{
159 __libc_lock_lock (lock);
160
161 if (result)
162 nis_freeresult (result);
163 result = NULL;
164 if (names)
165 {
166 nis_freenames (names);
167 names = NULL;
168 }
169
170 __libc_lock_unlock (lock);
171
172 return NSS_STATUS_SUCCESS;
173}
174
175static enum nss_status
176internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen)
177{
178 int parse_res;
179
180 /* Get the next entry until we found a correct one. */
181 do
182 {
183 if (result == NULL)
184 {
185 names = nis_getnames ("passwd.org_dir");
186 if (names == NULL || names[0] == NULL)
187 return NSS_STATUS_UNAVAIL;
188
189 result = nis_first_entry(names[0]);
190 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
191 return niserr2nss (result->status);
192 }
193 else
194 {
195 nis_result *res;
196
197 res = nis_next_entry(names[0], &result->cookie);
198 nis_freeresult (result);
199 result = res;
200 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
201 return niserr2nss (result->status);
202 }
203
204 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen);
205 } while (!parse_res);
206
207 return NSS_STATUS_SUCCESS;
208}
209
210enum nss_status
211_nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen)
212{
213 int status;
214
215 __libc_lock_lock (lock);
216
217 status = internal_nisplus_getpwent_r (result, buffer, buflen);
218
219 __libc_lock_unlock (lock);
220
221 return status;
222}
223
224enum nss_status
225_nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
226 char *buffer, size_t buflen)
227{
228 int parse_res;
229
230 if (name == NULL || strlen (name) > 8)
231 return NSS_STATUS_NOTFOUND;
232 else
233 {
234 nis_result *result;
235 char buf[strlen (name) + 24];
236
237 sprintf(buf, "[name=%s],passwd.org_dir", name);
238
239 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
240
241 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
242 {
243 enum nss_status status = niserr2nss (result->status);
244
245 nis_freeresult (result);
246 return status;
247 }
248
249 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen);
250
251 nis_freeresult (result);
252
253 if (parse_res)
254 return NSS_STATUS_SUCCESS;
255
256 if (!parse_res && errno == ERANGE)
257 return NSS_STATUS_TRYAGAIN;
258 else
259 return NSS_STATUS_NOTFOUND;
260 }
261}
262
263enum nss_status
264_nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw,
265 char *buffer, size_t buflen)
266{
267 int parse_res;
268 nis_result *result;
269 char buf[100];
270
271 sprintf(buf, "[uid=%d],passwd.org_dir", uid);
272
273 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
274
275 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
276 {
277 enum nss_status status = niserr2nss (result->status);
278
279 nis_freeresult (result);
280 return status;
281 }
282
283 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen);
284
285 nis_freeresult (result);
286 if (parse_res)
287 return NSS_STATUS_SUCCESS;
288
289 if (!parse_res && errno == ERANGE)
290 return NSS_STATUS_TRYAGAIN;
291 else
292 return NSS_STATUS_NOTFOUND;
293}