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