2013-10-19 00:41:49 +04:00
|
|
|
/* $NetBSD: server.c,v 1.32 2013/10/18 20:41:49 christos Exp $ */
|
1996-07-12 04:46:18 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-03-07 08:05:16 +03:00
|
|
|
* Copyright (c) 1983, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-10-19 17:58:48 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1996-07-12 04:46:18 +04:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)server.c 8.1 (Berkeley) 6/9/93";
|
|
|
|
#else
|
2013-10-19 00:41:49 +04:00
|
|
|
__RCSID("$NetBSD: server.c,v 1.32 2013/10/18 20:41:49 christos Exp $");
|
1996-07-12 04:46:18 +04:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
1997-10-18 18:34:39 +04:00
|
|
|
#include <sys/types.h>
|
1994-03-07 08:05:16 +03:00
|
|
|
#include <sys/wait.h>
|
1997-10-18 18:34:39 +04:00
|
|
|
|
|
|
|
#include <errno.h>
|
1998-12-19 23:32:17 +03:00
|
|
|
#include <fcntl.h>
|
2002-06-14 05:18:54 +04:00
|
|
|
#include <grp.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <stdarg.h>
|
1997-10-18 18:34:39 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
#include "defs.h"
|
|
|
|
|
1999-04-20 11:53:02 +04:00
|
|
|
#define ack() do { if (write(rem, "\0\n", 2) < 0) error("ack failed: %s\n", strerror(errno)); } while (0)
|
|
|
|
#define err() do { if (write(rem, "\1\n", 2) < 0) error("err failed: %s\n", strerror(errno)); } while (0)
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
struct linkbuf *ihead; /* list of files with more than one link */
|
|
|
|
char buf[BUFSIZ]; /* general purpose buffer */
|
|
|
|
char target[BUFSIZ]; /* target/source directory name */
|
|
|
|
char *tp; /* pointer to end of target name */
|
|
|
|
char *Tdest; /* pointer to last T dest*/
|
2004-08-06 19:50:02 +04:00
|
|
|
char *Destcopy; /* pointer to current dest */
|
|
|
|
int Destcopylen; /* length of destination directory name */
|
|
|
|
int Sourcelen; /* length of source directory name */
|
1993-03-21 12:45:37 +03:00
|
|
|
int catname; /* cat name to target name */
|
|
|
|
char *stp[32]; /* stack of saved tp's for directories */
|
|
|
|
int oumask; /* old umask for creating files */
|
|
|
|
|
|
|
|
extern FILE *lfp; /* log file for mailing changes */
|
|
|
|
|
2002-06-14 05:18:54 +04:00
|
|
|
static int chkparent(char *);
|
|
|
|
static void clean(char *);
|
|
|
|
static void comment(char *);
|
|
|
|
static void dospecial(char *);
|
|
|
|
static int fchtogm(int, char *, time_t, char *, char *, mode_t);
|
|
|
|
static void hardlink(char *);
|
|
|
|
static void note(const char *, ...)
|
2000-10-11 18:46:00 +04:00
|
|
|
__attribute__((__format__(__printf__, 1, 2)));
|
2002-06-14 05:18:54 +04:00
|
|
|
static void query(char *);
|
|
|
|
static void recvf(char *, int);
|
|
|
|
static void removeit(struct stat *);
|
|
|
|
static int response(void);
|
|
|
|
static void rmchk(int);
|
1994-03-07 08:05:16 +03:00
|
|
|
static struct linkbuf *
|
2002-06-14 05:18:54 +04:00
|
|
|
savelink(struct stat *);
|
|
|
|
static void sendf(char *, int);
|
|
|
|
static int update(char *, int, struct stat *);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Server routine to read requests and process them.
|
|
|
|
* Commands are:
|
|
|
|
* Tname - Transmit file if out of date
|
|
|
|
* Vname - Verify if file out of date or not
|
|
|
|
* Qname - Query if file exists. Return mtime & size if it does.
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
void
|
2002-06-14 05:18:54 +04:00
|
|
|
server(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char cmdbuf[BUFSIZ];
|
1997-10-19 17:58:48 +04:00
|
|
|
char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
signal(SIGHUP, cleanup);
|
|
|
|
signal(SIGINT, cleanup);
|
|
|
|
signal(SIGQUIT, cleanup);
|
|
|
|
signal(SIGTERM, cleanup);
|
|
|
|
signal(SIGPIPE, cleanup);
|
|
|
|
|
|
|
|
rem = 0;
|
|
|
|
oumask = umask(0);
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf), "V%d\n", VERSION);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, strlen(buf)) < 0)
|
|
|
|
error("server: could not write remote end: %s\n",
|
|
|
|
strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
cp = cmdbuf;
|
|
|
|
if (read(rem, cp, 1) <= 0)
|
|
|
|
return;
|
|
|
|
if (*cp++ == '\n') {
|
|
|
|
error("server: expected control record\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
if (read(rem, cp, 1) != 1)
|
1994-03-07 08:05:16 +03:00
|
|
|
cleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
} while (*cp++ != '\n' && cp < &cmdbuf[BUFSIZ]);
|
|
|
|
*--cp = '\0';
|
|
|
|
cp = cmdbuf;
|
|
|
|
switch (*cp++) {
|
|
|
|
case 'T': /* init target file/directory name */
|
|
|
|
catname = 1; /* target should be directory */
|
|
|
|
goto dotarget;
|
|
|
|
|
|
|
|
case 't': /* init target file/directory name */
|
|
|
|
catname = 0;
|
|
|
|
dotarget:
|
|
|
|
if (exptilde(target, cp) == NULL)
|
|
|
|
continue;
|
|
|
|
tp = target;
|
|
|
|
while (*tp)
|
|
|
|
tp++;
|
|
|
|
ack();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'R': /* Transfer a regular file. */
|
|
|
|
recvf(cp, S_IFREG);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'D': /* Transfer a directory. */
|
|
|
|
recvf(cp, S_IFDIR);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'K': /* Transfer symbolic link. */
|
|
|
|
recvf(cp, S_IFLNK);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'k': /* Transfer hard link. */
|
|
|
|
hardlink(cp);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'E': /* End. (of directory) */
|
|
|
|
*tp = '\0';
|
|
|
|
if (catname <= 0) {
|
|
|
|
error("server: too many 'E's\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tp = stp[--catname];
|
|
|
|
*tp = '\0';
|
|
|
|
ack();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'C': /* Clean. Cleanup a directory */
|
|
|
|
clean(cp);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'Q': /* Query. Does the file/directory exist? */
|
|
|
|
query(cp);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'S': /* Special. Execute commands */
|
|
|
|
dospecial(cp);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
#ifdef notdef
|
|
|
|
/*
|
|
|
|
* These entries are reserved but not currently used.
|
|
|
|
* The intent is to allow remote hosts to have master copies.
|
|
|
|
* Currently, only the host rdist runs on can have masters.
|
|
|
|
*/
|
|
|
|
case 'X': /* start a new list of files to exclude */
|
|
|
|
except = bp = NULL;
|
|
|
|
case 'x': /* add name to list of files to exclude */
|
|
|
|
if (*cp == '\0') {
|
|
|
|
ack();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (*cp == '~') {
|
|
|
|
if (exptilde(buf, cp) == NULL)
|
|
|
|
continue;
|
|
|
|
cp = buf;
|
|
|
|
}
|
|
|
|
if (bp == NULL)
|
|
|
|
except = bp = expand(makeblock(NAME, cp), E_VARS);
|
|
|
|
else
|
|
|
|
bp->b_next = expand(makeblock(NAME, cp), E_VARS);
|
|
|
|
while (bp->b_next != NULL)
|
|
|
|
bp = bp->b_next;
|
|
|
|
ack();
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'I': /* Install. Transfer file if out of date. */
|
|
|
|
opts = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '7')
|
|
|
|
opts = (opts << 3) | (*cp++ - '0');
|
|
|
|
if (*cp++ != ' ') {
|
|
|
|
error("server: options not delimited\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
install(cp, opts);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'L': /* Log. save message in log file */
|
|
|
|
log(lfp, cp);
|
|
|
|
continue;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
case '\1':
|
|
|
|
nerrs++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case '\2':
|
|
|
|
return;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("server: unknown command '%s'\n", cp);
|
|
|
|
case '\0':
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the file(s) if they are different.
|
|
|
|
* destdir = 1 if destination should be a directory
|
|
|
|
* (i.e., more than one source is being copied to the same destination).
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
void
|
2002-06-14 05:18:54 +04:00
|
|
|
install(char *src, char *dest, int destdir, int opts)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char *rname;
|
|
|
|
char destcopy[BUFSIZ];
|
|
|
|
|
|
|
|
if (dest == NULL) {
|
|
|
|
opts &= ~WHOLE; /* WHOLE mode only useful if renaming */
|
|
|
|
dest = src;
|
2004-08-06 19:50:02 +04:00
|
|
|
} else if (!(opts & WHOLE)) {
|
|
|
|
/* prepare for proper renaming of directory trees */
|
|
|
|
Destcopy = destcopy;
|
|
|
|
Destcopylen = strlen(dest);
|
|
|
|
while (Destcopylen > 0 && dest[Destcopylen] == '/')
|
|
|
|
Destcopylen--;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2004-08-06 19:50:02 +04:00
|
|
|
strlcpy(destcopy, dest, sizeof(destcopy));
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (nflag || debug) {
|
|
|
|
printf("%s%s%s%s%s %s %s\n", opts & VERIFY ? "verify":"install",
|
|
|
|
opts & WHOLE ? " -w" : "",
|
|
|
|
opts & YOUNGER ? " -y" : "",
|
|
|
|
opts & COMPARE ? " -b" : "",
|
|
|
|
opts & REMOVE ? " -R" : "", src, dest);
|
|
|
|
if (nflag)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rname = exptilde(target, src);
|
|
|
|
if (rname == NULL)
|
|
|
|
return;
|
|
|
|
tp = target;
|
|
|
|
while (*tp)
|
|
|
|
tp++;
|
2004-08-06 19:50:02 +04:00
|
|
|
if (Destcopy) {
|
|
|
|
/* We can only do this after expansion of src */
|
|
|
|
Sourcelen = strlen(target);
|
|
|
|
while (Sourcelen > 0 && target[Sourcelen] == '/')
|
|
|
|
Sourcelen--;
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
|
|
|
* If we are renaming a directory and we want to preserve
|
2001-08-24 14:24:45 +04:00
|
|
|
* the directory hierarchy (-w), we must strip off the leading
|
1993-03-21 12:45:37 +03:00
|
|
|
* directory name and preserve the rest.
|
|
|
|
*/
|
|
|
|
if (opts & WHOLE) {
|
|
|
|
while (*rname == '/')
|
|
|
|
rname++;
|
|
|
|
destdir = 1;
|
|
|
|
} else {
|
1997-10-19 17:58:48 +04:00
|
|
|
rname = strrchr(target, '/');
|
1993-03-21 12:45:37 +03:00
|
|
|
if (rname == NULL)
|
|
|
|
rname = target;
|
|
|
|
else
|
|
|
|
rname++;
|
|
|
|
}
|
|
|
|
if (debug)
|
|
|
|
printf("target = %s, rname = %s\n", target, rname);
|
|
|
|
/*
|
|
|
|
* Pass the destination file/directory name to remote.
|
|
|
|
*/
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf), "%c%s\n", destdir ? 'T' : 't', dest);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("buf = %s", buf);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, strlen(buf)) < 0)
|
|
|
|
error("could not pass filename to remote: %s\n",
|
|
|
|
strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
if (response() < 0)
|
|
|
|
return;
|
|
|
|
|
2004-08-06 19:50:02 +04:00
|
|
|
if (destdir)
|
1993-03-21 12:45:37 +03:00
|
|
|
Tdest = destcopy;
|
|
|
|
sendf(rname, opts);
|
2004-08-06 19:50:02 +04:00
|
|
|
Destcopy = 0;
|
1993-03-21 12:45:37 +03:00
|
|
|
Tdest = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define protoname() (pw ? pw->pw_name : user)
|
|
|
|
#define protogroup() (gr ? gr->gr_name : group)
|
|
|
|
/*
|
|
|
|
* Transfer the file or directory in target[].
|
|
|
|
* rname is the name of the file on the remote host.
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
static void
|
2002-06-14 05:18:54 +04:00
|
|
|
sendf(char *rname, int opts)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 17:58:48 +04:00
|
|
|
struct subcmd *sc;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stat stb;
|
|
|
|
int sizerr, f, u, len;
|
|
|
|
off_t i;
|
|
|
|
DIR *d;
|
1998-12-19 23:32:17 +03:00
|
|
|
struct dirent *dp;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *otp, *cp;
|
|
|
|
extern struct subcmd *subcmds;
|
|
|
|
static char user[15], group[15];
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
printf("sendf(%s, %x)\n", rname, opts);
|
|
|
|
|
|
|
|
if (except(target))
|
|
|
|
return;
|
|
|
|
if ((opts & FOLLOW ? stat(target, &stb) : lstat(target, &stb)) < 0) {
|
|
|
|
error("%s: %s\n", target, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((u = update(rname, opts, &stb)) == 0) {
|
1997-10-19 18:25:24 +04:00
|
|
|
if (S_ISREG(stb.st_mode) && stb.st_nlink > 1)
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) savelink(&stb);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pw == NULL || pw->pw_uid != stb.st_uid)
|
|
|
|
if ((pw = getpwuid(stb.st_uid)) == NULL) {
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "%s: no password entry for uid %d \n",
|
1993-03-21 12:45:37 +03:00
|
|
|
target, stb.st_uid);
|
|
|
|
pw = NULL;
|
1997-10-19 17:58:48 +04:00
|
|
|
(void)snprintf(user, sizeof(user), ":%lu",
|
|
|
|
(u_long)stb.st_uid);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (gr == NULL || gr->gr_gid != stb.st_gid)
|
|
|
|
if ((gr = getgrgid(stb.st_gid)) == NULL) {
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "%s: no name for group %d\n",
|
1993-03-21 12:45:37 +03:00
|
|
|
target, stb.st_gid);
|
|
|
|
gr = NULL;
|
1996-07-12 04:06:32 +04:00
|
|
|
(void)snprintf(group, sizeof(group), ":%lu",
|
1997-10-19 17:58:48 +04:00
|
|
|
(u_long)stb.st_gid);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (u == 1) {
|
|
|
|
if (opts & VERIFY) {
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "need to install: %s\n", target);
|
1993-03-21 12:45:37 +03:00
|
|
|
goto dospecial;
|
|
|
|
}
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "installing: %s\n", target);
|
1993-03-21 12:45:37 +03:00
|
|
|
opts &= ~(COMPARE|REMOVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (stb.st_mode & S_IFMT) {
|
|
|
|
case S_IFDIR:
|
|
|
|
if ((d = opendir(target)) == NULL) {
|
|
|
|
error("%s: %s\n", target, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf), "D%o %04o 0 0 %s %s %s\n",
|
|
|
|
opts, stb.st_mode & 07777, protoname(), protogroup(),
|
|
|
|
rname);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("buf = %s", buf);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, strlen(buf)) < 0)
|
|
|
|
error("can not write dir spec to remote: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (response() < 0) {
|
|
|
|
closedir(d);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts & REMOVE)
|
|
|
|
rmchk(opts);
|
|
|
|
|
|
|
|
otp = tp;
|
|
|
|
len = tp - target;
|
1997-10-19 17:58:48 +04:00
|
|
|
while ((dp = readdir(d)) != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!strcmp(dp->d_name, ".") ||
|
|
|
|
!strcmp(dp->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
|
|
|
|
error("%s/%s: Name too long\n", target,
|
|
|
|
dp->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tp = otp;
|
|
|
|
*tp++ = '/';
|
|
|
|
cp = dp->d_name;
|
1997-10-19 17:58:48 +04:00
|
|
|
while ((*tp++ = *cp++) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
;
|
|
|
|
tp--;
|
|
|
|
sendf(dp->d_name, opts);
|
|
|
|
}
|
|
|
|
closedir(d);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, "E\n", 2) < 0)
|
|
|
|
error("can not write E to remote: %s\n",
|
|
|
|
strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) response();
|
|
|
|
tp = otp;
|
|
|
|
*tp = '\0';
|
|
|
|
return;
|
|
|
|
|
|
|
|
case S_IFLNK:
|
|
|
|
if (u != 1)
|
|
|
|
opts |= COMPARE;
|
|
|
|
if (stb.st_nlink > 1) {
|
|
|
|
struct linkbuf *lp;
|
|
|
|
|
|
|
|
if ((lp = savelink(&stb)) != NULL) {
|
|
|
|
/* install link */
|
|
|
|
if (*lp->target == 0)
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf),
|
|
|
|
"k%o %s %s\n", opts, lp->pathname, rname);
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf),
|
|
|
|
"k%o %s/%s %s\n", opts, lp->target,
|
|
|
|
lp->pathname, rname);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("buf = %s", buf);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, strlen(buf)) < 0)
|
|
|
|
error("can not write link spec to remote: %s\n",
|
|
|
|
strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) response();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2001-01-05 02:05:54 +03:00
|
|
|
(void) snprintf(buf, sizeof(buf), "K%o %o %lld %ld %s %s %s\n",
|
1997-10-19 18:50:58 +04:00
|
|
|
opts, stb.st_mode & 07777, (unsigned long long)stb.st_size,
|
|
|
|
(u_long)stb.st_mtime, protoname(), protogroup(), rname);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("buf = %s", buf);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, strlen(buf)) < 0)
|
|
|
|
error("can not write link spec to remote: %s\n",
|
|
|
|
strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
if (response() < 0)
|
|
|
|
return;
|
|
|
|
sizerr = (readlink(target, buf, BUFSIZ) != stb.st_size);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, stb.st_size) < 0)
|
|
|
|
error("can not write link name to remote: %s\n",
|
|
|
|
strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("readlink = %.*s\n", (int)stb.st_size, buf);
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
case S_IFREG:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("%s: not a file or directory\n", target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (u == 2) {
|
|
|
|
if (opts & VERIFY) {
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "need to update: %s\n", target);
|
1993-03-21 12:45:37 +03:00
|
|
|
goto dospecial;
|
|
|
|
}
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "updating: %s\n", target);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stb.st_nlink > 1) {
|
|
|
|
struct linkbuf *lp;
|
|
|
|
|
|
|
|
if ((lp = savelink(&stb)) != NULL) {
|
|
|
|
/* install link */
|
|
|
|
if (*lp->target == 0)
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf), "k%o %s %s\n", opts,
|
1993-03-21 12:45:37 +03:00
|
|
|
lp->pathname, rname);
|
|
|
|
else
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf), "k%o %s/%s %s\n",
|
|
|
|
opts, lp->target, lp->pathname, rname);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("buf = %s", buf);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, strlen(buf)) <0)
|
|
|
|
error("write of file name failed: %s\n",
|
|
|
|
strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) response();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-03-07 08:05:16 +03:00
|
|
|
if ((f = open(target, O_RDONLY, 0)) < 0) {
|
1993-03-21 12:45:37 +03:00
|
|
|
error("%s: %s\n", target, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2001-01-05 02:05:54 +03:00
|
|
|
(void)snprintf(buf, sizeof(buf), "R%o %o %lld %lu %s %s %s\n", opts,
|
1997-10-19 18:50:58 +04:00
|
|
|
stb.st_mode & 07777, (unsigned long long)stb.st_size,
|
|
|
|
(u_long)stb.st_mtime, protoname(), protogroup(), rname);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("buf = %s", buf);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, strlen(buf)) < 0)
|
|
|
|
error("write of file name failed: %s\n", strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
if (response() < 0) {
|
|
|
|
(void) close(f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
sizerr = 0;
|
|
|
|
for (i = 0; i < stb.st_size; i += BUFSIZ) {
|
|
|
|
int amt = BUFSIZ;
|
|
|
|
if (i + amt > stb.st_size)
|
|
|
|
amt = stb.st_size - i;
|
|
|
|
if (sizerr == 0 && read(f, buf, amt) != amt)
|
|
|
|
sizerr = 1;
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, amt) < 0)
|
|
|
|
error("write of file data failed: %s\n", strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
(void) close(f);
|
|
|
|
done:
|
|
|
|
if (sizerr) {
|
|
|
|
error("%s: file changed size\n", target);
|
|
|
|
err();
|
|
|
|
} else
|
|
|
|
ack();
|
|
|
|
f = response();
|
1997-10-19 17:58:48 +04:00
|
|
|
if (f < 0 || (f == 0 && (opts & COMPARE)))
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
dospecial:
|
|
|
|
for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
|
|
|
|
if (sc->sc_type != SPECIAL)
|
|
|
|
continue;
|
|
|
|
if (sc->sc_args != NULL && !inlist(sc->sc_args, target))
|
|
|
|
continue;
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "special \"%s\"\n", sc->sc_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (opts & VERIFY)
|
|
|
|
continue;
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf), "SFILE=%s;%s\n", target,
|
|
|
|
sc->sc_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("buf = %s", buf);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, strlen(buf)) < 0)
|
|
|
|
error("write of special failed: %s\n", strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
while (response() > 0)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-03-07 08:05:16 +03:00
|
|
|
static struct linkbuf *
|
2009-04-13 08:35:36 +04:00
|
|
|
savelink(struct stat *st)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct linkbuf *lp;
|
|
|
|
|
|
|
|
for (lp = ihead; lp != NULL; lp = lp->nextp)
|
2009-04-13 08:35:36 +04:00
|
|
|
if (lp->inum == st->st_ino && lp->devnum == st->st_dev) {
|
1993-03-21 12:45:37 +03:00
|
|
|
lp->count--;
|
|
|
|
return(lp);
|
|
|
|
}
|
|
|
|
lp = (struct linkbuf *) malloc(sizeof(*lp));
|
|
|
|
if (lp == NULL)
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "out of memory, link information lost\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
else {
|
|
|
|
lp->nextp = ihead;
|
|
|
|
ihead = lp;
|
2009-04-13 08:35:36 +04:00
|
|
|
lp->inum = st->st_ino;
|
|
|
|
lp->devnum = st->st_dev;
|
|
|
|
lp->count = st->st_nlink - 1;
|
2004-08-06 19:50:02 +04:00
|
|
|
if (Destcopy) {
|
|
|
|
/*
|
|
|
|
* Change the starting directory of target
|
|
|
|
* into the destination directory
|
|
|
|
*/
|
|
|
|
strncpy(lp->pathname, Destcopy, Destcopylen);
|
|
|
|
strlcpy(lp->pathname + Destcopylen, target + Sourcelen, sizeof(lp->pathname) - Destcopylen);
|
|
|
|
} else
|
|
|
|
strlcpy(lp->pathname, target, sizeof(lp->pathname));
|
1993-03-21 12:45:37 +03:00
|
|
|
if (Tdest)
|
2003-07-23 08:11:12 +04:00
|
|
|
strlcpy(lp->target, Tdest, sizeof(lp->target));
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
|
|
|
*lp->target = 0;
|
|
|
|
}
|
|
|
|
return(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if file needs to be updated on the remote machine.
|
|
|
|
* Returns 0 if no update, 1 if remote doesn't exist, 2 if out of date
|
|
|
|
* and 3 if comparing binaries to determine if out of date.
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
static int
|
2009-04-13 08:35:36 +04:00
|
|
|
update(char *rname, int opts, struct stat *st)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 17:58:48 +04:00
|
|
|
char *cp, *s;
|
|
|
|
off_t size;
|
|
|
|
time_t mtime;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (debug)
|
2009-04-13 08:35:36 +04:00
|
|
|
printf("update(%s, %lx, %lx)\n", rname, (long)opts, (long)st);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if the file exists on the remote machine.
|
|
|
|
*/
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf), "Q%s\n", rname);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("buf = %s", buf);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, strlen(buf)) < 0)
|
|
|
|
error("write to remote failed: %s\n", strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
again:
|
|
|
|
cp = s = buf;
|
|
|
|
do {
|
|
|
|
if (read(rem, cp, 1) != 1)
|
1994-03-07 08:05:16 +03:00
|
|
|
lostconn(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
} while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
|
|
|
|
|
|
|
|
switch (*s++) {
|
|
|
|
case 'Y':
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'N': /* file doesn't exist so install it */
|
|
|
|
return(1);
|
|
|
|
|
|
|
|
case '\1':
|
|
|
|
nerrs++;
|
|
|
|
if (*s != '\n') {
|
|
|
|
if (!iamremote) {
|
|
|
|
fflush(stdout);
|
|
|
|
(void) write(2, s, cp - s);
|
|
|
|
}
|
|
|
|
if (lfp != NULL)
|
|
|
|
(void) fwrite(s, 1, cp - s, lfp);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
|
|
|
|
case '\3':
|
|
|
|
*--cp = '\0';
|
|
|
|
if (lfp != NULL)
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "update: note: %s\n", s);
|
1993-03-21 12:45:37 +03:00
|
|
|
goto again;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*--cp = '\0';
|
|
|
|
error("update: unexpected response '%s'\n", s);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*s == '\n')
|
|
|
|
return(2);
|
|
|
|
|
|
|
|
if (opts & COMPARE)
|
|
|
|
return(3);
|
|
|
|
|
|
|
|
size = 0;
|
1998-12-19 23:34:53 +03:00
|
|
|
while (isdigit((unsigned char)*s))
|
1993-03-21 12:45:37 +03:00
|
|
|
size = size * 10 + (*s++ - '0');
|
|
|
|
if (*s++ != ' ') {
|
|
|
|
error("update: size not delimited\n");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
mtime = 0;
|
1998-12-19 23:34:53 +03:00
|
|
|
while (isdigit((unsigned char)*s))
|
1993-03-21 12:45:37 +03:00
|
|
|
mtime = mtime * 10 + (*s++ - '0');
|
|
|
|
if (*s != '\n') {
|
|
|
|
error("update: mtime not delimited\n");
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* File needs to be updated?
|
|
|
|
*/
|
|
|
|
if (opts & YOUNGER) {
|
2009-04-13 08:35:36 +04:00
|
|
|
if (st->st_mtime == mtime)
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
2009-04-13 08:35:36 +04:00
|
|
|
if (st->st_mtime < mtime) {
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "Warning: %s: remote copy is newer\n",
|
|
|
|
target);
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
}
|
2009-04-13 08:35:36 +04:00
|
|
|
} else if (st->st_mtime == mtime && st->st_size == size)
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
return(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Query. Check to see if file exists. Return one of the following:
|
|
|
|
* N\n - doesn't exist
|
|
|
|
* Ysize mtime\n - exists and its a regular file (size & mtime of file)
|
|
|
|
* Y\n - exists and its a directory or symbolic link
|
|
|
|
* ^Aerror message\n
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
static void
|
2002-06-14 05:18:54 +04:00
|
|
|
query(char *name)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
struct stat stb;
|
|
|
|
|
|
|
|
if (catname)
|
1996-07-12 04:38:50 +04:00
|
|
|
(void) snprintf(tp, sizeof(target) - (tp - target),
|
|
|
|
"/%s", name);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (lstat(target, &stb) < 0) {
|
1999-04-20 11:53:02 +04:00
|
|
|
if (errno == ENOENT) {
|
|
|
|
if (write(rem, "N\n", 2) < 0)
|
|
|
|
error("write to remote failed: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
} else
|
1993-03-21 12:45:37 +03:00
|
|
|
error("%s:%s: %s\n", host, target, strerror(errno));
|
|
|
|
*tp = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (stb.st_mode & S_IFMT) {
|
|
|
|
case S_IFREG:
|
2001-01-05 02:05:54 +03:00
|
|
|
(void)snprintf(buf, sizeof(buf), "Y%lld %ld\n",
|
1997-10-19 18:50:58 +04:00
|
|
|
(unsigned long long)stb.st_size, (u_long)stb.st_mtime);
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, buf, strlen(buf)) < 0)
|
|
|
|
error("write to remote failed: %s\n", strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case S_IFLNK:
|
|
|
|
case S_IFDIR:
|
1999-04-20 11:53:02 +04:00
|
|
|
if (write(rem, "Y\n", 2) < 0)
|
|
|
|
error("write to remote failed: %s\n", strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("%s: not a file or directory\n", name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*tp = '\0';
|
|
|
|
}
|
|
|
|
|
1994-03-07 08:05:16 +03:00
|
|
|
static void
|
2002-06-14 05:18:54 +04:00
|
|
|
recvf(char *cmd, int type)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 17:58:48 +04:00
|
|
|
char *cp = cmd;
|
2013-10-19 00:41:49 +04:00
|
|
|
int f = -1, opts = 0, wrerr;
|
1997-10-19 18:25:24 +04:00
|
|
|
mode_t mode;
|
1993-03-21 12:45:37 +03:00
|
|
|
off_t i, size;
|
|
|
|
time_t mtime;
|
|
|
|
struct stat stb;
|
|
|
|
char *owner, *group;
|
|
|
|
char new[BUFSIZ];
|
|
|
|
extern char *tempname;
|
|
|
|
|
|
|
|
while (*cp >= '0' && *cp <= '7')
|
|
|
|
opts = (opts << 3) | (*cp++ - '0');
|
|
|
|
if (*cp++ != ' ') {
|
|
|
|
error("recvf: options not delimited\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mode = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '7')
|
|
|
|
mode = (mode << 3) | (*cp++ - '0');
|
|
|
|
if (*cp++ != ' ') {
|
|
|
|
error("recvf: mode not delimited\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
size = 0;
|
1998-12-19 23:34:53 +03:00
|
|
|
while (isdigit((unsigned char)*cp))
|
1993-03-21 12:45:37 +03:00
|
|
|
size = size * 10 + (*cp++ - '0');
|
|
|
|
if (*cp++ != ' ') {
|
|
|
|
error("recvf: size not delimited\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mtime = 0;
|
1998-12-19 23:34:53 +03:00
|
|
|
while (isdigit((unsigned char)*cp))
|
1993-03-21 12:45:37 +03:00
|
|
|
mtime = mtime * 10 + (*cp++ - '0');
|
|
|
|
if (*cp++ != ' ') {
|
|
|
|
error("recvf: mtime not delimited\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
owner = cp;
|
|
|
|
while (*cp && *cp != ' ')
|
|
|
|
cp++;
|
|
|
|
if (*cp != ' ') {
|
|
|
|
error("recvf: owner name not delimited\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*cp++ = '\0';
|
|
|
|
group = cp;
|
|
|
|
while (*cp && *cp != ' ')
|
|
|
|
cp++;
|
|
|
|
if (*cp != ' ') {
|
|
|
|
error("recvf: group name not delimited\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*cp++ = '\0';
|
|
|
|
|
|
|
|
if (type == S_IFDIR) {
|
2009-04-13 08:35:36 +04:00
|
|
|
if (catname >= (int)sizeof(stp)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
error("%s:%s: too many directory levels\n",
|
|
|
|
host, target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
stp[catname] = tp;
|
|
|
|
if (catname++) {
|
|
|
|
*tp++ = '/';
|
1997-10-19 17:58:48 +04:00
|
|
|
while ((*tp++ = *cp++) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
;
|
|
|
|
tp--;
|
|
|
|
}
|
|
|
|
if (opts & VERIFY) {
|
|
|
|
ack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (lstat(target, &stb) == 0) {
|
1997-10-19 18:25:24 +04:00
|
|
|
if (S_ISDIR(stb.st_mode)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((stb.st_mode & 07777) == mode) {
|
|
|
|
ack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buf[0] = '\0';
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf + 1, sizeof(buf) - 1,
|
1999-04-20 11:53:02 +04:00
|
|
|
"%s: Warning: remote mode %o != local mode %o\n",
|
|
|
|
target, stb.st_mode & 07777, mode);
|
|
|
|
if (write(rem, buf, strlen(buf + 1) + 1) < 0)
|
|
|
|
error("write to remote failed: %s\n",
|
|
|
|
strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
errno = ENOTDIR;
|
1997-10-19 18:25:24 +04:00
|
|
|
} else if ((errno == ENOENT && mkdir(target, mode) == 0) ||
|
1997-10-19 17:58:48 +04:00
|
|
|
(chkparent(target) == 0 && mkdir(target, mode) == 0)) {
|
1997-10-19 18:25:24 +04:00
|
|
|
if (fchtogm(-1, target, mtime, owner, group, mode) == 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
ack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
error("%s:%s: %s\n", host, target, strerror(errno));
|
|
|
|
tp = stp[--catname];
|
|
|
|
*tp = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (catname)
|
1996-07-12 04:38:50 +04:00
|
|
|
(void) snprintf(tp, sizeof(target) - (tp - target), "/%s", cp);
|
1997-10-19 17:58:48 +04:00
|
|
|
cp = strrchr(target, '/');
|
1993-03-21 12:45:37 +03:00
|
|
|
if (cp == NULL)
|
2003-07-23 08:11:12 +04:00
|
|
|
strlcpy(new, tempname, sizeof(new));
|
1993-03-21 12:45:37 +03:00
|
|
|
else if (cp == target)
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(new, sizeof(new), "/%s", tempname);
|
1993-03-21 12:45:37 +03:00
|
|
|
else {
|
|
|
|
*cp = '\0';
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(new, sizeof(new), "%s/%s", target, tempname);
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp = '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type == S_IFLNK) {
|
|
|
|
int j;
|
|
|
|
|
|
|
|
ack();
|
|
|
|
cp = buf;
|
|
|
|
for (i = 0; i < size; i += j) {
|
|
|
|
if ((j = read(rem, cp, size - i)) <= 0)
|
1994-03-07 08:05:16 +03:00
|
|
|
cleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
cp += j;
|
|
|
|
}
|
|
|
|
*cp = '\0';
|
|
|
|
if (response() < 0) {
|
|
|
|
err();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (symlink(buf, new) < 0) {
|
|
|
|
if (errno != ENOENT || chkparent(new) < 0 ||
|
|
|
|
symlink(buf, new) < 0)
|
1994-03-07 08:05:16 +03:00
|
|
|
goto badnew1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
mode &= 0777;
|
|
|
|
if (opts & COMPARE) {
|
|
|
|
char tbuf[BUFSIZ];
|
|
|
|
|
|
|
|
if ((i = readlink(target, tbuf, BUFSIZ)) >= 0 &&
|
|
|
|
i == size && strncmp(buf, tbuf, size) == 0) {
|
|
|
|
(void) unlink(new);
|
|
|
|
ack();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (opts & VERIFY)
|
|
|
|
goto differ;
|
|
|
|
}
|
|
|
|
goto fixup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((f = creat(new, mode)) < 0) {
|
|
|
|
if (errno != ENOENT || chkparent(new) < 0 ||
|
|
|
|
(f = creat(new, mode)) < 0)
|
1994-03-07 08:05:16 +03:00
|
|
|
goto badnew1;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ack();
|
|
|
|
wrerr = 0;
|
|
|
|
for (i = 0; i < size; i += BUFSIZ) {
|
|
|
|
int amt = BUFSIZ;
|
|
|
|
|
|
|
|
cp = buf;
|
|
|
|
if (i + amt > size)
|
|
|
|
amt = size - i;
|
|
|
|
do {
|
|
|
|
int j = read(rem, cp, amt);
|
|
|
|
|
|
|
|
if (j <= 0) {
|
|
|
|
(void) close(f);
|
|
|
|
(void) unlink(new);
|
1994-03-07 08:05:16 +03:00
|
|
|
cleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
amt -= j;
|
|
|
|
cp += j;
|
|
|
|
} while (amt > 0);
|
|
|
|
amt = BUFSIZ;
|
|
|
|
if (i + amt > size)
|
|
|
|
amt = size - i;
|
|
|
|
if (wrerr == 0 && write(f, buf, amt) != amt) {
|
|
|
|
wrerr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (response() < 0) {
|
|
|
|
err();
|
1994-03-07 08:05:16 +03:00
|
|
|
goto badnew2;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1994-03-07 08:05:16 +03:00
|
|
|
if (wrerr)
|
|
|
|
goto badnew1;
|
1993-03-21 12:45:37 +03:00
|
|
|
if (opts & COMPARE) {
|
|
|
|
FILE *f1, *f2;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
if ((f1 = fopen(target, "r")) == NULL)
|
1994-03-07 08:05:16 +03:00
|
|
|
goto badtarget;
|
1993-03-21 12:45:37 +03:00
|
|
|
if ((f2 = fopen(new, "r")) == NULL) {
|
1994-03-07 08:05:16 +03:00
|
|
|
badnew1: error("%s:%s: %s\n", host, new, strerror(errno));
|
|
|
|
goto badnew2;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
while ((c = getc(f1)) == getc(f2))
|
|
|
|
if (c == EOF) {
|
|
|
|
(void) fclose(f1);
|
|
|
|
(void) fclose(f2);
|
|
|
|
ack();
|
1994-03-07 08:05:16 +03:00
|
|
|
goto badnew2;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
(void) fclose(f1);
|
|
|
|
(void) fclose(f2);
|
|
|
|
if (opts & VERIFY) {
|
1994-03-07 08:05:16 +03:00
|
|
|
differ: buf[0] = '\0';
|
1997-10-18 18:34:39 +04:00
|
|
|
(void)snprintf(buf + 1, sizeof(buf) - 1,
|
1996-07-12 04:06:32 +04:00
|
|
|
"need to update: %s\n",target);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) write(rem, buf, strlen(buf + 1) + 1);
|
1994-03-07 08:05:16 +03:00
|
|
|
goto badnew2;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-10-19 18:25:24 +04:00
|
|
|
if (fchtogm(f, new, mtime, owner, group, mode) < 0) {
|
1997-10-18 18:34:39 +04:00
|
|
|
badnew2:
|
1996-05-21 16:11:54 +04:00
|
|
|
if (f != -1)
|
|
|
|
(void) close(f);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) unlink(new);
|
|
|
|
return;
|
|
|
|
}
|
1994-03-07 08:05:16 +03:00
|
|
|
(void) close(f);
|
|
|
|
|
|
|
|
fixup: if (rename(new, target) < 0) {
|
|
|
|
badtarget: error("%s:%s: %s\n", host, target, strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) unlink(new);
|
|
|
|
return;
|
|
|
|
}
|
1994-03-07 08:05:16 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
if (opts & COMPARE) {
|
|
|
|
buf[0] = '\0';
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf + 1, sizeof(buf) - 1,
|
|
|
|
"updated %s\n", target);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) write(rem, buf, strlen(buf + 1) + 1);
|
|
|
|
} else
|
|
|
|
ack();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Creat a hard link to existing file.
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
static void
|
2002-06-14 05:18:54 +04:00
|
|
|
hardlink(char *cmd)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 17:58:48 +04:00
|
|
|
char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stat stb;
|
|
|
|
char *oldname;
|
|
|
|
int opts, exists = 0;
|
|
|
|
|
|
|
|
cp = cmd;
|
|
|
|
opts = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '7')
|
|
|
|
opts = (opts << 3) | (*cp++ - '0');
|
|
|
|
if (*cp++ != ' ') {
|
|
|
|
error("hardlink: options not delimited\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
oldname = cp;
|
|
|
|
while (*cp && *cp != ' ')
|
|
|
|
cp++;
|
|
|
|
if (*cp != ' ') {
|
|
|
|
error("hardlink: oldname name not delimited\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
*cp++ = '\0';
|
|
|
|
|
|
|
|
if (catname) {
|
1996-07-12 04:38:50 +04:00
|
|
|
(void) snprintf(tp, sizeof(target) - (tp - target), "/%s", cp);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
if (lstat(target, &stb) == 0) {
|
1997-10-19 18:25:24 +04:00
|
|
|
if (!S_ISREG(stb.st_mode) && !S_ISLNK(stb.st_mode)) {
|
|
|
|
error("%s: %s: not a regular file\n", host, target);
|
1993-03-21 12:45:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
exists = 1;
|
|
|
|
}
|
|
|
|
if (chkparent(target) < 0 ) {
|
|
|
|
error("%s:%s: %s (no parent)\n",
|
|
|
|
host, target, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (exists && (unlink(target) < 0)) {
|
|
|
|
error("%s:%s: %s (unlink)\n",
|
|
|
|
host, target, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (link(oldname, target) < 0) {
|
|
|
|
error("%s:can't link %s to %s\n",
|
|
|
|
host, target, oldname);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ack();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if parent directory exists and create one if not.
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
static int
|
2002-06-14 05:18:54 +04:00
|
|
|
chkparent(char *name)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 17:58:48 +04:00
|
|
|
char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stat stb;
|
|
|
|
|
1997-10-19 17:58:48 +04:00
|
|
|
cp = strrchr(name, '/');
|
1993-03-21 12:45:37 +03:00
|
|
|
if (cp == NULL || cp == name)
|
|
|
|
return(0);
|
|
|
|
*cp = '\0';
|
|
|
|
if (lstat(name, &stb) < 0) {
|
|
|
|
if (errno == ENOENT && chkparent(name) >= 0 &&
|
|
|
|
mkdir(name, 0777 & ~oumask) >= 0) {
|
|
|
|
*cp = '/';
|
|
|
|
return(0);
|
|
|
|
}
|
1997-10-19 18:25:24 +04:00
|
|
|
} else if (S_ISDIR(stb.st_mode)) {
|
1993-03-21 12:45:37 +03:00
|
|
|
*cp = '/';
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
*cp = '/';
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Change owner, group and mode of file.
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
static int
|
2002-06-14 05:18:54 +04:00
|
|
|
fchtogm(int fd, char *file, time_t mtime, char *owner, char *group, __mode_t mode)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 17:58:48 +04:00
|
|
|
int i;
|
1997-10-19 18:25:24 +04:00
|
|
|
struct timeval tv[2];
|
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
1993-03-21 12:45:37 +03:00
|
|
|
extern char user[];
|
|
|
|
|
|
|
|
uid = userid;
|
|
|
|
if (userid == 0) {
|
|
|
|
if (*owner == ':') {
|
|
|
|
uid = atoi(owner + 1);
|
|
|
|
} else if (pw == NULL || strcmp(owner, pw->pw_name) != 0) {
|
|
|
|
if ((pw = getpwnam(owner)) == NULL) {
|
|
|
|
if (mode & 04000) {
|
|
|
|
note("%s:%s: unknown login name, clearing setuid",
|
|
|
|
host, owner);
|
|
|
|
mode &= ~04000;
|
|
|
|
uid = 0;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
uid = pw->pw_uid;
|
|
|
|
} else
|
|
|
|
uid = pw->pw_uid;
|
|
|
|
if (*group == ':') {
|
|
|
|
gid = atoi(group + 1);
|
|
|
|
goto ok;
|
|
|
|
}
|
|
|
|
} else if ((mode & 04000) && strcmp(user, owner) != 0)
|
|
|
|
mode &= ~04000;
|
|
|
|
gid = -1;
|
|
|
|
if (gr == NULL || strcmp(group, gr->gr_name) != 0) {
|
|
|
|
if ((*group == ':' && (getgrgid(gid = atoi(group + 1)) == NULL))
|
|
|
|
|| ((gr = getgrnam(group)) == NULL)) {
|
|
|
|
if (mode & 02000) {
|
|
|
|
note("%s:%s: unknown group", host, group);
|
|
|
|
mode &= ~02000;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
gid = gr->gr_gid;
|
|
|
|
} else
|
|
|
|
gid = gr->gr_gid;
|
2006-12-18 18:14:42 +03:00
|
|
|
if (userid && gid != (gid_t)-1) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (gr) for (i = 0; gr->gr_mem[i] != NULL; i++)
|
|
|
|
if (!(strcmp(user, gr->gr_mem[i])))
|
|
|
|
goto ok;
|
|
|
|
mode &= ~02000;
|
|
|
|
gid = -1;
|
|
|
|
}
|
1997-10-19 18:25:24 +04:00
|
|
|
ok:
|
|
|
|
(void) gettimeofday(&tv[0], (struct timezone *)0);
|
|
|
|
tv[1].tv_sec = mtime;
|
|
|
|
tv[1].tv_usec = 0;
|
|
|
|
if (fd != -1 ? futimes(fd, tv) < 0 : utimes(file, tv) < 0)
|
|
|
|
note("%s: %s utimes: %s", host, file, strerror(errno));
|
|
|
|
if (fd != -1 ? fchown(fd, uid, gid) < 0 : chown(file, uid, gid) < 0)
|
1994-03-07 08:05:16 +03:00
|
|
|
note("%s: %s chown: %s", host, file, strerror(errno));
|
|
|
|
else if (mode & 07000 &&
|
1997-10-19 18:25:24 +04:00
|
|
|
(fd != -1 ? fchmod(fd, mode) < 0 : chmod(file, mode) < 0))
|
1994-03-07 08:05:16 +03:00
|
|
|
note("%s: %s chmod: %s", host, file, strerror(errno));
|
1993-03-21 12:45:37 +03:00
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for files on the machine being updated that are not on the master
|
|
|
|
* machine and remove them.
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
static void
|
2002-06-14 05:18:54 +04:00
|
|
|
rmchk(int opts)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-19 17:58:48 +04:00
|
|
|
char *cp, *s;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stat stb;
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
printf("rmchk()\n");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tell the remote to clean the files from the last directory sent.
|
|
|
|
*/
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf), "C%o\n", opts & VERIFY);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("buf = %s", buf);
|
|
|
|
(void) write(rem, buf, strlen(buf));
|
|
|
|
if (response() < 0)
|
|
|
|
return;
|
|
|
|
for (;;) {
|
|
|
|
cp = s = buf;
|
|
|
|
do {
|
|
|
|
if (read(rem, cp, 1) != 1)
|
1994-03-07 08:05:16 +03:00
|
|
|
lostconn(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
} while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
|
|
|
|
|
|
|
|
switch (*s++) {
|
|
|
|
case 'Q': /* Query if file should be removed */
|
|
|
|
/*
|
|
|
|
* Return the following codes to remove query.
|
|
|
|
* N\n -- file exists - DON'T remove.
|
|
|
|
* Y\n -- file doesn't exist - REMOVE.
|
|
|
|
*/
|
|
|
|
*--cp = '\0';
|
1996-07-12 04:38:50 +04:00
|
|
|
(void) snprintf(tp, sizeof(target) - (tp - target),
|
|
|
|
"/%s", s);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (debug)
|
|
|
|
printf("check %s\n", target);
|
|
|
|
if (except(target))
|
|
|
|
(void) write(rem, "N\n", 2);
|
|
|
|
else if (lstat(target, &stb) < 0)
|
|
|
|
(void) write(rem, "Y\n", 2);
|
|
|
|
else
|
|
|
|
(void) write(rem, "N\n", 2);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\0':
|
|
|
|
*--cp = '\0';
|
|
|
|
if (*s != '\0')
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "%s\n", s);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'E':
|
|
|
|
*tp = '\0';
|
|
|
|
ack();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case '\1':
|
|
|
|
case '\2':
|
|
|
|
nerrs++;
|
|
|
|
if (*s != '\n') {
|
|
|
|
if (!iamremote) {
|
|
|
|
fflush(stdout);
|
|
|
|
(void) write(2, s, cp - s);
|
|
|
|
}
|
|
|
|
if (lfp != NULL)
|
|
|
|
(void) fwrite(s, 1, cp - s, lfp);
|
|
|
|
}
|
|
|
|
if (buf[0] == '\2')
|
1994-03-07 08:05:16 +03:00
|
|
|
lostconn(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("rmchk: unexpected response '%s'\n", buf);
|
|
|
|
err();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the current directory (initialized by the 'T' command to server())
|
|
|
|
* for extraneous files and remove them.
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
static void
|
2002-06-14 05:18:54 +04:00
|
|
|
clean(char *cp)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
DIR *d;
|
1998-12-19 23:32:17 +03:00
|
|
|
struct dirent *dp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stat stb;
|
|
|
|
char *otp;
|
|
|
|
int len, opts;
|
|
|
|
|
|
|
|
opts = 0;
|
|
|
|
while (*cp >= '0' && *cp <= '7')
|
|
|
|
opts = (opts << 3) | (*cp++ - '0');
|
|
|
|
if (*cp != '\0') {
|
|
|
|
error("clean: options not delimited\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((d = opendir(target)) == NULL) {
|
|
|
|
error("%s:%s: %s\n", host, target, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ack();
|
|
|
|
|
|
|
|
otp = tp;
|
|
|
|
len = tp - target;
|
1997-10-19 17:58:48 +04:00
|
|
|
while ((dp = readdir(d)) != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
|
|
|
|
error("%s:%s/%s: Name too long\n",
|
|
|
|
host, target, dp->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tp = otp;
|
|
|
|
*tp++ = '/';
|
2003-01-20 08:29:53 +03:00
|
|
|
cp = dp->d_name;
|
1997-10-19 17:58:48 +04:00
|
|
|
while ((*tp++ = *cp++) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
;
|
|
|
|
tp--;
|
|
|
|
if (lstat(target, &stb) < 0) {
|
|
|
|
error("%s:%s: %s\n", host, target, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(buf, sizeof(buf), "Q%s\n", dp->d_name);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) write(rem, buf, strlen(buf));
|
|
|
|
cp = buf;
|
|
|
|
do {
|
|
|
|
if (read(rem, cp, 1) != 1)
|
1994-03-07 08:05:16 +03:00
|
|
|
cleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
} while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
|
|
|
|
*--cp = '\0';
|
|
|
|
cp = buf;
|
|
|
|
if (*cp != 'Y')
|
|
|
|
continue;
|
|
|
|
if (opts & VERIFY) {
|
|
|
|
cp = buf;
|
|
|
|
*cp++ = '\0';
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(cp, sizeof(buf) - 1,
|
|
|
|
"need to remove: %s\n", target);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) write(rem, buf, strlen(cp) + 1);
|
|
|
|
} else
|
|
|
|
removeit(&stb);
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
(void) write(rem, "E\n", 2);
|
|
|
|
(void) response();
|
|
|
|
tp = otp;
|
|
|
|
*tp = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a file or directory (recursively) and send back an acknowledge
|
|
|
|
* or an error message.
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
static void
|
2009-04-13 08:35:36 +04:00
|
|
|
removeit(struct stat *st)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
DIR *d;
|
1998-12-19 23:32:17 +03:00
|
|
|
struct dirent *dp;
|
1997-10-19 17:58:48 +04:00
|
|
|
char *cp;
|
1993-03-21 12:45:37 +03:00
|
|
|
struct stat stb;
|
|
|
|
char *otp;
|
|
|
|
int len;
|
|
|
|
|
2009-04-13 08:35:36 +04:00
|
|
|
switch (st->st_mode & S_IFMT) {
|
1993-03-21 12:45:37 +03:00
|
|
|
case S_IFREG:
|
|
|
|
case S_IFLNK:
|
|
|
|
if (unlink(target) < 0)
|
|
|
|
goto bad;
|
|
|
|
goto removed;
|
|
|
|
|
|
|
|
case S_IFDIR:
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
error("%s:%s: not a plain file\n", host, target);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((d = opendir(target)) == NULL)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
otp = tp;
|
|
|
|
len = tp - target;
|
1997-10-19 17:58:48 +04:00
|
|
|
while ((dp = readdir(d)) != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
|
|
|
|
continue;
|
|
|
|
if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
|
|
|
|
error("%s:%s/%s: Name too long\n",
|
|
|
|
host, target, dp->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
tp = otp;
|
|
|
|
*tp++ = '/';
|
2003-01-20 08:29:53 +03:00
|
|
|
cp = dp->d_name;
|
1997-10-19 17:58:48 +04:00
|
|
|
while ((*tp++ = *cp++) != 0)
|
1993-03-21 12:45:37 +03:00
|
|
|
;
|
|
|
|
tp--;
|
|
|
|
if (lstat(target, &stb) < 0) {
|
|
|
|
error("%s:%s: %s\n", host, target, strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
removeit(&stb);
|
|
|
|
}
|
|
|
|
closedir(d);
|
|
|
|
tp = otp;
|
|
|
|
*tp = '\0';
|
|
|
|
if (rmdir(target) < 0) {
|
|
|
|
bad:
|
|
|
|
error("%s:%s: %s\n", host, target, strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
removed:
|
|
|
|
cp = buf;
|
|
|
|
*cp++ = '\0';
|
1996-07-12 04:06:32 +04:00
|
|
|
(void) snprintf(cp, sizeof(buf) - 1, "removed %s\n", target);
|
1993-03-21 12:45:37 +03:00
|
|
|
(void) write(rem, buf, strlen(cp) + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a shell command to handle special cases.
|
|
|
|
*/
|
1994-03-07 08:05:16 +03:00
|
|
|
static void
|
2002-06-14 05:18:54 +04:00
|
|
|
dospecial(char *cmd)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
int fd[2], status, pid, i;
|
1997-10-19 17:58:48 +04:00
|
|
|
char *cp, *s;
|
1993-03-21 12:45:37 +03:00
|
|
|
char sbuf[BUFSIZ];
|
|
|
|
|
|
|
|
if (pipe(fd) < 0) {
|
|
|
|
error("%s\n", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((pid = fork()) == 0) {
|
|
|
|
/*
|
|
|
|
* Return everything the shell commands print.
|
|
|
|
*/
|
|
|
|
(void) close(0);
|
|
|
|
(void) close(1);
|
|
|
|
(void) close(2);
|
|
|
|
(void) open(_PATH_DEVNULL, O_RDONLY);
|
|
|
|
(void) dup(fd[1]);
|
|
|
|
(void) dup(fd[1]);
|
|
|
|
(void) close(fd[0]);
|
|
|
|
(void) close(fd[1]);
|
|
|
|
setgid(groupid);
|
|
|
|
setuid(userid);
|
2006-05-11 04:22:52 +04:00
|
|
|
execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
|
1993-03-21 12:45:37 +03:00
|
|
|
_exit(127);
|
|
|
|
}
|
|
|
|
(void) close(fd[1]);
|
|
|
|
s = sbuf;
|
|
|
|
*s++ = '\0';
|
|
|
|
while ((i = read(fd[0], buf, sizeof(buf))) > 0) {
|
|
|
|
cp = buf;
|
|
|
|
do {
|
|
|
|
*s++ = *cp++;
|
|
|
|
if (cp[-1] != '\n') {
|
|
|
|
if (s < &sbuf[sizeof(sbuf)-1])
|
|
|
|
continue;
|
|
|
|
*s++ = '\n';
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Throw away blank lines.
|
|
|
|
*/
|
|
|
|
if (s == &sbuf[2]) {
|
|
|
|
s--;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
(void) write(rem, sbuf, s - sbuf);
|
|
|
|
s = &sbuf[1];
|
|
|
|
} while (--i);
|
|
|
|
}
|
|
|
|
if (s > &sbuf[1]) {
|
|
|
|
*s++ = '\n';
|
|
|
|
(void) write(rem, sbuf, s - sbuf);
|
|
|
|
}
|
|
|
|
while ((i = wait(&status)) != pid && i != -1)
|
|
|
|
;
|
|
|
|
if (i == -1)
|
|
|
|
status = -1;
|
|
|
|
(void) close(fd[0]);
|
|
|
|
if (status)
|
|
|
|
error("shell returned %d\n", status);
|
|
|
|
else
|
|
|
|
ack();
|
|
|
|
}
|
|
|
|
|
1994-03-07 08:05:16 +03:00
|
|
|
|
|
|
|
void
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(FILE *fp, const char *fmt, ...)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-03-07 08:05:16 +03:00
|
|
|
va_list ap;
|
2001-09-24 17:22:25 +04:00
|
|
|
|
|
|
|
/* Print changes locally if not quiet mode */
|
|
|
|
if (!qflag) {
|
|
|
|
va_start(ap, fmt);
|
1994-03-07 08:05:16 +03:00
|
|
|
(void)vprintf(fmt, ap);
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/* Save changes (for mailing) if really updating files */
|
2001-09-24 17:22:25 +04:00
|
|
|
if (!(options & VERIFY) && fp != NULL) {
|
|
|
|
va_start(ap, fmt);
|
1994-03-07 08:05:16 +03:00
|
|
|
(void)vfprintf(fp, fmt, ap);
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-03-07 08:05:16 +03:00
|
|
|
void
|
|
|
|
error(const char *fmt, ...)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
static FILE *fp;
|
1994-03-07 08:05:16 +03:00
|
|
|
va_list ap;
|
2001-09-24 17:22:25 +04:00
|
|
|
|
|
|
|
++nerrs;
|
|
|
|
if (!fp && !(fp = fdopen(rem, "w")))
|
|
|
|
return;
|
1994-03-07 08:05:16 +03:00
|
|
|
va_start(ap, fmt);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (iamremote) {
|
|
|
|
(void)fprintf(fp, "%crdist: ", 0x01);
|
1994-03-07 08:05:16 +03:00
|
|
|
(void)vfprintf(fp, fmt, ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
fflush(fp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fflush(stdout);
|
|
|
|
(void)fprintf(stderr, "rdist: ");
|
1994-03-07 08:05:16 +03:00
|
|
|
(void)vfprintf(stderr, fmt, ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (lfp != NULL) {
|
|
|
|
(void)fprintf(lfp, "rdist: ");
|
2001-09-24 17:22:25 +04:00
|
|
|
va_start(ap, fmt);
|
1994-03-07 08:05:16 +03:00
|
|
|
(void)vfprintf(lfp, fmt, ap);
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
fflush(lfp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1994-03-07 08:05:16 +03:00
|
|
|
void
|
|
|
|
fatal(const char *fmt, ...)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
static FILE *fp;
|
1994-03-07 08:05:16 +03:00
|
|
|
va_list ap;
|
2001-09-24 17:22:25 +04:00
|
|
|
|
|
|
|
++nerrs;
|
|
|
|
if (!fp && !(fp = fdopen(rem, "w")))
|
|
|
|
return;
|
1994-03-07 08:05:16 +03:00
|
|
|
va_start(ap, fmt);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (iamremote) {
|
|
|
|
(void)fprintf(fp, "%crdist: ", 0x02);
|
1994-03-07 08:05:16 +03:00
|
|
|
(void)vfprintf(fp, fmt, ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
fflush(fp);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fflush(stdout);
|
|
|
|
(void)fprintf(stderr, "rdist: ");
|
1994-03-07 08:05:16 +03:00
|
|
|
(void)vfprintf(stderr, fmt, ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
fflush(stderr);
|
|
|
|
}
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
if (lfp != NULL) {
|
|
|
|
(void)fprintf(lfp, "rdist: ");
|
2001-09-24 17:22:25 +04:00
|
|
|
va_start(ap, fmt);
|
1994-03-07 08:05:16 +03:00
|
|
|
(void)vfprintf(lfp, fmt, ap);
|
2001-09-24 17:22:25 +04:00
|
|
|
va_end(ap);
|
1993-03-21 12:45:37 +03:00
|
|
|
fflush(lfp);
|
|
|
|
}
|
1994-03-07 08:05:16 +03:00
|
|
|
cleanup(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-03-07 08:05:16 +03:00
|
|
|
static int
|
2002-06-14 05:18:54 +04:00
|
|
|
response(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
char *cp, *s;
|
|
|
|
char resp[BUFSIZ];
|
|
|
|
|
|
|
|
if (debug)
|
|
|
|
printf("response()\n");
|
|
|
|
|
|
|
|
cp = s = resp;
|
|
|
|
do {
|
|
|
|
if (read(rem, cp, 1) != 1)
|
1994-03-07 08:05:16 +03:00
|
|
|
lostconn(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
} while (*cp++ != '\n' && cp < &resp[BUFSIZ]);
|
|
|
|
|
|
|
|
switch (*s++) {
|
|
|
|
case '\0':
|
|
|
|
*--cp = '\0';
|
|
|
|
if (*s != '\0') {
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "%s\n", s);
|
1993-03-21 12:45:37 +03:00
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
case '\3':
|
|
|
|
*--cp = '\0';
|
2002-12-06 06:09:28 +03:00
|
|
|
dolog(lfp, "Note: %s\n",s);
|
1993-03-21 12:45:37 +03:00
|
|
|
return(response());
|
|
|
|
|
|
|
|
default:
|
|
|
|
s--;
|
|
|
|
/* fall into... */
|
|
|
|
case '\1':
|
|
|
|
case '\2':
|
|
|
|
nerrs++;
|
|
|
|
if (*s != '\n') {
|
|
|
|
if (!iamremote) {
|
|
|
|
fflush(stdout);
|
|
|
|
(void) write(2, s, cp - s);
|
|
|
|
}
|
|
|
|
if (lfp != NULL)
|
|
|
|
(void) fwrite(s, 1, cp - s, lfp);
|
|
|
|
}
|
|
|
|
if (resp[0] == '\2')
|
1994-03-07 08:05:16 +03:00
|
|
|
lostconn(0);
|
1993-03-21 12:45:37 +03:00
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove temporary files and do any cleanup operations before exiting.
|
|
|
|
*/
|
|
|
|
void
|
2006-12-18 18:14:42 +03:00
|
|
|
/*ARGSUSED*/
|
|
|
|
cleanup(int signo __unused)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
|
|
|
(void) unlink(tempfile);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
1994-03-07 08:05:16 +03:00
|
|
|
static void
|
|
|
|
note(const char *fmt, ...)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
2009-04-13 08:35:36 +04:00
|
|
|
static char nbuf[BUFSIZ];
|
1994-03-07 08:05:16 +03:00
|
|
|
va_list ap;
|
2002-06-14 05:18:54 +04:00
|
|
|
|
1994-03-07 08:05:16 +03:00
|
|
|
va_start(ap, fmt);
|
2009-04-13 08:35:36 +04:00
|
|
|
(void)vsnprintf(nbuf, sizeof(nbuf), fmt, ap);
|
1994-03-07 08:05:16 +03:00
|
|
|
va_end(ap);
|
2009-04-13 08:35:36 +04:00
|
|
|
comment(nbuf);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-03-07 08:05:16 +03:00
|
|
|
static void
|
2002-06-14 05:18:54 +04:00
|
|
|
comment(char *s)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-03-07 08:05:16 +03:00
|
|
|
char c;
|
|
|
|
|
|
|
|
c = '\3';
|
1993-03-21 12:45:37 +03:00
|
|
|
write(rem, &c, 1);
|
|
|
|
write(rem, s, strlen(s));
|
|
|
|
c = '\n';
|
|
|
|
write(rem, &c, 1);
|
|
|
|
}
|