d1dc992c83321e6eba0c753fc3b7a8a5e79db95e
[platform/upstream/glibc.git] / nis / nss_nis / nis-alias.c
1 /* Copyright (C) 1996, 1997, 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.
4
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.
9
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.
14
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.  */
19
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>
28
29 #include "nss-nis.h"
30
31 __libc_lock_define_initialized (static, lock)
32
33 static bool_t new_start = 1;
34 static char *oldkey = NULL;
35 static int oldkeylen = 0;
36
37 static int
38 _nss_nis_parse_aliasent (const char *key, char *alias, struct aliasent *result,
39                          char *buffer, size_t buflen, int *errnop)
40 {
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;
46
47   result->alias_members_len = 0;
48   *first_unused = '\0';
49   first_unused++;
50   strcpy (first_unused, key);
51
52   if (first_unused[room_left - 1] != '\0')
53     {
54       /* The line is too long for our buffer.  */
55     no_more_room:
56       *errnop = ERANGE;
57       return -1;
58     }
59
60   result->alias_name = first_unused;
61
62   /* Terminate the line for any case.  */
63   cp = strpbrk (alias, "#\n");
64   if (cp != NULL)
65     *cp = '\0';
66
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;
73
74   line = alias;
75
76   while (*line != '\0')
77     {
78       /* Skip leading blanks.  */
79       while (isspace (*line))
80         line++;
81
82       if (*line == '\0')
83         break;
84
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;
89
90       while (*line != '\0' && *line != ',')
91         line++;
92
93       if (line != result->alias_members[result->alias_members_len])
94         {
95           *line = '\0';
96           line++;
97           result->alias_members_len++;
98         }
99     }
100   return result->alias_members_len == 0 ? 0 : 1;
101 }
102
103 enum nss_status
104 _nss_nis_setaliasent (void)
105 {
106   __libc_lock_lock (lock);
107
108   new_start = 1;
109   if (oldkey != NULL)
110     {
111       free (oldkey);
112       oldkey = NULL;
113       oldkeylen = 0;
114     }
115
116   __libc_lock_unlock (lock);
117
118   return NSS_STATUS_SUCCESS;
119 }
120
121 enum nss_status
122 _nss_nis_endaliasent (void)
123 {
124   __libc_lock_lock (lock);
125
126   new_start = 1;
127   if (oldkey != NULL)
128     {
129       free (oldkey);
130       oldkey = NULL;
131       oldkeylen = 0;
132     }
133
134   __libc_lock_unlock (lock);
135
136   return NSS_STATUS_SUCCESS;
137 }
138
139 static enum nss_status
140 internal_nis_getaliasent_r (struct aliasent *alias, char *buffer,
141                             size_t buflen, int *errnop)
142 {
143   char *domain;
144   char *result;
145   int len;
146   char *outkey;
147   int keylen;
148   char *p;
149   int parse_res;
150
151   if (yp_get_default_domain (&domain))
152     return NSS_STATUS_UNAVAIL;
153
154   alias->alias_local = 0;
155
156   /* Get the next entry until we found a correct one. */
157   do
158     {
159       enum nss_status retval;
160
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)
169         {
170           if (retval == NSS_STATUS_NOTFOUND)
171             *errnop = ENOENT;
172           else if (retval == NSS_STATUS_TRYAGAIN)
173             *errnop = errno;
174           return retval;
175         }
176
177       if ((size_t) (len + 1) > buflen)
178         {
179           free (result);
180           *errnop = ERANGE;
181           return NSS_STATUS_TRYAGAIN;
182         }
183       p = strncpy (buffer, result, len);
184       buffer[len] = '\0';
185       while (isspace (*p))
186         ++p;
187       free (result);
188
189       parse_res = _nss_nis_parse_aliasent (outkey, p, alias, buffer, buflen,
190                                            errnop);
191       if (parse_res == -1)
192         {
193           free (outkey);
194           *errnop = ERANGE;
195           return NSS_STATUS_TRYAGAIN;
196         }
197
198       free (oldkey);
199       oldkey = outkey;
200       oldkeylen = keylen;
201       new_start = 0;
202     }
203   while (!parse_res);
204
205   return NSS_STATUS_SUCCESS;
206 }
207
208 enum nss_status
209 _nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen,
210                         int *errnop)
211 {
212   enum nss_status status;
213
214   __libc_lock_lock (lock);
215
216   status = internal_nis_getaliasent_r (alias, buffer, buflen, errnop);
217
218   __libc_lock_unlock (lock);
219
220   return status;
221 }
222
223 enum nss_status
224 _nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias,
225                            char *buffer, size_t buflen, int *errnop)
226 {
227   enum nss_status retval;
228   int parse_res;
229   char *domain;
230   char *result;
231   int len;
232   char *p;
233   size_t namlen = strlen (name);
234   char name2[namlen + 1];
235   int i;
236
237   if (name == NULL)
238     {
239       *errnop = EINVAL;
240       return NSS_STATUS_UNAVAIL;
241     }
242
243   if (yp_get_default_domain (&domain))
244     return NSS_STATUS_UNAVAIL;
245
246   /* Convert name to lowercase.  */
247   for (i = 0; i < namlen; ++i)
248     name2[i] = tolower (name[i]);
249   name2[i] = '\0';
250
251   retval = yperr2nss (yp_match (domain, "mail.aliases", name2, namlen,
252                                 &result, &len));
253
254   if (retval != NSS_STATUS_SUCCESS)
255     {
256       if (retval == NSS_STATUS_TRYAGAIN)
257         *errnop = errno;
258       return retval;
259     }
260
261   if ((size_t) (len + 1) > buflen)
262     {
263       free (result);
264       *errnop = ERANGE;
265       return NSS_STATUS_TRYAGAIN;
266     }
267
268   p = strncpy (buffer, result, len);
269   buffer[len] = '\0';
270   while (isspace (*p))
271     ++p;
272   free (result);
273
274   alias->alias_local = 0;
275   parse_res = _nss_nis_parse_aliasent (name, p, alias, buffer, buflen, errnop);
276   if (parse_res < 1)
277     {
278       if (parse_res == -1)
279         return NSS_STATUS_TRYAGAIN;
280       else
281         return NSS_STATUS_NOTFOUND;
282     }
283
284   return NSS_STATUS_SUCCESS;
285 }