xref: /illumos-gate/usr/src/cmd/fs.d/nfs/mountd/nfsauth.c (revision 0a701b1ec2b55bddc48b62124df936152ff820f7)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51cc55349Srmesta  * Common Development and Distribution License (the "License").
61cc55349Srmesta  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21a237e38eSth 
227c478bd9Sstevel@tonic-gate /*
23*0a701b1eSRobert Gordon  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
241cc55349Srmesta  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/file.h>
347c478bd9Sstevel@tonic-gate #include <sys/time.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <rpcsvc/mount.h>
377c478bd9Sstevel@tonic-gate #include <sys/pathconf.h>
387c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
397c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
407c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
417c478bd9Sstevel@tonic-gate #include <signal.h>
427c478bd9Sstevel@tonic-gate #include <syslog.h>
437c478bd9Sstevel@tonic-gate #include <locale.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <thread.h>
467c478bd9Sstevel@tonic-gate #include <netdir.h>
471cc55349Srmesta #include <nfs/auth.h>
48a237e38eSth #include <sharefs/share.h>
497c478bd9Sstevel@tonic-gate #include "../lib/sharetab.h"
507c478bd9Sstevel@tonic-gate #include "mountd.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static void
531cc55349Srmesta nfsauth_access(auth_req *argp, auth_res *result)
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
567c478bd9Sstevel@tonic-gate 	struct nd_hostservlist *clnames = NULL;
577c478bd9Sstevel@tonic-gate 	struct netbuf nbuf;
587c478bd9Sstevel@tonic-gate 	struct share *sh;
597c478bd9Sstevel@tonic-gate 	char tmp[MAXIPADDRLEN];
607c478bd9Sstevel@tonic-gate 	char *host = NULL;
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	result->auth_perm = NFSAUTH_DENIED;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	/*
657c478bd9Sstevel@tonic-gate 	 * Convert the client's address to a hostname
667c478bd9Sstevel@tonic-gate 	 */
677c478bd9Sstevel@tonic-gate 	nconf = getnetconfigent(argp->req_netid);
687c478bd9Sstevel@tonic-gate 	if (nconf == NULL) {
697c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "No netconfig entry for %s", argp->req_netid);
707c478bd9Sstevel@tonic-gate 		return;
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	nbuf.len = argp->req_client.n_len;
747c478bd9Sstevel@tonic-gate 	nbuf.buf = argp->req_client.n_bytes;
757c478bd9Sstevel@tonic-gate 
76*0a701b1eSRobert Gordon 	if (nbuf.len == 0 || nbuf.buf == NULL)
77*0a701b1eSRobert Gordon 		return;
78*0a701b1eSRobert Gordon 
797c478bd9Sstevel@tonic-gate 	if (netdir_getbyaddr(nconf, &clnames, &nbuf)) {
807c478bd9Sstevel@tonic-gate 		host = &tmp[0];
817c478bd9Sstevel@tonic-gate 		if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
827c478bd9Sstevel@tonic-gate 			struct sockaddr_in *sa;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 			/* LINTED pointer alignment */
857c478bd9Sstevel@tonic-gate 			sa = (struct sockaddr_in *)nbuf.buf;
867c478bd9Sstevel@tonic-gate 			(void) inet_ntoa_r(sa->sin_addr, tmp);
877c478bd9Sstevel@tonic-gate 		} else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) {
887c478bd9Sstevel@tonic-gate 			struct sockaddr_in6 *sa;
897c478bd9Sstevel@tonic-gate 			/* LINTED pointer */
907c478bd9Sstevel@tonic-gate 			sa = (struct sockaddr_in6 *)nbuf.buf;
917c478bd9Sstevel@tonic-gate 			(void) inet_ntop(AF_INET6, sa->sin6_addr.s6_addr,
92*0a701b1eSRobert Gordon 			    tmp, INET6_ADDRSTRLEN);
937c478bd9Sstevel@tonic-gate 		}
947c478bd9Sstevel@tonic-gate 		clnames = anon_client(host);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * Now find the export
997c478bd9Sstevel@tonic-gate 	 */
1007c478bd9Sstevel@tonic-gate 	sh = findentry(argp->req_path);
1017c478bd9Sstevel@tonic-gate 	if (sh == NULL) {
1027c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s not exported", argp->req_path);
1037c478bd9Sstevel@tonic-gate 		goto done;
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	result->auth_perm = check_client(sh, &nbuf, clnames, argp->req_flavor);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	sharefree(sh);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (result->auth_perm == NFSAUTH_DENIED) {
1117c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "%s denied access to %s",
112*0a701b1eSRobert Gordon 		    clnames->h_hostservs[0].h_host, argp->req_path);
1137c478bd9Sstevel@tonic-gate 	}
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate done:
1167c478bd9Sstevel@tonic-gate 	freenetconfigent(nconf);
1177c478bd9Sstevel@tonic-gate 	if (clnames)
1187c478bd9Sstevel@tonic-gate 		netdir_free(clnames, ND_HOSTSERVLIST);
1197c478bd9Sstevel@tonic-gate }
1201cc55349Srmesta 
1211cc55349Srmesta void
1221cc55349Srmesta nfsauth_func(void *cookie, char *dataptr, size_t arg_size,
1231cc55349Srmesta 	door_desc_t *dp, uint_t n_desc)
1241cc55349Srmesta 
1251cc55349Srmesta {
1261cc55349Srmesta 	nfsauth_arg_t	*ap;
1271cc55349Srmesta 	nfsauth_res_t	 res = {0};
1281cc55349Srmesta 	nfsauth_res_t	*rp = &res;
1291cc55349Srmesta 	XDR		 xdrs_a;
1301cc55349Srmesta 	XDR		 xdrs_r;
1311cc55349Srmesta 	caddr_t		 abuf = dataptr;
1321cc55349Srmesta 	size_t		 absz = arg_size;
1331cc55349Srmesta 	size_t		 rbsz = (size_t)(BYTES_PER_XDR_UNIT * 2);
1341cc55349Srmesta 	char		 result[BYTES_PER_XDR_UNIT * 2];
1351cc55349Srmesta 	caddr_t		 rbuf = (caddr_t)&result;
1361cc55349Srmesta 	varg_t		 varg = {0};
1371cc55349Srmesta 
1381cc55349Srmesta 	/*
1391cc55349Srmesta 	 * Decode the inbound door data, so we can look at the cmd.
1401cc55349Srmesta 	 */
1411cc55349Srmesta 	xdrmem_create(&xdrs_a, abuf, absz, XDR_DECODE);
1421cc55349Srmesta 	if (!xdr_varg(&xdrs_a, &varg)) {
1431cc55349Srmesta 		/*
1441cc55349Srmesta 		 * If the arguments can't be decoded, bail.
1451cc55349Srmesta 		 */
1461cc55349Srmesta 		if (varg.vers == V_ERROR)
1471cc55349Srmesta 			syslog(LOG_ERR, gettext("Arg version mismatch"));
1481cc55349Srmesta 		res.stat = NFSAUTH_DR_DECERR;
1491cc55349Srmesta 		goto encres;
1501cc55349Srmesta 	}
1511cc55349Srmesta 
1521cc55349Srmesta 	/*
1531cc55349Srmesta 	 * Now set the args pointer to the proper version of the args
1541cc55349Srmesta 	 */
1551cc55349Srmesta 	switch (varg.vers) {
1561cc55349Srmesta 	case V_PROTO:
1571cc55349Srmesta 		ap = &varg.arg_u.arg;
1581cc55349Srmesta 		break;
1591cc55349Srmesta 
1601cc55349Srmesta 		/* Additional arguments versions go here */
1611cc55349Srmesta 
1621cc55349Srmesta 	default:
1631cc55349Srmesta 		syslog(LOG_ERR, gettext("Invalid args version"));
1641cc55349Srmesta 		goto encres;
1651cc55349Srmesta 	}
1661cc55349Srmesta 
1671cc55349Srmesta 	/*
1681cc55349Srmesta 	 * Call the specified cmd
1691cc55349Srmesta 	 */
1701cc55349Srmesta 	switch (ap->cmd) {
1711cc55349Srmesta 		case NFSAUTH_ACCESS:
1721cc55349Srmesta 			nfsauth_access(&ap->areq, &rp->ares);
1731cc55349Srmesta 			rp->stat = NFSAUTH_DR_OKAY;
1741cc55349Srmesta 			break;
1751cc55349Srmesta 		default:
1761cc55349Srmesta 			rp->stat = NFSAUTH_DR_BADCMD;
1771cc55349Srmesta 			break;
1781cc55349Srmesta 	}
1791cc55349Srmesta 
1801cc55349Srmesta encres:
1811cc55349Srmesta 	/*
1821cc55349Srmesta 	 * Free space used to decode the args
1831cc55349Srmesta 	 */
1841cc55349Srmesta 	xdrs_a.x_op = XDR_FREE;
1851cc55349Srmesta 	(void) xdr_varg(&xdrs_a, &varg);
1861cc55349Srmesta 	xdr_destroy(&xdrs_a);
1871cc55349Srmesta 
1881cc55349Srmesta 	/*
1891cc55349Srmesta 	 * Encode the results before passing thru door.
1901cc55349Srmesta 	 *
1911cc55349Srmesta 	 * The result (nfsauth_res_t) is always two int's, so we don't
1921cc55349Srmesta 	 * have to dynamically size (or allocate) the results buffer.
1931cc55349Srmesta 	 */
1941cc55349Srmesta 	xdrmem_create(&xdrs_r, rbuf, rbsz, XDR_ENCODE);
1951cc55349Srmesta 	if (!xdr_nfsauth_res(&xdrs_r, rp)) {
1961cc55349Srmesta 		/*
1971cc55349Srmesta 		 * return only the status code
1981cc55349Srmesta 		 */
1991cc55349Srmesta 		rp->stat = NFSAUTH_DR_EFAIL;
2001cc55349Srmesta 		rbsz = sizeof (uint_t);
2011cc55349Srmesta 		*rbuf = (uint_t)rp->stat;
2021cc55349Srmesta 	}
2031cc55349Srmesta 	xdr_destroy(&xdrs_r);
2041cc55349Srmesta 
2051cc55349Srmesta 	(void) door_return((char *)rbuf, rbsz, NULL, 0);
2061cc55349Srmesta 	(void) door_return(NULL, 0, NULL, 0);
2071cc55349Srmesta 	/* NOTREACHED */
2081cc55349Srmesta }
209