1 /* Copyright (C) 1997-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@suse.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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
25 #include <rpcsvc/nis.h>
26 #include <libc-lock.h>
28 #include "nss-nisplus.h"
30 __libc_lock_define_initialized (static, lock
);
32 static nis_result
*result
;
33 static nis_name tablename_val
;
34 static u_long tablename_len
;
36 #define NISENTRYVAL(idx, col, res) \
37 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
39 #define NISENTRYLEN(idx, col, res) \
40 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
44 _nss_nisplus_parse_servent (nis_result
*result
, struct servent
*serv
,
45 char *buffer
, size_t buflen
, int *errnop
)
47 char *first_unused
= buffer
;
48 size_t room_left
= buflen
;
53 if ((result
->status
!= NIS_SUCCESS
&& result
->status
!= NIS_S_SUCCESS
)
54 || __type_of (NIS_RES_OBJECT (result
)) != NIS_ENTRY_OBJ
55 || strcmp (NIS_RES_OBJECT (result
)->EN_data
.en_type
, "services_tbl") != 0
56 || NIS_RES_OBJECT (result
)->EN_data
.en_cols
.en_cols_len
< 4)
59 if (NISENTRYLEN (0, 0, result
) >= room_left
)
65 strncpy (first_unused
, NISENTRYVAL (0, 0, result
),
66 NISENTRYLEN (0, 0, result
));
67 first_unused
[NISENTRYLEN (0, 0, result
)] = '\0';
68 serv
->s_name
= first_unused
;
69 size_t len
= strlen (first_unused
) + 1;
73 if (NISENTRYLEN (0, 2, result
) >= room_left
)
75 strncpy (first_unused
, NISENTRYVAL (0, 2, result
),
76 NISENTRYLEN (0, 2, result
));
77 first_unused
[NISENTRYLEN (0, 2, result
)] = '\0';
78 serv
->s_proto
= first_unused
;
79 len
= strlen (first_unused
) + 1;
83 serv
->s_port
= htons (atoi (NISENTRYVAL (0, 3, result
)));
85 /* XXX Rewrite at some point to allocate the array first and then
86 copy the strings. It wasteful to first concatenate the strings
87 to just split them again later. */
88 char *line
= first_unused
;
89 for (unsigned int i
= 0; i
< NIS_RES_NUMOBJ (result
); ++i
)
91 if (strcmp (NISENTRYVAL (i
, 1, result
), serv
->s_name
) != 0)
93 if (NISENTRYLEN (i
, 1, result
) + 2 > room_left
)
95 *first_unused
++ = ' ';
96 first_unused
= __stpncpy (first_unused
, NISENTRYVAL (i
, 1, result
),
97 NISENTRYLEN (i
, 1, result
));
98 room_left
-= NISENTRYLEN (i
, 1, result
) + 1;
101 *first_unused
++ = '\0';
103 /* Adjust the pointer so it is aligned for
105 size_t adjust
= ((__alignof__ (char *)
106 - (first_unused
- (char *) 0) % __alignof__ (char *))
107 % __alignof__ (char *));
108 if (room_left
< adjust
+ sizeof (char *))
110 first_unused
+= adjust
;
112 serv
->s_aliases
= (char **) first_unused
;
114 /* For the terminating NULL pointer. */
115 room_left
-= (sizeof (char *));
118 while (*line
!= '\0')
120 /* Skip leading blanks. */
121 while (isspace (*line
))
127 if (room_left
< sizeof (char *))
130 room_left
-= sizeof (char *);
131 serv
->s_aliases
[i
++] = line
;
133 while (*line
!= '\0' && *line
!= ' ')
139 serv
->s_aliases
[i
] = NULL
;
145 static enum nss_status
146 _nss_create_tablename (int *errnop
)
148 if (tablename_val
== NULL
)
150 const char *local_dir
= nis_local_directory ();
151 size_t local_dir_len
= strlen (local_dir
);
152 static const char prefix
[] = "services.org_dir.";
154 char *p
= malloc (sizeof (prefix
) + local_dir_len
);
158 return NSS_STATUS_TRYAGAIN
;
161 memcpy (__stpcpy (p
, prefix
), local_dir
, local_dir_len
+ 1);
163 tablename_len
= sizeof (prefix
) - 1 + local_dir_len
;
165 atomic_write_barrier ();
170 return NSS_STATUS_SUCCESS
;
175 _nss_nisplus_setservent (int stayopen
)
177 enum nss_status status
= NSS_STATUS_SUCCESS
;
180 __libc_lock_lock (lock
);
184 nis_freeresult (result
);
188 if (tablename_val
== NULL
)
189 status
= _nss_create_tablename (&err
);
191 __libc_lock_unlock (lock
);
197 _nss_nisplus_endservent (void)
199 __libc_lock_lock (lock
);
203 nis_freeresult (result
);
207 __libc_lock_unlock (lock
);
209 return NSS_STATUS_SUCCESS
;
212 static enum nss_status
213 internal_nisplus_getservent_r (struct servent
*serv
, char *buffer
,
214 size_t buflen
, int *errnop
)
218 /* Get the next entry until we found a correct one. */
221 nis_result
*saved_res
;
226 if (tablename_val
== NULL
)
228 enum nss_status status
= _nss_create_tablename (errnop
);
230 if (status
!= NSS_STATUS_SUCCESS
)
234 result
= nis_first_entry (tablename_val
);
238 return NSS_STATUS_TRYAGAIN
;
240 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
241 return niserr2nss (result
->status
);
246 result
= nis_next_entry (tablename_val
, &result
->cookie
);
250 return NSS_STATUS_TRYAGAIN
;
252 if (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
)
254 nis_freeresult (saved_res
);
255 return niserr2nss (result
->status
);
259 parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
,
261 if (__glibc_unlikely (parse_res
== -1))
263 nis_freeresult (result
);
266 return NSS_STATUS_TRYAGAIN
;
271 nis_freeresult (saved_res
);
276 return NSS_STATUS_SUCCESS
;
280 _nss_nisplus_getservent_r (struct servent
*result
, char *buffer
,
281 size_t buflen
, int *errnop
)
283 __libc_lock_lock (lock
);
285 int status
= internal_nisplus_getservent_r (result
, buffer
, buflen
, errnop
);
287 __libc_lock_unlock (lock
);
293 _nss_nisplus_getservbyname_r (const char *name
, const char *protocol
,
294 struct servent
*serv
,
295 char *buffer
, size_t buflen
, int *errnop
)
297 if (tablename_val
== NULL
)
299 __libc_lock_lock (lock
);
301 enum nss_status status
= _nss_create_tablename (errnop
);
303 __libc_lock_unlock (lock
);
305 if (status
!= NSS_STATUS_SUCCESS
)
309 if (name
== NULL
|| protocol
== NULL
)
312 return NSS_STATUS_NOTFOUND
;
315 size_t protocol_len
= strlen (protocol
);
316 char buf
[strlen (name
) + protocol_len
+ 17 + tablename_len
];
319 /* Search at first in the alias list, and use the correct name
320 for the next search */
321 snprintf (buf
, sizeof (buf
), "[name=%s,proto=%s],%s", name
, protocol
,
323 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
330 /* If we did not find it, try it as original name. But if the
331 database is correct, we should find it in the first case, too */
332 if ((result
->status
!= NIS_SUCCESS
333 && result
->status
!= NIS_S_SUCCESS
)
334 || __type_of (NIS_RES_OBJECT (result
)) != NIS_ENTRY_OBJ
335 || strcmp (NIS_RES_OBJECT (result
)->EN_data
.en_type
,
337 || NIS_RES_OBJECT (result
)->EN_data
.en_cols
.en_cols_len
< 4)
338 snprintf (buf
, sizeof (buf
), "[cname=%s,proto=%s],%s", name
, protocol
,
342 /* We need to allocate a new buffer since there is no
343 guarantee the returned name has a length limit. */
344 const char *entryval
= NISENTRYVAL(0, 0, result
);
345 size_t buflen
= (strlen (entryval
) + protocol_len
+ 17
347 bufptr
= alloca (buflen
);
348 snprintf (bufptr
, buflen
, "[cname=%s,proto=%s],%s",
349 entryval
, protocol
, tablename_val
);
352 nis_freeresult (result
);
353 result
= nis_list (bufptr
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
360 return NSS_STATUS_TRYAGAIN
;
363 if (__glibc_unlikely (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
))
365 enum nss_status status
= niserr2nss (result
->status
);
367 __set_errno (olderr
);
369 nis_freeresult (result
);
373 int parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
,
375 nis_freeresult (result
);
377 if (__glibc_unlikely (parse_res
< 1))
382 return NSS_STATUS_TRYAGAIN
;
386 __set_errno (olderr
);
387 return NSS_STATUS_NOTFOUND
;
391 return NSS_STATUS_SUCCESS
;
395 _nss_nisplus_getservbyport_r (const int number
, const char *protocol
,
396 struct servent
*serv
,
397 char *buffer
, size_t buflen
, int *errnop
)
399 if (tablename_val
== NULL
)
401 __libc_lock_lock (lock
);
403 enum nss_status status
= _nss_create_tablename (errnop
);
405 __libc_lock_unlock (lock
);
407 if (status
!= NSS_STATUS_SUCCESS
)
411 if (protocol
== NULL
)
414 return NSS_STATUS_NOTFOUND
;
417 char buf
[17 + 3 * sizeof (int) + strlen (protocol
) + tablename_len
];
420 snprintf (buf
, sizeof (buf
), "[port=%d,proto=%s],%s",
421 number
, protocol
, tablename_val
);
423 nis_result
*result
= nis_list (buf
, FOLLOW_PATH
| FOLLOW_LINKS
| USE_DGRAM
,
429 return NSS_STATUS_TRYAGAIN
;
432 if (__glibc_unlikely (niserr2nss (result
->status
) != NSS_STATUS_SUCCESS
))
434 enum nss_status status
= niserr2nss (result
->status
);
436 __set_errno (olderr
);
438 nis_freeresult (result
);
442 int parse_res
= _nss_nisplus_parse_servent (result
, serv
, buffer
, buflen
,
444 nis_freeresult (result
);
446 if (__glibc_unlikely (parse_res
< 1))
451 return NSS_STATUS_TRYAGAIN
;
455 __set_errno (olderr
);
456 return NSS_STATUS_NOTFOUND
;
460 return NSS_STATUS_SUCCESS
;