xref: /illumos-gate/usr/src/cmd/ypcmd/ypserv_ancil.c (revision 581cede61ac9c14d8d4ea452562a567189eead78)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #ifndef	lint
38 static	char sccsid[] = "@(#)ypserv_ancil.c 1.13 88/02/08 Copyr 1984 Sun Micro";
39 #endif
40 
41 #include <dirent.h>
42 #include <syslog.h>
43 #include "ypsym.h"
44 #include "ypdefs.h"
45 USE_YPDBPATH
46 USE_DBM
47 #include "shim_hooks.h"
48 #include "shim.h"
49 #include "yptol.h"
50 
51 extern unsigned int strlen();
52 extern int strcmp();
53 extern int isvar_sysv();
54 extern char *strncpy();
55 extern int yp_getkey();
56 
57 /*
58  * This generates a list of the maps in a domain.
59  */
60 int
61 yplist_maps(domain, list)
62 	char *domain;
63 	struct ypmaplist **list;
64 {
65 	DIR *dirp;
66 	struct dirent *dp;
67 	char domdir[MAXNAMLEN + 1];
68 	char path[MAXNAMLEN + 1];
69 	char map_key[YPMAXMAP + 1];
70 	int error;
71 	char *ext;
72 	struct ypmaplist *map;
73 	int namesz;
74 	char *mapname;
75 
76 	*list = (struct ypmaplist *)NULL;
77 
78 	if (!ypcheck_domain(domain)) {
79 		return (YP_NODOM);
80 	}
81 
82 	(void) strcpy(domdir, ypdbpath);
83 	(void) strcat(domdir, "/");
84 	(void) strcat(domdir, domain);
85 
86 	if ((dirp = opendir(domdir)) == NULL) {
87 		return (YP_YPERR);
88 	}
89 
90 	error = YP_TRUE;
91 
92 	for (dp = readdir(dirp); error == YP_TRUE && dp != NULL;
93 	    dp = readdir(dirp)) {
94 		/*
95 		 * If it's possible that the file name is one of the two files
96 		 * implementing a map, remove the extension (dbm_pag or dbm_dir)
97 		 */
98 		namesz =  (int)strlen(dp->d_name);
99 
100 		if (namesz < sizeof (dbm_pag) - 1)
101 			continue;		/* Too Short */
102 
103 		ext = &(dp->d_name[namesz - (sizeof (dbm_pag) - 1)]);
104 
105 		if (strcmp(ext, dbm_pag) != 0)
106 			continue;		/* No dbm file extension */
107 
108 		*ext = '\0';
109 
110 
111 		/*
112 		 * In yptol mode look at LDAP_ prefixed maps. In non yptol mode
113 		 * ignore them.
114 		 */
115 		if (yptol_mode) {
116 			if (0 != strncmp(dp->d_name, NTOL_PREFIX,
117 							strlen(NTOL_PREFIX)))
118 				continue;
119 
120 			/*
121 			 * Already have an LDAP_ prefix. Don't want to add it
122 			 * twice.
123 			 */
124 			mapname = dp->d_name + strlen(NTOL_PREFIX);
125 		} else {
126 			if (0 == strncmp(dp->d_name, NTOL_PREFIX,
127 							strlen(NTOL_PREFIX)))
128 				continue;
129 			mapname = dp->d_name;
130 		}
131 
132 		ypmkfilename(domain, mapname, path);
133 
134 		/*
135 		 * At this point, path holds the map file base name (no dbm
136 		 * file extension), and mapname holds the map name.
137 		 */
138 		if (ypcheck_map_existence(path) &&
139 		    !onmaplist(mapname, *list)) {
140 
141 			if ((map = (struct ypmaplist *)malloc(
142 			    sizeof (struct ypmaplist))) == NULL) {
143 				error = YP_YPERR;
144 				break;
145 			}
146 
147 			map->ypml_next = *list;
148 			*list = map;
149 			namesz = (int)strlen(mapname);
150 
151 			if (namesz <= YPMAXMAP) {
152 				if (yp_getkey(mapname, map_key,
153 						MAXALIASLEN) < 0) {
154 
155 					fprintf(stderr,
156 					"yplist_maps: getkey failed for %s\n",
157 						mapname);
158 					error = YP_YPERR;
159 					break;
160 				} else
161 					(void) strcpy(map->ypml_name, map_key);
162 			} else {
163 				if (yp_getkey(mapname, map_key,
164 						MAXALIASLEN) < 0) {
165 					fprintf(stderr,
166 					"yplist_maps: getkey failed for %s\n",
167 						mapname);
168 					error = YP_YPERR;
169 					break;
170 				} else if (strcmp(mapname, map_key) == 0) {
171 					(void) strncpy(map->ypml_name,
172 							mapname,
173 							(unsigned int) namesz);
174 					map->ypml_name[YPMAXMAP] = '\0';
175 				} else {
176 					(void) strcpy(map->ypml_name, map_key);
177 				}
178 			}
179 		}
180 	}
181 
182 	closedir(dirp);
183 	return (error);
184 }
185