xref: /onnv-gate/usr/src/cmd/ypcmd/ypserv_map.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
32*0Sstevel@tonic-gate  * under license from the Regents of the University of California.
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <stdlib.h>
38*0Sstevel@tonic-gate #include <dirent.h>
39*0Sstevel@tonic-gate #include <string.h>
40*0Sstevel@tonic-gate #include <malloc.h>
41*0Sstevel@tonic-gate #include "ypsym.h"
42*0Sstevel@tonic-gate #include "ypdefs.h"
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /* Use N2L version of DBM calls */
45*0Sstevel@tonic-gate #include "shim_hooks.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate USE_YP_MASTER_NAME
48*0Sstevel@tonic-gate USE_YP_LAST_MODIFIED
49*0Sstevel@tonic-gate USE_YPDBPATH
50*0Sstevel@tonic-gate USE_YP_SECURE
51*0Sstevel@tonic-gate USE_DBM
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate #include <ctype.h>
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate static DBM *cur_fdb; /* will be passwd back up by ypset_current_map */
56*0Sstevel@tonic-gate static enum { UNKNOWN, SECURE, PUBLIC } current_map_access = UNKNOWN;
57*0Sstevel@tonic-gate static char map_owner[MAX_MASTER_NAME + 1];
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate extern unsigned int ypcheck_domain();
60*0Sstevel@tonic-gate int check_secure_net_ti(struct netbuf *caller, char *ypname);
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate /*
63*0Sstevel@tonic-gate  * The retrieves the order number of a named map from the order number datum
64*0Sstevel@tonic-gate  * in the map data base.
65*0Sstevel@tonic-gate  */
66*0Sstevel@tonic-gate bool
ypget_map_order(char * map,char * domain,uint_t * order)67*0Sstevel@tonic-gate ypget_map_order(char *map, char *domain, uint_t *order)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate 	datum key;
70*0Sstevel@tonic-gate 	datum val;
71*0Sstevel@tonic-gate 	char toconvert[MAX_ASCII_ORDER_NUMBER_LENGTH + 1];
72*0Sstevel@tonic-gate 	uint_t error;
73*0Sstevel@tonic-gate 	DBM *fdb;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	if ((fdb = ypset_current_map(map, domain, &error)) != NULL) {
76*0Sstevel@tonic-gate 		key.dptr = yp_last_modified;
77*0Sstevel@tonic-gate 		key.dsize = yp_last_modified_sz;
78*0Sstevel@tonic-gate 		val = dbm_fetch(fdb, key);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 		if (val.dptr != (char *)NULL) {
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate 			if (val.dsize > MAX_ASCII_ORDER_NUMBER_LENGTH) {
83*0Sstevel@tonic-gate 			return (FALSE);
84*0Sstevel@tonic-gate 			}
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate 			/*
87*0Sstevel@tonic-gate 			 * This is getting recopied here because val.dptr
88*0Sstevel@tonic-gate 			 * points to static memory owned by the dbm package,
89*0Sstevel@tonic-gate 			 * and we have no idea whether numeric characters
90*0Sstevel@tonic-gate 			 * follow the order number characters, nor whether
91*0Sstevel@tonic-gate 			 * the mess is null-terminated at all.
92*0Sstevel@tonic-gate 			 */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 			memcpy(toconvert, val.dptr, val.dsize);
95*0Sstevel@tonic-gate 			toconvert[val.dsize] = '\0';
96*0Sstevel@tonic-gate 			*order = (unsigned long) atol(toconvert);
97*0Sstevel@tonic-gate 			return (TRUE);
98*0Sstevel@tonic-gate 		} else {
99*0Sstevel@tonic-gate 			return (FALSE);
100*0Sstevel@tonic-gate 		}
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	} else {
103*0Sstevel@tonic-gate 		return (FALSE);
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate }
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate /*
108*0Sstevel@tonic-gate  * The retrieves the master server name of a named map from the master datum
109*0Sstevel@tonic-gate  * in the map data base.
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate bool
ypget_map_master(char ** owner,DBM * fdb)112*0Sstevel@tonic-gate ypget_map_master(char **owner, DBM *fdb)
113*0Sstevel@tonic-gate {
114*0Sstevel@tonic-gate 	datum key;
115*0Sstevel@tonic-gate 	datum val;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	key.dptr = yp_master_name;
118*0Sstevel@tonic-gate 	key.dsize = yp_master_name_sz;
119*0Sstevel@tonic-gate 	val = dbm_fetch(fdb, key);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	if (val.dptr != (char *)NULL) {
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 		if (val.dsize > MAX_MASTER_NAME) {
124*0Sstevel@tonic-gate 			return (FALSE);
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 		/*
128*0Sstevel@tonic-gate 		 * This is getting recopied here because val.dptr
129*0Sstevel@tonic-gate 		 * points to static memory owned by the dbm package.
130*0Sstevel@tonic-gate 		 */
131*0Sstevel@tonic-gate 		memcpy(map_owner, val.dptr, val.dsize);
132*0Sstevel@tonic-gate 		map_owner[val.dsize] = '\0';
133*0Sstevel@tonic-gate 		*owner = map_owner;
134*0Sstevel@tonic-gate 		return (TRUE);
135*0Sstevel@tonic-gate 	} else {
136*0Sstevel@tonic-gate 		return (FALSE);
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate }
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate /*
141*0Sstevel@tonic-gate  * This makes a map into the current map, and calls dbminit on that map
142*0Sstevel@tonic-gate  * and returns the DBM pointer to the map. Procedures called by
143*0Sstevel@tonic-gate  * ypserv dispatch routine would use this pointer for successive
144*0Sstevel@tonic-gate  * ndbm operations.  Returns an YP_xxxx error code in error if FALSE.
145*0Sstevel@tonic-gate  */
146*0Sstevel@tonic-gate DBM *
ypset_current_map(char * map,char * domain,uint_t * error)147*0Sstevel@tonic-gate ypset_current_map(char *map, char *domain, uint_t *error)
148*0Sstevel@tonic-gate {
149*0Sstevel@tonic-gate 	char mapname[MAXNAMLEN + 1];
150*0Sstevel@tonic-gate 	int lenm, lend;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	/* Do not allow any path as a domain name or a map name.   */
153*0Sstevel@tonic-gate 	if (!map || ((lenm = (int)strlen(map)) == 0) || (lenm > YPMAXMAP) ||
154*0Sstevel@tonic-gate 	    !domain || ((lend = (int)strlen(domain)) == 0) ||
155*0Sstevel@tonic-gate 	    (lend > YPMAXDOMAIN) || (strchr(map, '/') != NULL) ||
156*0Sstevel@tonic-gate 	    (strchr(domain, '/') != NULL)) {
157*0Sstevel@tonic-gate 		*error = YP_BADARGS;
158*0Sstevel@tonic-gate 		return (FALSE);
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	if (FALSE == ypmkfilename(domain, map, mapname))
162*0Sstevel@tonic-gate 		return (FALSE);
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate 	if ((cur_fdb) && (strcmp(mapname, get_map_name(cur_fdb)) == 0)) {
165*0Sstevel@tonic-gate 		return (cur_fdb);
166*0Sstevel@tonic-gate 	}
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate 	/* If there was a previous open map close it */
169*0Sstevel@tonic-gate 	if (NULL != cur_fdb)
170*0Sstevel@tonic-gate 		dbm_close(cur_fdb);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	/* Set the map access as "unknown" as the new map has not been loaded */
173*0Sstevel@tonic-gate 	current_map_access = UNKNOWN;
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate 	/* All the map locking is now handled inside the dbm_open shim */
176*0Sstevel@tonic-gate 	if ((cur_fdb = dbm_open(mapname, O_RDWR, 0644)) != NULL) {
177*0Sstevel@tonic-gate 		return (cur_fdb);
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	if (ypcheck_domain(domain)) {
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 		if (ypcheck_map_existence(mapname)) {
183*0Sstevel@tonic-gate 			*error = YP_BADDB;
184*0Sstevel@tonic-gate 		} else {
185*0Sstevel@tonic-gate 			*error = YP_NOMAP;
186*0Sstevel@tonic-gate 		}
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	} else {
189*0Sstevel@tonic-gate 		*error = YP_NODOM;
190*0Sstevel@tonic-gate 	}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 	return (NULL);
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate /*
196*0Sstevel@tonic-gate  * This checks to see if there is a current map, and, if there is, does a
197*0Sstevel@tonic-gate  * dbmclose on it and sets the current map name and its DBM ptr to null.
198*0Sstevel@tonic-gate  */
199*0Sstevel@tonic-gate void
ypclr_current_map(void)200*0Sstevel@tonic-gate ypclr_current_map(void)
201*0Sstevel@tonic-gate {
202*0Sstevel@tonic-gate 	if (cur_fdb != NULL) {
203*0Sstevel@tonic-gate 		(void) dbm_close(cur_fdb);
204*0Sstevel@tonic-gate 		cur_fdb = NULL;
205*0Sstevel@tonic-gate 	}
206*0Sstevel@tonic-gate 	current_map_access = UNKNOWN;
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate 
209*0Sstevel@tonic-gate /*
210*0Sstevel@tonic-gate  * Checks to see if caller has permission to query the current map (as
211*0Sstevel@tonic-gate  * set by ypset_current_map()).  Returns TRUE if access is granted and
212*0Sstevel@tonic-gate  * FALSE otherwise.  If FALSE then sets *error to YP_xxxxxxxx.
213*0Sstevel@tonic-gate  */
214*0Sstevel@tonic-gate bool
yp_map_access(SVCXPRT * transp,uint_t * error,DBM * fdb)215*0Sstevel@tonic-gate yp_map_access(SVCXPRT *transp, uint_t *error, DBM *fdb)
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate 	char *ypname = "ypserv";
218*0Sstevel@tonic-gate 	struct netbuf *nbuf;
219*0Sstevel@tonic-gate 	sa_family_t af;
220*0Sstevel@tonic-gate 	in_port_t port;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	nbuf = svc_getrpccaller(transp);
223*0Sstevel@tonic-gate 	af = ((struct sockaddr_storage *)nbuf->buf)->ss_family;
224*0Sstevel@tonic-gate 	if (af != AF_INET && af != AF_INET6)
225*0Sstevel@tonic-gate 		return (FALSE);
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	if (!(check_secure_net_ti(nbuf, ypname))) {
228*0Sstevel@tonic-gate 		*error = YP_NOMAP;
229*0Sstevel@tonic-gate 		return (FALSE);
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 	/* XXX - I expect that this won't happen much */
233*0Sstevel@tonic-gate 	if (current_map_access == PUBLIC) {
234*0Sstevel@tonic-gate 		return (TRUE);
235*0Sstevel@tonic-gate 	}
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 	if (af == AF_INET6) {
238*0Sstevel@tonic-gate 		port = ntohs(((struct sockaddr_in6 *)nbuf->buf)->sin6_port);
239*0Sstevel@tonic-gate 	} else {
240*0Sstevel@tonic-gate 		port = ntohs(((struct sockaddr_in *)nbuf->buf)->sin_port);
241*0Sstevel@tonic-gate 	}
242*0Sstevel@tonic-gate 	if (port < IPPORT_RESERVED) {
243*0Sstevel@tonic-gate 		return (TRUE);
244*0Sstevel@tonic-gate 	}
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate 	if (current_map_access == UNKNOWN) {
247*0Sstevel@tonic-gate 		datum key;
248*0Sstevel@tonic-gate 		datum val;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 		key.dptr = yp_secure;
251*0Sstevel@tonic-gate 		key.dsize = yp_secure_sz;
252*0Sstevel@tonic-gate 		val = dbm_fetch(fdb, key);
253*0Sstevel@tonic-gate 		if (val.dptr == (char *)NULL) {
254*0Sstevel@tonic-gate 			current_map_access = PUBLIC;
255*0Sstevel@tonic-gate 			return (TRUE);
256*0Sstevel@tonic-gate 		}
257*0Sstevel@tonic-gate 		current_map_access = SECURE;
258*0Sstevel@tonic-gate 	}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	/* current_map_access == SECURE and non-priviledged caller */
261*0Sstevel@tonic-gate 	*error = YP_NOMAP;
262*0Sstevel@tonic-gate 	return (FALSE);
263*0Sstevel@tonic-gate }
264