sd: remove 'ssd' driver support
[unleashed/tickless.git] / usr / src / lib / libnisdb / yptol / shim_ancil.c
blob5519cb0639198af0bb8455d8ba4b68c5e9667bb4
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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.
35 #include <stdlib.h>
36 #include <dirent.h>
37 #include <strings.h>
38 #include "ypsym.h"
39 #include "ypdefs.h"
40 USE_YPDBPATH
41 USE_DBM
42 #include "shim.h"
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
55 bool_t
56 ypmkfilename(domain, map, path)
57 char *domain;
58 char *map;
59 char *path;
61 int length;
63 /* Do not allow any path as a domain name. */
64 if (strchr(domain, '/') != NULL)
65 return (FALSE);
67 length = strlen(domain) + strlen(map) + ypdbpath_sz + 3;
68 if (yptol_mode)
69 length += strlen(NTOL_PREFIX) + 1;
71 if ((MAXNAMLEN + 1) < length) {
72 (void) fprintf(stderr, "ypserv: Map name string too long.\n");
73 return (FALSE);
76 strcpy(path, ypdbpath);
77 strcat(path, "/");
78 strcat(path, domain);
79 strcat(path, "/");
81 /* If in N2L mode add N2L prefix */
82 if (yptol_mode)
83 strcat(path, NTOL_PREFIX);
84 strcat(path, map);
86 return (TRUE);
90 * check whether a map is already in an array/list
92 * RETURNS: TRUE if yes
93 * FALSE if not
95 bool_t
96 on_maplist(char *mapname, char **list) {
97 int i = 0;
99 if (list == NULL) {
100 return (FALSE);
103 while (list[i] != NULL) {
104 if (strcmp(mapname, list[i++]) == 0) {
105 return (TRUE);
109 return (FALSE);
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
118 * FALSE if not
120 bool_t
121 add_in_maplist(char *mapname, char ***list, int *list_len) {
122 int i = 0;
123 char **list_tmp;
125 if (list == NULL) {
126 return (FALSE);
129 list_tmp = *list;
131 if (list_tmp == NULL) {
132 *list_len = 0;
133 } else {
134 /* find 1st free element */
135 while (list_tmp[i] != NULL) {
137 * increment in loop so that
138 * list_tmp[i] == NULL
139 * when exiting
141 i++;
145 /* if we don't know list length, assume we reach its end */
146 if (*list_len == -1) {
147 *list_len = i;
150 /* do we need to reallocate ? */
151 if (i+1 >= *list_len) {
152 list_tmp = reallocarray(list_tmp, *list_len + ARRAY_CHUNK,
153 sizeof (char *));
154 if (list_tmp == NULL) {
155 return (FALSE);
157 *list = list_tmp;
158 *list_len += ARRAY_CHUNK;
161 /* add in list */
162 (*list)[i] = strdup(mapname);
163 if ((*list)[i] == NULL) {
164 /* strdup() failed */
165 return (FALSE);
167 (*list)[++i] = NULL;
169 return (TRUE);
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.
179 bool
180 ypcheck_domain_yptol(char *domain)
182 char path[MAXNAMLEN + 1];
183 struct stat filestat;
184 bool present = FALSE;
186 strcpy(path, ypdbpath);
187 strcat(path, "/");
188 if (strlcat(path, domain, MAXNAMLEN + 1) >= MAXNAMLEN + 1)
189 return (present);
191 if (stat(path, &filestat) != -1) {
192 if (S_ISDIR(filestat.st_mode))
193 present = TRUE;
195 return (present);
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
201 * path.
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
208 * not be reported.
210 * Was originally in cmd/ypcmd/shared/utils.c as ypcheck_map_existence().
211 * Now ypcheck_map_existence() calls this function.
213 bool
214 ypcheck_map_existence_yptol(char *pname)
216 char dbfile[MAXNAMLEN + sizeof (TTL_POSTFIX) + 1];
217 struct stat64 filestat;
218 int len;
220 if (!pname || ((len = (int)strlen(pname)) == 0) ||
221 (len + sizeof (dbm_pag) + sizeof (TTL_POSTFIX)) >
222 sizeof (dbfile)) {
223 return (FALSE);
226 errno = 0;
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",
236 dbfile);
238 return (FALSE);
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",
249 dbfile);
251 return (FALSE);
254 if (yptol_mode) {
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",
264 dbfile);
266 return (FALSE);
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",
278 dbfile);
280 return (FALSE);
284 return (TRUE);
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
299 bool_t
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);
305 DIR *dirp;
306 struct dirent *dp;
307 int name_len;
308 int dbm_pag_len = sizeof (dbm_pag);
309 char *ext;
310 char *mapname;
311 int map_list_len = -1;
313 if (map_list == NULL) {
314 return (FALSE);
317 /* no domain, not a problem */
318 if (domain == NULL) {
319 return (TRUE);
322 /* not a valid domain, not a problem */
323 if (!ypcheck_domain_yptol(domain)) {
324 return (TRUE);
327 if (snprintf(domdir, domdir_len, "%s/%s", ypdbpath, domain)
328 > domdir_len) {
329 return (FALSE);
332 if ((dirp = opendir(domdir)) == NULL) {
333 return (FALSE);
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 */
354 *ext = '\0';
357 * In yptol mode look at LDAP_ prefixed maps. In non yptol mode
358 * ignore them.
360 if (yptol_mode) {
361 if (0 != strncmp(dp->d_name, NTOL_PREFIX,
362 strlen(NTOL_PREFIX))) {
363 continue;
367 * Already have an LDAP_ prefix. Don't want to add it
368 * twice.
370 mapname = dp->d_name + strlen(NTOL_PREFIX);
371 } else {
372 if (0 == strncmp(dp->d_name, NTOL_PREFIX,
373 strlen(NTOL_PREFIX))) {
374 continue;
376 mapname = dp->d_name;
379 if (ypmkfilename(domain, mapname, path) == FALSE) {
380 (void) closedir(dirp);
381 return (FALSE);
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) ==
391 FALSE) {
392 (void) closedir(dirp);
393 return (FALSE);
398 (void) closedir(dirp);
399 return (TRUE);