* libio/fmemopen.c (fmemopen): Free stream memory in case of
[platform/upstream/glibc.git] / nis / nss_nis / nis-alias.c
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.
4
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.
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    Lesser General Public License for more details.
14
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.  */
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;
35 static int oldkeylen;
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 /* The 'endaliasent' function is identical.  */
121 strong_alias (_nss_nis_setaliasent, _nss_nis_endaliasent)
122
123 static enum nss_status
124 internal_nis_getaliasent_r (struct aliasent *alias, char *buffer,
125                             size_t buflen, int *errnop)
126 {
127   char *domain;
128   char *result;
129   int len;
130   char *outkey;
131   int keylen;
132   char *p;
133   int parse_res;
134
135   if (yp_get_default_domain (&domain))
136     return NSS_STATUS_UNAVAIL;
137
138   alias->alias_local = 0;
139
140   /* Get the next entry until we found a correct one. */
141   do
142     {
143       enum nss_status retval;
144
145       if (new_start)
146         retval = yperr2nss (yp_first (domain, "mail.aliases",
147                                       &outkey, &keylen, &result, &len));
148       else
149         retval = yperr2nss ( yp_next (domain, "mail.aliases", oldkey,
150                                       oldkeylen, &outkey, &keylen,
151                                       &result, &len));
152       if (retval != NSS_STATUS_SUCCESS)
153         {
154           if (retval == NSS_STATUS_TRYAGAIN)
155             *errnop = errno;
156           return retval;
157         }
158
159       if ((size_t) (len + 1) > buflen)
160         {
161           free (result);
162           *errnop = ERANGE;
163           return NSS_STATUS_TRYAGAIN;
164         }
165       p = strncpy (buffer, result, len);
166       buffer[len] = '\0';
167       while (isspace (*p))
168         ++p;
169       free (result);
170
171       parse_res = _nss_nis_parse_aliasent (outkey, p, alias, buffer, buflen,
172                                            errnop);
173       if (parse_res == -1)
174         {
175           free (outkey);
176           *errnop = ERANGE;
177           return NSS_STATUS_TRYAGAIN;
178         }
179
180       free (oldkey);
181       oldkey = outkey;
182       oldkeylen = keylen;
183       new_start = 0;
184     }
185   while (!parse_res);
186
187   return NSS_STATUS_SUCCESS;
188 }
189
190 enum nss_status
191 _nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen,
192                         int *errnop)
193 {
194   enum nss_status status;
195
196   __libc_lock_lock (lock);
197
198   status = internal_nis_getaliasent_r (alias, buffer, buflen, errnop);
199
200   __libc_lock_unlock (lock);
201
202   return status;
203 }
204
205 enum nss_status
206 _nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias,
207                            char *buffer, size_t buflen, int *errnop)
208 {
209   if (name == NULL)
210     {
211       *errnop = EINVAL;
212       return NSS_STATUS_UNAVAIL;
213     }
214
215   size_t namlen = strlen (name);
216   char name2[namlen + 1];
217
218   char *domain;
219   if (yp_get_default_domain (&domain))
220     return NSS_STATUS_UNAVAIL;
221
222   /* Convert name to lowercase.  */
223   size_t i;
224   for (i = 0; i < namlen; ++i)
225     name2[i] = _tolower (name[i]);
226   name2[i] = '\0';
227
228   char *result;
229   int len;
230   enum nss_status retval = yperr2nss (yp_match (domain, "mail.aliases", name2,
231                                                 namlen, &result, &len));
232
233   if (retval != NSS_STATUS_SUCCESS)
234     {
235       if (retval == NSS_STATUS_TRYAGAIN)
236         *errnop = errno;
237       return retval;
238     }
239
240   if ((size_t) (len + 1) > buflen)
241     {
242       free (result);
243       *errnop = ERANGE;
244       return NSS_STATUS_TRYAGAIN;
245     }
246
247   char *p = strncpy (buffer, result, len);
248   buffer[len] = '\0';
249   while (isspace (*p))
250     ++p;
251   free (result);
252
253   alias->alias_local = 0;
254   int parse_res = _nss_nis_parse_aliasent (name, p, alias, buffer, buflen,
255                                            errnop);
256   if (parse_res < 1)
257     {
258       if (parse_res == -1)
259         return NSS_STATUS_TRYAGAIN;
260       else
261         return NSS_STATUS_NOTFOUND;
262     }
263
264   return NSS_STATUS_SUCCESS;
265 }