Update.
[glibc.git] / nis / nss_nisplus / nisplus-alias.c
blobb27f1a87ab5b1b5fd5092b6228e9a979dc8e39aa
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 <libc-lock.h>
26 #include <rpcsvc/nis.h>
27 #include <rpcsvc/nislib.h>
29 #include "nss-nisplus.h"
31 __libc_lock_define_initialized (static, lock)
33 static nis_result *result = NULL;
34 static nis_name *names = NULL;
36 #define NISENTRYVAL(idx,col,res) \
37 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_val)
39 #define NISENTRYLEN(idx,col,res) \
40 ((res)->objects.objects_val[(idx)].zo_data.objdata_u.en_data.en_cols.en_cols_val[(col)].ec_value.ec_value_len)
42 static int
43 _nss_nisplus_parse_aliasent (nis_result *result, struct aliasent *alias,
44 char *buffer, size_t buflen)
46 if (result == NULL)
47 return 0;
49 if ((result->status != NIS_SUCCESS && result->status != NIS_S_SUCCESS) ||
50 result->objects.objects_len != 1 ||
51 result->objects.objects_val[0].zo_data.zo_type != ENTRY_OBJ ||
52 strcmp(result->objects.objects_val[0].zo_data.objdata_u.en_data.en_type,
53 "mail_aliases") != 0 ||
54 result->objects.objects_val[0].zo_data.objdata_u.en_data.en_cols.en_cols_len < 2)
55 return 0;
56 else
58 char *first_unused = buffer + NISENTRYLEN(0, 1, result) + 1;
59 size_t room_left =
60 buflen - (buflen % __alignof__ (char *)) -
61 NISENTRYLEN(0, 1, result) - 2;
62 char *line;
63 char *cp;
65 if (NISENTRYLEN(0, 1, result) >= buflen)
67 /* The line is too long for our buffer. */
68 no_more_room:
69 __set_errno (ERANGE);
70 return 0;
72 else
74 strncpy (buffer, NISENTRYVAL(0, 1, result), NISENTRYLEN(0, 1, result));
75 buffer[NISENTRYLEN(0, 1, result)] = '\0';
78 if (NISENTRYLEN(0, 0, result) >= room_left)
79 goto no_more_room;
81 alias->alias_local = 0;
82 alias->alias_members_len = 0;
83 *first_unused = '\0';
84 ++first_unused;
85 strcpy (first_unused, NISENTRYVAL(0, 0, result));
86 first_unused[NISENTRYLEN(0, 0, result)] = '\0';
87 alias->alias_name = first_unused;
89 /* Terminate the line for any case. */
90 cp = strpbrk (alias->alias_name, "#\n");
91 if (cp != NULL)
92 *cp = '\0';
94 first_unused += strlen (alias->alias_name) +1;
95 /* Adjust the pointer so it is aligned for
96 storing pointers. */
97 first_unused += __alignof__ (char *) - 1;
98 first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *));
99 alias->alias_members = (char **) first_unused;
101 line = buffer;
103 while (*line != '\0')
105 /* Skip leading blanks. */
106 while (isspace (*line))
107 line++;
109 if (*line == '\0')
110 break;
112 if (room_left < sizeof (char *))
113 goto no_more_room;
114 room_left -= sizeof (char *);
115 alias->alias_members[alias->alias_members_len] = line;
117 while (*line != '\0' && *line != ',')
118 line++;
120 if (line != alias->alias_members[alias->alias_members_len])
122 *line = '\0';
123 line++;
124 alias->alias_members_len++;
128 return alias->alias_members_len == 0 ? 0 : 1;
132 enum nss_status
133 _nss_nisplus_setaliasent (void)
135 __libc_lock_lock (lock);
137 if (result)
138 nis_freeresult (result);
139 result = NULL;
140 if (names)
142 nis_freenames (names);
143 names = NULL;
146 __libc_lock_unlock (lock);
148 return NSS_STATUS_SUCCESS;
151 enum nss_status
152 _nss_nisplus_endaliasent (void)
154 __libc_lock_lock (lock);
156 if (result)
157 nis_freeresult (result);
158 result = NULL;
159 if (names)
161 nis_freenames (names);
162 names = NULL;
165 __libc_lock_unlock (lock);
167 return NSS_STATUS_SUCCESS;
170 static enum nss_status
171 internal_nisplus_getaliasent_r (struct aliasent *alias,
172 char *buffer, size_t buflen)
174 int parse_res;
176 /* Get the next entry until we found a correct one. */
179 if (result == NULL)
181 names = nis_getnames("mail_aliases.org_dir");
182 if (names == NULL || names[0] == NULL)
183 return NSS_STATUS_UNAVAIL;
185 result = nis_first_entry(names[0]);
186 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
187 return niserr2nss (result->status);
189 else
191 nis_result *res2;
193 res2 = nis_next_entry(names[0], &result->cookie);
194 nis_freeresult (result);
195 result = res2;
196 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
197 return niserr2nss (result->status);
200 parse_res = _nss_nisplus_parse_aliasent (result, alias, buffer, buflen);
201 } while (!parse_res);
203 return NSS_STATUS_SUCCESS;
206 enum nss_status
207 _nss_nisplus_getaliasent_r (struct aliasent *result, char *buffer,
208 size_t buflen)
210 int status;
212 __libc_lock_lock (lock);
214 status = internal_nisplus_getaliasent_r (result, buffer, buflen);
216 __libc_lock_unlock (lock);
218 return status;
221 enum nss_status
222 _nss_nisplus_getaliasbyname_r (const char *name, struct aliasent *alias,
223 char *buffer, size_t buflen)
225 int parse_res;
227 if (name == NULL || strlen(name) > 8)
228 return NSS_STATUS_NOTFOUND;
229 else
231 nis_result *result;
232 char buf[strlen (name) + 30];
234 sprintf(buf, "[name=%s],mail_aliases.org_dir", name);
236 result = nis_list(buf, EXPAND_NAME, NULL, NULL);
238 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
239 return niserr2nss (result->status);
241 parse_res = _nss_nisplus_parse_aliasent (result, alias, buffer, buflen);
243 if (parse_res)
244 return NSS_STATUS_SUCCESS;
246 if (!parse_res && errno == ERANGE)
247 return NSS_STATUS_TRYAGAIN;
248 else
249 return NSS_STATUS_NOTFOUND;