Home | History | Annotate | Line # | Download | only in client
      1 /*	$NetBSD: nfs_clkrpc.c,v 1.3 2024/07/05 04:31:52 rin Exp $	*/
      2 /*-
      3  * Copyright (c) 1989, 1993
      4  *	The Regents of the University of California.  All rights reserved.
      5  *
      6  * This code is derived from software contributed to Berkeley by
      7  * Rick Macklem at The University of Guelph.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  */
     34 
     35 #include <sys/cdefs.h>
     36 /* __FBSDID("FreeBSD: head/sys/fs/nfsclient/nfs_clkrpc.c 255216 2013-09-04 22:47:56Z rmacklem "); */
     37 __RCSID("$NetBSD: nfs_clkrpc.c,v 1.3 2024/07/05 04:31:52 rin Exp $");
     38 
     39 #ifdef NOTYET /* _KERNEL_OPT */
     40 #include "opt_kgssapi.h"
     41 #endif
     42 
     43 #include <fs/nfs/common/nfsport.h>
     44 
     45 #include <rpc/rpc.h>
     46 #include <rpc/rpcsec_gss.h>
     47 #include <rpc/replay.h>
     48 
     49 
     50 NFSDLOCKMUTEX;
     51 
     52 extern SVCPOOL	*nfscbd_pool;
     53 
     54 static int nfs_cbproc(struct nfsrv_descript *, u_int32_t);
     55 
     56 extern u_long sb_max_adj;
     57 extern int nfs_numnfscbd;
     58 extern int nfscl_debuglevel;
     59 
     60 /*
     61  * NFS client system calls for handling callbacks.
     62  */
     63 
     64 /*
     65  * Handles server to client callbacks.
     66  */
     67 static void
     68 nfscb_program(struct svc_req *rqst, SVCXPRT *xprt)
     69 {
     70 	struct nfsrv_descript nd;
     71 	int cacherep, credflavor;
     72 
     73 	memset(&nd, 0, sizeof(nd));
     74 	if (rqst->rq_proc != NFSPROC_NULL &&
     75 	    rqst->rq_proc != NFSV4PROC_CBCOMPOUND) {
     76 		svcerr_noproc(rqst);
     77 		svc_freereq(rqst);
     78 		return;
     79 	}
     80 	nd.nd_procnum = rqst->rq_proc;
     81 	nd.nd_flag = (ND_NFSCB | ND_NFSV4);
     82 
     83 	/*
     84 	 * Note: we want rq_addr, not svc_getrpccaller for nd_nam2 -
     85 	 * NFS_SRVMAXDATA uses a NULL value for nd_nam2 to detect TCP
     86 	 * mounts.
     87 	 */
     88 	nd.nd_mrep = rqst->rq_args;
     89 	rqst->rq_args = NULL;
     90 	newnfs_realign(&nd.nd_mrep, M_WAITOK);
     91 	nd.nd_md = nd.nd_mrep;
     92 	nd.nd_dpos = mtod(nd.nd_md, caddr_t);
     93 	nd.nd_nam = svc_getrpccaller(rqst);
     94 	nd.nd_nam2 = rqst->rq_addr;
     95 	nd.nd_mreq = NULL;
     96 	nd.nd_cred = NULL;
     97 
     98 	NFSCL_DEBUG(1, "cbproc=%d\n",nd.nd_procnum);
     99 	if (nd.nd_procnum != NFSPROC_NULL) {
    100 		if (!svc_getcred(rqst, &nd.nd_cred, &credflavor)) {
    101 			svcerr_weakauth(rqst);
    102 			svc_freereq(rqst);
    103 			m_freem(nd.nd_mrep);
    104 			return;
    105 		}
    106 
    107 		/* For now, I don't care what credential flavor was used. */
    108 #ifdef notyet
    109 #ifdef MAC
    110 		mac_cred_associate_nfsd(nd.nd_cred);
    111 #endif
    112 #endif
    113 		cacherep = nfs_cbproc(&nd, rqst->rq_xid);
    114 	} else {
    115 		NFSMGET(nd.nd_mreq);
    116 		nd.nd_mreq->m_len = 0;
    117 		cacherep = RC_REPLY;
    118 	}
    119 	m_freem(nd.nd_mrep);
    120 
    121 	if (nd.nd_cred != NULL)
    122 		crfree(nd.nd_cred);
    123 
    124 	if (cacherep == RC_DROPIT) {
    125 		m_freem(nd.nd_mreq);
    126 		svc_freereq(rqst);
    127 		return;
    128 	}
    129 
    130 	if (nd.nd_mreq == NULL) {
    131 		svcerr_decode(rqst);
    132 		svc_freereq(rqst);
    133 		return;
    134 	}
    135 
    136 	if (nd.nd_repstat & NFSERR_AUTHERR) {
    137 		svcerr_auth(rqst, nd.nd_repstat & ~NFSERR_AUTHERR);
    138 		m_freem(nd.nd_mreq);
    139 	} else if (!svc_sendreply_mbuf(rqst, nd.nd_mreq))
    140 		svcerr_systemerr(rqst);
    141 	else
    142 		NFSCL_DEBUG(1, "cbrep sent\n");
    143 	svc_freereq(rqst);
    144 }
    145 
    146 /*
    147  * Check the cache and, optionally, do the RPC.
    148  * Return the appropriate cache response.
    149  */
    150 static int
    151 nfs_cbproc(struct nfsrv_descript *nd, u_int32_t xid)
    152 {
    153 	struct thread *td = curthread;
    154 	int cacherep;
    155 
    156 	if (nd->nd_nam2 == NULL)
    157 		nd->nd_flag |= ND_STREAMSOCK;
    158 
    159 	nfscl_docb(nd, td);
    160 	if (nd->nd_repstat == NFSERR_DONTREPLY)
    161 		cacherep = RC_DROPIT;
    162 	else
    163 		cacherep = RC_REPLY;
    164 	return (cacherep);
    165 }
    166 
    167 /*
    168  * Adds a socket to the list for servicing by nfscbds.
    169  */
    170 int
    171 nfscbd_addsock(struct file *fp)
    172 {
    173 	int siz;
    174 	struct socket *so;
    175 	int error;
    176 	SVCXPRT *xprt;
    177 
    178 	so = fp->f_data;
    179 
    180 	siz = sb_max_adj;
    181 	error = soreserve(so, siz, siz);
    182 	if (error)
    183 		return (error);
    184 
    185 	/*
    186 	 * Steal the socket from userland so that it doesn't close
    187 	 * unexpectedly.
    188 	 */
    189 	if (so->so_type == SOCK_DGRAM)
    190 		xprt = svc_dg_create(nfscbd_pool, so, 0, 0);
    191 	else
    192 		xprt = svc_vc_create(nfscbd_pool, so, 0, 0);
    193 	if (xprt) {
    194 		fp->f_ops = &badfileops;
    195 		fp->f_data = NULL;
    196 		svc_reg(xprt, NFS_CALLBCKPROG, NFSV4_CBVERS, nfscb_program,
    197 		    NULL);
    198 		SVC_RELEASE(xprt);
    199 	}
    200 
    201 	return (0);
    202 }
    203 
    204 /*
    205  * Called by nfssvc() for nfscbds. Just loops around servicing rpc requests
    206  * until it is killed by a signal.
    207  *
    208  * For now, only support callbacks via RPCSEC_GSS if there is a KerberosV
    209  * keytab entry with a host based entry in it on the client. (I'm not even
    210  * sure that getting Acceptor credentials for a user principal with a
    211  * credentials cache is possible, but even if it is, major changes to the
    212  * kgssapi would be required.)
    213  * I don't believe that this is a serious limitation since, as of 2009, most
    214  * NFSv4 servers supporting callbacks are using AUTH_SYS for callbacks even
    215  * when the client is using RPCSEC_GSS. (This BSD server uses AUTH_SYS
    216  * for callbacks unless nfsrv_gsscallbackson is set non-zero.)
    217  */
    218 int
    219 nfscbd_nfsd(struct thread *td, struct nfsd_nfscbd_args *args)
    220 {
    221 	char principal[128];
    222 	int error;
    223 
    224 	if (args != NULL) {
    225 		error = copyinstr(args->principal, principal,
    226 		    sizeof(principal), NULL);
    227 		if (error)
    228 			return (error);
    229 	} else {
    230 		principal[0] = '\0';
    231 	}
    232 
    233 	/*
    234 	 * Only the first nfsd actually does any work. The RPC code
    235 	 * adds threads to it as needed. Any extra processes offered
    236 	 * by nfsd just exit. If nfsd is new enough, it will call us
    237 	 * once with a structure that specifies how many threads to
    238 	 * use.
    239 	 */
    240 	NFSD_LOCK();
    241 	if (nfs_numnfscbd == 0) {
    242 		nfs_numnfscbd++;
    243 
    244 		NFSD_UNLOCK();
    245 
    246 		if (principal[0] != '\0')
    247 			rpc_gss_set_svc_name_call(principal, "kerberosv5",
    248 			    GSS_C_INDEFINITE, NFS_CALLBCKPROG, NFSV4_CBVERS);
    249 
    250 		nfscbd_pool->sp_minthreads = 4;
    251 		nfscbd_pool->sp_maxthreads = 4;
    252 
    253 		svc_run(nfscbd_pool);
    254 
    255 		rpc_gss_clear_svc_name_call(NFS_CALLBCKPROG, NFSV4_CBVERS);
    256 
    257 		NFSD_LOCK();
    258 		nfs_numnfscbd--;
    259 		nfsrvd_cbinit(1);
    260 	}
    261 	NFSD_UNLOCK();
    262 
    263 	return (0);
    264 }
    265 
    266 /*
    267  * Initialize the data structures for the server.
    268  * Handshake with any new nfsds starting up to avoid any chance of
    269  * corruption.
    270  */
    271 void
    272 nfsrvd_cbinit(int terminating)
    273 {
    274 
    275 	NFSD_LOCK_ASSERT();
    276 
    277 	if (terminating) {
    278 		/* Wait for any xprt registrations to complete. */
    279 		while (nfs_numnfscbd > 0)
    280 			msleep(&nfs_numnfscbd, NFSDLOCKMUTEXPTR, PZERO,
    281 			    "nfscbdt", 0);
    282 	}
    283 
    284 	if (nfscbd_pool == NULL) {
    285 		NFSD_UNLOCK();
    286 		nfscbd_pool = svcpool_create("nfscbd", NULL);
    287 		nfscbd_pool->sp_rcache = NULL;
    288 		nfscbd_pool->sp_assign = NULL;
    289 		nfscbd_pool->sp_done = NULL;
    290 		NFSD_LOCK();
    291 	}
    292 }
    293 
    294