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. */
25 #include <libc-lock.h>
26 #include <rpcsvc/nis.h>
27 #include <rpcsvc/nislib.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock
);
33 static nis_result
*result
= NULL
;
34 static nis_name
*names
= NULL
;
36 #define NISENTRYVAL(idx,col,res) \
37 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
39 #define NISENTRYLEN(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
43 _nss_nisplus_parse_grent (nis_result
* result
, struct group
*gr
,
44 char *buffer
, size_t buflen
)
46 char *first_unused
= buffer
;
47 size_t room_left
= buflen
;
54 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
) ||
55 result
->objects
.objects_len
!= 1 ||
56 result
->objects
.objects_val
[0].zo_data
.zo_type
!= ENTRY_OBJ
||
57 strcmp (result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_type
,
59 result
->objects
.objects_val
[0].zo_data
.objdata_u
.en_data
.en_cols
.en_cols_len
< 4)
62 if (NISENTRYLEN (0, 0, result
) >= room_left
)
64 /* The line is too long for our buffer. */
70 strncpy (first_unused
, NISENTRYVAL (0, 0, result
),
71 NISENTRYLEN (0, 0, result
));
72 first_unused
[NISENTRYLEN (0, 0, result
)] = '\0';
73 gr
->gr_name
= first_unused
;
74 room_left
-= (strlen (first_unused
) + 1);
75 first_unused
+= strlen (first_unused
) + 1;
77 if (NISENTRYLEN (0, 1, result
) >= room_left
)
80 strncpy (first_unused
, NISENTRYVAL (0, 1, result
),
81 NISENTRYLEN (0, 1, result
));
82 first_unused
[NISENTRYLEN (0, 1, result
)] = '\0';
83 gr
->gr_passwd
= first_unused
;
84 room_left
-= (strlen (first_unused
) + 1);
85 first_unused
+= strlen (first_unused
) + 1;
87 if (NISENTRYLEN (0, 2, result
) >= room_left
)
90 strncpy (first_unused
, NISENTRYVAL (0, 2, result
),
91 NISENTRYLEN (0, 2, result
));
92 first_unused
[NISENTRYLEN (0, 2, result
)] = '\0';
93 gr
->gr_gid
= atoi (first_unused
);
94 room_left
-= (strlen (first_unused
) + 1);
95 first_unused
+= strlen (first_unused
) + 1;
97 if (NISENTRYLEN (0, 3, result
) >= room_left
)
100 strncpy (first_unused
, NISENTRYVAL (0, 3, result
),
101 NISENTRYLEN (0, 3, result
));
102 first_unused
[NISENTRYLEN (0, 3, result
)] = '\0';
104 room_left
-= (strlen (line
) + 1);
105 first_unused
+= strlen (line
) + 1;
106 /* Adjust the pointer so it is aligned for
108 first_unused
+= __alignof__ (char *) - 1;
109 first_unused
-= ((first_unused
- (char *) 0) % __alignof__ (char *));
110 gr
->gr_mem
= (char **) first_unused
;
113 while (*line
!= '\0')
115 /* Skip leading blanks. */
116 while (isspace (*line
))
122 if (room_left
< sizeof (char *))
124 room_left
-= sizeof (char *);
125 gr
->gr_mem
[count
] = line
;
127 while (*line
!= '\0' && *line
!= ',' && !isspace(*line
))
130 if (line
!= gr
->gr_mem
[count
])
140 gr
->gr_mem
[count
] = NULL
;
142 if (room_left
< sizeof (char *))
144 room_left
-= sizeof (char *);
145 gr
->gr_mem
[count
] = NULL
;
151 _nss_nisplus_setgrent (void)
153 __libc_lock_lock (lock
);
156 nis_freeresult (result
);
160 nis_freenames (names
);
164 __libc_lock_unlock (lock
);
166 return NSS_STATUS_SUCCESS
;
170 _nss_nisplus_endgrent (void)
172 __libc_lock_lock (lock
);
175 nis_freeresult (result
);
179 nis_freenames (names
);
183 __libc_lock_unlock (lock
);
185 return NSS_STATUS_SUCCESS
;
188 static enum nss_status
189 internal_nisplus_getgrent_r (struct group
*gr
, char *buffer
, size_t buflen
)
193 /* Get the next entry until we found a correct one. */
198 names
= nis_getnames ("group.org_dir");
199 if (names
== NULL
|| names
[0] == NULL
)
200 return NSS_STATUS_UNAVAIL
;
202 result
= nis_first_entry (names
[0]);
203 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
204 return niserr2nss (result
->status
);
210 res
= nis_next_entry (names
[0], &result
->cookie
);
211 nis_freeresult (result
);
213 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
214 return niserr2nss (result
->status
);
217 parse_res
= _nss_nisplus_parse_grent (result
, gr
, buffer
, buflen
);
221 return NSS_STATUS_SUCCESS
;
225 _nss_nisplus_getgrent_r (struct group
*result
, char *buffer
, size_t buflen
)
229 __libc_lock_lock (lock
);
231 status
= internal_nisplus_getgrent_r (result
, buffer
, buflen
);
233 __libc_lock_unlock (lock
);
239 _nss_nisplus_getgrnam_r (const char *name
, struct group
*gr
,
240 char *buffer
, size_t buflen
)
244 if (name
== NULL
|| strlen (name
) > 8)
245 return NSS_STATUS_NOTFOUND
;
249 char buf
[strlen (name
) + 24];
251 sprintf (buf
, "[name=%s],group.org_dir", name
);
253 result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
255 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
257 enum nss_status status
= niserr2nss (result
->status
);
259 nis_freeresult (result
);
263 parse_res
= _nss_nisplus_parse_grent (result
, gr
, buffer
, buflen
);
265 nis_freeresult (result
);
268 return NSS_STATUS_SUCCESS
;
270 if (!parse_res
&& errno
== ERANGE
)
271 return NSS_STATUS_TRYAGAIN
;
273 return NSS_STATUS_NOTFOUND
;
278 _nss_nisplus_getgrgid_r (const gid_t gid
, struct group
*gr
,
279 char *buffer
, size_t buflen
)
285 sprintf (buf
, "[gid=%d],group.org_dir", gid
);
287 result
= nis_list (buf
, EXPAND_NAME
, NULL
, NULL
);
289 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
291 enum nss_status status
= niserr2nss (result
->status
);
293 nis_freeresult (result
);
297 parse_res
= _nss_nisplus_parse_grent (result
, gr
, buffer
, buflen
);
299 nis_freeresult (result
);
302 return NSS_STATUS_SUCCESS
;
304 if (!parse_res
&& errno
== ERANGE
)
305 return NSS_STATUS_TRYAGAIN
;
307 return NSS_STATUS_NOTFOUND
;