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