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