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