4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2015 Gary Mills
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * Portions of this source code were derived from Berkeley 4.3 BSD
32 * under license from the Regents of the University of California.
43 #include "../ldap_util.h"
46 * This constructs a file name from a passed domain name, a passed map name,
47 * and a globally known YP data base path prefix.
49 * Has to be in shim because it needs the N2L prefix
51 * RETURNS : TRUE = A name was successfully created
52 * FALSE = A name could not be created
56 ypmkfilename(domain
, map
, path
)
63 /* Do not allow any path as a domain name. */
64 if (strchr(domain
, '/') != NULL
)
67 length
= strlen(domain
) + strlen(map
) + ypdbpath_sz
+ 3;
69 length
+= strlen(NTOL_PREFIX
) + 1;
71 if ((MAXNAMLEN
+ 1) < length
) {
72 (void) fprintf(stderr
, "ypserv: Map name string too long.\n");
76 strcpy(path
, ypdbpath
);
81 /* If in N2L mode add N2L prefix */
83 strcat(path
, NTOL_PREFIX
);
90 * check whether a map is already in an array/list
92 * RETURNS: TRUE if yes
96 on_maplist(char *mapname
, char **list
) {
103 while (list
[i
] != NULL
) {
104 if (strcmp(mapname
, list
[i
++]) == 0) {
113 * add a map at the end of an array/list
115 * list_len: if -1, we do not know list length
117 * RETURNS: TRUE if map was added
121 add_in_maplist(char *mapname
, char ***list
, int *list_len
) {
131 if (list_tmp
== NULL
) {
134 /* find 1st free element */
135 while (list_tmp
[i
] != NULL
) {
137 * increment in loop so that
138 * list_tmp[i] == NULL
145 /* if we don't know list length, assume we reach its end */
146 if (*list_len
== -1) {
150 /* do we need to reallocate ? */
151 if (i
+1 >= *list_len
) {
152 list_tmp
= reallocarray(list_tmp
, *list_len
+ ARRAY_CHUNK
,
154 if (list_tmp
== NULL
) {
158 *list_len
+= ARRAY_CHUNK
;
162 (*list
)[i
] = strdup(mapname
);
163 if ((*list
)[i
] == NULL
) {
164 /* strdup() failed */
173 * This checks to see whether a domain name is present at the local node as a
174 * subdirectory of ypdbpath
176 * Was originally in cmd/ypcmd/shared/ancil.c as ypcheck_domain(domain).
177 * Now ypcheck_domain(domain) calls this function.
180 ypcheck_domain_yptol(char *domain
)
182 char path
[MAXNAMLEN
+ 1];
183 struct stat filestat
;
184 bool present
= FALSE
;
186 strcpy(path
, ypdbpath
);
188 if (strlcat(path
, domain
, MAXNAMLEN
+ 1) >= MAXNAMLEN
+ 1)
191 if (stat(path
, &filestat
) != -1) {
192 if (S_ISDIR(filestat
.st_mode
))
199 * This performs an existence check on the dbm data base files <name>.pag and
200 * <name>.dir. pname is a ptr to the filename. This should be an absolute
202 * Returns TRUE if the map exists and is accessible; else FALSE.
204 * Note: The file name should be a "base" form, without a file "extension" of
205 * .dir or .pag appended. See ypmkfilename for a function which will generate
206 * the name correctly. Errors in the stat call will be reported at this level,
207 * however, the non-existence of a file is not considered an error, and so will
210 * Was originally in cmd/ypcmd/shared/utils.c as ypcheck_map_existence().
211 * Now ypcheck_map_existence() calls this function.
214 ypcheck_map_existence_yptol(char *pname
)
216 char dbfile
[MAXNAMLEN
+ sizeof (TTL_POSTFIX
) + 1];
217 struct stat64 filestat
;
220 if (!pname
|| ((len
= (int)strlen(pname
)) == 0) ||
221 (len
+ sizeof (dbm_pag
) + sizeof (TTL_POSTFIX
)) >
228 /* Check for existance of .dir file */
229 (void) strcpy(dbfile
, pname
);
230 (void) strcat(dbfile
, dbm_dir
);
232 if (stat64(dbfile
, &filestat
) == -1) {
233 if (errno
!= ENOENT
) {
234 (void) fprintf(stderr
,
235 "ypserv: Stat error on map file %s.\n",
241 /* Check for existance of .pag file */
242 (void) strcpy(dbfile
, pname
);
243 (void) strcat(dbfile
, dbm_pag
);
245 if (stat64(dbfile
, &filestat
) == -1) {
246 if (errno
!= ENOENT
) {
247 (void) fprintf(stderr
,
248 "ypserv: Stat error on map file %s.\n",
255 /* Check for existance of TTL .dir file */
256 (void) strcpy(dbfile
, pname
);
257 (void) strcat(dbfile
, TTL_POSTFIX
);
258 (void) strcat(dbfile
, dbm_dir
);
260 if (stat64(dbfile
, &filestat
) == -1) {
261 if (errno
!= ENOENT
) {
262 (void) fprintf(stderr
,
263 "ypserv: Stat error on map file %s.\n",
269 /* Check for existance of TTL .pag file */
270 (void) strcpy(dbfile
, pname
);
271 (void) strcat(dbfile
, TTL_POSTFIX
);
272 (void) strcat(dbfile
, dbm_pag
);
274 if (stat64(dbfile
, &filestat
) == -1) {
275 if (errno
!= ENOENT
) {
276 (void) fprintf(stderr
,
277 "ypserv: Stat error on map file %s.\n",
288 * This adds maps in a domain to a given list,
289 * from maps in /var/yp/<domain>
290 * Inspired from yplist_maps() in cmd/ypcmd/ypserv_ancil.c
292 * domain is the relevant domain name
293 * map_list is the list of maps in an array of map names,
294 * which may or may not be empty
296 * RETURNS : TRUE = everything went fine
297 * FALSE = an error occured
300 add_map_domain_to_list(char *domain
, char ***map_list
)
302 char domdir
[MAXNAMLEN
+ 1];
303 char path
[MAXNAMLEN
+ 1];
304 int domdir_len
= sizeof (domdir
);
308 int dbm_pag_len
= sizeof (dbm_pag
);
311 int map_list_len
= -1;
313 if (map_list
== NULL
) {
317 /* no domain, not a problem */
318 if (domain
== NULL
) {
322 /* not a valid domain, not a problem */
323 if (!ypcheck_domain_yptol(domain
)) {
327 if (snprintf(domdir
, domdir_len
, "%s/%s", ypdbpath
, domain
)
332 if ((dirp
= opendir(domdir
)) == NULL
) {
336 for (dp
= readdir(dirp
); dp
!= NULL
;
337 dp
= readdir(dirp
)) {
339 * If it's possible that the file name is one of the two files
340 * implementing a map, remove the extension (dbm_pag or dbm_dir)
342 name_len
= (int)strlen(dp
->d_name
);
344 if (name_len
< dbm_pag_len
- 1) {
345 continue; /* Too Short */
348 ext
= &(dp
->d_name
[name_len
- (dbm_pag_len
- 1)]);
350 if (strcmp(ext
, dbm_pag
) != 0) {
351 continue; /* No dbm file extension */
357 * In yptol mode look at LDAP_ prefixed maps. In non yptol mode
361 if (0 != strncmp(dp
->d_name
, NTOL_PREFIX
,
362 strlen(NTOL_PREFIX
))) {
367 * Already have an LDAP_ prefix. Don't want to add it
370 mapname
= dp
->d_name
+ strlen(NTOL_PREFIX
);
372 if (0 == strncmp(dp
->d_name
, NTOL_PREFIX
,
373 strlen(NTOL_PREFIX
))) {
376 mapname
= dp
->d_name
;
379 if (ypmkfilename(domain
, mapname
, path
) == FALSE
) {
380 (void) closedir(dirp
);
385 * At this point, path holds the map file base name (no dbm
386 * file extension), and mapname holds the map name.
388 if (ypcheck_map_existence_yptol(path
) &&
389 !on_maplist(mapname
, *map_list
)) {
390 if (add_in_maplist(mapname
, map_list
, &map_list_len
) ==
392 (void) closedir(dirp
);
398 (void) closedir(dirp
);