1 /* Copyright (C) 1996-2013 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996.
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/>. */
24 #include <bits/libc-lock.h>
25 #include <rpcsvc/yp.h>
26 #include <rpcsvc/ypclnt.h>
30 __libc_lock_define_initialized (static, lock)
32 static bool_t new_start = 1;
37 _nss_nis_parse_aliasent (const char *key, char *alias, struct aliasent *result,
38 char *buffer, size_t buflen, int *errnop)
40 char *first_unused = buffer + strlen (alias) + 1;
42 buflen - (buflen % __alignof__ (char *)) - strlen (alias) - 2;
46 result->alias_members_len = 0;
49 strcpy (first_unused, key);
51 if (first_unused[room_left - 1] != '\0')
53 /* The line is too long for our buffer. */
59 result->alias_name = first_unused;
61 /* Terminate the line for any case. */
62 cp = strpbrk (alias, "#\n");
66 first_unused += strlen (result->alias_name) + 1;
67 /* Adjust the pointer so it is aligned for
69 first_unused += __alignof__ (char *) - 1;
70 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
71 result->alias_members = (char **) first_unused;
77 /* Skip leading blanks. */
78 while (isspace (*line))
84 if (room_left < sizeof (char *))
86 room_left -= sizeof (char *);
87 result->alias_members[result->alias_members_len] = line;
89 while (*line != '\0' && *line != ',')
92 if (line != result->alias_members[result->alias_members_len])
96 result->alias_members_len++;
99 return result->alias_members_len == 0 ? 0 : 1;
103 _nss_nis_setaliasent (void)
105 __libc_lock_lock (lock);
115 __libc_lock_unlock (lock);
117 return NSS_STATUS_SUCCESS;
119 /* The 'endaliasent' function is identical. */
120 strong_alias (_nss_nis_setaliasent, _nss_nis_endaliasent)
122 static enum nss_status
123 internal_nis_getaliasent_r (struct aliasent *alias, char *buffer,
124 size_t buflen, int *errnop)
128 if (__builtin_expect (yp_get_default_domain (&domain), 0))
129 return NSS_STATUS_UNAVAIL;
131 alias->alias_local = 0;
133 /* Get the next entry until we found a correct one. */
144 yperr = yp_first (domain, "mail.aliases", &outkey, &keylen, &result,
147 yperr = yp_next (domain, "mail.aliases", oldkey, oldkeylen, &outkey,
148 &keylen, &result, &len);
150 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
152 enum nss_status retval = yperr2nss (yperr);
154 if (retval == NSS_STATUS_TRYAGAIN)
159 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
163 return NSS_STATUS_TRYAGAIN;
165 char *p = strncpy (buffer, result, len);
171 parse_res = _nss_nis_parse_aliasent (outkey, p, alias, buffer,
173 if (__builtin_expect (parse_res == -1, 0))
177 return NSS_STATUS_TRYAGAIN;
187 return NSS_STATUS_SUCCESS;
191 _nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen,
194 enum nss_status status;
196 __libc_lock_lock (lock);
198 status = internal_nis_getaliasent_r (alias, buffer, buflen, errnop);
200 __libc_lock_unlock (lock);
206 _nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias,
207 char *buffer, size_t buflen, int *errnop)
212 return NSS_STATUS_UNAVAIL;
216 if (__builtin_expect (yp_get_default_domain (&domain), 0))
217 return NSS_STATUS_UNAVAIL;
219 size_t namlen = strlen (name);
221 int use_alloca = __libc_use_alloca (namlen + 1);
223 name2 = __alloca (namlen + 1);
226 name2 = malloc (namlen + 1);
230 return NSS_STATUS_TRYAGAIN;
234 /* Convert name to lowercase. */
236 for (i = 0; i < namlen; ++i)
237 name2[i] = _tolower (name[i]);
242 int yperr = yp_match (domain, "mail.aliases", name2, namlen, &result, &len);
247 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
249 enum nss_status retval = yperr2nss (yperr);
251 if (retval == NSS_STATUS_TRYAGAIN)
256 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
260 return NSS_STATUS_TRYAGAIN;
263 char *p = strncpy (buffer, result, len);
269 alias->alias_local = 0;
270 int parse_res = _nss_nis_parse_aliasent (name, p, alias, buffer, buflen,
272 if (__builtin_expect (parse_res < 1, 0))
275 return NSS_STATUS_TRYAGAIN;
277 return NSS_STATUS_NOTFOUND;
280 return NSS_STATUS_SUCCESS;