Replace FSF snail mail address with URLs.
[glibc.git] / nis / nss_nisplus / nisplus-alias.c
blob9ec1239291c22ace726f63cdcc3809f988bff74c
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, see
18 <http://www.gnu.org/licenses/>. */
20 #include <atomic.h>
21 #include <nss.h>
22 #include <errno.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <aliases.h>
26 #include <bits/libc-lock.h>
27 #include <rpcsvc/nis.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock)
33 static nis_result *result;
34 static u_long next_entry;
35 static nis_name tablename_val;
36 static size_t tablename_len;
38 #define NISENTRYVAL(idx, col, res) \
39 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_val)
41 #define NISENTRYLEN(idx, col, res) \
42 (NIS_RES_OBJECT (res)[idx].EN_data.en_cols.en_cols_val[col].ec_value.ec_value_len)
44 static enum nss_status
45 _nss_create_tablename (int *errnop)
47 if (tablename_val == NULL)
49 const char *local_dir = nis_local_directory ();
50 size_t local_dir_len = strlen (local_dir);
51 static const char prefix[] = "mail_aliases.org_dir.";
53 char *p = malloc (sizeof (prefix) + local_dir_len);
54 if (p == NULL)
56 *errnop = errno;
57 return NSS_STATUS_TRYAGAIN;
60 memcpy (__stpcpy (p, prefix), local_dir, local_dir_len + 1);
62 tablename_len = sizeof (prefix) - 1 + local_dir_len;
64 atomic_write_barrier ();
66 tablename_val = p;
69 return NSS_STATUS_SUCCESS;
72 static int
73 _nss_nisplus_parse_aliasent (nis_result *result, unsigned long entry,
74 struct aliasent *alias, char *buffer,
75 size_t buflen, int *errnop)
77 if (result == NULL)
78 return 0;
80 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS)
81 || __type_of (&NIS_RES_OBJECT (result)[entry]) != NIS_ENTRY_OBJ
82 || strcmp (NIS_RES_OBJECT (result)[entry].EN_data.en_type,
83 "mail_aliases") != 0
84 || NIS_RES_OBJECT (result)[entry].EN_data.en_cols.en_cols_len < 2)
85 return 0;
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;
95 char *cp = __stpncpy (buffer, NISENTRYVAL (entry, 1, result),
96 NISENTRYLEN (entry, 1, result));
97 *cp = '\0';
99 char *first_unused = cp + 1;
100 size_t room_left = buflen - (first_unused - buffer);
102 alias->alias_local = 0;
103 alias->alias_members_len = 0;
105 if (NISENTRYLEN (entry, 0, result) >= room_left)
106 goto no_more_room;
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 size_t len = strlen (alias->alias_name) + 1;
119 first_unused += len;
120 room_left -= len;
122 /* Adjust the pointer so it is aligned for
123 storing pointers. */
124 size_t adjust = ((__alignof__ (char *)
125 - (first_unused - (char *) 0) % __alignof__ (char *))
126 % __alignof__ (char *));
127 if (room_left < adjust)
128 goto no_more_room;
129 first_unused += adjust;
130 room_left -= adjust;
132 alias->alias_members = (char **) first_unused;
134 char *line = buffer;
135 while (*line != '\0')
137 /* Skip leading blanks. */
138 while (isspace (*line))
139 ++line;
141 if (*line == '\0')
142 break;
144 if (room_left < sizeof (char *))
145 goto no_more_room;
146 room_left -= sizeof (char *);
147 alias->alias_members[alias->alias_members_len] = line;
149 while (*line != '\0' && *line != ',')
150 ++line;
152 if (line != alias->alias_members[alias->alias_members_len])
154 *line++ = '\0';
155 ++alias->alias_members_len;
157 else if (*line == ',')
158 ++line;
161 return alias->alias_members_len == 0 ? 0 : 1;
164 static enum nss_status
165 internal_setaliasent (void)
167 enum nss_status status;
168 int err;
170 if (result != NULL)
172 nis_freeresult (result);
173 result = NULL;
176 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
177 return NSS_STATUS_UNAVAIL;
179 next_entry = 0;
180 result = nis_list (tablename_val, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
181 if (result == NULL)
183 status = NSS_STATUS_TRYAGAIN;
184 __set_errno (ENOMEM);
186 else
188 status = niserr2nss (result->status);
189 if (status != NSS_STATUS_SUCCESS)
191 nis_freeresult (result);
192 result = NULL;
195 return status;
198 enum nss_status
199 _nss_nisplus_setaliasent (void)
201 enum nss_status status;
203 __libc_lock_lock (lock);
205 status = internal_setaliasent ();
207 __libc_lock_unlock (lock);
209 return status;
212 enum nss_status
213 _nss_nisplus_endaliasent (void)
215 __libc_lock_lock (lock);
217 if (result != NULL)
219 nis_freeresult (result);
220 result = NULL;
222 next_entry = 0;
224 __libc_lock_unlock (lock);
226 return NSS_STATUS_SUCCESS;
229 static enum nss_status
230 internal_nisplus_getaliasent_r (struct aliasent *alias,
231 char *buffer, size_t buflen, int *errnop)
233 int parse_res;
235 if (result == NULL)
237 enum nss_status status;
239 status = internal_setaliasent ();
240 if (result == NULL || status != NSS_STATUS_SUCCESS)
241 return status;
244 /* Get the next entry until we found a correct one. */
247 if (next_entry >= result->objects.objects_len)
248 return NSS_STATUS_NOTFOUND;
250 parse_res = _nss_nisplus_parse_aliasent (result, next_entry, alias,
251 buffer, buflen, errnop);
252 if (parse_res == -1)
253 return NSS_STATUS_TRYAGAIN;
255 ++next_entry;
257 while (!parse_res);
259 return NSS_STATUS_SUCCESS;
262 enum nss_status
263 _nss_nisplus_getaliasent_r (struct aliasent *result, char *buffer,
264 size_t buflen, int *errnop)
266 int status;
268 __libc_lock_lock (lock);
270 status = internal_nisplus_getaliasent_r (result, buffer, buflen, errnop);
272 __libc_lock_unlock (lock);
274 return status;
277 enum nss_status
278 _nss_nisplus_getaliasbyname_r (const char *name, struct aliasent *alias,
279 char *buffer, size_t buflen, int *errnop)
281 int parse_res;
283 if (tablename_val == NULL)
285 __libc_lock_lock (lock);
287 enum nss_status status = _nss_create_tablename (errnop);
289 __libc_lock_unlock (lock);
291 if (status != NSS_STATUS_SUCCESS)
292 return status;
295 if (name != NULL)
297 *errnop = EINVAL;
298 return NSS_STATUS_UNAVAIL;
301 char buf[strlen (name) + 9 + tablename_len];
302 int olderr = errno;
304 snprintf (buf, sizeof (buf), "[name=%s],%s", name, tablename_val);
306 nis_result *result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
308 if (result == NULL)
310 *errnop = ENOMEM;
311 return NSS_STATUS_TRYAGAIN;
314 if (__builtin_expect (niserr2nss (result->status) != NSS_STATUS_SUCCESS, 0))
316 enum nss_status status = niserr2nss (result->status);
317 nis_freeresult (result);
318 return status;
321 parse_res = _nss_nisplus_parse_aliasent (result, 0, alias,
322 buffer, buflen, errnop);
324 /* We do not need the lookup result anymore. */
325 nis_freeresult (result);
327 if (__builtin_expect (parse_res < 1, 0))
329 __set_errno (olderr);
331 if (parse_res == -1)
332 return NSS_STATUS_TRYAGAIN;
333 else
334 return NSS_STATUS_NOTFOUND;
337 return NSS_STATUS_SUCCESS;