nis: Fix leak on realloc failure in nis_getnames [BZ #28150]
[glibc.git] / nis / nis_subr.c
blob6784fc353f8dc9acd2f36996ea7c3e8a9128e4ca
1 /* Copyright (c) 1997-2021 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Thorsten Kukuk <kukuk@vt.uni-paderborn.de>, 1997.
5 The GNU C Library is free software; you can redistribute it and/or
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.
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
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <string.h>
21 #include <rpcsvc/nis.h>
22 #include <shlib-compat.h>
24 nis_name
25 nis_leaf_of (const_nis_name name)
27 static char result[NIS_MAXNAMELEN + 1];
29 return nis_leaf_of_r (name, result, NIS_MAXNAMELEN);
31 libnsl_hidden_nolink_def (nis_leaf_of, GLIBC_2_1)
33 nis_name
34 nis_leaf_of_r (const_nis_name name, char *buffer, size_t buflen)
36 size_t i = 0;
38 buffer[0] = '\0';
40 while (name[i] != '.' && name[i] != '\0')
41 i++;
43 if (__glibc_unlikely (i >= buflen))
45 __set_errno (ERANGE);
46 return NULL;
49 *((char *) __mempcpy (buffer, name, i)) = '\0';
51 return buffer;
53 libnsl_hidden_nolink_def (nis_leaf_of_r, GLIBC_2_1)
55 nis_name
56 nis_name_of (const_nis_name name)
58 static char result[NIS_MAXNAMELEN + 1];
60 return nis_name_of_r (name, result, NIS_MAXNAMELEN);
62 libnsl_hidden_nolink_def (nis_name_of, GLIBC_2_1)
64 nis_name
65 nis_name_of_r (const_nis_name name, char *buffer, size_t buflen)
67 char *local_domain;
68 int diff;
70 local_domain = nis_local_directory ();
72 diff = strlen (name) - strlen (local_domain);
73 if (diff <= 0)
74 return NULL;
76 if (strcmp (&name[diff], local_domain) != 0)
77 return NULL;
79 if ((size_t) diff >= buflen)
81 __set_errno (ERANGE);
82 return NULL;
85 *((char *) __mempcpy (buffer, name, diff - 1)) = '\0';
87 if (diff - 1 == 0)
88 return NULL;
90 return buffer;
92 libnsl_hidden_nolink_def (nis_name_of_r, GLIBC_2_1)
94 static __always_inline int
95 count_dots (const_nis_name str)
97 int count = 0;
99 for (size_t i = 0; str[i] != '\0'; ++i)
100 if (str[i] == '.')
101 ++count;
103 return count;
106 nis_name *
107 nis_getnames (const_nis_name name)
109 const char *local_domain = nis_local_directory ();
110 size_t local_domain_len = strlen (local_domain);
111 size_t name_len = strlen (name);
112 char *path;
113 int pos = 0;
114 char *saveptr = NULL;
115 int have_point;
116 const char *cp;
117 const char *cp2;
119 int count = 2;
120 nis_name *getnames = malloc ((count + 1) * sizeof (char *));
121 if (__glibc_unlikely (getnames == NULL))
122 return NULL;
124 /* Do we have a fully qualified NIS+ name ? If yes, give it back */
125 if (name[name_len - 1] == '.')
127 if ((getnames[0] = strdup (name)) == NULL)
129 free_null:
130 while (pos-- > 0)
131 free (getnames[pos]);
132 free (getnames);
133 return NULL;
136 getnames[1] = NULL;
138 return getnames;
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)
146 have_point = 0;
147 cp = &local_domain[local_domain_len - 2];
148 cp2 = &name[name_len - 1];
150 while (*cp == *cp2)
152 if (*cp == '.')
153 have_point = 1;
154 --cp;
155 --cp2;
156 if (cp < local_domain)
158 have_point = cp2 < name || *cp2 == '.';
159 break;
161 if (cp2 < name)
163 have_point = *cp == '.';
164 break;
168 if (have_point)
170 getnames[0] = malloc (name_len + 2);
171 if (getnames[0] == NULL)
172 goto free_null;
174 strcpy (stpcpy (getnames[0], name), ".");
175 ++pos;
179 /* Get the search path, where we have to search "name" */
180 path = getenv ("NIS_PATH");
181 if (path == NULL)
182 path = strdupa ("$");
183 else
184 path = strdupa (path);
186 have_point = strchr (name, '.') != NULL;
188 cp = __strtok_r (path, ":", &saveptr);
189 while (cp)
191 if (strcmp (cp, "$") == 0)
193 const char *cptr = local_domain;
194 char *tmp;
196 while (*cptr != '\0' && count_dots (cptr) >= 2)
198 if (pos >= count)
200 count += 5;
201 nis_name *newp = realloc (getnames,
202 (count + 1) * sizeof (char *));
203 if (__glibc_unlikely (newp == NULL))
204 goto free_null;
205 getnames = newp;
207 tmp = malloc (strlen (cptr) + local_domain_len + name_len + 2);
208 if (__glibc_unlikely (tmp == NULL))
209 goto free_null;
211 getnames[pos] = tmp;
212 tmp = stpcpy (tmp, name);
213 *tmp++ = '.';
214 if (cptr[1] != '\0')
215 stpcpy (tmp, cptr);
216 else
217 ++cptr;
219 ++pos;
221 while (*cptr != '.' && *cptr != '\0')
222 ++cptr;
223 if (cptr[0] != '\0' && cptr[1] != '\0')
224 /* If we have only ".", don't remove the "." */
225 ++cptr;
228 else
230 char *tmp;
231 size_t cplen = strlen (cp);
233 if (cp[cplen - 1] == '$')
235 char *p;
237 tmp = malloc (cplen + local_domain_len + name_len + 2);
238 if (__glibc_unlikely (tmp == NULL))
239 goto free_null;
241 p = __stpcpy (tmp, name);
242 *p++ = '.';
243 p = __mempcpy (p, cp, cplen);
244 --p;
245 if (p[-1] != '.')
246 *p++ = '.';
247 __stpcpy (p, local_domain);
249 else
251 char *p;
253 tmp = malloc (cplen + name_len + 3);
254 if (__glibc_unlikely (tmp == NULL))
255 goto free_null;
257 p = __mempcpy (tmp, name, name_len);
258 *p++ = '.';
259 p = __mempcpy (p, cp, cplen);
260 if (p[-1] != '.')
261 *p++ = '.';
262 *p = '\0';
265 if (pos >= count)
267 count += 5;
268 nis_name *newp = realloc (getnames,
269 (count + 1) * sizeof (char *));
270 if (__glibc_unlikely (newp == NULL))
272 free (tmp);
273 goto free_null;
275 getnames = newp;
277 getnames[pos] = tmp;
278 ++pos;
280 cp = __strtok_r (NULL, ":", &saveptr);
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;
290 getnames[pos] = NULL;
292 return getnames;
294 libnsl_hidden_nolink_def (nis_getnames, GLIBC_2_1)
296 void
297 nis_freenames (nis_name *names)
299 int i = 0;
301 while (names[i] != NULL)
303 free (names[i]);
304 ++i;
307 free (names);
309 libnsl_hidden_nolink_def (nis_freenames, GLIBC_2_1)
311 name_pos
312 nis_dir_cmp (const_nis_name n1, const_nis_name n2)
314 int len1, len2;
316 len1 = strlen (n1);
317 len2 = strlen (n2);
319 if (len1 == len2)
321 if (strcmp (n1, n2) == 0)
322 return SAME_NAME;
323 else
324 return NOT_SEQUENTIAL;
327 if (len1 < len2)
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;
336 else
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;
347 libnsl_hidden_nolink_def (nis_dir_cmp, GLIBC_2_1)
349 void
350 nis_destroy_object (nis_object *obj)
352 nis_free_object (obj);
354 libnsl_hidden_nolink_def (nis_destroy_object, GLIBC_2_1)