]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (c) 1997-2014 Free Software Foundation, Inc. |
e61abf83 UD |
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 | |
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. | |
e61abf83 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. |
e61abf83 | 14 | |
41bdb6e2 | 15 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
16 | License along with the GNU C Library; if not, see |
17 | <http://www.gnu.org/licenses/>. */ | |
e61abf83 UD |
18 | |
19 | #include <errno.h> | |
20 | #include <string.h> | |
21 | #include <rpcsvc/nis.h> | |
e61abf83 UD |
22 | |
23 | nis_name | |
c131718c | 24 | nis_leaf_of (const_nis_name name) |
e61abf83 UD |
25 | { |
26 | static char result[NIS_MAXNAMELEN + 1]; | |
27 | ||
28 | return nis_leaf_of_r (name, result, NIS_MAXNAMELEN); | |
29 | } | |
30 | ||
31 | nis_name | |
c131718c | 32 | nis_leaf_of_r (const_nis_name name, char *buffer, size_t buflen) |
e61abf83 UD |
33 | { |
34 | size_t i = 0; | |
35 | ||
36 | buffer[0] = '\0'; | |
37 | ||
38 | while (name[i] != '.' && name[i] != '\0') | |
39 | i++; | |
40 | ||
b4f6f4be | 41 | if (__builtin_expect (i >= buflen, 0)) |
e61abf83 | 42 | { |
7440c23e | 43 | __set_errno (ERANGE); |
e61abf83 UD |
44 | return NULL; |
45 | } | |
46 | ||
1ce359b0 | 47 | *((char *) __mempcpy (buffer, name, i)) = '\0'; |
e61abf83 UD |
48 | |
49 | return buffer; | |
50 | } | |
7440c23e | 51 | libnsl_hidden_def (nis_leaf_of_r) |
e61abf83 UD |
52 | |
53 | nis_name | |
c131718c | 54 | nis_name_of (const_nis_name name) |
e61abf83 UD |
55 | { |
56 | static char result[NIS_MAXNAMELEN + 1]; | |
57 | ||
58 | return nis_name_of_r (name, result, NIS_MAXNAMELEN); | |
59 | } | |
60 | ||
61 | nis_name | |
c131718c | 62 | nis_name_of_r (const_nis_name name, char *buffer, size_t buflen) |
e61abf83 UD |
63 | { |
64 | char *local_domain; | |
65 | int diff; | |
66 | ||
67 | local_domain = nis_local_directory (); | |
68 | ||
69 | diff = strlen (name) - strlen (local_domain); | |
70 | if (diff <= 0) | |
71 | return NULL; | |
72 | ||
73 | if (strcmp (&name[diff], local_domain) != 0) | |
74 | return NULL; | |
75 | ||
76 | if ((size_t) diff >= buflen) | |
77 | { | |
7440c23e | 78 | __set_errno (ERANGE); |
e61abf83 UD |
79 | return NULL; |
80 | } | |
32abdb71 UD |
81 | |
82 | *((char *) __mempcpy (buffer, name, diff - 1)) = '\0'; | |
e61abf83 UD |
83 | |
84 | if (diff - 1 == 0) | |
85 | return NULL; | |
86 | ||
87 | return buffer; | |
88 | } | |
7440c23e | 89 | libnsl_hidden_def (nis_name_of_r) |
e61abf83 | 90 | |
1ce359b0 | 91 | static int __always_inline |
c131718c | 92 | count_dots (const_nis_name str) |
e61abf83 UD |
93 | { |
94 | int count = 0; | |
e61abf83 | 95 | |
1ce359b0 | 96 | for (size_t i = 0; str[i] != '\0'; ++i) |
e61abf83 UD |
97 | if (str[i] == '.') |
98 | ++count; | |
99 | ||
100 | return count; | |
101 | } | |
102 | ||
32abdb71 UD |
103 | /* If we run out of memory, we don't give already allocated memory |
104 | free. The overhead for bringing getnames back in a safe state to | |
105 | free it is to big. */ | |
e61abf83 | 106 | nis_name * |
c131718c | 107 | nis_getnames (const_nis_name name) |
e61abf83 | 108 | { |
02f366b3 UD |
109 | const char *local_domain = nis_local_directory (); |
110 | size_t local_domain_len = strlen (local_domain); | |
111 | size_t name_len = strlen (name); | |
9be31a51 | 112 | char *path; |
9be31a51 | 113 | int pos = 0; |
9d9febc7 | 114 | char *saveptr = NULL; |
02f366b3 UD |
115 | int have_point; |
116 | const char *cp; | |
117 | const char *cp2; | |
e61abf83 | 118 | |
02f366b3 UD |
119 | int count = 2; |
120 | nis_name *getnames = malloc ((count + 1) * sizeof (char *)); | |
54eb84d0 | 121 | if (__builtin_expect (getnames == NULL, 0)) |
e61abf83 UD |
122 | return NULL; |
123 | ||
124 | /* Do we have a fully qualified NIS+ name ? If yes, give it back */ | |
02f366b3 | 125 | if (name[name_len - 1] == '.') |
e61abf83 UD |
126 | { |
127 | if ((getnames[0] = strdup (name)) == NULL) | |
9be31a51 UD |
128 | { |
129 | free_null: | |
130 | while (pos-- > 0) | |
131 | free (getnames[pos]); | |
132 | free (getnames); | |
133 | return NULL; | |
134 | } | |
32abdb71 | 135 | |
e61abf83 UD |
136 | getnames[1] = NULL; |
137 | ||
138 | return getnames; | |
139 | } | |
140 | ||
02f366b3 UD |
141 | /* If the passed NAME is shared a suffix (the latter of course with |
142 | a final dot) with each other we pass back NAME with a final | |
143 | dot. */ | |
144 | if (local_domain_len > 2) | |
145 | { | |
146 | have_point = 0; | |
147 | cp = &local_domain[local_domain_len - 2]; | |
148 | cp2 = &name[name_len - 1]; | |
149 | ||
150 | while (*cp == *cp2) | |
151 | { | |
152 | if (*cp == '.') | |
153 | have_point = 1; | |
154 | --cp; | |
155 | --cp2; | |
156 | if (cp < local_domain) | |
157 | { | |
158 | have_point = cp2 < name || *cp2 == '.'; | |
159 | break; | |
160 | } | |
161 | if (cp2 < name) | |
162 | { | |
163 | have_point = *cp == '.'; | |
164 | break; | |
165 | } | |
166 | } | |
167 | ||
168 | if (have_point) | |
169 | { | |
170 | getnames[0] = malloc (name_len + 2); | |
171 | if (getnames[0] == NULL) | |
172 | goto free_null; | |
173 | ||
174 | strcpy (stpcpy (getnames[0], name), "."); | |
175 | ++pos; | |
176 | } | |
177 | } | |
178 | ||
e61abf83 | 179 | /* Get the search path, where we have to search "name" */ |
f0d5e1f6 | 180 | path = getenv ("NIS_PATH"); |
e61abf83 UD |
181 | if (path == NULL) |
182 | path = strdupa ("$"); | |
183 | else | |
184 | path = strdupa (path); | |
185 | ||
02f366b3 | 186 | have_point = strchr (name, '.') != NULL; |
2d7da676 | 187 | |
32abdb71 | 188 | cp = __strtok_r (path, ":", &saveptr); |
e61abf83 UD |
189 | while (cp) |
190 | { | |
191 | if (strcmp (cp, "$") == 0) | |
192 | { | |
02f366b3 | 193 | const char *cptr = local_domain; |
e61abf83 UD |
194 | char *tmp; |
195 | ||
02f366b3 | 196 | while (*cptr != '\0' && count_dots (cptr) >= 2) |
e61abf83 UD |
197 | { |
198 | if (pos >= count) | |
199 | { | |
200 | count += 5; | |
9be31a51 UD |
201 | nis_name *newp = realloc (getnames, |
202 | (count + 1) * sizeof (char *)); | |
203 | if (__builtin_expect (newp == NULL, 0)) | |
204 | goto free_null; | |
205 | getnames = newp; | |
e61abf83 | 206 | } |
02f366b3 | 207 | tmp = malloc (strlen (cptr) + local_domain_len + name_len + 2); |
54eb84d0 | 208 | if (__builtin_expect (tmp == NULL, 0)) |
9be31a51 | 209 | goto free_null; |
e61abf83 UD |
210 | |
211 | getnames[pos] = tmp; | |
212 | tmp = stpcpy (tmp, name); | |
213 | *tmp++ = '.'; | |
2d7da676 UD |
214 | if (cptr[1] != '\0') |
215 | stpcpy (tmp, cptr); | |
216 | else | |
217 | ++cptr; | |
e61abf83 UD |
218 | |
219 | ++pos; | |
220 | ||
2d7da676 UD |
221 | while (*cptr != '.' && *cptr != '\0') |
222 | ++cptr; | |
223 | if (cptr[0] != '\0' && cptr[1] != '\0') | |
224 | /* If we have only ".", don't remove the "." */ | |
e61abf83 | 225 | ++cptr; |
e61abf83 UD |
226 | } |
227 | } | |
228 | else | |
229 | { | |
230 | char *tmp; | |
32abdb71 | 231 | size_t cplen = strlen (cp); |
e61abf83 | 232 | |
32abdb71 | 233 | if (cp[cplen - 1] == '$') |
e61abf83 | 234 | { |
2d7da676 UD |
235 | char *p; |
236 | ||
02f366b3 | 237 | tmp = malloc (cplen + local_domain_len + name_len + 2); |
54eb84d0 | 238 | if (__builtin_expect (tmp == NULL, 0)) |
9be31a51 | 239 | goto free_null; |
e61abf83 | 240 | |
32abdb71 | 241 | p = __stpcpy (tmp, name); |
2d7da676 | 242 | *p++ = '.'; |
32abdb71 | 243 | p = __mempcpy (p, cp, cplen); |
2d7da676 UD |
244 | --p; |
245 | if (p[-1] != '.') | |
246 | *p++ = '.'; | |
32abdb71 | 247 | __stpcpy (p, local_domain); |
e61abf83 UD |
248 | } |
249 | else | |
250 | { | |
2d7da676 UD |
251 | char *p; |
252 | ||
2a6ee549 | 253 | tmp = malloc (cplen + name_len + 3); |
54eb84d0 | 254 | if (__builtin_expect (tmp == NULL, 0)) |
9be31a51 | 255 | goto free_null; |
e61abf83 | 256 | |
2a6ee549 | 257 | p = __mempcpy (tmp, name, name_len); |
2d7da676 | 258 | *p++ = '.'; |
2a6ee549 UD |
259 | p = __mempcpy (p, cp, cplen); |
260 | if (p[-1] != '.') | |
261 | *p++ = '.'; | |
262 | *p = '\0'; | |
e61abf83 UD |
263 | } |
264 | ||
2d7da676 | 265 | if (pos >= count) |
e61abf83 UD |
266 | { |
267 | count += 5; | |
9be31a51 UD |
268 | nis_name *newp = realloc (getnames, |
269 | (count + 1) * sizeof (char *)); | |
270 | if (__builtin_expect (newp == NULL, 0)) | |
271 | goto free_null; | |
272 | getnames = newp; | |
e61abf83 UD |
273 | } |
274 | getnames[pos] = tmp; | |
275 | ++pos; | |
276 | } | |
32abdb71 | 277 | cp = __strtok_r (NULL, ":", &saveptr); |
e61abf83 UD |
278 | } |
279 | ||
ef4ae86a UD |
280 | if (pos == 0 |
281 | && __asprintf (&getnames[pos++], "%s%s%s%s", | |
282 | name, name[name_len - 1] == '.' ? "" : ".", | |
283 | local_domain, | |
284 | local_domain[local_domain_len - 1] == '.' ? "" : ".") < 0) | |
285 | goto free_null; | |
286 | ||
e61abf83 UD |
287 | getnames[pos] = NULL; |
288 | ||
289 | return getnames; | |
290 | } | |
7440c23e | 291 | libnsl_hidden_def (nis_getnames) |
e61abf83 UD |
292 | |
293 | void | |
294 | nis_freenames (nis_name *names) | |
295 | { | |
296 | int i = 0; | |
297 | ||
298 | while (names[i] != NULL) | |
299 | { | |
300 | free (names[i]); | |
301 | ++i; | |
302 | } | |
303 | ||
304 | free (names); | |
305 | } | |
7440c23e | 306 | libnsl_hidden_def (nis_freenames) |
e61abf83 UD |
307 | |
308 | name_pos | |
c131718c | 309 | nis_dir_cmp (const_nis_name n1, const_nis_name n2) |
e61abf83 UD |
310 | { |
311 | int len1, len2; | |
312 | ||
313 | len1 = strlen (n1); | |
314 | len2 = strlen (n2); | |
315 | ||
316 | if (len1 == len2) | |
317 | { | |
318 | if (strcmp (n1, n2) == 0) | |
319 | return SAME_NAME; | |
320 | else | |
321 | return NOT_SEQUENTIAL; | |
322 | } | |
323 | ||
324 | if (len1 < len2) | |
325 | { | |
326 | if (n2[len2 - len1 - 1] != '.') | |
327 | return NOT_SEQUENTIAL; | |
328 | else if (strcmp (&n2[len2 - len1], n1) == 0) | |
329 | return HIGHER_NAME; | |
330 | else | |
331 | return NOT_SEQUENTIAL; | |
332 | } | |
333 | else | |
334 | { | |
335 | if (n1[len1 - len2 - 1] != '.') | |
336 | return NOT_SEQUENTIAL; | |
337 | else if (strcmp (&n1[len1 - len2], n2) == 0) | |
338 | return LOWER_NAME; | |
339 | else | |
340 | return NOT_SEQUENTIAL; | |
341 | ||
342 | } | |
343 | } | |
7440c23e | 344 | libnsl_hidden_def (nis_dir_cmp) |
e61abf83 UD |
345 | |
346 | void | |
347 | nis_destroy_object (nis_object *obj) | |
348 | { | |
349 | nis_free_object (obj); | |
350 | } | |
7440c23e | 351 | libnsl_hidden_def (nis_destroy_object) |