Home | History | Annotate | Line # | Download | only in rdist
server.c revision 1.6
      1  1.1  cgd /*
      2  1.5  cgd  * Copyright (c) 1983, 1993
      3  1.5  cgd  *	The Regents of the University of California.  All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6  1.1  cgd  * modification, are permitted provided that the following conditions
      7  1.1  cgd  * are met:
      8  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14  1.1  cgd  *    must display the following acknowledgement:
     15  1.1  cgd  *	This product includes software developed by the University of
     16  1.1  cgd  *	California, Berkeley and its contributors.
     17  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18  1.1  cgd  *    may be used to endorse or promote products derived from this software
     19  1.1  cgd  *    without specific prior written permission.
     20  1.1  cgd  *
     21  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  1.1  cgd  * SUCH DAMAGE.
     32  1.1  cgd  */
     33  1.1  cgd 
     34  1.1  cgd #ifndef lint
     35  1.5  cgd /* from: static char sccsid[] = "@(#)server.c	8.1 (Berkeley) 6/9/93"; */
     36  1.6  cgd static char *rcsid = "$Id: server.c,v 1.6 1994/03/13 13:33:28 cgd Exp $";
     37  1.1  cgd #endif /* not lint */
     38  1.1  cgd 
     39  1.5  cgd #include <sys/wait.h>
     40  1.1  cgd #include "defs.h"
     41  1.1  cgd 
     42  1.1  cgd #define	ack() 	(void) write(rem, "\0\n", 2)
     43  1.1  cgd #define	err() 	(void) write(rem, "\1\n", 2)
     44  1.1  cgd 
     45  1.1  cgd struct	linkbuf *ihead;		/* list of files with more than one link */
     46  1.1  cgd char	buf[BUFSIZ];		/* general purpose buffer */
     47  1.1  cgd char	target[BUFSIZ];		/* target/source directory name */
     48  1.1  cgd char	*tp;			/* pointer to end of target name */
     49  1.1  cgd char	*Tdest;			/* pointer to last T dest*/
     50  1.1  cgd int	catname;		/* cat name to target name */
     51  1.1  cgd char	*stp[32];		/* stack of saved tp's for directories */
     52  1.1  cgd int	oumask;			/* old umask for creating files */
     53  1.1  cgd 
     54  1.1  cgd extern	FILE *lfp;		/* log file for mailing changes */
     55  1.1  cgd 
     56  1.5  cgd static int	chkparent __P((char *));
     57  1.5  cgd static void	clean __P((char *));
     58  1.5  cgd static void	comment __P((char *));
     59  1.5  cgd static void	dospecial __P((char *));
     60  1.5  cgd static int	fchog __P((int, char *, char *, char *, int));
     61  1.5  cgd static void	hardlink __P((char *));
     62  1.5  cgd static void	note __P((const char *, ...));
     63  1.5  cgd static void	query __P((char *));
     64  1.5  cgd static void	recvf __P((char *, int));
     65  1.5  cgd static void	removeit __P((struct stat *));
     66  1.5  cgd static int	response __P((void));
     67  1.5  cgd static void	rmchk __P((int));
     68  1.5  cgd static struct linkbuf *
     69  1.5  cgd 		    savelink __P((struct stat *));
     70  1.5  cgd static void	sendf __P((char *, int));
     71  1.5  cgd static int	update __P((char *, int, struct stat *));
     72  1.1  cgd 
     73  1.1  cgd /*
     74  1.1  cgd  * Server routine to read requests and process them.
     75  1.1  cgd  * Commands are:
     76  1.1  cgd  *	Tname	- Transmit file if out of date
     77  1.1  cgd  *	Vname	- Verify if file out of date or not
     78  1.1  cgd  *	Qname	- Query if file exists. Return mtime & size if it does.
     79  1.1  cgd  */
     80  1.5  cgd void
     81  1.1  cgd server()
     82  1.1  cgd {
     83  1.1  cgd 	char cmdbuf[BUFSIZ];
     84  1.1  cgd 	register char *cp;
     85  1.1  cgd 
     86  1.1  cgd 	signal(SIGHUP, cleanup);
     87  1.1  cgd 	signal(SIGINT, cleanup);
     88  1.1  cgd 	signal(SIGQUIT, cleanup);
     89  1.1  cgd 	signal(SIGTERM, cleanup);
     90  1.1  cgd 	signal(SIGPIPE, cleanup);
     91  1.1  cgd 
     92  1.1  cgd 	rem = 0;
     93  1.1  cgd 	oumask = umask(0);
     94  1.1  cgd 	(void) sprintf(buf, "V%d\n", VERSION);
     95  1.1  cgd 	(void) write(rem, buf, strlen(buf));
     96  1.1  cgd 
     97  1.1  cgd 	for (;;) {
     98  1.1  cgd 		cp = cmdbuf;
     99  1.1  cgd 		if (read(rem, cp, 1) <= 0)
    100  1.1  cgd 			return;
    101  1.1  cgd 		if (*cp++ == '\n') {
    102  1.1  cgd 			error("server: expected control record\n");
    103  1.1  cgd 			continue;
    104  1.1  cgd 		}
    105  1.1  cgd 		do {
    106  1.1  cgd 			if (read(rem, cp, 1) != 1)
    107  1.5  cgd 				cleanup(0);
    108  1.1  cgd 		} while (*cp++ != '\n' && cp < &cmdbuf[BUFSIZ]);
    109  1.1  cgd 		*--cp = '\0';
    110  1.1  cgd 		cp = cmdbuf;
    111  1.1  cgd 		switch (*cp++) {
    112  1.1  cgd 		case 'T':  /* init target file/directory name */
    113  1.1  cgd 			catname = 1;	/* target should be directory */
    114  1.1  cgd 			goto dotarget;
    115  1.1  cgd 
    116  1.1  cgd 		case 't':  /* init target file/directory name */
    117  1.1  cgd 			catname = 0;
    118  1.1  cgd 		dotarget:
    119  1.1  cgd 			if (exptilde(target, cp) == NULL)
    120  1.1  cgd 				continue;
    121  1.1  cgd 			tp = target;
    122  1.1  cgd 			while (*tp)
    123  1.1  cgd 				tp++;
    124  1.1  cgd 			ack();
    125  1.1  cgd 			continue;
    126  1.1  cgd 
    127  1.1  cgd 		case 'R':  /* Transfer a regular file. */
    128  1.1  cgd 			recvf(cp, S_IFREG);
    129  1.1  cgd 			continue;
    130  1.1  cgd 
    131  1.1  cgd 		case 'D':  /* Transfer a directory. */
    132  1.1  cgd 			recvf(cp, S_IFDIR);
    133  1.1  cgd 			continue;
    134  1.1  cgd 
    135  1.1  cgd 		case 'K':  /* Transfer symbolic link. */
    136  1.1  cgd 			recvf(cp, S_IFLNK);
    137  1.1  cgd 			continue;
    138  1.1  cgd 
    139  1.1  cgd 		case 'k':  /* Transfer hard link. */
    140  1.1  cgd 			hardlink(cp);
    141  1.1  cgd 			continue;
    142  1.1  cgd 
    143  1.1  cgd 		case 'E':  /* End. (of directory) */
    144  1.1  cgd 			*tp = '\0';
    145  1.1  cgd 			if (catname <= 0) {
    146  1.1  cgd 				error("server: too many 'E's\n");
    147  1.1  cgd 				continue;
    148  1.1  cgd 			}
    149  1.1  cgd 			tp = stp[--catname];
    150  1.1  cgd 			*tp = '\0';
    151  1.1  cgd 			ack();
    152  1.1  cgd 			continue;
    153  1.1  cgd 
    154  1.1  cgd 		case 'C':  /* Clean. Cleanup a directory */
    155  1.1  cgd 			clean(cp);
    156  1.1  cgd 			continue;
    157  1.1  cgd 
    158  1.1  cgd 		case 'Q':  /* Query. Does the file/directory exist? */
    159  1.1  cgd 			query(cp);
    160  1.1  cgd 			continue;
    161  1.1  cgd 
    162  1.1  cgd 		case 'S':  /* Special. Execute commands */
    163  1.1  cgd 			dospecial(cp);
    164  1.1  cgd 			continue;
    165  1.1  cgd 
    166  1.1  cgd #ifdef notdef
    167  1.1  cgd 		/*
    168  1.1  cgd 		 * These entries are reserved but not currently used.
    169  1.1  cgd 		 * The intent is to allow remote hosts to have master copies.
    170  1.1  cgd 		 * Currently, only the host rdist runs on can have masters.
    171  1.1  cgd 		 */
    172  1.1  cgd 		case 'X':  /* start a new list of files to exclude */
    173  1.1  cgd 			except = bp = NULL;
    174  1.1  cgd 		case 'x':  /* add name to list of files to exclude */
    175  1.1  cgd 			if (*cp == '\0') {
    176  1.1  cgd 				ack();
    177  1.1  cgd 				continue;
    178  1.1  cgd 			}
    179  1.1  cgd 			if (*cp == '~') {
    180  1.1  cgd 				if (exptilde(buf, cp) == NULL)
    181  1.1  cgd 					continue;
    182  1.1  cgd 				cp = buf;
    183  1.1  cgd 			}
    184  1.1  cgd 			if (bp == NULL)
    185  1.1  cgd 				except = bp = expand(makeblock(NAME, cp), E_VARS);
    186  1.1  cgd 			else
    187  1.1  cgd 				bp->b_next = expand(makeblock(NAME, cp), E_VARS);
    188  1.1  cgd 			while (bp->b_next != NULL)
    189  1.1  cgd 				bp = bp->b_next;
    190  1.1  cgd 			ack();
    191  1.1  cgd 			continue;
    192  1.1  cgd 
    193  1.1  cgd 		case 'I':  /* Install. Transfer file if out of date. */
    194  1.1  cgd 			opts = 0;
    195  1.1  cgd 			while (*cp >= '0' && *cp <= '7')
    196  1.1  cgd 				opts = (opts << 3) | (*cp++ - '0');
    197  1.1  cgd 			if (*cp++ != ' ') {
    198  1.1  cgd 				error("server: options not delimited\n");
    199  1.1  cgd 				return;
    200  1.1  cgd 			}
    201  1.1  cgd 			install(cp, opts);
    202  1.1  cgd 			continue;
    203  1.1  cgd 
    204  1.1  cgd 		case 'L':  /* Log. save message in log file */
    205  1.1  cgd 			log(lfp, cp);
    206  1.1  cgd 			continue;
    207  1.1  cgd #endif
    208  1.1  cgd 
    209  1.1  cgd 		case '\1':
    210  1.1  cgd 			nerrs++;
    211  1.1  cgd 			continue;
    212  1.1  cgd 
    213  1.1  cgd 		case '\2':
    214  1.1  cgd 			return;
    215  1.1  cgd 
    216  1.1  cgd 		default:
    217  1.1  cgd 			error("server: unknown command '%s'\n", cp);
    218  1.1  cgd 		case '\0':
    219  1.1  cgd 			continue;
    220  1.1  cgd 		}
    221  1.1  cgd 	}
    222  1.1  cgd }
    223  1.1  cgd 
    224  1.1  cgd /*
    225  1.1  cgd  * Update the file(s) if they are different.
    226  1.1  cgd  * destdir = 1 if destination should be a directory
    227  1.1  cgd  * (i.e., more than one source is being copied to the same destination).
    228  1.1  cgd  */
    229  1.5  cgd void
    230  1.1  cgd install(src, dest, destdir, opts)
    231  1.1  cgd 	char *src, *dest;
    232  1.1  cgd 	int destdir, opts;
    233  1.1  cgd {
    234  1.1  cgd 	char *rname;
    235  1.1  cgd 	char destcopy[BUFSIZ];
    236  1.1  cgd 
    237  1.1  cgd 	if (dest == NULL) {
    238  1.1  cgd 		opts &= ~WHOLE; /* WHOLE mode only useful if renaming */
    239  1.1  cgd 		dest = src;
    240  1.1  cgd 	}
    241  1.1  cgd 
    242  1.1  cgd 	if (nflag || debug) {
    243  1.1  cgd 		printf("%s%s%s%s%s %s %s\n", opts & VERIFY ? "verify":"install",
    244  1.1  cgd 			opts & WHOLE ? " -w" : "",
    245  1.1  cgd 			opts & YOUNGER ? " -y" : "",
    246  1.1  cgd 			opts & COMPARE ? " -b" : "",
    247  1.1  cgd 			opts & REMOVE ? " -R" : "", src, dest);
    248  1.1  cgd 		if (nflag)
    249  1.1  cgd 			return;
    250  1.1  cgd 	}
    251  1.1  cgd 
    252  1.1  cgd 	rname = exptilde(target, src);
    253  1.1  cgd 	if (rname == NULL)
    254  1.1  cgd 		return;
    255  1.1  cgd 	tp = target;
    256  1.1  cgd 	while (*tp)
    257  1.1  cgd 		tp++;
    258  1.1  cgd 	/*
    259  1.1  cgd 	 * If we are renaming a directory and we want to preserve
    260  1.1  cgd 	 * the directory heirarchy (-w), we must strip off the leading
    261  1.1  cgd 	 * directory name and preserve the rest.
    262  1.1  cgd 	 */
    263  1.1  cgd 	if (opts & WHOLE) {
    264  1.1  cgd 		while (*rname == '/')
    265  1.1  cgd 			rname++;
    266  1.1  cgd 		destdir = 1;
    267  1.1  cgd 	} else {
    268  1.1  cgd 		rname = rindex(target, '/');
    269  1.1  cgd 		if (rname == NULL)
    270  1.1  cgd 			rname = target;
    271  1.1  cgd 		else
    272  1.1  cgd 			rname++;
    273  1.1  cgd 	}
    274  1.1  cgd 	if (debug)
    275  1.1  cgd 		printf("target = %s, rname = %s\n", target, rname);
    276  1.1  cgd 	/*
    277  1.1  cgd 	 * Pass the destination file/directory name to remote.
    278  1.1  cgd 	 */
    279  1.1  cgd 	(void) sprintf(buf, "%c%s\n", destdir ? 'T' : 't', dest);
    280  1.1  cgd 	if (debug)
    281  1.1  cgd 		printf("buf = %s", buf);
    282  1.1  cgd 	(void) write(rem, buf, strlen(buf));
    283  1.1  cgd 	if (response() < 0)
    284  1.1  cgd 		return;
    285  1.1  cgd 
    286  1.1  cgd 	if (destdir) {
    287  1.1  cgd 		strcpy(destcopy, dest);
    288  1.1  cgd 		Tdest = destcopy;
    289  1.1  cgd 	}
    290  1.1  cgd 	sendf(rname, opts);
    291  1.1  cgd 	Tdest = 0;
    292  1.1  cgd }
    293  1.1  cgd 
    294  1.1  cgd #define protoname() (pw ? pw->pw_name : user)
    295  1.1  cgd #define protogroup() (gr ? gr->gr_name : group)
    296  1.1  cgd /*
    297  1.1  cgd  * Transfer the file or directory in target[].
    298  1.1  cgd  * rname is the name of the file on the remote host.
    299  1.1  cgd  */
    300  1.5  cgd static void
    301  1.1  cgd sendf(rname, opts)
    302  1.1  cgd 	char *rname;
    303  1.1  cgd 	int opts;
    304  1.1  cgd {
    305  1.1  cgd 	register struct subcmd *sc;
    306  1.1  cgd 	struct stat stb;
    307  1.1  cgd 	int sizerr, f, u, len;
    308  1.1  cgd 	off_t i;
    309  1.1  cgd 	DIR *d;
    310  1.5  cgd 	struct direct *dp;
    311  1.1  cgd 	char *otp, *cp;
    312  1.1  cgd 	extern struct subcmd *subcmds;
    313  1.1  cgd 	static char user[15], group[15];
    314  1.1  cgd 
    315  1.1  cgd 	if (debug)
    316  1.1  cgd 		printf("sendf(%s, %x)\n", rname, opts);
    317  1.1  cgd 
    318  1.1  cgd 	if (except(target))
    319  1.1  cgd 		return;
    320  1.1  cgd 	if ((opts & FOLLOW ? stat(target, &stb) : lstat(target, &stb)) < 0) {
    321  1.1  cgd 		error("%s: %s\n", target, strerror(errno));
    322  1.1  cgd 		return;
    323  1.1  cgd 	}
    324  1.1  cgd 	if ((u = update(rname, opts, &stb)) == 0) {
    325  1.1  cgd 		if ((stb.st_mode & S_IFMT) == S_IFREG && stb.st_nlink > 1)
    326  1.1  cgd 			(void) savelink(&stb);
    327  1.1  cgd 		return;
    328  1.1  cgd 	}
    329  1.1  cgd 
    330  1.1  cgd 	if (pw == NULL || pw->pw_uid != stb.st_uid)
    331  1.1  cgd 		if ((pw = getpwuid(stb.st_uid)) == NULL) {
    332  1.1  cgd 			log(lfp, "%s: no password entry for uid %d \n",
    333  1.1  cgd 				target, stb.st_uid);
    334  1.1  cgd 			pw = NULL;
    335  1.5  cgd 			(void)sprintf(user, ":%lu", stb.st_uid);
    336  1.1  cgd 		}
    337  1.1  cgd 	if (gr == NULL || gr->gr_gid != stb.st_gid)
    338  1.1  cgd 		if ((gr = getgrgid(stb.st_gid)) == NULL) {
    339  1.1  cgd 			log(lfp, "%s: no name for group %d\n",
    340  1.1  cgd 				target, stb.st_gid);
    341  1.1  cgd 			gr = NULL;
    342  1.5  cgd 			(void)sprintf(group, ":%lu", stb.st_gid);
    343  1.1  cgd 		}
    344  1.1  cgd 	if (u == 1) {
    345  1.1  cgd 		if (opts & VERIFY) {
    346  1.1  cgd 			log(lfp, "need to install: %s\n", target);
    347  1.1  cgd 			goto dospecial;
    348  1.1  cgd 		}
    349  1.1  cgd 		log(lfp, "installing: %s\n", target);
    350  1.1  cgd 		opts &= ~(COMPARE|REMOVE);
    351  1.1  cgd 	}
    352  1.1  cgd 
    353  1.1  cgd 	switch (stb.st_mode & S_IFMT) {
    354  1.1  cgd 	case S_IFDIR:
    355  1.1  cgd 		if ((d = opendir(target)) == NULL) {
    356  1.1  cgd 			error("%s: %s\n", target, strerror(errno));
    357  1.1  cgd 			return;
    358  1.1  cgd 		}
    359  1.1  cgd 		(void) sprintf(buf, "D%o %04o 0 0 %s %s %s\n", opts,
    360  1.1  cgd 			stb.st_mode & 07777, protoname(), protogroup(), rname);
    361  1.1  cgd 		if (debug)
    362  1.1  cgd 			printf("buf = %s", buf);
    363  1.1  cgd 		(void) write(rem, buf, strlen(buf));
    364  1.1  cgd 		if (response() < 0) {
    365  1.1  cgd 			closedir(d);
    366  1.1  cgd 			return;
    367  1.1  cgd 		}
    368  1.1  cgd 
    369  1.1  cgd 		if (opts & REMOVE)
    370  1.1  cgd 			rmchk(opts);
    371  1.1  cgd 
    372  1.1  cgd 		otp = tp;
    373  1.1  cgd 		len = tp - target;
    374  1.1  cgd 		while (dp = readdir(d)) {
    375  1.1  cgd 			if (!strcmp(dp->d_name, ".") ||
    376  1.1  cgd 			    !strcmp(dp->d_name, ".."))
    377  1.1  cgd 				continue;
    378  1.1  cgd 			if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
    379  1.1  cgd 				error("%s/%s: Name too long\n", target,
    380  1.1  cgd 					dp->d_name);
    381  1.1  cgd 				continue;
    382  1.1  cgd 			}
    383  1.1  cgd 			tp = otp;
    384  1.1  cgd 			*tp++ = '/';
    385  1.1  cgd 			cp = dp->d_name;
    386  1.1  cgd 			while (*tp++ = *cp++)
    387  1.1  cgd 				;
    388  1.1  cgd 			tp--;
    389  1.1  cgd 			sendf(dp->d_name, opts);
    390  1.1  cgd 		}
    391  1.1  cgd 		closedir(d);
    392  1.1  cgd 		(void) write(rem, "E\n", 2);
    393  1.1  cgd 		(void) response();
    394  1.1  cgd 		tp = otp;
    395  1.1  cgd 		*tp = '\0';
    396  1.1  cgd 		return;
    397  1.1  cgd 
    398  1.1  cgd 	case S_IFLNK:
    399  1.1  cgd 		if (u != 1)
    400  1.1  cgd 			opts |= COMPARE;
    401  1.1  cgd 		if (stb.st_nlink > 1) {
    402  1.1  cgd 			struct linkbuf *lp;
    403  1.1  cgd 
    404  1.1  cgd 			if ((lp = savelink(&stb)) != NULL) {
    405  1.1  cgd 				/* install link */
    406  1.1  cgd 				if (*lp->target == 0)
    407  1.1  cgd 				(void) sprintf(buf, "k%o %s %s\n", opts,
    408  1.1  cgd 					lp->pathname, rname);
    409  1.1  cgd 				else
    410  1.1  cgd 				(void) sprintf(buf, "k%o %s/%s %s\n", opts,
    411  1.1  cgd 					lp->target, lp->pathname, rname);
    412  1.1  cgd 				if (debug)
    413  1.1  cgd 					printf("buf = %s", buf);
    414  1.1  cgd 				(void) write(rem, buf, strlen(buf));
    415  1.1  cgd 				(void) response();
    416  1.1  cgd 				return;
    417  1.1  cgd 			}
    418  1.1  cgd 		}
    419  1.5  cgd 		(void) sprintf(buf, "K%o %o %qd %ld %s %s %s\n", opts,
    420  1.6  cgd 			stb.st_mode & 07777, (quad_t)stb.st_size, stb.st_mtime,
    421  1.1  cgd 			protoname(), protogroup(), rname);
    422  1.1  cgd 		if (debug)
    423  1.1  cgd 			printf("buf = %s", buf);
    424  1.1  cgd 		(void) write(rem, buf, strlen(buf));
    425  1.1  cgd 		if (response() < 0)
    426  1.1  cgd 			return;
    427  1.1  cgd 		sizerr = (readlink(target, buf, BUFSIZ) != stb.st_size);
    428  1.1  cgd 		(void) write(rem, buf, stb.st_size);
    429  1.1  cgd 		if (debug)
    430  1.1  cgd 			printf("readlink = %.*s\n", (int)stb.st_size, buf);
    431  1.1  cgd 		goto done;
    432  1.1  cgd 
    433  1.1  cgd 	case S_IFREG:
    434  1.1  cgd 		break;
    435  1.1  cgd 
    436  1.1  cgd 	default:
    437  1.1  cgd 		error("%s: not a file or directory\n", target);
    438  1.1  cgd 		return;
    439  1.1  cgd 	}
    440  1.1  cgd 
    441  1.1  cgd 	if (u == 2) {
    442  1.1  cgd 		if (opts & VERIFY) {
    443  1.1  cgd 			log(lfp, "need to update: %s\n", target);
    444  1.1  cgd 			goto dospecial;
    445  1.1  cgd 		}
    446  1.1  cgd 		log(lfp, "updating: %s\n", target);
    447  1.1  cgd 	}
    448  1.1  cgd 
    449  1.1  cgd 	if (stb.st_nlink > 1) {
    450  1.1  cgd 		struct linkbuf *lp;
    451  1.1  cgd 
    452  1.1  cgd 		if ((lp = savelink(&stb)) != NULL) {
    453  1.1  cgd 			/* install link */
    454  1.1  cgd 			if (*lp->target == 0)
    455  1.1  cgd 			(void) sprintf(buf, "k%o %s %s\n", opts,
    456  1.1  cgd 				lp->pathname, rname);
    457  1.1  cgd 			else
    458  1.1  cgd 			(void) sprintf(buf, "k%o %s/%s %s\n", opts,
    459  1.1  cgd 				lp->target, lp->pathname, rname);
    460  1.1  cgd 			if (debug)
    461  1.1  cgd 				printf("buf = %s", buf);
    462  1.1  cgd 			(void) write(rem, buf, strlen(buf));
    463  1.1  cgd 			(void) response();
    464  1.1  cgd 			return;
    465  1.1  cgd 		}
    466  1.1  cgd 	}
    467  1.1  cgd 
    468  1.5  cgd 	if ((f = open(target, O_RDONLY, 0)) < 0) {
    469  1.1  cgd 		error("%s: %s\n", target, strerror(errno));
    470  1.1  cgd 		return;
    471  1.1  cgd 	}
    472  1.5  cgd 	(void) sprintf(buf, "R%o %o %qd %ld %s %s %s\n", opts,
    473  1.6  cgd 		stb.st_mode & 07777, (quad_t)stb.st_size, stb.st_mtime,
    474  1.1  cgd 		protoname(), protogroup(), rname);
    475  1.1  cgd 	if (debug)
    476  1.1  cgd 		printf("buf = %s", buf);
    477  1.1  cgd 	(void) write(rem, buf, strlen(buf));
    478  1.1  cgd 	if (response() < 0) {
    479  1.1  cgd 		(void) close(f);
    480  1.1  cgd 		return;
    481  1.1  cgd 	}
    482  1.1  cgd 	sizerr = 0;
    483  1.1  cgd 	for (i = 0; i < stb.st_size; i += BUFSIZ) {
    484  1.1  cgd 		int amt = BUFSIZ;
    485  1.1  cgd 		if (i + amt > stb.st_size)
    486  1.1  cgd 			amt = stb.st_size - i;
    487  1.1  cgd 		if (sizerr == 0 && read(f, buf, amt) != amt)
    488  1.1  cgd 			sizerr = 1;
    489  1.1  cgd 		(void) write(rem, buf, amt);
    490  1.1  cgd 	}
    491  1.1  cgd 	(void) close(f);
    492  1.1  cgd done:
    493  1.1  cgd 	if (sizerr) {
    494  1.1  cgd 		error("%s: file changed size\n", target);
    495  1.1  cgd 		err();
    496  1.1  cgd 	} else
    497  1.1  cgd 		ack();
    498  1.1  cgd 	f = response();
    499  1.1  cgd 	if (f < 0 || f == 0 && (opts & COMPARE))
    500  1.1  cgd 		return;
    501  1.1  cgd dospecial:
    502  1.1  cgd 	for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
    503  1.1  cgd 		if (sc->sc_type != SPECIAL)
    504  1.1  cgd 			continue;
    505  1.1  cgd 		if (sc->sc_args != NULL && !inlist(sc->sc_args, target))
    506  1.1  cgd 			continue;
    507  1.1  cgd 		log(lfp, "special \"%s\"\n", sc->sc_name);
    508  1.1  cgd 		if (opts & VERIFY)
    509  1.1  cgd 			continue;
    510  1.1  cgd 		(void) sprintf(buf, "SFILE=%s;%s\n", target, sc->sc_name);
    511  1.1  cgd 		if (debug)
    512  1.1  cgd 			printf("buf = %s", buf);
    513  1.1  cgd 		(void) write(rem, buf, strlen(buf));
    514  1.1  cgd 		while (response() > 0)
    515  1.1  cgd 			;
    516  1.1  cgd 	}
    517  1.1  cgd }
    518  1.1  cgd 
    519  1.5  cgd static struct linkbuf *
    520  1.1  cgd savelink(stp)
    521  1.1  cgd 	struct stat *stp;
    522  1.1  cgd {
    523  1.1  cgd 	struct linkbuf *lp;
    524  1.1  cgd 
    525  1.1  cgd 	for (lp = ihead; lp != NULL; lp = lp->nextp)
    526  1.1  cgd 		if (lp->inum == stp->st_ino && lp->devnum == stp->st_dev) {
    527  1.1  cgd 			lp->count--;
    528  1.1  cgd 			return(lp);
    529  1.1  cgd 		}
    530  1.1  cgd 	lp = (struct linkbuf *) malloc(sizeof(*lp));
    531  1.1  cgd 	if (lp == NULL)
    532  1.1  cgd 		log(lfp, "out of memory, link information lost\n");
    533  1.1  cgd 	else {
    534  1.1  cgd 		lp->nextp = ihead;
    535  1.1  cgd 		ihead = lp;
    536  1.1  cgd 		lp->inum = stp->st_ino;
    537  1.1  cgd 		lp->devnum = stp->st_dev;
    538  1.1  cgd 		lp->count = stp->st_nlink - 1;
    539  1.1  cgd 		strcpy(lp->pathname, target);
    540  1.1  cgd 		if (Tdest)
    541  1.1  cgd 			strcpy(lp->target, Tdest);
    542  1.1  cgd 		else
    543  1.1  cgd 			*lp->target = 0;
    544  1.1  cgd 	}
    545  1.1  cgd 	return(NULL);
    546  1.1  cgd }
    547  1.1  cgd 
    548  1.1  cgd /*
    549  1.1  cgd  * Check to see if file needs to be updated on the remote machine.
    550  1.1  cgd  * Returns 0 if no update, 1 if remote doesn't exist, 2 if out of date
    551  1.1  cgd  * and 3 if comparing binaries to determine if out of date.
    552  1.1  cgd  */
    553  1.5  cgd static int
    554  1.1  cgd update(rname, opts, stp)
    555  1.1  cgd 	char *rname;
    556  1.1  cgd 	int opts;
    557  1.1  cgd 	struct stat *stp;
    558  1.1  cgd {
    559  1.1  cgd 	register char *cp, *s;
    560  1.1  cgd 	register off_t size;
    561  1.1  cgd 	register time_t mtime;
    562  1.1  cgd 
    563  1.1  cgd 	if (debug)
    564  1.1  cgd 		printf("update(%s, %x, %x)\n", rname, opts, stp);
    565  1.1  cgd 
    566  1.1  cgd 	/*
    567  1.1  cgd 	 * Check to see if the file exists on the remote machine.
    568  1.1  cgd 	 */
    569  1.1  cgd 	(void) sprintf(buf, "Q%s\n", rname);
    570  1.1  cgd 	if (debug)
    571  1.1  cgd 		printf("buf = %s", buf);
    572  1.1  cgd 	(void) write(rem, buf, strlen(buf));
    573  1.1  cgd again:
    574  1.1  cgd 	cp = s = buf;
    575  1.1  cgd 	do {
    576  1.1  cgd 		if (read(rem, cp, 1) != 1)
    577  1.5  cgd 			lostconn(0);
    578  1.1  cgd 	} while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
    579  1.1  cgd 
    580  1.1  cgd 	switch (*s++) {
    581  1.1  cgd 	case 'Y':
    582  1.1  cgd 		break;
    583  1.1  cgd 
    584  1.1  cgd 	case 'N':  /* file doesn't exist so install it */
    585  1.1  cgd 		return(1);
    586  1.1  cgd 
    587  1.1  cgd 	case '\1':
    588  1.1  cgd 		nerrs++;
    589  1.1  cgd 		if (*s != '\n') {
    590  1.1  cgd 			if (!iamremote) {
    591  1.1  cgd 				fflush(stdout);
    592  1.1  cgd 				(void) write(2, s, cp - s);
    593  1.1  cgd 			}
    594  1.1  cgd 			if (lfp != NULL)
    595  1.1  cgd 				(void) fwrite(s, 1, cp - s, lfp);
    596  1.1  cgd 		}
    597  1.1  cgd 		return(0);
    598  1.1  cgd 
    599  1.1  cgd 	case '\3':
    600  1.1  cgd 		*--cp = '\0';
    601  1.1  cgd 		if (lfp != NULL)
    602  1.1  cgd 			log(lfp, "update: note: %s\n", s);
    603  1.1  cgd 		goto again;
    604  1.1  cgd 
    605  1.1  cgd 	default:
    606  1.1  cgd 		*--cp = '\0';
    607  1.1  cgd 		error("update: unexpected response '%s'\n", s);
    608  1.1  cgd 		return(0);
    609  1.1  cgd 	}
    610  1.1  cgd 
    611  1.1  cgd 	if (*s == '\n')
    612  1.1  cgd 		return(2);
    613  1.1  cgd 
    614  1.1  cgd 	if (opts & COMPARE)
    615  1.1  cgd 		return(3);
    616  1.1  cgd 
    617  1.1  cgd 	size = 0;
    618  1.1  cgd 	while (isdigit(*s))
    619  1.1  cgd 		size = size * 10 + (*s++ - '0');
    620  1.1  cgd 	if (*s++ != ' ') {
    621  1.1  cgd 		error("update: size not delimited\n");
    622  1.1  cgd 		return(0);
    623  1.1  cgd 	}
    624  1.1  cgd 	mtime = 0;
    625  1.1  cgd 	while (isdigit(*s))
    626  1.1  cgd 		mtime = mtime * 10 + (*s++ - '0');
    627  1.1  cgd 	if (*s != '\n') {
    628  1.1  cgd 		error("update: mtime not delimited\n");
    629  1.1  cgd 		return(0);
    630  1.1  cgd 	}
    631  1.1  cgd 	/*
    632  1.1  cgd 	 * File needs to be updated?
    633  1.1  cgd 	 */
    634  1.1  cgd 	if (opts & YOUNGER) {
    635  1.1  cgd 		if (stp->st_mtime == mtime)
    636  1.1  cgd 			return(0);
    637  1.1  cgd 		if (stp->st_mtime < mtime) {
    638  1.1  cgd 			log(lfp, "Warning: %s: remote copy is newer\n", target);
    639  1.1  cgd 			return(0);
    640  1.1  cgd 		}
    641  1.1  cgd 	} else if (stp->st_mtime == mtime && stp->st_size == size)
    642  1.1  cgd 		return(0);
    643  1.1  cgd 	return(2);
    644  1.1  cgd }
    645  1.1  cgd 
    646  1.1  cgd /*
    647  1.1  cgd  * Query. Check to see if file exists. Return one of the following:
    648  1.1  cgd  *	N\n		- doesn't exist
    649  1.1  cgd  *	Ysize mtime\n	- exists and its a regular file (size & mtime of file)
    650  1.1  cgd  *	Y\n		- exists and its a directory or symbolic link
    651  1.1  cgd  *	^Aerror message\n
    652  1.1  cgd  */
    653  1.5  cgd static void
    654  1.1  cgd query(name)
    655  1.1  cgd 	char *name;
    656  1.1  cgd {
    657  1.1  cgd 	struct stat stb;
    658  1.1  cgd 
    659  1.1  cgd 	if (catname)
    660  1.1  cgd 		(void) sprintf(tp, "/%s", name);
    661  1.1  cgd 
    662  1.1  cgd 	if (lstat(target, &stb) < 0) {
    663  1.1  cgd 		if (errno == ENOENT)
    664  1.1  cgd 			(void) write(rem, "N\n", 2);
    665  1.1  cgd 		else
    666  1.1  cgd 			error("%s:%s: %s\n", host, target, strerror(errno));
    667  1.1  cgd 		*tp = '\0';
    668  1.1  cgd 		return;
    669  1.1  cgd 	}
    670  1.1  cgd 
    671  1.1  cgd 	switch (stb.st_mode & S_IFMT) {
    672  1.1  cgd 	case S_IFREG:
    673  1.6  cgd 		(void) sprintf(buf, "Y%qd %ld\n", (quad_t)stb.st_size,
    674  1.6  cgd 		    stb.st_mtime);
    675  1.1  cgd 		(void) write(rem, buf, strlen(buf));
    676  1.1  cgd 		break;
    677  1.1  cgd 
    678  1.1  cgd 	case S_IFLNK:
    679  1.1  cgd 	case S_IFDIR:
    680  1.1  cgd 		(void) write(rem, "Y\n", 2);
    681  1.1  cgd 		break;
    682  1.1  cgd 
    683  1.1  cgd 	default:
    684  1.1  cgd 		error("%s: not a file or directory\n", name);
    685  1.1  cgd 		break;
    686  1.1  cgd 	}
    687  1.1  cgd 	*tp = '\0';
    688  1.1  cgd }
    689  1.1  cgd 
    690  1.5  cgd static void
    691  1.1  cgd recvf(cmd, type)
    692  1.1  cgd 	char *cmd;
    693  1.1  cgd 	int type;
    694  1.1  cgd {
    695  1.1  cgd 	register char *cp;
    696  1.1  cgd 	int f, mode, opts, wrerr, olderrno;
    697  1.1  cgd 	off_t i, size;
    698  1.1  cgd 	time_t mtime;
    699  1.1  cgd 	struct stat stb;
    700  1.1  cgd 	struct timeval tvp[2];
    701  1.1  cgd 	char *owner, *group;
    702  1.1  cgd 	char new[BUFSIZ];
    703  1.1  cgd 	extern char *tempname;
    704  1.1  cgd 
    705  1.1  cgd 	cp = cmd;
    706  1.1  cgd 	opts = 0;
    707  1.1  cgd 	while (*cp >= '0' && *cp <= '7')
    708  1.1  cgd 		opts = (opts << 3) | (*cp++ - '0');
    709  1.1  cgd 	if (*cp++ != ' ') {
    710  1.1  cgd 		error("recvf: options not delimited\n");
    711  1.1  cgd 		return;
    712  1.1  cgd 	}
    713  1.1  cgd 	mode = 0;
    714  1.1  cgd 	while (*cp >= '0' && *cp <= '7')
    715  1.1  cgd 		mode = (mode << 3) | (*cp++ - '0');
    716  1.1  cgd 	if (*cp++ != ' ') {
    717  1.1  cgd 		error("recvf: mode not delimited\n");
    718  1.1  cgd 		return;
    719  1.1  cgd 	}
    720  1.1  cgd 	size = 0;
    721  1.1  cgd 	while (isdigit(*cp))
    722  1.1  cgd 		size = size * 10 + (*cp++ - '0');
    723  1.1  cgd 	if (*cp++ != ' ') {
    724  1.1  cgd 		error("recvf: size not delimited\n");
    725  1.1  cgd 		return;
    726  1.1  cgd 	}
    727  1.1  cgd 	mtime = 0;
    728  1.1  cgd 	while (isdigit(*cp))
    729  1.1  cgd 		mtime = mtime * 10 + (*cp++ - '0');
    730  1.1  cgd 	if (*cp++ != ' ') {
    731  1.1  cgd 		error("recvf: mtime not delimited\n");
    732  1.1  cgd 		return;
    733  1.1  cgd 	}
    734  1.1  cgd 	owner = cp;
    735  1.1  cgd 	while (*cp && *cp != ' ')
    736  1.1  cgd 		cp++;
    737  1.1  cgd 	if (*cp != ' ') {
    738  1.1  cgd 		error("recvf: owner name not delimited\n");
    739  1.1  cgd 		return;
    740  1.1  cgd 	}
    741  1.1  cgd 	*cp++ = '\0';
    742  1.1  cgd 	group = cp;
    743  1.1  cgd 	while (*cp && *cp != ' ')
    744  1.1  cgd 		cp++;
    745  1.1  cgd 	if (*cp != ' ') {
    746  1.1  cgd 		error("recvf: group name not delimited\n");
    747  1.1  cgd 		return;
    748  1.1  cgd 	}
    749  1.1  cgd 	*cp++ = '\0';
    750  1.1  cgd 
    751  1.1  cgd 	if (type == S_IFDIR) {
    752  1.1  cgd 		if (catname >= sizeof(stp)) {
    753  1.1  cgd 			error("%s:%s: too many directory levels\n",
    754  1.1  cgd 				host, target);
    755  1.1  cgd 			return;
    756  1.1  cgd 		}
    757  1.1  cgd 		stp[catname] = tp;
    758  1.1  cgd 		if (catname++) {
    759  1.1  cgd 			*tp++ = '/';
    760  1.1  cgd 			while (*tp++ = *cp++)
    761  1.1  cgd 				;
    762  1.1  cgd 			tp--;
    763  1.1  cgd 		}
    764  1.1  cgd 		if (opts & VERIFY) {
    765  1.1  cgd 			ack();
    766  1.1  cgd 			return;
    767  1.1  cgd 		}
    768  1.1  cgd 		if (lstat(target, &stb) == 0) {
    769  1.1  cgd 			if (ISDIR(stb.st_mode)) {
    770  1.1  cgd 				if ((stb.st_mode & 07777) == mode) {
    771  1.1  cgd 					ack();
    772  1.1  cgd 					return;
    773  1.1  cgd 				}
    774  1.1  cgd 				buf[0] = '\0';
    775  1.1  cgd 				(void) sprintf(buf + 1,
    776  1.1  cgd 					"%s: Warning: remote mode %o != local mode %o\n",
    777  1.1  cgd 					target, stb.st_mode & 07777, mode);
    778  1.1  cgd 				(void) write(rem, buf, strlen(buf + 1) + 1);
    779  1.1  cgd 				return;
    780  1.1  cgd 			}
    781  1.1  cgd 			errno = ENOTDIR;
    782  1.1  cgd 		} else if (errno == ENOENT && (mkdir(target, mode) == 0 ||
    783  1.1  cgd 		    chkparent(target) == 0 && mkdir(target, mode) == 0)) {
    784  1.5  cgd 			if (fchog(-1, target, owner, group, mode) == 0)
    785  1.1  cgd 				ack();
    786  1.1  cgd 			return;
    787  1.1  cgd 		}
    788  1.1  cgd 		error("%s:%s: %s\n", host, target, strerror(errno));
    789  1.1  cgd 		tp = stp[--catname];
    790  1.1  cgd 		*tp = '\0';
    791  1.1  cgd 		return;
    792  1.1  cgd 	}
    793  1.1  cgd 
    794  1.1  cgd 	if (catname)
    795  1.1  cgd 		(void) sprintf(tp, "/%s", cp);
    796  1.1  cgd 	cp = rindex(target, '/');
    797  1.1  cgd 	if (cp == NULL)
    798  1.1  cgd 		strcpy(new, tempname);
    799  1.1  cgd 	else if (cp == target)
    800  1.1  cgd 		(void) sprintf(new, "/%s", tempname);
    801  1.1  cgd 	else {
    802  1.1  cgd 		*cp = '\0';
    803  1.1  cgd 		(void) sprintf(new, "%s/%s", target, tempname);
    804  1.1  cgd 		*cp = '/';
    805  1.1  cgd 	}
    806  1.1  cgd 
    807  1.1  cgd 	if (type == S_IFLNK) {
    808  1.1  cgd 		int j;
    809  1.1  cgd 
    810  1.1  cgd 		ack();
    811  1.1  cgd 		cp = buf;
    812  1.1  cgd 		for (i = 0; i < size; i += j) {
    813  1.1  cgd 			if ((j = read(rem, cp, size - i)) <= 0)
    814  1.5  cgd 				cleanup(0);
    815  1.1  cgd 			cp += j;
    816  1.1  cgd 		}
    817  1.1  cgd 		*cp = '\0';
    818  1.1  cgd 		if (response() < 0) {
    819  1.1  cgd 			err();
    820  1.1  cgd 			return;
    821  1.1  cgd 		}
    822  1.1  cgd 		if (symlink(buf, new) < 0) {
    823  1.1  cgd 			if (errno != ENOENT || chkparent(new) < 0 ||
    824  1.1  cgd 			    symlink(buf, new) < 0)
    825  1.5  cgd 				goto badnew1;
    826  1.1  cgd 		}
    827  1.1  cgd 		mode &= 0777;
    828  1.1  cgd 		if (opts & COMPARE) {
    829  1.1  cgd 			char tbuf[BUFSIZ];
    830  1.1  cgd 
    831  1.1  cgd 			if ((i = readlink(target, tbuf, BUFSIZ)) >= 0 &&
    832  1.1  cgd 			    i == size && strncmp(buf, tbuf, size) == 0) {
    833  1.1  cgd 				(void) unlink(new);
    834  1.1  cgd 				ack();
    835  1.1  cgd 				return;
    836  1.1  cgd 			}
    837  1.1  cgd 			if (opts & VERIFY)
    838  1.1  cgd 				goto differ;
    839  1.1  cgd 		}
    840  1.1  cgd 		goto fixup;
    841  1.1  cgd 	}
    842  1.1  cgd 
    843  1.1  cgd 	if ((f = creat(new, mode)) < 0) {
    844  1.1  cgd 		if (errno != ENOENT || chkparent(new) < 0 ||
    845  1.1  cgd 		    (f = creat(new, mode)) < 0)
    846  1.5  cgd 			goto badnew1;
    847  1.1  cgd 	}
    848  1.1  cgd 
    849  1.1  cgd 	ack();
    850  1.1  cgd 	wrerr = 0;
    851  1.1  cgd 	for (i = 0; i < size; i += BUFSIZ) {
    852  1.1  cgd 		int amt = BUFSIZ;
    853  1.1  cgd 
    854  1.1  cgd 		cp = buf;
    855  1.1  cgd 		if (i + amt > size)
    856  1.1  cgd 			amt = size - i;
    857  1.1  cgd 		do {
    858  1.1  cgd 			int j = read(rem, cp, amt);
    859  1.1  cgd 
    860  1.1  cgd 			if (j <= 0) {
    861  1.1  cgd 				(void) close(f);
    862  1.1  cgd 				(void) unlink(new);
    863  1.5  cgd 				cleanup(0);
    864  1.1  cgd 			}
    865  1.1  cgd 			amt -= j;
    866  1.1  cgd 			cp += j;
    867  1.1  cgd 		} while (amt > 0);
    868  1.1  cgd 		amt = BUFSIZ;
    869  1.1  cgd 		if (i + amt > size)
    870  1.1  cgd 			amt = size - i;
    871  1.1  cgd 		if (wrerr == 0 && write(f, buf, amt) != amt) {
    872  1.1  cgd 			olderrno = errno;
    873  1.1  cgd 			wrerr++;
    874  1.1  cgd 		}
    875  1.1  cgd 	}
    876  1.1  cgd 	if (response() < 0) {
    877  1.1  cgd 		err();
    878  1.5  cgd 		goto badnew2;
    879  1.1  cgd 	}
    880  1.5  cgd 	if (wrerr)
    881  1.5  cgd 		goto badnew1;
    882  1.1  cgd 	if (opts & COMPARE) {
    883  1.1  cgd 		FILE *f1, *f2;
    884  1.1  cgd 		int c;
    885  1.1  cgd 
    886  1.1  cgd 		if ((f1 = fopen(target, "r")) == NULL)
    887  1.5  cgd 			goto badtarget;
    888  1.1  cgd 		if ((f2 = fopen(new, "r")) == NULL) {
    889  1.5  cgd badnew1:		error("%s:%s: %s\n", host, new, strerror(errno));
    890  1.5  cgd 			goto badnew2;
    891  1.1  cgd 		}
    892  1.1  cgd 		while ((c = getc(f1)) == getc(f2))
    893  1.1  cgd 			if (c == EOF) {
    894  1.1  cgd 				(void) fclose(f1);
    895  1.1  cgd 				(void) fclose(f2);
    896  1.1  cgd 				ack();
    897  1.5  cgd 				goto badnew2;
    898  1.1  cgd 			}
    899  1.1  cgd 		(void) fclose(f1);
    900  1.1  cgd 		(void) fclose(f2);
    901  1.1  cgd 		if (opts & VERIFY) {
    902  1.5  cgd differ:			buf[0] = '\0';
    903  1.1  cgd 			(void) sprintf(buf + 1, "need to update: %s\n",target);
    904  1.1  cgd 			(void) write(rem, buf, strlen(buf + 1) + 1);
    905  1.5  cgd 			goto badnew2;
    906  1.1  cgd 		}
    907  1.1  cgd 	}
    908  1.1  cgd 
    909  1.1  cgd 	/*
    910  1.1  cgd 	 * Set last modified time
    911  1.1  cgd 	 */
    912  1.5  cgd 	tvp[0].tv_sec = time(0);
    913  1.1  cgd 	tvp[0].tv_usec = 0;
    914  1.1  cgd 	tvp[1].tv_sec = mtime;
    915  1.1  cgd 	tvp[1].tv_usec = 0;
    916  1.5  cgd 	if (utimes(new, tvp) < 0)
    917  1.5  cgd 		note("%s: utimes failed %s: %s\n", host, new, strerror(errno));
    918  1.5  cgd 
    919  1.5  cgd 	if (fchog(f, new, owner, group, mode) < 0) {
    920  1.5  cgd badnew2:	(void) close(f);
    921  1.1  cgd 		(void) unlink(new);
    922  1.1  cgd 		return;
    923  1.1  cgd 	}
    924  1.5  cgd 	(void) close(f);
    925  1.5  cgd 
    926  1.5  cgd fixup:	if (rename(new, target) < 0) {
    927  1.5  cgd badtarget:	error("%s:%s: %s\n", host, target, strerror(errno));
    928  1.1  cgd 		(void) unlink(new);
    929  1.1  cgd 		return;
    930  1.1  cgd 	}
    931  1.5  cgd 
    932  1.1  cgd 	if (opts & COMPARE) {
    933  1.1  cgd 		buf[0] = '\0';
    934  1.1  cgd 		(void) sprintf(buf + 1, "updated %s\n", target);
    935  1.1  cgd 		(void) write(rem, buf, strlen(buf + 1) + 1);
    936  1.1  cgd 	} else
    937  1.1  cgd 		ack();
    938  1.1  cgd }
    939  1.1  cgd 
    940  1.1  cgd /*
    941  1.1  cgd  * Creat a hard link to existing file.
    942  1.1  cgd  */
    943  1.5  cgd static void
    944  1.1  cgd hardlink(cmd)
    945  1.1  cgd 	char *cmd;
    946  1.1  cgd {
    947  1.1  cgd 	register char *cp;
    948  1.1  cgd 	struct stat stb;
    949  1.1  cgd 	char *oldname;
    950  1.1  cgd 	int opts, exists = 0;
    951  1.1  cgd 
    952  1.1  cgd 	cp = cmd;
    953  1.1  cgd 	opts = 0;
    954  1.1  cgd 	while (*cp >= '0' && *cp <= '7')
    955  1.1  cgd 		opts = (opts << 3) | (*cp++ - '0');
    956  1.1  cgd 	if (*cp++ != ' ') {
    957  1.1  cgd 		error("hardlink: options not delimited\n");
    958  1.1  cgd 		return;
    959  1.1  cgd 	}
    960  1.1  cgd 	oldname = cp;
    961  1.1  cgd 	while (*cp && *cp != ' ')
    962  1.1  cgd 		cp++;
    963  1.1  cgd 	if (*cp != ' ') {
    964  1.1  cgd 		error("hardlink: oldname name not delimited\n");
    965  1.1  cgd 		return;
    966  1.1  cgd 	}
    967  1.1  cgd 	*cp++ = '\0';
    968  1.1  cgd 
    969  1.1  cgd 	if (catname) {
    970  1.1  cgd 		(void) sprintf(tp, "/%s", cp);
    971  1.1  cgd 	}
    972  1.1  cgd 	if (lstat(target, &stb) == 0) {
    973  1.1  cgd 		int mode = stb.st_mode & S_IFMT;
    974  1.1  cgd 		if (mode != S_IFREG && mode != S_IFLNK) {
    975  1.1  cgd 			error("%s:%s: not a regular file\n", host, target);
    976  1.1  cgd 			return;
    977  1.1  cgd 		}
    978  1.1  cgd 		exists = 1;
    979  1.1  cgd 	}
    980  1.1  cgd 	if (chkparent(target) < 0 ) {
    981  1.1  cgd 		error("%s:%s: %s (no parent)\n",
    982  1.1  cgd 			host, target, strerror(errno));
    983  1.1  cgd 		return;
    984  1.1  cgd 	}
    985  1.1  cgd 	if (exists && (unlink(target) < 0)) {
    986  1.1  cgd 		error("%s:%s: %s (unlink)\n",
    987  1.1  cgd 			host, target, strerror(errno));
    988  1.1  cgd 		return;
    989  1.1  cgd 	}
    990  1.1  cgd 	if (link(oldname, target) < 0) {
    991  1.1  cgd 		error("%s:can't link %s to %s\n",
    992  1.1  cgd 			host, target, oldname);
    993  1.1  cgd 		return;
    994  1.1  cgd 	}
    995  1.1  cgd 	ack();
    996  1.1  cgd }
    997  1.1  cgd 
    998  1.1  cgd /*
    999  1.1  cgd  * Check to see if parent directory exists and create one if not.
   1000  1.1  cgd  */
   1001  1.5  cgd static int
   1002  1.1  cgd chkparent(name)
   1003  1.1  cgd 	char *name;
   1004  1.1  cgd {
   1005  1.1  cgd 	register char *cp;
   1006  1.1  cgd 	struct stat stb;
   1007  1.1  cgd 
   1008  1.1  cgd 	cp = rindex(name, '/');
   1009  1.1  cgd 	if (cp == NULL || cp == name)
   1010  1.1  cgd 		return(0);
   1011  1.1  cgd 	*cp = '\0';
   1012  1.1  cgd 	if (lstat(name, &stb) < 0) {
   1013  1.1  cgd 		if (errno == ENOENT && chkparent(name) >= 0 &&
   1014  1.1  cgd 		    mkdir(name, 0777 & ~oumask) >= 0) {
   1015  1.1  cgd 			*cp = '/';
   1016  1.1  cgd 			return(0);
   1017  1.1  cgd 		}
   1018  1.1  cgd 	} else if (ISDIR(stb.st_mode)) {
   1019  1.1  cgd 		*cp = '/';
   1020  1.1  cgd 		return(0);
   1021  1.1  cgd 	}
   1022  1.1  cgd 	*cp = '/';
   1023  1.1  cgd 	return(-1);
   1024  1.1  cgd }
   1025  1.1  cgd 
   1026  1.1  cgd /*
   1027  1.1  cgd  * Change owner, group and mode of file.
   1028  1.1  cgd  */
   1029  1.5  cgd static int
   1030  1.5  cgd fchog(fd, file, owner, group, mode)
   1031  1.5  cgd 	int fd;
   1032  1.1  cgd 	char *file, *owner, *group;
   1033  1.1  cgd 	int mode;
   1034  1.1  cgd {
   1035  1.1  cgd 	register int i;
   1036  1.1  cgd 	int uid, gid;
   1037  1.1  cgd 	extern char user[];
   1038  1.1  cgd 	extern int userid;
   1039  1.1  cgd 
   1040  1.1  cgd 	uid = userid;
   1041  1.1  cgd 	if (userid == 0) {
   1042  1.1  cgd 		if (*owner == ':') {
   1043  1.1  cgd 			uid = atoi(owner + 1);
   1044  1.1  cgd 		} else if (pw == NULL || strcmp(owner, pw->pw_name) != 0) {
   1045  1.1  cgd 			if ((pw = getpwnam(owner)) == NULL) {
   1046  1.1  cgd 				if (mode & 04000) {
   1047  1.1  cgd 					note("%s:%s: unknown login name, clearing setuid",
   1048  1.1  cgd 						host, owner);
   1049  1.1  cgd 					mode &= ~04000;
   1050  1.1  cgd 					uid = 0;
   1051  1.1  cgd 				}
   1052  1.1  cgd 			} else
   1053  1.1  cgd 				uid = pw->pw_uid;
   1054  1.1  cgd 		} else
   1055  1.1  cgd 			uid = pw->pw_uid;
   1056  1.1  cgd 		if (*group == ':') {
   1057  1.1  cgd 			gid = atoi(group + 1);
   1058  1.1  cgd 			goto ok;
   1059  1.1  cgd 		}
   1060  1.1  cgd 	} else if ((mode & 04000) && strcmp(user, owner) != 0)
   1061  1.1  cgd 		mode &= ~04000;
   1062  1.1  cgd 	gid = -1;
   1063  1.1  cgd 	if (gr == NULL || strcmp(group, gr->gr_name) != 0) {
   1064  1.1  cgd 		if ((*group == ':' && (getgrgid(gid = atoi(group + 1)) == NULL))
   1065  1.1  cgd 		   || ((gr = getgrnam(group)) == NULL)) {
   1066  1.1  cgd 			if (mode & 02000) {
   1067  1.1  cgd 				note("%s:%s: unknown group", host, group);
   1068  1.1  cgd 				mode &= ~02000;
   1069  1.1  cgd 			}
   1070  1.1  cgd 		} else
   1071  1.1  cgd 			gid = gr->gr_gid;
   1072  1.1  cgd 	} else
   1073  1.1  cgd 		gid = gr->gr_gid;
   1074  1.1  cgd 	if (userid && gid >= 0) {
   1075  1.1  cgd 		if (gr) for (i = 0; gr->gr_mem[i] != NULL; i++)
   1076  1.1  cgd 			if (!(strcmp(user, gr->gr_mem[i])))
   1077  1.1  cgd 				goto ok;
   1078  1.1  cgd 		mode &= ~02000;
   1079  1.1  cgd 		gid = -1;
   1080  1.1  cgd 	}
   1081  1.5  cgd ok:	if (fd != -1 && fchown(fd, uid, gid) < 0 || chown(file, uid, gid) < 0)
   1082  1.5  cgd 		note("%s: %s chown: %s", host, file, strerror(errno));
   1083  1.5  cgd 	else if (mode & 07000 &&
   1084  1.5  cgd 	   (fd != -1 && fchmod(fd, mode) < 0 || chmod(file, mode) < 0))
   1085  1.5  cgd 		note("%s: %s chmod: %s", host, file, strerror(errno));
   1086  1.1  cgd 	return(0);
   1087  1.1  cgd }
   1088  1.1  cgd 
   1089  1.1  cgd /*
   1090  1.1  cgd  * Check for files on the machine being updated that are not on the master
   1091  1.1  cgd  * machine and remove them.
   1092  1.1  cgd  */
   1093  1.5  cgd static void
   1094  1.1  cgd rmchk(opts)
   1095  1.1  cgd 	int opts;
   1096  1.1  cgd {
   1097  1.1  cgd 	register char *cp, *s;
   1098  1.1  cgd 	struct stat stb;
   1099  1.1  cgd 
   1100  1.1  cgd 	if (debug)
   1101  1.1  cgd 		printf("rmchk()\n");
   1102  1.1  cgd 
   1103  1.1  cgd 	/*
   1104  1.1  cgd 	 * Tell the remote to clean the files from the last directory sent.
   1105  1.1  cgd 	 */
   1106  1.1  cgd 	(void) sprintf(buf, "C%o\n", opts & VERIFY);
   1107  1.1  cgd 	if (debug)
   1108  1.1  cgd 		printf("buf = %s", buf);
   1109  1.1  cgd 	(void) write(rem, buf, strlen(buf));
   1110  1.1  cgd 	if (response() < 0)
   1111  1.1  cgd 		return;
   1112  1.1  cgd 	for (;;) {
   1113  1.1  cgd 		cp = s = buf;
   1114  1.1  cgd 		do {
   1115  1.1  cgd 			if (read(rem, cp, 1) != 1)
   1116  1.5  cgd 				lostconn(0);
   1117  1.1  cgd 		} while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
   1118  1.1  cgd 
   1119  1.1  cgd 		switch (*s++) {
   1120  1.1  cgd 		case 'Q': /* Query if file should be removed */
   1121  1.1  cgd 			/*
   1122  1.1  cgd 			 * Return the following codes to remove query.
   1123  1.1  cgd 			 * N\n -- file exists - DON'T remove.
   1124  1.1  cgd 			 * Y\n -- file doesn't exist - REMOVE.
   1125  1.1  cgd 			 */
   1126  1.1  cgd 			*--cp = '\0';
   1127  1.1  cgd 			(void) sprintf(tp, "/%s", s);
   1128  1.1  cgd 			if (debug)
   1129  1.1  cgd 				printf("check %s\n", target);
   1130  1.1  cgd 			if (except(target))
   1131  1.1  cgd 				(void) write(rem, "N\n", 2);
   1132  1.1  cgd 			else if (lstat(target, &stb) < 0)
   1133  1.1  cgd 				(void) write(rem, "Y\n", 2);
   1134  1.1  cgd 			else
   1135  1.1  cgd 				(void) write(rem, "N\n", 2);
   1136  1.1  cgd 			break;
   1137  1.1  cgd 
   1138  1.1  cgd 		case '\0':
   1139  1.1  cgd 			*--cp = '\0';
   1140  1.1  cgd 			if (*s != '\0')
   1141  1.1  cgd 				log(lfp, "%s\n", s);
   1142  1.1  cgd 			break;
   1143  1.1  cgd 
   1144  1.1  cgd 		case 'E':
   1145  1.1  cgd 			*tp = '\0';
   1146  1.1  cgd 			ack();
   1147  1.1  cgd 			return;
   1148  1.1  cgd 
   1149  1.1  cgd 		case '\1':
   1150  1.1  cgd 		case '\2':
   1151  1.1  cgd 			nerrs++;
   1152  1.1  cgd 			if (*s != '\n') {
   1153  1.1  cgd 				if (!iamremote) {
   1154  1.1  cgd 					fflush(stdout);
   1155  1.1  cgd 					(void) write(2, s, cp - s);
   1156  1.1  cgd 				}
   1157  1.1  cgd 				if (lfp != NULL)
   1158  1.1  cgd 					(void) fwrite(s, 1, cp - s, lfp);
   1159  1.1  cgd 			}
   1160  1.1  cgd 			if (buf[0] == '\2')
   1161  1.5  cgd 				lostconn(0);
   1162  1.1  cgd 			break;
   1163  1.1  cgd 
   1164  1.1  cgd 		default:
   1165  1.1  cgd 			error("rmchk: unexpected response '%s'\n", buf);
   1166  1.1  cgd 			err();
   1167  1.1  cgd 		}
   1168  1.1  cgd 	}
   1169  1.1  cgd }
   1170  1.1  cgd 
   1171  1.1  cgd /*
   1172  1.1  cgd  * Check the current directory (initialized by the 'T' command to server())
   1173  1.1  cgd  * for extraneous files and remove them.
   1174  1.1  cgd  */
   1175  1.5  cgd static void
   1176  1.1  cgd clean(cp)
   1177  1.1  cgd 	register char *cp;
   1178  1.1  cgd {
   1179  1.1  cgd 	DIR *d;
   1180  1.5  cgd 	register struct direct *dp;
   1181  1.1  cgd 	struct stat stb;
   1182  1.1  cgd 	char *otp;
   1183  1.1  cgd 	int len, opts;
   1184  1.1  cgd 
   1185  1.1  cgd 	opts = 0;
   1186  1.1  cgd 	while (*cp >= '0' && *cp <= '7')
   1187  1.1  cgd 		opts = (opts << 3) | (*cp++ - '0');
   1188  1.1  cgd 	if (*cp != '\0') {
   1189  1.1  cgd 		error("clean: options not delimited\n");
   1190  1.1  cgd 		return;
   1191  1.1  cgd 	}
   1192  1.1  cgd 	if ((d = opendir(target)) == NULL) {
   1193  1.1  cgd 		error("%s:%s: %s\n", host, target, strerror(errno));
   1194  1.1  cgd 		return;
   1195  1.1  cgd 	}
   1196  1.1  cgd 	ack();
   1197  1.1  cgd 
   1198  1.1  cgd 	otp = tp;
   1199  1.1  cgd 	len = tp - target;
   1200  1.1  cgd 	while (dp = readdir(d)) {
   1201  1.1  cgd 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
   1202  1.1  cgd 			continue;
   1203  1.1  cgd 		if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
   1204  1.1  cgd 			error("%s:%s/%s: Name too long\n",
   1205  1.1  cgd 				host, target, dp->d_name);
   1206  1.1  cgd 			continue;
   1207  1.1  cgd 		}
   1208  1.1  cgd 		tp = otp;
   1209  1.1  cgd 		*tp++ = '/';
   1210  1.1  cgd 		cp = dp->d_name;;
   1211  1.1  cgd 		while (*tp++ = *cp++)
   1212  1.1  cgd 			;
   1213  1.1  cgd 		tp--;
   1214  1.1  cgd 		if (lstat(target, &stb) < 0) {
   1215  1.1  cgd 			error("%s:%s: %s\n", host, target, strerror(errno));
   1216  1.1  cgd 			continue;
   1217  1.1  cgd 		}
   1218  1.1  cgd 		(void) sprintf(buf, "Q%s\n", dp->d_name);
   1219  1.1  cgd 		(void) write(rem, buf, strlen(buf));
   1220  1.1  cgd 		cp = buf;
   1221  1.1  cgd 		do {
   1222  1.1  cgd 			if (read(rem, cp, 1) != 1)
   1223  1.5  cgd 				cleanup(0);
   1224  1.1  cgd 		} while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
   1225  1.1  cgd 		*--cp = '\0';
   1226  1.1  cgd 		cp = buf;
   1227  1.1  cgd 		if (*cp != 'Y')
   1228  1.1  cgd 			continue;
   1229  1.1  cgd 		if (opts & VERIFY) {
   1230  1.1  cgd 			cp = buf;
   1231  1.1  cgd 			*cp++ = '\0';
   1232  1.1  cgd 			(void) sprintf(cp, "need to remove: %s\n", target);
   1233  1.1  cgd 			(void) write(rem, buf, strlen(cp) + 1);
   1234  1.1  cgd 		} else
   1235  1.1  cgd 			removeit(&stb);
   1236  1.1  cgd 	}
   1237  1.1  cgd 	closedir(d);
   1238  1.1  cgd 	(void) write(rem, "E\n", 2);
   1239  1.1  cgd 	(void) response();
   1240  1.1  cgd 	tp = otp;
   1241  1.1  cgd 	*tp = '\0';
   1242  1.1  cgd }
   1243  1.1  cgd 
   1244  1.1  cgd /*
   1245  1.1  cgd  * Remove a file or directory (recursively) and send back an acknowledge
   1246  1.1  cgd  * or an error message.
   1247  1.1  cgd  */
   1248  1.5  cgd static void
   1249  1.1  cgd removeit(stp)
   1250  1.1  cgd 	struct stat *stp;
   1251  1.1  cgd {
   1252  1.1  cgd 	DIR *d;
   1253  1.5  cgd 	struct direct *dp;
   1254  1.1  cgd 	register char *cp;
   1255  1.1  cgd 	struct stat stb;
   1256  1.1  cgd 	char *otp;
   1257  1.1  cgd 	int len;
   1258  1.1  cgd 
   1259  1.1  cgd 	switch (stp->st_mode & S_IFMT) {
   1260  1.1  cgd 	case S_IFREG:
   1261  1.1  cgd 	case S_IFLNK:
   1262  1.1  cgd 		if (unlink(target) < 0)
   1263  1.1  cgd 			goto bad;
   1264  1.1  cgd 		goto removed;
   1265  1.1  cgd 
   1266  1.1  cgd 	case S_IFDIR:
   1267  1.1  cgd 		break;
   1268  1.1  cgd 
   1269  1.1  cgd 	default:
   1270  1.1  cgd 		error("%s:%s: not a plain file\n", host, target);
   1271  1.1  cgd 		return;
   1272  1.1  cgd 	}
   1273  1.1  cgd 
   1274  1.1  cgd 	if ((d = opendir(target)) == NULL)
   1275  1.1  cgd 		goto bad;
   1276  1.1  cgd 
   1277  1.1  cgd 	otp = tp;
   1278  1.1  cgd 	len = tp - target;
   1279  1.1  cgd 	while (dp = readdir(d)) {
   1280  1.1  cgd 		if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
   1281  1.1  cgd 			continue;
   1282  1.1  cgd 		if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
   1283  1.1  cgd 			error("%s:%s/%s: Name too long\n",
   1284  1.1  cgd 				host, target, dp->d_name);
   1285  1.1  cgd 			continue;
   1286  1.1  cgd 		}
   1287  1.1  cgd 		tp = otp;
   1288  1.1  cgd 		*tp++ = '/';
   1289  1.1  cgd 		cp = dp->d_name;;
   1290  1.1  cgd 		while (*tp++ = *cp++)
   1291  1.1  cgd 			;
   1292  1.1  cgd 		tp--;
   1293  1.1  cgd 		if (lstat(target, &stb) < 0) {
   1294  1.1  cgd 			error("%s:%s: %s\n", host, target, strerror(errno));
   1295  1.1  cgd 			continue;
   1296  1.1  cgd 		}
   1297  1.1  cgd 		removeit(&stb);
   1298  1.1  cgd 	}
   1299  1.1  cgd 	closedir(d);
   1300  1.1  cgd 	tp = otp;
   1301  1.1  cgd 	*tp = '\0';
   1302  1.1  cgd 	if (rmdir(target) < 0) {
   1303  1.1  cgd bad:
   1304  1.1  cgd 		error("%s:%s: %s\n", host, target, strerror(errno));
   1305  1.1  cgd 		return;
   1306  1.1  cgd 	}
   1307  1.1  cgd removed:
   1308  1.1  cgd 	cp = buf;
   1309  1.1  cgd 	*cp++ = '\0';
   1310  1.1  cgd 	(void) sprintf(cp, "removed %s\n", target);
   1311  1.1  cgd 	(void) write(rem, buf, strlen(cp) + 1);
   1312  1.1  cgd }
   1313  1.1  cgd 
   1314  1.1  cgd /*
   1315  1.1  cgd  * Execute a shell command to handle special cases.
   1316  1.1  cgd  */
   1317  1.5  cgd static void
   1318  1.1  cgd dospecial(cmd)
   1319  1.1  cgd 	char *cmd;
   1320  1.1  cgd {
   1321  1.1  cgd 	int fd[2], status, pid, i;
   1322  1.1  cgd 	register char *cp, *s;
   1323  1.1  cgd 	char sbuf[BUFSIZ];
   1324  1.1  cgd 	extern int userid, groupid;
   1325  1.1  cgd 
   1326  1.1  cgd 	if (pipe(fd) < 0) {
   1327  1.1  cgd 		error("%s\n", strerror(errno));
   1328  1.1  cgd 		return;
   1329  1.1  cgd 	}
   1330  1.1  cgd 	if ((pid = fork()) == 0) {
   1331  1.1  cgd 		/*
   1332  1.1  cgd 		 * Return everything the shell commands print.
   1333  1.1  cgd 		 */
   1334  1.1  cgd 		(void) close(0);
   1335  1.1  cgd 		(void) close(1);
   1336  1.1  cgd 		(void) close(2);
   1337  1.1  cgd 		(void) open(_PATH_DEVNULL, O_RDONLY);
   1338  1.1  cgd 		(void) dup(fd[1]);
   1339  1.1  cgd 		(void) dup(fd[1]);
   1340  1.1  cgd 		(void) close(fd[0]);
   1341  1.1  cgd 		(void) close(fd[1]);
   1342  1.1  cgd 		setgid(groupid);
   1343  1.1  cgd 		setuid(userid);
   1344  1.1  cgd 		execl(_PATH_BSHELL, "sh", "-c", cmd, 0);
   1345  1.1  cgd 		_exit(127);
   1346  1.1  cgd 	}
   1347  1.1  cgd 	(void) close(fd[1]);
   1348  1.1  cgd 	s = sbuf;
   1349  1.1  cgd 	*s++ = '\0';
   1350  1.1  cgd 	while ((i = read(fd[0], buf, sizeof(buf))) > 0) {
   1351  1.1  cgd 		cp = buf;
   1352  1.1  cgd 		do {
   1353  1.1  cgd 			*s++ = *cp++;
   1354  1.1  cgd 			if (cp[-1] != '\n') {
   1355  1.1  cgd 				if (s < &sbuf[sizeof(sbuf)-1])
   1356  1.1  cgd 					continue;
   1357  1.1  cgd 				*s++ = '\n';
   1358  1.1  cgd 			}
   1359  1.1  cgd 			/*
   1360  1.1  cgd 			 * Throw away blank lines.
   1361  1.1  cgd 			 */
   1362  1.1  cgd 			if (s == &sbuf[2]) {
   1363  1.1  cgd 				s--;
   1364  1.1  cgd 				continue;
   1365  1.1  cgd 			}
   1366  1.1  cgd 			(void) write(rem, sbuf, s - sbuf);
   1367  1.1  cgd 			s = &sbuf[1];
   1368  1.1  cgd 		} while (--i);
   1369  1.1  cgd 	}
   1370  1.1  cgd 	if (s > &sbuf[1]) {
   1371  1.1  cgd 		*s++ = '\n';
   1372  1.1  cgd 		(void) write(rem, sbuf, s - sbuf);
   1373  1.1  cgd 	}
   1374  1.1  cgd 	while ((i = wait(&status)) != pid && i != -1)
   1375  1.1  cgd 		;
   1376  1.1  cgd 	if (i == -1)
   1377  1.1  cgd 		status = -1;
   1378  1.1  cgd 	(void) close(fd[0]);
   1379  1.1  cgd 	if (status)
   1380  1.1  cgd 		error("shell returned %d\n", status);
   1381  1.1  cgd 	else
   1382  1.1  cgd 		ack();
   1383  1.1  cgd }
   1384  1.1  cgd 
   1385  1.5  cgd #if __STDC__
   1386  1.5  cgd #include <stdarg.h>
   1387  1.5  cgd #else
   1388  1.5  cgd #include <varargs.h>
   1389  1.5  cgd #endif
   1390  1.5  cgd 
   1391  1.5  cgd void
   1392  1.5  cgd #if __STDC__
   1393  1.5  cgd log(FILE *fp, const char *fmt, ...)
   1394  1.5  cgd #else
   1395  1.5  cgd log(fp, fmt, va_alist)
   1396  1.1  cgd 	FILE *fp;
   1397  1.1  cgd 	char *fmt;
   1398  1.5  cgd         va_dcl
   1399  1.5  cgd #endif
   1400  1.1  cgd {
   1401  1.5  cgd 	va_list ap;
   1402  1.5  cgd #if __STDC__
   1403  1.5  cgd 	va_start(ap, fmt);
   1404  1.5  cgd #else
   1405  1.5  cgd 	va_start(ap);
   1406  1.5  cgd #endif
   1407  1.1  cgd 	/* Print changes locally if not quiet mode */
   1408  1.1  cgd 	if (!qflag)
   1409  1.5  cgd 		(void)vprintf(fmt, ap);
   1410  1.1  cgd 
   1411  1.1  cgd 	/* Save changes (for mailing) if really updating files */
   1412  1.1  cgd 	if (!(options & VERIFY) && fp != NULL)
   1413  1.5  cgd 		(void)vfprintf(fp, fmt, ap);
   1414  1.5  cgd 	va_end(ap);
   1415  1.1  cgd }
   1416  1.1  cgd 
   1417  1.5  cgd void
   1418  1.5  cgd #if __STDC__
   1419  1.5  cgd error(const char *fmt, ...)
   1420  1.5  cgd #else
   1421  1.5  cgd error(fmt, va_alist)
   1422  1.1  cgd 	char *fmt;
   1423  1.5  cgd         va_dcl
   1424  1.5  cgd #endif
   1425  1.1  cgd {
   1426  1.1  cgd 	static FILE *fp;
   1427  1.5  cgd 	va_list ap;
   1428  1.5  cgd #if __STDC__
   1429  1.5  cgd 	va_start(ap, fmt);
   1430  1.5  cgd #else
   1431  1.5  cgd 	va_start(ap);
   1432  1.5  cgd #endif
   1433  1.1  cgd 
   1434  1.1  cgd 	++nerrs;
   1435  1.1  cgd 	if (!fp && !(fp = fdopen(rem, "w")))
   1436  1.1  cgd 		return;
   1437  1.1  cgd 	if (iamremote) {
   1438  1.1  cgd 		(void)fprintf(fp, "%crdist: ", 0x01);
   1439  1.5  cgd 		(void)vfprintf(fp, fmt, ap);
   1440  1.1  cgd 		fflush(fp);
   1441  1.1  cgd 	}
   1442  1.1  cgd 	else {
   1443  1.1  cgd 		fflush(stdout);
   1444  1.1  cgd 		(void)fprintf(stderr, "rdist: ");
   1445  1.5  cgd 		(void)vfprintf(stderr, fmt, ap);
   1446  1.1  cgd 		fflush(stderr);
   1447  1.1  cgd 	}
   1448  1.1  cgd 	if (lfp != NULL) {
   1449  1.1  cgd 		(void)fprintf(lfp, "rdist: ");
   1450  1.5  cgd 		(void)vfprintf(lfp, fmt, ap);
   1451  1.1  cgd 		fflush(lfp);
   1452  1.1  cgd 	}
   1453  1.5  cgd 	va_end(ap);
   1454  1.1  cgd }
   1455  1.1  cgd 
   1456  1.5  cgd void
   1457  1.5  cgd #if __STDC__
   1458  1.5  cgd fatal(const char *fmt, ...)
   1459  1.5  cgd #else
   1460  1.5  cgd fatal(fmt, va_alist)
   1461  1.1  cgd 	char *fmt;
   1462  1.5  cgd         va_dcl
   1463  1.5  cgd #endif
   1464  1.1  cgd {
   1465  1.1  cgd 	static FILE *fp;
   1466  1.5  cgd 	va_list ap;
   1467  1.5  cgd #if __STDC__
   1468  1.5  cgd 	va_start(ap, fmt);
   1469  1.5  cgd #else
   1470  1.5  cgd 	va_start(ap);
   1471  1.5  cgd #endif
   1472  1.1  cgd 
   1473  1.1  cgd 	++nerrs;
   1474  1.1  cgd 	if (!fp && !(fp = fdopen(rem, "w")))
   1475  1.1  cgd 		return;
   1476  1.1  cgd 	if (iamremote) {
   1477  1.1  cgd 		(void)fprintf(fp, "%crdist: ", 0x02);
   1478  1.5  cgd 		(void)vfprintf(fp, fmt, ap);
   1479  1.1  cgd 		fflush(fp);
   1480  1.1  cgd 	}
   1481  1.1  cgd 	else {
   1482  1.1  cgd 		fflush(stdout);
   1483  1.1  cgd 		(void)fprintf(stderr, "rdist: ");
   1484  1.5  cgd 		(void)vfprintf(stderr, fmt, ap);
   1485  1.1  cgd 		fflush(stderr);
   1486  1.1  cgd 	}
   1487  1.1  cgd 	if (lfp != NULL) {
   1488  1.1  cgd 		(void)fprintf(lfp, "rdist: ");
   1489  1.5  cgd 		(void)vfprintf(lfp, fmt, ap);
   1490  1.1  cgd 		fflush(lfp);
   1491  1.1  cgd 	}
   1492  1.5  cgd 	cleanup(0);
   1493  1.1  cgd }
   1494  1.1  cgd 
   1495  1.5  cgd static int
   1496  1.1  cgd response()
   1497  1.1  cgd {
   1498  1.1  cgd 	char *cp, *s;
   1499  1.1  cgd 	char resp[BUFSIZ];
   1500  1.1  cgd 
   1501  1.1  cgd 	if (debug)
   1502  1.1  cgd 		printf("response()\n");
   1503  1.1  cgd 
   1504  1.1  cgd 	cp = s = resp;
   1505  1.1  cgd 	do {
   1506  1.1  cgd 		if (read(rem, cp, 1) != 1)
   1507  1.5  cgd 			lostconn(0);
   1508  1.1  cgd 	} while (*cp++ != '\n' && cp < &resp[BUFSIZ]);
   1509  1.1  cgd 
   1510  1.1  cgd 	switch (*s++) {
   1511  1.1  cgd 	case '\0':
   1512  1.1  cgd 		*--cp = '\0';
   1513  1.1  cgd 		if (*s != '\0') {
   1514  1.1  cgd 			log(lfp, "%s\n", s);
   1515  1.1  cgd 			return(1);
   1516  1.1  cgd 		}
   1517  1.1  cgd 		return(0);
   1518  1.1  cgd 	case '\3':
   1519  1.1  cgd 		*--cp = '\0';
   1520  1.1  cgd 		log(lfp, "Note: %s\n",s);
   1521  1.1  cgd 		return(response());
   1522  1.1  cgd 
   1523  1.1  cgd 	default:
   1524  1.1  cgd 		s--;
   1525  1.1  cgd 		/* fall into... */
   1526  1.1  cgd 	case '\1':
   1527  1.1  cgd 	case '\2':
   1528  1.1  cgd 		nerrs++;
   1529  1.1  cgd 		if (*s != '\n') {
   1530  1.1  cgd 			if (!iamremote) {
   1531  1.1  cgd 				fflush(stdout);
   1532  1.1  cgd 				(void) write(2, s, cp - s);
   1533  1.1  cgd 			}
   1534  1.1  cgd 			if (lfp != NULL)
   1535  1.1  cgd 				(void) fwrite(s, 1, cp - s, lfp);
   1536  1.1  cgd 		}
   1537  1.1  cgd 		if (resp[0] == '\2')
   1538  1.5  cgd 			lostconn(0);
   1539  1.1  cgd 		return(-1);
   1540  1.1  cgd 	}
   1541  1.1  cgd }
   1542  1.1  cgd 
   1543  1.1  cgd /*
   1544  1.1  cgd  * Remove temporary files and do any cleanup operations before exiting.
   1545  1.1  cgd  */
   1546  1.1  cgd void
   1547  1.5  cgd cleanup(signo)
   1548  1.5  cgd 	int signo;
   1549  1.1  cgd {
   1550  1.1  cgd 	(void) unlink(tempfile);
   1551  1.1  cgd 	exit(1);
   1552  1.1  cgd }
   1553  1.1  cgd 
   1554  1.5  cgd static void
   1555  1.5  cgd #if __STDC__
   1556  1.5  cgd note(const char *fmt, ...)
   1557  1.5  cgd #else
   1558  1.5  cgd note(fmt, va_alist)
   1559  1.1  cgd 	char *fmt;
   1560  1.5  cgd         va_dcl
   1561  1.5  cgd #endif
   1562  1.1  cgd {
   1563  1.1  cgd 	static char buf[BUFSIZ];
   1564  1.5  cgd 	va_list ap;
   1565  1.5  cgd #if __STDC__
   1566  1.5  cgd 	va_start(ap, fmt);
   1567  1.5  cgd #else
   1568  1.5  cgd 	va_start(ap);
   1569  1.5  cgd #endif
   1570  1.5  cgd 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
   1571  1.5  cgd 	va_end(ap);
   1572  1.1  cgd 	comment(buf);
   1573  1.1  cgd }
   1574  1.1  cgd 
   1575  1.5  cgd static void
   1576  1.1  cgd comment(s)
   1577  1.5  cgd 	char *s;
   1578  1.1  cgd {
   1579  1.5  cgd 	char c;
   1580  1.5  cgd 
   1581  1.5  cgd 	c = '\3';
   1582  1.1  cgd 	write(rem, &c, 1);
   1583  1.1  cgd 	write(rem, s, strlen(s));
   1584  1.1  cgd 	c = '\n';
   1585  1.1  cgd 	write(rem, &c, 1);
   1586  1.1  cgd }
   1587