xref: /onnv-gate/usr/src/cmd/fs.d/nfs/mountd/nfsauth.c (revision 3957:86c9dda5df37)
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
52140Srmesta  * Common Development and Distribution License (the "License").
62140Srmesta  * 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  */
21*3957Sth199096 
220Sstevel@tonic-gate /*
23*3957Sth199096  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
242140Srmesta  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/stat.h>
350Sstevel@tonic-gate #include <sys/file.h>
360Sstevel@tonic-gate #include <sys/time.h>
370Sstevel@tonic-gate #include <sys/errno.h>
380Sstevel@tonic-gate #include <rpcsvc/mount.h>
390Sstevel@tonic-gate #include <sys/pathconf.h>
400Sstevel@tonic-gate #include <sys/systeminfo.h>
410Sstevel@tonic-gate #include <sys/utsname.h>
420Sstevel@tonic-gate #include <arpa/inet.h>
430Sstevel@tonic-gate #include <signal.h>
440Sstevel@tonic-gate #include <syslog.h>
450Sstevel@tonic-gate #include <locale.h>
460Sstevel@tonic-gate #include <unistd.h>
470Sstevel@tonic-gate #include <thread.h>
480Sstevel@tonic-gate #include <netdir.h>
492140Srmesta #include <nfs/auth.h>
50*3957Sth199096 #include <sharefs/share.h>
510Sstevel@tonic-gate #include "../lib/sharetab.h"
520Sstevel@tonic-gate #include "mountd.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static void
552140Srmesta nfsauth_access(auth_req *argp, auth_res *result)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	struct netconfig *nconf;
580Sstevel@tonic-gate 	struct nd_hostservlist *clnames = NULL;
590Sstevel@tonic-gate 	struct netbuf nbuf;
600Sstevel@tonic-gate 	struct share *sh;
610Sstevel@tonic-gate 	char tmp[MAXIPADDRLEN];
620Sstevel@tonic-gate 	char *host = NULL;
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	result->auth_perm = NFSAUTH_DENIED;
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * Convert the client's address to a hostname
680Sstevel@tonic-gate 	 */
690Sstevel@tonic-gate 	nconf = getnetconfigent(argp->req_netid);
700Sstevel@tonic-gate 	if (nconf == NULL) {
710Sstevel@tonic-gate 		syslog(LOG_ERR, "No netconfig entry for %s", argp->req_netid);
720Sstevel@tonic-gate 		return;
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	nbuf.len = argp->req_client.n_len;
760Sstevel@tonic-gate 	nbuf.buf = argp->req_client.n_bytes;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	if (netdir_getbyaddr(nconf, &clnames, &nbuf)) {
790Sstevel@tonic-gate 		host = &tmp[0];
800Sstevel@tonic-gate 		if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
810Sstevel@tonic-gate 			struct sockaddr_in *sa;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 			/* LINTED pointer alignment */
840Sstevel@tonic-gate 			sa = (struct sockaddr_in *)nbuf.buf;
850Sstevel@tonic-gate 			(void) inet_ntoa_r(sa->sin_addr, tmp);
860Sstevel@tonic-gate 		} else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) {
870Sstevel@tonic-gate 			struct sockaddr_in6 *sa;
880Sstevel@tonic-gate 			/* LINTED pointer */
890Sstevel@tonic-gate 			sa = (struct sockaddr_in6 *)nbuf.buf;
900Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, sa->sin6_addr.s6_addr,
910Sstevel@tonic-gate 				    tmp, INET6_ADDRSTRLEN);
920Sstevel@tonic-gate 		}
930Sstevel@tonic-gate 		clnames = anon_client(host);
940Sstevel@tonic-gate 	}
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	/*
970Sstevel@tonic-gate 	 * Now find the export
980Sstevel@tonic-gate 	 */
990Sstevel@tonic-gate 	sh = findentry(argp->req_path);
1000Sstevel@tonic-gate 	if (sh == NULL) {
1010Sstevel@tonic-gate 		syslog(LOG_ERR, "%s not exported", argp->req_path);
1020Sstevel@tonic-gate 		goto done;
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	result->auth_perm = check_client(sh, &nbuf, clnames, argp->req_flavor);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	sharefree(sh);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (result->auth_perm == NFSAUTH_DENIED) {
1100Sstevel@tonic-gate 		syslog(LOG_ERR, "%s denied access to %s",
1110Sstevel@tonic-gate 			clnames->h_hostservs[0].h_host, argp->req_path);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate done:
1150Sstevel@tonic-gate 	freenetconfigent(nconf);
1160Sstevel@tonic-gate 	if (clnames)
1170Sstevel@tonic-gate 		netdir_free(clnames, ND_HOSTSERVLIST);
1180Sstevel@tonic-gate }
1192140Srmesta 
1202140Srmesta void
1212140Srmesta nfsauth_func(void *cookie, char *dataptr, size_t arg_size,
1222140Srmesta 	door_desc_t *dp, uint_t n_desc)
1232140Srmesta 
1242140Srmesta {
1252140Srmesta 	nfsauth_arg_t	*ap;
1262140Srmesta 	nfsauth_res_t	 res = {0};
1272140Srmesta 	nfsauth_res_t	*rp = &res;
1282140Srmesta 	XDR		 xdrs_a;
1292140Srmesta 	XDR		 xdrs_r;
1302140Srmesta 	caddr_t		 abuf = dataptr;
1312140Srmesta 	size_t		 absz = arg_size;
1322140Srmesta 	size_t		 rbsz = (size_t)(BYTES_PER_XDR_UNIT * 2);
1332140Srmesta 	char		 result[BYTES_PER_XDR_UNIT * 2];
1342140Srmesta 	caddr_t		 rbuf = (caddr_t)&result;
1352140Srmesta 	varg_t		 varg = {0};
1362140Srmesta 
1372140Srmesta 	/*
1382140Srmesta 	 * Decode the inbound door data, so we can look at the cmd.
1392140Srmesta 	 */
1402140Srmesta 	xdrmem_create(&xdrs_a, abuf, absz, XDR_DECODE);
1412140Srmesta 	if (!xdr_varg(&xdrs_a, &varg)) {
1422140Srmesta 		/*
1432140Srmesta 		 * If the arguments can't be decoded, bail.
1442140Srmesta 		 */
1452140Srmesta 		if (varg.vers == V_ERROR)
1462140Srmesta 			syslog(LOG_ERR, gettext("Arg version mismatch"));
1472140Srmesta 		res.stat = NFSAUTH_DR_DECERR;
1482140Srmesta 		goto encres;
1492140Srmesta 	}
1502140Srmesta 
1512140Srmesta 	/*
1522140Srmesta 	 * Now set the args pointer to the proper version of the args
1532140Srmesta 	 */
1542140Srmesta 	switch (varg.vers) {
1552140Srmesta 	case V_PROTO:
1562140Srmesta 		ap = &varg.arg_u.arg;
1572140Srmesta 		break;
1582140Srmesta 
1592140Srmesta 		/* Additional arguments versions go here */
1602140Srmesta 
1612140Srmesta 	default:
1622140Srmesta 		syslog(LOG_ERR, gettext("Invalid args version"));
1632140Srmesta 		goto encres;
1642140Srmesta 	}
1652140Srmesta 
1662140Srmesta 	/*
1672140Srmesta 	 * Call the specified cmd
1682140Srmesta 	 */
1692140Srmesta 	switch (ap->cmd) {
1702140Srmesta 		case NFSAUTH_ACCESS:
1712140Srmesta 			nfsauth_access(&ap->areq, &rp->ares);
1722140Srmesta 			rp->stat = NFSAUTH_DR_OKAY;
1732140Srmesta 			break;
1742140Srmesta 		default:
1752140Srmesta 			rp->stat = NFSAUTH_DR_BADCMD;
1762140Srmesta 			break;
1772140Srmesta 	}
1782140Srmesta 
1792140Srmesta encres:
1802140Srmesta 	/*
1812140Srmesta 	 * Free space used to decode the args
1822140Srmesta 	 */
1832140Srmesta 	xdrs_a.x_op = XDR_FREE;
1842140Srmesta 	(void) xdr_varg(&xdrs_a, &varg);
1852140Srmesta 	xdr_destroy(&xdrs_a);
1862140Srmesta 
1872140Srmesta 	/*
1882140Srmesta 	 * Encode the results before passing thru door.
1892140Srmesta 	 *
1902140Srmesta 	 * The result (nfsauth_res_t) is always two int's, so we don't
1912140Srmesta 	 * have to dynamically size (or allocate) the results buffer.
1922140Srmesta 	 */
1932140Srmesta 	xdrmem_create(&xdrs_r, rbuf, rbsz, XDR_ENCODE);
1942140Srmesta 	if (!xdr_nfsauth_res(&xdrs_r, rp)) {
1952140Srmesta 		/*
1962140Srmesta 		 * return only the status code
1972140Srmesta 		 */
1982140Srmesta 		rp->stat = NFSAUTH_DR_EFAIL;
1992140Srmesta 		rbsz = sizeof (uint_t);
2002140Srmesta 		*rbuf = (uint_t)rp->stat;
2012140Srmesta 	}
2022140Srmesta 	xdr_destroy(&xdrs_r);
2032140Srmesta 
2042140Srmesta 	(void) door_return((char *)rbuf, rbsz, NULL, 0);
2052140Srmesta 	(void) door_return(NULL, 0, NULL, 0);
2062140Srmesta 	/* NOTREACHED */
2072140Srmesta }
208