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