Add _STRING_INLINE_unaligned and string_private.h
[glibc.git] / nis / nss_nisplus / nisplus-service.c
blobadbe26661764b8c10b8bbaed4e11e4e4c4c0f57f
1 /* Copyright (C) 1997-2016 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/>. */
19 #include <atomic.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <netdb.h>
23 #include <nss.h>
24 #include <string.h>
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)
43 static int
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;
50 if (result == NULL)
51 return 0;
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)
57 return 0;
59 if (NISENTRYLEN (0, 0, result) >= room_left)
61 no_more_room:
62 *errnop = ERANGE;
63 return -1;
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;
70 room_left -= len;
71 first_unused += len;
73 if (NISENTRYLEN (0, 2, result) >= room_left)
74 goto no_more_room;
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;
80 room_left -= len;
81 first_unused += len;
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)
94 goto no_more_room;
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
104 storing pointers. */
105 size_t adjust = ((__alignof__ (char *)
106 - (first_unused - (char *) 0) % __alignof__ (char *))
107 % __alignof__ (char *));
108 if (room_left < adjust + sizeof (char *))
109 goto no_more_room;
110 first_unused += adjust;
111 room_left -= adjust;
112 serv->s_aliases = (char **) first_unused;
114 /* For the terminating NULL pointer. */
115 room_left -= (sizeof (char *));
117 unsigned int i = 0;
118 while (*line != '\0')
120 /* Skip leading blanks. */
121 while (isspace (*line))
122 ++line;
124 if (*line == '\0')
125 break;
127 if (room_left < sizeof (char *))
128 goto no_more_room;
130 room_left -= sizeof (char *);
131 serv->s_aliases[i++] = line;
133 while (*line != '\0' && *line != ' ')
134 ++line;
136 if (*line == ' ')
137 *line++ = '\0';
139 serv->s_aliases[i] = NULL;
141 return 1;
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);
155 if (p == NULL)
157 *errnop = errno;
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 ();
167 tablename_val = p;
170 return NSS_STATUS_SUCCESS;
174 enum nss_status
175 _nss_nisplus_setservent (int stayopen)
177 enum nss_status status = NSS_STATUS_SUCCESS;
178 int err;
180 __libc_lock_lock (lock);
182 if (result != NULL)
184 nis_freeresult (result);
185 result = NULL;
188 if (tablename_val == NULL)
189 status = _nss_create_tablename (&err);
191 __libc_lock_unlock (lock);
193 return status;
196 enum nss_status
197 _nss_nisplus_endservent (void)
199 __libc_lock_lock (lock);
201 if (result != NULL)
203 nis_freeresult (result);
204 result = NULL;
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)
216 int parse_res;
218 /* Get the next entry until we found a correct one. */
221 nis_result *saved_res;
223 if (result == NULL)
225 saved_res = NULL;
226 if (tablename_val == NULL)
228 enum nss_status status = _nss_create_tablename (errnop);
230 if (status != NSS_STATUS_SUCCESS)
231 return status;
234 result = nis_first_entry (tablename_val);
235 if (result == NULL)
237 *errnop = errno;
238 return NSS_STATUS_TRYAGAIN;
240 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
241 return niserr2nss (result->status);
243 else
245 saved_res = result;
246 result = nis_next_entry (tablename_val, &result->cookie);
247 if (result == NULL)
249 *errnop = errno;
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,
260 buflen, errnop);
261 if (__glibc_unlikely (parse_res == -1))
263 nis_freeresult (result);
264 result = saved_res;
265 *errnop = ERANGE;
266 return NSS_STATUS_TRYAGAIN;
268 else
270 if (saved_res)
271 nis_freeresult (saved_res);
274 while (!parse_res);
276 return NSS_STATUS_SUCCESS;
279 enum nss_status
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);
289 return status;
292 enum nss_status
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)
306 return status;
309 if (name == NULL || protocol == NULL)
311 *errnop = EINVAL;
312 return NSS_STATUS_NOTFOUND;
315 size_t protocol_len = strlen (protocol);
316 char buf[strlen (name) + protocol_len + 17 + tablename_len];
317 int olderr = errno;
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,
322 tablename_val);
323 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS | USE_DGRAM,
324 NULL, NULL);
326 if (result != NULL)
328 char *bufptr = buf;
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,
336 "services_tbl") != 0
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,
339 tablename_val);
340 else
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
346 + tablename_len);
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,
354 NULL, NULL);
357 if (result == NULL)
359 *errnop = ENOMEM;
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);
370 return status;
373 int parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen,
374 errnop);
375 nis_freeresult (result);
377 if (__glibc_unlikely (parse_res < 1))
379 if (parse_res == -1)
381 *errnop = ERANGE;
382 return NSS_STATUS_TRYAGAIN;
384 else
386 __set_errno (olderr);
387 return NSS_STATUS_NOTFOUND;
391 return NSS_STATUS_SUCCESS;
394 enum nss_status
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)
408 return status;
411 if (protocol == NULL)
413 *errnop = EINVAL;
414 return NSS_STATUS_NOTFOUND;
417 char buf[17 + 3 * sizeof (int) + strlen (protocol) + tablename_len];
418 int olderr = errno;
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,
424 NULL, NULL);
426 if (result == NULL)
428 *errnop = ENOMEM;
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);
439 return status;
442 int parse_res = _nss_nisplus_parse_servent (result, serv, buffer, buflen,
443 errnop);
444 nis_freeresult (result);
446 if (__glibc_unlikely (parse_res < 1))
448 if (parse_res == -1)
450 *errnop = ERANGE;
451 return NSS_STATUS_TRYAGAIN;
453 else
455 __set_errno (olderr);
456 return NSS_STATUS_NOTFOUND;
460 return NSS_STATUS_SUCCESS;