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