1/*	$NetBSD$	*/
2
3/*
4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1990 Jan-Simon Pendry
6 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1990 The Regents of the University of California.
8 * All rights reserved.
9 *
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 * 1. Redistributions of source code must retain the above copyright
17 *    notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 *    must display the following acknowledgment:
23 *      This product includes software developed by the University of
24 *      California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 *    may be used to endorse or promote products derived from this software
27 *    without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 * SUCH DAMAGE.
40 *
41 *
42 * File: am-utils/include/am_utils.h
43 *
44 */
45
46/*
47 * Definitions that are specific to the am-utils package.
48 */
49
50#ifndef _AM_UTILS_H
51#define _AM_UTILS_H
52
53
54#include "aux_conf.h"
55
56/**************************************************************************/
57/*** MACROS								***/
58/**************************************************************************/
59
60/*
61 * General macros.
62 */
63#ifndef FALSE
64# define FALSE 0
65#endif /* not FALSE */
66#ifndef TRUE
67# define TRUE 1
68#endif /* not TRUE */
69#ifndef MAX
70# define MAX(a, b)	((a) > (b) ? (a) : (b))
71#endif /* not MAX */
72#ifndef MIN
73# define MIN(a, b)	((a) < (b) ? (a) : (b))
74#endif /* not MIN */
75
76#define	ONE_HOUR	(60 * 60)	/* One hour in seconds */
77
78#ifndef MAXHOSTNAMELEN
79# ifdef HOSTNAMESZ
80#  define MAXHOSTNAMELEN HOSTNAMESZ
81# else /* not HOSTNAMESZ */
82#  define MAXHOSTNAMELEN 256
83# endif /* not HOSTNAMESZ */
84#endif /* not MAXHOSTNAMELEN */
85
86/*
87 * for hlfsd, and amd for detecting uid/gid
88 */
89#ifndef INVALIDID
90/* this is also defined in include/am_utils.h */
91# define INVALIDID	(((unsigned short) ~0) - 3)
92#endif /* not INVALIDID */
93
94/*
95 * String comparison macros
96 */
97#define STREQ(s1, s2)		(strcmp((s1), (s2)) == 0)
98#define STRCEQ(s1, s2)		(strcasecmp((s1), (s2)) == 0)
99#define NSTREQ(s1, s2, n)	(strncmp((s1), (s2), (n)) == 0)
100#define NSTRCEQ(s1, s2, n)	(strncasecmp((s1), (s2), (n)) == 0)
101#define FSTREQ(s1, s2)		((*(s1) == *(s2)) && STREQ((s1),(s2)))
102
103/*
104 * Logging options/flags
105 */
106#define	XLOG_FATAL	0x0001
107#define	XLOG_ERROR	0x0002
108#define	XLOG_USER	0x0004
109#define	XLOG_WARNING	0x0008
110#define	XLOG_INFO	0x0010
111#define	XLOG_DEBUG	0x0020
112#define	XLOG_MAP	0x0040
113#define	XLOG_STATS	0x0080
114/* log option compositions */
115#define XLOG_MASK	0x00ff	/* mask for all flags */
116#define XLOG_MANDATORY	(XLOG_FATAL|XLOG_ERROR)	/* cannot turn these off */
117#define XLOG_ALL	(XLOG_FATAL|XLOG_ERROR|XLOG_USER|XLOG_WARNING|XLOG_INFO|XLOG_MAP|XLOG_STATS)
118/* default: fatal + error + user + warning + info */
119#define XLOG_DEFAULT	(XLOG_MASK & (XLOG_ALL & ~XLOG_MAP & ~XLOG_STATS))
120
121/* default: no logging options */
122
123#define NO_SUBNET	"notknown"   /* default subnet name for no subnet */
124#define	NEXP_AP		(1022)			/* gdmr: was 254 */
125#define NEXP_AP_MARGIN	(128)			/* ???? not used */
126
127/*
128 * Linked list macros
129 */
130#define	AM_FIRST(ty, q)	((ty *) ((q)->q_forw))
131#define	AM_LAST(ty, q)	((ty *) ((q)->q_back))
132#define	NEXT(ty, q)	((ty *) (((qelem *) q)->q_forw))
133#define	PREV(ty, q)	((ty *) (((qelem *) q)->q_back))
134#define	HEAD(ty, q)	((ty *) q)
135#define	ITER(v, ty, q) \
136	for ((v) = AM_FIRST(ty,(q)); (v) != HEAD(ty,(q)); (v) = NEXT(ty,(v)))
137
138/* allocate anything of type ty */
139#define	ALLOC(ty)	((ty *) xmalloc(sizeof(ty)))
140#define	CALLOC(ty)	((ty *) xzalloc(sizeof(ty)))
141/* simply allocate b bytes */
142#define	SALLOC(b)	xmalloc((b))
143
144/*
145 * Systems which have the mount table in a file need to read it before
146 * they can perform an unmount() system call.
147 */
148#define UMOUNT_FS(dir, mtb_name, unmount_flags)	umount_fs(dir, mtb_name, unmount_flags)
149/* next two are imported via $srcdir/conf/umount/umount_*.c */
150extern int umount_fs(char *mntdir, const char *mnttabname, u_int unmount_flags);
151#ifdef MNT2_GEN_OPT_FORCE
152extern int umount2_fs(const char *mntdir, u_int unmount_flags);
153#endif /* MNT2_GEN_OPT_FORCE */
154
155/* unmount-related flags (special handling of autofs, forced/lazy, etc.) */
156#define AMU_UMOUNT_FORCE        0x1
157#define AMU_UMOUNT_DETACH       0x2
158#define AMU_UMOUNT_AUTOFS       0x4
159
160/*
161 * The following values can be tuned...
162 */
163#define	ALLOWED_MOUNT_TIME	40	/* 40s for a mount */
164
165/*
166 * RPC-related macros.
167 */
168#define	RPC_XID_PORTMAP		0
169#define	RPC_XID_MOUNTD		1
170#define	RPC_XID_NFSPING		2
171#define	RPC_XID_WEBNFS		3
172#define	RPC_XID_MASK		(0x0f)	/* 16 id's for now */
173#define	MK_RPC_XID(type_id, uniq)	((type_id) | ((uniq) << 4))
174
175/*
176 * What level of AMD are we backward compatible with?
177 * This only applies to externally visible characteristics.
178 * Rev.Minor.Branch.Patch (2 digits each)
179 */
180#define	AMD_COMPAT	5000000	/* 5.0 */
181
182
183/**************************************************************************/
184/*** STRUCTURES AND TYPEDEFS						***/
185/**************************************************************************/
186
187/* some typedefs must come first */
188typedef char *amq_string;
189typedef struct _qelem qelem;
190typedef struct mntlist mntlist;
191
192/*
193 * Linked list
194 * (the name 'struct qelem' conflicts with linux's unistd.h)
195 */
196struct _qelem {
197  qelem *q_forw;
198  qelem *q_back;
199};
200
201/*
202 * Option tables
203 */
204struct opt_tab {
205  char *opt;
206  int flag;
207};
208
209/*
210 * Server states
211 */
212typedef enum {
213  Start,
214  Run,
215  Finishing,
216  Quit,
217  Done
218} serv_state;
219
220
221/*
222 * List of mount table entries
223 */
224struct mntlist {
225  struct mntlist *mnext;
226  mntent_t *mnt;
227};
228
229/*
230 * Mount map
231 */
232typedef struct mnt_map mnt_map;
233
234
235/**************************************************************************/
236/*** EXTERNALS								***/
237/**************************************************************************/
238
239/*
240 * Useful constants
241 */
242extern char *mnttab_file_name;	/* Mount table */
243extern char *cpu;		/* "CPU type" */
244extern char *endian;		/* "big" */
245extern char *hostdomain;	/* "southseas.nz" */
246extern char copyright[];	/* Copyright info */
247extern char version[];		/* Version info */
248
249/*
250 * Global variables.
251 */
252extern AUTH *nfs_auth;		/* Dummy authorization for remote servers */
253extern FILE *logfp;		/* Log file */
254extern SVCXPRT *nfsxprt;
255extern char *PrimNetName;	/* Name of primary connected network */
256extern char *PrimNetNum;	/* Name of primary connected network */
257extern char *SubsNetName;	/* Name of subsidiary connected network */
258extern char *SubsNetNum;	/* Name of subsidiary connected network */
259
260extern void am_set_progname(char *pn); /* "amd" */
261extern const char *am_get_progname(void); /* "amd" */
262extern void am_set_hostname(char *hn);
263extern const char *am_get_hostname(void);
264extern pid_t am_set_mypid(void);
265extern pid_t am_mypid;
266
267extern int foreground;		/* Foreground process */
268extern int orig_umask;		/* umask() on startup */
269extern serv_state amd_state;	/* Should we go now */
270extern struct in_addr myipaddr;	/* (An) IP address of this host */
271extern struct opt_tab xlog_opt[];
272extern u_short nfs_port;	/* Our NFS service port */
273
274/*
275 * Global routines
276 */
277extern CLIENT *get_mount_client(char *unused_host, struct sockaddr_in *sin, struct timeval *tv, int *sock, u_long mnt_version);
278extern RETSIGTYPE sigchld(int);
279extern bool_t xdr_amq_string(XDR *xdrs, amq_string *objp);
280extern bool_t xdr_dirpath(XDR *xdrs, dirpath *objp);
281extern char **strsplit(char *, int, int);
282extern char *expand_selectors(char *);
283extern char *get_version_string(void);
284extern char *inet_dquad(char *, size_t, u_long);
285extern char *print_wires(void);
286extern char *str3cat(char *, char *, char *, char *);
287extern char *strealloc(char *, char *);
288extern char *strip_selectors(char *, char *);
289extern char *strnsave(const char *, int);
290extern int amu_close(int fd);
291extern int bind_resv_port(int, u_short *);
292extern int cmdoption(char *, struct opt_tab *, u_int *);
293extern int compute_automounter_mount_flags(mntent_t *);
294extern int compute_mount_flags(mntent_t *);
295extern u_long get_amd_program_number(void);
296extern int getcreds(struct svc_req *, uid_t *, gid_t *, SVCXPRT *);
297extern int hasmntval(mntent_t *, char *);
298extern unsigned int hasmntvalerr(mntent_t *, char *, int *);
299extern char *hasmntstr(mntent_t *, char *);
300extern char *hasmnteq(mntent_t *, char *);
301extern char *haseq(char *);
302extern int is_network_member(const char *net);
303extern int is_interface_local(u_long);
304extern int islocalnet(u_long);
305extern int make_rpc_packet(char *, int, u_long, struct rpc_msg *, voidp, XDRPROC_T_TYPE, AUTH *);
306extern int mkdirs(char *, int);
307extern int mount_fs(mntent_t *, int, caddr_t, int, MTYPE_TYPE, u_long, const char *, const char *, int);
308extern void nfs_program_2(struct svc_req *rqstp, SVCXPRT *transp);
309extern int pickup_rpc_reply(voidp, int, voidp, XDRPROC_T_TYPE);
310extern int switch_option(char *);
311extern int switch_to_logfile(char *logfile, int orig_umask, int truncate_log);
312extern mntlist *read_mtab(char *, const char *);
313#ifndef HAVE_TRANSPORT_TYPE_TLI
314extern struct sockaddr_in *amu_svc_getcaller(SVCXPRT *xprt);
315#endif /* not HAVE_TRANSPORT_TYPE_TLI */
316extern time_t time(time_t *);
317extern void amu_get_myaddress(struct in_addr *iap, const char *preferred_localhost);
318extern void amu_release_controlling_tty(void);
319extern void compute_automounter_nfs_args(nfs_args_t *nap, mntent_t *mntp);
320extern void discard_mntlist(mntlist *mp);
321extern void free_mntlist(mntlist *);
322extern void getwire(char **name1, char **number1);
323extern void going_down(int);
324extern void mnt_free(mntent_t *);
325extern void plog(int, const char *,...)
326     __attribute__ ((__format__ (__printf__, 2, 3)));
327extern void rmdirs(char *);
328extern void rpc_msg_init(struct rpc_msg *, u_long, u_long, u_long);
329extern void set_amd_program_number(u_long program);
330extern void show_opts(int ch, struct opt_tab *);
331extern void unregister_amq(void);
332extern voidp xmalloc(int);
333extern voidp xrealloc(voidp, int);
334extern voidp xzalloc(int);
335extern int check_pmap_up(char *host, struct sockaddr_in* sin);
336extern u_long get_nfs_version(char *host, struct sockaddr_in *sin, u_long nfs_version, const char *proto);
337extern long get_server_pid(void);
338extern void setup_sighandler(int signum, void (*handler)(int));
339extern time_t clocktime(nfstime *nt);
340
341#if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
342# ifdef HAVE_C99_VARARGS_MACROS
343#define xsnprintf(str,size,fmt,...)	_xsnprintf(__FILE__,__LINE__,(str),(size),(fmt),__VA_ARGS__)
344# endif /* HAVE_C99_VARARGS_MACROS */
345# ifdef HAVE_GCC_VARARGS_MACROS
346#define xsnprintf(str,size,fmt,args...)		_xsnprintf(__FILE__,__LINE__,(str),(size),(fmt),args)
347# endif /* HAVE_GCC_VARARGS_MACROS */
348extern int _xsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, ...);
349#define xvsnprintf(str,size,fmt,ap)	_xvsnprintf(__FILE__,__LINE__,(str),(size),(fmt),(ap))
350extern int _xvsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, va_list ap);
351#else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
352extern int xsnprintf(char *str, size_t size, const char *format, ...);
353extern int xvsnprintf(char *str, size_t size, const char *format, va_list ap);
354#endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
355
356#ifdef DEBUG
357extern void _xstrlcat(const char *filename, int lineno, char *dst, const char *src, size_t len);
358# define xstrlcat(d,s,l)	_xstrlcat(__FILE__,__LINE__,(d),(s),(l))
359extern void _xstrlcpy(const char *filename, int lineno, char *dst, const char *src, size_t len);
360# define xstrlcpy(d,s,l)	_xstrlcpy(__FILE__,__LINE__,(d),(s),(l))
361#else /* not DEBUG */
362extern void xstrlcat(char *dst, const char *src, size_t len);
363extern void xstrlcpy(char *dst, const char *src, size_t len);
364#endif /* not DEBUG */
365
366#ifdef MOUNT_TABLE_ON_FILE
367extern void rewrite_mtab(mntlist *, const char *);
368extern void unlock_mntlist(void);
369extern void write_mntent(mntent_t *, const char *);
370#endif /* MOUNT_TABLE_ON_FILE */
371
372#if defined(HAVE_SYSLOG_H) || defined(HAVE_SYS_SYSLOG_H)
373extern int syslogging;
374#endif /* defined(HAVE_SYSLOG_H) || defined(HAVE_SYS_SYSLOG_H) */
375
376extern void compute_nfs_args(nfs_args_t *nap, mntent_t *mntp, int genflags, struct netconfig *nfsncp, struct sockaddr_in *ip_addr, u_long nfs_version, char *nfs_proto, am_nfs_handle_t *fhp, char *host_name, char *fs_name);
377extern int create_amq_service(int *udp_soAMQp, SVCXPRT **udp_amqpp, struct netconfig **udp_amqncpp, int *tcp_soAMQp, SVCXPRT **tcp_amqpp, struct netconfig **tcp_amqncpp, u_short preferred_amq_port);
378extern int create_nfs_service(int *soNFSp, u_short *nfs_portp, SVCXPRT **nfs_xprtp, void (*dispatch_fxn)(struct svc_req *rqstp, SVCXPRT *transp));
379extern int amu_svc_register(SVCXPRT *, u_long, u_long, void (*)(struct svc_req *, SVCXPRT *), u_long, struct netconfig *);
380
381#ifdef HAVE_TRANSPORT_TYPE_TLI
382
383extern int get_knetconfig(struct knetconfig **kncpp, struct netconfig *in_ncp, char *nc_protoname);
384extern struct netconfig *nfsncp;
385extern void free_knetconfig(struct knetconfig *kncp);
386
387#endif /* HAVE_TRANSPORT_TYPE_TLI */
388
389#ifdef HAVE_FS_AUTOFS
390extern int register_autofs_service(char *autofs_conftype, void (*autofs_dispatch)(struct svc_req *rqstp, SVCXPRT *xprt));
391extern int unregister_autofs_service(char *autofs_conftype);
392#endif /* HAVE_FS_AUTOFS */
393
394
395#ifndef HAVE_STRUCT_FHSTATUS_FHS_FH
396# define fhs_fh  fhstatus_u.fhs_fhandle
397#endif /* not HAVE_STRUCT_FHSTATUS_FHS_FH */
398
399
400/*
401 * Network File System: the new generation
402 * NFS V.3
403 */
404#ifdef HAVE_FS_NFS3
405# ifndef NFS_VERSION3
406#  define NFS_VERSION3 ((u_int) 3)
407# endif /* not NFS_VERSION3 */
408#endif /* HAVE_FS_NFS3 */
409
410
411/**************************************************************************/
412/*** DEBUGGING								***/
413/**************************************************************************/
414
415/*
416 * DEBUGGING:
417 */
418
419#ifdef DEBUG
420
421# define	D_DAEMON	0x0001	/* Enter daemon mode */
422# define	D_TRACE		0x0002	/* Do protocol trace */
423# define	D_FULL		0x0004	/* Do full trace */
424# define	D_MTAB		0x0008	/* Use local mtab */
425# define	D_AMQ		0x0010	/* Register amq program */
426# define	D_STR		0x0020	/* Debug string munging */
427# ifdef DEBUG_MEM
428#  define	D_MEM		0x0040	/* Trace memory allocations */
429# else /* not DEBUG_MEM */
430#  define	D_MEM		0x0000	/* Dummy */
431# endif /* not DEBUG_MEM */
432# define	D_FORK		0x0080	/* Fork server (hlfsd only) */
433# define	D_INFO		0x0100	/* info service specific debugging (hesiod, nis, etc) */
434# define	D_HRTIME	0x0200	/* Print high resolution time stamps */
435# define	D_XDRTRACE	0x0400	/* Trace xdr routines */
436# define	D_READDIR	0x0800	/* Show browsable_dir progress */
437/* debug option compositions */
438# define	D_MASK		0x0fff  /* mask of known flags */
439# define	D_BASIC		(D_TRACE|D_FULL|D_STR|D_MEM|D_INFO|D_XDRTRACE|D_READDIR)
440# define	D_CONTROL	(D_DAEMON|D_AMQ|D_FORK)
441/* immutable flags: cannot be changed via "amq -D" */
442# define	D_IMMUTABLE	(D_MTAB  | D_CONTROL)
443# define	D_ALL		(D_BASIC | D_CONTROL)
444# define	D_DEFAULT	(D_MASK & D_ALL & ~D_XDRTRACE)
445/* test mode: nodaemon, noamq, nofork, (local) mtab */
446# define	D_TEST		(D_BASIC | D_MTAB)
447
448# define	amuDebug(x)	(debug_flags & (x))
449# define	dlog		if (amuDebug(D_FULL)) dplog
450
451/* my favorite debugging tool -Erez */
452#define EZKDBG plog(XLOG_INFO,"EZK:%s:%s:%d\n",__FILE__,__FUNCTION__,__LINE__)
453
454# ifdef DEBUG_MEM
455/*
456 * If debugging memory, then call a special freeing function that logs
457 * more info, and resets the pointer to NULL so it cannot be used again.
458 */
459#  define	XFREE(x) dxfree(__FILE__,__LINE__,x)
460extern void dxfree(char *file, int line, voidp ptr);
461extern void malloc_verify(void);
462# else /* not DEBUG_MEM */
463/*
464 * If regular debugging, then free the pointer and reset to NULL.
465 * This should remain so for as long as am-utils is in alpha/beta testing.
466 */
467#  define	XFREE(x) do { free((voidp)x); x = NULL;} while (0)
468# endif /* not DEBUG_MEM */
469
470/* functions that depend solely on debugging */
471extern void print_nfs_args(const nfs_args_t *nap, u_long nfs_version);
472extern int debug_option (char *opt);
473extern void dplog(const char *fmt, ...)
474     __attribute__ ((__format__ (__printf__, 1, 2)));
475
476#else /* not DEBUG */
477
478/* set dummy flags to zero */
479# define	D_DAEMON	0x0001	/* Enter daemon mode */
480# define	D_TRACE		0x0000	/* dummy: Do protocol trace */
481# define	D_FULL		0x0000	/* dummy: Do full trace */
482# define	D_MTAB		0x0000	/* dummy: Use local mtab */
483# define	D_AMQ		0x0010	/* Register amq program */
484# define	D_STR		0x0000	/* dummy: Debug string munging */
485# define	D_MEM		0x0000	/* dummy: Trace memory allocations */
486# define	D_FORK		0x0080	/* Fork server (hlfsd only) */
487# define	D_INFO		0x0000	/* dummy: info service debugging */
488# define	D_HRTIME	0x0000	/* dummy: hi-res time stamps */
489# define	D_XDRTRACE	0x0000	/* dummy: Trace xdr routines */
490# define	D_READDIR	0x0000	/* dummy: browsable_dir progress */
491# define	D_CONTROL	(D_DAEMON|D_AMQ|D_FORK)
492# define	amuDebug(x)	(debug_flags & (x))
493/*
494 * If not debugging, then also reset the pointer.
495 * It's safer -- and besides, free() should do that anyway.
496 */
497# define	XFREE(x) do { free((voidp)x); x = NULL;} while (0)
498
499# if defined(HAVE_GCC_VARARGS_MACROS)
500#  define	dlog(fmt...)
501# elif defined(HAVE_C99_VARARGS_MACROS)
502#  define	dlog(...)
503# else  /* no c99 varargs */
504/* this define means that we CCP leaves code behind the (list,of,args)  */
505#  define	dlog
506# endif /* no c99 varargs */
507
508# define	print_nfs_args(nap, nfs_version)
509# define	debug_option(x)	(1)
510
511#endif /* not DEBUG */
512
513extern u_int debug_flags;	/* Debug options */
514extern struct opt_tab dbg_opt[];
515
516/**************************************************************************/
517/*** MISC (stuff left to autoconfiscate)				***/
518/**************************************************************************/
519
520#endif /* not _AM_UTILS_H */
521