(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / nis / nss_nisplus / nisplus-alias.c
blob152e5fc3fccafa2848025ff3ad7ebe5359b68ca4
1 /* Copyright (C) 1997, 1998, 2001, 2002, 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
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.
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.
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. */
20 #include <nss.h>
21 #include <errno.h>
22 #include <ctype.h>
23 #include <string.h>
24 #include <aliases.h>
25 #include <bits/libc-lock.h>
26 #include <rpcsvc/nis.h>
28 #include "nss-nisplus.h"
30 __libc_lock_define_initialized (static, lock)
32 static nis_result *result;
33 static u_long next_entry;
34 static nis_name tablename_val;
35 static u_long tablename_len;
37 #define NISENTRYVAL(idx,col,res) \
38 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
40 #define NISENTRYLEN(idx,col,res) \
41 ((res)->objects.objects_val[(idx)].EN_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
43 static enum nss_status
44 _nss_create_tablename (int *errnop)
46 if (tablename_val == NULL)
48 char buf [40 + strlen (nis_local_directory ())];
49 char *p;
51 p = __stpcpy (buf, "mail_aliases.org_dir.");
52 p = __stpcpy (p, nis_local_directory ());
53 tablename_val = __strdup (buf);
54 if (tablename_val == NULL)
56 *errnop = errno;
57 return NSS_STATUS_TRYAGAIN;
59 tablename_len = strlen (tablename_val);
61 return NSS_STATUS_SUCCESS;
64 static int
65 _nss_nisplus_parse_aliasent (nis_result *result, unsigned long entry,
66 struct aliasent *alias, char *buffer,
67 size_t buflen, int *errnop)
69 if (result == NULL)
70 return 0;
72 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
73 || __type_of (&result->objects.objects_val[entry]) != NIS_ENTRY_OBJ
74 || strcmp (result->objects.objects_val[entry].EN_data.en_type,
75 "mail_aliases") != 0
76 || result->objects.objects_val[entry].EN_data.en_cols.en_cols_len < 2)
77 return 0;
78 else
80 char *first_unused = buffer + NISENTRYLEN (0, 1, result) + 1;
81 size_t room_left =
82 buflen - (buflen % __alignof__ (char *)) -
83 NISENTRYLEN (0, 1, result) - 2;
84 char *line;
85 char *cp;
87 if (NISENTRYLEN (entry, 1, result) >= buflen)
89 /* The line is too long for our buffer. */
90 no_more_room:
91 *errnop = ERANGE;
92 return -1;
94 else
96 cp = __stpncpy (buffer, NISENTRYVAL (entry, 1, result),
97 NISENTRYLEN (entry, 1, result));
98 *cp = '\0';
101 if (NISENTRYLEN(entry, 0, result) >= room_left)
102 goto no_more_room;
104 alias->alias_local = 0;
105 alias->alias_members_len = 0;
106 *first_unused = '\0';
107 ++first_unused;
108 cp = __stpncpy (first_unused, NISENTRYVAL (entry, 0, result),
109 NISENTRYLEN (entry, 0, result));
110 *cp = '\0';
111 alias->alias_name = first_unused;
113 /* Terminate the line for any case. */
114 cp = strpbrk (alias->alias_name, "#\n");
115 if (cp != NULL)
116 *cp = '\0';
118 first_unused += strlen (alias->alias_name) +1;
119 /* Adjust the pointer so it is aligned for
120 storing pointers. */
121 first_unused += __alignof__ (char *) - 1;
122 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
123 alias->alias_members = (char **) first_unused;
125 line = buffer;
127 while (*line != '\0')
129 /* Skip leading blanks. */
130 while (isspace (*line))
131 ++line;
133 if (*line == '\0')
134 break;
136 if (room_left < sizeof (char *))
137 goto no_more_room;
138 room_left -= sizeof (char *);
139 alias->alias_members[alias->alias_members_len] = line;
141 while (*line != '\0' && *line != ',')
142 ++line;
144 if (line != alias->alias_members[alias->alias_members_len])
146 *line++ = '\0';
147 alias->alias_members_len++;
151 return alias->alias_members_len == 0 ? 0 : 1;
155 static enum nss_status
156 internal_setaliasent (void)
158 enum nss_status status;
159 int err;
161 if (result)
162 nis_freeresult (result);
163 result = NULL;
165 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
166 return NSS_STATUS_UNAVAIL;
168 next_entry = 0;
169 result = nis_list (tablename_val, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
170 if (result == NULL)
172 status = NSS_STATUS_TRYAGAIN;
173 __set_errno (ENOMEM);
175 else
177 status = niserr2nss (result->status);
178 if (status != NSS_STATUS_SUCCESS)
180 nis_freeresult (result);
181 result = NULL;
184 return status;
187 enum nss_status
188 _nss_nisplus_setaliasent (void)
190 enum nss_status status;
192 __libc_lock_lock (lock);
194 status = internal_setaliasent ();
196 __libc_lock_unlock (lock);
198 return status;
201 enum nss_status
202 _nss_nisplus_endaliasent (void)
204 __libc_lock_lock (lock);
206 if (result)
207 nis_freeresult (result);
208 result = NULL;
209 next_entry = 0;
211 __libc_lock_unlock (lock);
213 return NSS_STATUS_SUCCESS;
216 static enum nss_status
217 internal_nisplus_getaliasent_r (struct aliasent *alias,
218 char *buffer, size_t buflen, int *errnop)
220 int parse_res;
222 if (result == NULL)
224 enum nss_status status;
226 status = internal_setaliasent ();
227 if (result == NULL || status != NSS_STATUS_SUCCESS)
228 return status;
231 /* Get the next entry until we found a correct one. */
234 if (next_entry >= result->objects.objects_len)
235 return NSS_STATUS_NOTFOUND;
237 parse_res = _nss_nisplus_parse_aliasent (result, next_entry, alias,
238 buffer, buflen, errnop);
239 if (parse_res == -1)
240 return NSS_STATUS_TRYAGAIN;
242 ++next_entry;
243 } while (!parse_res);
245 return NSS_STATUS_SUCCESS;
248 enum nss_status
249 _nss_nisplus_getaliasent_r (struct aliasent *result, char *buffer,
250 size_t buflen, int *errnop)
252 int status;
254 __libc_lock_lock (lock);
256 status = internal_nisplus_getaliasent_r (result, buffer, buflen, errnop);
258 __libc_lock_unlock (lock);
260 return status;
263 enum nss_status
264 _nss_nisplus_getaliasbyname_r (const char *name, struct aliasent *alias,
265 char *buffer, size_t buflen, int *errnop)
267 int parse_res;
269 if (tablename_val == NULL)
271 enum nss_status status = _nss_create_tablename (errnop);
272 if (status != NSS_STATUS_SUCCESS)
273 return status;
276 if (name != NULL)
278 *errnop = EINVAL;
279 return NSS_STATUS_UNAVAIL;
281 else
283 nis_result *result;
284 char buf[strlen (name) + 30 + tablename_len];
285 int olderr = errno;
287 sprintf (buf, "[name=%s],%s", name, tablename_val);
289 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
291 if (result == NULL)
293 *errnop = ENOMEM;
294 return NSS_STATUS_TRYAGAIN;
296 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
297 return niserr2nss (result->status);
299 parse_res = _nss_nisplus_parse_aliasent (result, 0, alias,
300 buffer, buflen, errnop);
301 if (parse_res < 1)
303 __set_errno (olderr);
305 if (parse_res == -1)
306 return NSS_STATUS_TRYAGAIN;
307 else
308 return NSS_STATUS_NOTFOUND;
310 return NSS_STATUS_SUCCESS;