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 <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)
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
;
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
,
56 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 7)
59 if (NISENTRYLEN(0, 0, result
) >= room_left
)
61 /* The line is too long for our buffer. */
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
)
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
)
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
)
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
)
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
)
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
)
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;
138 _nss_nisplus_setpwent (void)
140 __libc_lock_lock (lock
);
143 nis_freeresult (result
);
147 nis_freenames (names
);
151 __libc_lock_unlock (lock
);
153 return NSS_STATUS_SUCCESS
;
157 _nss_nisplus_endpwent (void)
159 __libc_lock_lock (lock
);
162 nis_freeresult (result
);
166 nis_freenames (names
);
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
)
180 /* Get the next entry until we found a correct one. */
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
);
197 res
= nis_next_entry(names
[0], &result
->cookie
);
198 nis_freeresult (result
);
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
;
211 _nss_nisplus_getpwent_r (struct passwd
*result
, char *buffer
, size_t buflen
)
215 __libc_lock_lock (lock
);
217 status
= internal_nisplus_getpwent_r (result
, buffer
, buflen
);
219 __libc_lock_unlock (lock
);
225 _nss_nisplus_getpwnam_r (const char *name
, struct passwd
*pw
,
226 char *buffer
, size_t buflen
)
230 if (name
== NULL
|| strlen (name
) > 8)
231 return NSS_STATUS_NOTFOUND
;
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
);
249 parse_res
= _nss_nisplus_parse_pwent (result
, pw
, buffer
, buflen
);
251 nis_freeresult (result
);
254 return NSS_STATUS_SUCCESS
;
256 if (!parse_res
&& errno
== ERANGE
)
257 return NSS_STATUS_TRYAGAIN
;
259 return NSS_STATUS_NOTFOUND
;
264 _nss_nisplus_getpwuid_r (const uid_t uid
, struct passwd
*pw
,
265 char *buffer
, size_t buflen
)
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
);
283 parse_res
= _nss_nisplus_parse_pwent (result
, pw
, buffer
, buflen
);
285 nis_freeresult (result
);
287 return NSS_STATUS_SUCCESS
;
289 if (!parse_res
&& errno
== ERANGE
)
290 return NSS_STATUS_TRYAGAIN
;
292 return NSS_STATUS_NOTFOUND
;