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