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