Update.
[glibc.git] / nis / nss_nisplus / nisplus-alias.c
blob76c6f258132031557fbe8d46e4f8fb6c2a8e7445
1 /* Copyright (C) 1997 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 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 = NULL;
33 static u_long next_entry = 0;
34 static nis_name tablename_val = NULL;
35 static u_long tablename_len = 0;
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 (void)
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)
55 return NSS_STATUS_TRYAGAIN;
56 tablename_len = strlen (tablename_val);
58 return NSS_STATUS_SUCCESS;
61 static int
62 _nss_nisplus_parse_aliasent (nis_result *result, unsigned long entry,
63 struct aliasent *alias, char *buffer,
64 size_t buflen)
66 if (result == NULL)
67 return 0;
69 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
70 __type_of (&result->objects.objects_val[entry]) != NIS_ENTRY_OBJ ||
71 strcmp(result->objects.objects_val[entry].EN_data.en_type,
72 "mail_aliases") != 0 ||
73 result->objects.objects_val[entry].EN_data.en_cols.en_cols_len < 2)
74 return 0;
75 else
77 char *first_unused = buffer + NISENTRYLEN(0, 1, result) + 1;
78 size_t room_left =
79 buflen - (buflen % __alignof__ (char *)) -
80 NISENTRYLEN(0, 1, result) - 2;
81 char *line;
82 char *cp;
84 if (NISENTRYLEN(entry, 1, result) >= buflen)
86 /* The line is too long for our buffer. */
87 no_more_room:
88 __set_errno (ERANGE);
89 return -1;
91 else
93 strncpy (buffer, NISENTRYVAL(entry, 1, result),
94 NISENTRYLEN(entry, 1, result));
95 buffer[NISENTRYLEN(entry, 1, result)] = '\0';
98 if (NISENTRYLEN(entry, 0, result) >= room_left)
99 goto no_more_room;
101 alias->alias_local = 0;
102 alias->alias_members_len = 0;
103 *first_unused = '\0';
104 ++first_unused;
105 strcpy (first_unused, NISENTRYVAL(entry, 0, result));
106 first_unused[NISENTRYLEN(entry, 0, result)] = '\0';
107 alias->alias_name = first_unused;
109 /* Terminate the line for any case. */
110 cp = strpbrk (alias->alias_name, "#\n");
111 if (cp != NULL)
112 *cp = '\0';
114 first_unused += strlen (alias->alias_name) +1;
115 /* Adjust the pointer so it is aligned for
116 storing pointers. */
117 first_unused += __alignof__ (char *) - 1;
118 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
119 alias->alias_members = (char **) first_unused;
121 line = buffer;
123 while (*line != '\0')
125 /* Skip leading blanks. */
126 while (isspace (*line))
127 line++;
129 if (*line == '\0')
130 break;
132 if (room_left < sizeof (char *))
133 goto no_more_room;
134 room_left -= sizeof (char *);
135 alias->alias_members[alias->alias_members_len] = line;
137 while (*line != '\0' && *line != ',')
138 line++;
140 if (line != alias->alias_members[alias->alias_members_len])
142 *line = '\0';
143 line++;
144 alias->alias_members_len++;
148 return alias->alias_members_len == 0 ? 0 : 1;
152 static enum nss_status
153 internal_setaliasent (void)
155 enum nss_status status;
157 if (result)
158 nis_freeresult (result);
159 result = NULL;
161 if (_nss_create_tablename () != NSS_STATUS_SUCCESS)
162 return NSS_STATUS_UNAVAIL;
164 next_entry = 0;
165 result = nis_list(tablename_val, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
166 status = niserr2nss (result->status);
167 if (status != NSS_STATUS_SUCCESS)
169 nis_freeresult (result);
170 result = NULL;
172 return status;
175 enum nss_status
176 _nss_nisplus_setaliasent (void)
178 enum nss_status status;
180 __libc_lock_lock (lock);
182 status = internal_setaliasent ();
184 __libc_lock_unlock (lock);
186 return status;
189 enum nss_status
190 _nss_nisplus_endaliasent (void)
192 __libc_lock_lock (lock);
194 if (result)
195 nis_freeresult (result);
196 result = NULL;
197 next_entry = 0;
199 __libc_lock_unlock (lock);
201 return NSS_STATUS_SUCCESS;
204 static enum nss_status
205 internal_nisplus_getaliasent_r (struct aliasent *alias,
206 char *buffer, size_t buflen)
208 int parse_res;
210 if (result == NULL)
212 enum nss_status status;
214 status = internal_setaliasent ();
215 if (result == NULL || status != NSS_STATUS_SUCCESS)
216 return status;
219 /* Get the next entry until we found a correct one. */
222 if (next_entry >= result->objects.objects_len)
223 return NSS_STATUS_NOTFOUND;
225 if ((parse_res = _nss_nisplus_parse_aliasent (result, next_entry, alias,
226 buffer, buflen)) == -1)
227 return NSS_STATUS_TRYAGAIN;
229 ++next_entry;
230 } while (!parse_res);
232 return NSS_STATUS_SUCCESS;
235 enum nss_status
236 _nss_nisplus_getaliasent_r (struct aliasent *result, char *buffer,
237 size_t buflen)
239 int status;
241 __libc_lock_lock (lock);
243 status = internal_nisplus_getaliasent_r (result, buffer, buflen);
245 __libc_lock_unlock (lock);
247 return status;
250 enum nss_status
251 _nss_nisplus_getaliasbyname_r (const char *name, struct aliasent *alias,
252 char *buffer, size_t buflen)
254 int parse_res;
256 if (tablename_val == NULL)
257 if (_nss_create_tablename() != NSS_STATUS_SUCCESS)
258 return NSS_STATUS_UNAVAIL;
260 if (name != NULL || strlen(name) <= 8)
262 nis_result *result;
263 char buf[strlen (name) + 30 + tablename_len];
265 sprintf(buf, "[name=%s],%s", name, tablename_val);
267 result = nis_list(buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
269 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
270 return niserr2nss (result->status);
272 if ((parse_res = _nss_nisplus_parse_aliasent (result, 0, alias,
273 buffer, buflen)) == -1)
274 return NSS_STATUS_TRYAGAIN;
276 if (parse_res)
277 return NSS_STATUS_SUCCESS;
279 return NSS_STATUS_NOTFOUND;