amq_subr.c revision 38790
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 1.1.1.1 1998/08/23 22:07:20 obrien Exp $
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  long mtime;
307
308  if (!xdr_amq_string(xdrs, &mp->am_mnt->mf_info)) {
309    return (FALSE);
310  }
311  if (!xdr_amq_string(xdrs, &mp->am_path)) {
312    return (FALSE);
313  }
314  if (!xdr_amq_string(xdrs, mp->am_link ? &mp->am_link : &mp->am_mnt->mf_mount)) {
315    return (FALSE);
316  }
317  if (!xdr_amq_string(xdrs, &mp->am_mnt->mf_ops->fs_type)) {
318    return (FALSE);
319  }
320  mtime = mp->am_stats.s_mtime;
321  if (!xdr_long(xdrs, &mtime)) {
322    return (FALSE);
323  }
324  if (!xdr_u_short(xdrs, &mp->am_stats.s_uid)) {
325    return (FALSE);
326  }
327  if (!xdr_int(xdrs, &mp->am_stats.s_getattr)) {
328    return (FALSE);
329  }
330  if (!xdr_int(xdrs, &mp->am_stats.s_lookup)) {
331    return (FALSE);
332  }
333  if (!xdr_int(xdrs, &mp->am_stats.s_readdir)) {
334    return (FALSE);
335  }
336  if (!xdr_int(xdrs, &mp->am_stats.s_readlink)) {
337    return (FALSE);
338  }
339  if (!xdr_int(xdrs, &mp->am_stats.s_statfs)) {
340    return (FALSE);
341  }
342  return (TRUE);
343}
344
345
346bool_t
347xdr_amq_mount_subtree(XDR *xdrs, amq_mount_tree *objp)
348{
349  am_node *mp = (am_node *) objp;
350
351  if (!xdr_amq_mount_tree_node(xdrs, objp)) {
352    return (FALSE);
353  }
354  if (!xdr_pointer(xdrs, (char **) &mp->am_osib, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
355    return (FALSE);
356  }
357  if (!xdr_pointer(xdrs, (char **) &mp->am_child, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
358    return (FALSE);
359  }
360  return (TRUE);
361}
362
363
364bool_t
365xdr_amq_mount_tree(XDR *xdrs, amq_mount_tree *objp)
366{
367  am_node *mp = (am_node *) objp;
368  am_node *mnil = 0;
369
370  if (!xdr_amq_mount_tree_node(xdrs, objp)) {
371    return (FALSE);
372  }
373  if (!xdr_pointer(xdrs, (char **) &mnil, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
374    return (FALSE);
375  }
376  if (!xdr_pointer(xdrs, (char **) &mp->am_child, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_subtree)) {
377    return (FALSE);
378  }
379  return (TRUE);
380}
381
382
383bool_t
384xdr_amq_mount_tree_p(XDR *xdrs, amq_mount_tree_p *objp)
385{
386  if (!xdr_pointer(xdrs, (char **) objp, sizeof(amq_mount_tree), (XDRPROC_T_TYPE) xdr_amq_mount_tree)) {
387    return (FALSE);
388  }
389  return (TRUE);
390}
391
392
393bool_t
394xdr_amq_mount_stats(XDR *xdrs, amq_mount_stats *objp)
395{
396  if (!xdr_int(xdrs, &objp->as_drops)) {
397    return (FALSE);
398  }
399  if (!xdr_int(xdrs, &objp->as_stale)) {
400    return (FALSE);
401  }
402  if (!xdr_int(xdrs, &objp->as_mok)) {
403    return (FALSE);
404  }
405  if (!xdr_int(xdrs, &objp->as_merr)) {
406    return (FALSE);
407  }
408  if (!xdr_int(xdrs, &objp->as_uerr)) {
409    return (FALSE);
410  }
411  return (TRUE);
412}
413
414
415
416bool_t
417xdr_amq_mount_tree_list(XDR *xdrs, amq_mount_tree_list *objp)
418{
419  if (!xdr_array(xdrs,
420		 (char **) &objp->amq_mount_tree_list_val,
421		 (u_int *) &objp->amq_mount_tree_list_len,
422		 ~0,
423		 sizeof(amq_mount_tree_p),
424		 (XDRPROC_T_TYPE) xdr_amq_mount_tree_p)) {
425    return (FALSE);
426  }
427  return (TRUE);
428}
429
430
431
432/*
433 * Compute length of list
434 */
435bool_t
436xdr_amq_mount_info_qelem(XDR *xdrs, qelem *qhead)
437{
438  mntfs *mf;
439  u_int len = 0;
440
441  for (mf = AM_LAST(mntfs, qhead); mf != HEAD(mntfs, qhead); mf = PREV(mntfs, mf)) {
442    if (!(mf->mf_ops->fs_flags & FS_AMQINFO))
443      continue;
444    len++;
445  }
446  xdr_u_int(xdrs, &len);
447
448  /*
449   * Send individual data items
450   */
451  for (mf = AM_LAST(mntfs, qhead); mf != HEAD(mntfs, qhead); mf = PREV(mntfs, mf)) {
452    int up;
453    if (!(mf->mf_ops->fs_flags & FS_AMQINFO))
454      continue;
455
456    if (!xdr_amq_string(xdrs, &mf->mf_ops->fs_type)) {
457      return (FALSE);
458    }
459    if (!xdr_amq_string(xdrs, &mf->mf_mount)) {
460      return (FALSE);
461    }
462    if (!xdr_amq_string(xdrs, &mf->mf_info)) {
463      return (FALSE);
464    }
465    if (!xdr_amq_string(xdrs, &mf->mf_server->fs_host)) {
466      return (FALSE);
467    }
468    if (!xdr_int(xdrs, &mf->mf_error)) {
469      return (FALSE);
470    }
471    if (!xdr_int(xdrs, &mf->mf_refc)) {
472      return (FALSE);
473    }
474    if (mf->mf_server->fs_flags & FSF_ERROR)
475      up = 0;
476    else
477      switch (mf->mf_server->fs_flags & (FSF_DOWN | FSF_VALID)) {
478      case FSF_DOWN | FSF_VALID:
479	up = 0;
480	break;
481      case FSF_VALID:
482	up = 1;
483	break;
484      default:
485	up = -1;
486	break;
487      }
488    if (!xdr_int(xdrs, &up)) {
489      return (FALSE);
490    }
491  }
492  return (TRUE);
493}
494
495
496bool_t
497xdr_pri_free(XDRPROC_T_TYPE xdr_args, caddr_t args_ptr)
498{
499  XDR xdr;
500
501  xdr.x_op = XDR_FREE;
502  return ((*xdr_args) (&xdr, (caddr_t *) args_ptr));
503}
504