xref: /titanic_50/usr/src/cmd/ypcmd/ypserv_map.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 2004 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 #include <stdlib.h>
38*7c478bd9Sstevel@tonic-gate #include <dirent.h>
39*7c478bd9Sstevel@tonic-gate #include <string.h>
40*7c478bd9Sstevel@tonic-gate #include <malloc.h>
41*7c478bd9Sstevel@tonic-gate #include "ypsym.h"
42*7c478bd9Sstevel@tonic-gate #include "ypdefs.h"
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /* Use N2L version of DBM calls */
45*7c478bd9Sstevel@tonic-gate #include "shim_hooks.h"
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate USE_YP_MASTER_NAME
48*7c478bd9Sstevel@tonic-gate USE_YP_LAST_MODIFIED
49*7c478bd9Sstevel@tonic-gate USE_YPDBPATH
50*7c478bd9Sstevel@tonic-gate USE_YP_SECURE
51*7c478bd9Sstevel@tonic-gate USE_DBM
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #include <ctype.h>
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate static DBM *cur_fdb; /* will be passwd back up by ypset_current_map */
56*7c478bd9Sstevel@tonic-gate static enum { UNKNOWN, SECURE, PUBLIC } current_map_access = UNKNOWN;
57*7c478bd9Sstevel@tonic-gate static char map_owner[MAX_MASTER_NAME + 1];
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate extern unsigned int ypcheck_domain();
60*7c478bd9Sstevel@tonic-gate int check_secure_net_ti(struct netbuf *caller, char *ypname);
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate /*
63*7c478bd9Sstevel@tonic-gate  * The retrieves the order number of a named map from the order number datum
64*7c478bd9Sstevel@tonic-gate  * in the map data base.
65*7c478bd9Sstevel@tonic-gate  */
66*7c478bd9Sstevel@tonic-gate bool
ypget_map_order(char * map,char * domain,uint_t * order)67*7c478bd9Sstevel@tonic-gate ypget_map_order(char *map, char *domain, uint_t *order)
68*7c478bd9Sstevel@tonic-gate {
69*7c478bd9Sstevel@tonic-gate 	datum key;
70*7c478bd9Sstevel@tonic-gate 	datum val;
71*7c478bd9Sstevel@tonic-gate 	char toconvert[MAX_ASCII_ORDER_NUMBER_LENGTH + 1];
72*7c478bd9Sstevel@tonic-gate 	uint_t error;
73*7c478bd9Sstevel@tonic-gate 	DBM *fdb;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	if ((fdb = ypset_current_map(map, domain, &error)) != NULL) {
76*7c478bd9Sstevel@tonic-gate 		key.dptr = yp_last_modified;
77*7c478bd9Sstevel@tonic-gate 		key.dsize = yp_last_modified_sz;
78*7c478bd9Sstevel@tonic-gate 		val = dbm_fetch(fdb, key);
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 		if (val.dptr != (char *)NULL) {
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 			if (val.dsize > MAX_ASCII_ORDER_NUMBER_LENGTH) {
83*7c478bd9Sstevel@tonic-gate 			return (FALSE);
84*7c478bd9Sstevel@tonic-gate 			}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 			/*
87*7c478bd9Sstevel@tonic-gate 			 * This is getting recopied here because val.dptr
88*7c478bd9Sstevel@tonic-gate 			 * points to static memory owned by the dbm package,
89*7c478bd9Sstevel@tonic-gate 			 * and we have no idea whether numeric characters
90*7c478bd9Sstevel@tonic-gate 			 * follow the order number characters, nor whether
91*7c478bd9Sstevel@tonic-gate 			 * the mess is null-terminated at all.
92*7c478bd9Sstevel@tonic-gate 			 */
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 			memcpy(toconvert, val.dptr, val.dsize);
95*7c478bd9Sstevel@tonic-gate 			toconvert[val.dsize] = '\0';
96*7c478bd9Sstevel@tonic-gate 			*order = (unsigned long) atol(toconvert);
97*7c478bd9Sstevel@tonic-gate 			return (TRUE);
98*7c478bd9Sstevel@tonic-gate 		} else {
99*7c478bd9Sstevel@tonic-gate 			return (FALSE);
100*7c478bd9Sstevel@tonic-gate 		}
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	} else {
103*7c478bd9Sstevel@tonic-gate 		return (FALSE);
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate /*
108*7c478bd9Sstevel@tonic-gate  * The retrieves the master server name of a named map from the master datum
109*7c478bd9Sstevel@tonic-gate  * in the map data base.
110*7c478bd9Sstevel@tonic-gate  */
111*7c478bd9Sstevel@tonic-gate bool
ypget_map_master(char ** owner,DBM * fdb)112*7c478bd9Sstevel@tonic-gate ypget_map_master(char **owner, DBM *fdb)
113*7c478bd9Sstevel@tonic-gate {
114*7c478bd9Sstevel@tonic-gate 	datum key;
115*7c478bd9Sstevel@tonic-gate 	datum val;
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	key.dptr = yp_master_name;
118*7c478bd9Sstevel@tonic-gate 	key.dsize = yp_master_name_sz;
119*7c478bd9Sstevel@tonic-gate 	val = dbm_fetch(fdb, key);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	if (val.dptr != (char *)NULL) {
122*7c478bd9Sstevel@tonic-gate 
123*7c478bd9Sstevel@tonic-gate 		if (val.dsize > MAX_MASTER_NAME) {
124*7c478bd9Sstevel@tonic-gate 			return (FALSE);
125*7c478bd9Sstevel@tonic-gate 		}
126*7c478bd9Sstevel@tonic-gate 
127*7c478bd9Sstevel@tonic-gate 		/*
128*7c478bd9Sstevel@tonic-gate 		 * This is getting recopied here because val.dptr
129*7c478bd9Sstevel@tonic-gate 		 * points to static memory owned by the dbm package.
130*7c478bd9Sstevel@tonic-gate 		 */
131*7c478bd9Sstevel@tonic-gate 		memcpy(map_owner, val.dptr, val.dsize);
132*7c478bd9Sstevel@tonic-gate 		map_owner[val.dsize] = '\0';
133*7c478bd9Sstevel@tonic-gate 		*owner = map_owner;
134*7c478bd9Sstevel@tonic-gate 		return (TRUE);
135*7c478bd9Sstevel@tonic-gate 	} else {
136*7c478bd9Sstevel@tonic-gate 		return (FALSE);
137*7c478bd9Sstevel@tonic-gate 	}
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate /*
141*7c478bd9Sstevel@tonic-gate  * This makes a map into the current map, and calls dbminit on that map
142*7c478bd9Sstevel@tonic-gate  * and returns the DBM pointer to the map. Procedures called by
143*7c478bd9Sstevel@tonic-gate  * ypserv dispatch routine would use this pointer for successive
144*7c478bd9Sstevel@tonic-gate  * ndbm operations.  Returns an YP_xxxx error code in error if FALSE.
145*7c478bd9Sstevel@tonic-gate  */
146*7c478bd9Sstevel@tonic-gate DBM *
ypset_current_map(char * map,char * domain,uint_t * error)147*7c478bd9Sstevel@tonic-gate ypset_current_map(char *map, char *domain, uint_t *error)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	char mapname[MAXNAMLEN + 1];
150*7c478bd9Sstevel@tonic-gate 	int lenm, lend;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	/* Do not allow any path as a domain name or a map name.   */
153*7c478bd9Sstevel@tonic-gate 	if (!map || ((lenm = (int)strlen(map)) == 0) || (lenm > YPMAXMAP) ||
154*7c478bd9Sstevel@tonic-gate 	    !domain || ((lend = (int)strlen(domain)) == 0) ||
155*7c478bd9Sstevel@tonic-gate 	    (lend > YPMAXDOMAIN) || (strchr(map, '/') != NULL) ||
156*7c478bd9Sstevel@tonic-gate 	    (strchr(domain, '/') != NULL)) {
157*7c478bd9Sstevel@tonic-gate 		*error = YP_BADARGS;
158*7c478bd9Sstevel@tonic-gate 		return (FALSE);
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	if (FALSE == ypmkfilename(domain, map, mapname))
162*7c478bd9Sstevel@tonic-gate 		return (FALSE);
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 	if ((cur_fdb) && (strcmp(mapname, get_map_name(cur_fdb)) == 0)) {
165*7c478bd9Sstevel@tonic-gate 		return (cur_fdb);
166*7c478bd9Sstevel@tonic-gate 	}
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	/* If there was a previous open map close it */
169*7c478bd9Sstevel@tonic-gate 	if (NULL != cur_fdb)
170*7c478bd9Sstevel@tonic-gate 		dbm_close(cur_fdb);
171*7c478bd9Sstevel@tonic-gate 
172*7c478bd9Sstevel@tonic-gate 	/* Set the map access as "unknown" as the new map has not been loaded */
173*7c478bd9Sstevel@tonic-gate 	current_map_access = UNKNOWN;
174*7c478bd9Sstevel@tonic-gate 
175*7c478bd9Sstevel@tonic-gate 	/* All the map locking is now handled inside the dbm_open shim */
176*7c478bd9Sstevel@tonic-gate 	if ((cur_fdb = dbm_open(mapname, O_RDWR, 0644)) != NULL) {
177*7c478bd9Sstevel@tonic-gate 		return (cur_fdb);
178*7c478bd9Sstevel@tonic-gate 	}
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	if (ypcheck_domain(domain)) {
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 		if (ypcheck_map_existence(mapname)) {
183*7c478bd9Sstevel@tonic-gate 			*error = YP_BADDB;
184*7c478bd9Sstevel@tonic-gate 		} else {
185*7c478bd9Sstevel@tonic-gate 			*error = YP_NOMAP;
186*7c478bd9Sstevel@tonic-gate 		}
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 	} else {
189*7c478bd9Sstevel@tonic-gate 		*error = YP_NODOM;
190*7c478bd9Sstevel@tonic-gate 	}
191*7c478bd9Sstevel@tonic-gate 
192*7c478bd9Sstevel@tonic-gate 	return (NULL);
193*7c478bd9Sstevel@tonic-gate }
194*7c478bd9Sstevel@tonic-gate 
195*7c478bd9Sstevel@tonic-gate /*
196*7c478bd9Sstevel@tonic-gate  * This checks to see if there is a current map, and, if there is, does a
197*7c478bd9Sstevel@tonic-gate  * dbmclose on it and sets the current map name and its DBM ptr to null.
198*7c478bd9Sstevel@tonic-gate  */
199*7c478bd9Sstevel@tonic-gate void
ypclr_current_map(void)200*7c478bd9Sstevel@tonic-gate ypclr_current_map(void)
201*7c478bd9Sstevel@tonic-gate {
202*7c478bd9Sstevel@tonic-gate 	if (cur_fdb != NULL) {
203*7c478bd9Sstevel@tonic-gate 		(void) dbm_close(cur_fdb);
204*7c478bd9Sstevel@tonic-gate 		cur_fdb = NULL;
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate 	current_map_access = UNKNOWN;
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate /*
210*7c478bd9Sstevel@tonic-gate  * Checks to see if caller has permission to query the current map (as
211*7c478bd9Sstevel@tonic-gate  * set by ypset_current_map()).  Returns TRUE if access is granted and
212*7c478bd9Sstevel@tonic-gate  * FALSE otherwise.  If FALSE then sets *error to YP_xxxxxxxx.
213*7c478bd9Sstevel@tonic-gate  */
214*7c478bd9Sstevel@tonic-gate bool
yp_map_access(SVCXPRT * transp,uint_t * error,DBM * fdb)215*7c478bd9Sstevel@tonic-gate yp_map_access(SVCXPRT *transp, uint_t *error, DBM *fdb)
216*7c478bd9Sstevel@tonic-gate {
217*7c478bd9Sstevel@tonic-gate 	char *ypname = "ypserv";
218*7c478bd9Sstevel@tonic-gate 	struct netbuf *nbuf;
219*7c478bd9Sstevel@tonic-gate 	sa_family_t af;
220*7c478bd9Sstevel@tonic-gate 	in_port_t port;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	nbuf = svc_getrpccaller(transp);
223*7c478bd9Sstevel@tonic-gate 	af = ((struct sockaddr_storage *)nbuf->buf)->ss_family;
224*7c478bd9Sstevel@tonic-gate 	if (af != AF_INET && af != AF_INET6)
225*7c478bd9Sstevel@tonic-gate 		return (FALSE);
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 	if (!(check_secure_net_ti(nbuf, ypname))) {
228*7c478bd9Sstevel@tonic-gate 		*error = YP_NOMAP;
229*7c478bd9Sstevel@tonic-gate 		return (FALSE);
230*7c478bd9Sstevel@tonic-gate 	}
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 	/* XXX - I expect that this won't happen much */
233*7c478bd9Sstevel@tonic-gate 	if (current_map_access == PUBLIC) {
234*7c478bd9Sstevel@tonic-gate 		return (TRUE);
235*7c478bd9Sstevel@tonic-gate 	}
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	if (af == AF_INET6) {
238*7c478bd9Sstevel@tonic-gate 		port = ntohs(((struct sockaddr_in6 *)nbuf->buf)->sin6_port);
239*7c478bd9Sstevel@tonic-gate 	} else {
240*7c478bd9Sstevel@tonic-gate 		port = ntohs(((struct sockaddr_in *)nbuf->buf)->sin_port);
241*7c478bd9Sstevel@tonic-gate 	}
242*7c478bd9Sstevel@tonic-gate 	if (port < IPPORT_RESERVED) {
243*7c478bd9Sstevel@tonic-gate 		return (TRUE);
244*7c478bd9Sstevel@tonic-gate 	}
245*7c478bd9Sstevel@tonic-gate 
246*7c478bd9Sstevel@tonic-gate 	if (current_map_access == UNKNOWN) {
247*7c478bd9Sstevel@tonic-gate 		datum key;
248*7c478bd9Sstevel@tonic-gate 		datum val;
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 		key.dptr = yp_secure;
251*7c478bd9Sstevel@tonic-gate 		key.dsize = yp_secure_sz;
252*7c478bd9Sstevel@tonic-gate 		val = dbm_fetch(fdb, key);
253*7c478bd9Sstevel@tonic-gate 		if (val.dptr == (char *)NULL) {
254*7c478bd9Sstevel@tonic-gate 			current_map_access = PUBLIC;
255*7c478bd9Sstevel@tonic-gate 			return (TRUE);
256*7c478bd9Sstevel@tonic-gate 		}
257*7c478bd9Sstevel@tonic-gate 		current_map_access = SECURE;
258*7c478bd9Sstevel@tonic-gate 	}
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	/* current_map_access == SECURE and non-priviledged caller */
261*7c478bd9Sstevel@tonic-gate 	*error = YP_NOMAP;
262*7c478bd9Sstevel@tonic-gate 	return (FALSE);
263*7c478bd9Sstevel@tonic-gate }
264