2.9
[glibc/nacl-glibc.git] / nis / nss_nis / nis-alias.c
blob9286e36ba64e379f6b0c699042eeebbd55d3267b
1 /* Copyright (C) 1996-2002, 2003, 2006 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <nss.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <aliases.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/yp.h>
27 #include <rpcsvc/ypclnt.h>
29 #include "nss-nis.h"
31 __libc_lock_define_initialized (static, lock)
33 static bool_t new_start = 1;
34 static char *oldkey;
35 static int oldkeylen;
37 static int
38 _nss_nis_parse_aliasent (const char *key, char *alias, struct aliasent *result,
39 char *buffer, size_t buflen, int *errnop)
41 char *first_unused = buffer + strlen (alias) + 1;
42 size_t room_left =
43 buflen - (buflen % __alignof__ (char *)) - strlen (alias) - 2;
44 char *line;
45 char *cp;
47 result->alias_members_len = 0;
48 *first_unused = '\0';
49 first_unused++;
50 strcpy (first_unused, key);
52 if (first_unused[room_left - 1] != '\0')
54 /* The line is too long for our buffer. */
55 no_more_room:
56 *errnop = ERANGE;
57 return -1;
60 result->alias_name = first_unused;
62 /* Terminate the line for any case. */
63 cp = strpbrk (alias, "#\n");
64 if (cp != NULL)
65 *cp = '\0';
67 first_unused += strlen (result->alias_name) + 1;
68 /* Adjust the pointer so it is aligned for
69 storing pointers. */
70 first_unused += __alignof__ (char *) - 1;
71 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
72 result->alias_members = (char **) first_unused;
74 line = alias;
76 while (*line != '\0')
78 /* Skip leading blanks. */
79 while (isspace (*line))
80 line++;
82 if (*line == '\0')
83 break;
85 if (room_left < sizeof (char *))
86 goto no_more_room;
87 room_left -= sizeof (char *);
88 result->alias_members[result->alias_members_len] = line;
90 while (*line != '\0' && *line != ',')
91 line++;
93 if (line != result->alias_members[result->alias_members_len])
95 *line = '\0';
96 line++;
97 result->alias_members_len++;
100 return result->alias_members_len == 0 ? 0 : 1;
103 enum nss_status
104 _nss_nis_setaliasent (void)
106 __libc_lock_lock (lock);
108 new_start = 1;
109 if (oldkey != NULL)
111 free (oldkey);
112 oldkey = NULL;
113 oldkeylen = 0;
116 __libc_lock_unlock (lock);
118 return NSS_STATUS_SUCCESS;
120 /* The 'endaliasent' function is identical. */
121 strong_alias (_nss_nis_setaliasent, _nss_nis_endaliasent)
123 static enum nss_status
124 internal_nis_getaliasent_r (struct aliasent *alias, char *buffer,
125 size_t buflen, int *errnop)
127 char *domain;
129 if (__builtin_expect (yp_get_default_domain (&domain), 0))
130 return NSS_STATUS_UNAVAIL;
132 alias->alias_local = 0;
134 /* Get the next entry until we found a correct one. */
135 int parse_res;
138 char *result;
139 int len;
140 char *outkey;
141 int keylen;
142 int yperr;
144 if (new_start)
145 yperr = yp_first (domain, "mail.aliases", &outkey, &keylen, &result,
146 &len);
147 else
148 yperr = yp_next (domain, "mail.aliases", oldkey, oldkeylen, &outkey,
149 &keylen, &result, &len);
151 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
153 enum nss_status retval = yperr2nss (yperr);
155 if (retval == NSS_STATUS_TRYAGAIN)
156 *errnop = errno;
157 return retval;
160 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
162 free (result);
163 *errnop = ERANGE;
164 return NSS_STATUS_TRYAGAIN;
166 char *p = strncpy (buffer, result, len);
167 buffer[len] = '\0';
168 while (isspace (*p))
169 ++p;
170 free (result);
172 parse_res = _nss_nis_parse_aliasent (outkey, p, alias, buffer,
173 buflen, errnop);
174 if (__builtin_expect (parse_res == -1, 0))
176 free (outkey);
177 *errnop = ERANGE;
178 return NSS_STATUS_TRYAGAIN;
181 free (oldkey);
182 oldkey = outkey;
183 oldkeylen = keylen;
184 new_start = 0;
186 while (!parse_res);
188 return NSS_STATUS_SUCCESS;
191 enum nss_status
192 _nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen,
193 int *errnop)
195 enum nss_status status;
197 __libc_lock_lock (lock);
199 status = internal_nis_getaliasent_r (alias, buffer, buflen, errnop);
201 __libc_lock_unlock (lock);
203 return status;
206 enum nss_status
207 _nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias,
208 char *buffer, size_t buflen, int *errnop)
210 if (name == NULL)
212 *errnop = EINVAL;
213 return NSS_STATUS_UNAVAIL;
216 size_t namlen = strlen (name);
217 char name2[namlen + 1];
219 char *domain;
220 if (__builtin_expect (yp_get_default_domain (&domain), 0))
221 return NSS_STATUS_UNAVAIL;
223 /* Convert name to lowercase. */
224 size_t i;
225 for (i = 0; i < namlen; ++i)
226 name2[i] = _tolower (name[i]);
227 name2[i] = '\0';
229 char *result;
230 int len;
231 int yperr = yp_match (domain, "mail.aliases", name2, namlen, &result, &len);
233 if (__builtin_expect (yperr != YPERR_SUCCESS, 0))
235 enum nss_status retval = yperr2nss (yperr);
237 if (retval == NSS_STATUS_TRYAGAIN)
238 *errnop = errno;
239 return retval;
242 if (__builtin_expect ((size_t) (len + 1) > buflen, 0))
244 free (result);
245 *errnop = ERANGE;
246 return NSS_STATUS_TRYAGAIN;
249 char *p = strncpy (buffer, result, len);
250 buffer[len] = '\0';
251 while (isspace (*p))
252 ++p;
253 free (result);
255 alias->alias_local = 0;
256 int parse_res = _nss_nis_parse_aliasent (name, p, alias, buffer, buflen,
257 errnop);
258 if (__builtin_expect (parse_res < 1, 0))
260 if (parse_res == -1)
261 return NSS_STATUS_TRYAGAIN;
262 else
263 return NSS_STATUS_NOTFOUND;
266 return NSS_STATUS_SUCCESS;