xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.bin/rdist/server.c (revision 11621:08daccd04ea4)
10Sstevel@tonic-gate /*
2*11621SChandan.Bn@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate /*
70Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
80Sstevel@tonic-gate  * All rights reserved.
90Sstevel@tonic-gate  *
100Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
110Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
120Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
130Sstevel@tonic-gate  * advertising materials, and other materials related to such
140Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
150Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
160Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
170Sstevel@tonic-gate  * from this software without specific prior written permission.
180Sstevel@tonic-gate  */
190Sstevel@tonic-gate 
200Sstevel@tonic-gate #include "defs.h"
210Sstevel@tonic-gate #include <signal.h>
220Sstevel@tonic-gate #include <string.h>
230Sstevel@tonic-gate #include <errno.h>
240Sstevel@tonic-gate #include <limits.h>
250Sstevel@tonic-gate #include <ctype.h>
260Sstevel@tonic-gate #include <krb5defs.h>
270Sstevel@tonic-gate 
280Sstevel@tonic-gate /*
290Sstevel@tonic-gate  * If we want to write *to* the client rdist program, *from* the server
300Sstevel@tonic-gate  * side (server-side child `rdist -Server' process exec'ed off of in.rshd),
310Sstevel@tonic-gate  * we write to stdout/stderr, since there is a pipe connecting stdout/stderr
320Sstevel@tonic-gate  * to the outside world (which is why we use `wrem' and not `rem').
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate int wrem = 1;
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #define	ack() 	(void) write(wrem, "\0\n", 2)
370Sstevel@tonic-gate #define	err() 	(void) write(wrem, "\1\n", 2)
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
400Sstevel@tonic-gate  * Set when a desread() is reqd. in response()
410Sstevel@tonic-gate  */
420Sstevel@tonic-gate 
430Sstevel@tonic-gate struct	linkbuf *ihead;		/* list of files with more than one link */
440Sstevel@tonic-gate char	buf[RDIST_BUFSIZ];	/* general purpose buffer */
450Sstevel@tonic-gate char	source[RDIST_BUFSIZ];	/* base source directory name */
460Sstevel@tonic-gate char	destination[RDIST_BUFSIZ];	/* base destination directory name */
470Sstevel@tonic-gate char	target[RDIST_BUFSIZ];	/* target/source directory name */
480Sstevel@tonic-gate char	*tp;			/* pointer to end of target name */
490Sstevel@tonic-gate char	*Tdest;			/* pointer to last T dest */
500Sstevel@tonic-gate int	catname;		/* cat name to target name */
510Sstevel@tonic-gate char	*stp[32];		/* stack of saved tp's for directories */
520Sstevel@tonic-gate int	oumask;			/* old umask for creating files */
530Sstevel@tonic-gate 
540Sstevel@tonic-gate extern	FILE *lfp;		/* log file for mailing changes */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate void	cleanup();
570Sstevel@tonic-gate struct	linkbuf *savelink();
580Sstevel@tonic-gate char	*strsub();
590Sstevel@tonic-gate 
60473Sbw static void comment(char *s);
61473Sbw static void note();
62473Sbw static void hardlink(char *cmd);
63473Sbw void error();
64473Sbw void log();
65473Sbw static void recursive_remove(struct stat *stp);
66473Sbw static void recvf(char *cmd, int type);
67473Sbw static void query(char *name);
68473Sbw static void sendf(char *rname, int opts);
69473Sbw static void rmchk(int opts);
70473Sbw static void dospecial(char *cmd);
71473Sbw static void clean(char *cp);
72473Sbw 
730Sstevel@tonic-gate /*
740Sstevel@tonic-gate  * Server routine to read requests and process them.
750Sstevel@tonic-gate  * Commands are:
760Sstevel@tonic-gate  *	Tname	- Transmit file if out of date
770Sstevel@tonic-gate  *	Vname	- Verify if file out of date or not
780Sstevel@tonic-gate  *	Qname	- Query if file exists. Return mtime & size if it does.
790Sstevel@tonic-gate  */
80473Sbw void
server()810Sstevel@tonic-gate server()
820Sstevel@tonic-gate {
830Sstevel@tonic-gate 	char cmdbuf[RDIST_BUFSIZ];
840Sstevel@tonic-gate 	register char *cp;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	signal(SIGHUP, cleanup);
870Sstevel@tonic-gate 	signal(SIGINT, cleanup);
880Sstevel@tonic-gate 	signal(SIGQUIT, cleanup);
890Sstevel@tonic-gate 	signal(SIGTERM, cleanup);
900Sstevel@tonic-gate 	signal(SIGPIPE, cleanup);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	rem = 0;
930Sstevel@tonic-gate 	oumask = umask(0);
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	(void) sprintf(buf, "V%d\n", VERSION);
960Sstevel@tonic-gate 	(void) write(wrem, buf, strlen(buf));
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	for (;;) {
990Sstevel@tonic-gate 		cp = cmdbuf;
1000Sstevel@tonic-gate 		if (read(rem, cp, 1) <= 0)
1010Sstevel@tonic-gate 			return;
1020Sstevel@tonic-gate 		if (*cp++ == '\n') {
1030Sstevel@tonic-gate 			error("server: expected control record\n");
1040Sstevel@tonic-gate 			continue;
1050Sstevel@tonic-gate 		}
1060Sstevel@tonic-gate 		do {
1070Sstevel@tonic-gate 			if (read(rem, cp, 1) != 1)
1080Sstevel@tonic-gate 				cleanup();
1090Sstevel@tonic-gate 		} while (*cp++ != '\n' && cp < &cmdbuf[RDIST_BUFSIZ]);
1100Sstevel@tonic-gate 		*--cp = '\0';
1110Sstevel@tonic-gate 		cp = cmdbuf;
1120Sstevel@tonic-gate 		switch (*cp++) {
1130Sstevel@tonic-gate 		case 'T':  /* init target file/directory name */
1140Sstevel@tonic-gate 			catname = 1;	/* target should be directory */
1150Sstevel@tonic-gate 			goto dotarget;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		case 't':  /* init target file/directory name */
1180Sstevel@tonic-gate 			catname = 0;
1190Sstevel@tonic-gate 		dotarget:
1200Sstevel@tonic-gate 			if (exptilde(target, sizeof (target), cp) == NULL)
1210Sstevel@tonic-gate 				continue;
1220Sstevel@tonic-gate 			tp = target;
1230Sstevel@tonic-gate 			while (*tp)
1240Sstevel@tonic-gate 				tp++;
1250Sstevel@tonic-gate 			ack();
1260Sstevel@tonic-gate 			continue;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 		case 'R':  /* Transfer a regular file. */
1290Sstevel@tonic-gate 			recvf(cp, S_IFREG);
1300Sstevel@tonic-gate 			continue;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 		case 'D':  /* Transfer a directory. */
1330Sstevel@tonic-gate 			recvf(cp, S_IFDIR);
1340Sstevel@tonic-gate 			continue;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 		case 'K':  /* Transfer symbolic link. */
1370Sstevel@tonic-gate 			recvf(cp, S_IFLNK);
1380Sstevel@tonic-gate 			continue;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 		case 'k':  /* Transfer hard link. */
1410Sstevel@tonic-gate 			hardlink(cp);
1420Sstevel@tonic-gate 			continue;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 		case 'E':  /* End. (of directory) */
1450Sstevel@tonic-gate 			*tp = '\0';
1460Sstevel@tonic-gate 			if (catname <= 0) {
1470Sstevel@tonic-gate 				error("server: too many 'E's\n");
1480Sstevel@tonic-gate 				continue;
1490Sstevel@tonic-gate 			}
1500Sstevel@tonic-gate 			tp = stp[--catname];
1510Sstevel@tonic-gate 			*tp = '\0';
1520Sstevel@tonic-gate 			ack();
1530Sstevel@tonic-gate 			continue;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 		case 'C':  /* Clean. Cleanup a directory */
1560Sstevel@tonic-gate 			clean(cp);
1570Sstevel@tonic-gate 			continue;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		case 'Q':  /* Query. Does the file/directory exist? */
1600Sstevel@tonic-gate 			query(cp);
1610Sstevel@tonic-gate 			continue;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 		case 'S':  /* Special. Execute commands */
1640Sstevel@tonic-gate 			dospecial(cp);
1650Sstevel@tonic-gate 			continue;
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate #ifdef notdef
1680Sstevel@tonic-gate 		/*
1690Sstevel@tonic-gate 		 * These entries are reserved but not currently used.
1700Sstevel@tonic-gate 		 * The intent is to allow remote hosts to have master copies.
1710Sstevel@tonic-gate 		 * Currently, only the host rdist runs on can have masters.
1720Sstevel@tonic-gate 		 */
1730Sstevel@tonic-gate 		case 'X':  /* start a new list of files to exclude */
1740Sstevel@tonic-gate 			except = bp = NULL;
1750Sstevel@tonic-gate 		case 'x':  /* add name to list of files to exclude */
1760Sstevel@tonic-gate 			if (*cp == '\0') {
1770Sstevel@tonic-gate 				ack();
1780Sstevel@tonic-gate 				continue;
1790Sstevel@tonic-gate 			}
1800Sstevel@tonic-gate 			if (*cp == '~') {
1810Sstevel@tonic-gate 				if (exptilde(buf, sizeof (buf), cp) == NULL)
1820Sstevel@tonic-gate 					continue;
1830Sstevel@tonic-gate 				cp = buf;
1840Sstevel@tonic-gate 			}
1850Sstevel@tonic-gate 			if (bp == NULL)
1860Sstevel@tonic-gate 				except = bp = expand(makeblock(NAME, cp),
187*11621SChandan.Bn@Sun.COM 				    E_VARS);
1880Sstevel@tonic-gate 			else
1890Sstevel@tonic-gate 				bp->b_next = expand(makeblock(NAME, cp),
190*11621SChandan.Bn@Sun.COM 				    E_VARS);
1910Sstevel@tonic-gate 			while (bp->b_next != NULL)
1920Sstevel@tonic-gate 				bp = bp->b_next;
1930Sstevel@tonic-gate 			ack();
1940Sstevel@tonic-gate 			continue;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 		case 'I':  /* Install. Transfer file if out of date. */
1970Sstevel@tonic-gate 			opts = 0;
1980Sstevel@tonic-gate 			while (*cp >= '0' && *cp <= '7')
1990Sstevel@tonic-gate 				opts = (opts << 3) | (*cp++ - '0');
2000Sstevel@tonic-gate 			if (*cp++ != ' ') {
2010Sstevel@tonic-gate 				error("server: options not delimited\n");
2020Sstevel@tonic-gate 				return;
2030Sstevel@tonic-gate 			}
2040Sstevel@tonic-gate 			install(cp, opts);
2050Sstevel@tonic-gate 			continue;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 		case 'L':  /* Log. save message in log file */
2080Sstevel@tonic-gate 			log(lfp, cp);
2090Sstevel@tonic-gate 			continue;
2100Sstevel@tonic-gate #endif
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 		case '\1':
2130Sstevel@tonic-gate 			nerrs++;
2140Sstevel@tonic-gate 			continue;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 		case '\2':
2170Sstevel@tonic-gate 			return;
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 		default:
2200Sstevel@tonic-gate 			error("server: unknown command '%s'\n", cp);
2210Sstevel@tonic-gate 		case '\0':
2220Sstevel@tonic-gate 			continue;
2230Sstevel@tonic-gate 		}
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate /*
2280Sstevel@tonic-gate  * Update the file(s) if they are different.
2290Sstevel@tonic-gate  * destdir = 1 if destination should be a directory
2300Sstevel@tonic-gate  * (i.e., more than one source is being copied to the same destination).
2310Sstevel@tonic-gate  */
232473Sbw void
install(src,dest,destdir,opts)2330Sstevel@tonic-gate install(src, dest, destdir, opts)
2340Sstevel@tonic-gate 	char *src, *dest;
2350Sstevel@tonic-gate 	int destdir, opts;
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	char *rname;
2380Sstevel@tonic-gate 	char destcopy[RDIST_BUFSIZ];
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (dest == NULL) {
2410Sstevel@tonic-gate 		opts &= ~WHOLE; /* WHOLE mode only useful if renaming */
2420Sstevel@tonic-gate 		dest = src;
2430Sstevel@tonic-gate 	}
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (nflag || debug) {
2460Sstevel@tonic-gate 		printf("%s%s%s%s%s %s %s\n", opts & VERIFY ? "verify":"install",
2470Sstevel@tonic-gate 			opts & WHOLE ? " -w" : "",
2480Sstevel@tonic-gate 			opts & YOUNGER ? " -y" : "",
2490Sstevel@tonic-gate 			opts & COMPARE ? " -b" : "",
2500Sstevel@tonic-gate 			opts & REMOVE ? " -R" : "", src, dest);
2510Sstevel@tonic-gate 		if (nflag)
2520Sstevel@tonic-gate 			return;
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	rname = exptilde(target, sizeof (target), src);
2560Sstevel@tonic-gate 	if (rname == NULL)
2570Sstevel@tonic-gate 		return;
2580Sstevel@tonic-gate 	tp = target;
2590Sstevel@tonic-gate 	while (*tp)
2600Sstevel@tonic-gate 		tp++;
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * If we are renaming a directory and we want to preserve
2630Sstevel@tonic-gate 	 * the directory heirarchy (-w), we must strip off the leading
2640Sstevel@tonic-gate 	 * directory name and preserve the rest.
2650Sstevel@tonic-gate 	 */
2660Sstevel@tonic-gate 	if (opts & WHOLE) {
2670Sstevel@tonic-gate 		while (*rname == '/')
2680Sstevel@tonic-gate 			rname++;
2690Sstevel@tonic-gate 		destdir = 1;
2700Sstevel@tonic-gate 	} else {
2710Sstevel@tonic-gate 		rname = rindex(target, '/');
2720Sstevel@tonic-gate 		if (rname == NULL)
2730Sstevel@tonic-gate 			rname = target;
2740Sstevel@tonic-gate 		else
2750Sstevel@tonic-gate 			rname++;
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 	if (debug)
2780Sstevel@tonic-gate 		printf("target = %s, rname = %s\n", target, rname);
2790Sstevel@tonic-gate 	/*
2800Sstevel@tonic-gate 	 * Pass the destination file/directory name to remote.
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%c%s\n", destdir ? 'T' : 't', dest) >=
2830Sstevel@tonic-gate 	    sizeof (buf)) {
2840Sstevel@tonic-gate 		error("%s: Name too long\n", dest);
2850Sstevel@tonic-gate 		return;
2860Sstevel@tonic-gate 	}
2870Sstevel@tonic-gate 	if (debug)
2880Sstevel@tonic-gate 		printf("buf = %s", buf);
2890Sstevel@tonic-gate 	(void) deswrite(rem, buf, strlen(buf), 0);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	if (response() < 0)
2920Sstevel@tonic-gate 		return;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	strcpy(source, src);
2950Sstevel@tonic-gate 	if (destdir) {
2960Sstevel@tonic-gate 		strcpy(destcopy, dest);
2970Sstevel@tonic-gate 		Tdest = destcopy;
2980Sstevel@tonic-gate 		strcpy(destination, rname);
2990Sstevel@tonic-gate 	} else {
3000Sstevel@tonic-gate 		strcpy(destination, dest);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 	sendf(rname, opts);
3030Sstevel@tonic-gate 	Tdest = 0;
3040Sstevel@tonic-gate }
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate #define	protoname()	(pw ? pw->pw_name : user)
3070Sstevel@tonic-gate #define	protogroup()	(gr ? gr->gr_name : group)
3080Sstevel@tonic-gate /*
3090Sstevel@tonic-gate  * Transfer the file or directory in target[].
3100Sstevel@tonic-gate  * rname is the name of the file on the remote host.
3110Sstevel@tonic-gate  */
312473Sbw void
sendf(rname,opts)3130Sstevel@tonic-gate sendf(rname, opts)
3140Sstevel@tonic-gate 	char *rname;
3150Sstevel@tonic-gate 	int opts;
3160Sstevel@tonic-gate {
3170Sstevel@tonic-gate 	register struct subcmd *sc;
3180Sstevel@tonic-gate 	struct stat stb;
3190Sstevel@tonic-gate 	int sizerr, f, u, len;
3200Sstevel@tonic-gate 	off_t i;
3210Sstevel@tonic-gate 	DIR *d;
3220Sstevel@tonic-gate 	struct dirent *dp;
3230Sstevel@tonic-gate 	char *otp, *cp;
3240Sstevel@tonic-gate 	extern struct subcmd *subcmds;
3250Sstevel@tonic-gate 	static char user[15], group[15];
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	if (debug)
3280Sstevel@tonic-gate 		printf("sendf(%s, %x%s)\n", rname, opts, printb(opts, OBITS));
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	if (except(target))
3310Sstevel@tonic-gate 		return;
3320Sstevel@tonic-gate 	if ((opts & FOLLOW ? stat(target, &stb) : lstat(target, &stb)) < 0) {
3330Sstevel@tonic-gate 		error("%s: %s\n", target, strerror(errno));
3340Sstevel@tonic-gate 		return;
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 	if (index(rname, '\n')) {
3370Sstevel@tonic-gate 		error("file name '%s' contains an embedded newline - "
3380Sstevel@tonic-gate 		    "can't update\n", rname);
3390Sstevel@tonic-gate 		return;
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 	if ((u = update(rname, opts, &stb)) == 0) {
3420Sstevel@tonic-gate 		if ((stb.st_mode & S_IFMT) == S_IFREG && stb.st_nlink > 1)
3430Sstevel@tonic-gate 			(void) savelink(&stb, opts);
3440Sstevel@tonic-gate 		return;
3450Sstevel@tonic-gate 	}
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (pw == NULL || pw->pw_uid != stb.st_uid)
3480Sstevel@tonic-gate 		if ((pw = getpwuid(stb.st_uid)) == NULL) {
3490Sstevel@tonic-gate 			log(lfp, "%s: no password entry for uid %d \n",
3500Sstevel@tonic-gate 				target, stb.st_uid);
3510Sstevel@tonic-gate 			pw = NULL;
3520Sstevel@tonic-gate 			sprintf(user, ":%d", stb.st_uid);
3530Sstevel@tonic-gate 		}
3540Sstevel@tonic-gate 	if (gr == NULL || gr->gr_gid != stb.st_gid)
3550Sstevel@tonic-gate 		if ((gr = getgrgid(stb.st_gid)) == NULL) {
3560Sstevel@tonic-gate 			log(lfp, "%s: no name for group %d\n",
3570Sstevel@tonic-gate 				target, stb.st_gid);
3580Sstevel@tonic-gate 			gr = NULL;
3590Sstevel@tonic-gate 			sprintf(group, ":%d", stb.st_gid);
3600Sstevel@tonic-gate 		}
3610Sstevel@tonic-gate 	if (u == 1) {
3620Sstevel@tonic-gate 		if (opts & VERIFY) {
3630Sstevel@tonic-gate 			log(lfp, "need to install: %s\n", target);
3640Sstevel@tonic-gate 			goto dospecial;
3650Sstevel@tonic-gate 		}
3660Sstevel@tonic-gate 		log(lfp, "installing: %s\n", target);
3670Sstevel@tonic-gate 		opts &= ~(COMPARE|REMOVE);
3680Sstevel@tonic-gate 	}
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate 	switch (stb.st_mode & S_IFMT) {
3710Sstevel@tonic-gate 	case S_IFDIR:
3720Sstevel@tonic-gate 		if ((d = opendir(target)) == NULL) {
3730Sstevel@tonic-gate 			error("%s: %s\n", target, strerror(errno));
3740Sstevel@tonic-gate 			return;
3750Sstevel@tonic-gate 		}
3760Sstevel@tonic-gate 		if (snprintf(buf, sizeof (buf), "D%o %04o 0 0 %s %s %s\n",
3770Sstevel@tonic-gate 		    opts, stb.st_mode & 07777, protoname(), protogroup(),
3780Sstevel@tonic-gate 		    rname) >= sizeof (buf)) {
3790Sstevel@tonic-gate 			error("%s: Name too long\n", rname);
3800Sstevel@tonic-gate 			closedir(d);
3810Sstevel@tonic-gate 			return;
3820Sstevel@tonic-gate 		}
3830Sstevel@tonic-gate 		if (debug)
3840Sstevel@tonic-gate 			printf("buf = %s", buf);
3850Sstevel@tonic-gate 		(void) deswrite(rem, buf, strlen(buf), 0);
3860Sstevel@tonic-gate 		if (response() < 0) {
3870Sstevel@tonic-gate 			closedir(d);
3880Sstevel@tonic-gate 			return;
3890Sstevel@tonic-gate 		}
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 		if (opts & REMOVE)
3920Sstevel@tonic-gate 			rmchk(opts);
3930Sstevel@tonic-gate 
3940Sstevel@tonic-gate 		otp = tp;
3950Sstevel@tonic-gate 		len = tp - target;
3960Sstevel@tonic-gate 		while (dp = readdir(d)) {
3970Sstevel@tonic-gate 			if ((strcmp(dp->d_name, ".") == 0)||
3980Sstevel@tonic-gate 			    (strcmp(dp->d_name, "..") == 0))
3990Sstevel@tonic-gate 				continue;
4000Sstevel@tonic-gate 			if ((int)(len + 1 + strlen(dp->d_name)) >=
4010Sstevel@tonic-gate 			    (int)(RDIST_BUFSIZ - 1)) {
4020Sstevel@tonic-gate 				error("%.*s/%s: Name too long\n", len, target,
4030Sstevel@tonic-gate 					dp->d_name);
4040Sstevel@tonic-gate 				continue;
4050Sstevel@tonic-gate 			}
4060Sstevel@tonic-gate 			tp = otp;
4070Sstevel@tonic-gate 			*tp++ = '/';
4080Sstevel@tonic-gate 			cp = dp->d_name;
4090Sstevel@tonic-gate 			while (*tp++ = *cp++)
4100Sstevel@tonic-gate 				;
4110Sstevel@tonic-gate 			tp--;
4120Sstevel@tonic-gate 			sendf(dp->d_name, opts);
4130Sstevel@tonic-gate 		}
4140Sstevel@tonic-gate 		closedir(d);
4150Sstevel@tonic-gate 		(void) deswrite(rem, "E\n", 2, 0);
4160Sstevel@tonic-gate 		(void) response();
4170Sstevel@tonic-gate 		tp = otp;
4180Sstevel@tonic-gate 		*tp = '\0';
4190Sstevel@tonic-gate 		return;
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	case S_IFLNK:
4220Sstevel@tonic-gate 		if (u != 1)
4230Sstevel@tonic-gate 			opts |= COMPARE;
4240Sstevel@tonic-gate 		if (stb.st_nlink > 1) {
4250Sstevel@tonic-gate 			struct linkbuf *lp;
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 			if ((lp = savelink(&stb, opts)) != NULL) {
4280Sstevel@tonic-gate 				/* install link */
4290Sstevel@tonic-gate 				if (*lp->target == 0)
4300Sstevel@tonic-gate 					len = snprintf(buf, sizeof (buf),
4310Sstevel@tonic-gate 					    "k%o %s %s\n", opts, lp->pathname,
4320Sstevel@tonic-gate 					    rname);
4330Sstevel@tonic-gate 				else
4340Sstevel@tonic-gate 					len = snprintf(buf, sizeof (buf),
4350Sstevel@tonic-gate 					    "k%o %s/%s %s\n", opts, lp->target,
4360Sstevel@tonic-gate 					    lp->pathname, rname);
4370Sstevel@tonic-gate 				if (len >= sizeof (buf)) {
4380Sstevel@tonic-gate 					error("%s: Name too long\n", rname);
4390Sstevel@tonic-gate 					return;
4400Sstevel@tonic-gate 				}
4410Sstevel@tonic-gate 				if (debug)
4420Sstevel@tonic-gate 					printf("buf = %s", buf);
4430Sstevel@tonic-gate 				(void) deswrite(rem, buf, strlen(buf), 0);
4440Sstevel@tonic-gate 				(void) response();
4450Sstevel@tonic-gate 				return;
4460Sstevel@tonic-gate 			}
4470Sstevel@tonic-gate 		}
4480Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "K%o %o %ld %ld %s %s %s\n",
4490Sstevel@tonic-gate 		    opts, stb.st_mode & 07777, stb.st_size, stb.st_mtime,
4500Sstevel@tonic-gate 		    protoname(), protogroup(), rname);
4510Sstevel@tonic-gate 		if (debug)
4520Sstevel@tonic-gate 			printf("buf = %s", buf);
4530Sstevel@tonic-gate 		(void) deswrite(rem, buf, strlen(buf), 0);
4540Sstevel@tonic-gate 		if (response() < 0)
4550Sstevel@tonic-gate 			return;
4560Sstevel@tonic-gate 		sizerr = (readlink(target, buf, RDIST_BUFSIZ) != stb.st_size);
4570Sstevel@tonic-gate 		(void) deswrite(rem, buf, stb.st_size, 0);
4580Sstevel@tonic-gate 		if (debug)
4590Sstevel@tonic-gate 			printf("readlink = %.*s\n", (int)stb.st_size, buf);
4600Sstevel@tonic-gate 		goto done;
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	case S_IFREG:
4630Sstevel@tonic-gate 		break;
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate 	default:
4660Sstevel@tonic-gate 		error("%s: not a file or directory\n", target);
4670Sstevel@tonic-gate 		return;
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (u == 2) {
4710Sstevel@tonic-gate 		if (opts & VERIFY) {
4720Sstevel@tonic-gate 			log(lfp, "need to update: %s\n", target);
4730Sstevel@tonic-gate 			goto dospecial;
4740Sstevel@tonic-gate 		}
4750Sstevel@tonic-gate 		log(lfp, "updating: %s\n", target);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	if (stb.st_nlink > 1) {
4790Sstevel@tonic-gate 		struct linkbuf *lp;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 		if ((lp = savelink(&stb, opts)) != NULL) {
4820Sstevel@tonic-gate 			/* install link */
4830Sstevel@tonic-gate 			if (*lp->target == 0)
4840Sstevel@tonic-gate 				len = snprintf(buf, sizeof (buf), "k%o %s %s\n",
4850Sstevel@tonic-gate 				    opts, lp->pathname, rname);
4860Sstevel@tonic-gate 			else
4870Sstevel@tonic-gate 				len = snprintf(buf, sizeof (buf),
4880Sstevel@tonic-gate 				    "k%o %s/%s %s\n", opts, lp->target,
4890Sstevel@tonic-gate 				    lp->pathname, rname);
4900Sstevel@tonic-gate 			if (len >= sizeof (buf)) {
4910Sstevel@tonic-gate 				error("%s: Name too long\n", rname);
4920Sstevel@tonic-gate 				return;
4930Sstevel@tonic-gate 			}
4940Sstevel@tonic-gate 			if (debug)
4950Sstevel@tonic-gate 				printf("buf = %s", buf);
4960Sstevel@tonic-gate 			(void) deswrite(rem, buf, strlen(buf), 0);
4970Sstevel@tonic-gate 			(void) response();
4980Sstevel@tonic-gate 			return;
4990Sstevel@tonic-gate 		}
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	if ((f = open(target, 0)) < 0) {
5030Sstevel@tonic-gate 		error("%s: %s\n", target, strerror(errno));
5040Sstevel@tonic-gate 		return;
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "R%o %o %ld %ld %s %s %s\n", opts,
5070Sstevel@tonic-gate 		stb.st_mode & 07777, stb.st_size, stb.st_mtime,
5080Sstevel@tonic-gate 		protoname(), protogroup(), rname);
5090Sstevel@tonic-gate 	if (debug)
5100Sstevel@tonic-gate 		printf("buf = %s", buf);
5110Sstevel@tonic-gate 	(void) deswrite(rem, buf, strlen(buf), 0);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if (response() < 0) {
5140Sstevel@tonic-gate 		(void) close(f);
5150Sstevel@tonic-gate 		return;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	sizerr = 0;
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	for (i = 0; i < stb.st_size; i += RDIST_BUFSIZ) {
5210Sstevel@tonic-gate 		int amt = RDIST_BUFSIZ;
5220Sstevel@tonic-gate 		if (i + amt > stb.st_size)
5230Sstevel@tonic-gate 			amt = stb.st_size - i;
5240Sstevel@tonic-gate 		if (sizerr == 0 && read(f, buf, amt) != amt)
5250Sstevel@tonic-gate 			sizerr = 1;
5260Sstevel@tonic-gate 		(void) deswrite(rem, buf, amt, 0);
5270Sstevel@tonic-gate 	}
5280Sstevel@tonic-gate 	(void) close(f);
5290Sstevel@tonic-gate done:
5300Sstevel@tonic-gate 	if (sizerr) {
5310Sstevel@tonic-gate 		error("%s: file changed size\n", target);
5320Sstevel@tonic-gate 		(void) deswrite(rem, "\1\n", 2, 0);
5330Sstevel@tonic-gate 	} else
5340Sstevel@tonic-gate 		(void) deswrite(rem, "\0\n", 2, 0);
5350Sstevel@tonic-gate 	f = response();
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 	if (f < 0 || f == 0 && (opts & COMPARE))
5380Sstevel@tonic-gate 		return;
5390Sstevel@tonic-gate dospecial:
5400Sstevel@tonic-gate 	for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
5410Sstevel@tonic-gate 		if (sc->sc_type != SPECIAL)
5420Sstevel@tonic-gate 			continue;
5430Sstevel@tonic-gate 		if (sc->sc_args != NULL && !inlist(sc->sc_args, target))
5440Sstevel@tonic-gate 			continue;
5450Sstevel@tonic-gate 		log(lfp, "special \"%s\"\n", sc->sc_name);
5460Sstevel@tonic-gate 		if (opts & VERIFY)
5470Sstevel@tonic-gate 			continue;
5480Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "SFILE=%s;%s\n", target,
5490Sstevel@tonic-gate 		    sc->sc_name);
5500Sstevel@tonic-gate 		if (debug)
5510Sstevel@tonic-gate 			printf("buf = %s", buf);
5520Sstevel@tonic-gate 		(void) deswrite(rem, buf, strlen(buf), 0);
5530Sstevel@tonic-gate 		while (response() > 0)
5540Sstevel@tonic-gate 			;
5550Sstevel@tonic-gate 	}
5560Sstevel@tonic-gate }
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate struct linkbuf *
savelink(stp,opts)5590Sstevel@tonic-gate savelink(stp, opts)
5600Sstevel@tonic-gate 	struct stat *stp;
5610Sstevel@tonic-gate 	int opts;
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate 	struct linkbuf *lp;
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	for (lp = ihead; lp != NULL; lp = lp->nextp)
5660Sstevel@tonic-gate 		if (lp->inum == stp->st_ino && lp->devnum == stp->st_dev) {
5670Sstevel@tonic-gate 			lp->count--;
5680Sstevel@tonic-gate 			return (lp);
5690Sstevel@tonic-gate 		}
5700Sstevel@tonic-gate 	lp = (struct linkbuf *)malloc(sizeof (*lp));
5710Sstevel@tonic-gate 	if (lp == NULL)
5720Sstevel@tonic-gate 		log(lfp, "out of memory, link information lost\n");
5730Sstevel@tonic-gate 	else {
5740Sstevel@tonic-gate 		lp->nextp = ihead;
5750Sstevel@tonic-gate 		ihead = lp;
5760Sstevel@tonic-gate 		lp->inum = stp->st_ino;
5770Sstevel@tonic-gate 		lp->devnum = stp->st_dev;
5780Sstevel@tonic-gate 		lp->count = stp->st_nlink - 1;
579*11621SChandan.Bn@Sun.COM 
580*11621SChandan.Bn@Sun.COM 		if (strlcpy(lp->pathname,
581*11621SChandan.Bn@Sun.COM 		    opts & WHOLE ? target : strsub(source, destination, target),
582*11621SChandan.Bn@Sun.COM 		    sizeof (lp->pathname)) >= sizeof (lp->pathname)) {
583*11621SChandan.Bn@Sun.COM 			error("%s: target name too long\n", target);
584*11621SChandan.Bn@Sun.COM 		}
585*11621SChandan.Bn@Sun.COM 
586*11621SChandan.Bn@Sun.COM 		if (Tdest) {
587*11621SChandan.Bn@Sun.COM 			if (strlcpy(lp->target, Tdest,
588*11621SChandan.Bn@Sun.COM 			    sizeof (lp->target)) >= sizeof (lp->target))
589*11621SChandan.Bn@Sun.COM 				error("%s: target name too long\n", Tdest);
590*11621SChandan.Bn@Sun.COM 		} else
5910Sstevel@tonic-gate 			*lp->target = 0;
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 	return (NULL);
5940Sstevel@tonic-gate }
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate /*
5970Sstevel@tonic-gate  * Check to see if file needs to be updated on the remote machine.
5980Sstevel@tonic-gate  * Returns 0 if no update, 1 if remote doesn't exist, 2 if out of date
5990Sstevel@tonic-gate  * and 3 if comparing binaries to determine if out of date.
6000Sstevel@tonic-gate  */
601473Sbw int
update(rname,opts,stp)6020Sstevel@tonic-gate update(rname, opts, stp)
6030Sstevel@tonic-gate 	char *rname;
6040Sstevel@tonic-gate 	int opts;
6050Sstevel@tonic-gate 	struct stat *stp;
6060Sstevel@tonic-gate {
6070Sstevel@tonic-gate 	register char *cp, *s;
6080Sstevel@tonic-gate 	register off_t size;
6090Sstevel@tonic-gate 	register time_t mtime;
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	if (debug)
6120Sstevel@tonic-gate 		printf("update(%s, %x%s, %x)\n", rname, opts,
6130Sstevel@tonic-gate 			printb(opts, OBITS), stp);
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/*
6160Sstevel@tonic-gate 	 * Check to see if the file exists on the remote machine.
6170Sstevel@tonic-gate 	 */
6180Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "Q%s\n", rname) >= sizeof (buf)) {
6190Sstevel@tonic-gate 		error("%s: Name too long\n", rname);
6200Sstevel@tonic-gate 		return (0);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 	if (debug)
6230Sstevel@tonic-gate 		printf("buf = %s", buf);
6240Sstevel@tonic-gate 	(void) deswrite(rem, buf, strlen(buf), 0);
6250Sstevel@tonic-gate again:
6260Sstevel@tonic-gate 	cp = s = buf;
6270Sstevel@tonic-gate more:
6280Sstevel@tonic-gate 	do {
6290Sstevel@tonic-gate 		if (desread(rem, cp, 1, 0) != 1)
6300Sstevel@tonic-gate 			lostconn();
6310Sstevel@tonic-gate 	} while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]);
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	if (cp <  &buf[RDIST_BUFSIZ])
6340Sstevel@tonic-gate 		*cp = '\0';
6350Sstevel@tonic-gate 	if (debug) {
6360Sstevel@tonic-gate 		printf("update reply:  ");
6370Sstevel@tonic-gate 		switch (*s) {
6380Sstevel@tonic-gate 			case 'Y':
6390Sstevel@tonic-gate 			case 'N':
6400Sstevel@tonic-gate 				putchar(*s);
6410Sstevel@tonic-gate 				break;
6420Sstevel@tonic-gate 			default:
6430Sstevel@tonic-gate 				if (iscntrl(*s)) {
6440Sstevel@tonic-gate 					putchar('^');
6450Sstevel@tonic-gate 					putchar('A' + *s - 1);
6460Sstevel@tonic-gate 				} else
6470Sstevel@tonic-gate 					printf("%#x", *s & 0xff);
6480Sstevel@tonic-gate 				break;
6490Sstevel@tonic-gate 		}
6500Sstevel@tonic-gate 		printf("%s", &s[1]);
6510Sstevel@tonic-gate 	}
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	switch (*s++) {
6540Sstevel@tonic-gate 	case 'Y':
6550Sstevel@tonic-gate 		break;
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	case 'N':  /* file doesn't exist so install it */
6580Sstevel@tonic-gate 		return (1);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	case '\1':
6610Sstevel@tonic-gate 		nerrs++;
6620Sstevel@tonic-gate 		if (*s != '\n') {
6630Sstevel@tonic-gate 			if (!iamremote) {
6640Sstevel@tonic-gate 				fflush(stdout);
6650Sstevel@tonic-gate 				(void) write(2, s, cp - s);
6660Sstevel@tonic-gate 			}
6670Sstevel@tonic-gate 			if (lfp != NULL)
6680Sstevel@tonic-gate 				(void) fwrite(s, 1, cp - s, lfp);
6690Sstevel@tonic-gate 		}
6700Sstevel@tonic-gate 		if (cp == &buf[RDIST_BUFSIZ] && *(cp - 1) != '\n') {
6710Sstevel@tonic-gate 			/* preserve status code */
6720Sstevel@tonic-gate 			cp = s;
6730Sstevel@tonic-gate 			s = buf;
6740Sstevel@tonic-gate 			goto more;
6750Sstevel@tonic-gate 		}
6760Sstevel@tonic-gate 		return (0);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	case '\3':
6790Sstevel@tonic-gate 		*--cp = '\0';
6800Sstevel@tonic-gate 		if (lfp != NULL)
6810Sstevel@tonic-gate 			log(lfp, "update: note: %s\n", s);
6820Sstevel@tonic-gate 		goto again;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 	default:
6850Sstevel@tonic-gate 		*--cp = '\0';
6860Sstevel@tonic-gate 		error("update: unexpected response '%s'\n", s);
6870Sstevel@tonic-gate 		return (0);
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	if (*s == '\n')
6910Sstevel@tonic-gate 		return (2);
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	if (opts & COMPARE)
6940Sstevel@tonic-gate 		return (3);
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	size = 0;
6970Sstevel@tonic-gate 	while (isdigit(*s))
6980Sstevel@tonic-gate 		size = size * 10 + (*s++ - '0');
6990Sstevel@tonic-gate 	if (*s++ != ' ') {
7000Sstevel@tonic-gate 		error("update: size not delimited\n");
7010Sstevel@tonic-gate 		return (0);
7020Sstevel@tonic-gate 	}
7030Sstevel@tonic-gate 	mtime = 0;
7040Sstevel@tonic-gate 	while (isdigit(*s))
7050Sstevel@tonic-gate 		mtime = mtime * 10 + (*s++ - '0');
7060Sstevel@tonic-gate 	if (*s != '\n') {
7070Sstevel@tonic-gate 		error("update: mtime not delimited\n");
7080Sstevel@tonic-gate 		return (0);
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 	/*
7110Sstevel@tonic-gate 	 * File needs to be updated?
7120Sstevel@tonic-gate 	 */
7130Sstevel@tonic-gate 	if (opts & YOUNGER) {
7140Sstevel@tonic-gate 		if (stp->st_mtime == mtime)
7150Sstevel@tonic-gate 			return (0);
7160Sstevel@tonic-gate 		if (stp->st_mtime < mtime) {
7170Sstevel@tonic-gate 			log(lfp, "Warning: %s: remote copy is newer\n", target);
7180Sstevel@tonic-gate 			return (0);
7190Sstevel@tonic-gate 		}
7200Sstevel@tonic-gate 	} else if (stp->st_mtime == mtime && stp->st_size == size)
7210Sstevel@tonic-gate 		return (0);
7220Sstevel@tonic-gate 	return (2);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate  * Query. Check to see if file exists. Return one of the following:
7270Sstevel@tonic-gate  *	N\n		- doesn't exist
7280Sstevel@tonic-gate  *	Ysize mtime\n	- exists and its a regular file (size & mtime of file)
7290Sstevel@tonic-gate  *	Y\n		- exists and its a directory or symbolic link
7300Sstevel@tonic-gate  *	^Aerror message\n
7310Sstevel@tonic-gate  */
732473Sbw static void
query(name)7330Sstevel@tonic-gate query(name)
7340Sstevel@tonic-gate 	char *name;
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	struct stat stb;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	if (catname) {
7390Sstevel@tonic-gate 		if (sizeof (target) - (tp - target) >= strlen(name) + 2) {
7400Sstevel@tonic-gate 			(void) sprintf(tp, "/%s", name);
7410Sstevel@tonic-gate 		} else {
7420Sstevel@tonic-gate 			error("%.*s/%s: Name too long\n", tp - target,
7430Sstevel@tonic-gate 			    target, name);
7440Sstevel@tonic-gate 			return;
7450Sstevel@tonic-gate 		}
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	if (lstat(target, &stb) < 0) {
7490Sstevel@tonic-gate 		if (errno == ENOENT)
7500Sstevel@tonic-gate 			(void) write(wrem, "N\n", 2);
7510Sstevel@tonic-gate 		else
7520Sstevel@tonic-gate 			error("%s:%s: %s\n", host, target, strerror(errno));
7530Sstevel@tonic-gate 		*tp = '\0';
7540Sstevel@tonic-gate 		return;
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 	switch (stb.st_mode & S_IFMT) {
7580Sstevel@tonic-gate 	case S_IFREG:
7590Sstevel@tonic-gate 		(void) sprintf(buf, "Y%ld %ld\n", stb.st_size, stb.st_mtime);
7600Sstevel@tonic-gate 		(void) write(wrem, buf, strlen(buf));
7610Sstevel@tonic-gate 		break;
7620Sstevel@tonic-gate 
7630Sstevel@tonic-gate 	case S_IFLNK:
7640Sstevel@tonic-gate 	case S_IFDIR:
7650Sstevel@tonic-gate 		(void) write(wrem, "Y\n", 2);
7660Sstevel@tonic-gate 		break;
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	default:
7690Sstevel@tonic-gate 		error("%s: not a file or directory\n", name);
7700Sstevel@tonic-gate 		break;
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 	*tp = '\0';
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
775473Sbw static void
recvf(cmd,type)7760Sstevel@tonic-gate recvf(cmd, type)
7770Sstevel@tonic-gate 	char *cmd;
7780Sstevel@tonic-gate 	int type;
7790Sstevel@tonic-gate {
7800Sstevel@tonic-gate 	register char *cp;
7810Sstevel@tonic-gate 	int f, mode, opts, wrerr, olderrno;
7820Sstevel@tonic-gate 	off_t i, size;
7830Sstevel@tonic-gate 	time_t mtime;
7840Sstevel@tonic-gate 	struct stat stb;
7850Sstevel@tonic-gate 	struct timeval tvp[2];
7860Sstevel@tonic-gate 	char *owner, *group;
7870Sstevel@tonic-gate 	char new[RDIST_BUFSIZ];
7880Sstevel@tonic-gate 	extern char *tmpname;
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 	cp = cmd;
7910Sstevel@tonic-gate 	opts = 0;
7920Sstevel@tonic-gate 	while (*cp >= '0' && *cp <= '7')
7930Sstevel@tonic-gate 		opts = (opts << 3) | (*cp++ - '0');
7940Sstevel@tonic-gate 	if (*cp++ != ' ') {
7950Sstevel@tonic-gate 		error("recvf: options not delimited\n");
7960Sstevel@tonic-gate 		return;
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate 	mode = 0;
7990Sstevel@tonic-gate 	while (*cp >= '0' && *cp <= '7')
8000Sstevel@tonic-gate 		mode = (mode << 3) | (*cp++ - '0');
8010Sstevel@tonic-gate 	if (*cp++ != ' ') {
8020Sstevel@tonic-gate 		error("recvf: mode not delimited\n");
8030Sstevel@tonic-gate 		return;
8040Sstevel@tonic-gate 	}
8050Sstevel@tonic-gate 	size = 0;
8060Sstevel@tonic-gate 	while (isdigit(*cp))
8070Sstevel@tonic-gate 		size = size * 10 + (*cp++ - '0');
8080Sstevel@tonic-gate 	if (*cp++ != ' ') {
8090Sstevel@tonic-gate 		error("recvf: size not delimited\n");
8100Sstevel@tonic-gate 		return;
8110Sstevel@tonic-gate 	}
8120Sstevel@tonic-gate 	mtime = 0;
8130Sstevel@tonic-gate 	while (isdigit(*cp))
8140Sstevel@tonic-gate 		mtime = mtime * 10 + (*cp++ - '0');
8150Sstevel@tonic-gate 	if (*cp++ != ' ') {
8160Sstevel@tonic-gate 		error("recvf: mtime not delimited\n");
8170Sstevel@tonic-gate 		return;
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate 	owner = cp;
8200Sstevel@tonic-gate 	while (*cp && *cp != ' ')
8210Sstevel@tonic-gate 		cp++;
8220Sstevel@tonic-gate 	if (*cp != ' ') {
8230Sstevel@tonic-gate 		error("recvf: owner name not delimited\n");
8240Sstevel@tonic-gate 		return;
8250Sstevel@tonic-gate 	}
8260Sstevel@tonic-gate 	*cp++ = '\0';
8270Sstevel@tonic-gate 	group = cp;
8280Sstevel@tonic-gate 	while (*cp && *cp != ' ')
8290Sstevel@tonic-gate 		cp++;
8300Sstevel@tonic-gate 	if (*cp != ' ') {
8310Sstevel@tonic-gate 		error("recvf: group name not delimited\n");
8320Sstevel@tonic-gate 		return;
8330Sstevel@tonic-gate 	}
8340Sstevel@tonic-gate 	*cp++ = '\0';
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 	if (type == S_IFDIR) {
8370Sstevel@tonic-gate 		int	isdot;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		if (strcmp(cp, ".") == 0)
8400Sstevel@tonic-gate 			isdot = 1;
8410Sstevel@tonic-gate 		else
8420Sstevel@tonic-gate 			isdot = 0;
8430Sstevel@tonic-gate 		if (catname >= sizeof (stp) / sizeof (stp[0])) {
8440Sstevel@tonic-gate 			error("%s:%s: too many directory levels\n",
8450Sstevel@tonic-gate 				host, target);
8460Sstevel@tonic-gate 			return;
8470Sstevel@tonic-gate 		}
8480Sstevel@tonic-gate 		stp[catname] = tp;
8490Sstevel@tonic-gate 		if (catname++) {
8500Sstevel@tonic-gate 			*tp++ = '/';
8510Sstevel@tonic-gate 			while (*tp++ = *cp++)
8520Sstevel@tonic-gate 				;
8530Sstevel@tonic-gate 			tp--;
8540Sstevel@tonic-gate 		}
8550Sstevel@tonic-gate 		if (opts & VERIFY) {
8560Sstevel@tonic-gate 			ack();
8570Sstevel@tonic-gate 			return;
8580Sstevel@tonic-gate 		}
8590Sstevel@tonic-gate 		if (lstat(target, &stb) == 0) {
8600Sstevel@tonic-gate 			if (ISDIR(stb.st_mode)) {
8610Sstevel@tonic-gate 				if ((stb.st_mode & 07777) == mode) {
8620Sstevel@tonic-gate 					ack();
8630Sstevel@tonic-gate 					return;
8640Sstevel@tonic-gate 				}
8650Sstevel@tonic-gate 				sendrem("%s: Warning: remote mode %o != "
8660Sstevel@tonic-gate 				    "local mode %o", target,
8670Sstevel@tonic-gate 				    stb.st_mode & 07777, mode);
8680Sstevel@tonic-gate 				return;
8690Sstevel@tonic-gate 			}
8700Sstevel@tonic-gate 			errno = ENOTDIR;
8710Sstevel@tonic-gate 		} else if (errno == ENOENT && (mkdir(target, mode) == 0 ||
8720Sstevel@tonic-gate 		    chkparent(target) == 0 &&
8730Sstevel@tonic-gate 		    (isdot == 1 || mkdir(target, mode) == 0))) {
8740Sstevel@tonic-gate 			if (chog(target, owner, group, mode) == 0)
8750Sstevel@tonic-gate 				ack();
8760Sstevel@tonic-gate 			return;
8770Sstevel@tonic-gate 		}
8780Sstevel@tonic-gate 		error("%s:%s: %s\n", host, target, strerror(errno));
8790Sstevel@tonic-gate 		tp = stp[--catname];
8800Sstevel@tonic-gate 		*tp = '\0';
8810Sstevel@tonic-gate 		return;
8820Sstevel@tonic-gate 	}
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	if (catname) {
8850Sstevel@tonic-gate 		if (sizeof (target) - (tp - target) >= strlen(cp) + 2) {
8860Sstevel@tonic-gate 			(void) sprintf(tp, "/%s", cp);
8870Sstevel@tonic-gate 		} else {
8880Sstevel@tonic-gate 			error("%.*s/%s: Name too long\n", tp - target,
8890Sstevel@tonic-gate 			    target, cp);
8900Sstevel@tonic-gate 			return;
8910Sstevel@tonic-gate 		}
8920Sstevel@tonic-gate 	}
8930Sstevel@tonic-gate 	cp = rindex(target, '/');
8940Sstevel@tonic-gate 	if (cp == NULL)
8950Sstevel@tonic-gate 		strcpy(new, tmpname);
8960Sstevel@tonic-gate 	else if (cp == target)
8970Sstevel@tonic-gate 		(void) sprintf(new, "/%s", tmpname);
8980Sstevel@tonic-gate 	else {
8990Sstevel@tonic-gate 		*cp = '\0';
900*11621SChandan.Bn@Sun.COM 
901*11621SChandan.Bn@Sun.COM 		/*
902*11621SChandan.Bn@Sun.COM 		 * sizeof (target) =  RDIST_BUFSIZ and sizeof (tmpname) = 11
903*11621SChandan.Bn@Sun.COM 		 * RDIST_BUFSIZ = 50*1024 is much greater than PATH_MAX that is
904*11621SChandan.Bn@Sun.COM 		 * allowed by the kernel, so it's safe to call snprintf() here
905*11621SChandan.Bn@Sun.COM 		 */
906*11621SChandan.Bn@Sun.COM 		(void) snprintf(new, sizeof (new), "%s/%s", target, tmpname);
9070Sstevel@tonic-gate 		*cp = '/';
9080Sstevel@tonic-gate 	}
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate 	if (type == S_IFLNK) {
9110Sstevel@tonic-gate 		int j;
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate 		ack();
9140Sstevel@tonic-gate 		cp = buf;
9150Sstevel@tonic-gate 		for (i = 0; i < size; i += j) {
9160Sstevel@tonic-gate 			if ((j = read(rem, cp, size - i)) <= 0)
9170Sstevel@tonic-gate 				cleanup();
9180Sstevel@tonic-gate 			cp += j;
9190Sstevel@tonic-gate 		}
9200Sstevel@tonic-gate 		*cp = '\0';
9210Sstevel@tonic-gate 		if (response() < 0) {
9220Sstevel@tonic-gate 			err();
9230Sstevel@tonic-gate 			return;
9240Sstevel@tonic-gate 		}
9250Sstevel@tonic-gate 		if (symlink(buf, new) < 0) {
9260Sstevel@tonic-gate 			if (errno != ENOENT || chkparent(new) < 0 ||
9270Sstevel@tonic-gate 			    symlink(buf, new) < 0)
9280Sstevel@tonic-gate 				goto badn;
9290Sstevel@tonic-gate 		}
9300Sstevel@tonic-gate 		mode &= 0777;
9310Sstevel@tonic-gate 		if (opts & COMPARE) {
9320Sstevel@tonic-gate 			char tbuf[MAXPATHLEN];
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 			if ((i = readlink(target, tbuf, MAXPATHLEN)) >= 0 &&
9350Sstevel@tonic-gate 			    i == size && strncmp(buf, tbuf, size) == 0) {
9360Sstevel@tonic-gate 				(void) unlink(new);
9370Sstevel@tonic-gate 				ack();
9380Sstevel@tonic-gate 				return;
9390Sstevel@tonic-gate 			}
9400Sstevel@tonic-gate 			if (opts & VERIFY)
9410Sstevel@tonic-gate 				goto differ;
9420Sstevel@tonic-gate 		}
9430Sstevel@tonic-gate 		goto fixup;
9440Sstevel@tonic-gate 	}
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	if ((f = creat(new, mode & ~06000)) < 0) {
9470Sstevel@tonic-gate 		if (errno != ENOENT || chkparent(new) < 0 ||
9480Sstevel@tonic-gate 		    (f = creat(new, mode & ~06000)) < 0)
9490Sstevel@tonic-gate 			goto badn;
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 	ack();
9530Sstevel@tonic-gate 	wrerr = 0;
9540Sstevel@tonic-gate 	for (i = 0; i < size; i += RDIST_BUFSIZ) {
9550Sstevel@tonic-gate 		int amt = RDIST_BUFSIZ;
9560Sstevel@tonic-gate 
9570Sstevel@tonic-gate 		cp = buf;
9580Sstevel@tonic-gate 		if (i + amt > size)
9590Sstevel@tonic-gate 			amt = size - i;
9600Sstevel@tonic-gate 		do {
9610Sstevel@tonic-gate 			int j = read(rem, cp, amt);
9620Sstevel@tonic-gate 			if (j <= 0) {
9630Sstevel@tonic-gate 				(void) close(f);
9640Sstevel@tonic-gate 				(void) unlink(new);
9650Sstevel@tonic-gate 				cleanup();
9660Sstevel@tonic-gate 			}
9670Sstevel@tonic-gate 			amt -= j;
9680Sstevel@tonic-gate 			cp += j;
9690Sstevel@tonic-gate 		} while (amt > 0);
9700Sstevel@tonic-gate 		amt = RDIST_BUFSIZ;
9710Sstevel@tonic-gate 		if (i + amt > size)
9720Sstevel@tonic-gate 			amt = size - i;
9730Sstevel@tonic-gate 		if (wrerr == 0 && write(f, buf, amt) != amt) {
9740Sstevel@tonic-gate 			olderrno = errno;
9750Sstevel@tonic-gate 			wrerr++;
9760Sstevel@tonic-gate 		}
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 	(void) close(f);
9790Sstevel@tonic-gate 
9800Sstevel@tonic-gate 	if (response() < 0) {
9810Sstevel@tonic-gate 		err();
9820Sstevel@tonic-gate 		(void) unlink(new);
9830Sstevel@tonic-gate 		return;
9840Sstevel@tonic-gate 	}
9850Sstevel@tonic-gate 	if (wrerr) {
9860Sstevel@tonic-gate 		error("%s:%s: %s\n", host, new, strerror(olderrno));
9870Sstevel@tonic-gate 		(void) unlink(new);
9880Sstevel@tonic-gate 		return;
9890Sstevel@tonic-gate 	}
9900Sstevel@tonic-gate 	if (opts & COMPARE) {
9910Sstevel@tonic-gate 		FILE *f1, *f2;
9920Sstevel@tonic-gate 		int c;
9930Sstevel@tonic-gate 
9940Sstevel@tonic-gate 		if ((f1 = fopen(target, "r")) == NULL)
9950Sstevel@tonic-gate 			goto badt;
9960Sstevel@tonic-gate 		if ((f2 = fopen(new, "r")) == NULL) {
9970Sstevel@tonic-gate 		badn:
9980Sstevel@tonic-gate 			error("%s:%s: %s\n", host, new, strerror(errno));
9990Sstevel@tonic-gate 			(void) unlink(new);
10000Sstevel@tonic-gate 			return;
10010Sstevel@tonic-gate 		}
10020Sstevel@tonic-gate 		while ((c = getc(f1)) == getc(f2))
10030Sstevel@tonic-gate 			if (c == EOF) {
10040Sstevel@tonic-gate 				(void) fclose(f1);
10050Sstevel@tonic-gate 				(void) fclose(f2);
10060Sstevel@tonic-gate 				(void) unlink(new);
10070Sstevel@tonic-gate 				ack();
10080Sstevel@tonic-gate 				return;
10090Sstevel@tonic-gate 			}
10100Sstevel@tonic-gate 		(void) fclose(f1);
10110Sstevel@tonic-gate 		(void) fclose(f2);
10120Sstevel@tonic-gate 		if (opts & VERIFY) {
10130Sstevel@tonic-gate 		differ:
10140Sstevel@tonic-gate 			(void) unlink(new);
10150Sstevel@tonic-gate 			sendrem("need to update: %s", target);
10160Sstevel@tonic-gate 			return;
10170Sstevel@tonic-gate 		}
10180Sstevel@tonic-gate 	}
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/*
10210Sstevel@tonic-gate 	 * Set last modified time.  For type == S_IFDIR, the lstat above filled
10220Sstevel@tonic-gate 	 * in stb.  Otherwise, do it now.
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 	if (type != S_IFDIR)
10250Sstevel@tonic-gate 		(void) lstat(new, &stb);
10260Sstevel@tonic-gate 	tvp[0].tv_sec = stb.st_atime;	/* old atime from target */
10270Sstevel@tonic-gate 	tvp[0].tv_usec = 0;
10280Sstevel@tonic-gate 	tvp[1].tv_sec = mtime;
10290Sstevel@tonic-gate 	tvp[1].tv_usec = 0;
10300Sstevel@tonic-gate 	if (utimes(new, tvp) < 0) {
10310Sstevel@tonic-gate 		note("%s:utimes failed %s: %s", host, new, strerror(errno));
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 	if (chog(new, owner, group, mode) < 0) {
10340Sstevel@tonic-gate 		(void) unlink(new);
10350Sstevel@tonic-gate 		return;
10360Sstevel@tonic-gate 	}
10370Sstevel@tonic-gate fixup:
10380Sstevel@tonic-gate 	if (rename(new, target) < 0) {
10390Sstevel@tonic-gate badt:
10400Sstevel@tonic-gate 		error("%s:%s: %s\n", host, target, strerror(errno));
10410Sstevel@tonic-gate 		(void) unlink(new);
10420Sstevel@tonic-gate 		return;
10430Sstevel@tonic-gate 	}
10440Sstevel@tonic-gate 	if (opts & COMPARE) {
10450Sstevel@tonic-gate 		sendrem("updated %s", target);
10460Sstevel@tonic-gate 	} else
10470Sstevel@tonic-gate 		ack();
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate /*
10510Sstevel@tonic-gate  * Creat a hard link to existing file.
10520Sstevel@tonic-gate  */
1053473Sbw static void
hardlink(cmd)10540Sstevel@tonic-gate hardlink(cmd)
10550Sstevel@tonic-gate 	char *cmd;
10560Sstevel@tonic-gate {
10570Sstevel@tonic-gate 	register char *cp;
10580Sstevel@tonic-gate 	struct stat stb;
10590Sstevel@tonic-gate 	char *oldname;
10600Sstevel@tonic-gate 	int opts, exists = 0;
10610Sstevel@tonic-gate 	char oldnamebuf[RDIST_BUFSIZ];
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate 	cp = cmd;
10640Sstevel@tonic-gate 	opts = 0;
10650Sstevel@tonic-gate 	while (*cp >= '0' && *cp <= '7')
10660Sstevel@tonic-gate 		opts = (opts << 3) | (*cp++ - '0');
10670Sstevel@tonic-gate 	if (*cp++ != ' ') {
10680Sstevel@tonic-gate 		error("hardlink: options not delimited\n");
10690Sstevel@tonic-gate 		return;
10700Sstevel@tonic-gate 	}
10710Sstevel@tonic-gate 	oldname = cp;
10720Sstevel@tonic-gate 	while (*cp && *cp != ' ')
10730Sstevel@tonic-gate 		cp++;
10740Sstevel@tonic-gate 	if (*cp != ' ') {
10750Sstevel@tonic-gate 		error("hardlink: oldname name not delimited\n");
10760Sstevel@tonic-gate 		return;
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 	*cp++ = '\0';
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	if (catname) {
10810Sstevel@tonic-gate 		if (sizeof (target) - (tp - target) >= strlen(cp) + 2) {
10820Sstevel@tonic-gate 			(void) sprintf(tp, "/%s", cp);
10830Sstevel@tonic-gate 		} else {
10840Sstevel@tonic-gate 			error("%.*s/%s: Name too long\n", tp - target,
10850Sstevel@tonic-gate 			    target, cp);
10860Sstevel@tonic-gate 			return;
10870Sstevel@tonic-gate 		}
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate 	if (lstat(target, &stb) == 0) {
10900Sstevel@tonic-gate 		int mode = stb.st_mode & S_IFMT;
10910Sstevel@tonic-gate 		if (mode != S_IFREG && mode != S_IFLNK) {
10920Sstevel@tonic-gate 			error("%s:%s: not a regular file\n", host, target);
10930Sstevel@tonic-gate 			return;
10940Sstevel@tonic-gate 		}
10950Sstevel@tonic-gate 		exists = 1;
10960Sstevel@tonic-gate 	}
10970Sstevel@tonic-gate 	if (chkparent(target) < 0) {
10980Sstevel@tonic-gate 		error("%s:%s: %s (no parent)\n",
10990Sstevel@tonic-gate 			host, target, strerror(errno));
11000Sstevel@tonic-gate 		return;
11010Sstevel@tonic-gate 	}
11020Sstevel@tonic-gate 	if (opts & VERIFY) {
11030Sstevel@tonic-gate 		struct stat nstb;
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 		if (exists && lstat(oldname, &nstb) == 0 &&
11060Sstevel@tonic-gate 		    nstb.st_mode == stb.st_mode &&
11070Sstevel@tonic-gate 		    nstb.st_ino == stb.st_ino &&
11080Sstevel@tonic-gate 		    nstb.st_dev == stb.st_dev) {
11090Sstevel@tonic-gate 			ack();
11100Sstevel@tonic-gate 			return;
11110Sstevel@tonic-gate 		} else {
11120Sstevel@tonic-gate 			sendrem("need to update: %s", target);
11130Sstevel@tonic-gate 			return;
11140Sstevel@tonic-gate 		}
11150Sstevel@tonic-gate 	}
11160Sstevel@tonic-gate 	if (exists && (unlink(target) < 0)) {
11170Sstevel@tonic-gate 		error("%s:%s: %s (unlink)\n",
11180Sstevel@tonic-gate 			host, target, strerror(errno));
11190Sstevel@tonic-gate 		return;
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 	if (*oldname == '~')
11220Sstevel@tonic-gate 		oldname = exptilde(oldnamebuf, sizeof (oldnamebuf), oldname);
11230Sstevel@tonic-gate 	if (link(oldname, target) < 0) {
11240Sstevel@tonic-gate 		error("%s:can't link %s to %s\n",
11250Sstevel@tonic-gate 			host, target, oldname);
11260Sstevel@tonic-gate 		return;
11270Sstevel@tonic-gate 	}
11280Sstevel@tonic-gate 	ack();
11290Sstevel@tonic-gate }
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate /*
11320Sstevel@tonic-gate  * Check to see if parent directory exists and create one if not.
11330Sstevel@tonic-gate  */
1134473Sbw int
chkparent(name)11350Sstevel@tonic-gate chkparent(name)
11360Sstevel@tonic-gate 	char *name;
11370Sstevel@tonic-gate {
11380Sstevel@tonic-gate 	register char *cp;
11390Sstevel@tonic-gate 	struct stat stb;
11400Sstevel@tonic-gate 
11410Sstevel@tonic-gate 	cp = rindex(name, '/');
11420Sstevel@tonic-gate 	if (cp == NULL || cp == name)
11430Sstevel@tonic-gate 		return (0);
11440Sstevel@tonic-gate 	*cp = '\0';
11450Sstevel@tonic-gate 	if (lstat(name, &stb) < 0) {
11460Sstevel@tonic-gate 		if (errno == ENOENT && chkparent(name) >= 0 &&
11470Sstevel@tonic-gate 		    mkdir(name, 0777 & ~oumask) >= 0) {
11480Sstevel@tonic-gate 			*cp = '/';
11490Sstevel@tonic-gate 			return (0);
11500Sstevel@tonic-gate 		}
11510Sstevel@tonic-gate 	} else if (ISDIR(stb.st_mode)) {
11520Sstevel@tonic-gate 		*cp = '/';
11530Sstevel@tonic-gate 		return (0);
11540Sstevel@tonic-gate 	}
11550Sstevel@tonic-gate 	*cp = '/';
11560Sstevel@tonic-gate 	return (-1);
11570Sstevel@tonic-gate }
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate /*
11600Sstevel@tonic-gate  * Change owner, group and mode of file.
11610Sstevel@tonic-gate  */
1162473Sbw int
chog(file,owner,group,mode)11630Sstevel@tonic-gate chog(file, owner, group, mode)
11640Sstevel@tonic-gate 	char *file, *owner, *group;
11650Sstevel@tonic-gate 	int mode;
11660Sstevel@tonic-gate {
11670Sstevel@tonic-gate 	register int i;
11680Sstevel@tonic-gate 	uid_t uid, gid;
11690Sstevel@tonic-gate 	extern char user[];
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/*
11720Sstevel@tonic-gate 	 * by default, set uid of file to the uid of the person running
11730Sstevel@tonic-gate 	 * this program.
11740Sstevel@tonic-gate 	 */
11750Sstevel@tonic-gate 	uid = getuid();
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	/*
11780Sstevel@tonic-gate 	 * We'll use available privileges so we just try to do what
11790Sstevel@tonic-gate 	 * the client specifies.  If the chown() fails we'll not
11800Sstevel@tonic-gate 	 * add the set-[ug]id bits; and if we want to add the set-[ug]id
11810Sstevel@tonic-gate 	 * bits and we're not permitted to do so, the OS will prevent us
11820Sstevel@tonic-gate 	 * from doing so.
11830Sstevel@tonic-gate 	 */
11840Sstevel@tonic-gate 	if (*owner == ':') {
11850Sstevel@tonic-gate 		uid = atoi(owner + 1);
11860Sstevel@tonic-gate 	} else if (pw == NULL || strcmp(owner, pw->pw_name) != 0) {
11870Sstevel@tonic-gate 		if ((pw = getpwnam(owner)) == NULL) {
11880Sstevel@tonic-gate 			if (mode & 04000) {
11890Sstevel@tonic-gate 				note("%s:%s: unknown login name, "
11900Sstevel@tonic-gate 				    "clearing setuid", host, owner);
11910Sstevel@tonic-gate 				mode &= ~04000;
11920Sstevel@tonic-gate 			}
11930Sstevel@tonic-gate 		} else {
11940Sstevel@tonic-gate 			uid = pw->pw_uid;
11950Sstevel@tonic-gate 		}
11960Sstevel@tonic-gate 	} else {
11970Sstevel@tonic-gate 		uid = pw->pw_uid;
11980Sstevel@tonic-gate 	}
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 	if (*group == ':') {
12010Sstevel@tonic-gate 		gid = atoi(group + 1);
12020Sstevel@tonic-gate 		goto ok;
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 	gid = -1;
12060Sstevel@tonic-gate 	if (gr == NULL || strcmp(group, gr->gr_name) != 0) {
12070Sstevel@tonic-gate 		if ((*group == ':' &&
12080Sstevel@tonic-gate 		    (getgrgid(gid = atoi(group + 1)) == NULL)) ||
12090Sstevel@tonic-gate 		    ((gr = getgrnam(group)) == NULL)) {
12100Sstevel@tonic-gate 			if (mode & 02000) {
12110Sstevel@tonic-gate 				note("%s:%s: unknown group", host, group);
12120Sstevel@tonic-gate 				mode &= ~02000;
12130Sstevel@tonic-gate 			}
12140Sstevel@tonic-gate 		} else
12150Sstevel@tonic-gate 			gid = gr->gr_gid;
12160Sstevel@tonic-gate 	} else
12170Sstevel@tonic-gate 		gid = gr->gr_gid;
12180Sstevel@tonic-gate ok:
12190Sstevel@tonic-gate 	if (chown(file, uid, gid) < 0 ||
12200Sstevel@tonic-gate 	    (mode & 07000) && chmod(file, mode) < 0) {
12210Sstevel@tonic-gate 		note("%s: chown or chmod failed: file %s:  %s",
12220Sstevel@tonic-gate 		    host, file, strerror(errno));
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 	return (0);
12250Sstevel@tonic-gate }
12260Sstevel@tonic-gate 
12270Sstevel@tonic-gate /*
12280Sstevel@tonic-gate  * Check for files on the machine being updated that are not on the master
12290Sstevel@tonic-gate  * machine and remove them.
12300Sstevel@tonic-gate  */
1231473Sbw static void
rmchk(opts)12320Sstevel@tonic-gate rmchk(opts)
12330Sstevel@tonic-gate 	int opts;
12340Sstevel@tonic-gate {
12350Sstevel@tonic-gate 	register char *cp, *s;
12360Sstevel@tonic-gate 	struct stat stb;
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	if (debug)
12390Sstevel@tonic-gate 		printf("rmchk()\n");
12400Sstevel@tonic-gate 
12410Sstevel@tonic-gate 	/*
12420Sstevel@tonic-gate 	 * Tell the remote to clean the files from the last directory sent.
12430Sstevel@tonic-gate 	 */
12440Sstevel@tonic-gate 	(void) sprintf(buf, "C%o\n", opts & VERIFY);
12450Sstevel@tonic-gate 	if (debug)
12460Sstevel@tonic-gate 		printf("buf = %s", buf);
12470Sstevel@tonic-gate 	(void) deswrite(rem, buf, strlen(buf), 0);
12480Sstevel@tonic-gate 	if (response() < 0)
12490Sstevel@tonic-gate 		return;
12500Sstevel@tonic-gate 	for (;;) {
12510Sstevel@tonic-gate 		cp = s = buf;
12520Sstevel@tonic-gate 		do {
12530Sstevel@tonic-gate 			if (desread(rem, cp, 1, 0) != 1)
12540Sstevel@tonic-gate 				lostconn();
12550Sstevel@tonic-gate 		} while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]);
12560Sstevel@tonic-gate 
12570Sstevel@tonic-gate 		switch (*s++) {
12580Sstevel@tonic-gate 		case 'Q': /* Query if file should be removed */
12590Sstevel@tonic-gate 			/*
12600Sstevel@tonic-gate 			 * Return the following codes to remove query.
12610Sstevel@tonic-gate 			 * N\n -- file exists - DON'T remove.
12620Sstevel@tonic-gate 			 * Y\n -- file doesn't exist - REMOVE.
12630Sstevel@tonic-gate 			 */
12640Sstevel@tonic-gate 			*--cp = '\0';
12650Sstevel@tonic-gate 			(void) sprintf(tp, "/%s", s);
12660Sstevel@tonic-gate 			if (debug)
12670Sstevel@tonic-gate 				printf("check %s\n", target);
12680Sstevel@tonic-gate 			if (except(target))
12690Sstevel@tonic-gate 				(void) deswrite(rem, "N\n", 2, 0);
12700Sstevel@tonic-gate 			else if (lstat(target, &stb) < 0)
12710Sstevel@tonic-gate 				(void) deswrite(rem, "Y\n", 2, 0);
12720Sstevel@tonic-gate 			else
12730Sstevel@tonic-gate 				(void) deswrite(rem, "N\n", 2, 0);
12740Sstevel@tonic-gate 			break;
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate 		case '\0':
12770Sstevel@tonic-gate 			*--cp = '\0';
12780Sstevel@tonic-gate 			if (*s != '\0')
12790Sstevel@tonic-gate 				log(lfp, "%s\n", s);
12800Sstevel@tonic-gate 			break;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 		case 'E':
12830Sstevel@tonic-gate 			*tp = '\0';
12840Sstevel@tonic-gate 			(void) deswrite(rem, "\0\n", 2, 0);
12850Sstevel@tonic-gate 			return;
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 		case '\1':
12880Sstevel@tonic-gate 		case '\2':
12890Sstevel@tonic-gate 			nerrs++;
12900Sstevel@tonic-gate 			if (*s != '\n') {
12910Sstevel@tonic-gate 				if (!iamremote) {
12920Sstevel@tonic-gate 					fflush(stdout);
12930Sstevel@tonic-gate 					(void) write(2, s, cp - s);
12940Sstevel@tonic-gate 				}
12950Sstevel@tonic-gate 				if (lfp != NULL)
12960Sstevel@tonic-gate 					(void) fwrite(s, 1, cp - s, lfp);
12970Sstevel@tonic-gate 			}
12980Sstevel@tonic-gate 			if (buf[0] == '\2')
12990Sstevel@tonic-gate 				lostconn();
13000Sstevel@tonic-gate 			break;
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 		default:
13030Sstevel@tonic-gate 			error("rmchk: unexpected response '%s'\n", buf);
13040Sstevel@tonic-gate 			(void) deswrite(rem, "\1\n", 2, 0);
13050Sstevel@tonic-gate 		}
13060Sstevel@tonic-gate 	}
13070Sstevel@tonic-gate }
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate /*
13100Sstevel@tonic-gate  * Check the current directory (initialized by the 'T' command to server())
13110Sstevel@tonic-gate  * for extraneous files and remove them.
13120Sstevel@tonic-gate  */
1313473Sbw static void
clean(cp)13140Sstevel@tonic-gate clean(cp)
13150Sstevel@tonic-gate 	register char *cp;
13160Sstevel@tonic-gate {
13170Sstevel@tonic-gate 	DIR *d;
13180Sstevel@tonic-gate 	register struct dirent *dp;
13190Sstevel@tonic-gate 	struct stat stb;
13200Sstevel@tonic-gate 	char *otp;
13210Sstevel@tonic-gate 	int len, opts;
13220Sstevel@tonic-gate 
13230Sstevel@tonic-gate 	opts = 0;
13240Sstevel@tonic-gate 	while (*cp >= '0' && *cp <= '7')
13250Sstevel@tonic-gate 		opts = (opts << 3) | (*cp++ - '0');
13260Sstevel@tonic-gate 	if (*cp != '\0') {
13270Sstevel@tonic-gate 		error("clean: options not delimited\n");
13280Sstevel@tonic-gate 		return;
13290Sstevel@tonic-gate 	}
13300Sstevel@tonic-gate 	if ((d = opendir(target)) == NULL) {
13310Sstevel@tonic-gate 		error("%s:%s: %s\n", host, target, strerror(errno));
13320Sstevel@tonic-gate 		return;
13330Sstevel@tonic-gate 	}
13340Sstevel@tonic-gate 	ack();
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	otp = tp;
13370Sstevel@tonic-gate 	len = tp - target;
13380Sstevel@tonic-gate 	while (dp = readdir(d)) {
13390Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
13400Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
13410Sstevel@tonic-gate 			continue;
13420Sstevel@tonic-gate 		if ((int)(len + 1 + strlen(dp->d_name)) >=
13430Sstevel@tonic-gate 		    (int)(RDIST_BUFSIZ - 1)) {
13440Sstevel@tonic-gate 			error("%s:%s/%s: Name too long\n",
13450Sstevel@tonic-gate 				host, target, dp->d_name);
13460Sstevel@tonic-gate 			continue;
13470Sstevel@tonic-gate 		}
13480Sstevel@tonic-gate 		tp = otp;
13490Sstevel@tonic-gate 		*tp++ = '/';
13500Sstevel@tonic-gate 		cp = dp->d_name;
13510Sstevel@tonic-gate 		while (*tp++ = *cp++)
13520Sstevel@tonic-gate 			;
13530Sstevel@tonic-gate 		tp--;
13540Sstevel@tonic-gate 		if (lstat(target, &stb) < 0) {
13550Sstevel@tonic-gate 			error("%s:%s: %s\n", host, target, strerror(errno));
13560Sstevel@tonic-gate 			continue;
13570Sstevel@tonic-gate 		}
13580Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "Q%s\n", dp->d_name);
13590Sstevel@tonic-gate 		(void) write(wrem, buf, strlen(buf));
13600Sstevel@tonic-gate 		cp = buf;
13610Sstevel@tonic-gate 		do {
13620Sstevel@tonic-gate 			if (read(rem, cp, 1) != 1)
13630Sstevel@tonic-gate 				cleanup();
13640Sstevel@tonic-gate 		} while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]);
13650Sstevel@tonic-gate 		*--cp = '\0';
13660Sstevel@tonic-gate 		cp = buf;
13670Sstevel@tonic-gate 		if (*cp != 'Y')
13680Sstevel@tonic-gate 			continue;
13690Sstevel@tonic-gate 		if (opts & VERIFY) {
13700Sstevel@tonic-gate 			sendrem("need to remove: %s", target);
13710Sstevel@tonic-gate 		} else
13720Sstevel@tonic-gate 			(void) recursive_remove(&stb);
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 	closedir(d);
13750Sstevel@tonic-gate 	(void) write(wrem, "E\n", 2);
13760Sstevel@tonic-gate 	(void) response();
13770Sstevel@tonic-gate 	tp = otp;
13780Sstevel@tonic-gate 	*tp = '\0';
13790Sstevel@tonic-gate }
13800Sstevel@tonic-gate 
13810Sstevel@tonic-gate /*
13820Sstevel@tonic-gate  * Remove a file or directory (recursively) and send back an acknowledge
13830Sstevel@tonic-gate  * or an error message.
13840Sstevel@tonic-gate  */
1385473Sbw static void
recursive_remove(stp)13860Sstevel@tonic-gate recursive_remove(stp)
13870Sstevel@tonic-gate 	struct stat *stp;
13880Sstevel@tonic-gate {
13890Sstevel@tonic-gate 	DIR *d;
13900Sstevel@tonic-gate 	struct dirent *dp;
13910Sstevel@tonic-gate 	register char *cp;
13920Sstevel@tonic-gate 	struct stat stb;
13930Sstevel@tonic-gate 	char *otp;
13940Sstevel@tonic-gate 	int len;
13950Sstevel@tonic-gate 
13960Sstevel@tonic-gate 	switch (stp->st_mode & S_IFMT) {
13970Sstevel@tonic-gate 	case S_IFREG:
13980Sstevel@tonic-gate 	case S_IFLNK:
13990Sstevel@tonic-gate 		if (unlink(target) < 0)
14000Sstevel@tonic-gate 			goto bad;
14010Sstevel@tonic-gate 		goto removed;
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 	case S_IFDIR:
14040Sstevel@tonic-gate 		break;
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	default:
14070Sstevel@tonic-gate 		error("%s:%s: not a plain file\n", host, target);
14080Sstevel@tonic-gate 		return;
14090Sstevel@tonic-gate 	}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 	if ((d = opendir(target)) == NULL)
14120Sstevel@tonic-gate 		goto bad;
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate 	otp = tp;
14150Sstevel@tonic-gate 	len = tp - target;
14160Sstevel@tonic-gate 	while (dp = readdir(d)) {
14170Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
14180Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
14190Sstevel@tonic-gate 			continue;
14200Sstevel@tonic-gate 		if ((int)(len + 1 + strlen(dp->d_name)) >=
14210Sstevel@tonic-gate 		    (int)(RDIST_BUFSIZ - 1)) {
14220Sstevel@tonic-gate 			error("%s:%s/%s: Name too long\n",
14230Sstevel@tonic-gate 				host, target, dp->d_name);
14240Sstevel@tonic-gate 			continue;
14250Sstevel@tonic-gate 		}
14260Sstevel@tonic-gate 		tp = otp;
14270Sstevel@tonic-gate 		*tp++ = '/';
14280Sstevel@tonic-gate 		cp = dp->d_name;
14290Sstevel@tonic-gate 		while (*tp++ = *cp++)
14300Sstevel@tonic-gate 			;
14310Sstevel@tonic-gate 		tp--;
14320Sstevel@tonic-gate 		if (lstat(target, &stb) < 0) {
14330Sstevel@tonic-gate 			error("%s:%s: %s\n", host, target, strerror(errno));
14340Sstevel@tonic-gate 			continue;
14350Sstevel@tonic-gate 		}
14360Sstevel@tonic-gate 		recursive_remove(&stb);
14370Sstevel@tonic-gate 	}
14380Sstevel@tonic-gate 	closedir(d);
14390Sstevel@tonic-gate 	tp = otp;
14400Sstevel@tonic-gate 	*tp = '\0';
14410Sstevel@tonic-gate 	if (rmdir(target) < 0) {
14420Sstevel@tonic-gate bad:
14430Sstevel@tonic-gate 		error("%s:%s: %s\n", host, target, strerror(errno));
14440Sstevel@tonic-gate 		return;
14450Sstevel@tonic-gate 	}
14460Sstevel@tonic-gate removed:
14470Sstevel@tonic-gate 	sendrem("removed %s", target);
14480Sstevel@tonic-gate }
14490Sstevel@tonic-gate 
14500Sstevel@tonic-gate /*
14510Sstevel@tonic-gate  * Execute a shell command to handle special cases.
14520Sstevel@tonic-gate  */
1453473Sbw static void
dospecial(cmd)14540Sstevel@tonic-gate dospecial(cmd)
14550Sstevel@tonic-gate 	char *cmd;
14560Sstevel@tonic-gate {
14570Sstevel@tonic-gate 	int fd[2], status, pid, i;
14580Sstevel@tonic-gate 	register char *cp, *s;
14590Sstevel@tonic-gate 	char sbuf[RDIST_BUFSIZ];
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate 	if (pipe(fd) < 0) {
14620Sstevel@tonic-gate 		error("%s\n", strerror(errno));
14630Sstevel@tonic-gate 		return;
14640Sstevel@tonic-gate 	}
14650Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
14660Sstevel@tonic-gate 		/*
14670Sstevel@tonic-gate 		 * Return everything the shell commands print.
14680Sstevel@tonic-gate 		 */
14690Sstevel@tonic-gate 		(void) close(0);
14700Sstevel@tonic-gate 		(void) close(1);
14710Sstevel@tonic-gate 		(void) close(2);
14720Sstevel@tonic-gate 		(void) open("/dev/null", 0);
14730Sstevel@tonic-gate 		(void) dup(fd[1]);
14740Sstevel@tonic-gate 		(void) dup(fd[1]);
14750Sstevel@tonic-gate 		(void) close(fd[0]);
14760Sstevel@tonic-gate 		(void) close(fd[1]);
14770Sstevel@tonic-gate 		execl("/bin/sh", "sh", "-c", cmd, 0);
14780Sstevel@tonic-gate 		_exit(127);
14790Sstevel@tonic-gate 	}
14800Sstevel@tonic-gate 	(void) close(fd[1]);
14810Sstevel@tonic-gate 	s = sbuf;
14820Sstevel@tonic-gate 	*s++ = '\0';
14830Sstevel@tonic-gate 	while ((i = read(fd[0], buf, RDIST_BUFSIZ)) > 0) {
14840Sstevel@tonic-gate 		cp = buf;
14850Sstevel@tonic-gate 		do {
14860Sstevel@tonic-gate 			*s++ = *cp++;
14870Sstevel@tonic-gate 			if (cp[-1] != '\n') {
14880Sstevel@tonic-gate 				if (s < &sbuf[RDIST_BUFSIZ - 1])
14890Sstevel@tonic-gate 					continue;
14900Sstevel@tonic-gate 				*s++ = '\n';
14910Sstevel@tonic-gate 			}
14920Sstevel@tonic-gate 			/*
14930Sstevel@tonic-gate 			 * Throw away blank lines.
14940Sstevel@tonic-gate 			 */
14950Sstevel@tonic-gate 			if (s == &sbuf[2]) {
14960Sstevel@tonic-gate 				s--;
14970Sstevel@tonic-gate 				continue;
14980Sstevel@tonic-gate 			}
14990Sstevel@tonic-gate 			(void) write(wrem, sbuf, s - sbuf);
15000Sstevel@tonic-gate 			s = &sbuf[1];
15010Sstevel@tonic-gate 		} while (--i);
15020Sstevel@tonic-gate 	}
15030Sstevel@tonic-gate 	if (s > &sbuf[1]) {
15040Sstevel@tonic-gate 		*s++ = '\n';
15050Sstevel@tonic-gate 		(void) write(wrem, sbuf, s - sbuf);
15060Sstevel@tonic-gate 	}
15070Sstevel@tonic-gate 	while ((i = wait(&status)) != pid && i != -1)
15080Sstevel@tonic-gate 		;
15090Sstevel@tonic-gate 	if (i == -1)
15100Sstevel@tonic-gate 		status = -1;
15110Sstevel@tonic-gate 	(void) close(fd[0]);
15120Sstevel@tonic-gate 	if (status)
15130Sstevel@tonic-gate 		error("shell returned %d\n", status);
15140Sstevel@tonic-gate 	else
15150Sstevel@tonic-gate 		ack();
15160Sstevel@tonic-gate }
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate /*VARARGS2*/
1519473Sbw void
log(fp,fmt,a1,a2,a3)15200Sstevel@tonic-gate log(fp, fmt, a1, a2, a3)
15210Sstevel@tonic-gate 	FILE *fp;
15220Sstevel@tonic-gate 	char *fmt;
15230Sstevel@tonic-gate 	int a1, a2, a3;
15240Sstevel@tonic-gate {
15250Sstevel@tonic-gate 	/* Print changes locally if not quiet mode */
15260Sstevel@tonic-gate 	if (!qflag)
15270Sstevel@tonic-gate 		printf(fmt, a1, a2, a3);
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	/* Save changes (for mailing) if really updating files */
15300Sstevel@tonic-gate 	if (!(options & VERIFY) && fp != NULL)
15310Sstevel@tonic-gate 		fprintf(fp, fmt, a1, a2, a3);
15320Sstevel@tonic-gate }
15330Sstevel@tonic-gate 
15340Sstevel@tonic-gate /*VARARGS1*/
1535473Sbw void
error(fmt,a1,a2,a3)15360Sstevel@tonic-gate error(fmt, a1, a2, a3)
15370Sstevel@tonic-gate 	char *fmt;
15380Sstevel@tonic-gate 	int a1, a2, a3;
15390Sstevel@tonic-gate {
15400Sstevel@tonic-gate 	static FILE *fp;
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	nerrs++;
15430Sstevel@tonic-gate 	if (!fp && !(fp = fdopen(rem, "w")))
15440Sstevel@tonic-gate 		return;
15450Sstevel@tonic-gate 	if (iamremote) {
15460Sstevel@tonic-gate 		(void) fprintf(fp, "%crdist: ", 0x01);
15470Sstevel@tonic-gate 		(void) fprintf(fp, fmt, a1, a2, a3);
15480Sstevel@tonic-gate 		fflush(fp);
15490Sstevel@tonic-gate 	} else {
15500Sstevel@tonic-gate 		fflush(stdout);
15510Sstevel@tonic-gate 		(void) fprintf(stderr, "rdist: ");
15520Sstevel@tonic-gate 		(void) fprintf(stderr, fmt, a1, a2, a3);
15530Sstevel@tonic-gate 		fflush(stderr);
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 	if (lfp != NULL) {
15560Sstevel@tonic-gate 		(void) fprintf(lfp, "rdist: ");
15570Sstevel@tonic-gate 		(void) fprintf(lfp, fmt, a1, a2, a3);
15580Sstevel@tonic-gate 		fflush(lfp);
15590Sstevel@tonic-gate 	}
15600Sstevel@tonic-gate }
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate /*VARARGS1*/
1563473Sbw void
fatal(fmt,a1,a2,a3)15640Sstevel@tonic-gate fatal(fmt, a1, a2, a3)
15650Sstevel@tonic-gate 	char *fmt;
15660Sstevel@tonic-gate 	int a1, a2, a3;
15670Sstevel@tonic-gate {
15680Sstevel@tonic-gate 	static FILE *fp;
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	nerrs++;
15710Sstevel@tonic-gate 	if (!fp && !(fp = fdopen(rem, "w")))
15720Sstevel@tonic-gate 		return;
15730Sstevel@tonic-gate 	if (iamremote) {
15740Sstevel@tonic-gate 		(void) fprintf(fp, "%crdist: ", 0x02);
15750Sstevel@tonic-gate 		(void) fprintf(fp, fmt, a1, a2, a3);
15760Sstevel@tonic-gate 		fflush(fp);
15770Sstevel@tonic-gate 	} else {
15780Sstevel@tonic-gate 		fflush(stdout);
15790Sstevel@tonic-gate 		(void) fprintf(stderr, "rdist: ");
15800Sstevel@tonic-gate 		(void) fprintf(stderr, fmt, a1, a2, a3);
15810Sstevel@tonic-gate 		fflush(stderr);
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 	if (lfp != NULL) {
15840Sstevel@tonic-gate 		(void) fprintf(lfp, "rdist: ");
15850Sstevel@tonic-gate 		(void) fprintf(lfp, fmt, a1, a2, a3);
15860Sstevel@tonic-gate 		fflush(lfp);
15870Sstevel@tonic-gate 	}
15880Sstevel@tonic-gate 	cleanup();
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate 
1591473Sbw int
response()15920Sstevel@tonic-gate response()
15930Sstevel@tonic-gate {
15940Sstevel@tonic-gate 	char *cp, *s;
15950Sstevel@tonic-gate 	char resp[RDIST_BUFSIZ];
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	if (debug)
15980Sstevel@tonic-gate 		printf("response()\n");
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 	cp = s = resp;
16010Sstevel@tonic-gate more:
16020Sstevel@tonic-gate 	do {
16030Sstevel@tonic-gate 		if (desread(rem, cp, 1, 0) != 1)
16040Sstevel@tonic-gate 			lostconn();
16050Sstevel@tonic-gate 	} while (*cp++ != '\n' && cp < &resp[RDIST_BUFSIZ]);
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	switch (*s++) {
16080Sstevel@tonic-gate 	case '\0':
16090Sstevel@tonic-gate 		*--cp = '\0';
16100Sstevel@tonic-gate 		if (*s != '\0') {
16110Sstevel@tonic-gate 			log(lfp, "%s\n", s);
16120Sstevel@tonic-gate 			return (1);
16130Sstevel@tonic-gate 		}
16140Sstevel@tonic-gate 		return (0);
16150Sstevel@tonic-gate 	case '\3':
16160Sstevel@tonic-gate 		*--cp = '\0';
16170Sstevel@tonic-gate 		log(lfp, "Note: %s\n", s);
16180Sstevel@tonic-gate 		return (response());
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	default:
16210Sstevel@tonic-gate 		s--;
16220Sstevel@tonic-gate 		/* fall into... */
16230Sstevel@tonic-gate 	case '\1':
16240Sstevel@tonic-gate 	case '\2':
16250Sstevel@tonic-gate 		nerrs++;
16260Sstevel@tonic-gate 		if (*s != '\n') {
16270Sstevel@tonic-gate 			if (!iamremote) {
16280Sstevel@tonic-gate 				fflush(stdout);
16290Sstevel@tonic-gate 				(void) write(2, s, cp - s);
16300Sstevel@tonic-gate 			}
16310Sstevel@tonic-gate 			if (lfp != NULL)
16320Sstevel@tonic-gate 				(void) fwrite(s, 1, cp - s, lfp);
16330Sstevel@tonic-gate 		}
16340Sstevel@tonic-gate 		if (cp == &resp[RDIST_BUFSIZ] && *(cp - 1) != '\n') {
16350Sstevel@tonic-gate 			/* preserve status code */
16360Sstevel@tonic-gate 			cp = s;
16370Sstevel@tonic-gate 			s = resp;
16380Sstevel@tonic-gate 			goto more;
16390Sstevel@tonic-gate 		}
16400Sstevel@tonic-gate 		if (resp[0] == '\2')
16410Sstevel@tonic-gate 			lostconn();
16420Sstevel@tonic-gate 		return (-1);
16430Sstevel@tonic-gate 	}
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate /*
16470Sstevel@tonic-gate  * Remove temporary files and do any cleanup operations before exiting.
16480Sstevel@tonic-gate  */
16490Sstevel@tonic-gate void
cleanup()16500Sstevel@tonic-gate cleanup()
16510Sstevel@tonic-gate {
16520Sstevel@tonic-gate 	(void) unlink(Tmpfile);
16530Sstevel@tonic-gate 	exit(1);
16540Sstevel@tonic-gate }
16550Sstevel@tonic-gate 
1656473Sbw static void
note(fmt,a1,a2,a3)16570Sstevel@tonic-gate note(fmt, a1, a2, a3)
16580Sstevel@tonic-gate char *fmt;
16590Sstevel@tonic-gate int a1, a2, a3;
16600Sstevel@tonic-gate {
16610Sstevel@tonic-gate 	static char buf[RDIST_BUFSIZ];
16620Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf) - 1, fmt, a1, a2, a3);
16630Sstevel@tonic-gate 	comment(buf);
16640Sstevel@tonic-gate }
16650Sstevel@tonic-gate 
1666473Sbw static void
comment(s)16670Sstevel@tonic-gate comment(s)
16680Sstevel@tonic-gate char *s;
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate 	char three = '\3';
16710Sstevel@tonic-gate 	char nl = '\n';
16720Sstevel@tonic-gate 	struct iovec iov[3];
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	iov[0].iov_base = &three;
16750Sstevel@tonic-gate 	iov[0].iov_len = sizeof (char);
16760Sstevel@tonic-gate 	iov[1].iov_base = s;
16770Sstevel@tonic-gate 	iov[1].iov_len = strlen(s);
16780Sstevel@tonic-gate 	iov[2].iov_base = &nl;
16790Sstevel@tonic-gate 	iov[2].iov_len = sizeof (char);
16800Sstevel@tonic-gate 	(void) writev(rem, iov, 3);
16810Sstevel@tonic-gate }
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate /*
16840Sstevel@tonic-gate  * Send message to other end.
16850Sstevel@tonic-gate  * N.B.: uses buf[].
16860Sstevel@tonic-gate  */
16870Sstevel@tonic-gate void
sendrem(fmt,a1,a2,a3)16880Sstevel@tonic-gate sendrem(fmt, a1, a2, a3)
16890Sstevel@tonic-gate char *fmt;
16900Sstevel@tonic-gate int a1, a2, a3;
16910Sstevel@tonic-gate {
16920Sstevel@tonic-gate 	register int len;
16930Sstevel@tonic-gate 
16940Sstevel@tonic-gate 	buf[0] = '\0';
16950Sstevel@tonic-gate 	len = snprintf(buf + 1, sizeof (buf) - 1, fmt, a1, a2, a3) + 2;
16960Sstevel@tonic-gate 	if (len > sizeof (buf))
16970Sstevel@tonic-gate 		len = sizeof (buf);
16980Sstevel@tonic-gate 	buf[len - 1] = '\n';
16990Sstevel@tonic-gate 	(void) write(wrem, buf, len);
17000Sstevel@tonic-gate }
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate /*
17030Sstevel@tonic-gate  * strsub(old, new, s)
17040Sstevel@tonic-gate  *
17050Sstevel@tonic-gate  * Return a pointer to a new string created by replacing substring old
17060Sstevel@tonic-gate  * with substring new in string s.  String s is assumed to begin with
17070Sstevel@tonic-gate  * substring old.
17080Sstevel@tonic-gate  */
17090Sstevel@tonic-gate char *
strsub(old,new,s)17100Sstevel@tonic-gate strsub(old, new, s)
17110Sstevel@tonic-gate 	char *old, *new, *s;
17120Sstevel@tonic-gate {
17130Sstevel@tonic-gate 	static char pbuf[PATH_MAX];
17140Sstevel@tonic-gate 	register char *p, *q, *r, *plim;
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	/* prepend new to pbuf */
17170Sstevel@tonic-gate 	for (p = pbuf, q = new, plim = pbuf + sizeof (pbuf) - 1;
17180Sstevel@tonic-gate 	/* CSTYLED */
17190Sstevel@tonic-gate 	    *q && (p < plim);)
17200Sstevel@tonic-gate 		*p++ = *q++;
17210Sstevel@tonic-gate 	/* p now points to the byte in pbuf where more copying should begin */
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	/* skip over the part of s which begins with old */
17240Sstevel@tonic-gate 	for (r = old, q = s; *r; q++, r++)
17250Sstevel@tonic-gate 		;
17260Sstevel@tonic-gate 	/* q now points to the byte in s where more copying should begin */
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 	while (*q && (p < plim))
17290Sstevel@tonic-gate 		*p++ = *q++;
17300Sstevel@tonic-gate 	*p = '\0';
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 	return (pbuf);
17330Sstevel@tonic-gate }
1734