17c478bd9Sstevel@tonic-gate /*
256fbdff6SChandan  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1983 Regents of the University of California.
87c478bd9Sstevel@tonic-gate  * All rights reserved.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms are permitted
117c478bd9Sstevel@tonic-gate  * provided that the above copyright notice and this paragraph are
127c478bd9Sstevel@tonic-gate  * duplicated in all such forms and that any documentation,
137c478bd9Sstevel@tonic-gate  * advertising materials, and other materials related to such
147c478bd9Sstevel@tonic-gate  * distribution and use acknowledge that the software was developed
157c478bd9Sstevel@tonic-gate  * by the University of California, Berkeley.  The name of the
167c478bd9Sstevel@tonic-gate  * University may not be used to endorse or promote products derived
177c478bd9Sstevel@tonic-gate  * from this software without specific prior written permission.
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "defs.h"
217c478bd9Sstevel@tonic-gate #include <signal.h>
227c478bd9Sstevel@tonic-gate #include <string.h>
237c478bd9Sstevel@tonic-gate #include <errno.h>
247c478bd9Sstevel@tonic-gate #include <limits.h>
257c478bd9Sstevel@tonic-gate #include <ctype.h>
267c478bd9Sstevel@tonic-gate #include <krb5defs.h>
2756143343SToomas Soome #include <stdarg.h>
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * If we want to write *to* the client rdist program, *from* the server
317c478bd9Sstevel@tonic-gate  * side (server-side child `rdist -Server' process exec'ed off of in.rshd),
327c478bd9Sstevel@tonic-gate  * we write to stdout/stderr, since there is a pipe connecting stdout/stderr
337c478bd9Sstevel@tonic-gate  * to the outside world (which is why we use `wrem' and not `rem').
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate int wrem = 1;
367c478bd9Sstevel@tonic-gate 
3756143343SToomas Soome #define	ack()	(void) write(wrem, "\0\n", 2)
3856143343SToomas Soome #define	err()	(void) write(wrem, "\1\n", 2)
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * Set when a desread() is reqd. in response()
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate struct	linkbuf *ihead;		/* list of files with more than one link */
4556143343SToomas Soome extern char	buf[RDIST_BUFSIZ];	/* general purpose buffer */
467c478bd9Sstevel@tonic-gate char	source[RDIST_BUFSIZ];	/* base source directory name */
477c478bd9Sstevel@tonic-gate char	destination[RDIST_BUFSIZ];	/* base destination directory name */
487c478bd9Sstevel@tonic-gate char	target[RDIST_BUFSIZ];	/* target/source directory name */
497c478bd9Sstevel@tonic-gate char	*tp;			/* pointer to end of target name */
507c478bd9Sstevel@tonic-gate char	*Tdest;			/* pointer to last T dest */
517c478bd9Sstevel@tonic-gate int	catname;		/* cat name to target name */
527c478bd9Sstevel@tonic-gate char	*stp[32];		/* stack of saved tp's for directories */
537c478bd9Sstevel@tonic-gate int	oumask;			/* old umask for creating files */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate extern	FILE *lfp;		/* log file for mailing changes */
567c478bd9Sstevel@tonic-gate 
5756143343SToomas Soome void	cleanup(int);
5856143343SToomas Soome struct linkbuf *savelink(struct stat *, int);
5956143343SToomas Soome char	*strsub(char *, char *, char *);
607c478bd9Sstevel@tonic-gate 
6156143343SToomas Soome static void comment(char *);
6256143343SToomas Soome static void note(char *, ...);
63740638c8Sbw static void hardlink(char *cmd);
6456143343SToomas Soome void error(char *, ...);
6556143343SToomas Soome void log(FILE *, char *, ...);
66740638c8Sbw static void recursive_remove(struct stat *stp);
67740638c8Sbw static void recvf(char *cmd, int type);
68740638c8Sbw static void query(char *name);
69740638c8Sbw static void sendf(char *rname, int opts);
70740638c8Sbw static void rmchk(int opts);
71740638c8Sbw static void dospecial(char *cmd);
72740638c8Sbw static void clean(char *cp);
73740638c8Sbw 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Server routine to read requests and process them.
767c478bd9Sstevel@tonic-gate  * Commands are:
777c478bd9Sstevel@tonic-gate  *	Tname	- Transmit file if out of date
787c478bd9Sstevel@tonic-gate  *	Vname	- Verify if file out of date or not
797c478bd9Sstevel@tonic-gate  *	Qname	- Query if file exists. Return mtime & size if it does.
807c478bd9Sstevel@tonic-gate  */
81740638c8Sbw void
server(void)8256143343SToomas Soome server(void)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	char cmdbuf[RDIST_BUFSIZ];
8556143343SToomas Soome 	char *cp;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	signal(SIGHUP, cleanup);
887c478bd9Sstevel@tonic-gate 	signal(SIGINT, cleanup);
897c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, cleanup);
907c478bd9Sstevel@tonic-gate 	signal(SIGTERM, cleanup);
917c478bd9Sstevel@tonic-gate 	signal(SIGPIPE, cleanup);
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	rem = 0;
947c478bd9Sstevel@tonic-gate 	oumask = umask(0);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "V%d\n", VERSION);
977c478bd9Sstevel@tonic-gate 	(void) write(wrem, buf, strlen(buf));
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	for (;;) {
1007c478bd9Sstevel@tonic-gate 		cp = cmdbuf;
1017c478bd9Sstevel@tonic-gate 		if (read(rem, cp, 1) <= 0)
1027c478bd9Sstevel@tonic-gate 			return;
1037c478bd9Sstevel@tonic-gate 		if (*cp++ == '\n') {
1047c478bd9Sstevel@tonic-gate 			error("server: expected control record\n");
1057c478bd9Sstevel@tonic-gate 			continue;
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 		do {
1087c478bd9Sstevel@tonic-gate 			if (read(rem, cp, 1) != 1)
10956143343SToomas Soome 				cleanup(0);
1107c478bd9Sstevel@tonic-gate 		} while (*cp++ != '\n' && cp < &cmdbuf[RDIST_BUFSIZ]);
1117c478bd9Sstevel@tonic-gate 		*--cp = '\0';
1127c478bd9Sstevel@tonic-gate 		cp = cmdbuf;
1137c478bd9Sstevel@tonic-gate 		switch (*cp++) {
1147c478bd9Sstevel@tonic-gate 		case 'T':  /* init target file/directory name */
1157c478bd9Sstevel@tonic-gate 			catname = 1;	/* target should be directory */
1167c478bd9Sstevel@tonic-gate 			goto dotarget;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 		case 't':  /* init target file/directory name */
1197c478bd9Sstevel@tonic-gate 			catname = 0;
1207c478bd9Sstevel@tonic-gate 		dotarget:
1217c478bd9Sstevel@tonic-gate 			if (exptilde(target, sizeof (target), cp) == NULL)
1227c478bd9Sstevel@tonic-gate 				continue;
1237c478bd9Sstevel@tonic-gate 			tp = target;
1247c478bd9Sstevel@tonic-gate 			while (*tp)
1257c478bd9Sstevel@tonic-gate 				tp++;
1267c478bd9Sstevel@tonic-gate 			ack();
1277c478bd9Sstevel@tonic-gate 			continue;
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate 		case 'R':  /* Transfer a regular file. */
1307c478bd9Sstevel@tonic-gate 			recvf(cp, S_IFREG);
1317c478bd9Sstevel@tonic-gate 			continue;
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 		case 'D':  /* Transfer a directory. */
1347c478bd9Sstevel@tonic-gate 			recvf(cp, S_IFDIR);
1357c478bd9Sstevel@tonic-gate 			continue;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 		case 'K':  /* Transfer symbolic link. */
1387c478bd9Sstevel@tonic-gate 			recvf(cp, S_IFLNK);
1397c478bd9Sstevel@tonic-gate 			continue;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		case 'k':  /* Transfer hard link. */
1427c478bd9Sstevel@tonic-gate 			hardlink(cp);
1437c478bd9Sstevel@tonic-gate 			continue;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 		case 'E':  /* End. (of directory) */
1467c478bd9Sstevel@tonic-gate 			*tp = '\0';
1477c478bd9Sstevel@tonic-gate 			if (catname <= 0) {
1487c478bd9Sstevel@tonic-gate 				error("server: too many 'E's\n");
1497c478bd9Sstevel@tonic-gate 				continue;
1507c478bd9Sstevel@tonic-gate 			}
1517c478bd9Sstevel@tonic-gate 			tp = stp[--catname];
1527c478bd9Sstevel@tonic-gate 			*tp = '\0';
1537c478bd9Sstevel@tonic-gate 			ack();
1547c478bd9Sstevel@tonic-gate 			continue;
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 		case 'C':  /* Clean. Cleanup a directory */
1577c478bd9Sstevel@tonic-gate 			clean(cp);
1587c478bd9Sstevel@tonic-gate 			continue;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		case 'Q':  /* Query. Does the file/directory exist? */
1617c478bd9Sstevel@tonic-gate 			query(cp);
1627c478bd9Sstevel@tonic-gate 			continue;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 		case 'S':  /* Special. Execute commands */
1657c478bd9Sstevel@tonic-gate 			dospecial(cp);
1667c478bd9Sstevel@tonic-gate 			continue;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate #ifdef notdef
1697c478bd9Sstevel@tonic-gate 		/*
1707c478bd9Sstevel@tonic-gate 		 * These entries are reserved but not currently used.
1717c478bd9Sstevel@tonic-gate 		 * The intent is to allow remote hosts to have master copies.
1727c478bd9Sstevel@tonic-gate 		 * Currently, only the host rdist runs on can have masters.
1737c478bd9Sstevel@tonic-gate 		 */
1747c478bd9Sstevel@tonic-gate 		case 'X':  /* start a new list of files to exclude */
1757c478bd9Sstevel@tonic-gate 			except = bp = NULL;
1767c478bd9Sstevel@tonic-gate 		case 'x':  /* add name to list of files to exclude */
1777c478bd9Sstevel@tonic-gate 			if (*cp == '\0') {
1787c478bd9Sstevel@tonic-gate 				ack();
1797c478bd9Sstevel@tonic-gate 				continue;
1807c478bd9Sstevel@tonic-gate 			}
1817c478bd9Sstevel@tonic-gate 			if (*cp == '~') {
1827c478bd9Sstevel@tonic-gate 				if (exptilde(buf, sizeof (buf), cp) == NULL)
1837c478bd9Sstevel@tonic-gate 					continue;
1847c478bd9Sstevel@tonic-gate 				cp = buf;
1857c478bd9Sstevel@tonic-gate 			}
1867c478bd9Sstevel@tonic-gate 			if (bp == NULL)
1877c478bd9Sstevel@tonic-gate 				except = bp = expand(makeblock(NAME, cp),
18856fbdff6SChandan 				    E_VARS);
1897c478bd9Sstevel@tonic-gate 			else
1907c478bd9Sstevel@tonic-gate 				bp->b_next = expand(makeblock(NAME, cp),
19156fbdff6SChandan 				    E_VARS);
1927c478bd9Sstevel@tonic-gate 			while (bp->b_next != NULL)
1937c478bd9Sstevel@tonic-gate 				bp = bp->b_next;
1947c478bd9Sstevel@tonic-gate 			ack();
1957c478bd9Sstevel@tonic-gate 			continue;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		case 'I':  /* Install. Transfer file if out of date. */
1987c478bd9Sstevel@tonic-gate 			opts = 0;
1997c478bd9Sstevel@tonic-gate 			while (*cp >= '0' && *cp <= '7')
2007c478bd9Sstevel@tonic-gate 				opts = (opts << 3) | (*cp++ - '0');
2017c478bd9Sstevel@tonic-gate 			if (*cp++ != ' ') {
2027c478bd9Sstevel@tonic-gate 				error("server: options not delimited\n");
2037c478bd9Sstevel@tonic-gate 				return;
2047c478bd9Sstevel@tonic-gate 			}
2057c478bd9Sstevel@tonic-gate 			install(cp, opts);
2067c478bd9Sstevel@tonic-gate 			continue;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 		case 'L':  /* Log. save message in log file */
2097c478bd9Sstevel@tonic-gate 			log(lfp, cp);
2107c478bd9Sstevel@tonic-gate 			continue;
2117c478bd9Sstevel@tonic-gate #endif
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 		case '\1':
2147c478bd9Sstevel@tonic-gate 			nerrs++;
2157c478bd9Sstevel@tonic-gate 			continue;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 		case '\2':
2187c478bd9Sstevel@tonic-gate 			return;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		default:
2217c478bd9Sstevel@tonic-gate 			error("server: unknown command '%s'\n", cp);
222c15ff06aSToomas Soome 			continue;
2237c478bd9Sstevel@tonic-gate 		case '\0':
2247c478bd9Sstevel@tonic-gate 			continue;
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate /*
2307c478bd9Sstevel@tonic-gate  * Update the file(s) if they are different.
2317c478bd9Sstevel@tonic-gate  * destdir = 1 if destination should be a directory
2327c478bd9Sstevel@tonic-gate  * (i.e., more than one source is being copied to the same destination).
2337c478bd9Sstevel@tonic-gate  */
234740638c8Sbw void
install(char * src,char * dest,int destdir,int opts)23556143343SToomas Soome install(char *src, char *dest, int destdir, int opts)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	char *rname;
2387c478bd9Sstevel@tonic-gate 	char destcopy[RDIST_BUFSIZ];
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (dest == NULL) {
2417c478bd9Sstevel@tonic-gate 		opts &= ~WHOLE; /* WHOLE mode only useful if renaming */
2427c478bd9Sstevel@tonic-gate 		dest = src;
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (nflag || debug) {
2467c478bd9Sstevel@tonic-gate 		printf("%s%s%s%s%s %s %s\n", opts & VERIFY ? "verify":"install",
24756143343SToomas Soome 		    opts & WHOLE ? " -w" : "",
24856143343SToomas Soome 		    opts & YOUNGER ? " -y" : "",
24956143343SToomas Soome 		    opts & COMPARE ? " -b" : "",
25056143343SToomas Soome 		    opts & REMOVE ? " -R" : "", src, dest);
2517c478bd9Sstevel@tonic-gate 		if (nflag)
2527c478bd9Sstevel@tonic-gate 			return;
2537c478bd9Sstevel@tonic-gate 	}
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 	rname = exptilde(target, sizeof (target), src);
2567c478bd9Sstevel@tonic-gate 	if (rname == NULL)
2577c478bd9Sstevel@tonic-gate 		return;
2587c478bd9Sstevel@tonic-gate 	tp = target;
2597c478bd9Sstevel@tonic-gate 	while (*tp)
2607c478bd9Sstevel@tonic-gate 		tp++;
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * If we are renaming a directory and we want to preserve
2637c478bd9Sstevel@tonic-gate 	 * the directory heirarchy (-w), we must strip off the leading
2647c478bd9Sstevel@tonic-gate 	 * directory name and preserve the rest.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	if (opts & WHOLE) {
2677c478bd9Sstevel@tonic-gate 		while (*rname == '/')
2687c478bd9Sstevel@tonic-gate 			rname++;
2697c478bd9Sstevel@tonic-gate 		destdir = 1;
2707c478bd9Sstevel@tonic-gate 	} else {
2717c478bd9Sstevel@tonic-gate 		rname = rindex(target, '/');
2727c478bd9Sstevel@tonic-gate 		if (rname == NULL)
2737c478bd9Sstevel@tonic-gate 			rname = target;
2747c478bd9Sstevel@tonic-gate 		else
2757c478bd9Sstevel@tonic-gate 			rname++;
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate 	if (debug)
2787c478bd9Sstevel@tonic-gate 		printf("target = %s, rname = %s\n", target, rname);
2797c478bd9Sstevel@tonic-gate 	/*
2807c478bd9Sstevel@tonic-gate 	 * Pass the destination file/directory name to remote.
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "%c%s\n", destdir ? 'T' : 't', dest) >=
2837c478bd9Sstevel@tonic-gate 	    sizeof (buf)) {
2847c478bd9Sstevel@tonic-gate 		error("%s: Name too long\n", dest);
2857c478bd9Sstevel@tonic-gate 		return;
2867c478bd9Sstevel@tonic-gate 	}
2877c478bd9Sstevel@tonic-gate 	if (debug)
2887c478bd9Sstevel@tonic-gate 		printf("buf = %s", buf);
2897c478bd9Sstevel@tonic-gate 	(void) deswrite(rem, buf, strlen(buf), 0);
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (response() < 0)
2927c478bd9Sstevel@tonic-gate 		return;
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	strcpy(source, src);
2957c478bd9Sstevel@tonic-gate 	if (destdir) {
2967c478bd9Sstevel@tonic-gate 		strcpy(destcopy, dest);
2977c478bd9Sstevel@tonic-gate 		Tdest = destcopy;
2987c478bd9Sstevel@tonic-gate 		strcpy(destination, rname);
2997c478bd9Sstevel@tonic-gate 	} else {
3007c478bd9Sstevel@tonic-gate 		strcpy(destination, dest);
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate 	sendf(rname, opts);
3037c478bd9Sstevel@tonic-gate 	Tdest = 0;
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate #define	protoname()	(pw ? pw->pw_name : user)
3077c478bd9Sstevel@tonic-gate #define	protogroup()	(gr ? gr->gr_name : group)
3087c478bd9Sstevel@tonic-gate /*
3097c478bd9Sstevel@tonic-gate  * Transfer the file or directory in target[].
3107c478bd9Sstevel@tonic-gate  * rname is the name of the file on the remote host.
3117c478bd9Sstevel@tonic-gate  */
312740638c8Sbw void
sendf(char * rname,int opts)31356143343SToomas Soome sendf(char *rname, int opts)
3147c478bd9Sstevel@tonic-gate {
31556143343SToomas Soome 	struct subcmd *sc;
3167c478bd9Sstevel@tonic-gate 	struct stat stb;
3177c478bd9Sstevel@tonic-gate 	int sizerr, f, u, len;
3187c478bd9Sstevel@tonic-gate 	off_t i;
3197c478bd9Sstevel@tonic-gate 	DIR *d;
3207c478bd9Sstevel@tonic-gate 	struct dirent *dp;
3217c478bd9Sstevel@tonic-gate 	char *otp, *cp;
3227c478bd9Sstevel@tonic-gate 	extern struct subcmd *subcmds;
3237c478bd9Sstevel@tonic-gate 	static char user[15], group[15];
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate 	if (debug)
3267c478bd9Sstevel@tonic-gate 		printf("sendf(%s, %x%s)\n", rname, opts, printb(opts, OBITS));
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	if (except(target))
3297c478bd9Sstevel@tonic-gate 		return;
3307c478bd9Sstevel@tonic-gate 	if ((opts & FOLLOW ? stat(target, &stb) : lstat(target, &stb)) < 0) {
3317c478bd9Sstevel@tonic-gate 		error("%s: %s\n", target, strerror(errno));
3327c478bd9Sstevel@tonic-gate 		return;
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 	if (index(rname, '\n')) {
3357c478bd9Sstevel@tonic-gate 		error("file name '%s' contains an embedded newline - "
3367c478bd9Sstevel@tonic-gate 		    "can't update\n", rname);
3377c478bd9Sstevel@tonic-gate 		return;
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate 	if ((u = update(rname, opts, &stb)) == 0) {
3407c478bd9Sstevel@tonic-gate 		if ((stb.st_mode & S_IFMT) == S_IFREG && stb.st_nlink > 1)
3417c478bd9Sstevel@tonic-gate 			(void) savelink(&stb, opts);
3427c478bd9Sstevel@tonic-gate 		return;
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	if (pw == NULL || pw->pw_uid != stb.st_uid)
3467c478bd9Sstevel@tonic-gate 		if ((pw = getpwuid(stb.st_uid)) == NULL) {
3477c478bd9Sstevel@tonic-gate 			log(lfp, "%s: no password entry for uid %d \n",
34856143343SToomas Soome 			    target, stb.st_uid);
3497c478bd9Sstevel@tonic-gate 			pw = NULL;
3507c478bd9Sstevel@tonic-gate 			sprintf(user, ":%d", stb.st_uid);
3517c478bd9Sstevel@tonic-gate 		}
3527c478bd9Sstevel@tonic-gate 	if (gr == NULL || gr->gr_gid != stb.st_gid)
3537c478bd9Sstevel@tonic-gate 		if ((gr = getgrgid(stb.st_gid)) == NULL) {
3547c478bd9Sstevel@tonic-gate 			log(lfp, "%s: no name for group %d\n",
35556143343SToomas Soome 			    target, stb.st_gid);
3567c478bd9Sstevel@tonic-gate 			gr = NULL;
3577c478bd9Sstevel@tonic-gate 			sprintf(group, ":%d", stb.st_gid);
3587c478bd9Sstevel@tonic-gate 		}
3597c478bd9Sstevel@tonic-gate 	if (u == 1) {
3607c478bd9Sstevel@tonic-gate 		if (opts & VERIFY) {
3617c478bd9Sstevel@tonic-gate 			log(lfp, "need to install: %s\n", target);
3627c478bd9Sstevel@tonic-gate 			goto dospecial;
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 		log(lfp, "installing: %s\n", target);
3657c478bd9Sstevel@tonic-gate 		opts &= ~(COMPARE|REMOVE);
3667c478bd9Sstevel@tonic-gate 	}
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 	switch (stb.st_mode & S_IFMT) {
3697c478bd9Sstevel@tonic-gate 	case S_IFDIR:
3707c478bd9Sstevel@tonic-gate 		if ((d = opendir(target)) == NULL) {
3717c478bd9Sstevel@tonic-gate 			error("%s: %s\n", target, strerror(errno));
3727c478bd9Sstevel@tonic-gate 			return;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 		if (snprintf(buf, sizeof (buf), "D%o %04o 0 0 %s %s %s\n",
3757c478bd9Sstevel@tonic-gate 		    opts, stb.st_mode & 07777, protoname(), protogroup(),
3767c478bd9Sstevel@tonic-gate 		    rname) >= sizeof (buf)) {
3777c478bd9Sstevel@tonic-gate 			error("%s: Name too long\n", rname);
3787c478bd9Sstevel@tonic-gate 			closedir(d);
3797c478bd9Sstevel@tonic-gate 			return;
3807c478bd9Sstevel@tonic-gate 		}
3817c478bd9Sstevel@tonic-gate 		if (debug)
3827c478bd9Sstevel@tonic-gate 			printf("buf = %s", buf);
3837c478bd9Sstevel@tonic-gate 		(void) deswrite(rem, buf, strlen(buf), 0);
3847c478bd9Sstevel@tonic-gate 		if (response() < 0) {
3857c478bd9Sstevel@tonic-gate 			closedir(d);
3867c478bd9Sstevel@tonic-gate 			return;
3877c478bd9Sstevel@tonic-gate 		}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 		if (opts & REMOVE)
3907c478bd9Sstevel@tonic-gate 			rmchk(opts);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 		otp = tp;
3937c478bd9Sstevel@tonic-gate 		len = tp - target;
3947c478bd9Sstevel@tonic-gate 		while (dp = readdir(d)) {
3957c478bd9Sstevel@tonic-gate 			if ((strcmp(dp->d_name, ".") == 0)||
3967c478bd9Sstevel@tonic-gate 			    (strcmp(dp->d_name, "..") == 0))
3977c478bd9Sstevel@tonic-gate 				continue;
3987c478bd9Sstevel@tonic-gate 			if ((int)(len + 1 + strlen(dp->d_name)) >=
3997c478bd9Sstevel@tonic-gate 			    (int)(RDIST_BUFSIZ - 1)) {
4007c478bd9Sstevel@tonic-gate 				error("%.*s/%s: Name too long\n", len, target,
40156143343SToomas Soome 				    dp->d_name);
4027c478bd9Sstevel@tonic-gate 				continue;
4037c478bd9Sstevel@tonic-gate 			}
4047c478bd9Sstevel@tonic-gate 			tp = otp;
4057c478bd9Sstevel@tonic-gate 			*tp++ = '/';
4067c478bd9Sstevel@tonic-gate 			cp = dp->d_name;
4077c478bd9Sstevel@tonic-gate 			while (*tp++ = *cp++)
4087c478bd9Sstevel@tonic-gate 				;
4097c478bd9Sstevel@tonic-gate 			tp--;
4107c478bd9Sstevel@tonic-gate 			sendf(dp->d_name, opts);
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 		closedir(d);
4137c478bd9Sstevel@tonic-gate 		(void) deswrite(rem, "E\n", 2, 0);
4147c478bd9Sstevel@tonic-gate 		(void) response();
4157c478bd9Sstevel@tonic-gate 		tp = otp;
4167c478bd9Sstevel@tonic-gate 		*tp = '\0';
4177c478bd9Sstevel@tonic-gate 		return;
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	case S_IFLNK:
4207c478bd9Sstevel@tonic-gate 		if (u != 1)
4217c478bd9Sstevel@tonic-gate 			opts |= COMPARE;
4227c478bd9Sstevel@tonic-gate 		if (stb.st_nlink > 1) {
4237c478bd9Sstevel@tonic-gate 			struct linkbuf *lp;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 			if ((lp = savelink(&stb, opts)) != NULL) {
4267c478bd9Sstevel@tonic-gate 				/* install link */
4277c478bd9Sstevel@tonic-gate 				if (*lp->target == 0)
4287c478bd9Sstevel@tonic-gate 					len = snprintf(buf, sizeof (buf),
4297c478bd9Sstevel@tonic-gate 					    "k%o %s %s\n", opts, lp->pathname,
4307c478bd9Sstevel@tonic-gate 					    rname);
4317c478bd9Sstevel@tonic-gate 				else
4327c478bd9Sstevel@tonic-gate 					len = snprintf(buf, sizeof (buf),
4337c478bd9Sstevel@tonic-gate 					    "k%o %s/%s %s\n", opts, lp->target,
4347c478bd9Sstevel@tonic-gate 					    lp->pathname, rname);
4357c478bd9Sstevel@tonic-gate 				if (len >= sizeof (buf)) {
4367c478bd9Sstevel@tonic-gate 					error("%s: Name too long\n", rname);
4377c478bd9Sstevel@tonic-gate 					return;
4387c478bd9Sstevel@tonic-gate 				}
4397c478bd9Sstevel@tonic-gate 				if (debug)
4407c478bd9Sstevel@tonic-gate 					printf("buf = %s", buf);
4417c478bd9Sstevel@tonic-gate 				(void) deswrite(rem, buf, strlen(buf), 0);
4427c478bd9Sstevel@tonic-gate 				(void) response();
4437c478bd9Sstevel@tonic-gate 				return;
4447c478bd9Sstevel@tonic-gate 			}
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "K%o %o %ld %ld %s %s %s\n",
4477c478bd9Sstevel@tonic-gate 		    opts, stb.st_mode & 07777, stb.st_size, stb.st_mtime,
4487c478bd9Sstevel@tonic-gate 		    protoname(), protogroup(), rname);
4497c478bd9Sstevel@tonic-gate 		if (debug)
4507c478bd9Sstevel@tonic-gate 			printf("buf = %s", buf);
4517c478bd9Sstevel@tonic-gate 		(void) deswrite(rem, buf, strlen(buf), 0);
4527c478bd9Sstevel@tonic-gate 		if (response() < 0)
4537c478bd9Sstevel@tonic-gate 			return;
4547c478bd9Sstevel@tonic-gate 		sizerr = (readlink(target, buf, RDIST_BUFSIZ) != stb.st_size);
4557c478bd9Sstevel@tonic-gate 		(void) deswrite(rem, buf, stb.st_size, 0);
4567c478bd9Sstevel@tonic-gate 		if (debug)
4577c478bd9Sstevel@tonic-gate 			printf("readlink = %.*s\n", (int)stb.st_size, buf);
4587c478bd9Sstevel@tonic-gate 		goto done;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	case S_IFREG:
4617c478bd9Sstevel@tonic-gate 		break;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	default:
4647c478bd9Sstevel@tonic-gate 		error("%s: not a file or directory\n", target);
4657c478bd9Sstevel@tonic-gate 		return;
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (u == 2) {
4697c478bd9Sstevel@tonic-gate 		if (opts & VERIFY) {
4707c478bd9Sstevel@tonic-gate 			log(lfp, "need to update: %s\n", target);
4717c478bd9Sstevel@tonic-gate 			goto dospecial;
4727c478bd9Sstevel@tonic-gate 		}
4737c478bd9Sstevel@tonic-gate 		log(lfp, "updating: %s\n", target);
4747c478bd9Sstevel@tonic-gate 	}
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	if (stb.st_nlink > 1) {
4777c478bd9Sstevel@tonic-gate 		struct linkbuf *lp;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		if ((lp = savelink(&stb, opts)) != NULL) {
4807c478bd9Sstevel@tonic-gate 			/* install link */
4817c478bd9Sstevel@tonic-gate 			if (*lp->target == 0)
4827c478bd9Sstevel@tonic-gate 				len = snprintf(buf, sizeof (buf), "k%o %s %s\n",
4837c478bd9Sstevel@tonic-gate 				    opts, lp->pathname, rname);
4847c478bd9Sstevel@tonic-gate 			else
4857c478bd9Sstevel@tonic-gate 				len = snprintf(buf, sizeof (buf),
4867c478bd9Sstevel@tonic-gate 				    "k%o %s/%s %s\n", opts, lp->target,
4877c478bd9Sstevel@tonic-gate 				    lp->pathname, rname);
4887c478bd9Sstevel@tonic-gate 			if (len >= sizeof (buf)) {
4897c478bd9Sstevel@tonic-gate 				error("%s: Name too long\n", rname);
4907c478bd9Sstevel@tonic-gate 				return;
4917c478bd9Sstevel@tonic-gate 			}
4927c478bd9Sstevel@tonic-gate 			if (debug)
4937c478bd9Sstevel@tonic-gate 				printf("buf = %s", buf);
4947c478bd9Sstevel@tonic-gate 			(void) deswrite(rem, buf, strlen(buf), 0);
4957c478bd9Sstevel@tonic-gate 			(void) response();
4967c478bd9Sstevel@tonic-gate 			return;
4977c478bd9Sstevel@tonic-gate 		}
4987c478bd9Sstevel@tonic-gate 	}
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	if ((f = open(target, 0)) < 0) {
5017c478bd9Sstevel@tonic-gate 		error("%s: %s\n", target, strerror(errno));
5027c478bd9Sstevel@tonic-gate 		return;
5037c478bd9Sstevel@tonic-gate 	}
5047c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "R%o %o %ld %ld %s %s %s\n", opts,
50556143343SToomas Soome 	    stb.st_mode & 07777, stb.st_size, stb.st_mtime,
50656143343SToomas Soome 	    protoname(), protogroup(), rname);
5077c478bd9Sstevel@tonic-gate 	if (debug)
5087c478bd9Sstevel@tonic-gate 		printf("buf = %s", buf);
5097c478bd9Sstevel@tonic-gate 	(void) deswrite(rem, buf, strlen(buf), 0);
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 	if (response() < 0) {
5127c478bd9Sstevel@tonic-gate 		(void) close(f);
5137c478bd9Sstevel@tonic-gate 		return;
5147c478bd9Sstevel@tonic-gate 	}
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate 	sizerr = 0;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	for (i = 0; i < stb.st_size; i += RDIST_BUFSIZ) {
5197c478bd9Sstevel@tonic-gate 		int amt = RDIST_BUFSIZ;
5207c478bd9Sstevel@tonic-gate 		if (i + amt > stb.st_size)
5217c478bd9Sstevel@tonic-gate 			amt = stb.st_size - i;
5227c478bd9Sstevel@tonic-gate 		if (sizerr == 0 && read(f, buf, amt) != amt)
5237c478bd9Sstevel@tonic-gate 			sizerr = 1;
5247c478bd9Sstevel@tonic-gate 		(void) deswrite(rem, buf, amt, 0);
5257c478bd9Sstevel@tonic-gate 	}
5267c478bd9Sstevel@tonic-gate 	(void) close(f);
5277c478bd9Sstevel@tonic-gate done:
5287c478bd9Sstevel@tonic-gate 	if (sizerr) {
5297c478bd9Sstevel@tonic-gate 		error("%s: file changed size\n", target);
5307c478bd9Sstevel@tonic-gate 		(void) deswrite(rem, "\1\n", 2, 0);
5317c478bd9Sstevel@tonic-gate 	} else
5327c478bd9Sstevel@tonic-gate 		(void) deswrite(rem, "\0\n", 2, 0);
5337c478bd9Sstevel@tonic-gate 	f = response();
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	if (f < 0 || f == 0 && (opts & COMPARE))
5367c478bd9Sstevel@tonic-gate 		return;
5377c478bd9Sstevel@tonic-gate dospecial:
5387c478bd9Sstevel@tonic-gate 	for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
5397c478bd9Sstevel@tonic-gate 		if (sc->sc_type != SPECIAL)
5407c478bd9Sstevel@tonic-gate 			continue;
5417c478bd9Sstevel@tonic-gate 		if (sc->sc_args != NULL && !inlist(sc->sc_args, target))
5427c478bd9Sstevel@tonic-gate 			continue;
5437c478bd9Sstevel@tonic-gate 		log(lfp, "special \"%s\"\n", sc->sc_name);
5447c478bd9Sstevel@tonic-gate 		if (opts & VERIFY)
5457c478bd9Sstevel@tonic-gate 			continue;
5467c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "SFILE=%s;%s\n", target,
5477c478bd9Sstevel@tonic-gate 		    sc->sc_name);
5487c478bd9Sstevel@tonic-gate 		if (debug)
5497c478bd9Sstevel@tonic-gate 			printf("buf = %s", buf);
5507c478bd9Sstevel@tonic-gate 		(void) deswrite(rem, buf, strlen(buf), 0);
5517c478bd9Sstevel@tonic-gate 		while (response() > 0)
5527c478bd9Sstevel@tonic-gate 			;
5537c478bd9Sstevel@tonic-gate 	}
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate struct linkbuf *
savelink(struct stat * stp,int opts)55756143343SToomas Soome savelink(struct stat *stp, int opts)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	struct linkbuf *lp;
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate 	for (lp = ihead; lp != NULL; lp = lp->nextp)
5627c478bd9Sstevel@tonic-gate 		if (lp->inum == stp->st_ino && lp->devnum == stp->st_dev) {
5637c478bd9Sstevel@tonic-gate 			lp->count--;
5647c478bd9Sstevel@tonic-gate 			return (lp);
5657c478bd9Sstevel@tonic-gate 		}
5667c478bd9Sstevel@tonic-gate 	lp = (struct linkbuf *)malloc(sizeof (*lp));
5677c478bd9Sstevel@tonic-gate 	if (lp == NULL)
5687c478bd9Sstevel@tonic-gate 		log(lfp, "out of memory, link information lost\n");
5697c478bd9Sstevel@tonic-gate 	else {
5707c478bd9Sstevel@tonic-gate 		lp->nextp = ihead;
5717c478bd9Sstevel@tonic-gate 		ihead = lp;
5727c478bd9Sstevel@tonic-gate 		lp->inum = stp->st_ino;
5737c478bd9Sstevel@tonic-gate 		lp->devnum = stp->st_dev;
5747c478bd9Sstevel@tonic-gate 		lp->count = stp->st_nlink - 1;
57556fbdff6SChandan 
57656fbdff6SChandan 		if (strlcpy(lp->pathname,
57756fbdff6SChandan 		    opts & WHOLE ? target : strsub(source, destination, target),
57856fbdff6SChandan 		    sizeof (lp->pathname)) >= sizeof (lp->pathname)) {
57956fbdff6SChandan 			error("%s: target name too long\n", target);
58056fbdff6SChandan 		}
58156fbdff6SChandan 
58256fbdff6SChandan 		if (Tdest) {
58356fbdff6SChandan 			if (strlcpy(lp->target, Tdest,
58456fbdff6SChandan 			    sizeof (lp->target)) >= sizeof (lp->target))
58556fbdff6SChandan 				error("%s: target name too long\n", Tdest);
58656fbdff6SChandan 		} else
5877c478bd9Sstevel@tonic-gate 			*lp->target = 0;
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	return (NULL);
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate /*
5937c478bd9Sstevel@tonic-gate  * Check to see if file needs to be updated on the remote machine.
5947c478bd9Sstevel@tonic-gate  * Returns 0 if no update, 1 if remote doesn't exist, 2 if out of date
5957c478bd9Sstevel@tonic-gate  * and 3 if comparing binaries to determine if out of date.
5967c478bd9Sstevel@tonic-gate  */
597740638c8Sbw int
update(char * rname,int opts,struct stat * stp)59856143343SToomas Soome update(char *rname, int opts, struct stat *stp)
5997c478bd9Sstevel@tonic-gate {
60056143343SToomas Soome 	char *cp, *s;
60156143343SToomas Soome 	off_t size;
60256143343SToomas Soome 	time_t mtime;
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate 	if (debug)
6057c478bd9Sstevel@tonic-gate 		printf("update(%s, %x%s, %x)\n", rname, opts,
60656143343SToomas Soome 		    printb(opts, OBITS), stp);
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/*
6097c478bd9Sstevel@tonic-gate 	 * Check to see if the file exists on the remote machine.
6107c478bd9Sstevel@tonic-gate 	 */
6117c478bd9Sstevel@tonic-gate 	if (snprintf(buf, sizeof (buf), "Q%s\n", rname) >= sizeof (buf)) {
6127c478bd9Sstevel@tonic-gate 		error("%s: Name too long\n", rname);
6137c478bd9Sstevel@tonic-gate 		return (0);
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 	if (debug)
6167c478bd9Sstevel@tonic-gate 		printf("buf = %s", buf);
6177c478bd9Sstevel@tonic-gate 	(void) deswrite(rem, buf, strlen(buf), 0);
6187c478bd9Sstevel@tonic-gate again:
6197c478bd9Sstevel@tonic-gate 	cp = s = buf;
6207c478bd9Sstevel@tonic-gate more:
6217c478bd9Sstevel@tonic-gate 	do {
6227c478bd9Sstevel@tonic-gate 		if (desread(rem, cp, 1, 0) != 1)
6237c478bd9Sstevel@tonic-gate 			lostconn();
6247c478bd9Sstevel@tonic-gate 	} while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if (cp <  &buf[RDIST_BUFSIZ])
6277c478bd9Sstevel@tonic-gate 		*cp = '\0';
6287c478bd9Sstevel@tonic-gate 	if (debug) {
6297c478bd9Sstevel@tonic-gate 		printf("update reply:  ");
6307c478bd9Sstevel@tonic-gate 		switch (*s) {
6317c478bd9Sstevel@tonic-gate 			case 'Y':
6327c478bd9Sstevel@tonic-gate 			case 'N':
6337c478bd9Sstevel@tonic-gate 				putchar(*s);
6347c478bd9Sstevel@tonic-gate 				break;
6357c478bd9Sstevel@tonic-gate 			default:
6367c478bd9Sstevel@tonic-gate 				if (iscntrl(*s)) {
6377c478bd9Sstevel@tonic-gate 					putchar('^');
6387c478bd9Sstevel@tonic-gate 					putchar('A' + *s - 1);
6397c478bd9Sstevel@tonic-gate 				} else
6407c478bd9Sstevel@tonic-gate 					printf("%#x", *s & 0xff);
6417c478bd9Sstevel@tonic-gate 				break;
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 		printf("%s", &s[1]);
6447c478bd9Sstevel@tonic-gate 	}
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	switch (*s++) {
6477c478bd9Sstevel@tonic-gate 	case 'Y':
6487c478bd9Sstevel@tonic-gate 		break;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	case 'N':  /* file doesn't exist so install it */
6517c478bd9Sstevel@tonic-gate 		return (1);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	case '\1':
6547c478bd9Sstevel@tonic-gate 		nerrs++;
6557c478bd9Sstevel@tonic-gate 		if (*s != '\n') {
6567c478bd9Sstevel@tonic-gate 			if (!iamremote) {
6577c478bd9Sstevel@tonic-gate 				fflush(stdout);
6587c478bd9Sstevel@tonic-gate 				(void) write(2, s, cp - s);
6597c478bd9Sstevel@tonic-gate 			}
6607c478bd9Sstevel@tonic-gate 			if (lfp != NULL)
6617c478bd9Sstevel@tonic-gate 				(void) fwrite(s, 1, cp - s, lfp);
6627c478bd9Sstevel@tonic-gate 		}
6637c478bd9Sstevel@tonic-gate 		if (cp == &buf[RDIST_BUFSIZ] && *(cp - 1) != '\n') {
6647c478bd9Sstevel@tonic-gate 			/* preserve status code */
6657c478bd9Sstevel@tonic-gate 			cp = s;
6667c478bd9Sstevel@tonic-gate 			s = buf;
6677c478bd9Sstevel@tonic-gate 			goto more;
6687c478bd9Sstevel@tonic-gate 		}
6697c478bd9Sstevel@tonic-gate 		return (0);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	case '\3':
6727c478bd9Sstevel@tonic-gate 		*--cp = '\0';
6737c478bd9Sstevel@tonic-gate 		if (lfp != NULL)
6747c478bd9Sstevel@tonic-gate 			log(lfp, "update: note: %s\n", s);
6757c478bd9Sstevel@tonic-gate 		goto again;
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	default:
6787c478bd9Sstevel@tonic-gate 		*--cp = '\0';
6797c478bd9Sstevel@tonic-gate 		error("update: unexpected response '%s'\n", s);
6807c478bd9Sstevel@tonic-gate 		return (0);
6817c478bd9Sstevel@tonic-gate 	}
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	if (*s == '\n')
6847c478bd9Sstevel@tonic-gate 		return (2);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	if (opts & COMPARE)
6877c478bd9Sstevel@tonic-gate 		return (3);
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	size = 0;
6907c478bd9Sstevel@tonic-gate 	while (isdigit(*s))
6917c478bd9Sstevel@tonic-gate 		size = size * 10 + (*s++ - '0');
6927c478bd9Sstevel@tonic-gate 	if (*s++ != ' ') {
6937c478bd9Sstevel@tonic-gate 		error("update: size not delimited\n");
6947c478bd9Sstevel@tonic-gate 		return (0);
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 	mtime = 0;
6977c478bd9Sstevel@tonic-gate 	while (isdigit(*s))
6987c478bd9Sstevel@tonic-gate 		mtime = mtime * 10 + (*s++ - '0');
6997c478bd9Sstevel@tonic-gate 	if (*s != '\n') {
7007c478bd9Sstevel@tonic-gate 		error("update: mtime not delimited\n");
7017c478bd9Sstevel@tonic-gate 		return (0);
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * File needs to be updated?
7057c478bd9Sstevel@tonic-gate 	 */
7067c478bd9Sstevel@tonic-gate 	if (opts & YOUNGER) {
7077c478bd9Sstevel@tonic-gate 		if (stp->st_mtime == mtime)
7087c478bd9Sstevel@tonic-gate 			return (0);
7097c478bd9Sstevel@tonic-gate 		if (stp->st_mtime < mtime) {
7107c478bd9Sstevel@tonic-gate 			log(lfp, "Warning: %s: remote copy is newer\n", target);
7117c478bd9Sstevel@tonic-gate 			return (0);
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 	} else if (stp->st_mtime == mtime && stp->st_size == size)
7147c478bd9Sstevel@tonic-gate 		return (0);
7157c478bd9Sstevel@tonic-gate 	return (2);
7167c478bd9Sstevel@tonic-gate }
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate /*
7197c478bd9Sstevel@tonic-gate  * Query. Check to see if file exists. Return one of the following:
7207c478bd9Sstevel@tonic-gate  *	N\n		- doesn't exist
7217c478bd9Sstevel@tonic-gate  *	Ysize mtime\n	- exists and its a regular file (size & mtime of file)
7227c478bd9Sstevel@tonic-gate  *	Y\n		- exists and its a directory or symbolic link
7237c478bd9Sstevel@tonic-gate  *	^Aerror message\n
7247c478bd9Sstevel@tonic-gate  */
725740638c8Sbw static void
query(char * name)72656143343SToomas Soome query(char *name)
7277c478bd9Sstevel@tonic-gate {
7287c478bd9Sstevel@tonic-gate 	struct stat stb;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	if (catname) {
7317c478bd9Sstevel@tonic-gate 		if (sizeof (target) - (tp - target) >= strlen(name) + 2) {
7327c478bd9Sstevel@tonic-gate 			(void) sprintf(tp, "/%s", name);
7337c478bd9Sstevel@tonic-gate 		} else {
7347c478bd9Sstevel@tonic-gate 			error("%.*s/%s: Name too long\n", tp - target,
7357c478bd9Sstevel@tonic-gate 			    target, name);
7367c478bd9Sstevel@tonic-gate 			return;
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	if (lstat(target, &stb) < 0) {
7417c478bd9Sstevel@tonic-gate 		if (errno == ENOENT)
7427c478bd9Sstevel@tonic-gate 			(void) write(wrem, "N\n", 2);
7437c478bd9Sstevel@tonic-gate 		else
7447c478bd9Sstevel@tonic-gate 			error("%s:%s: %s\n", host, target, strerror(errno));
7457c478bd9Sstevel@tonic-gate 		*tp = '\0';
7467c478bd9Sstevel@tonic-gate 		return;
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 
7497c478bd9Sstevel@tonic-gate 	switch (stb.st_mode & S_IFMT) {
7507c478bd9Sstevel@tonic-gate 	case S_IFREG:
7517c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "Y%ld %ld\n", stb.st_size, stb.st_mtime);
7527c478bd9Sstevel@tonic-gate 		(void) write(wrem, buf, strlen(buf));
7537c478bd9Sstevel@tonic-gate 		break;
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate 	case S_IFLNK:
7567c478bd9Sstevel@tonic-gate 	case S_IFDIR:
7577c478bd9Sstevel@tonic-gate 		(void) write(wrem, "Y\n", 2);
7587c478bd9Sstevel@tonic-gate 		break;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	default:
7617c478bd9Sstevel@tonic-gate 		error("%s: not a file or directory\n", name);
7627c478bd9Sstevel@tonic-gate 		break;
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	*tp = '\0';
7657c478bd9Sstevel@tonic-gate }
7667c478bd9Sstevel@tonic-gate 
767740638c8Sbw static void
recvf(char * cmd,int type)76856143343SToomas Soome recvf(char *cmd, int type)
7697c478bd9Sstevel@tonic-gate {
77056143343SToomas Soome 	char *cp;
7717c478bd9Sstevel@tonic-gate 	int f, mode, opts, wrerr, olderrno;
7727c478bd9Sstevel@tonic-gate 	off_t i, size;
7737c478bd9Sstevel@tonic-gate 	time_t mtime;
7747c478bd9Sstevel@tonic-gate 	struct stat stb;
7757c478bd9Sstevel@tonic-gate 	struct timeval tvp[2];
7767c478bd9Sstevel@tonic-gate 	char *owner, *group;
7777c478bd9Sstevel@tonic-gate 	char new[RDIST_BUFSIZ];
7787c478bd9Sstevel@tonic-gate 	extern char *tmpname;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	cp = cmd;
7817c478bd9Sstevel@tonic-gate 	opts = 0;
7827c478bd9Sstevel@tonic-gate 	while (*cp >= '0' && *cp <= '7')
7837c478bd9Sstevel@tonic-gate 		opts = (opts << 3) | (*cp++ - '0');
7847c478bd9Sstevel@tonic-gate 	if (*cp++ != ' ') {
7857c478bd9Sstevel@tonic-gate 		error("recvf: options not delimited\n");
7867c478bd9Sstevel@tonic-gate 		return;
7877c478bd9Sstevel@tonic-gate 	}
7887c478bd9Sstevel@tonic-gate 	mode = 0;
7897c478bd9Sstevel@tonic-gate 	while (*cp >= '0' && *cp <= '7')
7907c478bd9Sstevel@tonic-gate 		mode = (mode << 3) | (*cp++ - '0');
7917c478bd9Sstevel@tonic-gate 	if (*cp++ != ' ') {
7927c478bd9Sstevel@tonic-gate 		error("recvf: mode not delimited\n");
7937c478bd9Sstevel@tonic-gate 		return;
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 	size = 0;
7967c478bd9Sstevel@tonic-gate 	while (isdigit(*cp))
7977c478bd9Sstevel@tonic-gate 		size = size * 10 + (*cp++ - '0');
7987c478bd9Sstevel@tonic-gate 	if (*cp++ != ' ') {
7997c478bd9Sstevel@tonic-gate 		error("recvf: size not delimited\n");
8007c478bd9Sstevel@tonic-gate 		return;
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 	mtime = 0;
8037c478bd9Sstevel@tonic-gate 	while (isdigit(*cp))
8047c478bd9Sstevel@tonic-gate 		mtime = mtime * 10 + (*cp++ - '0');
8057c478bd9Sstevel@tonic-gate 	if (*cp++ != ' ') {
8067c478bd9Sstevel@tonic-gate 		error("recvf: mtime not delimited\n");
8077c478bd9Sstevel@tonic-gate 		return;
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate 	owner = cp;
8107c478bd9Sstevel@tonic-gate 	while (*cp && *cp != ' ')
8117c478bd9Sstevel@tonic-gate 		cp++;
8127c478bd9Sstevel@tonic-gate 	if (*cp != ' ') {
8137c478bd9Sstevel@tonic-gate 		error("recvf: owner name not delimited\n");
8147c478bd9Sstevel@tonic-gate 		return;
8157c478bd9Sstevel@tonic-gate 	}
8167c478bd9Sstevel@tonic-gate 	*cp++ = '\0';
8177c478bd9Sstevel@tonic-gate 	group = cp;
8187c478bd9Sstevel@tonic-gate 	while (*cp && *cp != ' ')
8197c478bd9Sstevel@tonic-gate 		cp++;
8207c478bd9Sstevel@tonic-gate 	if (*cp != ' ') {
8217c478bd9Sstevel@tonic-gate 		error("recvf: group name not delimited\n");
8227c478bd9Sstevel@tonic-gate 		return;
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 	*cp++ = '\0';
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	if (type == S_IFDIR) {
8277c478bd9Sstevel@tonic-gate 		int	isdot;
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 		if (strcmp(cp, ".") == 0)
8307c478bd9Sstevel@tonic-gate 			isdot = 1;
8317c478bd9Sstevel@tonic-gate 		else
8327c478bd9Sstevel@tonic-gate 			isdot = 0;
8337c478bd9Sstevel@tonic-gate 		if (catname >= sizeof (stp) / sizeof (stp[0])) {
8347c478bd9Sstevel@tonic-gate 			error("%s:%s: too many directory levels\n",
83556143343SToomas Soome 			    host, target);
8367c478bd9Sstevel@tonic-gate 			return;
8377c478bd9Sstevel@tonic-gate 		}
8387c478bd9Sstevel@tonic-gate 		stp[catname] = tp;
8397c478bd9Sstevel@tonic-gate 		if (catname++) {
8407c478bd9Sstevel@tonic-gate 			*tp++ = '/';
8417c478bd9Sstevel@tonic-gate 			while (*tp++ = *cp++)
8427c478bd9Sstevel@tonic-gate 				;
8437c478bd9Sstevel@tonic-gate 			tp--;
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 		if (opts & VERIFY) {
8467c478bd9Sstevel@tonic-gate 			ack();
8477c478bd9Sstevel@tonic-gate 			return;
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 		if (lstat(target, &stb) == 0) {
8507c478bd9Sstevel@tonic-gate 			if (ISDIR(stb.st_mode)) {
8517c478bd9Sstevel@tonic-gate 				if ((stb.st_mode & 07777) == mode) {
8527c478bd9Sstevel@tonic-gate 					ack();
8537c478bd9Sstevel@tonic-gate 					return;
8547c478bd9Sstevel@tonic-gate 				}
8557c478bd9Sstevel@tonic-gate 				sendrem("%s: Warning: remote mode %o != "
8567c478bd9Sstevel@tonic-gate 				    "local mode %o", target,
8577c478bd9Sstevel@tonic-gate 				    stb.st_mode & 07777, mode);
8587c478bd9Sstevel@tonic-gate 				return;
8597c478bd9Sstevel@tonic-gate 			}
8607c478bd9Sstevel@tonic-gate 			errno = ENOTDIR;
8617c478bd9Sstevel@tonic-gate 		} else if (errno == ENOENT && (mkdir(target, mode) == 0 ||
8627c478bd9Sstevel@tonic-gate 		    chkparent(target) == 0 &&
8637c478bd9Sstevel@tonic-gate 		    (isdot == 1 || mkdir(target, mode) == 0))) {
8647c478bd9Sstevel@tonic-gate 			if (chog(target, owner, group, mode) == 0)
8657c478bd9Sstevel@tonic-gate 				ack();
8667c478bd9Sstevel@tonic-gate 			return;
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 		error("%s:%s: %s\n", host, target, strerror(errno));
8697c478bd9Sstevel@tonic-gate 		tp = stp[--catname];
8707c478bd9Sstevel@tonic-gate 		*tp = '\0';
8717c478bd9Sstevel@tonic-gate 		return;
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	if (catname) {
8757c478bd9Sstevel@tonic-gate 		if (sizeof (target) - (tp - target) >= strlen(cp) + 2) {
8767c478bd9Sstevel@tonic-gate 			(void) sprintf(tp, "/%s", cp);
8777c478bd9Sstevel@tonic-gate 		} else {
8787c478bd9Sstevel@tonic-gate 			error("%.*s/%s: Name too long\n", tp - target,
8797c478bd9Sstevel@tonic-gate 			    target, cp);
8807c478bd9Sstevel@tonic-gate 			return;
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate 	cp = rindex(target, '/');
8847c478bd9Sstevel@tonic-gate 	if (cp == NULL)
8857c478bd9Sstevel@tonic-gate 		strcpy(new, tmpname);
8867c478bd9Sstevel@tonic-gate 	else if (cp == target)
8877c478bd9Sstevel@tonic-gate 		(void) sprintf(new, "/%s", tmpname);
8887c478bd9Sstevel@tonic-gate 	else {
8897c478bd9Sstevel@tonic-gate 		*cp = '\0';
89056fbdff6SChandan 
89156fbdff6SChandan 		/*
89256fbdff6SChandan 		 * sizeof (target) =  RDIST_BUFSIZ and sizeof (tmpname) = 11
89356fbdff6SChandan 		 * RDIST_BUFSIZ = 50*1024 is much greater than PATH_MAX that is
89456fbdff6SChandan 		 * allowed by the kernel, so it's safe to call snprintf() here
89556fbdff6SChandan 		 */
89656fbdff6SChandan 		(void) snprintf(new, sizeof (new), "%s/%s", target, tmpname);
8977c478bd9Sstevel@tonic-gate 		*cp = '/';
8987c478bd9Sstevel@tonic-gate 	}
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 	if (type == S_IFLNK) {
9017c478bd9Sstevel@tonic-gate 		int j;
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate 		ack();
9047c478bd9Sstevel@tonic-gate 		cp = buf;
9057c478bd9Sstevel@tonic-gate 		for (i = 0; i < size; i += j) {
9067c478bd9Sstevel@tonic-gate 			if ((j = read(rem, cp, size - i)) <= 0)
90756143343SToomas Soome 				cleanup(0);
9087c478bd9Sstevel@tonic-gate 			cp += j;
9097c478bd9Sstevel@tonic-gate 		}
9107c478bd9Sstevel@tonic-gate 		*cp = '\0';
9117c478bd9Sstevel@tonic-gate 		if (response() < 0) {
9127c478bd9Sstevel@tonic-gate 			err();
9137c478bd9Sstevel@tonic-gate 			return;
9147c478bd9Sstevel@tonic-gate 		}
9157c478bd9Sstevel@tonic-gate 		if (symlink(buf, new) < 0) {
9167c478bd9Sstevel@tonic-gate 			if (errno != ENOENT || chkparent(new) < 0 ||
9177c478bd9Sstevel@tonic-gate 			    symlink(buf, new) < 0)
9187c478bd9Sstevel@tonic-gate 				goto badn;
9197c478bd9Sstevel@tonic-gate 		}
9207c478bd9Sstevel@tonic-gate 		mode &= 0777;
9217c478bd9Sstevel@tonic-gate 		if (opts & COMPARE) {
9227c478bd9Sstevel@tonic-gate 			char tbuf[MAXPATHLEN];
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 			if ((i = readlink(target, tbuf, MAXPATHLEN)) >= 0 &&
9257c478bd9Sstevel@tonic-gate 			    i == size && strncmp(buf, tbuf, size) == 0) {
9267c478bd9Sstevel@tonic-gate 				(void) unlink(new);
9277c478bd9Sstevel@tonic-gate 				ack();
9287c478bd9Sstevel@tonic-gate 				return;
9297c478bd9Sstevel@tonic-gate 			}
9307c478bd9Sstevel@tonic-gate 			if (opts & VERIFY)
9317c478bd9Sstevel@tonic-gate 				goto differ;
9327c478bd9Sstevel@tonic-gate 		}
9337c478bd9Sstevel@tonic-gate 		goto fixup;
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	if ((f = creat(new, mode & ~06000)) < 0) {
9377c478bd9Sstevel@tonic-gate 		if (errno != ENOENT || chkparent(new) < 0 ||
9387c478bd9Sstevel@tonic-gate 		    (f = creat(new, mode & ~06000)) < 0)
9397c478bd9Sstevel@tonic-gate 			goto badn;
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	ack();
9437c478bd9Sstevel@tonic-gate 	wrerr = 0;
9447c478bd9Sstevel@tonic-gate 	for (i = 0; i < size; i += RDIST_BUFSIZ) {
9457c478bd9Sstevel@tonic-gate 		int amt = RDIST_BUFSIZ;
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 		cp = buf;
9487c478bd9Sstevel@tonic-gate 		if (i + amt > size)
9497c478bd9Sstevel@tonic-gate 			amt = size - i;
9507c478bd9Sstevel@tonic-gate 		do {
9517c478bd9Sstevel@tonic-gate 			int j = read(rem, cp, amt);
9527c478bd9Sstevel@tonic-gate 			if (j <= 0) {
9537c478bd9Sstevel@tonic-gate 				(void) close(f);
9547c478bd9Sstevel@tonic-gate 				(void) unlink(new);
95556143343SToomas Soome 				cleanup(0);
9567c478bd9Sstevel@tonic-gate 			}
9577c478bd9Sstevel@tonic-gate 			amt -= j;
9587c478bd9Sstevel@tonic-gate 			cp += j;
9597c478bd9Sstevel@tonic-gate 		} while (amt > 0);
9607c478bd9Sstevel@tonic-gate 		amt = RDIST_BUFSIZ;
9617c478bd9Sstevel@tonic-gate 		if (i + amt > size)
9627c478bd9Sstevel@tonic-gate 			amt = size - i;
9637c478bd9Sstevel@tonic-gate 		if (wrerr == 0 && write(f, buf, amt) != amt) {
9647c478bd9Sstevel@tonic-gate 			olderrno = errno;
9657c478bd9Sstevel@tonic-gate 			wrerr++;
9667c478bd9Sstevel@tonic-gate 		}
9677c478bd9Sstevel@tonic-gate 	}
9687c478bd9Sstevel@tonic-gate 	(void) close(f);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	if (response() < 0) {
9717c478bd9Sstevel@tonic-gate 		err();
9727c478bd9Sstevel@tonic-gate 		(void) unlink(new);
9737c478bd9Sstevel@tonic-gate 		return;
9747c478bd9Sstevel@tonic-gate 	}
9757c478bd9Sstevel@tonic-gate 	if (wrerr) {
9767c478bd9Sstevel@tonic-gate 		error("%s:%s: %s\n", host, new, strerror(olderrno));
9777c478bd9Sstevel@tonic-gate 		(void) unlink(new);
9787c478bd9Sstevel@tonic-gate 		return;
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 	if (opts & COMPARE) {
9817c478bd9Sstevel@tonic-gate 		FILE *f1, *f2;
9827c478bd9Sstevel@tonic-gate 		int c;
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate 		if ((f1 = fopen(target, "r")) == NULL)
9857c478bd9Sstevel@tonic-gate 			goto badt;
9867c478bd9Sstevel@tonic-gate 		if ((f2 = fopen(new, "r")) == NULL) {
9877c478bd9Sstevel@tonic-gate 		badn:
9887c478bd9Sstevel@tonic-gate 			error("%s:%s: %s\n", host, new, strerror(errno));
9897c478bd9Sstevel@tonic-gate 			(void) unlink(new);
9907c478bd9Sstevel@tonic-gate 			return;
9917c478bd9Sstevel@tonic-gate 		}
9927c478bd9Sstevel@tonic-gate 		while ((c = getc(f1)) == getc(f2))
9937c478bd9Sstevel@tonic-gate 			if (c == EOF) {
9947c478bd9Sstevel@tonic-gate 				(void) fclose(f1);
9957c478bd9Sstevel@tonic-gate 				(void) fclose(f2);
9967c478bd9Sstevel@tonic-gate 				(void) unlink(new);
9977c478bd9Sstevel@tonic-gate 				ack();
9987c478bd9Sstevel@tonic-gate 				return;
9997c478bd9Sstevel@tonic-gate 			}
10007c478bd9Sstevel@tonic-gate 		(void) fclose(f1);
10017c478bd9Sstevel@tonic-gate 		(void) fclose(f2);
10027c478bd9Sstevel@tonic-gate 		if (opts & VERIFY) {
10037c478bd9Sstevel@tonic-gate 		differ:
10047c478bd9Sstevel@tonic-gate 			(void) unlink(new);
10057c478bd9Sstevel@tonic-gate 			sendrem("need to update: %s", target);
10067c478bd9Sstevel@tonic-gate 			return;
10077c478bd9Sstevel@tonic-gate 		}
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate 	/*
10117c478bd9Sstevel@tonic-gate 	 * Set last modified time.  For type == S_IFDIR, the lstat above filled
10127c478bd9Sstevel@tonic-gate 	 * in stb.  Otherwise, do it now.
10137c478bd9Sstevel@tonic-gate 	 */
10147c478bd9Sstevel@tonic-gate 	if (type != S_IFDIR)
10157c478bd9Sstevel@tonic-gate 		(void) lstat(new, &stb);
10167c478bd9Sstevel@tonic-gate 	tvp[0].tv_sec = stb.st_atime;	/* old atime from target */
10177c478bd9Sstevel@tonic-gate 	tvp[0].tv_usec = 0;
10187c478bd9Sstevel@tonic-gate 	tvp[1].tv_sec = mtime;
10197c478bd9Sstevel@tonic-gate 	tvp[1].tv_usec = 0;
10207c478bd9Sstevel@tonic-gate 	if (utimes(new, tvp) < 0) {
10217c478bd9Sstevel@tonic-gate 		note("%s:utimes failed %s: %s", host, new, strerror(errno));
10227c478bd9Sstevel@tonic-gate 	}
10237c478bd9Sstevel@tonic-gate 	if (chog(new, owner, group, mode) < 0) {
10247c478bd9Sstevel@tonic-gate 		(void) unlink(new);
10257c478bd9Sstevel@tonic-gate 		return;
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate fixup:
10287c478bd9Sstevel@tonic-gate 	if (rename(new, target) < 0) {
10297c478bd9Sstevel@tonic-gate badt:
10307c478bd9Sstevel@tonic-gate 		error("%s:%s: %s\n", host, target, strerror(errno));
10317c478bd9Sstevel@tonic-gate 		(void) unlink(new);
10327c478bd9Sstevel@tonic-gate 		return;
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 	if (opts & COMPARE) {
10357c478bd9Sstevel@tonic-gate 		sendrem("updated %s", target);
10367c478bd9Sstevel@tonic-gate 	} else
10377c478bd9Sstevel@tonic-gate 		ack();
10387c478bd9Sstevel@tonic-gate }
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate /*
10417c478bd9Sstevel@tonic-gate  * Creat a hard link to existing file.
10427c478bd9Sstevel@tonic-gate  */
1043740638c8Sbw static void
hardlink(char * cmd)104456143343SToomas Soome hardlink(char *cmd)
10457c478bd9Sstevel@tonic-gate {
104656143343SToomas Soome 	char *cp;
10477c478bd9Sstevel@tonic-gate 	struct stat stb;
10487c478bd9Sstevel@tonic-gate 	char *oldname;
10497c478bd9Sstevel@tonic-gate 	int opts, exists = 0;
10507c478bd9Sstevel@tonic-gate 	char oldnamebuf[RDIST_BUFSIZ];
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	cp = cmd;
10537c478bd9Sstevel@tonic-gate 	opts = 0;
10547c478bd9Sstevel@tonic-gate 	while (*cp >= '0' && *cp <= '7')
10557c478bd9Sstevel@tonic-gate 		opts = (opts << 3) | (*cp++ - '0');
10567c478bd9Sstevel@tonic-gate 	if (*cp++ != ' ') {
10577c478bd9Sstevel@tonic-gate 		error("hardlink: options not delimited\n");
10587c478bd9Sstevel@tonic-gate 		return;
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 	oldname = cp;
10617c478bd9Sstevel@tonic-gate 	while (*cp && *cp != ' ')
10627c478bd9Sstevel@tonic-gate 		cp++;
10637c478bd9Sstevel@tonic-gate 	if (*cp != ' ') {
10647c478bd9Sstevel@tonic-gate 		error("hardlink: oldname name not delimited\n");
10657c478bd9Sstevel@tonic-gate 		return;
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 	*cp++ = '\0';
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	if (catname) {
10707c478bd9Sstevel@tonic-gate 		if (sizeof (target) - (tp - target) >= strlen(cp) + 2) {
10717c478bd9Sstevel@tonic-gate 			(void) sprintf(tp, "/%s", cp);
10727c478bd9Sstevel@tonic-gate 		} else {
10737c478bd9Sstevel@tonic-gate 			error("%.*s/%s: Name too long\n", tp - target,
10747c478bd9Sstevel@tonic-gate 			    target, cp);
10757c478bd9Sstevel@tonic-gate 			return;
10767c478bd9Sstevel@tonic-gate 		}
10777c478bd9Sstevel@tonic-gate 	}
10787c478bd9Sstevel@tonic-gate 	if (lstat(target, &stb) == 0) {
10797c478bd9Sstevel@tonic-gate 		int mode = stb.st_mode & S_IFMT;
10807c478bd9Sstevel@tonic-gate 		if (mode != S_IFREG && mode != S_IFLNK) {
10817c478bd9Sstevel@tonic-gate 			error("%s:%s: not a regular file\n", host, target);
10827c478bd9Sstevel@tonic-gate 			return;
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 		exists = 1;
10857c478bd9Sstevel@tonic-gate 	}
10867c478bd9Sstevel@tonic-gate 	if (chkparent(target) < 0) {
10877c478bd9Sstevel@tonic-gate 		error("%s:%s: %s (no parent)\n",
108856143343SToomas Soome 		    host, target, strerror(errno));
10897c478bd9Sstevel@tonic-gate 		return;
10907c478bd9Sstevel@tonic-gate 	}
10917c478bd9Sstevel@tonic-gate 	if (opts & VERIFY) {
10927c478bd9Sstevel@tonic-gate 		struct stat nstb;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 		if (exists && lstat(oldname, &nstb) == 0 &&
10957c478bd9Sstevel@tonic-gate 		    nstb.st_mode == stb.st_mode &&
10967c478bd9Sstevel@tonic-gate 		    nstb.st_ino == stb.st_ino &&
10977c478bd9Sstevel@tonic-gate 		    nstb.st_dev == stb.st_dev) {
10987c478bd9Sstevel@tonic-gate 			ack();
10997c478bd9Sstevel@tonic-gate 			return;
11007c478bd9Sstevel@tonic-gate 		} else {
11017c478bd9Sstevel@tonic-gate 			sendrem("need to update: %s", target);
11027c478bd9Sstevel@tonic-gate 			return;
11037c478bd9Sstevel@tonic-gate 		}
11047c478bd9Sstevel@tonic-gate 	}
11057c478bd9Sstevel@tonic-gate 	if (exists && (unlink(target) < 0)) {
11067c478bd9Sstevel@tonic-gate 		error("%s:%s: %s (unlink)\n",
110756143343SToomas Soome 		    host, target, strerror(errno));
11087c478bd9Sstevel@tonic-gate 		return;
11097c478bd9Sstevel@tonic-gate 	}
11107c478bd9Sstevel@tonic-gate 	if (*oldname == '~')
11117c478bd9Sstevel@tonic-gate 		oldname = exptilde(oldnamebuf, sizeof (oldnamebuf), oldname);
11127c478bd9Sstevel@tonic-gate 	if (link(oldname, target) < 0) {
11137c478bd9Sstevel@tonic-gate 		error("%s:can't link %s to %s\n",
111456143343SToomas Soome 		    host, target, oldname);
11157c478bd9Sstevel@tonic-gate 		return;
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 	ack();
11187c478bd9Sstevel@tonic-gate }
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate /*
11217c478bd9Sstevel@tonic-gate  * Check to see if parent directory exists and create one if not.
11227c478bd9Sstevel@tonic-gate  */
1123740638c8Sbw int
chkparent(char * name)112456143343SToomas Soome chkparent(char *name)
11257c478bd9Sstevel@tonic-gate {
112656143343SToomas Soome 	char *cp;
11277c478bd9Sstevel@tonic-gate 	struct stat stb;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	cp = rindex(name, '/');
11307c478bd9Sstevel@tonic-gate 	if (cp == NULL || cp == name)
11317c478bd9Sstevel@tonic-gate 		return (0);
11327c478bd9Sstevel@tonic-gate 	*cp = '\0';
11337c478bd9Sstevel@tonic-gate 	if (lstat(name, &stb) < 0) {
11347c478bd9Sstevel@tonic-gate 		if (errno == ENOENT && chkparent(name) >= 0 &&
11357c478bd9Sstevel@tonic-gate 		    mkdir(name, 0777 & ~oumask) >= 0) {
11367c478bd9Sstevel@tonic-gate 			*cp = '/';
11377c478bd9Sstevel@tonic-gate 			return (0);
11387c478bd9Sstevel@tonic-gate 		}
11397c478bd9Sstevel@tonic-gate 	} else if (ISDIR(stb.st_mode)) {
11407c478bd9Sstevel@tonic-gate 		*cp = '/';
11417c478bd9Sstevel@tonic-gate 		return (0);
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 	*cp = '/';
11447c478bd9Sstevel@tonic-gate 	return (-1);
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate /*
11487c478bd9Sstevel@tonic-gate  * Change owner, group and mode of file.
11497c478bd9Sstevel@tonic-gate  */
1150740638c8Sbw int
chog(char * file,char * owner,char * group,int mode)115156143343SToomas Soome chog(char *file, char *owner, char *group, int mode)
11527c478bd9Sstevel@tonic-gate {
115356143343SToomas Soome 	int i;
11547c478bd9Sstevel@tonic-gate 	uid_t uid, gid;
11557c478bd9Sstevel@tonic-gate 	extern char user[];
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	/*
11587c478bd9Sstevel@tonic-gate 	 * by default, set uid of file to the uid of the person running
11597c478bd9Sstevel@tonic-gate 	 * this program.
11607c478bd9Sstevel@tonic-gate 	 */
11617c478bd9Sstevel@tonic-gate 	uid = getuid();
11627c478bd9Sstevel@tonic-gate 
11637c478bd9Sstevel@tonic-gate 	/*
11647c478bd9Sstevel@tonic-gate 	 * We'll use available privileges so we just try to do what
11657c478bd9Sstevel@tonic-gate 	 * the client specifies.  If the chown() fails we'll not
11667c478bd9Sstevel@tonic-gate 	 * add the set-[ug]id bits; and if we want to add the set-[ug]id
11677c478bd9Sstevel@tonic-gate 	 * bits and we're not permitted to do so, the OS will prevent us
11687c478bd9Sstevel@tonic-gate 	 * from doing so.
11697c478bd9Sstevel@tonic-gate 	 */
11707c478bd9Sstevel@tonic-gate 	if (*owner == ':') {
11717c478bd9Sstevel@tonic-gate 		uid = atoi(owner + 1);
11727c478bd9Sstevel@tonic-gate 	} else if (pw == NULL || strcmp(owner, pw->pw_name) != 0) {
11737c478bd9Sstevel@tonic-gate 		if ((pw = getpwnam(owner)) == NULL) {
11747c478bd9Sstevel@tonic-gate 			if (mode & 04000) {
11757c478bd9Sstevel@tonic-gate 				note("%s:%s: unknown login name, "
11767c478bd9Sstevel@tonic-gate 				    "clearing setuid", host, owner);
11777c478bd9Sstevel@tonic-gate 				mode &= ~04000;
11787c478bd9Sstevel@tonic-gate 			}
11797c478bd9Sstevel@tonic-gate 		} else {
11807c478bd9Sstevel@tonic-gate 			uid = pw->pw_uid;
11817c478bd9Sstevel@tonic-gate 		}
11827c478bd9Sstevel@tonic-gate 	} else {
11837c478bd9Sstevel@tonic-gate 		uid = pw->pw_uid;
11847c478bd9Sstevel@tonic-gate 	}
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	if (*group == ':') {
11877c478bd9Sstevel@tonic-gate 		gid = atoi(group + 1);
11887c478bd9Sstevel@tonic-gate 		goto ok;
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 
11917c478bd9Sstevel@tonic-gate 	gid = -1;
11927c478bd9Sstevel@tonic-gate 	if (gr == NULL || strcmp(group, gr->gr_name) != 0) {
11937c478bd9Sstevel@tonic-gate 		if ((*group == ':' &&
11947c478bd9Sstevel@tonic-gate 		    (getgrgid(gid = atoi(group + 1)) == NULL)) ||
11957c478bd9Sstevel@tonic-gate 		    ((gr = getgrnam(group)) == NULL)) {
11967c478bd9Sstevel@tonic-gate 			if (mode & 02000) {
11977c478bd9Sstevel@tonic-gate 				note("%s:%s: unknown group", host, group);
11987c478bd9Sstevel@tonic-gate 				mode &= ~02000;
11997c478bd9Sstevel@tonic-gate 			}
12007c478bd9Sstevel@tonic-gate 		} else
12017c478bd9Sstevel@tonic-gate 			gid = gr->gr_gid;
12027c478bd9Sstevel@tonic-gate 	} else
12037c478bd9Sstevel@tonic-gate 		gid = gr->gr_gid;
12047c478bd9Sstevel@tonic-gate ok:
12057c478bd9Sstevel@tonic-gate 	if (chown(file, uid, gid) < 0 ||
12067c478bd9Sstevel@tonic-gate 	    (mode & 07000) && chmod(file, mode) < 0) {
12077c478bd9Sstevel@tonic-gate 		note("%s: chown or chmod failed: file %s:  %s",
12087c478bd9Sstevel@tonic-gate 		    host, file, strerror(errno));
12097c478bd9Sstevel@tonic-gate 	}
12107c478bd9Sstevel@tonic-gate 	return (0);
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate /*
12147c478bd9Sstevel@tonic-gate  * Check for files on the machine being updated that are not on the master
12157c478bd9Sstevel@tonic-gate  * machine and remove them.
12167c478bd9Sstevel@tonic-gate  */
1217740638c8Sbw static void
rmchk(int opts)121856143343SToomas Soome rmchk(int opts)
12197c478bd9Sstevel@tonic-gate {
122056143343SToomas Soome 	char *cp, *s;
12217c478bd9Sstevel@tonic-gate 	struct stat stb;
12227c478bd9Sstevel@tonic-gate 
12237c478bd9Sstevel@tonic-gate 	if (debug)
12247c478bd9Sstevel@tonic-gate 		printf("rmchk()\n");
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	/*
12277c478bd9Sstevel@tonic-gate 	 * Tell the remote to clean the files from the last directory sent.
12287c478bd9Sstevel@tonic-gate 	 */
12297c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "C%o\n", opts & VERIFY);
12307c478bd9Sstevel@tonic-gate 	if (debug)
12317c478bd9Sstevel@tonic-gate 		printf("buf = %s", buf);
12327c478bd9Sstevel@tonic-gate 	(void) deswrite(rem, buf, strlen(buf), 0);
12337c478bd9Sstevel@tonic-gate 	if (response() < 0)
12347c478bd9Sstevel@tonic-gate 		return;
12357c478bd9Sstevel@tonic-gate 	for (;;) {
12367c478bd9Sstevel@tonic-gate 		cp = s = buf;
12377c478bd9Sstevel@tonic-gate 		do {
12387c478bd9Sstevel@tonic-gate 			if (desread(rem, cp, 1, 0) != 1)
12397c478bd9Sstevel@tonic-gate 				lostconn();
12407c478bd9Sstevel@tonic-gate 		} while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]);
12417c478bd9Sstevel@tonic-gate 
12427c478bd9Sstevel@tonic-gate 		switch (*s++) {
12437c478bd9Sstevel@tonic-gate 		case 'Q': /* Query if file should be removed */
12447c478bd9Sstevel@tonic-gate 			/*
12457c478bd9Sstevel@tonic-gate 			 * Return the following codes to remove query.
12467c478bd9Sstevel@tonic-gate 			 * N\n -- file exists - DON'T remove.
12477c478bd9Sstevel@tonic-gate 			 * Y\n -- file doesn't exist - REMOVE.
12487c478bd9Sstevel@tonic-gate 			 */
12497c478bd9Sstevel@tonic-gate 			*--cp = '\0';
12507c478bd9Sstevel@tonic-gate 			(void) sprintf(tp, "/%s", s);
12517c478bd9Sstevel@tonic-gate 			if (debug)
12527c478bd9Sstevel@tonic-gate 				printf("check %s\n", target);
12537c478bd9Sstevel@tonic-gate 			if (except(target))
12547c478bd9Sstevel@tonic-gate 				(void) deswrite(rem, "N\n", 2, 0);
12557c478bd9Sstevel@tonic-gate 			else if (lstat(target, &stb) < 0)
12567c478bd9Sstevel@tonic-gate 				(void) deswrite(rem, "Y\n", 2, 0);
12577c478bd9Sstevel@tonic-gate 			else
12587c478bd9Sstevel@tonic-gate 				(void) deswrite(rem, "N\n", 2, 0);
12597c478bd9Sstevel@tonic-gate 			break;
12607c478bd9Sstevel@tonic-gate 
12617c478bd9Sstevel@tonic-gate 		case '\0':
12627c478bd9Sstevel@tonic-gate 			*--cp = '\0';
12637c478bd9Sstevel@tonic-gate 			if (*s != '\0')
12647c478bd9Sstevel@tonic-gate 				log(lfp, "%s\n", s);
12657c478bd9Sstevel@tonic-gate 			break;
12667c478bd9Sstevel@tonic-gate 
12677c478bd9Sstevel@tonic-gate 		case 'E':
12687c478bd9Sstevel@tonic-gate 			*tp = '\0';
12697c478bd9Sstevel@tonic-gate 			(void) deswrite(rem, "\0\n", 2, 0);
12707c478bd9Sstevel@tonic-gate 			return;
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 		case '\1':
12737c478bd9Sstevel@tonic-gate 		case '\2':
12747c478bd9Sstevel@tonic-gate 			nerrs++;
12757c478bd9Sstevel@tonic-gate 			if (*s != '\n') {
12767c478bd9Sstevel@tonic-gate 				if (!iamremote) {
12777c478bd9Sstevel@tonic-gate 					fflush(stdout);
12787c478bd9Sstevel@tonic-gate 					(void) write(2, s, cp - s);
12797c478bd9Sstevel@tonic-gate 				}
12807c478bd9Sstevel@tonic-gate 				if (lfp != NULL)
12817c478bd9Sstevel@tonic-gate 					(void) fwrite(s, 1, cp - s, lfp);
12827c478bd9Sstevel@tonic-gate 			}
12837c478bd9Sstevel@tonic-gate 			if (buf[0] == '\2')
12847c478bd9Sstevel@tonic-gate 				lostconn();
12857c478bd9Sstevel@tonic-gate 			break;
12867c478bd9Sstevel@tonic-gate 
12877c478bd9Sstevel@tonic-gate 		default:
12887c478bd9Sstevel@tonic-gate 			error("rmchk: unexpected response '%s'\n", buf);
12897c478bd9Sstevel@tonic-gate 			(void) deswrite(rem, "\1\n", 2, 0);
12907c478bd9Sstevel@tonic-gate 		}
12917c478bd9Sstevel@tonic-gate 	}
12927c478bd9Sstevel@tonic-gate }
12937c478bd9Sstevel@tonic-gate 
12947c478bd9Sstevel@tonic-gate /*
12957c478bd9Sstevel@tonic-gate  * Check the current directory (initialized by the 'T' command to server())
12967c478bd9Sstevel@tonic-gate  * for extraneous files and remove them.
12977c478bd9Sstevel@tonic-gate  */
1298740638c8Sbw static void
clean(char * cp)129956143343SToomas Soome clean(char *cp)
13007c478bd9Sstevel@tonic-gate {
13017c478bd9Sstevel@tonic-gate 	DIR *d;
130256143343SToomas Soome 	struct dirent *dp;
13037c478bd9Sstevel@tonic-gate 	struct stat stb;
13047c478bd9Sstevel@tonic-gate 	char *otp;
13057c478bd9Sstevel@tonic-gate 	int len, opts;
13067c478bd9Sstevel@tonic-gate 
13077c478bd9Sstevel@tonic-gate 	opts = 0;
13087c478bd9Sstevel@tonic-gate 	while (*cp >= '0' && *cp <= '7')
13097c478bd9Sstevel@tonic-gate 		opts = (opts << 3) | (*cp++ - '0');
13107c478bd9Sstevel@tonic-gate 	if (*cp != '\0') {
13117c478bd9Sstevel@tonic-gate 		error("clean: options not delimited\n");
13127c478bd9Sstevel@tonic-gate 		return;
13137c478bd9Sstevel@tonic-gate 	}
13147c478bd9Sstevel@tonic-gate 	if ((d = opendir(target)) == NULL) {
13157c478bd9Sstevel@tonic-gate 		error("%s:%s: %s\n", host, target, strerror(errno));
13167c478bd9Sstevel@tonic-gate 		return;
13177c478bd9Sstevel@tonic-gate 	}
13187c478bd9Sstevel@tonic-gate 	ack();
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	otp = tp;
13217c478bd9Sstevel@tonic-gate 	len = tp - target;
13227c478bd9Sstevel@tonic-gate 	while (dp = readdir(d)) {
13237c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
13247c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
13257c478bd9Sstevel@tonic-gate 			continue;
13267c478bd9Sstevel@tonic-gate 		if ((int)(len + 1 + strlen(dp->d_name)) >=
13277c478bd9Sstevel@tonic-gate 		    (int)(RDIST_BUFSIZ - 1)) {
13287c478bd9Sstevel@tonic-gate 			error("%s:%s/%s: Name too long\n",
132956143343SToomas Soome 			    host, target, dp->d_name);
13307c478bd9Sstevel@tonic-gate 			continue;
13317c478bd9Sstevel@tonic-gate 		}
13327c478bd9Sstevel@tonic-gate 		tp = otp;
13337c478bd9Sstevel@tonic-gate 		*tp++ = '/';
13347c478bd9Sstevel@tonic-gate 		cp = dp->d_name;
13357c478bd9Sstevel@tonic-gate 		while (*tp++ = *cp++)
13367c478bd9Sstevel@tonic-gate 			;
13377c478bd9Sstevel@tonic-gate 		tp--;
13387c478bd9Sstevel@tonic-gate 		if (lstat(target, &stb) < 0) {
13397c478bd9Sstevel@tonic-gate 			error("%s:%s: %s\n", host, target, strerror(errno));
13407c478bd9Sstevel@tonic-gate 			continue;
13417c478bd9Sstevel@tonic-gate 		}
13427c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (buf), "Q%s\n", dp->d_name);
13437c478bd9Sstevel@tonic-gate 		(void) write(wrem, buf, strlen(buf));
13447c478bd9Sstevel@tonic-gate 		cp = buf;
13457c478bd9Sstevel@tonic-gate 		do {
13467c478bd9Sstevel@tonic-gate 			if (read(rem, cp, 1) != 1)
134756143343SToomas Soome 				cleanup(0);
13487c478bd9Sstevel@tonic-gate 		} while (*cp++ != '\n' && cp < &buf[RDIST_BUFSIZ]);
13497c478bd9Sstevel@tonic-gate 		*--cp = '\0';
13507c478bd9Sstevel@tonic-gate 		cp = buf;
13517c478bd9Sstevel@tonic-gate 		if (*cp != 'Y')
13527c478bd9Sstevel@tonic-gate 			continue;
13537c478bd9Sstevel@tonic-gate 		if (opts & VERIFY) {
13547c478bd9Sstevel@tonic-gate 			sendrem("need to remove: %s", target);
13557c478bd9Sstevel@tonic-gate 		} else
13567c478bd9Sstevel@tonic-gate 			(void) recursive_remove(&stb);
13577c478bd9Sstevel@tonic-gate 	}
13587c478bd9Sstevel@tonic-gate 	closedir(d);
13597c478bd9Sstevel@tonic-gate 	(void) write(wrem, "E\n", 2);
13607c478bd9Sstevel@tonic-gate 	(void) response();
13617c478bd9Sstevel@tonic-gate 	tp = otp;
13627c478bd9Sstevel@tonic-gate 	*tp = '\0';
13637c478bd9Sstevel@tonic-gate }
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate /*
13667c478bd9Sstevel@tonic-gate  * Remove a file or directory (recursively) and send back an acknowledge
13677c478bd9Sstevel@tonic-gate  * or an error message.
13687c478bd9Sstevel@tonic-gate  */
1369740638c8Sbw static void
recursive_remove(struct stat * stp)137056143343SToomas Soome recursive_remove(struct stat *stp)
13717c478bd9Sstevel@tonic-gate {
13727c478bd9Sstevel@tonic-gate 	DIR *d;
13737c478bd9Sstevel@tonic-gate 	struct dirent *dp;
137456143343SToomas Soome 	char *cp;
13757c478bd9Sstevel@tonic-gate 	struct stat stb;
13767c478bd9Sstevel@tonic-gate 	char *otp;
13777c478bd9Sstevel@tonic-gate 	int len;
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate 	switch (stp->st_mode & S_IFMT) {
13807c478bd9Sstevel@tonic-gate 	case S_IFREG:
13817c478bd9Sstevel@tonic-gate 	case S_IFLNK:
13827c478bd9Sstevel@tonic-gate 		if (unlink(target) < 0)
13837c478bd9Sstevel@tonic-gate 			goto bad;
13847c478bd9Sstevel@tonic-gate 		goto removed;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	case S_IFDIR:
13877c478bd9Sstevel@tonic-gate 		break;
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	default:
13907c478bd9Sstevel@tonic-gate 		error("%s:%s: not a plain file\n", host, target);
13917c478bd9Sstevel@tonic-gate 		return;
13927c478bd9Sstevel@tonic-gate 	}
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate 	if ((d = opendir(target)) == NULL)
13957c478bd9Sstevel@tonic-gate 		goto bad;
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate 	otp = tp;
13987c478bd9Sstevel@tonic-gate 	len = tp - target;
13997c478bd9Sstevel@tonic-gate 	while (dp = readdir(d)) {
14007c478bd9Sstevel@tonic-gate 		if ((strcmp(dp->d_name, ".") == 0) ||
14017c478bd9Sstevel@tonic-gate 		    (strcmp(dp->d_name, "..") == 0))
14027c478bd9Sstevel@tonic-gate 			continue;
14037c478bd9Sstevel@tonic-gate 		if ((int)(len + 1 + strlen(dp->d_name)) >=
14047c478bd9Sstevel@tonic-gate 		    (int)(RDIST_BUFSIZ - 1)) {
14057c478bd9Sstevel@tonic-gate 			error("%s:%s/%s: Name too long\n",
140656143343SToomas Soome 			    host, target, dp->d_name);
14077c478bd9Sstevel@tonic-gate 			continue;
14087c478bd9Sstevel@tonic-gate 		}
14097c478bd9Sstevel@tonic-gate 		tp = otp;
14107c478bd9Sstevel@tonic-gate 		*tp++ = '/';
14117c478bd9Sstevel@tonic-gate 		cp = dp->d_name;
14127c478bd9Sstevel@tonic-gate 		while (*tp++ = *cp++)
14137c478bd9Sstevel@tonic-gate 			;
14147c478bd9Sstevel@tonic-gate 		tp--;
14157c478bd9Sstevel@tonic-gate 		if (lstat(target, &stb) < 0) {
14167c478bd9Sstevel@tonic-gate 			error("%s:%s: %s\n", host, target, strerror(errno));
14177c478bd9Sstevel@tonic-gate 			continue;
14187c478bd9Sstevel@tonic-gate 		}
14197c478bd9Sstevel@tonic-gate 		recursive_remove(&stb);
14207c478bd9Sstevel@tonic-gate 	}
14217c478bd9Sstevel@tonic-gate 	closedir(d);
14227c478bd9Sstevel@tonic-gate 	tp = otp;
14237c478bd9Sstevel@tonic-gate 	*tp = '\0';
14247c478bd9Sstevel@tonic-gate 	if (rmdir(target) < 0) {
14257c478bd9Sstevel@tonic-gate bad:
14267c478bd9Sstevel@tonic-gate 		error("%s:%s: %s\n", host, target, strerror(errno));
14277c478bd9Sstevel@tonic-gate 		return;
14287c478bd9Sstevel@tonic-gate 	}
14297c478bd9Sstevel@tonic-gate removed:
14307c478bd9Sstevel@tonic-gate 	sendrem("removed %s", target);
14317c478bd9Sstevel@tonic-gate }
14327c478bd9Sstevel@tonic-gate 
14337c478bd9Sstevel@tonic-gate /*
14347c478bd9Sstevel@tonic-gate  * Execute a shell command to handle special cases.
14357c478bd9Sstevel@tonic-gate  */
1436740638c8Sbw static void
dospecial(char * cmd)143756143343SToomas Soome dospecial(char *cmd)
14387c478bd9Sstevel@tonic-gate {
14397c478bd9Sstevel@tonic-gate 	int fd[2], status, pid, i;
144056143343SToomas Soome 	char *cp, *s;
14417c478bd9Sstevel@tonic-gate 	char sbuf[RDIST_BUFSIZ];
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate 	if (pipe(fd) < 0) {
14447c478bd9Sstevel@tonic-gate 		error("%s\n", strerror(errno));
14457c478bd9Sstevel@tonic-gate 		return;
14467c478bd9Sstevel@tonic-gate 	}
14477c478bd9Sstevel@tonic-gate 	if ((pid = fork()) == 0) {
14487c478bd9Sstevel@tonic-gate 		/*
14497c478bd9Sstevel@tonic-gate 		 * Return everything the shell commands print.
14507c478bd9Sstevel@tonic-gate 		 */
14517c478bd9Sstevel@tonic-gate 		(void) close(0);
14527c478bd9Sstevel@tonic-gate 		(void) close(1);
14537c478bd9Sstevel@tonic-gate 		(void) close(2);
14547c478bd9Sstevel@tonic-gate 		(void) open("/dev/null", 0);
14557c478bd9Sstevel@tonic-gate 		(void) dup(fd[1]);
14567c478bd9Sstevel@tonic-gate 		(void) dup(fd[1]);
14577c478bd9Sstevel@tonic-gate 		(void) close(fd[0]);
14587c478bd9Sstevel@tonic-gate 		(void) close(fd[1]);
14597c478bd9Sstevel@tonic-gate 		execl("/bin/sh", "sh", "-c", cmd, 0);
14607c478bd9Sstevel@tonic-gate 		_exit(127);
14617c478bd9Sstevel@tonic-gate 	}
14627c478bd9Sstevel@tonic-gate 	(void) close(fd[1]);
14637c478bd9Sstevel@tonic-gate 	s = sbuf;
14647c478bd9Sstevel@tonic-gate 	*s++ = '\0';
14657c478bd9Sstevel@tonic-gate 	while ((i = read(fd[0], buf, RDIST_BUFSIZ)) > 0) {
14667c478bd9Sstevel@tonic-gate 		cp = buf;
14677c478bd9Sstevel@tonic-gate 		do {
14687c478bd9Sstevel@tonic-gate 			*s++ = *cp++;
14697c478bd9Sstevel@tonic-gate 			if (cp[-1] != '\n') {
14707c478bd9Sstevel@tonic-gate 				if (s < &sbuf[RDIST_BUFSIZ - 1])
14717c478bd9Sstevel@tonic-gate 					continue;
14727c478bd9Sstevel@tonic-gate 				*s++ = '\n';
14737c478bd9Sstevel@tonic-gate 			}
14747c478bd9Sstevel@tonic-gate 			/*
14757c478bd9Sstevel@tonic-gate 			 * Throw away blank lines.
14767c478bd9Sstevel@tonic-gate 			 */
14777c478bd9Sstevel@tonic-gate 			if (s == &sbuf[2]) {
14787c478bd9Sstevel@tonic-gate 				s--;
14797c478bd9Sstevel@tonic-gate 				continue;
14807c478bd9Sstevel@tonic-gate 			}
14817c478bd9Sstevel@tonic-gate 			(void) write(wrem, sbuf, s - sbuf);
14827c478bd9Sstevel@tonic-gate 			s = &sbuf[1];
14837c478bd9Sstevel@tonic-gate 		} while (--i);
14847c478bd9Sstevel@tonic-gate 	}
14857c478bd9Sstevel@tonic-gate 	if (s > &sbuf[1]) {
14867c478bd9Sstevel@tonic-gate 		*s++ = '\n';
14877c478bd9Sstevel@tonic-gate 		(void) write(wrem, sbuf, s - sbuf);
14887c478bd9Sstevel@tonic-gate 	}
14897c478bd9Sstevel@tonic-gate 	while ((i = wait(&status)) != pid && i != -1)
14907c478bd9Sstevel@tonic-gate 		;
14917c478bd9Sstevel@tonic-gate 	if (i == -1)
14927c478bd9Sstevel@tonic-gate 		status = -1;
14937c478bd9Sstevel@tonic-gate 	(void) close(fd[0]);
14947c478bd9Sstevel@tonic-gate 	if (status)
14957c478bd9Sstevel@tonic-gate 		error("shell returned %d\n", status);
14967c478bd9Sstevel@tonic-gate 	else
14977c478bd9Sstevel@tonic-gate 		ack();
14987c478bd9Sstevel@tonic-gate }
14997c478bd9Sstevel@tonic-gate 
1500740638c8Sbw void
log(FILE * fp,char * fmt,...)150156143343SToomas Soome log(FILE *fp, char *fmt, ...)
15027c478bd9Sstevel@tonic-gate {
150356143343SToomas Soome 	va_list ap;
150456143343SToomas Soome 
15057c478bd9Sstevel@tonic-gate 	/* Print changes locally if not quiet mode */
150656143343SToomas Soome 	if (!qflag) {
150756143343SToomas Soome 		va_start(ap, fmt);
150856143343SToomas Soome 		vprintf(fmt, ap);
150956143343SToomas Soome 		va_end(ap);
151056143343SToomas Soome 	}
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	/* Save changes (for mailing) if really updating files */
151356143343SToomas Soome 	va_start(ap, fmt);
15147c478bd9Sstevel@tonic-gate 	if (!(options & VERIFY) && fp != NULL)
151556143343SToomas Soome 		vfprintf(fp, fmt, ap);
151656143343SToomas Soome 	va_end(ap);
15177c478bd9Sstevel@tonic-gate }
15187c478bd9Sstevel@tonic-gate 
1519740638c8Sbw void
error(char * fmt,...)152056143343SToomas Soome error(char *fmt, ...)
15217c478bd9Sstevel@tonic-gate {
152256143343SToomas Soome 	va_list ap;
15237c478bd9Sstevel@tonic-gate 	static FILE *fp;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	nerrs++;
15267c478bd9Sstevel@tonic-gate 	if (iamremote) {
1527*83c2c0baSToomas Soome 		if (!fp && !(fp = fdopen(rem, "w")))
1528*83c2c0baSToomas Soome 			return;
152956143343SToomas Soome 		va_start(ap, fmt);
15307c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%crdist: ", 0x01);
153156143343SToomas Soome 		(void) vfprintf(fp, fmt, ap);
15327c478bd9Sstevel@tonic-gate 		fflush(fp);
153356143343SToomas Soome 		va_end(ap);
15347c478bd9Sstevel@tonic-gate 	} else {
153556143343SToomas Soome 		va_start(ap, fmt);
15367c478bd9Sstevel@tonic-gate 		fflush(stdout);
15377c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rdist: ");
153856143343SToomas Soome 		(void) vfprintf(stderr, fmt, ap);
15397c478bd9Sstevel@tonic-gate 		fflush(stderr);
154056143343SToomas Soome 		va_end(ap);
15417c478bd9Sstevel@tonic-gate 	}
15427c478bd9Sstevel@tonic-gate 	if (lfp != NULL) {
154356143343SToomas Soome 		va_start(ap, fmt);
15447c478bd9Sstevel@tonic-gate 		(void) fprintf(lfp, "rdist: ");
154556143343SToomas Soome 		(void) vfprintf(lfp, fmt, ap);
15467c478bd9Sstevel@tonic-gate 		fflush(lfp);
154756143343SToomas Soome 		va_end(ap);
15487c478bd9Sstevel@tonic-gate 	}
15497c478bd9Sstevel@tonic-gate }
15507c478bd9Sstevel@tonic-gate 
1551740638c8Sbw void
fatal(char * fmt,...)155256143343SToomas Soome fatal(char *fmt, ...)
15537c478bd9Sstevel@tonic-gate {
155456143343SToomas Soome 	va_list ap;
15557c478bd9Sstevel@tonic-gate 	static FILE *fp;
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	nerrs++;
15587c478bd9Sstevel@tonic-gate 	if (iamremote) {
1559*83c2c0baSToomas Soome 		if (!fp && !(fp = fdopen(rem, "w")))
1560*83c2c0baSToomas Soome 			return;
156156143343SToomas Soome 		va_start(ap, fmt);
15627c478bd9Sstevel@tonic-gate 		(void) fprintf(fp, "%crdist: ", 0x02);
156356143343SToomas Soome 		(void) vfprintf(fp, fmt, ap);
15647c478bd9Sstevel@tonic-gate 		fflush(fp);
156556143343SToomas Soome 		va_end(ap);
15667c478bd9Sstevel@tonic-gate 	} else {
156756143343SToomas Soome 		va_start(ap, fmt);
15687c478bd9Sstevel@tonic-gate 		fflush(stdout);
15697c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "rdist: ");
157056143343SToomas Soome 		(void) vfprintf(stderr, fmt, ap);
15717c478bd9Sstevel@tonic-gate 		fflush(stderr);
157256143343SToomas Soome 		va_end(ap);
15737c478bd9Sstevel@tonic-gate 	}
15747c478bd9Sstevel@tonic-gate 	if (lfp != NULL) {
157556143343SToomas Soome 		va_start(ap, fmt);
15767c478bd9Sstevel@tonic-gate 		(void) fprintf(lfp, "rdist: ");
157756143343SToomas Soome 		(void) vfprintf(lfp, fmt, ap);
15787c478bd9Sstevel@tonic-gate 		fflush(lfp);
157956143343SToomas Soome 		va_end(ap);
15807c478bd9Sstevel@tonic-gate 	}
158156143343SToomas Soome 	cleanup(0);
15827c478bd9Sstevel@tonic-gate }
15837c478bd9Sstevel@tonic-gate 
1584740638c8Sbw int
response(void)158556143343SToomas Soome response(void)
15867c478bd9Sstevel@tonic-gate {
15877c478bd9Sstevel@tonic-gate 	char *cp, *s;
15887c478bd9Sstevel@tonic-gate 	char resp[RDIST_BUFSIZ];
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate 	if (debug)
15917c478bd9Sstevel@tonic-gate 		printf("response()\n");
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	cp = s = resp;
15947c478bd9Sstevel@tonic-gate more:
15957c478bd9Sstevel@tonic-gate 	do {
15967c478bd9Sstevel@tonic-gate 		if (desread(rem, cp, 1, 0) != 1)
15977c478bd9Sstevel@tonic-gate 			lostconn();
15987c478bd9Sstevel@tonic-gate 	} while (*cp++ != '\n' && cp < &resp[RDIST_BUFSIZ]);
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 	switch (*s++) {
16017c478bd9Sstevel@tonic-gate 	case '\0':
16027c478bd9Sstevel@tonic-gate 		*--cp = '\0';
16037c478bd9Sstevel@tonic-gate 		if (*s != '\0') {
16047c478bd9Sstevel@tonic-gate 			log(lfp, "%s\n", s);
16057c478bd9Sstevel@tonic-gate 			return (1);
16067c478bd9Sstevel@tonic-gate 		}
16077c478bd9Sstevel@tonic-gate 		return (0);
16087c478bd9Sstevel@tonic-gate 	case '\3':
16097c478bd9Sstevel@tonic-gate 		*--cp = '\0';
16107c478bd9Sstevel@tonic-gate 		log(lfp, "Note: %s\n", s);
16117c478bd9Sstevel@tonic-gate 		return (response());
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	default:
16147c478bd9Sstevel@tonic-gate 		s--;
1615c15ff06aSToomas Soome 		/* FALLTHROUGH */
16167c478bd9Sstevel@tonic-gate 	case '\1':
16177c478bd9Sstevel@tonic-gate 	case '\2':
16187c478bd9Sstevel@tonic-gate 		nerrs++;
16197c478bd9Sstevel@tonic-gate 		if (*s != '\n') {
16207c478bd9Sstevel@tonic-gate 			if (!iamremote) {
16217c478bd9Sstevel@tonic-gate 				fflush(stdout);
16227c478bd9Sstevel@tonic-gate 				(void) write(2, s, cp - s);
16237c478bd9Sstevel@tonic-gate 			}
16247c478bd9Sstevel@tonic-gate 			if (lfp != NULL)
16257c478bd9Sstevel@tonic-gate 				(void) fwrite(s, 1, cp - s, lfp);
16267c478bd9Sstevel@tonic-gate 		}
16277c478bd9Sstevel@tonic-gate 		if (cp == &resp[RDIST_BUFSIZ] && *(cp - 1) != '\n') {
16287c478bd9Sstevel@tonic-gate 			/* preserve status code */
16297c478bd9Sstevel@tonic-gate 			cp = s;
16307c478bd9Sstevel@tonic-gate 			s = resp;
16317c478bd9Sstevel@tonic-gate 			goto more;
16327c478bd9Sstevel@tonic-gate 		}
16337c478bd9Sstevel@tonic-gate 		if (resp[0] == '\2')
16347c478bd9Sstevel@tonic-gate 			lostconn();
16357c478bd9Sstevel@tonic-gate 		return (-1);
16367c478bd9Sstevel@tonic-gate 	}
16377c478bd9Sstevel@tonic-gate }
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate /*
16407c478bd9Sstevel@tonic-gate  * Remove temporary files and do any cleanup operations before exiting.
16417c478bd9Sstevel@tonic-gate  */
16427c478bd9Sstevel@tonic-gate void
cleanup(int arg __unused)164356143343SToomas Soome cleanup(int arg __unused)
16447c478bd9Sstevel@tonic-gate {
16457c478bd9Sstevel@tonic-gate 	(void) unlink(Tmpfile);
16467c478bd9Sstevel@tonic-gate 	exit(1);
16477c478bd9Sstevel@tonic-gate }
16487c478bd9Sstevel@tonic-gate 
1649740638c8Sbw static void
note(char * fmt,...)165056143343SToomas Soome note(char *fmt, ...)
16517c478bd9Sstevel@tonic-gate {
165256143343SToomas Soome 	va_list ap;
16537c478bd9Sstevel@tonic-gate 	static char buf[RDIST_BUFSIZ];
165456143343SToomas Soome 
165556143343SToomas Soome 	va_start(ap, fmt);
165656143343SToomas Soome 	(void) vsnprintf(buf, sizeof (buf) - 1, fmt, ap);
16577c478bd9Sstevel@tonic-gate 	comment(buf);
165856143343SToomas Soome 	va_end(ap);
16597c478bd9Sstevel@tonic-gate }
16607c478bd9Sstevel@tonic-gate 
1661740638c8Sbw static void
comment(char * s)166256143343SToomas Soome comment(char *s)
16637c478bd9Sstevel@tonic-gate {
16647c478bd9Sstevel@tonic-gate 	char three = '\3';
16657c478bd9Sstevel@tonic-gate 	char nl = '\n';
16667c478bd9Sstevel@tonic-gate 	struct iovec iov[3];
16677c478bd9Sstevel@tonic-gate 
16687c478bd9Sstevel@tonic-gate 	iov[0].iov_base = &three;
16697c478bd9Sstevel@tonic-gate 	iov[0].iov_len = sizeof (char);
16707c478bd9Sstevel@tonic-gate 	iov[1].iov_base = s;
16717c478bd9Sstevel@tonic-gate 	iov[1].iov_len = strlen(s);
16727c478bd9Sstevel@tonic-gate 	iov[2].iov_base = &nl;
16737c478bd9Sstevel@tonic-gate 	iov[2].iov_len = sizeof (char);
16747c478bd9Sstevel@tonic-gate 	(void) writev(rem, iov, 3);
16757c478bd9Sstevel@tonic-gate }
16767c478bd9Sstevel@tonic-gate 
16777c478bd9Sstevel@tonic-gate /*
16787c478bd9Sstevel@tonic-gate  * Send message to other end.
16797c478bd9Sstevel@tonic-gate  * N.B.: uses buf[].
16807c478bd9Sstevel@tonic-gate  */
16817c478bd9Sstevel@tonic-gate void
sendrem(char * fmt,int a1,int a2,int a3)168256143343SToomas Soome sendrem(char *fmt, int a1, int a2, int a3)
16837c478bd9Sstevel@tonic-gate {
168456143343SToomas Soome 	int len;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	buf[0] = '\0';
16877c478bd9Sstevel@tonic-gate 	len = snprintf(buf + 1, sizeof (buf) - 1, fmt, a1, a2, a3) + 2;
16887c478bd9Sstevel@tonic-gate 	if (len > sizeof (buf))
16897c478bd9Sstevel@tonic-gate 		len = sizeof (buf);
16907c478bd9Sstevel@tonic-gate 	buf[len - 1] = '\n';
16917c478bd9Sstevel@tonic-gate 	(void) write(wrem, buf, len);
16927c478bd9Sstevel@tonic-gate }
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate /*
16957c478bd9Sstevel@tonic-gate  * strsub(old, new, s)
16967c478bd9Sstevel@tonic-gate  *
16977c478bd9Sstevel@tonic-gate  * Return a pointer to a new string created by replacing substring old
16987c478bd9Sstevel@tonic-gate  * with substring new in string s.  String s is assumed to begin with
16997c478bd9Sstevel@tonic-gate  * substring old.
17007c478bd9Sstevel@tonic-gate  */
17017c478bd9Sstevel@tonic-gate char *
strsub(char * old,char * new,char * s)170256143343SToomas Soome strsub(char *old, char *new, char *s)
17037c478bd9Sstevel@tonic-gate {
17047c478bd9Sstevel@tonic-gate 	static char pbuf[PATH_MAX];
170556143343SToomas Soome 	char *p, *q, *r, *plim;
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	/* prepend new to pbuf */
17087c478bd9Sstevel@tonic-gate 	for (p = pbuf, q = new, plim = pbuf + sizeof (pbuf) - 1;
17097c478bd9Sstevel@tonic-gate 	/* CSTYLED */
17107c478bd9Sstevel@tonic-gate 	    *q && (p < plim);)
17117c478bd9Sstevel@tonic-gate 		*p++ = *q++;
17127c478bd9Sstevel@tonic-gate 	/* p now points to the byte in pbuf where more copying should begin */
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	/* skip over the part of s which begins with old */
17157c478bd9Sstevel@tonic-gate 	for (r = old, q = s; *r; q++, r++)
17167c478bd9Sstevel@tonic-gate 		;
17177c478bd9Sstevel@tonic-gate 	/* q now points to the byte in s where more copying should begin */
17187c478bd9Sstevel@tonic-gate 
17197c478bd9Sstevel@tonic-gate 	while (*q && (p < plim))
17207c478bd9Sstevel@tonic-gate 		*p++ = *q++;
17217c478bd9Sstevel@tonic-gate 	*p = '\0';
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	return (pbuf);
17247c478bd9Sstevel@tonic-gate }
1725