Updated to fedora-glibc-20050627T0850
[glibc.git] / glibc-compat / nss_nis / nis-alias.c
blob14149699d34bb4e367f4de3103e397d5e1999dc6
1 /* Copyright (C) 1996, 1998 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 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. */
20 #include <nss.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <string.h>
24 #include <glibc-compat/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 = NULL;
35 static int oldkeylen = 0;
37 static int
38 _nss_nis_parse_aliasent (const char *key, char *alias, struct aliasent *result,
39 char *buffer, size_t buflen)
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 __set_errno (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;
121 enum nss_status
122 _nss_nis_endaliasent (void)
124 __libc_lock_lock (lock);
126 new_start = 1;
127 if (oldkey != NULL)
129 free (oldkey);
130 oldkey = NULL;
131 oldkeylen = 0;
134 __libc_lock_unlock (lock);
136 return NSS_STATUS_SUCCESS;
139 static enum nss_status
140 internal_nis_getaliasent_r (struct aliasent *alias, char *buffer,
141 size_t buflen)
143 char *domain;
144 char *result;
145 int len;
146 char *outkey;
147 int keylen;
148 char *p;
149 int parse_res;
151 if (yp_get_default_domain (&domain))
152 return NSS_STATUS_UNAVAIL;
154 alias->alias_local = 0;
156 /* Get the next entry until we found a correct one. */
159 enum nss_status retval;
161 if (new_start)
162 retval = yperr2nss (yp_first (domain, "mail.aliases",
163 &outkey, &keylen, &result, &len));
164 else
165 retval = yperr2nss ( yp_next (domain, "mail.aliases", oldkey,
166 oldkeylen, &outkey, &keylen,
167 &result, &len));
168 if (retval != NSS_STATUS_SUCCESS)
170 if (retval == NSS_STATUS_TRYAGAIN)
171 __set_errno (EAGAIN);
172 return retval;
175 if ((size_t) (len + 1) > buflen)
177 free (result);
178 __set_errno (ERANGE);
179 return NSS_STATUS_TRYAGAIN;
181 p = strncpy (buffer, result, len);
182 buffer[len] = '\0';
183 while (isspace (*p))
184 ++p;
185 free (result);
187 parse_res = _nss_nis_parse_aliasent (outkey, p, alias, buffer, buflen);
188 if (parse_res == -1)
190 __set_errno (ERANGE);
191 return NSS_STATUS_TRYAGAIN;
194 free (oldkey);
195 oldkey = outkey;
196 oldkeylen = keylen;
197 new_start = 0;
199 while (!parse_res);
201 return NSS_STATUS_SUCCESS;
204 enum nss_status
205 _nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen)
207 enum nss_status status;
209 __libc_lock_lock (lock);
211 status = internal_nis_getaliasent_r (alias, buffer, buflen);
213 __libc_lock_unlock (lock);
215 return status;
218 enum nss_status
219 _nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias,
220 char *buffer, size_t buflen)
222 enum nss_status retval;
223 int parse_res;
224 char *domain;
225 char *result;
226 int len;
227 char *p;
228 size_t namlen = strlen (name);
229 char name2[namlen + 1];
230 int i;
232 if (name == NULL)
234 __set_errno (EINVAL);
235 return NSS_STATUS_UNAVAIL;
238 if (yp_get_default_domain (&domain))
239 return NSS_STATUS_UNAVAIL;
241 /* Convert name to lowercase. */
242 for (i = 0; i < namlen; ++i)
243 name2[i] = tolower (name[i]);
244 name2[i] = '\0';
246 retval = yperr2nss (yp_match (domain, "mail.aliases", name, namlen,
247 &result, &len));
249 if (retval != NSS_STATUS_SUCCESS)
251 if (retval == NSS_STATUS_TRYAGAIN)
252 __set_errno (EAGAIN);
253 return retval;
256 if ((size_t) (len + 1) > buflen)
258 free (result);
259 __set_errno (ERANGE);
260 return NSS_STATUS_TRYAGAIN;
263 p = strncpy (buffer, result, len);
264 buffer[len] = '\0';
265 while (isspace (*p))
266 ++p;
267 free (result);
269 alias->alias_local = 0;
270 parse_res = _nss_nis_parse_aliasent (name, p, alias, buffer, buflen);
271 if (parse_res == -1)
272 return NSS_STATUS_TRYAGAIN;
273 else
274 if (parse_res == 0)
275 return NSS_STATUS_NOTFOUND;
276 else
277 return NSS_STATUS_SUCCESS;