]>
Commit | Line | Data |
---|---|---|
d614a753 | 1 | /* Copyright (C) 1996-2020 Free Software Foundation, Inc. |
6259ec0d UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1996. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
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. | |
6259ec0d UD |
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 | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
6259ec0d | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 16 | License along with the GNU C Library; if not, see |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
6259ec0d UD |
18 | |
19 | #include <nss.h> | |
20 | #include <ctype.h> | |
21 | #include <errno.h> | |
22 | #include <string.h> | |
23 | #include <aliases.h> | |
ec999b8e | 24 | #include <libc-lock.h> |
6259ec0d UD |
25 | #include <rpcsvc/yp.h> |
26 | #include <rpcsvc/ypclnt.h> | |
27 | ||
28 | #include "nss-nis.h" | |
29 | ||
30 | __libc_lock_define_initialized (static, lock) | |
31 | ||
32 | static bool_t new_start = 1; | |
fc9f33e3 UD |
33 | static char *oldkey; |
34 | static int oldkeylen; | |
6259ec0d UD |
35 | |
36 | static int | |
7e3be507 | 37 | _nss_nis_parse_aliasent (const char *key, char *alias, struct aliasent *result, |
d71b808a | 38 | char *buffer, size_t buflen, int *errnop) |
6259ec0d UD |
39 | { |
40 | char *first_unused = buffer + strlen (alias) + 1; | |
41 | size_t room_left = | |
42 | buflen - (buflen % __alignof__ (char *)) - strlen (alias) - 2; | |
43 | char *line; | |
44 | char *cp; | |
45 | ||
46 | result->alias_members_len = 0; | |
47 | *first_unused = '\0'; | |
48 | first_unused++; | |
49 | strcpy (first_unused, key); | |
50 | ||
51 | if (first_unused[room_left - 1] != '\0') | |
52 | { | |
53 | /* The line is too long for our buffer. */ | |
54 | no_more_room: | |
d71b808a | 55 | *errnop = ERANGE; |
6259ec0d UD |
56 | return -1; |
57 | } | |
58 | ||
59 | result->alias_name = first_unused; | |
60 | ||
61 | /* Terminate the line for any case. */ | |
62 | cp = strpbrk (alias, "#\n"); | |
63 | if (cp != NULL) | |
64 | *cp = '\0'; | |
65 | ||
66 | first_unused += strlen (result->alias_name) + 1; | |
67 | /* Adjust the pointer so it is aligned for | |
68 | storing pointers. */ | |
69 | first_unused += __alignof__ (char *) - 1; | |
70 | first_unused -= ((first_unused - (char *) 0) % __alignof__ (char *)); | |
71 | result->alias_members = (char **) first_unused; | |
72 | ||
73 | line = alias; | |
74 | ||
75 | while (*line != '\0') | |
76 | { | |
77 | /* Skip leading blanks. */ | |
78 | while (isspace (*line)) | |
79 | line++; | |
80 | ||
81 | if (*line == '\0') | |
82 | break; | |
83 | ||
84 | if (room_left < sizeof (char *)) | |
85 | goto no_more_room; | |
86 | room_left -= sizeof (char *); | |
87 | result->alias_members[result->alias_members_len] = line; | |
88 | ||
89 | while (*line != '\0' && *line != ',') | |
90 | line++; | |
91 | ||
92 | if (line != result->alias_members[result->alias_members_len]) | |
93 | { | |
94 | *line = '\0'; | |
95 | line++; | |
96 | result->alias_members_len++; | |
97 | } | |
98 | } | |
99 | return result->alias_members_len == 0 ? 0 : 1; | |
100 | } | |
101 | ||
102 | enum nss_status | |
103 | _nss_nis_setaliasent (void) | |
104 | { | |
105 | __libc_lock_lock (lock); | |
106 | ||
107 | new_start = 1; | |
108 | if (oldkey != NULL) | |
109 | { | |
110 | free (oldkey); | |
111 | oldkey = NULL; | |
112 | oldkeylen = 0; | |
113 | } | |
114 | ||
115 | __libc_lock_unlock (lock); | |
116 | ||
117 | return NSS_STATUS_SUCCESS; | |
118 | } | |
6675b191 UD |
119 | /* The 'endaliasent' function is identical. */ |
120 | strong_alias (_nss_nis_setaliasent, _nss_nis_endaliasent) | |
6259ec0d UD |
121 | |
122 | static enum nss_status | |
123 | internal_nis_getaliasent_r (struct aliasent *alias, char *buffer, | |
d71b808a | 124 | size_t buflen, int *errnop) |
6259ec0d UD |
125 | { |
126 | char *domain; | |
6259ec0d | 127 | |
a1ffb40e | 128 | if (__glibc_unlikely (yp_get_default_domain (&domain))) |
6259ec0d UD |
129 | return NSS_STATUS_UNAVAIL; |
130 | ||
131 | alias->alias_local = 0; | |
132 | ||
133 | /* Get the next entry until we found a correct one. */ | |
ab9a9ff8 | 134 | int parse_res; |
6259ec0d UD |
135 | do |
136 | { | |
ab9a9ff8 UD |
137 | char *result; |
138 | int len; | |
139 | char *outkey; | |
140 | int keylen; | |
141 | int yperr; | |
6259ec0d UD |
142 | |
143 | if (new_start) | |
f2962a71 | 144 | yperr = yp_first (domain, "mail.aliases", &outkey, &keylen, &result, |
ab9a9ff8 | 145 | &len); |
6259ec0d | 146 | else |
f2962a71 | 147 | yperr = yp_next (domain, "mail.aliases", oldkey, oldkeylen, &outkey, |
ab9a9ff8 UD |
148 | &keylen, &result, &len); |
149 | ||
a1ffb40e | 150 | if (__glibc_unlikely (yperr != YPERR_SUCCESS)) |
6259ec0d | 151 | { |
ab9a9ff8 UD |
152 | enum nss_status retval = yperr2nss (yperr); |
153 | ||
34816665 | 154 | if (retval == NSS_STATUS_TRYAGAIN) |
f2962a71 UD |
155 | *errnop = errno; |
156 | return retval; | |
157 | } | |
6259ec0d | 158 | |
a1ffb40e | 159 | if (__glibc_unlikely ((size_t) (len + 1) > buflen)) |
f2962a71 | 160 | { |
6259ec0d | 161 | free (result); |
f2962a71 UD |
162 | *errnop = ERANGE; |
163 | return NSS_STATUS_TRYAGAIN; | |
164 | } | |
ab9a9ff8 | 165 | char *p = strncpy (buffer, result, len); |
6259ec0d UD |
166 | buffer[len] = '\0'; |
167 | while (isspace (*p)) | |
f2962a71 | 168 | ++p; |
6259ec0d UD |
169 | free (result); |
170 | ||
ab9a9ff8 UD |
171 | parse_res = _nss_nis_parse_aliasent (outkey, p, alias, buffer, |
172 | buflen, errnop); | |
a1ffb40e | 173 | if (__glibc_unlikely (parse_res == -1)) |
6259ec0d | 174 | { |
60c96635 | 175 | free (outkey); |
d71b808a | 176 | *errnop = ERANGE; |
6259ec0d UD |
177 | return NSS_STATUS_TRYAGAIN; |
178 | } | |
179 | ||
180 | free (oldkey); | |
181 | oldkey = outkey; | |
182 | oldkeylen = keylen; | |
183 | new_start = 0; | |
184 | } | |
185 | while (!parse_res); | |
186 | ||
187 | return NSS_STATUS_SUCCESS; | |
188 | } | |
189 | ||
190 | enum nss_status | |
d71b808a UD |
191 | _nss_nis_getaliasent_r (struct aliasent *alias, char *buffer, size_t buflen, |
192 | int *errnop) | |
6259ec0d UD |
193 | { |
194 | enum nss_status status; | |
195 | ||
196 | __libc_lock_lock (lock); | |
197 | ||
d71b808a | 198 | status = internal_nis_getaliasent_r (alias, buffer, buflen, errnop); |
6259ec0d UD |
199 | |
200 | __libc_lock_unlock (lock); | |
201 | ||
202 | return status; | |
203 | } | |
204 | ||
205 | enum nss_status | |
206 | _nss_nis_getaliasbyname_r (const char *name, struct aliasent *alias, | |
d71b808a | 207 | char *buffer, size_t buflen, int *errnop) |
6259ec0d | 208 | { |
6259ec0d UD |
209 | if (name == NULL) |
210 | { | |
ac9f45cf | 211 | *errnop = EINVAL; |
6259ec0d UD |
212 | return NSS_STATUS_UNAVAIL; |
213 | } | |
214 | ||
0292b0dd | 215 | char *domain; |
a1ffb40e | 216 | if (__glibc_unlikely (yp_get_default_domain (&domain))) |
6259ec0d UD |
217 | return NSS_STATUS_UNAVAIL; |
218 | ||
f2962a71 UD |
219 | size_t namlen = strlen (name); |
220 | char *name2; | |
221 | int use_alloca = __libc_use_alloca (namlen + 1); | |
222 | if (use_alloca) | |
223 | name2 = __alloca (namlen + 1); | |
224 | else | |
225 | { | |
226 | name2 = malloc (namlen + 1); | |
227 | if (name2 == NULL) | |
228 | { | |
229 | *errnop = ENOMEM; | |
230 | return NSS_STATUS_TRYAGAIN; | |
231 | } | |
232 | } | |
233 | ||
b112c02f | 234 | /* Convert name to lowercase. */ |
0292b0dd | 235 | size_t i; |
b112c02f | 236 | for (i = 0; i < namlen; ++i) |
4caef86c | 237 | name2[i] = _tolower (name[i]); |
b112c02f UD |
238 | name2[i] = '\0'; |
239 | ||
0292b0dd UD |
240 | char *result; |
241 | int len; | |
ab9a9ff8 | 242 | int yperr = yp_match (domain, "mail.aliases", name2, namlen, &result, &len); |
b112c02f | 243 | |
f2962a71 UD |
244 | if (!use_alloca) |
245 | free (name2); | |
246 | ||
a1ffb40e | 247 | if (__glibc_unlikely (yperr != YPERR_SUCCESS)) |
6259ec0d | 248 | { |
ab9a9ff8 UD |
249 | enum nss_status retval = yperr2nss (yperr); |
250 | ||
6259ec0d | 251 | if (retval == NSS_STATUS_TRYAGAIN) |
d71b808a | 252 | *errnop = errno; |
6259ec0d UD |
253 | return retval; |
254 | } | |
255 | ||
a1ffb40e | 256 | if (__glibc_unlikely ((size_t) (len + 1) > buflen)) |
6259ec0d UD |
257 | { |
258 | free (result); | |
d71b808a | 259 | *errnop = ERANGE; |
6259ec0d UD |
260 | return NSS_STATUS_TRYAGAIN; |
261 | } | |
262 | ||
0292b0dd | 263 | char *p = strncpy (buffer, result, len); |
6259ec0d UD |
264 | buffer[len] = '\0'; |
265 | while (isspace (*p)) | |
266 | ++p; | |
267 | free (result); | |
268 | ||
269 | alias->alias_local = 0; | |
0292b0dd UD |
270 | int parse_res = _nss_nis_parse_aliasent (name, p, alias, buffer, buflen, |
271 | errnop); | |
a1ffb40e | 272 | if (__glibc_unlikely (parse_res < 1)) |
ac9f45cf UD |
273 | { |
274 | if (parse_res == -1) | |
275 | return NSS_STATUS_TRYAGAIN; | |
276 | else | |
34816665 | 277 | return NSS_STATUS_NOTFOUND; |
ac9f45cf | 278 | } |
d71b808a | 279 | |
ac9f45cf | 280 | return NSS_STATUS_SUCCESS; |
6259ec0d | 281 | } |