amq_subr.c revision 38494
1/*
2 * Copyright (c) 1997-1998 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 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 acknowledgement:
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 *      %W% (Berkeley) %G%
40 *
41 * $Id: amq_subr.c,v 5.2.2.1 1992/02/09 15:08:18 jsp beta $
42 *
43 */
44/*
45 * Auxilliary routines for amq tool
46 */
47
48#ifdef HAVE_CONFIG_H
49# include <config.h>
50#endif /* HAVE_CONFIG_H */
51#include <am_defs.h>
52#include <amd.h>
53
54/* forward definitions */
55bool_t xdr_amq_mount_tree_node(XDR *xdrs, amq_mount_tree *objp);
56bool_t xdr_amq_mount_subtree(XDR *xdrs, amq_mount_tree *objp);
57
58
59voidp
60amqproc_null_1_svc(voidp argp, struct svc_req *rqstp)
61{
62  static char res;
63
64  return (voidp) &res;
65}
66
67
68/*
69 * Return a sub-tree of mounts
70 */
71amq_mount_tree_p *
72amqproc_mnttree_1_svc(voidp argp, struct svc_req *rqstp)
73{
74  static am_node *mp;
75
76  mp = find_ap(*(char **) argp);
77  return (amq_mount_tree_p *) &mp;
78}
79
80
81/*
82 * Unmount a single node
83 */
84voidp
85amqproc_umnt_1_svc(voidp argp, struct svc_req *rqstp)
86{
87  static char res;
88  am_node *mp = find_ap(*(char **) argp);
89
90  if (mp)
91    forcibly_timeout_mp(mp);
92
93  return (voidp) &res;
94}
95
96
97/*
98 * Return global statistics
99 */
100amq_mount_stats *
101amqproc_stats_1_svc(voidp argp, struct svc_req *rqstp)
102{
103  return (amq_mount_stats *) &amd_stats;
104}
105
106
107/*
108 * Return the entire tree of mount nodes
109 */
110amq_mount_tree_list *
111amqproc_export_1_svc(voidp argp, struct svc_req *rqstp)
112{
113  static amq_mount_tree_list aml;
114
115  aml.amq_mount_tree_list_val = (amq_mount_tree_p *) &exported_ap[0];
116  aml.amq_mount_tree_list_len = 1;	/* XXX */
117
118  return &aml;
119}
120
121
122int *
123amqproc_setopt_1_svc(voidp argp, struct svc_req *rqstp)
124{
125  static int rc;
126  amq_setopt *opt = (amq_setopt *) argp;
127
128  rc = 0;
129
130  switch (opt->as_opt) {
131
132  case AMOPT_DEBUG:
133#ifdef DEBUG
134    if (debug_option(opt->as_str))
135#endif /* DEBUG */
136      rc = EINVAL;
137    break;
138
139  case AMOPT_LOGFILE:
140    if (gopt.logfile && opt->as_str
141	&& STREQ(gopt.logfile, opt->as_str)) {
142      if (switch_to_logfile(opt->as_str))
143	rc = EINVAL;
144    } else {
145      rc = EACCES;
146    }
147    break;
148
149  case AMOPT_XLOG:
150    if (switch_option(opt->as_str))
151      rc = EINVAL;
152    break;
153
154  case AMOPT_FLUSHMAPC:
155    if (amd_state == Run) {
156      plog(XLOG_INFO, "amq says flush cache");
157      do_mapc_reload = 0;
158      flush_nfs_fhandle_cache((fserver *) 0);
159      flush_srvr_nfs_cache();
160    }
161    break;
162  }
163
164  return &rc;
165}
166
167
168amq_mount_info_list *
169amqproc_getmntfs_1_svc(voidp argp, struct svc_req *rqstp)
170{
171  return (amq_mount_info_list *) &mfhead;	/* XXX */
172}
173
174#ifdef ENABLE_AMQ_MOUNT
175/*
176 * This is code that is vulnerable to IP spoofing attacks.  Unless you
177 * absolutely need it, I suggest you do not enable it
178 * (using configure --enable-amq-mount)
179 */
180static int
181ok_security(struct svc_req *rqstp)
182{
183  struct sockaddr_in *sin = (struct sockaddr_in *) NULL;
184
185  if ((sin = amu_svc_getcaller(rqstp->rq_xprt)) == NULL) {
186    plog(XLOG_ERROR, "amu_svc_getcaller returned NULL");
187    return(0);			/* assume security is therefore not OK */
188  }
189
190  if (ntohs(sin->sin_port) >= 1024 ||
191      !(sin->sin_addr.s_addr == htonl(0x7f000001) ||
192	sin->sin_addr.s_addr == myipaddr.s_addr)) {
193    char dq[20];
194    plog(XLOG_INFO, "AMQ request from %s.%d DENIED",
195	 inet_dquad(dq, sin->sin_addr.s_addr),
196	 ntohs(sin->sin_port));
197    return (0);
198  }
199
200  return (1);
201}
202
203
204int *
205amqproc_mount_1_svc(voidp argp, struct svc_req *rqstp)
206{
207  static int rc;
208  char *s = *(amq_string *) argp;
209  char *cp;
210
211  plog(XLOG_INFO, "amq requested mount of %s", s);
212  /*
213   * Minimalist security check.
214   */
215  if (!ok_security(rqstp)) {
216    rc = EACCES;
217    return &rc;
218  }
219  /*
220   * Find end of key
221   */
222  for (cp = (char *) s; *cp && (!isascii(*cp) || !isspace(*cp)); cp++) ;
223
224  if (!*cp) {
225    plog(XLOG_INFO, "amqproc_mount: Invalid arguments");
226    rc = EINVAL;
227    return &rc;
228  }
229  *cp++ = '\0';
230
231  /*
232   * Find start of value
233   */
234  while (*cp && isascii(*cp) && isspace(*cp))
235    cp++;
236
237  root_newmap(s, cp, (char *) 0, NULL);
238  rc = mount_auto_node(s, (voidp) root_node);
239  if (rc < 0)
240    return 0;
241  return &rc;
242}
243
244#else /* not ENABLE_AMQ_MOUNT */
245
246int *
247amqproc_mount_1_svc(voidp argp, struct svc_req *rqstp)
248{
249  static int rc;
250  char *s = *(amq_string *) argp;
251
252  plog(XLOG_ERROR, "amq requested mount of %s, but code is disabled", s);
253
254  rc = EINVAL;
255  return &rc;
256}
257#endif /* not ENABLE_AMQ_MOUNT */
258
259
260amq_string *
261amqproc_getvers_1_svc(voidp argp, struct svc_req *rqstp)
262{
263  static amq_string res;
264
265  res = get_version_string();
266  return &res;
267}
268
269
270/* get PID of remote amd */
271int *
272amqproc_getpid_1_svc(voidp argp, struct svc_req *rqstp)
273{
274  static int res;
275
276  res = getpid();
277  return &res;
278}
279
280
281/*
282 * XDR routines.
283 */
284
285
286bool_t
287xdr_amq_setopt(XDR *xdrs, amq_setopt *objp)
288{
289  if (!xdr_enum(xdrs, (enum_t *) & objp->as_opt)) {
290    return (FALSE);
291  }
292  if (!xdr_string(xdrs, &objp->as_str, AMQ_STRLEN)) {
293    return (FALSE);
294  }
295  return (TRUE);
296}
297
298
299/*
300 * More XDR routines  - Should be used for OUTPUT ONLY.
301 */
302bool_t
303xdr_amq_mount_tree_node(XDR *xdrs, amq_mount_tree *objp)
304{
305  am_node *mp = (am_node *) objp;
306
307  if (!xdr_amq_string(xdrs, &mp->am_mnt->mf_info)) {
308    return (FALSE);
309  }
310  if (!xdr_amq_string(xdrs, &mp->am_path)) {
311    return (FALSE);
312  }
313  if (!xdr_amq_string(xdrs, mp->am_link ? &mp->am_link : &mp->am_mnt->mf_mount)) {
314    return (FALSE);
315  }
316  if (!xdr_amq_string(xdrs, &mp->am_mnt->mf_ops->fs_type)) {
317    return (FALSE);
318  }
319  if (!xdr_long(xdrs, (long *) &mp->am_stats.s_mtime)) {
320    return (FALSE);
321  }
322  if (!xdr_u_short(xdrs, &mp->am_stats.s_uid)) {
323    return (FALSE);
324  }
325  if (!xdr_int(xdrs, &mp->am_stats.s_getattr)) {
326    return (FALSE);
327  }
328  if (!xdr_int(xdrs, &mp->am_stats.s_lookup)) {
329    return (FALSE);
330  }
331  if (!xdr_int(xdrs, &mp->am_stats.s_readdir)) {
332    return (FALSE);
333  }
334  if (!xdr_int(xdrs, &mp->am_stats.s_readlink)) {
335    return (FALSE);
336  }
337  if (!xdr_int(xdrs, &mp->am_stats.s_statfs)) {
338    return (FALSE);
339  }
340  return (TRUE);
341}
342
343
344bool_t
345xdr_amq_mount_subtree(XDR *xdrs, amq_mount_tree *objp)
346{
347  am_node *mp = (am_node *) objp;
348
349  if (!xdr_amq_mount_tree_node(xdrs, objp)) {
350    return (FALSE);
351  }
352  if (!xdr_pointer(xdrs, (char **) &mp->am_osib, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
353    return (FALSE);
354  }
355  if (!xdr_pointer(xdrs, (char **) &mp->am_child, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
356    return (FALSE);
357  }
358  return (TRUE);
359}
360
361
362bool_t
363xdr_amq_mount_tree(XDR *xdrs, amq_mount_tree *objp)
364{
365  am_node *mp = (am_node *) objp;
366  am_node *mnil = 0;
367
368  if (!xdr_amq_mount_tree_node(xdrs, objp)) {
369    return (FALSE);
370  }
371  if (!xdr_pointer(xdrs, (char **) &mnil, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
372    return (FALSE);
373  }
374  if (!xdr_pointer(xdrs, (char **) &mp->am_child, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
375    return (FALSE);
376  }
377  return (TRUE);
378}
379
380
381bool_t
382xdr_amq_mount_tree_p(XDR *xdrs, amq_mount_tree_p *objp)
383{
384  if (!xdr_pointer(xdrs, (char **) objp, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_tree)) {
385    return (FALSE);
386  }
387  return (TRUE);
388}
389
390
391bool_t
392xdr_amq_mount_stats(XDR *xdrs, amq_mount_stats *objp)
393{
394  if (!xdr_int(xdrs, &objp->as_drops)) {
395    return (FALSE);
396  }
397  if (!xdr_int(xdrs, &objp->as_stale)) {
398    return (FALSE);
399  }
400  if (!xdr_int(xdrs, &objp->as_mok)) {
401    return (FALSE);
402  }
403  if (!xdr_int(xdrs, &objp->as_merr)) {
404    return (FALSE);
405  }
406  if (!xdr_int(xdrs, &objp->as_uerr)) {
407    return (FALSE);
408  }
409  return (TRUE);
410}
411
412
413
414bool_t
415xdr_amq_mount_tree_list(XDR *xdrs, amq_mount_tree_list *objp)
416{
417  if (!xdr_array(xdrs,
418		 (char **) &objp->amq_mount_tree_list_val,
419		 (u_int *) &objp->amq_mount_tree_list_len,
420		 ~0,
421		 sizeof(amq_mount_tree_p),
422		 (XDRPROC_T_TYPE) xdr_amq_mount_tree_p)) {
423    return (FALSE);
424  }
425  return (TRUE);
426}
427
428
429
430/*
431 * Compute length of list
432 */
433bool_t
434xdr_amq_mount_info_qelem(XDR *xdrs, qelem *qhead)
435{
436  mntfs *mf;
437  u_int len = 0;
438
439  for (mf = AM_LAST(mntfs, qhead); mf != HEAD(mntfs, qhead); mf = PREV(mntfs, mf)) {
440    if (!(mf->mf_ops->fs_flags & FS_AMQINFO))
441      continue;
442    len++;
443  }
444  xdr_u_int(xdrs, &len);
445
446  /*
447   * Send individual data items
448   */
449  for (mf = AM_LAST(mntfs, qhead); mf != HEAD(mntfs, qhead); mf = PREV(mntfs, mf)) {
450    int up;
451    if (!(mf->mf_ops->fs_flags & FS_AMQINFO))
452      continue;
453
454    if (!xdr_amq_string(xdrs, &mf->mf_ops->fs_type)) {
455      return (FALSE);
456    }
457    if (!xdr_amq_string(xdrs, &mf->mf_mount)) {
458      return (FALSE);
459    }
460    if (!xdr_amq_string(xdrs, &mf->mf_info)) {
461      return (FALSE);
462    }
463    if (!xdr_amq_string(xdrs, &mf->mf_server->fs_host)) {
464      return (FALSE);
465    }
466    if (!xdr_int(xdrs, &mf->mf_error)) {
467      return (FALSE);
468    }
469    if (!xdr_int(xdrs, &mf->mf_refc)) {
470      return (FALSE);
471    }
472    if (mf->mf_server->fs_flags & FSF_ERROR)
473      up = 0;
474    else
475      switch (mf->mf_server->fs_flags & (FSF_DOWN | FSF_VALID)) {
476      case FSF_DOWN | FSF_VALID:
477	up = 0;
478	break;
479      case FSF_VALID:
480	up = 1;
481	break;
482      default:
483	up = -1;
484	break;
485      }
486    if (!xdr_int(xdrs, &up)) {
487      return (FALSE);
488    }
489  }
490  return (TRUE);
491}
492
493
494bool_t
495xdr_pri_free(XDRPROC_T_TYPE xdr_args, caddr_t args_ptr)
496{
497  XDR xdr;
498
499  xdr.x_op = XDR_FREE;
500  return ((*xdr_args) (&xdr, (caddr_t *) args_ptr));
501}
502