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 */
213957Sth199096
220Sstevel@tonic-gate /*
23*9804SGerald.Thornbrugh@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
242140Srmesta * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <stdio.h>
280Sstevel@tonic-gate #include <stdlib.h>
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <sys/file.h>
340Sstevel@tonic-gate #include <sys/time.h>
350Sstevel@tonic-gate #include <sys/errno.h>
360Sstevel@tonic-gate #include <rpcsvc/mount.h>
370Sstevel@tonic-gate #include <sys/pathconf.h>
380Sstevel@tonic-gate #include <sys/systeminfo.h>
390Sstevel@tonic-gate #include <sys/utsname.h>
400Sstevel@tonic-gate #include <arpa/inet.h>
410Sstevel@tonic-gate #include <signal.h>
420Sstevel@tonic-gate #include <syslog.h>
430Sstevel@tonic-gate #include <locale.h>
440Sstevel@tonic-gate #include <unistd.h>
450Sstevel@tonic-gate #include <thread.h>
460Sstevel@tonic-gate #include <netdir.h>
472140Srmesta #include <nfs/auth.h>
483957Sth199096 #include <sharefs/share.h>
490Sstevel@tonic-gate #include "../lib/sharetab.h"
500Sstevel@tonic-gate #include "mountd.h"
510Sstevel@tonic-gate
520Sstevel@tonic-gate static void
nfsauth_access(auth_req * argp,auth_res * result)532140Srmesta 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
767387SRobert.Gordon@Sun.COM if (nbuf.len == 0 || nbuf.buf == NULL)
77*9804SGerald.Thornbrugh@Sun.COM goto done;
787387SRobert.Gordon@Sun.COM
790Sstevel@tonic-gate if (netdir_getbyaddr(nconf, &clnames, &nbuf)) {
800Sstevel@tonic-gate host = &tmp[0];
810Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_INET) == 0) {
820Sstevel@tonic-gate struct sockaddr_in *sa;
830Sstevel@tonic-gate
840Sstevel@tonic-gate /* LINTED pointer alignment */
850Sstevel@tonic-gate sa = (struct sockaddr_in *)nbuf.buf;
860Sstevel@tonic-gate (void) inet_ntoa_r(sa->sin_addr, tmp);
870Sstevel@tonic-gate } else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) {
880Sstevel@tonic-gate struct sockaddr_in6 *sa;
890Sstevel@tonic-gate /* LINTED pointer */
900Sstevel@tonic-gate sa = (struct sockaddr_in6 *)nbuf.buf;
910Sstevel@tonic-gate (void) inet_ntop(AF_INET6, sa->sin6_addr.s6_addr,
927387SRobert.Gordon@Sun.COM tmp, INET6_ADDRSTRLEN);
930Sstevel@tonic-gate }
940Sstevel@tonic-gate clnames = anon_client(host);
950Sstevel@tonic-gate }
96*9804SGerald.Thornbrugh@Sun.COM /*
97*9804SGerald.Thornbrugh@Sun.COM * Both netdir_getbyaddr() and anon_client() can return a NULL
98*9804SGerald.Thornbrugh@Sun.COM * clnames. This has been seen when the DNS entry for the client
99*9804SGerald.Thornbrugh@Sun.COM * name does not have the correct format or a reverse lookup DNS
100*9804SGerald.Thornbrugh@Sun.COM * entry cannot be found for the client's IP address.
101*9804SGerald.Thornbrugh@Sun.COM */
102*9804SGerald.Thornbrugh@Sun.COM if (clnames == NULL) {
103*9804SGerald.Thornbrugh@Sun.COM syslog(LOG_ERR, "Could not find DNS entry for %s",
104*9804SGerald.Thornbrugh@Sun.COM argp->req_netid);
105*9804SGerald.Thornbrugh@Sun.COM goto done;
106*9804SGerald.Thornbrugh@Sun.COM }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate * Now find the export
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate sh = findentry(argp->req_path);
1120Sstevel@tonic-gate if (sh == NULL) {
1130Sstevel@tonic-gate syslog(LOG_ERR, "%s not exported", argp->req_path);
1140Sstevel@tonic-gate goto done;
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate result->auth_perm = check_client(sh, &nbuf, clnames, argp->req_flavor);
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate sharefree(sh);
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate if (result->auth_perm == NFSAUTH_DENIED) {
1220Sstevel@tonic-gate syslog(LOG_ERR, "%s denied access to %s",
1237387SRobert.Gordon@Sun.COM clnames->h_hostservs[0].h_host, argp->req_path);
1240Sstevel@tonic-gate }
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate done:
1270Sstevel@tonic-gate freenetconfigent(nconf);
1280Sstevel@tonic-gate if (clnames)
1290Sstevel@tonic-gate netdir_free(clnames, ND_HOSTSERVLIST);
1300Sstevel@tonic-gate }
1312140Srmesta
1322140Srmesta void
nfsauth_func(void * cookie,char * dataptr,size_t arg_size,door_desc_t * dp,uint_t n_desc)1332140Srmesta nfsauth_func(void *cookie, char *dataptr, size_t arg_size,
1342140Srmesta door_desc_t *dp, uint_t n_desc)
1352140Srmesta
1362140Srmesta {
1372140Srmesta nfsauth_arg_t *ap;
1382140Srmesta nfsauth_res_t res = {0};
1392140Srmesta nfsauth_res_t *rp = &res;
1402140Srmesta XDR xdrs_a;
1412140Srmesta XDR xdrs_r;
1422140Srmesta caddr_t abuf = dataptr;
1432140Srmesta size_t absz = arg_size;
1442140Srmesta size_t rbsz = (size_t)(BYTES_PER_XDR_UNIT * 2);
1452140Srmesta char result[BYTES_PER_XDR_UNIT * 2];
1462140Srmesta caddr_t rbuf = (caddr_t)&result;
1472140Srmesta varg_t varg = {0};
1482140Srmesta
1492140Srmesta /*
1502140Srmesta * Decode the inbound door data, so we can look at the cmd.
1512140Srmesta */
1522140Srmesta xdrmem_create(&xdrs_a, abuf, absz, XDR_DECODE);
1532140Srmesta if (!xdr_varg(&xdrs_a, &varg)) {
1542140Srmesta /*
1552140Srmesta * If the arguments can't be decoded, bail.
1562140Srmesta */
1572140Srmesta if (varg.vers == V_ERROR)
1582140Srmesta syslog(LOG_ERR, gettext("Arg version mismatch"));
1592140Srmesta res.stat = NFSAUTH_DR_DECERR;
1602140Srmesta goto encres;
1612140Srmesta }
1622140Srmesta
1632140Srmesta /*
1642140Srmesta * Now set the args pointer to the proper version of the args
1652140Srmesta */
1662140Srmesta switch (varg.vers) {
1672140Srmesta case V_PROTO:
1682140Srmesta ap = &varg.arg_u.arg;
1692140Srmesta break;
1702140Srmesta
1712140Srmesta /* Additional arguments versions go here */
1722140Srmesta
1732140Srmesta default:
1742140Srmesta syslog(LOG_ERR, gettext("Invalid args version"));
1752140Srmesta goto encres;
1762140Srmesta }
1772140Srmesta
1782140Srmesta /*
1792140Srmesta * Call the specified cmd
1802140Srmesta */
1812140Srmesta switch (ap->cmd) {
1822140Srmesta case NFSAUTH_ACCESS:
1832140Srmesta nfsauth_access(&ap->areq, &rp->ares);
1842140Srmesta rp->stat = NFSAUTH_DR_OKAY;
1852140Srmesta break;
1862140Srmesta default:
1872140Srmesta rp->stat = NFSAUTH_DR_BADCMD;
1882140Srmesta break;
1892140Srmesta }
1902140Srmesta
1912140Srmesta encres:
1922140Srmesta /*
1932140Srmesta * Free space used to decode the args
1942140Srmesta */
1952140Srmesta xdrs_a.x_op = XDR_FREE;
1962140Srmesta (void) xdr_varg(&xdrs_a, &varg);
1972140Srmesta xdr_destroy(&xdrs_a);
1982140Srmesta
1992140Srmesta /*
2002140Srmesta * Encode the results before passing thru door.
2012140Srmesta *
2022140Srmesta * The result (nfsauth_res_t) is always two int's, so we don't
2032140Srmesta * have to dynamically size (or allocate) the results buffer.
2042140Srmesta */
2052140Srmesta xdrmem_create(&xdrs_r, rbuf, rbsz, XDR_ENCODE);
2062140Srmesta if (!xdr_nfsauth_res(&xdrs_r, rp)) {
2072140Srmesta /*
2082140Srmesta * return only the status code
2092140Srmesta */
2102140Srmesta rp->stat = NFSAUTH_DR_EFAIL;
2112140Srmesta rbsz = sizeof (uint_t);
2122140Srmesta *rbuf = (uint_t)rp->stat;
2132140Srmesta }
2142140Srmesta xdr_destroy(&xdrs_r);
2152140Srmesta
2162140Srmesta (void) door_return((char *)rbuf, rbsz, NULL, 0);
2172140Srmesta (void) door_return(NULL, 0, NULL, 0);
2182140Srmesta /* NOTREACHED */
2192140Srmesta }
220