xref: /onnv-gate/usr/src/cmd/fs.d/nfs/mountd/nfsauth.c (revision 2140:043bd360aabc)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*2140Srmesta  * Common Development and Distribution License (the "License").
6*2140Srmesta  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*2140Srmesta  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*2140Srmesta  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/stat.h>
340Sstevel@tonic-gate #include <sys/file.h>
350Sstevel@tonic-gate #include <sys/time.h>
360Sstevel@tonic-gate #include <sys/errno.h>
370Sstevel@tonic-gate #include <rpcsvc/mount.h>
380Sstevel@tonic-gate #include <sys/pathconf.h>
390Sstevel@tonic-gate #include <sys/systeminfo.h>
400Sstevel@tonic-gate #include <sys/utsname.h>
410Sstevel@tonic-gate #include <arpa/inet.h>
420Sstevel@tonic-gate #include <signal.h>
430Sstevel@tonic-gate #include <syslog.h>
440Sstevel@tonic-gate #include <locale.h>
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <thread.h>
470Sstevel@tonic-gate #include <netdir.h>
48*2140Srmesta #include <nfs/auth.h>
490Sstevel@tonic-gate #include "../lib/sharetab.h"
500Sstevel@tonic-gate #include "mountd.h"
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static void
53*2140Srmesta nfsauth_access(auth_req *argp, auth_res *result)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	struct netconfig *nconf;
560Sstevel@tonic-gate 	struct nd_hostservlist *clnames = NULL;
570Sstevel@tonic-gate 	struct netbuf nbuf;
580Sstevel@tonic-gate 	struct share *sh;
590Sstevel@tonic-gate 	char tmp[MAXIPADDRLEN];
600Sstevel@tonic-gate 	char *host = NULL;
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	result->auth_perm = NFSAUTH_DENIED;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	/*
650Sstevel@tonic-gate 	 * Convert the client's address to a hostname
660Sstevel@tonic-gate 	 */
670Sstevel@tonic-gate 	nconf = getnetconfigent(argp->req_netid);
680Sstevel@tonic-gate 	if (nconf == NULL) {
690Sstevel@tonic-gate 		syslog(LOG_ERR, "No netconfig entry for %s", argp->req_netid);
700Sstevel@tonic-gate 		return;
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	nbuf.len = argp->req_client.n_len;
740Sstevel@tonic-gate 	nbuf.buf = argp->req_client.n_bytes;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if (netdir_getbyaddr(nconf, &clnames, &nbuf)) {
770Sstevel@tonic-gate 		host = &tmp[0];
780Sstevel@tonic-gate 		if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
790Sstevel@tonic-gate 			struct sockaddr_in *sa;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 			/* LINTED pointer alignment */
820Sstevel@tonic-gate 			sa = (struct sockaddr_in *)nbuf.buf;
830Sstevel@tonic-gate 			(void) inet_ntoa_r(sa->sin_addr, tmp);
840Sstevel@tonic-gate 		} else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) {
850Sstevel@tonic-gate 			struct sockaddr_in6 *sa;
860Sstevel@tonic-gate 			/* LINTED pointer */
870Sstevel@tonic-gate 			sa = (struct sockaddr_in6 *)nbuf.buf;
880Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, sa->sin6_addr.s6_addr,
890Sstevel@tonic-gate 				    tmp, INET6_ADDRSTRLEN);
900Sstevel@tonic-gate 		}
910Sstevel@tonic-gate 		clnames = anon_client(host);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/*
950Sstevel@tonic-gate 	 * Now find the export
960Sstevel@tonic-gate 	 */
970Sstevel@tonic-gate 	sh = findentry(argp->req_path);
980Sstevel@tonic-gate 	if (sh == NULL) {
990Sstevel@tonic-gate 		syslog(LOG_ERR, "%s not exported", argp->req_path);
1000Sstevel@tonic-gate 		goto done;
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	result->auth_perm = check_client(sh, &nbuf, clnames, argp->req_flavor);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	sharefree(sh);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (result->auth_perm == NFSAUTH_DENIED) {
1080Sstevel@tonic-gate 		syslog(LOG_ERR, "%s denied access to %s",
1090Sstevel@tonic-gate 			clnames->h_hostservs[0].h_host, argp->req_path);
1100Sstevel@tonic-gate 	}
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate done:
1130Sstevel@tonic-gate 	freenetconfigent(nconf);
1140Sstevel@tonic-gate 	if (clnames)
1150Sstevel@tonic-gate 		netdir_free(clnames, ND_HOSTSERVLIST);
1160Sstevel@tonic-gate }
117*2140Srmesta 
118*2140Srmesta void
119*2140Srmesta nfsauth_func(void *cookie, char *dataptr, size_t arg_size,
120*2140Srmesta 	door_desc_t *dp, uint_t n_desc)
121*2140Srmesta 
122*2140Srmesta {
123*2140Srmesta 	nfsauth_arg_t	*ap;
124*2140Srmesta 	nfsauth_res_t	 res = {0};
125*2140Srmesta 	nfsauth_res_t	*rp = &res;
126*2140Srmesta 	XDR		 xdrs_a;
127*2140Srmesta 	XDR		 xdrs_r;
128*2140Srmesta 	caddr_t		 abuf = dataptr;
129*2140Srmesta 	size_t		 absz = arg_size;
130*2140Srmesta 	size_t		 rbsz = (size_t)(BYTES_PER_XDR_UNIT * 2);
131*2140Srmesta 	char		 result[BYTES_PER_XDR_UNIT * 2];
132*2140Srmesta 	caddr_t		 rbuf = (caddr_t)&result;
133*2140Srmesta 	varg_t		 varg = {0};
134*2140Srmesta 
135*2140Srmesta 	/*
136*2140Srmesta 	 * Decode the inbound door data, so we can look at the cmd.
137*2140Srmesta 	 */
138*2140Srmesta 	xdrmem_create(&xdrs_a, abuf, absz, XDR_DECODE);
139*2140Srmesta 	if (!xdr_varg(&xdrs_a, &varg)) {
140*2140Srmesta 		/*
141*2140Srmesta 		 * If the arguments can't be decoded, bail.
142*2140Srmesta 		 */
143*2140Srmesta 		if (varg.vers == V_ERROR)
144*2140Srmesta 			syslog(LOG_ERR, gettext("Arg version mismatch"));
145*2140Srmesta 		res.stat = NFSAUTH_DR_DECERR;
146*2140Srmesta 		goto encres;
147*2140Srmesta 	}
148*2140Srmesta 
149*2140Srmesta 	/*
150*2140Srmesta 	 * Now set the args pointer to the proper version of the args
151*2140Srmesta 	 */
152*2140Srmesta 	switch (varg.vers) {
153*2140Srmesta 	case V_PROTO:
154*2140Srmesta 		ap = &varg.arg_u.arg;
155*2140Srmesta 		break;
156*2140Srmesta 
157*2140Srmesta 		/* Additional arguments versions go here */
158*2140Srmesta 
159*2140Srmesta 	default:
160*2140Srmesta 		syslog(LOG_ERR, gettext("Invalid args version"));
161*2140Srmesta 		goto encres;
162*2140Srmesta 	}
163*2140Srmesta 
164*2140Srmesta 	/*
165*2140Srmesta 	 * Call the specified cmd
166*2140Srmesta 	 */
167*2140Srmesta 	switch (ap->cmd) {
168*2140Srmesta 		case NFSAUTH_ACCESS:
169*2140Srmesta 			nfsauth_access(&ap->areq, &rp->ares);
170*2140Srmesta 			rp->stat = NFSAUTH_DR_OKAY;
171*2140Srmesta 			break;
172*2140Srmesta 		default:
173*2140Srmesta 			rp->stat = NFSAUTH_DR_BADCMD;
174*2140Srmesta 			break;
175*2140Srmesta 	}
176*2140Srmesta 
177*2140Srmesta encres:
178*2140Srmesta 	/*
179*2140Srmesta 	 * Free space used to decode the args
180*2140Srmesta 	 */
181*2140Srmesta 	xdrs_a.x_op = XDR_FREE;
182*2140Srmesta 	(void) xdr_varg(&xdrs_a, &varg);
183*2140Srmesta 	xdr_destroy(&xdrs_a);
184*2140Srmesta 
185*2140Srmesta 	/*
186*2140Srmesta 	 * Encode the results before passing thru door.
187*2140Srmesta 	 *
188*2140Srmesta 	 * The result (nfsauth_res_t) is always two int's, so we don't
189*2140Srmesta 	 * have to dynamically size (or allocate) the results buffer.
190*2140Srmesta 	 */
191*2140Srmesta 	xdrmem_create(&xdrs_r, rbuf, rbsz, XDR_ENCODE);
192*2140Srmesta 	if (!xdr_nfsauth_res(&xdrs_r, rp)) {
193*2140Srmesta 		/*
194*2140Srmesta 		 * return only the status code
195*2140Srmesta 		 */
196*2140Srmesta 		rp->stat = NFSAUTH_DR_EFAIL;
197*2140Srmesta 		rbsz = sizeof (uint_t);
198*2140Srmesta 		*rbuf = (uint_t)rp->stat;
199*2140Srmesta 	}
200*2140Srmesta 	xdr_destroy(&xdrs_r);
201*2140Srmesta 
202*2140Srmesta 	(void) door_return((char *)rbuf, rbsz, NULL, 0);
203*2140Srmesta 	(void) door_return(NULL, 0, NULL, 0);
204*2140Srmesta 	/* NOTREACHED */
205*2140Srmesta }
206