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