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 <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
30 __libc_lock_define_initialized (static, lock
);
32 static nis_result
*result
= NULL
;
33 static nis_name tablename_val
= NULL
;
34 static u_long tablename_len
= 0;
36 #define NISENTRYVAL(idx,col,res) \
37 ((res)->objects.objects_val[(idx)].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)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
43 _nss_nisplus_parse_servent (nis_result
*result
, struct servent
*serv
,
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 __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
||
56 strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
57 "services_tbl") != 0 ||
58 result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 4)
61 if (NISENTRYLEN (0, 0, result
) >= room_left
)
67 strncpy (first_unused
, NISENTRYVAL (0, 0, result
),
68 NISENTRYLEN (0, 0, result
));
69 first_unused
[NISENTRYLEN (0, 0, result
)] = '\0';
70 serv
->s_name
= first_unused
;
71 room_left
-= (strlen (first_unused
) +1);
72 first_unused
+= strlen (first_unused
) +1;
74 if (NISENTRYLEN (0, 2, result
) >= room_left
)
76 strncpy (first_unused
, NISENTRYVAL (0, 2, result
),
77 NISENTRYLEN (0, 2, result
));
78 first_unused
[NISENTRYLEN (0, 2, result
)] = '\0';
79 serv
->s_proto
= first_unused
;
80 room_left
-= (strlen (first_unused
) +1);
81 first_unused
+= strlen (first_unused
) +1;
83 serv
->s_port
= atoi (NISENTRYVAL (0, 3, result
));
87 for (i
= 0; i
< result
->objects
.objects_len
; i
++)
89 if (strcmp (NISENTRYVAL (i
, 1, result
), serv
->s_name
) != 0)
91 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
94 p
= __stpncpy (p
, NISENTRYVAL (i
, 1, result
),
95 NISENTRYLEN (i
, 1, result
));
97 room_left
-= (NISENTRYLEN (i
, 1, result
) + 1);
103 /* Adjust the pointer so it is aligned for
105 first_unused
+= __alignof__ (char *) - 1;
106 first_unused
-= ((first_unused
- (char *) 0) % __alignof__ (char *));
107 serv
->s_aliases
= (char **) first_unused
;
108 if (room_left
< sizeof (char *))
110 room_left
-= (sizeof (char *));
111 serv
->s_aliases
[0] = NULL
;
114 while (*line
!= '\0')
116 /* Skip leading blanks. */
117 while (isspace (*line
))
123 if (room_left
< sizeof (char *))
126 room_left
-= sizeof (char *);
127 serv
->s_aliases
[i
] = line
;
129 while (*line
!= '\0' && *line
!= ' ')
139 serv
->s_aliases
[i
+1] = NULL
;
145 static enum nss_status
146 _nss_create_tablename (void)
148 if (tablename_val
== NULL
)
150 char buf
[40 + strlen (nis_local_directory ())];
153 p
= __stpcpy (buf
, "services.org_dir.");
154 p
= __stpcpy (p
, nis_local_directory ());
155 tablename_val
= __strdup (buf
);
156 if (tablename_val
== NULL
)
157 return NSS_STATUS_TRYAGAIN
;
158 tablename_len
= strlen (tablename_val
);
160 return NSS_STATUS_SUCCESS
;
165 _nss_nisplus_setservent (void)
167 enum nss_status status
= NSS_STATUS_SUCCESS
;
169 __libc_lock_lock (lock
);
172 nis_freeresult (result
);
175 if (tablename_val
== NULL
)
176 status
= _nss_create_tablename ();
178 __libc_lock_unlock (lock
);
184 _nss_nisplus_endservent (void)
186 __libc_lock_lock (lock
);
189 nis_freeresult (result
);
192 __libc_lock_unlock (lock
);
194 return NSS_STATUS_SUCCESS
;
197 static enum nss_status
198 internal_nisplus_getservent_r (struct servent
*serv
, char *buffer
,
203 /* Get the next entry until we found a correct one. */
206 nis_result
*saved_res
;
211 if (tablename_val
== NULL
)
212 if (_nss_create_tablename () != NSS_STATUS_SUCCESS
)
213 return NSS_STATUS_UNAVAIL
;
215 result
= nis_first_entry (tablename_val
);
216 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
217 return niserr2nss (result
->status
);
224 res
= nis_next_entry (tablename_val
, &result
->cookie
);
226 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
228 nis_freeresult (saved_res
);
229 return niserr2nss (result
->status
);
233 if ((parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
,
236 nis_freeresult (result
);
238 return NSS_STATUS_TRYAGAIN
;
243 nis_freeresult (saved_res
);
248 return NSS_STATUS_SUCCESS
;
252 _nss_nisplus_getservent_r (struct servent
*result
, char *buffer
,
257 __libc_lock_lock (lock
);
259 status
= internal_nisplus_getservent_r (result
, buffer
, buflen
);
261 __libc_lock_unlock (lock
);
267 _nss_nisplus_getservbyname_r (const char *name
, const char *protocol
,
268 struct servent
*serv
,
269 char *buffer
, size_t buflen
)
273 if (tablename_val
== NULL
)
274 if (_nss_create_tablename () != NSS_STATUS_SUCCESS
)
275 return NSS_STATUS_UNAVAIL
;
277 if (name
== NULL
|| protocol
== NULL
)
279 __set_errno (EINVAL
);
280 return NSS_STATUS_NOTFOUND
;
285 char buf
[strlen (name
) + 255 + tablename_len
];
287 /* Search at first in the alias list, and use the correct name
288 for the next search */
289 sprintf (buf
, "[name=%s,proto=%s],%s", name
, protocol
,
291 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
293 /* If we do not find it, try it as original name. But if the
294 database is correct, we should find it in the first case, too */
295 if ((result
->status
!= NIS_SUCCESS
&&
296 result
->status
!= NIS_S_SUCCESS
) ||
297 __type_of (result
->objects
.objects_val
) != NIS_ENTRY_OBJ
||
298 strcmp (result
->objects
.objects_val
->EN_data
.en_type
,
299 "services_tbl") != 0 ||
300 result
->objects
.objects_val
->EN_data
.en_cols
.en_cols_len
< 4)
301 sprintf (buf
, "[cname=%s,proto=%s],%s", name
, protocol
,
304 sprintf (buf
, "[cname=%s,proto=%s],%s",
305 NISENTRYVAL (0, 0, result
), protocol
, tablename_val
);
307 nis_freeresult (result
);
308 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
310 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
312 enum nss_status status
= niserr2nss (result
->status
);
314 nis_freeresult (result
);
318 parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
);
319 nis_freeresult (result
);
322 return NSS_STATUS_TRYAGAIN
;
325 return NSS_STATUS_SUCCESS
;
327 return NSS_STATUS_NOTFOUND
;
332 _nss_nisplus_getservbynumber_r (const int number
, const char *protocol
,
333 struct servent
*serv
,
334 char *buffer
, size_t buflen
)
336 if (tablename_val
== NULL
)
337 if (_nss_create_tablename () != NSS_STATUS_SUCCESS
)
338 return NSS_STATUS_UNAVAIL
;
340 if (protocol
== NULL
)
342 __set_errno (EINVAL
);
343 return NSS_STATUS_NOTFOUND
;
349 char buf
[60 + strlen (protocol
) + tablename_len
];
351 sprintf (buf
, "[number=%d,proto=%s],%s",
352 number
, protocol
, tablename_val
);
354 result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
, NULL
, NULL
);
356 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
358 enum nss_status status
= niserr2nss (result
->status
);
360 nis_freeresult (result
);
364 parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
);
365 nis_freeresult (result
);
368 return NSS_STATUS_TRYAGAIN
;
371 return NSS_STATUS_SUCCESS
;
373 return NSS_STATUS_NOTFOUND
;