]> git.ipfire.org Git - thirdparty/glibc.git/blob - nis/nss_nisplus/nisplus-alias.c
Update.
[thirdparty/glibc.git] / nis / nss_nisplus / nisplus-alias.c
1 /* Copyright (C) 1997, 1998, 2001 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.
4
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.
9
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.
14
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. */
19
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>
27
28 #include "nss-nisplus.h"
29
30 __libc_lock_define_initialized (static, lock)
31
32 static nis_result *result;
33 static u_long next_entry;
34 static nis_name tablename_val;
35 static u_long tablename_len;
36
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)
39
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)
42
43 static enum nss_status
44 _nss_create_tablename (int *errnop)
45 {
46 if (tablename_val == NULL)
47 {
48 char buf [40 + strlen (nis_local_directory ())];
49 char *p;
50
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 {
56 *errnop = errno;
57 return NSS_STATUS_TRYAGAIN;
58 }
59 tablename_len = strlen (tablename_val);
60 }
61 return NSS_STATUS_SUCCESS;
62 }
63
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)
68 {
69 if (result == NULL)
70 return 0;
71
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
79 {
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;
86
87 if (NISENTRYLEN (entry, 1, result) >= buflen)
88 {
89 /* The line is too long for our buffer. */
90 no_more_room:
91 *errnop = ERANGE;
92 return -1;
93 }
94 else
95 {
96 cp = __stpncpy (buffer, NISENTRYVAL (entry, 1, result),
97 NISENTRYLEN (entry, 1, result));
98 *cp = '\0';
99 }
100
101 if (NISENTRYLEN(entry, 0, result) >= room_left)
102 goto no_more_room;
103
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;
112
113 /* Terminate the line for any case. */
114 cp = strpbrk (alias->alias_name, "#\n");
115 if (cp != NULL)
116 *cp = '\0';
117
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;
124
125 line = buffer;
126
127 while (*line != '\0')
128 {
129 /* Skip leading blanks. */
130 while (isspace (*line))
131 ++line;
132
133 if (*line == '\0')
134 break;
135
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;
140
141 while (*line != '\0' && *line != ',')
142 ++line;
143
144 if (line != alias->alias_members[alias->alias_members_len])
145 {
146 *line++ = '\0';
147 alias->alias_members_len++;
148 }
149 }
150
151 return alias->alias_members_len == 0 ? 0 : 1;
152 }
153 }
154
155 static enum nss_status
156 internal_setaliasent (void)
157 {
158 enum nss_status status;
159 int err;
160
161 if (result)
162 nis_freeresult (result);
163 result = NULL;
164
165 if (_nss_create_tablename (&err) != NSS_STATUS_SUCCESS)
166 return NSS_STATUS_UNAVAIL;
167
168 next_entry = 0;
169 result = nis_list (tablename_val, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
170 status = niserr2nss (result->status);
171 if (status != NSS_STATUS_SUCCESS)
172 {
173 nis_freeresult (result);
174 result = NULL;
175 }
176 return status;
177 }
178
179 enum nss_status
180 _nss_nisplus_setaliasent (void)
181 {
182 enum nss_status status;
183
184 __libc_lock_lock (lock);
185
186 status = internal_setaliasent ();
187
188 __libc_lock_unlock (lock);
189
190 return status;
191 }
192
193 enum nss_status
194 _nss_nisplus_endaliasent (void)
195 {
196 __libc_lock_lock (lock);
197
198 if (result)
199 nis_freeresult (result);
200 result = NULL;
201 next_entry = 0;
202
203 __libc_lock_unlock (lock);
204
205 return NSS_STATUS_SUCCESS;
206 }
207
208 static enum nss_status
209 internal_nisplus_getaliasent_r (struct aliasent *alias,
210 char *buffer, size_t buflen, int *errnop)
211 {
212 int parse_res;
213
214 if (result == NULL)
215 {
216 enum nss_status status;
217
218 status = internal_setaliasent ();
219 if (result == NULL || status != NSS_STATUS_SUCCESS)
220 return status;
221 }
222
223 /* Get the next entry until we found a correct one. */
224 do
225 {
226 if (next_entry >= result->objects.objects_len)
227 return NSS_STATUS_NOTFOUND;
228
229 parse_res = _nss_nisplus_parse_aliasent (result, next_entry, alias,
230 buffer, buflen, errnop);
231 if (parse_res == -1)
232 return NSS_STATUS_TRYAGAIN;
233
234 ++next_entry;
235 } while (!parse_res);
236
237 return NSS_STATUS_SUCCESS;
238 }
239
240 enum nss_status
241 _nss_nisplus_getaliasent_r (struct aliasent *result, char *buffer,
242 size_t buflen, int *errnop)
243 {
244 int status;
245
246 __libc_lock_lock (lock);
247
248 status = internal_nisplus_getaliasent_r (result, buffer, buflen, errnop);
249
250 __libc_lock_unlock (lock);
251
252 return status;
253 }
254
255 enum nss_status
256 _nss_nisplus_getaliasbyname_r (const char *name, struct aliasent *alias,
257 char *buffer, size_t buflen, int *errnop)
258 {
259 int parse_res;
260
261 if (tablename_val == NULL)
262 {
263 enum nss_status status = _nss_create_tablename (errnop);
264 if (status != NSS_STATUS_SUCCESS)
265 return status;
266 }
267
268 if (name != NULL)
269 {
270 *errnop = EINVAL;
271 return NSS_STATUS_UNAVAIL;
272 }
273 else
274 {
275 nis_result *result;
276 char buf[strlen (name) + 30 + tablename_len];
277
278 sprintf (buf, "[name=%s],%s", name, tablename_val);
279
280 result = nis_list (buf, FOLLOW_PATH | FOLLOW_LINKS, NULL, NULL);
281
282 if (niserr2nss (result->status) != NSS_STATUS_SUCCESS)
283 return niserr2nss (result->status);
284
285 parse_res = _nss_nisplus_parse_aliasent (result, 0, alias,
286 buffer, buflen, errnop);
287 if (parse_res < 1)
288 {
289 if (parse_res == -1)
290 return NSS_STATUS_TRYAGAIN;
291 else
292 return NSS_STATUS_NOTFOUND;
293 }
294 return NSS_STATUS_SUCCESS;
295 }
296 }