 |
|
|
| |
1 /*
2 * Copyright (c) 1997-2005 Erez Zadok
3 * Copyright (c) 1989 Jan-Simon Pendry
4 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1989 The Regents of the University of California.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 *
40 * File: am-utils/amd/nfs_prot_svc.c
41 *
42 */
43
44 #ifdef HAVE_CONFIG_H
45 # include <config.h>
46 #endif /* HAVE_CONFIG_H */
47 #include <am_defs.h>
48 #include <amd.h>
49
50 /* external definitions */
51 extern voidp nfsproc_null_2_svc(voidp, struct svc_req *);
52 extern nfsattrstat *nfsproc_getattr_2_svc(am_nfs_fh *, struct svc_req *);
53 extern nfsattrstat *nfsproc_setattr_2_svc(nfssattrargs *, struct svc_req *);
54 extern voidp nfsproc_root_2_svc(voidp, struct svc_req *);
55 extern nfsdiropres *nfsproc_lookup_2_svc(nfsdiropargs *, struct svc_req *);
56 extern nfsreadlinkres *nfsproc_readlink_2_svc(am_nfs_fh *, struct svc_req *);
57 extern nfsreadres *nfsproc_read_2_svc(nfsreadargs *, struct svc_req *);
58 extern voidp nfsproc_writecache_2_svc(voidp, struct svc_req *);
59 extern nfsattrstat *nfsproc_write_2_svc(nfswriteargs *, struct svc_req *);
60 extern nfsdiropres *nfsproc_create_2_svc(nfscreateargs *, struct svc_req *);
61 extern nfsstat *nfsproc_remove_2_svc(nfsdiropargs *, struct svc_req *);
62 extern nfsstat *nfsproc_rename_2_svc(nfsrenameargs *, struct svc_req *);
63 extern nfsstat *nfsproc_link_2_svc(nfslinkargs *, struct svc_req *);
64 extern nfsstat *nfsproc_symlink_2_svc(nfssymlinkargs *, struct svc_req *);
65 extern nfsdiropres *nfsproc_mkdir_2_svc(nfscreateargs *, struct svc_req *);
66 extern nfsstat *nfsproc_rmdir_2_svc(nfsdiropargs *, struct svc_req *);
67 extern nfsreaddirres *nfsproc_readdir_2_svc(nfsreaddirargs *, struct svc_req *);
68 extern nfsstatfsres *nfsproc_statfs_2_svc(am_nfs_fh *, struct svc_req *);
69
70 /* global variables */
71 SVCXPRT *current_transp;
72
73 /* typedefs */
74 typedef char *(*nfssvcproc_t)(voidp, struct svc_req *);
75
76
77 void
78 nfs_program_2(struct svc_req *rqstp, SVCXPRT *transp)
79 {
80 union {
81 am_nfs_fh nfsproc_getattr_2_arg;
82 nfssattrargs nfsproc_setattr_2_arg;
83 nfsdiropargs nfsproc_lookup_2_arg;
84 am_nfs_fh nfsproc_readlink_2_arg;
85 nfsreadargs nfsproc_read_2_arg;
86 nfswriteargs nfsproc_write_2_arg;
87 nfscreateargs nfsproc_create_2_arg;
88 nfsdiropargs nfsproc_remove_2_arg;
89 nfsrenameargs nfsproc_rename_2_arg;
90 nfslinkargs nfsproc_link_2_arg;
91 nfssymlinkargs nfsproc_symlink_2_arg;
92 nfscreateargs nfsproc_mkdir_2_arg;
93 nfsdiropargs fsproc_rmdir_2_arg;
94 nfsreaddirargs nfsproc_readdir_2_arg;
95 am_nfs_fh nfsproc_statfs_2_arg;
96 } argument;
97 char *result;
98 xdrproc_t xdr_argument, xdr_result;
99 nfssvcproc_t local;
100
101 #ifdef HAVE_TRANSPORT_TYPE_TLI
102 /*
103 * On TLI systems we don't use an INET network type, but a "ticlts" (see
104 * /etc/netconfig and conf/transp_tli.c:create_nfs_service). This means
105 * that packets could only come from the loopback interface, and we don't
106 * need to check them and filter possibly spoofed packets. Therefore we
107 * only need to check if the UID caller is correct.
108 */
109 # ifdef HAVE___RPC_GET_LOCAL_UID
110 uid_t u;
111 /* extern definition for an internal libnsl function */
112 extern int __rpc_get_local_uid(SVCXPRT *transp, uid_t *uid);
113 if (__rpc_get_local_uid(transp, &u) >= 0 && u != 0) {
114 plog(XLOG_WARNING, "ignoring request from UID %ld, must be 0", (long) u);
115 return;
116 }
117 # else /* not HAVE___RPC_GET_LOCAL_UID */
118 dlog("cannot verify local uid for rpc request");
119 # endif /* HAVE___RPC_GET_LOCAL_UID */
120 #else /* not HAVE_TRANPORT_TYPE_TLI */
121 struct sockaddr_in *sinp;
122 char dq[20], dq2[28];
123 sinp = amu_svc_getcaller(rqstp->rq_xprt);
124 # ifdef MNT2_NFS_OPT_RESVPORT
125 /* Verify that the request comes from a reserved port */
126 if (sinp &&
127 ntohs(sinp->sin_port) >= IPPORT_RESERVED &&
128 !(gopt.flags & CFM_NFS_INSECURE_PORT)) {
129 plog(XLOG_WARNING, "ignoring request from %s:%u, port not reserved",
130 inet_dquad(dq, sinp->sin_addr.s_addr),
131 ntohs(sinp->sin_port));
132 return;
133 }
134 # endif /* MNT2_NFS_OPT_RESVPORT */
135 /* if the address does not match, ignore the request */
136 if (sinp && sinp->sin_addr.s_addr != myipaddr.s_addr) {
137 plog(XLOG_WARNING, "ignoring request from %s:%u, expected %s",
138 inet_dquad(dq, sinp->sin_addr.s_addr),
139 ntohs(sinp->sin_port),
140 inet_dquad(dq2, myipaddr.s_addr));
141 return;
142 }
143 #endif /* not HAVE_TRANPORT_TYPE_TLI */
144
145 current_transp = NULL;
146
147 switch (rqstp->rq_proc) {
148
149 case NFSPROC_NULL:
150 xdr_argument = (xdrproc_t) xdr_void;
151 xdr_result = (xdrproc_t) xdr_void;
152 local = (nfssvcproc_t) nfsproc_null_2_svc;
153 break;
154
155 case NFSPROC_GETATTR:
156 xdr_argument = (xdrproc_t) xdr_nfs_fh;
157 xdr_result = (xdrproc_t) xdr_attrstat;
158 local = (nfssvcproc_t) nfsproc_getattr_2_svc;
159 break;
160
161 case NFSPROC_SETATTR:
162 xdr_argument = (xdrproc_t) xdr_sattrargs;
163 xdr_result = (xdrproc_t) xdr_attrstat;
164 local = (nfssvcproc_t) nfsproc_setattr_2_svc;
165 break;
166
167 case NFSPROC_ROOT:
168 xdr_argument = (xdrproc_t) xdr_void;
169 xdr_result = (xdrproc_t) xdr_void;
170 local = (nfssvcproc_t) nfsproc_root_2_svc;
171 break;
172
173 case NFSPROC_LOOKUP:
174 xdr_argument = (xdrproc_t) xdr_diropargs;
175 xdr_result = (xdrproc_t) xdr_diropres;
176 local = (nfssvcproc_t) nfsproc_lookup_2_svc;
177 /*
178 * Cheap way to pass transp down to amfs_auto_lookuppn so it can
179 * be stored in the am_node structure and later used for
180 * quick_reply().
181 */
182 current_transp = transp;
183 break;
184
185 case NFSPROC_READLINK:
186 xdr_argument = (xdrproc_t) xdr_nfs_fh;
187 xdr_result = (xdrproc_t) xdr_readlinkres;
188 local = (nfssvcproc_t) nfsproc_readlink_2_svc;
189 break;
190
191 case NFSPROC_READ:
192 xdr_argument = (xdrproc_t) xdr_readargs;
193 xdr_result = (xdrproc_t) xdr_readres;
194 local = (nfssvcproc_t) nfsproc_read_2_svc;
195 break;
196
197 case NFSPROC_WRITECACHE:
198 xdr_argument = (xdrproc_t) xdr_void;
199 xdr_result = (xdrproc_t) xdr_void;
200 local = (nfssvcproc_t) nfsproc_writecache_2_svc;
201 break;
202
203 case NFSPROC_WRITE:
204 xdr_argument = (xdrproc_t) xdr_writeargs;
205 xdr_result = (xdrproc_t) xdr_attrstat;
206 local = (nfssvcproc_t) nfsproc_write_2_svc;
207 break;
208
209 case NFSPROC_CREATE:
210 xdr_argument = (xdrproc_t) xdr_createargs;
211 xdr_result = (xdrproc_t) xdr_diropres;
212 local = (nfssvcproc_t) nfsproc_create_2_svc;
213 break;
214
215 case NFSPROC_REMOVE:
216 xdr_argument = (xdrproc_t) xdr_diropargs;
217 xdr_result = (xdrproc_t) xdr_nfsstat;
218 local = (nfssvcproc_t) nfsproc_remove_2_svc;
219 break;
220
221 case NFSPROC_RENAME:
222 xdr_argument = (xdrproc_t) xdr_renameargs;
223 xdr_result = (xdrproc_t) xdr_nfsstat;
224 local = (nfssvcproc_t) nfsproc_rename_2_svc;
225 break;
226
227 case NFSPROC_LINK:
228 xdr_argument = (xdrproc_t) xdr_linkargs;
229 xdr_result = (xdrproc_t) xdr_nfsstat;
230 local = (nfssvcproc_t) nfsproc_link_2_svc;
231 break;
232
233 case NFSPROC_SYMLINK:
234 xdr_argument = (xdrproc_t) xdr_symlinkargs;
235 xdr_result = (xdrproc_t) xdr_nfsstat;
236 local = (nfssvcproc_t) nfsproc_symlink_2_svc;
237 break;
238
239 case NFSPROC_MKDIR:
240 xdr_argument = (xdrproc_t) xdr_createargs;
241 xdr_result = (xdrproc_t) xdr_diropres;
242 local = (nfssvcproc_t) nfsproc_mkdir_2_svc;
243 break;
244
245 case NFSPROC_RMDIR:
246 xdr_argument = (xdrproc_t) xdr_diropargs;
247 xdr_result = (xdrproc_t) xdr_nfsstat;
248 local = (nfssvcproc_t) nfsproc_rmdir_2_svc;
249 break;
250
251 case NFSPROC_READDIR:
252 xdr_argument = (xdrproc_t) xdr_readdirargs;
253 xdr_result = (xdrproc_t) xdr_readdirres;
254 local = (nfssvcproc_t) nfsproc_readdir_2_svc;
255 break;
256
257 case NFSPROC_STATFS:
258 xdr_argument = (xdrproc_t) xdr_nfs_fh;
259 xdr_result = (xdrproc_t) xdr_statfsres;
260 local = (nfssvcproc_t) nfsproc_statfs_2_svc;
261 break;
262
263 default:
264 svcerr_noproc(transp);
265 return;
266 }
267
268 memset((char *) &argument, 0, sizeof(argument));
269 if (!svc_getargs(transp,
270 (XDRPROC_T_TYPE) xdr_argument,
271 (SVC_IN_ARG_TYPE) &argument)) {
272 plog(XLOG_ERROR,
273 "NFS xdr decode failed for %d %d %d",
274 (int) rqstp->rq_prog, (int) rqstp->rq_vers, (int) rqstp->rq_proc);
275 svcerr_decode(transp);
276 return;
277 }
278 result = (*local) (&argument, rqstp);
279
280 current_transp = NULL;
281
282 if (result != NULL && !svc_sendreply(transp,
283 (XDRPROC_T_TYPE) xdr_result,
284 result)) {
285 svcerr_systemerr(transp);
286 }
287 if (!svc_freeargs(transp,
288 (XDRPROC_T_TYPE) xdr_argument,
289 (SVC_IN_ARG_TYPE) & argument)) {
290 plog(XLOG_FATAL, "unable to free rpc arguments in nfs_program_2");
291 going_down(1);
292 }
293 }
294