xref: /openbsd-src/usr.bin/rdist/defs.h (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: defs.h,v 1.31 2014/07/12 03:48:04 guenther Exp $	*/
2 
3 #ifndef __DEFS_H__
4 #define __DEFS_H__
5 /*
6  * Copyright (c) 1983 Regents of the University of California.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * $From: defs.h,v 1.6 2001/03/12 18:16:30 kim Exp $
36  * @(#)defs.h      5.2 (Berkeley) 3/20/86
37  */
38 
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/file.h>
42 #include <sys/time.h>
43 #include <sys/stat.h>
44 #include <ctype.h>
45 #include <errno.h>
46 #include <grp.h>
47 #include <pwd.h>
48 #include <paths.h>
49 #include <regex.h>
50 #include <setjmp.h>
51 #include <signal.h>
52 #include <stdarg.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <syslog.h>
57 #include <unistd.h>
58 
59 #ifndef __GNUC__
60 # ifndef __attribute__
61 #  define __attribute__(a)
62 # endif
63 #endif
64 
65 #include "version.h"
66 #include "config.h"
67 #include "pathnames.h"
68 #include "types.h"
69 #include "filesys.h"
70 
71 /*
72  * Define the read and write values for the file descriptor array
73  * used by pipe().
74  */
75 #define PIPE_READ		0
76 #define PIPE_WRITE		1
77 
78 	/* boolean truth */
79 #ifndef TRUE
80 #define TRUE		1
81 #endif
82 #ifndef FALSE
83 #define FALSE		0
84 #endif
85 
86 	/* lexical definitions */
87 #define	QUOTECHAR	160	/* quote next character */
88 
89 	/* table sizes */
90 #define HASHSIZE	1021
91 #define INMAX		3500
92 
93 	/* expand type definitions */
94 #define E_VARS		0x1
95 #define E_SHELL		0x2
96 #define E_TILDE		0x4
97 #define E_ALL		0x7
98 
99 	/* actions for lookup() */
100 #define LOOKUP		0
101 #define INSERT		1
102 #define REPLACE		2
103 
104 	/* Bit flag test macros */
105 #define IS_ON(b,f)	(b > 0 && (b & f))
106 #define IS_OFF(b,f)	!(IS_ON(b,f))
107 #define FLAG_ON(b,f)	b |= f
108 #define FLAG_OFF(b,f)	b &= ~(f)
109 
110 #define ALLOC(x) 	(struct x *) xmalloc(sizeof(struct x))
111 #define A(s)		((s) ? s : "<null>")
112 
113 /*
114  * Environment variable names
115  */
116 #define E_FILES		"FILES"			/* List of files */
117 #define E_LOCFILE	"FILE"			/* Local Filename  */
118 #define E_REMFILE	"REMFILE"		/* Remote Filename */
119 #define E_BASEFILE	"BASEFILE"		/* basename of Remote File */
120 
121 /*
122  * Suffix to use when saving files
123  */
124 #ifndef SAVE_SUFFIX
125 #define SAVE_SUFFIX	".OLD"
126 #endif
127 
128 /*
129  * Get system error string
130  */
131 #define SYSERR 		strerror(errno)
132 
133 #define COMMENT_CHAR	'#'		/* Config file comment char */
134 #define CNULL		'\0'		/* NULL character */
135 
136 /*
137  * These are the top level protocol commands.
138  */
139 #define C_NONE		'='		/* No command - pass cleanly */
140 #define C_ERRMSG	'\1'		/* Log an error message */
141 #define C_FERRMSG	'\2'		/* Log a fatal error message */
142 #define C_NOTEMSG	'\3'		/* Log a note message */
143 #define C_LOGMSG	'\4'		/* Log a message */
144 #define C_ACK		'\5'		/* Acknowledge */
145 #define C_SETCONFIG	'c'		/* Set configuration parameters */
146 #define C_DIRTARGET	'T'		/* Set target directory name */
147 #define C_TARGET	't'		/* Set target file name */
148 #define C_RECVREG	'R'		/* Receive a regular file */
149 #define C_RECVDIR	'D'		/* Receive a directory */
150 #define C_RECVSYMLINK	'K'		/* Receive a symbolic link */
151 #define C_RECVHARDLINK	'k'		/* Receive a hard link */
152 #define C_END		'E'		/* Indicate end of receive/send */
153 #define C_CLEAN		'C'		/* Clean up */
154 #define C_QUERY		'Q'		/* Query without checking */
155 #define C_SPECIAL	'S'		/* Execute special command */
156 #define C_CMDSPECIAL	's'		/* Execute cmd special command */
157 #define C_CHMOG		'M'		/* Chown,Chgrp,Chmod a file */
158 
159 #define	ack() 		(void) sendcmd(C_ACK, NULL)
160 #define	err() 		(void) sendcmd(C_ERRMSG, NULL)
161 
162 /*
163  * Session startup commands.
164  */
165 #define S_VERSION	'V'		/* Version number */
166 #define S_REMOTEUSER	'R'		/* Remote user name */
167 #define S_LOCALUSER	'L'		/* Local user name */
168 #define S_END		'E'		/* End of session startup commands */
169 
170 /*
171  * These are the commands for "set config".
172  */
173 #define SC_FREESPACE	's'		/* Set min free space */
174 #define SC_FREEFILES	'f'		/* Set min free files */
175 #define SC_HOSTNAME	'H'		/* Set client hostname */
176 #define SC_LOGGING	'L'		/* Set logging options */
177 #define SC_DEFOWNER	'o'		/* Set default owner */
178 #define SC_DEFGROUP	'g'		/* Set default group */
179 
180 /*
181  * Query commands
182  */
183 #define QC_ONNFS	'F'		/* File exists & is on a NFS */
184 #define QC_ONRO		'O'		/* File exists & is on a readonly fs */
185 #define QC_NO		'N'		/* File does not exist */
186 #define QC_SYM		'l'		/* File exists & is a symlink */
187 #define QC_YES		'Y'		/* File does exist */
188 
189 /*
190  * Clean commands
191  */
192 #define CC_QUERY	'Q'		/* Query if file should be rm'ed */
193 #define CC_END		'E'		/* End of cleaning */
194 #define CC_YES		'Y'		/* File doesn't exist - remove */
195 #define CC_NO		'N'		/* File does exist - don't remove */
196 
197 /*
198  * Run Command commands
199  */
200 #define RC_FILE		'F'		/* Name of a target file */
201 #define RC_COMMAND	'C'		/* Command to run */
202 
203 /*
204  * Name list
205  */
206 struct namelist {		/* for making lists of strings */
207 	char	*n_name;
208 	regex_t	*n_regex;
209 	struct	namelist *n_next;
210 };
211 
212 /*
213  * Sub command structure
214  */
215 struct subcmd {
216 	short	sc_type;	/* type - INSTALL,NOTIFY,EXCEPT,SPECIAL */
217 	opt_t	sc_options;
218 	char	*sc_name;
219 	struct	namelist *sc_args;
220 	struct	subcmd *sc_next;
221 };
222 
223 /*
224  * Cmd flags
225  */
226 #define CMD_ASSIGNED	0x01	/* This entry has been assigned */
227 #define CMD_CONNFAILED	0x02	/* Connection failed */
228 #define CMD_NOCHKNFS	0x04	/* Disable NFS checks */
229 
230 /*
231  * General command structure
232  */
233 struct cmd {
234 	int	c_type;		/* type - ARROW,DCOLON */
235 	int	c_flags;	/* flags - CMD_USED,CMD_FAILED */
236 	char	*c_name;	/* hostname or time stamp file name */
237 	char	*c_label;	/* label for partial update */
238 	struct	namelist *c_files;
239 	struct	subcmd *c_cmds;
240 	struct	cmd *c_next;
241 };
242 
243 /*
244  * Hard link buffer information
245  */
246 struct linkbuf {
247 	ino_t	inum;
248 	dev_t	devnum;
249 	int	count;
250 	char	*pathname;
251 	char	*src;
252 	char	*target;
253 	struct	linkbuf *nextp;
254 };
255 
256 extern char	       *optarg;		/* Option argument */
257 extern char	       *path_remsh;	/* Remote shell command */
258 extern char 		host[];		/* Host name of master copy */
259 extern char 	       *currenthost;	/* Name of current host */
260 extern char 	       *progname;	/* Name of this program */
261 extern char 	      **realargv;	/* Real argv */
262 extern int		optind;		/* Option index into argv */
263 extern int 		debug;		/* Debugging flag */
264 extern opt_t 		defoptions;	/* Default install options */
265 extern int 		do_fork;	/* Should we do fork()'ing */
266 extern int 		isserver;	/* Acting as remote server */
267 extern int 		nerrs;		/* Number of errors seen */
268 extern int 		nflag;		/* NOP flag, don't execute commands */
269 extern opt_t 		options;	/* Global options */
270 extern int 		proto_version;	/* Protocol version number */
271 extern int 		realargc;	/* Real argc */
272 extern int		rem_r;		/* Remote file descriptor, reading */
273 extern int 		rem_w;		/* Remote file descriptor, writing */
274 extern int 		rtimeout;	/* Response time out in seconds */
275 extern int		setjmp_ok;	/* setjmp/longjmp flag */
276 extern uid_t 		userid;		/* User ID of rdist user */
277 extern jmp_buf 		finish_jmpbuf;	/* Setjmp buffer for finish() */
278 extern struct linkbuf  *ihead;	/* list of files with more than one link */
279 extern struct passwd   *pw;	/* pointer to static area used by getpwent */
280 extern char defowner[64];		/* Default owner */
281 extern char defgroup[64];		/* Default group */
282 extern volatile sig_atomic_t contimedout; /* Connection timed out */
283 
284 /*
285  * Our own declarations.
286  */
287 
288 /* child.c */
289 void waitup(void);
290 int spawn(struct cmd *, struct cmd *);
291 
292 /* client.c */
293 char *remfilename(char *, char *, char *, char *, int);
294 int inlist(struct namelist *, char *);
295 void runcmdspecial(struct cmd *, opt_t);
296 int checkfilename(char *);
297 void freelinkinfo(struct linkbuf *);
298 void cleanup(int);
299 int install(char *, char *, int, int , opt_t);
300 
301 /* common.c */
302 ssize_t xwrite(int, void *, size_t);
303 int init(int, char **, char **);
304 void finish(void);
305 void lostconn(void);
306 void coredump(void);
307 void sighandler(int);
308 int sendcmd(char, const char *, ...) __attribute__((__format__ (printf, 2, 3)));
309 int remline(u_char *, int, int);
310 ssize_t readrem(char *, ssize_t);
311 char *getusername(uid_t, char *, opt_t);
312 char *getgroupname(gid_t, char *, opt_t);
313 int response(void);
314 char *exptilde(char *, char *, size_t);
315 int becomeuser(void);
316 int becomeroot(void);
317 int setfiletime(char *, time_t, time_t);
318 char *getversion(void);
319 void runcommand(char *);
320 void *xmalloc(size_t);
321 void *xrealloc(void *, size_t);
322 void *xcalloc(size_t, size_t);
323 char *xstrdup(const char *);
324 char *xbasename(char *);
325 char *searchpath(char *);
326 
327 /* distopt.c */
328 DISTOPTINFO *getdistopt(char *, int *);
329 int parsedistopts(char *, opt_t *, int);
330 char *getdistoptlist(void);
331 char *getondistoptlist(opt_t);
332 
333 /* docmd.c */
334 void markassigned(struct cmd *, struct cmd *);
335 int okname(char *);
336 int except(char *);
337 void docmds(struct namelist *, int, char **);
338 
339 /* expand.c */
340 struct namelist *expand(struct namelist *, int);
341 u_char *xstrchr(u_char *, int);
342 void expstr(u_char *);
343 void expsh(u_char *);
344 void matchdir(char *);
345 int execbrc(u_char *, u_char *);
346 int match(char *, char *);
347 int amatch(char *, u_char *);
348 
349 /* filesys.c */
350 char *find_file(char *, struct stat *, int *);
351 mntent_t *findmnt(struct stat *, struct mntinfo *);
352 int isdupmnt(mntent_t *, struct mntinfo *);
353 void wakeup(int);
354 struct mntinfo *makemntinfo(struct mntinfo *);
355 mntent_t *getmntpt(char *, struct stat *, int *);
356 int is_nfs_mounted(char *, struct stat *, int *);
357 int is_ro_mounted(char *, struct stat *, int *);
358 int is_symlinked(char *, struct stat *, int *);
359 int getfilesysinfo(char *, int64_t *, int64_t *);
360 
361 /* gram.c */
362 int yylex(void);
363 int any(int, char *);
364 void insert(char *, struct namelist *, struct namelist *, struct subcmd *);
365 void append(char *, struct namelist *, char *, struct subcmd *);
366 void yyerror(char *);
367 struct namelist *makenl(char *);
368 struct subcmd *makesubcmd(int);
369 int yyparse(void);
370 
371 /* isexec.c */
372 int isexec(char *, struct stat *);
373 
374 /* lookup.c */
375 void define(char *);
376 struct namelist *lookup(char *, int, struct namelist *);
377 
378 /* message.c */
379 void msgprusage(void);
380 void msgprconfig(void);
381 char *msgparseopts(char *, int);
382 void checkhostname(void);
383 void message(int, const char *, ...) __attribute__((format (printf, 2, 3)));
384 void debugmsg(int, const char *, ...) __attribute__((format (printf, 2, 3)));
385 void error(const char *, ...) __attribute__((format (printf, 1, 2)));
386 void fatalerr(const char *, ...) __attribute__((format (printf, 1, 2)));
387 char *getnotifyfile(void);
388 
389 /* rdist.c */
390 FILE *opendist(char *);
391 void docmdargs(int, char *[]);
392 char *getnlstr(struct namelist *);
393 
394 /* server.c */
395 void server(void);
396 
397 #include <vis.h>
398 #define DECODE(a, b)	strunvis(a, b)
399 #define ENCODE(a, b)	strvis(a, b, VIS_WHITE)
400 
401 #endif	/* __DEFS_H__ */
402