xref: /illumos-gate/usr/src/cmd/ypcmd/ypserv_ancil.c (revision 7a6d80f1660abd4755c68cbd094d4a914681d26e)
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 #ifndef	lint
36 static	char sccsid[] = "@(#)ypserv_ancil.c 1.13 88/02/08 Copyr 1984 Sun Micro";
37 #endif
38 
39 #include <dirent.h>
40 #include <syslog.h>
41 #include "ypsym.h"
42 #include "ypdefs.h"
43 USE_YPDBPATH
44 USE_DBM
45 #include "shim_hooks.h"
46 #include "shim.h"
47 #include "yptol.h"
48 
49 extern unsigned int strlen();
50 extern int strcmp();
51 extern int isvar_sysv();
52 extern char *strncpy();
53 extern int yp_getkey();
54 
55 /*
56  * This generates a list of the maps in a domain.
57  */
58 int
59 yplist_maps(domain, list)
60 	char *domain;
61 	struct ypmaplist **list;
62 {
63 	DIR *dirp;
64 	struct dirent *dp;
65 	char domdir[MAXNAMLEN + 1];
66 	char path[MAXNAMLEN + 1];
67 	char map_key[YPMAXMAP + 1];
68 	int error;
69 	char *ext;
70 	struct ypmaplist *map;
71 	int namesz;
72 	char *mapname;
73 
74 	*list = (struct ypmaplist *)NULL;
75 
76 	if (!ypcheck_domain(domain)) {
77 		return (YP_NODOM);
78 	}
79 
80 	(void) strcpy(domdir, ypdbpath);
81 	(void) strcat(domdir, "/");
82 	(void) strcat(domdir, domain);
83 
84 	if ((dirp = opendir(domdir)) == NULL) {
85 		return (YP_YPERR);
86 	}
87 
88 	error = YP_TRUE;
89 
90 	for (dp = readdir(dirp); error == YP_TRUE && dp != NULL;
91 	    dp = readdir(dirp)) {
92 		/*
93 		 * If it's possible that the file name is one of the two files
94 		 * implementing a map, remove the extension (dbm_pag or dbm_dir)
95 		 */
96 		namesz =  (int)strlen(dp->d_name);
97 
98 		if (namesz < sizeof (dbm_pag) - 1)
99 			continue;		/* Too Short */
100 
101 		ext = &(dp->d_name[namesz - (sizeof (dbm_pag) - 1)]);
102 
103 		if (strcmp(ext, dbm_pag) != 0)
104 			continue;		/* No dbm file extension */
105 
106 		*ext = '\0';
107 
108 
109 		/*
110 		 * In yptol mode look at LDAP_ prefixed maps. In non yptol mode
111 		 * ignore them.
112 		 */
113 		if (yptol_mode) {
114 			if (0 != strncmp(dp->d_name, NTOL_PREFIX,
115 							strlen(NTOL_PREFIX)))
116 				continue;
117 
118 			/*
119 			 * Already have an LDAP_ prefix. Don't want to add it
120 			 * twice.
121 			 */
122 			mapname = dp->d_name + strlen(NTOL_PREFIX);
123 		} else {
124 			if (0 == strncmp(dp->d_name, NTOL_PREFIX,
125 							strlen(NTOL_PREFIX)))
126 				continue;
127 			mapname = dp->d_name;
128 		}
129 
130 		ypmkfilename(domain, mapname, path);
131 
132 		/*
133 		 * At this point, path holds the map file base name (no dbm
134 		 * file extension), and mapname holds the map name.
135 		 */
136 		if (ypcheck_map_existence(path) &&
137 		    !onmaplist(mapname, *list)) {
138 
139 			if ((map = (struct ypmaplist *)malloc(
140 			    sizeof (struct ypmaplist))) == NULL) {
141 				error = YP_YPERR;
142 				break;
143 			}
144 
145 			map->ypml_next = *list;
146 			*list = map;
147 			namesz = (int)strlen(mapname);
148 
149 			if (namesz <= YPMAXMAP) {
150 				if (yp_getkey(mapname, map_key,
151 						MAXALIASLEN) < 0) {
152 
153 					fprintf(stderr,
154 					"yplist_maps: getkey failed for %s\n",
155 						mapname);
156 					error = YP_YPERR;
157 					break;
158 				} else
159 					(void) strcpy(map->ypml_name, map_key);
160 			} else {
161 				if (yp_getkey(mapname, map_key,
162 						MAXALIASLEN) < 0) {
163 					fprintf(stderr,
164 					"yplist_maps: getkey failed for %s\n",
165 						mapname);
166 					error = YP_YPERR;
167 					break;
168 				} else if (strcmp(mapname, map_key) == 0) {
169 					(void) strncpy(map->ypml_name,
170 							mapname,
171 							(unsigned int) namesz);
172 					map->ypml_name[YPMAXMAP] = '\0';
173 				} else {
174 					(void) strcpy(map->ypml_name, map_key);
175 				}
176 			}
177 		}
178 	}
179 
180 	closedir(dirp);
181 	return (error);
182 }
183