1 /*        $NetBSD: nfs_nfssvc.c,v 1.2 2016/12/13 22:41:46 pgoyette 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/nfs/nfs_nfssvc.c 298788 2016-04-29 16:07:25Z pfg "); */
37 __RCSID("$NetBSD: nfs_nfssvc.c,v 1.2 2016/12/13 22:41:46 pgoyette Exp $");
38 
39 #ifdef _KERNEL_OPT
40 #include "opt_newnfs.h"
41 #endif
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/sysproto.h>
46 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/priv.h>
49 #include <sys/proc.h>
50 #include <sys/lock.h>
51 #include <sys/mutex.h>
52 #include <sys/module.h>
53 #include <sys/sysent.h>
54 #include <sys/syscall.h>
55 
56 #include <security/audit/audit.h>
57 
58 #include <fs/nfs/common/nfssvc.h>
59 
60 static int nfssvc_offset = SYS_nfssvc;
61 static struct sysent nfssvc_prev_sysent;
62 MAKE_SYSENT(nfssvc);
63 
64 /*
65  * This tiny module simply handles the nfssvc() system call. The other
66  * nfs modules that use the system call register themselves by setting
67  * the nfsd_call_xxx function pointers non-NULL.
68  */
69 
70 int (*nfsd_call_nfsserver)(struct thread *, struct nfssvc_args *) = NULL;
71 int (*nfsd_call_nfscommon)(struct thread *, struct nfssvc_args *) = NULL;
72 int (*nfsd_call_nfscl)(struct thread *, struct nfssvc_args *) = NULL;
73 int (*nfsd_call_nfsd)(struct thread *, struct nfssvc_args *) = NULL;
74 
75 /*
76  * Nfs server pseudo system call for the nfsd's
77  */
78 int
sys_nfssvc(struct thread * td,struct nfssvc_args * uap)79 sys_nfssvc(struct thread *td, struct nfssvc_args *uap)
80 {
81           int error;
82 
83           KASSERT(!mtx_owned(&Giant), ("nfssvc(): called with Giant"));
84 
85           AUDIT_ARG_CMD(uap->flag);
86 
87           /* Allow anyone to get the stats. */
88           if ((uap->flag & ~NFSSVC_GETSTATS) != 0) {
89                     error = priv_check(td, PRIV_NFS_DAEMON);
90                     if (error != 0)
91                               return (error);
92           }
93           error = EINVAL;
94           if ((uap->flag & (NFSSVC_ADDSOCK | NFSSVC_OLDNFSD | NFSSVC_NFSD)) &&
95               nfsd_call_nfsserver != NULL)
96                     error = (*nfsd_call_nfsserver)(td, uap);
97           else if ((uap->flag & (NFSSVC_CBADDSOCK | NFSSVC_NFSCBD |
98               NFSSVC_DUMPMNTOPTS)) && nfsd_call_nfscl != NULL)
99                     error = (*nfsd_call_nfscl)(td, uap);
100           else if ((uap->flag & (NFSSVC_IDNAME | NFSSVC_GETSTATS |
101               NFSSVC_GSSDADDPORT | NFSSVC_GSSDADDFIRST | NFSSVC_GSSDDELETEALL |
102               NFSSVC_NFSUSERDPORT | NFSSVC_NFSUSERDDELPORT)) &&
103               nfsd_call_nfscommon != NULL)
104                     error = (*nfsd_call_nfscommon)(td, uap);
105           else if ((uap->flag & (NFSSVC_NFSDNFSD | NFSSVC_NFSDADDSOCK |
106               NFSSVC_PUBLICFH | NFSSVC_V4ROOTEXPORT | NFSSVC_NOPUBLICFH |
107               NFSSVC_STABLERESTART | NFSSVC_ADMINREVOKE |
108               NFSSVC_DUMPCLIENTS | NFSSVC_DUMPLOCKS | NFSSVC_BACKUPSTABLE |
109               NFSSVC_SUSPENDNFSD | NFSSVC_RESUMENFSD)) &&
110               nfsd_call_nfsd != NULL)
111                     error = (*nfsd_call_nfsd)(td, uap);
112           if (error == EINTR || error == ERESTART)
113                     error = 0;
114           return (error);
115 }
116 
117 /*
118  * Called once to initialize data structures...
119  */
120 static int
nfssvc_modevent(module_t mod,int type,void * data)121 nfssvc_modevent(module_t mod, int type, void *data)
122 {
123           static int registered;
124           int error = 0;
125 
126           switch (type) {
127           case MOD_LOAD:
128                     error = syscall_register(&nfssvc_offset, &nfssvc_sysent,
129                         &nfssvc_prev_sysent, SY_THR_STATIC_KLD);
130                     if (error)
131                               break;
132                     registered = 1;
133                     break;
134 
135           case MOD_UNLOAD:
136                     if (nfsd_call_nfsserver != NULL || nfsd_call_nfscommon != NULL
137                         || nfsd_call_nfscl != NULL || nfsd_call_nfsd != NULL) {
138                               error = EBUSY;
139                               break;
140                     }
141                     if (registered)
142                               syscall_deregister(&nfssvc_offset, &nfssvc_prev_sysent);
143                     registered = 0;
144                     break;
145           default:
146                     error = EOPNOTSUPP;
147                     break;
148           }
149           return error;
150 }
151 static moduledata_t nfssvc_mod = {
152           "nfssvc",
153           nfssvc_modevent,
154           NULL,
155 };
156 DECLARE_MODULE(nfssvc, nfssvc_mod, SI_SUB_VFS, SI_ORDER_ANY);
157 
158 /* So that loader and kldload(2) can find us, wherever we are.. */
159 MODULE_VERSION(nfssvc, 1);
160 
161