Annotation of src/usr.bin/oldrdist/server.c, Revision 1.19
1.19 ! mpech 1: /* $OpenBSD: server.c,v 1.18 2002/06/09 21:11:22 hin 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.19 ! mpech 38: static char *rcsid = "$OpenBSD: server.c,v 1.18 2002/06/09 21:11:22 hin 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.19 ! mpech 1084: uid_t uid;
! 1085: gid_t gid;
1.1 dm 1086: extern char user[];
1.19 ! mpech 1087: extern uid_t userid;
1.1 dm 1088:
1089: uid = userid;
1090: if (userid == 0) {
1091: if (*owner == ':') {
1092: uid = atoi(owner + 1);
1093: } else if (pw == NULL || strcmp(owner, pw->pw_name) != 0) {
1094: if ((pw = getpwnam(owner)) == NULL) {
1095: if (mode & 04000) {
1096: note("%s:%s: unknown login name, clearing setuid",
1097: host, owner);
1098: mode &= ~04000;
1099: uid = 0;
1100: }
1101: } else
1102: uid = pw->pw_uid;
1103: } else
1104: uid = pw->pw_uid;
1105: if (*group == ':') {
1106: gid = atoi(group + 1);
1107: goto ok;
1108: }
1109: } else if ((mode & 04000) && strcmp(user, owner) != 0)
1110: mode &= ~04000;
1111: gid = -1;
1112: if (gr == NULL || strcmp(group, gr->gr_name) != 0) {
1113: if ((*group == ':' && (getgrgid(gid = atoi(group + 1)) == NULL))
1114: || ((gr = getgrnam(group)) == NULL)) {
1115: if (mode & 02000) {
1116: note("%s:%s: unknown group", host, group);
1117: mode &= ~02000;
1118: }
1119: } else
1120: gid = gr->gr_gid;
1121: } else
1122: gid = gr->gr_gid;
1123: if (userid && gid >= 0) {
1124: if (gr) for (i = 0; gr->gr_mem[i] != NULL; i++)
1125: if (!(strcmp(user, gr->gr_mem[i])))
1126: goto ok;
1127: mode &= ~02000;
1128: gid = -1;
1129: }
1130: ok: if (fd != -1 && fchown(fd, uid, gid) < 0 || chown(file, uid, gid) < 0)
1131: note("%s: %s chown: %s", host, file, strerror(errno));
1132: else if (mode & 07000 &&
1133: (fd != -1 && fchmod(fd, mode) < 0 || chmod(file, mode) < 0))
1134: note("%s: %s chmod: %s", host, file, strerror(errno));
1135: return(0);
1136: }
1137:
1138: /*
1139: * Check for files on the machine being updated that are not on the master
1140: * machine and remove them.
1141: */
1142: static void
1143: rmchk(opts)
1144: int opts;
1145: {
1.15 mpech 1146: char *cp, *s;
1.1 dm 1147: struct stat stb;
1148:
1149: if (debug)
1150: printf("rmchk()\n");
1151:
1152: /*
1153: * Tell the remote to clean the files from the last directory sent.
1154: */
1.6 millert 1155: (void) snprintf(buf, sizeof(buf), "C%o\n", opts & VERIFY);
1.1 dm 1156: if (debug)
1157: printf("buf = %s", buf);
1158: (void) write(rem, buf, strlen(buf));
1159: if (response() < 0)
1160: return;
1161: for (;;) {
1162: cp = s = buf;
1163: do {
1164: if (read(rem, cp, 1) != 1)
1165: lostconn(0);
1166: } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
1167:
1168: switch (*s++) {
1169: case 'Q': /* Query if file should be removed */
1170: /*
1171: * Return the following codes to remove query.
1172: * N\n -- file exists - DON'T remove.
1173: * Y\n -- file doesn't exist - REMOVE.
1174: */
1175: *--cp = '\0';
1.6 millert 1176: (void) snprintf(tp, sizeof(target) - (tp - target),
1177: "/%s", s);
1.1 dm 1178: if (debug)
1179: printf("check %s\n", target);
1180: if (except(target))
1181: (void) write(rem, "N\n", 2);
1182: else if (lstat(target, &stb) < 0)
1183: (void) write(rem, "Y\n", 2);
1184: else
1185: (void) write(rem, "N\n", 2);
1186: break;
1187:
1188: case '\0':
1189: *--cp = '\0';
1190: if (*s != '\0')
1191: log(lfp, "%s\n", s);
1192: break;
1193:
1194: case 'E':
1195: *tp = '\0';
1196: ack();
1197: return;
1198:
1199: case '\1':
1200: case '\2':
1201: nerrs++;
1202: if (*s != '\n') {
1203: if (!iamremote) {
1204: fflush(stdout);
1205: (void) write(2, s, cp - s);
1206: }
1207: if (lfp != NULL)
1208: (void) fwrite(s, 1, cp - s, lfp);
1209: }
1210: if (buf[0] == '\2')
1211: lostconn(0);
1212: break;
1213:
1214: default:
1215: error("rmchk: unexpected response '%s'\n", buf);
1216: err();
1217: }
1218: }
1219: }
1220:
1221: /*
1222: * Check the current directory (initialized by the 'T' command to server())
1223: * for extraneous files and remove them.
1224: */
1225: static void
1226: clean(cp)
1.15 mpech 1227: char *cp;
1.1 dm 1228: {
1229: DIR *d;
1.15 mpech 1230: struct direct *dp;
1.1 dm 1231: struct stat stb;
1232: char *otp;
1233: int len, opts;
1234:
1235: opts = 0;
1236: while (*cp >= '0' && *cp <= '7')
1237: opts = (opts << 3) | (*cp++ - '0');
1238: if (*cp != '\0') {
1239: error("clean: options not delimited\n");
1240: return;
1241: }
1242: if ((d = opendir(target)) == NULL) {
1243: error("%s:%s: %s\n", host, target, strerror(errno));
1244: return;
1245: }
1246: ack();
1247:
1248: otp = tp;
1249: len = tp - target;
1250: while (dp = readdir(d)) {
1251: if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
1252: continue;
1253: if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
1254: error("%s:%s/%s: Name too long\n",
1255: host, target, dp->d_name);
1256: continue;
1257: }
1258: tp = otp;
1259: *tp++ = '/';
1260: cp = dp->d_name;;
1261: while (*tp++ = *cp++)
1262: ;
1263: tp--;
1264: if (lstat(target, &stb) < 0) {
1265: error("%s:%s: %s\n", host, target, strerror(errno));
1266: continue;
1267: }
1.6 millert 1268: (void) snprintf(buf, sizeof(buf), "Q%s\n", dp->d_name);
1.1 dm 1269: (void) write(rem, buf, strlen(buf));
1270: cp = buf;
1271: do {
1272: if (read(rem, cp, 1) != 1)
1273: cleanup(0);
1274: } while (*cp++ != '\n' && cp < &buf[BUFSIZ]);
1275: *--cp = '\0';
1276: cp = buf;
1277: if (*cp != 'Y')
1278: continue;
1279: if (opts & VERIFY) {
1280: cp = buf;
1281: *cp++ = '\0';
1.6 millert 1282: (void) snprintf(cp, sizeof(buf) - 1,
1.5 millert 1283: "need to remove: %s\n", target);
1.1 dm 1284: (void) write(rem, buf, strlen(cp) + 1);
1285: } else
1286: removeit(&stb);
1287: }
1288: closedir(d);
1289: (void) write(rem, "E\n", 2);
1290: (void) response();
1291: tp = otp;
1292: *tp = '\0';
1293: }
1294:
1295: /*
1296: * Remove a file or directory (recursively) and send back an acknowledge
1297: * or an error message.
1298: */
1299: static void
1300: removeit(stp)
1301: struct stat *stp;
1302: {
1303: DIR *d;
1304: struct direct *dp;
1.15 mpech 1305: char *cp;
1.1 dm 1306: struct stat stb;
1307: char *otp;
1308: int len;
1309:
1310: switch (stp->st_mode & S_IFMT) {
1311: case S_IFREG:
1312: case S_IFLNK:
1313: if (unlink(target) < 0)
1314: goto bad;
1315: goto removed;
1316:
1317: case S_IFDIR:
1318: break;
1319:
1320: default:
1321: error("%s:%s: not a plain file\n", host, target);
1322: return;
1323: }
1324:
1325: if ((d = opendir(target)) == NULL)
1326: goto bad;
1327:
1328: otp = tp;
1329: len = tp - target;
1330: while (dp = readdir(d)) {
1331: if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
1332: continue;
1333: if (len + 1 + strlen(dp->d_name) >= BUFSIZ - 1) {
1334: error("%s:%s/%s: Name too long\n",
1335: host, target, dp->d_name);
1336: continue;
1337: }
1338: tp = otp;
1339: *tp++ = '/';
1340: cp = dp->d_name;;
1341: while (*tp++ = *cp++)
1342: ;
1343: tp--;
1344: if (lstat(target, &stb) < 0) {
1345: error("%s:%s: %s\n", host, target, strerror(errno));
1346: continue;
1347: }
1348: removeit(&stb);
1349: }
1350: closedir(d);
1351: tp = otp;
1352: *tp = '\0';
1353: if (rmdir(target) < 0) {
1354: bad:
1355: error("%s:%s: %s\n", host, target, strerror(errno));
1356: return;
1357: }
1358: removed:
1359: cp = buf;
1360: *cp++ = '\0';
1.6 millert 1361: (void) snprintf(cp, sizeof(buf) - 1, "removed %s\n", target);
1.1 dm 1362: (void) write(rem, buf, strlen(cp) + 1);
1363: }
1364:
1365: /*
1366: * Execute a shell command to handle special cases.
1367: */
1368: static void
1369: dospecial(cmd)
1370: char *cmd;
1371: {
1.19 ! mpech 1372: int fd[2], status;
1.15 mpech 1373: char *cp, *s;
1.1 dm 1374: char sbuf[BUFSIZ];
1.19 ! mpech 1375: pid_t pid, i;
! 1376: extern uid_t userid;
! 1377: extern gid_t groupid;
1.1 dm 1378:
1379: if (pipe(fd) < 0) {
1380: error("%s\n", strerror(errno));
1381: return;
1382: }
1383: if ((pid = fork()) == 0) {
1384: /*
1385: * Return everything the shell commands print.
1386: */
1387: (void) close(0);
1388: (void) close(1);
1389: (void) close(2);
1390: (void) open(_PATH_DEVNULL, O_RDONLY);
1391: (void) dup(fd[1]);
1392: (void) dup(fd[1]);
1393: (void) close(fd[0]);
1394: (void) close(fd[1]);
1.4 millert 1395: #if defined(DIRECT_RCMD)
1.7 tholo 1396: setegid(groupid);
1.1 dm 1397: setgid(groupid);
1.7 tholo 1398: seteuid(userid);
1.1 dm 1399: setuid(userid);
1.4 millert 1400: #endif /* DIRECT_RCMD */
1.13 deraadt 1401: execl(_PATH_BSHELL, "sh", "-c", cmd, (char *)NULL);
1.1 dm 1402: _exit(127);
1403: }
1404: (void) close(fd[1]);
1405: s = sbuf;
1406: *s++ = '\0';
1.6 millert 1407: while ((i = read(fd[0], buf, sizeof(buf))) > 0) {
1.1 dm 1408: cp = buf;
1409: do {
1410: *s++ = *cp++;
1411: if (cp[-1] != '\n') {
1412: if (s < &sbuf[sizeof(sbuf)-1])
1413: continue;
1414: *s++ = '\n';
1415: }
1416: /*
1417: * Throw away blank lines.
1418: */
1419: if (s == &sbuf[2]) {
1420: s--;
1421: continue;
1422: }
1423: (void) write(rem, sbuf, s - sbuf);
1424: s = &sbuf[1];
1425: } while (--i);
1426: }
1427: if (s > &sbuf[1]) {
1428: *s++ = '\n';
1429: (void) write(rem, sbuf, s - sbuf);
1430: }
1431: while ((i = wait(&status)) != pid && i != -1)
1432: ;
1433: if (i == -1)
1434: status = -1;
1435: (void) close(fd[0]);
1436: if (status)
1437: error("shell returned %d\n", status);
1438: else
1439: ack();
1440: }
1441:
1442: void
1443: log(FILE *fp, const char *fmt, ...)
1444: {
1445: va_list ap;
1.17 millert 1446:
1.1 dm 1447: va_start(ap, fmt);
1448: /* Print changes locally if not quiet mode */
1449: if (!qflag)
1.5 millert 1450: (void) vprintf(fmt, ap);
1.1 dm 1451:
1452: /* Save changes (for mailing) if really updating files */
1453: if (!(options & VERIFY) && fp != NULL)
1.5 millert 1454: (void) vfprintf(fp, fmt, ap);
1.1 dm 1455: va_end(ap);
1456: }
1457:
1458: void
1459: error(const char *fmt, ...)
1460: {
1461: static FILE *fp;
1462: va_list ap;
1.17 millert 1463:
1.1 dm 1464: va_start(ap, fmt);
1465: ++nerrs;
1466: if (iamremote) {
1.4 millert 1467: if (!fp && (rem < 0 || !(fp = fdopen(rem, "w"))))
1468: return;
1.5 millert 1469: (void) fprintf(fp, "%crdist: ", 0x01);
1470: (void) vfprintf(fp, fmt, ap);
1.1 dm 1471: fflush(fp);
1472: }
1473: else {
1474: fflush(stdout);
1.5 millert 1475: (void) fprintf(stderr, "rdist: ");
1476: (void) vfprintf(stderr, fmt, ap);
1.1 dm 1477: fflush(stderr);
1478: }
1479: if (lfp != NULL) {
1.5 millert 1480: (void) fprintf(lfp, "rdist: ");
1481: (void) vfprintf(lfp, fmt, ap);
1.1 dm 1482: fflush(lfp);
1483: }
1484: va_end(ap);
1485: }
1486:
1487: void
1488: fatal(const char *fmt, ...)
1489: {
1490: static FILE *fp;
1491: va_list ap;
1.17 millert 1492:
1.1 dm 1493: va_start(ap, fmt);
1494: ++nerrs;
1495: if (!fp && !(fp = fdopen(rem, "w")))
1496: return;
1497: if (iamremote) {
1.5 millert 1498: (void) fprintf(fp, "%crdist: ", 0x02);
1499: (void) vfprintf(fp, fmt, ap);
1.1 dm 1500: fflush(fp);
1501: }
1502: else {
1503: fflush(stdout);
1.5 millert 1504: (void) fprintf(stderr, "rdist: ");
1505: (void) vfprintf(stderr, fmt, ap);
1.1 dm 1506: fflush(stderr);
1507: }
1508: if (lfp != NULL) {
1.5 millert 1509: (void) fprintf(lfp, "rdist: ");
1510: (void) vfprintf(lfp, fmt, ap);
1.1 dm 1511: fflush(lfp);
1512: }
1.14 deraadt 1513: va_end(ap);
1.1 dm 1514: cleanup(0);
1515: }
1516:
1517: static int
1518: response()
1519: {
1520: char *cp, *s;
1521: char resp[BUFSIZ];
1522:
1523: if (debug)
1524: printf("response()\n");
1525:
1526: cp = s = resp;
1527: do {
1528: if (read(rem, cp, 1) != 1)
1529: lostconn(0);
1530: } while (*cp++ != '\n' && cp < &resp[BUFSIZ]);
1531:
1532: switch (*s++) {
1533: case '\0':
1534: *--cp = '\0';
1535: if (*s != '\0') {
1536: log(lfp, "%s\n", s);
1537: return(1);
1538: }
1539: return(0);
1540: case '\3':
1541: *--cp = '\0';
1542: log(lfp, "Note: %s\n",s);
1543: return(response());
1544:
1545: default:
1546: s--;
1547: /* fall into... */
1548: case '\1':
1549: case '\2':
1550: nerrs++;
1551: if (*s != '\n') {
1552: if (!iamremote) {
1553: fflush(stdout);
1554: (void) write(2, s, cp - s);
1555: }
1556: if (lfp != NULL)
1557: (void) fwrite(s, 1, cp - s, lfp);
1558: }
1559: if (resp[0] == '\2')
1560: lostconn(0);
1561: return(-1);
1562: }
1563: }
1564:
1565: /*
1566: * Remove temporary files and do any cleanup operations before exiting.
1567: */
1568: void
1569: cleanup(signo)
1570: int signo;
1571: {
1572: (void) unlink(tempfile);
1573: exit(1);
1574: }
1575:
1576: static void
1577: note(const char *fmt, ...)
1578: {
1579: static char buf[BUFSIZ];
1580: va_list ap;
1.17 millert 1581:
1.1 dm 1582: va_start(ap, fmt);
1.6 millert 1583: (void) vsnprintf(buf, sizeof(buf), fmt, ap);
1.1 dm 1584: va_end(ap);
1585: comment(buf);
1586: }
1587:
1588: static void
1589: comment(s)
1590: char *s;
1591: {
1592: char c;
1593:
1594: c = '\3';
1595: write(rem, &c, 1);
1596: write(rem, s, strlen(s));
1597: c = '\n';
1598: write(rem, &c, 1);
1599: }