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