Update.
[glibc.git] / nis / nss_nisplus / nisplus-pwd.c
blobb65a9fe3951205cad1f97c361e1e06ac01bd1c8a
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. */
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>
28 #include "nss-nisplus.h"
30 __libc_lock_define_initialized (static, lock)
32 static nis_result *result = NULL;
33 static nis_name *names = NULL;
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)
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)
41 int
42 _nss_nisplus_parse_pwent (nis_result *result, struct passwd *pw,
43 char *buffer, size_t buflen)
45 char *first_unused = buffer;
46 size_t room_left = buflen;
48 if (result == NULL)
49 return 0;
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)
57 return 0;
59 if (NISENTRYLEN(0, 0, result) >= room_left)
61 /* The line is too long for our buffer. */
62 no_more_room:
63 __set_errno (ERANGE);
64 return 0;
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;
74 if (NISENTRYLEN(0, 1, result) >= room_left)
75 goto no_more_room;
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;
84 if (NISENTRYLEN(0, 2, result) >= room_left)
85 goto no_more_room;
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;
94 if (NISENTRYLEN(0, 3, result) >= room_left)
95 goto no_more_room;
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;
104 if (NISENTRYLEN(0, 4, result) >= room_left)
105 goto no_more_room;
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;
114 if (NISENTRYLEN(0, 5, result) >= room_left)
115 goto no_more_room;
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;
124 if (NISENTRYLEN(0, 6, result) >= room_left)
125 goto no_more_room;
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;
134 return 1;
137 enum nss_status
138 _nss_nisplus_setpwent (void)
140 __libc_lock_lock (lock);
142 if (result)
143 nis_freeresult (result);
144 result = NULL;
145 if (names)
147 nis_freenames (names);
148 names = NULL;
151 __libc_lock_unlock (lock);
153 return NSS_STATUS_SUCCESS;
156 enum nss_status
157 _nss_nisplus_endpwent (void)
159 __libc_lock_lock (lock);
161 if (result)
162 nis_freeresult (result);
163 result = NULL;
164 if (names)
166 nis_freenames (names);
167 names = NULL;
170 __libc_lock_unlock (lock);
172 return NSS_STATUS_SUCCESS;
175 static enum nss_status
176 internal_nisplus_getpwent_r (struct passwd *pw, char *buffer, size_t buflen)
178 int parse_res;
180 /* Get the next entry until we found a correct one. */
183 if (result == NULL)
185 names = nis_getnames ("passwd.org_dir");
186 if (names == NULL || names[0] == NULL)
187 return NSS_STATUS_UNAVAIL;
189 result = nis_first_entry(names[0]);
190 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
191 return niserr2nss (result->status);
193 else
195 nis_result *res;
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);
204 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen);
205 } while (!parse_res);
207 return NSS_STATUS_SUCCESS;
210 enum nss_status
211 _nss_nisplus_getpwent_r (struct passwd *result, char *buffer, size_t buflen)
213 int status;
215 __libc_lock_lock (lock);
217 status = internal_nisplus_getpwent_r (result, buffer, buflen);
219 __libc_lock_unlock (lock);
221 return status;
224 enum nss_status
225 _nss_nisplus_getpwnam_r (const char *name, struct passwd *pw,
226 char *buffer, size_t buflen)
228 int parse_res;
230 if (name == NULL || strlen (name) > 8)
231 return NSS_STATUS_NOTFOUND;
232 else
234 nis_result *result;
235 char buf[strlen (name) + 24];
237 sprintf(buf, "[name=%s],passwd.org_dir", name);
239 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
241 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
243 enum nss_status status = niserr2nss (result->status);
245 nis_freeresult (result);
246 return status;
249 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen);
251 nis_freeresult (result);
253 if (parse_res)
254 return NSS_STATUS_SUCCESS;
256 if (!parse_res && errno == ERANGE)
257 return NSS_STATUS_TRYAGAIN;
258 else
259 return NSS_STATUS_NOTFOUND;
263 enum nss_status
264 _nss_nisplus_getpwuid_r (const uid_t uid, struct passwd *pw,
265 char *buffer, size_t buflen)
267 int parse_res;
268 nis_result *result;
269 char buf[100];
271 sprintf(buf, "[uid=%d],passwd.org_dir", uid);
273 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
275 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
277 enum nss_status status = niserr2nss (result->status);
279 nis_freeresult (result);
280 return status;
283 parse_res = _nss_nisplus_parse_pwent (result, pw, buffer, buflen);
285 nis_freeresult (result);
286 if (parse_res)
287 return NSS_STATUS_SUCCESS;
289 if (!parse_res && errno == ERANGE)
290 return NSS_STATUS_TRYAGAIN;
291 else
292 return NSS_STATUS_NOTFOUND;