chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / nis / nss_nisplus / nisplus-alias.c
1 /* Copyright (C) 1997, 1998, 2001, 2002, 2003, 2005, 2006
2    Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public
8    License as published by the Free Software Foundation; either
9    version 2.1 of the License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; if not, write to the Free
18    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.  */
20
21 #include <atomic.h>
22 #include <nss.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <string.h>
26 #include <aliases.h>
27 #include <bits/libc-lock.h>
28 #include <rpcsvc/nis.h>
29
30 #include "nss-nisplus.h"
31
32 __libc_lock_define_initialized (static, lock)
33
34 static nis_result *result;
35 static u_long next_entry;
36 static nis_name tablename_val;
37 static size_t tablename_len;
38
39 #define NISENTRYVAL(idx, col, res) \
40         (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
41
42 #define NISENTRYLEN(idx, col, res) \
43         (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
44
45 static enum nss_status
46 _nss_create_tablename (int *errnop)
47 {
48   if (tablename_val == NULL)
49     {
50       const char *local_dir = nis_local_directory ();
51       size_t local_dir_len = strlen (local_dir);
52       static const char prefix[] = "mail_aliases.org_dir.";
53
54       char *p = malloc (sizeof (prefix) + local_dir_len);
55       if (p == NULL)
56         {
57           *errnop = errno;
58           return NSS_STATUS_TRYAGAIN;
59         }
60
61       memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
62
63       tablename_len = sizeof (prefix) - 1 + local_dir_len;
64
65       atomic_write_barrier ();
66
67       tablename_val = p;
68     }
69
70   return NSS_STATUS_SUCCESS;
71 }
72
73 static int
74 _nss_nisplus_parse_aliasent (nis_result *result, unsigned long entry,
75                              struct aliasent *alias, char *buffer,
76                              size_t buflen, int *errnop)
77 {
78   if (result == NULL)
79     return 0;
80
81   if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
82       || __type_of (&NIS_RES_OBJECT (result)[entry]) != NIS_ENTRY_OBJ
83       || strcmp (NIS_RES_OBJECT (result)[entry].EN_data.en_type,
84                  "mail_aliases") != 0
85       || NIS_RES_OBJECT (result)[entry].EN_data.en_cols.en_cols_len < 2)
86     return 0;
87
88   if (NISENTRYLEN (entry, 1, result) >= buflen)
89     {
90       /* The line is too long for our buffer.  */
91     no_more_room:
92       *errnop = ERANGE;
93       return -1;
94     }
95
96   char *cp = __stpncpy (buffer, NISENTRYVAL (entry, 1, result),
97                         NISENTRYLEN (entry, 1, result));
98   *cp = '\0';
99
100   char *first_unused = cp + 1;
101   size_t room_left = buflen - (first_unused - buffer);
102
103   alias->alias_local = 0;
104   alias->alias_members_len = 0;
105
106   if (NISENTRYLEN (entry, 0, result) >= room_left)
107     goto no_more_room;
108
109   cp = __stpncpy (first_unused, NISENTRYVAL (entry, 0, result),
110                   NISENTRYLEN (entry, 0, result));
111   *cp = '\0';
112   alias->alias_name = first_unused;
113
114   /* Terminate the line for any case.  */
115   cp = strpbrk (alias->alias_name, "#\n");
116   if (cp != NULL)
117     *cp = '\0';
118
119   size_t len = strlen (alias->alias_name) + 1;
120   first_unused += len;
121   room_left -= len;
122
123   /* Adjust the pointer so it is aligned for
124      storing pointers.  */
125   size_t adjust = ((__alignof__ (char *)
126                     - (first_unused - (char *) 0) % __alignof__ (char *))
127                    % __alignof__ (char *));
128   if (room_left < adjust)
129     goto no_more_room;
130   first_unused += adjust;
131   room_left -= adjust;
132
133   alias->alias_members = (char **) first_unused;
134
135   char *line = buffer;
136   while (*line != '\0')
137     {
138       /* Skip leading blanks.  */
139       while (isspace (*line))
140         ++line;
141
142       if (*line == '\0')
143         break;
144
145       if (room_left < sizeof (char *))
146         goto no_more_room;
147       room_left -= sizeof (char *);
148       alias->alias_members[alias->alias_members_len] = line;
149
150       while (*line != '\0' && *line != ',')
151         ++line;
152
153       if (line != alias->alias_members[alias->alias_members_len])
154         {
155           *line++ = '\0';
156           ++alias->alias_members_len;
157         }
158       else if (*line == ',')
159         ++line;
160     }
161
162   return alias->alias_members_len == 0 ? 0 : 1;
163 }
164
165 static enum nss_status
166 internal_setaliasent (void)
167 {
168   enum nss_status status;
169   int err;
170
171   if (result !=  NULL)
172     {
173       nis_freeresult (result);
174       result = NULL;
175     }
176
177   if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
178     return NSS_STATUS_UNAVAIL;
179
180   next_entry = 0;
181   result = nis_list (tablename_val, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
182   if (result == NULL)
183     {
184       status = NSS_STATUS_TRYAGAIN;
185       __set_errno (ENOMEM);
186     }
187   else
188     {
189       status = niserr2nss (result->status);
190       if (status != NSS_STATUS_SUCCESS)
191         {
192           nis_freeresult (result);
193           result = NULL;
194         }
195     }
196   return status;
197 }
198
199 enum nss_status
200 _nss_nisplus_setaliasent (void)
201 {
202   enum nss_status status;
203
204   __libc_lock_lock (lock);
205
206   status = internal_setaliasent ();
207
208   __libc_lock_unlock (lock);
209
210   return status;
211 }
212
213 enum nss_status
214 _nss_nisplus_endaliasent (void)
215 {
216   __libc_lock_lock (lock);
217
218   if (result != NULL)
219     {
220       nis_freeresult (result);
221       result = NULL;
222     }
223   next_entry = 0;
224
225   __libc_lock_unlock (lock);
226
227   return NSS_STATUS_SUCCESS;
228 }
229
230 static enum nss_status
231 internal_nisplus_getaliasent_r (struct aliasent *alias,
232                                 char *buffer, size_t buflen, int *errnop)
233 {
234   int parse_res;
235
236   if (result == NULL)
237     {
238       enum nss_status status;
239
240       status = internal_setaliasent ();
241       if (result == NULL || status != NSS_STATUS_SUCCESS)
242         return status;
243     }
244
245   /* Get the next entry until we found a correct one. */
246   do
247     {
248       if (next_entry >= result->objects.objects_len)
249         return NSS_STATUS_NOTFOUND;
250
251       parse_res = _nss_nisplus_parse_aliasent (result, next_entry, alias,
252                                                buffer, buflen, errnop);
253       if (parse_res == -1)
254         return NSS_STATUS_TRYAGAIN;
255
256       ++next_entry;
257     }
258   while (!parse_res);
259
260   return NSS_STATUS_SUCCESS;
261 }
262
263 enum nss_status
264 _nss_nisplus_getaliasent_r (struct aliasent *result, char *buffer,
265                             size_t buflen, int *errnop)
266 {
267   int status;
268
269   __libc_lock_lock (lock);
270
271   status = internal_nisplus_getaliasent_r (result, buffer, buflen, errnop);
272
273   __libc_lock_unlock (lock);
274
275   return status;
276 }
277
278 enum nss_status
279 _nss_nisplus_getaliasbyname_r (const char *name, struct aliasent *alias,
280                             char *buffer, size_t buflen, int *errnop)
281 {
282   int parse_res;
283
284   if (tablename_val == NULL)
285     {
286       __libc_lock_lock (lock);
287
288       enum nss_status status = _nss_create_tablename (errnop);
289
290       __libc_lock_unlock (lock);
291
292       if (status != NSS_STATUS_SUCCESS)
293         return status;
294     }
295
296   if (name != NULL)
297     {
298       *errnop = EINVAL;
299       return NSS_STATUS_UNAVAIL;
300     }
301
302   char buf[strlen (name) + 9 + tablename_len];
303   int olderr = errno;
304
305   snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
306
307   nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
308
309   if (result == NULL)
310     {
311       *errnop = ENOMEM;
312       return NSS_STATUS_TRYAGAIN;
313     }
314
315   if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
316     {
317       enum nss_status status = niserr2nss (result->status);
318       nis_freeresult (result);
319       return status;
320     }
321
322   parse_res = _nss_nisplus_parse_aliasent (result, 0, alias,
323                                            buffer, buflen, errnop);
324
325   /* We do not need the lookup result anymore.  */
326   nis_freeresult (result);
327
328   if (__builtin_expect (parse_res < 1, 0))
329     {
330       __set_errno (olderr);
331
332       if (parse_res == -1)
333         return NSS_STATUS_TRYAGAIN;
334       else
335         return NSS_STATUS_NOTFOUND;
336     }
337
338   return NSS_STATUS_SUCCESS;
339 }