2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)server.c 8.1 (Berkeley) 6/9/93
34 * $FreeBSD: src/usr.bin/rdist/server.c,v 1.10 1999/08/28 01:05:09 peter Exp $
35 * $DragonFly: src/usr.bin/rdist/server.c,v 1.3 2003/11/03 19:31:31 eirikn Exp $
41 #define ack() (void) write(rem, "\0\n", 2)
42 #define err() (void) write(rem, "\1\n", 2)
44 struct linkbuf *ihead = NULL; /* list of files with more than one link */
45 char buf[BUFSIZ]; /* general purpose buffer */
46 char target[BUFSIZ]; /* target/source directory name */
47 char source[BUFSIZ]; /* source directory name */
48 char *tp; /* pointer to end of target name */
49 char *Tdest; /* pointer to last T dest*/
50 int catname; /* cat name to target name */
51 char *stp[32]; /* stack of saved tp's for directories */
52 int oumask; /* old umask for creating files */
54 extern FILE *lfp; /* log file for mailing changes */
56 static int chkparent(char *);
57 static void clean(char *);
58 static void comment(char *);
59 static void dospecial(char *);
60 static int fchog(int, char *, char *, char *, int);
61 static void hardlink(char *);
62 static void note(const char *, ...);
63 static void query(char *);
64 static void recvf(char *, int);
65 static void removeit(struct stat *);
66 static int response(void);
67 static void rmchk(int);
68 static struct linkbuf *
69 savelink(struct stat *);
70 static void sendf(char *, int);
71 static int update(char *, int, struct stat *);
74 * Server routine to read requests and process them.
76 * Tname - Transmit file if out of date
77 * Vname - Verify if file out of date or not
78 * Qname - Query if file exists. Return mtime & size if it does.
86 signal(SIGHUP, cleanup);
87 signal(SIGINT, cleanup);
88 signal(SIGQUIT, cleanup);
89 signal(SIGTERM, cleanup);
90 signal(SIGPIPE, cleanup);
94 (void) snprintf(buf, sizeof(buf), "V%d\n", VERSION);
95 (void) write(rem, buf, strlen(buf));
99 if (read(rem, cp, 1) <= 0)
102 error("server: expected control record\n");
106 if (read(rem, cp, 1) != 1)
108 } while (*cp++ != '\n' && cp < &cmdbuf[BUFSIZ]);
112 case 'T': /* init target file/directory name */
113 catname = 1; /* target should be directory */
116 case 't': /* init target file/directory name */
119 if (exptilde(target, cp, sizeof(target)) == NULL)
127 case 'R': /* Transfer a regular file. */
131 case 'D': /* Transfer a directory. */
135 case 'K': /* Transfer symbolic link. */
139 case 'k': /* Transfer hard link. */
143 case 'E': /* End. (of directory) */
146 error("server: too many 'E's\n");
154 case 'C': /* Clean. Cleanup a directory */
158 case 'Q': /* Query. Does the file/directory exist? */
162 case 'S': /* Special. Execute commands */
168 * These entries are reserved but not currently used.
169 * The intent is to allow remote hosts to have master copies.
170 * Currently, only the host rdist runs on can have masters.
172 case 'X': /* start a new list of files to exclude */
174 case 'x': /* add name to list of files to exclude */
180 if (exptilde(buf, cp, sizeof(buf)) == NULL)
185 except = bp = expand(makeblock(NAME, cp), E_VARS);
187 bp->b_next = expand(makeblock(NAME, cp), E_VARS);
188 while (bp->b_next != NULL)
193 case 'I': /* Install. Transfer file if out of date. */
195 while (*cp >= '0' && *cp <= '7')
196 opts = (opts << 3) | (*cp++ - '0');
198 error("server: options not delimited\n");
204 case 'L': /* Log. save message in log file */
217 error("server: unknown command '%s'\n", cp);
225 * Update the file(s) if they are different.
226 * destdir = 1 if destination should be a directory
227 * (i.e., more than one source is being copied to the same destination).
230 install(src, dest, destdir, opts)
235 char destcopy[BUFSIZ];
243 opts &= ~WHOLE; /* WHOLE mode only useful if renaming */
247 if (nflag || debug) {
248 printf("%s%s%s%s%s %s %s\n", opts & VERIFY ? "verify":"install",
249 opts & WHOLE ? " -w" : "",
250 opts & YOUNGER ? " -y" : "",
251 opts & COMPARE ? " -b" : "",
252 opts & REMOVE ? " -R" : "", src, dest);
257 rname = exptilde(target, src, sizeof(target));
264 * If we are renaming a directory and we want to preserve
265 * the directory heirarchy (-w), we must strip off the leading
266 * directory name and preserve the rest.
269 while (*rname == '/')
273 rname = rindex(target, '/');
280 printf("target = %s, rname = %s\n", target, rname);
282 * Pass the destination file/directory name to remote.
284 (void) snprintf(buf, sizeof(buf), "%c%s\n", destdir ? 'T' : 't', dest);
286 printf("buf = %s", buf);
287 (void) write(rem, buf, strlen(buf));
292 * Save the name of the remote target destination if we are
293 * in WHOLE mode (destdir > 0) or if the source and destination
294 * are not the same. This info will be used later for maintaining
297 if (destdir || (src && dest && strcmp(src, dest))) {
298 strcpy(destcopy, dest);
306 remotename(pathname, src)
315 if (0 == strncmp(pathname, src, len))
323 installlink(lp, rname, opts)
328 if (*lp->target == 0)
329 (void) snprintf(buf, sizeof(buf), "k%o %s %s\n",
330 opts, lp->pathname, rname);
332 (void) snprintf(buf, sizeof(buf), "k%o %s/%s %s\n",
334 remotename(lp->pathname, lp->src), rname);
337 printf("lp->src = %s\n", lp->src);
338 printf("lp->target = %s\n", lp->target);
339 printf("lp->pathname = %s\n", lp->pathname);
340 printf("rname = %s\n", rname);
341 printf("buf = %s", buf);
343 (void) write(rem, buf, strlen(buf));
347 #define protoname() (pw ? pw->pw_name : user)
348 #define protogroup() (gr ? gr->gr_name : group)
350 * Transfer the file or directory in target[].
351 * rname is the name of the file on the remote host.
358 register struct subcmd *sc;
360 int sizerr, f, u, len;
365 extern struct subcmd *subcmds;
366 static char user[15], group[15];
369 printf("sendf(%s, %x)\n", rname, opts);
373 if ((opts & FOLLOW ? stat(target, &stb) : lstat(target, &stb)) < 0) {
374 error("%s: %s\n", target, strerror(errno));
377 if ((u = update(rname, opts, &stb)) == 0) {
378 if ((stb.st_mode & S_IFMT) == S_IFREG && stb.st_nlink > 1)
379 (void) savelink(&stb);
383 if (pw == NULL || pw->pw_uid != stb.st_uid)
384 if ((pw = getpwuid(stb.st_uid)) == NULL) {
385 log(lfp, "%s: no password entry for uid %d \n",
388 (void)snprintf(user, sizeof(user), ":%lu", stb.st_uid);
390 if (gr == NULL || gr->gr_gid != stb.st_gid)
391 if ((gr = getgrgid(stb.st_gid)) == NULL) {
392 log(lfp, "%s: no name for group %d\n",
395 (void)snprintf(group, sizeof(group), ":%lu", stb.st_gid);
399 log(lfp, "need to install: %s\n", target);
402 log(lfp, "installing: %s\n", target);
403 opts &= ~(COMPARE|REMOVE);
406 switch (stb.st_mode & S_IFMT) {
408 if ((d = opendir(target)) == NULL) {
409 error("%s: %s\n", target, strerror(errno));
412 (void) snprintf(buf, sizeof(buf), "D%o %04o 0 0 %s %s %s\n",
413 opts, stb.st_mode & 07777, protoname(), protogroup(),
416 printf("buf = %s", buf);
417 (void) write(rem, buf, strlen(buf));
418 if (response() < 0) {
428 while ((dp = readdir(d))) {
429 if (!strcmp(dp->d_name, ".") ||
430 !strcmp(dp->d_name, ".."))
432 if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
433 error("%s/%s: Name too long\n", target,
440 while ((*tp++ = *cp++))
443 sendf(dp->d_name, opts);
446 (void) write(rem, "E\n", 2);
455 if (stb.st_nlink > 1) {
458 if ((lp = savelink(&stb)) != NULL) {
459 installlink(lp, rname, opts);
463 (void) snprintf(buf, sizeof(buf),
464 "K%o %o %qd %ld %s %s %s\n", opts,
465 stb.st_mode & 07777, stb.st_size, stb.st_mtime,
466 protoname(), protogroup(), rname);
468 printf("buf = %s", buf);
469 (void) write(rem, buf, strlen(buf));
472 sizerr = (readlink(target, buf, BUFSIZ - 1) != stb.st_size);
473 (void) write(rem, buf, stb.st_size);
475 printf("readlink = %.*s\n", (int)stb.st_size, buf);
482 error("%s: not a file or directory\n", target);
488 log(lfp, "need to update: %s\n", target);
491 log(lfp, "updating: %s\n", target);
494 if (stb.st_nlink > 1) {
497 if ((lp = savelink(&stb)) != NULL) {
498 installlink(lp, rname, opts);
503 if ((f = open(target, O_RDONLY, 0)) < 0) {
504 error("%s: %s\n", target, strerror(errno));
507 (void) snprintf(buf, sizeof(buf), "R%o %o %qd %ld %s %s %s\n", opts,
508 stb.st_mode & 07777, stb.st_size, stb.st_mtime,
509 protoname(), protogroup(), rname);
511 printf("buf = %s", buf);
512 (void) write(rem, buf, strlen(buf));
513 if (response() < 0) {
518 for (i = 0; i < stb.st_size; i += BUFSIZ) {
520 if (i + amt > stb.st_size)
521 amt = stb.st_size - i;
522 if (sizerr == 0 && read(f, buf, amt) != amt)
524 (void) write(rem, buf, amt);
529 error("%s: file changed size\n", target);
534 if (f < 0 || (f == 0 && (opts & COMPARE)))
537 for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
538 if (sc->sc_type != SPECIAL)
540 if (sc->sc_args != NULL && !inlist(sc->sc_args, target))
542 log(lfp, "special \"%s\"\n", sc->sc_name);
545 (void) snprintf(buf, sizeof(buf), "SFILE=%s;%s\n", target,
548 printf("buf = %s", buf);
549 (void) write(rem, buf, strlen(buf));
550 while (response() > 0)
555 static struct linkbuf *
561 for (lp = ihead; lp != NULL; lp = lp->nextp)
562 if (lp->inum == stp->st_ino && lp->devnum == stp->st_dev) {
566 lp = (struct linkbuf *) malloc(sizeof(*lp));
568 log(lfp, "out of memory, link information lost\n");
572 lp->inum = stp->st_ino;
573 lp->devnum = stp->st_dev;
574 lp->count = stp->st_nlink - 1;
575 strcpy(lp->pathname, target);
576 strcpy(lp->src, source);
578 strcpy(lp->target, Tdest);
586 * Check to see if file needs to be updated on the remote machine.
587 * Returns 0 if no update, 1 if remote doesn't exist, 2 if out of date
588 * and 3 if comparing binaries to determine if out of date.
591 update(rname, opts, stp)
596 register char *cp, *s;
598 register time_t mtime;
601 printf("update(%s, %x, %p)\n", rname, opts, stp);
604 * Check to see if the file exists on the remote machine.
606 (void) snprintf(buf, sizeof(buf), "Q%s\n", rname);
608 printf("buf = %s", buf);
609 (void) write(rem, buf, strlen(buf));
613 if (read(rem, cp, 1) != 1)
615 } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
621 case 'N': /* file doesn't exist so install it */
629 (void) write(2, s, cp - s);
632 (void) fwrite(s, 1, cp - s, lfp);
639 log(lfp, "update: note: %s\n", s);
644 error("update: unexpected response '%s'\n", s);
656 size = size * 10 + (*s++ - '0');
658 error("update: size not delimited\n");
663 mtime = mtime * 10 + (*s++ - '0');
665 error("update: mtime not delimited\n");
669 * File needs to be updated?
671 if (opts & YOUNGER) {
672 if (stp->st_mtime == mtime)
674 if (stp->st_mtime < mtime) {
675 log(lfp, "Warning: %s: remote copy is newer\n", target);
678 } else if (stp->st_mtime == mtime && stp->st_size == size)
684 * Query. Check to see if file exists. Return one of the following:
685 * N\n - doesn't exist
686 * Ysize mtime\n - exists and its a regular file (size & mtime of file)
687 * Y\n - exists and its a directory or symbolic link
697 (void) snprintf(tp, sizeof(target) - (tp - target), "/%s",
700 if (lstat(target, &stb) < 0) {
702 (void) write(rem, "N\n", 2);
704 error("%s:%s: %s\n", host, target, strerror(errno));
709 switch (stb.st_mode & S_IFMT) {
711 (void) snprintf(buf, sizeof(buf), "Y%qd %ld\n", stb.st_size,
713 (void) write(rem, buf, strlen(buf));
718 (void) write(rem, "Y\n", 2);
722 error("%s: not a file or directory\n", name);
734 int f = -1, mode, opts, wrerr, olderrno;
738 struct timeval tvp[2];
741 extern char *tempname;
745 while (*cp >= '0' && *cp <= '7')
746 opts = (opts << 3) | (*cp++ - '0');
748 error("recvf: options not delimited\n");
752 while (*cp >= '0' && *cp <= '7')
753 mode = (mode << 3) | (*cp++ - '0');
755 error("recvf: mode not delimited\n");
760 size = size * 10 + (*cp++ - '0');
762 error("recvf: size not delimited\n");
767 mtime = mtime * 10 + (*cp++ - '0');
769 error("recvf: mtime not delimited\n");
773 while (*cp && *cp != ' ')
776 error("recvf: owner name not delimited\n");
781 while (*cp && *cp != ' ')
784 error("recvf: group name not delimited\n");
789 if (type == S_IFDIR) {
790 if (catname >= sizeof(stp)) {
791 error("%s:%s: too many directory levels\n",
798 while ((*tp++ = *cp++))
806 if (lstat(target, &stb) == 0) {
807 if (ISDIR(stb.st_mode)) {
808 if ((stb.st_mode & 07777) == mode) {
813 (void) snprintf(buf + 1, sizeof(buf) - 1,
814 "%s: Warning: remote mode %o != local mode %o\n",
815 target, stb.st_mode & 07777, mode);
816 (void) write(rem, buf, strlen(buf + 1) + 1);
820 } else if ((errno == ENOENT && mkdir(target, mode) == 0) ||
821 (chkparent(target) == 0 && mkdir(target, mode) == 0)) {
822 if (fchog(-1, target, owner, group, mode) == 0)
826 error("%s:%s: %s\n", host, target, strerror(errno));
833 (void) snprintf(tp, sizeof(target) - (tp - target), "/%s", cp);
834 cp = rindex(target, '/');
836 strcpy(new, tempname);
837 else if (cp == target)
838 (void) snprintf(new, sizeof(new), "/%s", tempname);
841 (void) snprintf(new, sizeof(new), "%s/%s", target, tempname);
845 if (type == S_IFLNK) {
850 for (i = 0; i < size; i += j) {
851 if ((j = read(rem, cp, size - i)) <= 0)
856 if (response() < 0) {
860 if (symlink(buf, new) < 0) {
861 if (errno != ENOENT || chkparent(new) < 0 ||
862 symlink(buf, new) < 0)
866 if (opts & COMPARE) {
869 if ((i = readlink(target, tbuf, BUFSIZ - 1)) >= 0 &&
870 i == size && strncmp(buf, tbuf, size) == 0) {
881 if ((f = creat(new, mode)) < 0) {
882 if (errno != ENOENT || chkparent(new) < 0 ||
883 (f = creat(new, mode)) < 0)
889 for (i = 0; i < size; i += BUFSIZ) {
896 int j = read(rem, cp, amt);
909 if (wrerr == 0 && write(f, buf, amt) != amt) {
914 if (response() < 0) {
920 if (opts & COMPARE) {
924 if ((f1 = fopen(target, "r")) == NULL)
926 if ((f2 = fopen(new, "r")) == NULL) {
927 badnew1: error("%s:%s: %s\n", host, new, strerror(errno));
930 while ((c = getc(f1)) == getc(f2))
940 differ: buf[0] = '\0';
941 (void) snprintf(buf + 1, sizeof(buf) - 1,
942 "need to update: %s\n",target);
943 (void) write(rem, buf, strlen(buf + 1) + 1);
949 * Set last modified time
951 tvp[0].tv_sec = time(0);
953 tvp[1].tv_sec = mtime;
955 if (utimes(new, tvp) < 0)
956 note("%s: utimes failed %s: %s\n", host, new, strerror(errno));
958 if (fchog(f, new, owner, group, mode) < 0) {
959 badnew2: if (f != -1) (void) close(f);
965 fixup: if (rename(new, target) < 0) {
966 badtarget: error("%s:%s: %s\n", host, target, strerror(errno));
971 if (opts & COMPARE) {
973 (void) snprintf(buf + 1, sizeof(buf) - 1,
974 "updated %s\n", target);
975 (void) write(rem, buf, strlen(buf + 1) + 1);
981 * Creat a hard link to existing file.
990 int opts, exists = 0;
994 while (*cp >= '0' && *cp <= '7')
995 opts = (opts << 3) | (*cp++ - '0');
997 error("hardlink: options not delimited\n");
1001 while (*cp && *cp != ' ')
1004 error("hardlink: oldname name not delimited\n");
1010 (void) snprintf(tp, sizeof(target) - (tp - target), "/%s", cp);
1012 if (lstat(target, &stb) == 0) {
1013 int mode = stb.st_mode & S_IFMT;
1014 if (mode != S_IFREG && mode != S_IFLNK) {
1015 error("%s:%s: not a regular file\n", host, target);
1020 if (chkparent(target) < 0 ) {
1021 error("%s:%s: %s (no parent)\n",
1022 host, target, strerror(errno));
1025 if (exists && (unlink(target) < 0)) {
1026 error("%s:%s: %s (unlink)\n",
1027 host, target, strerror(errno));
1030 if (link(oldname, target) < 0) {
1031 error("%s:can't link %s to %s\n",
1032 host, target, oldname);
1039 * Check to see if parent directory exists and create one if not.
1048 cp = rindex(name, '/');
1049 if (cp == NULL || cp == name)
1052 if (lstat(name, &stb) < 0) {
1053 if (errno == ENOENT && chkparent(name) >= 0 &&
1054 mkdir(name, 0777 & ~oumask) >= 0) {
1058 } else if (ISDIR(stb.st_mode)) {
1067 * Change owner, group and mode of file.
1070 fchog(fd, file, owner, group, mode)
1072 char *file, *owner, *group;
1082 if (*owner == ':') {
1083 uid = atoi(owner + 1);
1084 } else if (pw == NULL || strcmp(owner, pw->pw_name) != 0) {
1085 if ((pw = getpwnam(owner)) == NULL) {
1087 note("%s:%s: unknown login name, clearing setuid",
1096 if (*group == ':') {
1097 gid = atoi(group + 1);
1100 } else if ((mode & 04000) && strcmp(user, owner) != 0)
1103 if (gr == NULL || strcmp(group, gr->gr_name) != 0) {
1104 if ((*group == ':' && (getgrgid(gid = atoi(group + 1)) == NULL))
1105 || ((gr = getgrnam(group)) == NULL)) {
1107 note("%s:%s: unknown group", host, group);
1114 if (userid && gid >= 0) {
1115 if (gr) for (i = 0; gr->gr_mem[i] != NULL; i++)
1116 if (!(strcmp(user, gr->gr_mem[i])))
1121 ok: if ((fd != -1 && fchown(fd, uid, gid) < 0) || chown(file, uid, gid) < 0)
1122 note("%s: %s chown: %s", host, file, strerror(errno));
1123 else if (mode & 07000 &&
1124 ((fd != -1 && fchmod(fd, mode) < 0) || chmod(file, mode) < 0))
1125 note("%s: %s chmod: %s", host, file, strerror(errno));
1130 * Check for files on the machine being updated that are not on the master
1131 * machine and remove them.
1137 register char *cp, *s;
1141 printf("rmchk()\n");
1144 * Tell the remote to clean the files from the last directory sent.
1146 (void) snprintf(buf, sizeof(buf), "C%o\n", opts & VERIFY);
1148 printf("buf = %s", buf);
1149 (void) write(rem, buf, strlen(buf));
1155 if (read(rem, cp, 1) != 1)
1157 } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
1160 case 'Q': /* Query if file should be removed */
1162 * Return the following codes to remove query.
1163 * N\n -- file exists - DON'T remove.
1164 * Y\n -- file doesn't exist - REMOVE.
1167 (void) snprintf(tp, sizeof(target) - (tp - target),
1170 printf("check %s\n", target);
1172 (void) write(rem, "N\n", 2);
1173 else if (lstat(target, &stb) < 0)
1174 (void) write(rem, "Y\n", 2);
1176 (void) write(rem, "N\n", 2);
1182 log(lfp, "%s\n", s);
1196 (void) write(2, s, cp - s);
1199 (void) fwrite(s, 1, cp - s, lfp);
1206 error("rmchk: unexpected response '%s'\n", buf);
1213 * Check the current directory (initialized by the 'T' command to server())
1214 * for extraneous files and remove them.
1221 register struct dirent *dp;
1227 while (*cp >= '0' && *cp <= '7')
1228 opts = (opts << 3) | (*cp++ - '0');
1230 error("clean: options not delimited\n");
1233 if ((d = opendir(target)) == NULL) {
1234 error("%s:%s: %s\n", host, target, strerror(errno));
1241 while ((dp = readdir(d))) {
1242 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
1244 if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
1245 error("%s:%s/%s: Name too long\n",
1246 host, target, dp->d_name);
1252 while ((*tp++ = *cp++))
1255 if (lstat(target, &stb) < 0) {
1256 error("%s:%s: %s\n", host, target, strerror(errno));
1259 (void) snprintf(buf, sizeof(buf), "Q%s\n", dp->d_name);
1260 (void) write(rem, buf, strlen(buf));
1263 if (read(rem, cp, 1) != 1)
1265 } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
1270 if (opts & VERIFY) {
1273 (void) snprintf(cp, sizeof(buf) - 1,
1274 "need to remove: %s\n", target);
1275 (void) write(rem, buf, strlen(cp) + 1);
1280 (void) write(rem, "E\n", 2);
1287 * Remove a file or directory (recursively) and send back an acknowledge
1288 * or an error message.
1301 switch (stp->st_mode & S_IFMT) {
1304 if (unlink(target) < 0)
1312 error("%s:%s: not a plain file\n", host, target);
1316 if ((d = opendir(target)) == NULL)
1321 while ((dp = readdir(d))) {
1322 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
1324 if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
1325 error("%s:%s/%s: Name too long\n",
1326 host, target, dp->d_name);
1332 while ((*tp++ = *cp++))
1335 if (lstat(target, &stb) < 0) {
1336 error("%s:%s: %s\n", host, target, strerror(errno));
1344 if (rmdir(target) < 0) {
1346 error("%s:%s: %s\n", host, target, strerror(errno));
1352 (void) snprintf(cp, sizeof(buf) - 1, "removed %s\n", target);
1353 (void) write(rem, buf, strlen(cp) + 1);
1357 * Execute a shell command to handle special cases.
1363 int fd[2], status, pid, i;
1364 register char *cp, *s;
1366 extern int userid, groupid;
1369 error("%s\n", strerror(errno));
1372 if ((pid = fork()) == 0) {
1374 * Return everything the shell commands print.
1379 (void) open(_PATH_DEVNULL, O_RDONLY);
1382 (void) close(fd[0]);
1383 (void) close(fd[1]);
1386 execl(_PATH_BSHELL, "sh", "-c", cmd, 0);
1389 (void) close(fd[1]);
1392 while ((i = read(fd[0], buf, sizeof(buf))) > 0) {
1396 if (cp[-1] != '\n') {
1397 if (s < &sbuf[sizeof(sbuf)-1])
1402 * Throw away blank lines.
1404 if (s == &sbuf[2]) {
1408 (void) write(rem, sbuf, s - sbuf);
1414 (void) write(rem, sbuf, s - sbuf);
1416 while ((i = wait(&status)) != pid && i != -1)
1420 (void) close(fd[0]);
1422 error("shell returned %d\n", status);
1430 #include <varargs.h>
1435 log(FILE *fp, const char *fmt, ...)
1437 log(fp, fmt, va_alist)
1449 /* Print changes locally if not quiet mode */
1451 (void)vprintf(fmt, ap);
1453 /* Save changes (for mailing) if really updating files */
1454 if (!(options & VERIFY) && fp != NULL)
1455 (void)vfprintf(fp, fmt, ap);
1461 error(const char *fmt, ...)
1463 error(fmt, va_alist)
1477 if (!fp && !(fp = fdopen(rem, "w")))
1480 (void)fprintf(fp, "%crdist: ", 0x01);
1481 (void)vfprintf(fp, fmt, ap);
1486 (void)fprintf(stderr, "rdist: ");
1487 (void)vfprintf(stderr, fmt, ap);
1491 (void)fprintf(lfp, "rdist: ");
1492 (void)vfprintf(lfp, fmt, ap);
1500 fatal(const char *fmt, ...)
1502 fatal(fmt, va_alist)
1516 if (!fp && !(fp = fdopen(rem, "w")))
1519 (void)fprintf(fp, "%crdist: ", 0x02);
1520 (void)vfprintf(fp, fmt, ap);
1525 (void)fprintf(stderr, "rdist: ");
1526 (void)vfprintf(stderr, fmt, ap);
1530 (void)fprintf(lfp, "rdist: ");
1531 (void)vfprintf(lfp, fmt, ap);
1544 printf("response()\n");
1548 if (read(rem, cp, 1) != 1)
1550 } while (*cp++ != '\n' && cp < &resp[BUFSIZ]);
1556 log(lfp, "%s\n", s);
1562 log(lfp, "Note: %s\n",s);
1574 (void) write(2, s, cp - s);
1577 (void) fwrite(s, 1, cp - s, lfp);
1579 if (resp[0] == '\2')
1586 * Remove temporary files and do any cleanup operations before exiting.
1592 (void) unlink(tempfile);
1598 note(const char *fmt, ...)
1605 static char buf[BUFSIZ];
1612 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1625 write(rem, s, strlen(s));