1 /* Copyright (c) 1997-2019 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/>. */
21 #include <rpcsvc/nis.h>
22 #include <shlib-compat.h>
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
)
34 nis_leaf_of_r (const_nis_name name
, char *buffer
, size_t buflen
)
40 while (name
[i
] != '.' && name
[i
] != '\0')
43 if (__glibc_unlikely (i
>= buflen
))
49 *((char *) __mempcpy (buffer
, name
, i
)) = '\0';
53 libnsl_hidden_nolink_def (nis_leaf_of_r
, GLIBC_2_1
)
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
)
65 nis_name_of_r (const_nis_name name
, char *buffer
, size_t buflen
)
70 local_domain
= nis_local_directory ();
72 diff
= strlen (name
) - strlen (local_domain
);
76 if (strcmp (&name
[diff
], local_domain
) != 0)
79 if ((size_t) diff
>= buflen
)
85 *((char *) __mempcpy (buffer
, name
, diff
- 1)) = '\0';
92 libnsl_hidden_nolink_def (nis_name_of_r
, GLIBC_2_1
)
94 static __always_inline
int
95 count_dots (const_nis_name str
)
99 for (size_t i
= 0; str
[i
] != '\0'; ++i
)
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. */
110 nis_getnames (const_nis_name name
)
112 const char *local_domain
= nis_local_directory ();
113 size_t local_domain_len
= strlen (local_domain
);
114 size_t name_len
= strlen (name
);
117 char *saveptr
= NULL
;
123 nis_name
*getnames
= malloc ((count
+ 1) * sizeof (char *));
124 if (__glibc_unlikely (getnames
== NULL
))
127 /* Do we have a fully qualified NIS+ name ? If yes, give it back */
128 if (name
[name_len
- 1] == '.')
130 if ((getnames
[0] = strdup (name
)) == NULL
)
134 free (getnames
[pos
]);
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
147 if (local_domain_len
> 2)
150 cp
= &local_domain
[local_domain_len
- 2];
151 cp2
= &name
[name_len
- 1];
159 if (cp
< local_domain
)
161 have_point
= cp2
< name
|| *cp2
== '.';
166 have_point
= *cp
== '.';
173 getnames
[0] = malloc (name_len
+ 2);
174 if (getnames
[0] == NULL
)
177 strcpy (stpcpy (getnames
[0], name
), ".");
182 /* Get the search path, where we have to search "name" */
183 path
= getenv ("NIS_PATH");
185 path
= strdupa ("$");
187 path
= strdupa (path
);
189 have_point
= strchr (name
, '.') != NULL
;
191 cp
= __strtok_r (path
, ":", &saveptr
);
194 if (strcmp (cp
, "$") == 0)
196 const char *cptr
= local_domain
;
199 while (*cptr
!= '\0' && count_dots (cptr
) >= 2)
204 nis_name
*newp
= realloc (getnames
,
205 (count
+ 1) * sizeof (char *));
206 if (__glibc_unlikely (newp
== NULL
))
210 tmp
= malloc (strlen (cptr
) + local_domain_len
+ name_len
+ 2);
211 if (__glibc_unlikely (tmp
== NULL
))
215 tmp
= stpcpy (tmp
, name
);
224 while (*cptr
!= '.' && *cptr
!= '\0')
226 if (cptr
[0] != '\0' && cptr
[1] != '\0')
227 /* If we have only ".", don't remove the "." */
234 size_t cplen
= strlen (cp
);
236 if (cp
[cplen
- 1] == '$')
240 tmp
= malloc (cplen
+ local_domain_len
+ name_len
+ 2);
241 if (__glibc_unlikely (tmp
== NULL
))
244 p
= __stpcpy (tmp
, name
);
246 p
= __mempcpy (p
, cp
, cplen
);
250 __stpcpy (p
, local_domain
);
256 tmp
= malloc (cplen
+ name_len
+ 3);
257 if (__glibc_unlikely (tmp
== NULL
))
260 p
= __mempcpy (tmp
, name
, name_len
);
262 p
= __mempcpy (p
, cp
, cplen
);
271 nis_name
*newp
= realloc (getnames
,
272 (count
+ 1) * sizeof (char *));
273 if (__glibc_unlikely (newp
== NULL
))
280 cp
= __strtok_r (NULL
, ":", &saveptr
);
284 && __asprintf (&getnames
[pos
++], "%s%s%s%s",
285 name
, name
[name_len
- 1] == '.' ? "" : ".",
287 local_domain
[local_domain_len
- 1] == '.' ? "" : ".") < 0)
290 getnames
[pos
] = NULL
;
294 libnsl_hidden_nolink_def (nis_getnames
, GLIBC_2_1
)
297 nis_freenames (nis_name
*names
)
301 while (names
[i
] != NULL
)
309 libnsl_hidden_nolink_def (nis_freenames
, GLIBC_2_1
)
312 nis_dir_cmp (const_nis_name n1
, const_nis_name n2
)
321 if (strcmp (n1
, n2
) == 0)
324 return NOT_SEQUENTIAL
;
329 if (n2
[len2
- len1
- 1] != '.')
330 return NOT_SEQUENTIAL
;
331 else if (strcmp (&n2
[len2
- len1
], n1
) == 0)
334 return NOT_SEQUENTIAL
;
338 if (n1
[len1
- len2
- 1] != '.')
339 return NOT_SEQUENTIAL
;
340 else if (strcmp (&n1
[len1
- len2
], n2
) == 0)
343 return NOT_SEQUENTIAL
;
347 libnsl_hidden_nolink_def (nis_dir_cmp
, GLIBC_2_1
)
350 nis_destroy_object (nis_object
*obj
)
352 nis_free_object (obj
);
354 libnsl_hidden_nolink_def (nis_destroy_object
, GLIBC_2_1
)