Fix iov[] size in SH register_dump()
[glibc.git] / nis / nss_nisplus / nisplus-alias.c
blob57858721a189b88a7a307891199e5b25554b95e2
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.
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.
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.
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. */
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>
30 #include "nss-nisplus.h"
32 __libc_lock_define_initialized (static, lock)
34 static nis_result *result;
35 static u_long next_entry;
36 static nis_name tablename_val;
37 static size_t tablename_len;
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)
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)
45 static enum nss_status
46 _nss_create_tablename (int *errnop)
48 if (tablename_val == NULL)
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.";
54 char *p = malloc (sizeof (prefix) + local_dir_len);
55 if (p == NULL)
57 *errnop = errno;
58 return NSS_STATUS_TRYAGAIN;
61 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
63 tablename_len = sizeof (prefix) - 1 + local_dir_len;
65 atomic_write_barrier ();
67 tablename_val = p;
70 return NSS_STATUS_SUCCESS;
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)
78 if (result == NULL)
79 return 0;
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;
88 if (NISENTRYLEN (entry, 1, result) >= buflen)
90 /* The line is too long for our buffer. */
91 no_more_room:
92 *errnop = ERANGE;
93 return -1;
96 char *cp = __stpncpy (buffer, NISENTRYVAL (entry, 1, result),
97 NISENTRYLEN (entry, 1, result));
98 *cp = '\0';
100 char *first_unused = cp + 1;
101 size_t room_left = buflen - (first_unused - buffer);
103 alias->alias_local = 0;
104 alias->alias_members_len = 0;
106 if (NISENTRYLEN (entry, 0, result) >= room_left)
107 goto no_more_room;
109 cp = __stpncpy (first_unused, NISENTRYVAL (entry, 0, result),
110 NISENTRYLEN (entry, 0, result));
111 *cp = '\0';
112 alias->alias_name = first_unused;
114 /* Terminate the line for any case. */
115 cp = strpbrk (alias->alias_name, "#\n");
116 if (cp != NULL)
117 *cp = '\0';
119 size_t len = strlen (alias->alias_name) + 1;
120 first_unused += len;
121 room_left -= len;
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;
133 alias->alias_members = (char **) first_unused;
135 char *line = buffer;
136 while (*line != '\0')
138 /* Skip leading blanks. */
139 while (isspace (*line))
140 ++line;
142 if (*line == '\0')
143 break;
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;
150 while (*line != '\0' && *line != ',')
151 ++line;
153 if (line != alias->alias_members[alias->alias_members_len])
155 *line++ = '\0';
156 ++alias->alias_members_len;
158 else if (*line == ',')
159 ++line;
162 return alias->alias_members_len == 0 ? 0 : 1;
165 static enum nss_status
166 internal_setaliasent (void)
168 enum nss_status status;
169 int err;
171 if (result != NULL)
173 nis_freeresult (result);
174 result = NULL;
177 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
178 return NSS_STATUS_UNAVAIL;
180 next_entry = 0;
181 result = nis_list (tablename_val, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
182 if (result == NULL)
184 status = NSS_STATUS_TRYAGAIN;
185 __set_errno (ENOMEM);
187 else
189 status = niserr2nss (result->status);
190 if (status != NSS_STATUS_SUCCESS)
192 nis_freeresult (result);
193 result = NULL;
196 return status;
199 enum nss_status
200 _nss_nisplus_setaliasent (void)
202 enum nss_status status;
204 __libc_lock_lock (lock);
206 status = internal_setaliasent ();
208 __libc_lock_unlock (lock);
210 return status;
213 enum nss_status
214 _nss_nisplus_endaliasent (void)
216 __libc_lock_lock (lock);
218 if (result != NULL)
220 nis_freeresult (result);
221 result = NULL;
223 next_entry = 0;
225 __libc_lock_unlock (lock);
227 return NSS_STATUS_SUCCESS;
230 static enum nss_status
231 internal_nisplus_getaliasent_r (struct aliasent *alias,
232 char *buffer, size_t buflen, int *errnop)
234 int parse_res;
236 if (result == NULL)
238 enum nss_status status;
240 status = internal_setaliasent ();
241 if (result == NULL || status != NSS_STATUS_SUCCESS)
242 return status;
245 /* Get the next entry until we found a correct one. */
248 if (next_entry >= result->objects.objects_len)
249 return NSS_STATUS_NOTFOUND;
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;
256 ++next_entry;
258 while (!parse_res);
260 return NSS_STATUS_SUCCESS;
263 enum nss_status
264 _nss_nisplus_getaliasent_r (struct aliasent *result, char *buffer,
265 size_t buflen, int *errnop)
267 int status;
269 __libc_lock_lock (lock);
271 status = internal_nisplus_getaliasent_r (result, buffer, buflen, errnop);
273 __libc_lock_unlock (lock);
275 return status;
278 enum nss_status
279 _nss_nisplus_getaliasbyname_r (const char *name, struct aliasent *alias,
280 char *buffer, size_t buflen, int *errnop)
282 int parse_res;
284 if (tablename_val == NULL)
286 __libc_lock_lock (lock);
288 enum nss_status status = _nss_create_tablename (errnop);
290 __libc_lock_unlock (lock);
292 if (status != NSS_STATUS_SUCCESS)
293 return status;
296 if (name != NULL)
298 *errnop = EINVAL;
299 return NSS_STATUS_UNAVAIL;
302 char buf[strlen (name) + 9 + tablename_len];
303 int olderr = errno;
305 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
307 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
309 if (result == NULL)
311 *errnop = ENOMEM;
312 return NSS_STATUS_TRYAGAIN;
315 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
317 enum nss_status status = niserr2nss (result->status);
318 nis_freeresult (result);
319 return status;
322 parse_res = _nss_nisplus_parse_aliasent (result, 0, alias,
323 buffer, buflen, errnop);
325 /* We do not need the lookup result anymore. */
326 nis_freeresult (result);
328 if (__builtin_expect (parse_res < 1, 0))
330 __set_errno (olderr);
332 if (parse_res == -1)
333 return NSS_STATUS_TRYAGAIN;
334 else
335 return NSS_STATUS_NOTFOUND;
338 return NSS_STATUS_SUCCESS;