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