Annotation of src/usr.bin/rdist/client.c, Revision 1.25
1.25 ! guenther 1: /* $OpenBSD: client.c,v 1.24 2012/11/12 01:14:41 guenther Exp $ */
1.3 deraadt 2:
1.1 dm 3: /*
4: * Copyright (c) 1983 Regents of the University of California.
5: * 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.19 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:
1.18 millert 32: #include "defs.h"
33: #include "y.tab.h"
1.1 dm 34:
35: /*
36: * Routines used in client mode to communicate with remove server.
37: */
38:
39:
40: /*
41: * Update status
42: */
43: #define US_NOTHING 0 /* No update needed */
44: #define US_NOENT 1 /* Entry does not exist */
45: #define US_OUTDATE 2 /* Entry is out of date */
46: #define US_DOCOMP 3 /* Do a binary comparison */
1.18 millert 47: #define US_CHMOG 4 /* Modes or ownership of file differ */
1.1 dm 48:
49: struct linkbuf *ihead = NULL; /* list of files with more than one link */
50: char buf[BUFSIZ]; /* general purpose buffer */
51: u_char respbuff[BUFSIZ]; /* Response buffer */
52: char target[BUFSIZ]; /* target/source directory name */
53: char source[BUFSIZ]; /* source directory name */
54: char *ptarget; /* pointer to end of target name */
55: char *Tdest; /* pointer to last T dest*/
56: struct namelist *updfilelist = NULL; /* List of updated files */
57:
1.18 millert 58: static void runspecial(char *, opt_t, char *, int);
59: static void addcmdspecialfile(char *, char *, int);
60: static void freecmdspecialfiles(void);
61: static struct linkbuf *linkinfo(struct stat *);
62: static int sendhardlink(opt_t, struct linkbuf *, char *, int);
63: static int sendfile(char *, opt_t, struct stat *, char *, char *, int);
64: static int rmchk(opt_t);
65: static int senddir(char *, opt_t, struct stat *, char *, char *, int);
66: static int sendlink(char *, opt_t, struct stat *, char *, char *, int);
67: static int update(char *, opt_t, struct stat *);
68: static int dostat(char *, struct stat *, opt_t);
69: static int statupdate(int, char *, opt_t, char *, int, struct stat *, char *, char *);
70: static int fullupdate(int, char *, opt_t, char *, int, struct stat *, char *, char *);
71: static int sendit(char *, opt_t, int);
1.1 dm 72:
73: /*
74: * return remote file pathname (relative from target)
75: */
1.18 millert 76: char *
77: remfilename(char *src, char *dest, char *path, char *rname, int destdir)
1.1 dm 78: {
79: extern struct namelist *filelist;
1.13 mpech 80: char *lname, *cp;
1.1 dm 81: static char buff[BUFSIZ];
82: int srclen, pathlen;
83: char *p;
84:
85:
86: debugmsg(DM_MISC,
87: "remfilename: src=%s dest=%s path=%s rname=%s destdir=%d\n",
88: A(src), A(dest), A(path), A(rname), destdir);
89:
90: if (!dest) {
91: debugmsg(DM_MISC, "remfilename: remote filename=%s\n", path);
92: return(path);
93: }
94:
95: if (!destdir) {
96: debugmsg(DM_MISC, "remfilename: remote filename=%s\n", dest);
97: return(dest);
98: }
99:
100: buff[0] = CNULL;
101: lname = buff;
102: if (path && *path) {
103: cp = strrchr(path, '/');
104: if (cp == NULL)
1.18 millert 105: (void) snprintf(buff, sizeof(buff), "%s/%s", dest, path);
1.1 dm 106: else {
107: srclen = strlen(src);
108: pathlen = strlen(path);
109: if (srclen >= pathlen)
110: cp++; /* xbasename(path) */
111: else {
112: if (filelist && filelist->n_next == NULL)
113: /* path relative to src */
114: cp = path + srclen;
115: else {
116: if ((p = strrchr(src, '/')))
117: cp = path + srclen - strlen(p);
118: else
119: cp = path;
120: }
121: }
122: if ((*cp != '/') && *cp)
1.18 millert 123: (void) snprintf(buff, sizeof(buff), "%s/%s",
124: dest, cp);
1.1 dm 125: else
1.18 millert 126: (void) snprintf(buff, sizeof(buff), "%s%s",
127: dest, cp);
1.1 dm 128: }
129: } else
1.18 millert 130: (void) strlcpy(lname, dest, buf + sizeof buff - lname);
1.1 dm 131:
132: debugmsg(DM_MISC, "remfilename: remote filename=%s\n", lname);
133:
134: return(lname);
135: }
136:
137: /*
138: * Return true if name is in the list.
139: */
1.18 millert 140: int
141: inlist(struct namelist *list, char *file)
1.1 dm 142: {
1.13 mpech 143: struct namelist *nl;
1.1 dm 144:
145: for (nl = list; nl != NULL; nl = nl->n_next)
146: if (strcmp(file, nl->n_name) == 0)
147: return(1);
148: return(0);
149: }
150:
151: /*
152: * Run any special commands for this file
153: */
1.18 millert 154: static void
155: runspecial(char *starget, opt_t opts, char *rname, int destdir)
1.1 dm 156: {
1.13 mpech 157: struct subcmd *sc;
1.1 dm 158: extern struct subcmd *subcmds;
159: char *rfile;
160:
161: rfile = remfilename(source, Tdest, target, rname, destdir);
162:
163: for (sc = subcmds; sc != NULL; sc = sc->sc_next) {
164: if (sc->sc_type != SPECIAL)
165: continue;
166: if (sc->sc_args != NULL && !inlist(sc->sc_args, starget))
167: continue;
168: message(MT_CHANGE, "special \"%s\"", sc->sc_name);
169: if (IS_ON(opts, DO_VERIFY))
170: continue;
171: (void) sendcmd(C_SPECIAL,
172: "%s=%s;%s=%s;%s=%s;export %s %s %s;%s",
173: E_LOCFILE, starget,
174: E_REMFILE, rfile,
175: E_BASEFILE, xbasename(rfile),
176: E_LOCFILE, E_REMFILE, E_BASEFILE,
177: sc->sc_name);
178: while (response() > 0)
179: ;
180: }
181: }
182:
183: /*
184: * If we're doing a target with a "cmdspecial" in it, then
185: * save the name of the file being updated for use with "cmdspecial".
186: */
1.18 millert 187: static void
188: addcmdspecialfile(char *starget, char *rname, int destdir)
1.1 dm 189: {
190: char *rfile;
191: struct namelist *new;
1.13 mpech 192: struct subcmd *sc;
1.1 dm 193: extern struct subcmd *subcmds;
194: int isokay = 0;
195:
196: rfile = remfilename(source, Tdest, target, rname, destdir);
197:
198: for (sc = subcmds; sc != NULL && !isokay; sc = sc->sc_next) {
199: if (sc->sc_type != CMDSPECIAL)
200: continue;
201: if (sc->sc_args != NULL && !inlist(sc->sc_args, starget))
202: continue;
203: isokay = TRUE;
204: }
205:
206: if (isokay) {
207: new = (struct namelist *) xmalloc(sizeof(struct namelist));
1.8 millert 208: new->n_name = xstrdup(rfile);
1.17 millert 209: new->n_regex = NULL;
1.1 dm 210: new->n_next = updfilelist;
211: updfilelist = new;
212: }
213: }
214:
215: /*
216: * Free the file list
217: */
1.18 millert 218: static void
219: freecmdspecialfiles(void)
1.1 dm 220: {
1.13 mpech 221: struct namelist *ptr, *save;
1.1 dm 222:
223: for (ptr = updfilelist; ptr; ) {
224: if (ptr->n_name) (void) free(ptr->n_name);
225: save = ptr->n_next;
226: (void) free(ptr);
227: if (save)
228: ptr = save->n_next;
229: else
230: ptr = NULL;
231: }
232: updfilelist = NULL;
233: }
234:
235: /*
236: * Run commands for an entire cmd
237: */
1.18 millert 238: void
239: runcmdspecial(struct cmd *cmd, opt_t opts)
1.1 dm 240: {
1.13 mpech 241: struct subcmd *sc;
242: struct namelist *f;
1.1 dm 243: int first = TRUE;
244:
245: for (sc = cmd->c_cmds; sc != NULL; sc = sc->sc_next) {
246: if (sc->sc_type != CMDSPECIAL)
247: continue;
248: message(MT_CHANGE, "cmdspecial \"%s\"", sc->sc_name);
249: if (IS_ON(opts, DO_VERIFY))
250: continue;
251: /* Send all the file names */
252: for (f = updfilelist; f != NULL; f = f->n_next) {
253: if (first) {
254: (void) sendcmd(C_CMDSPECIAL, NULL);
255: if (response() < 0)
256: return;
257: first = FALSE;
258: }
259: (void) sendcmd(RC_FILE, f->n_name);
260: if (response() < 0)
261: return;
262: }
263: if (first) {
264: (void) sendcmd(C_CMDSPECIAL, NULL);
265: if (response() < 0)
266: return;
267: first = FALSE;
268: }
269: /* Send command to run and wait for it to complete */
270: (void) sendcmd(RC_COMMAND, sc->sc_name);
271: while (response() > 0)
272: ;
273: first = TRUE; /* Reset in case there are more CMDSPECIAL's */
274: }
275: freecmdspecialfiles();
276: }
277:
278: /*
279: * For security, reject filenames that contains a newline
280: */
1.18 millert 281: int
282: checkfilename(char *name)
1.1 dm 283: {
1.13 mpech 284: char *cp;
1.1 dm 285:
286: if (strchr(name, '\n')) {
287: for (cp = name; *cp; cp++)
288: if (*cp == '\n')
289: *cp = '?';
290: message(MT_NERROR,
291: "Refuse to handle filename containing newline: %s",
292: name);
293: return(-1);
294: }
295:
296: return(0);
297: }
298:
1.18 millert 299: void
300: freelinkinfo(struct linkbuf *lp)
1.8 millert 301: {
302: if (lp->pathname)
303: free(lp->pathname);
304: if (lp->src)
305: free(lp->src);
306: if (lp->target)
307: free(lp->target);
308: free(lp);
309: }
310:
1.1 dm 311: /*
312: * Save and retrieve hard link info
313: */
1.18 millert 314: static struct linkbuf *
315: linkinfo(struct stat *statp)
1.1 dm 316: {
317: struct linkbuf *lp;
318:
1.8 millert 319: /* XXX - linear search doesn't scale with many links */
1.1 dm 320: for (lp = ihead; lp != NULL; lp = lp->nextp)
321: if (lp->inum == statp->st_ino && lp->devnum == statp->st_dev) {
322: lp->count--;
323: return(lp);
324: }
325:
326: lp = (struct linkbuf *) xmalloc(sizeof(*lp));
327: lp->nextp = ihead;
328: ihead = lp;
329: lp->inum = statp->st_ino;
330: lp->devnum = statp->st_dev;
331: lp->count = statp->st_nlink - 1;
1.8 millert 332: lp->pathname = xstrdup(target);
333: lp->src = xstrdup(source);
1.1 dm 334: if (Tdest)
1.8 millert 335: lp->target = xstrdup(Tdest);
1.1 dm 336: else
1.8 millert 337: lp->target = NULL;
1.1 dm 338:
1.7 millert 339: return(NULL);
1.1 dm 340: }
341:
342: /*
343: * Send a hardlink
344: */
1.18 millert 345: static int
346: sendhardlink(opt_t opts, struct linkbuf *lp, char *rname, int destdir)
1.1 dm 347: {
348: static char buff[MAXPATHLEN];
1.18 millert 349: char *lname; /* name of file to link to */
350: char ername[MAXPATHLEN*4], elname[MAXPATHLEN*4];
1.1 dm 351:
352: debugmsg(DM_MISC,
353: "sendhardlink: rname='%s' pathname='%s' src='%s' target='%s'\n",
1.18 millert 354: rname, lp->pathname ? lp->pathname : "",
355: lp->src ? lp->src : "", lp->target ? lp->target : "");
1.1 dm 356:
1.10 millert 357: if (lp->target == NULL)
1.18 millert 358: lname = lp->pathname;
1.1 dm 359: else {
1.18 millert 360: lname = buff;
361: strlcpy(lname, remfilename(lp->src, lp->target,
362: lp->pathname, rname,
363: destdir), sizeof(buff));
364: debugmsg(DM_MISC, "sendhardlink: lname=%s\n", lname);
365: }
366: ENCODE(elname, lname);
367: ENCODE(ername, rname);
1.24 guenther 368: (void) sendcmd(C_RECVHARDLINK, "%lo %s %s",
1.18 millert 369: opts, elname, ername);
1.1 dm 370:
371: return(response());
372: }
373:
374: /*
375: * Send a file
376: */
1.18 millert 377: static int
378: sendfile(char *rname, opt_t opts, struct stat *stb, char *user,
379: char *group, int destdir)
1.1 dm 380: {
381: int goterr, f;
382: off_t i;
1.18 millert 383: char ername[MAXPATHLEN*4];
1.1 dm 384:
385: if (stb->st_nlink > 1) {
386: struct linkbuf *lp;
387:
388: if ((lp = linkinfo(stb)) != NULL)
389: return(sendhardlink(opts, lp, rname, destdir));
390: }
391:
392: if ((f = open(target, O_RDONLY)) < 0) {
393: error("%s: open for read failed: %s", target, SYSERR);
394: return(-1);
395: }
396:
397: /*
398: * Send file info
399: */
1.18 millert 400: ENCODE(ername, rname);
401:
1.24 guenther 402: (void) sendcmd(C_RECVREG, "%lo %04o %lld %lld %lld %s %s %s",
1.23 krw 403: opts, stb->st_mode & 07777, (long long) stb->st_size,
1.24 guenther 404: (long long)stb->st_mtime, (long long)stb->st_atime,
1.18 millert 405: user, group, ername);
1.1 dm 406: if (response() < 0) {
407: (void) close(f);
408: return(-1);
409: }
410:
1.18 millert 411:
1.23 krw 412: debugmsg(DM_MISC, "Send file '%s' %lld bytes\n", rname,
413: (long long) stb->st_size);
1.1 dm 414:
415: /*
416: * Set remote time out alarm handler.
417: */
418: (void) signal(SIGALRM, sighandler);
419:
420: /*
421: * Actually transfer the file
422: */
423: goterr = 0;
424: for (i = 0; i < stb->st_size; i += BUFSIZ) {
1.18 millert 425: off_t amt = BUFSIZ;
1.1 dm 426:
427: (void) alarm(rtimeout);
428: if (i + amt > stb->st_size)
429: amt = stb->st_size - i;
1.18 millert 430: if (read(f, buf, (size_t) amt) != (ssize_t) amt) {
1.1 dm 431: error("%s: File changed size", target);
432: err();
433: ++goterr;
434: /*
435: * XXX - We have to keep going because the
436: * server expects to receive a fixed number
437: * of bytes that we specified as the file size.
438: * We need Out Of Band communication to handle
439: * this situation gracefully.
440: */
441: }
1.18 millert 442: if (xwrite(rem_w, buf, (size_t) amt) < 0) {
443: error("%s: Error writing to client: %s",
1.1 dm 444: target, SYSERR);
445: err();
446: ++goterr;
447: break;
448: }
449: (void) alarm(0);
450: }
451:
452: (void) alarm(0); /* Insure alarm is off */
453: (void) close(f);
454:
455: debugmsg(DM_MISC, "Send file '%s' %s.\n",
456: (goterr) ? "failed" : "complete", rname);
457:
458: /*
459: * Check for errors and end send
460: */
461: if (goterr)
462: return(-1);
463: else {
464: ack();
465: f = response();
466: if (f < 0)
467: return(-1);
468: else if (f == 0 && IS_ON(opts, DO_COMPARE))
469: return(0);
470:
471: runspecial(target, opts, rname, destdir);
472: addcmdspecialfile(target, rname, destdir);
473:
474: return(0);
475: }
476: }
477:
478: /*
479: * Check for files on the machine being updated that are not on the master
480: * machine and remove them.
481: *
482: * Return < 0 on error.
483: * Return 0 if nothing happened.
484: * Return > 0 if anything is updated.
485: */
1.18 millert 486: static int
487: rmchk(opt_t opts)
1.1 dm 488: {
1.13 mpech 489: u_char *s;
1.1 dm 490: struct stat stb;
491: int didupdate = 0;
492: int n;
1.18 millert 493: char targ[MAXPATHLEN*4];
1.1 dm 494:
495: debugmsg(DM_CALL, "rmchk()\n");
496:
497: /*
498: * Tell the remote to clean the files from the last directory sent.
499: */
500: (void) sendcmd(C_CLEAN, "%o", IS_ON(opts, DO_VERIFY));
501: if (response() < 0)
502: return(-1);
503:
504: for ( ; ; ) {
505: n = remline(s = respbuff, sizeof(respbuff), TRUE);
506: if (n <= 0) {
507: error("rmchk: unexpected control record");
508: return(didupdate);
509: }
510:
511: switch (*s++) {
512: case CC_QUERY: /* Query if file should be removed */
513: /*
514: * Return the following codes to remove query.
515: * CC_NO -- file exists - DON'T remove.
516: * CC_YES -- file doesn't exist - REMOVE.
517: */
1.18 millert 518: if (DECODE(targ, (char *) s) == -1) {
519: error("rmchk: cannot decode file");
520: return(-1);
521: }
522: (void) snprintf(ptarget,
523: sizeof(target) - (ptarget - target),
524: "%s%s",
525: (ptarget[-1] == '/' ? "" : "/"),
526: targ);
1.1 dm 527: debugmsg(DM_MISC, "check %s\n", target);
528: if (except(target))
529: (void) sendcmd(CC_NO, NULL);
530: else if (lstat(target, &stb) < 0) {
531: if (sendcmd(CC_YES, NULL) == 0)
532: didupdate = 1;
533: } else
534: (void) sendcmd(CC_NO, NULL);
535: break;
536:
537: case CC_END:
538: *ptarget = CNULL;
539: ack();
540: return(didupdate);
541:
542: case C_LOGMSG:
543: if (n > 0)
544: message(MT_INFO, "%s", s);
545: break;
546:
1.4 millert 547: case C_NOTEMSG:
548: if (n > 0)
549: message(MT_NOTICE, "%s", s);
550: break;
551: /* Goto top of loop */
552:
1.1 dm 553: case C_ERRMSG:
554: message(MT_NERROR, "%s", s);
555: return(didupdate);
556:
557: case C_FERRMSG:
558: message(MT_FERROR, "%s", s);
559: finish();
560:
561: default:
562: error("rmchk: unexpected response '%s'", respbuff);
563: err();
564: }
565: }
566: /*NOTREACHED*/
567: }
568:
569: /*
570: * Send a directory
571: *
572: * Return < 0 on error.
573: * Return 0 if nothing happened.
574: * Return > 0 if anything is updated.
575: */
1.18 millert 576: static int
577: senddir(char *rname, opt_t opts, struct stat *stb, char *user,
578: char *group, int destdir)
1.1 dm 579: {
580: DIRENTRY *dp;
581: DIR *d;
582: char *optarget, *cp;
583: int len;
584: int didupdate = 0;
1.18 millert 585: char ername[MAXPATHLEN*4];
586:
587: /*
588: * Send recvdir command in recvit() format.
589: */
590: ENCODE(ername, rname);
1.24 guenther 591: (void) sendcmd(C_RECVDIR, "%lo %04o 0 0 0 %s %s %s",
1.18 millert 592: opts, stb->st_mode & 07777, user, group, ername);
593: if (response() < 0)
594: return(-1);
1.1 dm 595:
1.25 ! guenther 596: optarget = ptarget;
! 597:
1.1 dm 598: /*
1.2 dm 599: * Don't descend into directory
600: */
1.25 ! guenther 601: if (IS_ON(opts, DO_NODESCEND)) {
! 602: didupdate = 0;
1.24 guenther 603: goto out;
1.25 ! guenther 604: }
1.2 dm 605:
1.18 millert 606: if (IS_ON(opts, DO_REMOVE))
607: if (rmchk(opts) > 0)
608: ++didupdate;
609:
1.2 dm 610: if ((d = opendir(target)) == NULL) {
611: error("%s: opendir failed: %s", target, SYSERR);
1.25 ! guenther 612: didupdate = -1;
! 613: goto out;
1.2 dm 614: }
615:
1.1 dm 616: len = ptarget - target;
1.18 millert 617: while ((dp = readdir(d)) != NULL) {
1.1 dm 618: if (!strcmp(dp->d_name, ".") ||
619: !strcmp(dp->d_name, ".."))
620: continue;
621: if (len + 1 + (int) strlen(dp->d_name) >= MAXPATHLEN - 1) {
622: error("%s/%s: Name too long", target,
623: dp->d_name);
624: continue;
625: }
626: ptarget = optarget;
627: if (ptarget[-1] != '/')
628: *ptarget++ = '/';
629: cp = dp->d_name;
1.18 millert 630: while ((*ptarget++ = *cp++) != '\0')
631: continue;
1.1 dm 632: ptarget--;
633: if (sendit(dp->d_name, opts, destdir) > 0)
634: didupdate = 1;
635: }
636: (void) closedir(d);
637:
1.25 ! guenther 638: out:
1.1 dm 639: (void) sendcmd(C_END, NULL);
640: (void) response();
641:
642: ptarget = optarget;
643: *ptarget = CNULL;
644:
645: return(didupdate);
646: }
647:
648: /*
649: * Send a link
650: */
1.18 millert 651: static int
652: sendlink(char *rname, opt_t opts, struct stat *stb, char *user,
653: char *group, int destdir)
1.1 dm 654: {
1.7 millert 655: int f, n;
1.1 dm 656: static char tbuf[BUFSIZ];
657: char lbuf[MAXPATHLEN];
658: u_char *s;
1.18 millert 659: char ername[MAXPATHLEN*4];
1.1 dm 660:
1.24 guenther 661: debugmsg(DM_CALL, "sendlink(%s, %lx, stb, %d)\n", rname, opts, destdir);
1.1 dm 662:
663: if (stb->st_nlink > 1) {
664: struct linkbuf *lp;
665:
666: if ((lp = linkinfo(stb)) != NULL)
667: return(sendhardlink(opts, lp, rname, destdir));
668: }
669:
670: /*
671: * Gather and send basic link info
672: */
1.18 millert 673: ENCODE(ername, rname);
1.24 guenther 674: (void) sendcmd(C_RECVSYMLINK, "%lo %04o %lld %lld %lld %s %s %s",
1.23 krw 675: opts, stb->st_mode & 07777, (long long) stb->st_size,
1.24 guenther 676: (long long)stb->st_mtime, (long long)stb->st_atime,
1.18 millert 677: user, group, ername);
1.1 dm 678: if (response() < 0)
679: return(-1);
680:
681: /*
682: * Gather and send additional link info
683: */
1.7 millert 684: if ((n = readlink(target, lbuf, sizeof(lbuf)-1)) != -1)
685: lbuf[n] = '\0';
686: else {
687: error("%s: readlink failed", target);
688: err();
689: }
1.18 millert 690: (void) snprintf(tbuf, sizeof(tbuf), "%.*s", (int) stb->st_size, lbuf);
691: ENCODE(ername, tbuf);
692: (void) sendcmd(C_NONE, "%s\n", ername);
1.1 dm 693:
1.7 millert 694: if (n != stb->st_size) {
1.1 dm 695: error("%s: file changed size", target);
696: err();
697: } else
698: ack();
699:
700: /*
701: * Check response
702: */
703: f = response();
704: if (f < 0)
705: return(-1);
706: else if (f == 0 && IS_ON(opts, DO_COMPARE))
707: return(0);
708:
709: /*
710: * Read and process responses from server.
711: * The server may send multiple messages regarding
712: * file deletes if the remote target is a directory.
713: */
714: for (;;) {
715: n = remline(s = respbuff, sizeof(respbuff), TRUE);
716: if (n == -1) /* normal EOF */
717: return(0);
718: if (n == 0) {
719: error("expected control record");
720: continue;
721: }
722:
723: switch (*s++) {
724: case C_END: /* End of send operation */
725: *ptarget = CNULL;
726: ack();
727: runspecial(target, opts, rname, destdir);
728: addcmdspecialfile(target, rname, destdir);
729: return(0);
730:
731: case C_LOGMSG:
732: if (n > 0)
733: message(MT_INFO, "%s", s);
734: break;
1.4 millert 735:
736: case C_NOTEMSG:
737: if (n > 0)
738: message(MT_NOTICE, "%s", s);
739: break;
740: /* Goto top of loop */
1.1 dm 741:
742: case C_ERRMSG:
743: message(MT_NERROR, "%s", s);
744: return(-1);
745:
746: case C_FERRMSG:
747: message(MT_FERROR, "%s", s);
748: finish();
749:
750: default:
751: error("install link: unexpected response '%s'",
752: respbuff);
753: err();
754: }
755: }
756: /*NOTREACHED*/
757: }
758:
759: /*
760: * Check to see if file needs to be updated on the remote machine.
761: * Returns:
762: * US_NOTHING - no update
763: * US_NOENT - remote doesn't exist
764: * US_OUTDATE - out of date
765: * US_DOCOMP - comparing binaries to determine if out of date
1.18 millert 766: * US_CHMOG - File modes or ownership do not match
1.1 dm 767: */
1.18 millert 768: static int
769: update(char *rname, opt_t opts, struct stat *statp)
1.1 dm 770: {
1.13 mpech 771: off_t size;
772: time_t mtime;
1.1 dm 773: unsigned short lmode;
774: unsigned short rmode;
775: char *owner = NULL, *group = NULL;
776: int done, n;
777: u_char *cp;
1.18 millert 778: char ername[MAXPATHLEN*4];
1.1 dm 779:
1.24 guenther 780: debugmsg(DM_CALL, "update(%s, 0x%lx, %p)\n", rname, opts, statp);
1.1 dm 781:
1.21 deraadt 782: switch (statp->st_mode & S_IFMT) {
783: case S_IFBLK:
784: debugmsg(DM_MISC, "%s is a block special; skipping\n", target);
785: return(US_NOTHING);
786: case S_IFCHR:
787: debugmsg(DM_MISC, "%s is a character special; skipping\n",
788: target);
789: return(US_NOTHING);
790: case S_IFIFO:
791: debugmsg(DM_MISC, "%s is a fifo; skipping\n", target);
792: return(US_NOTHING);
793: case S_IFSOCK:
794: debugmsg(DM_MISC, "%s is a socket; skipping\n", target);
795: return(US_NOTHING);
796: }
797:
1.1 dm 798: if (IS_ON(opts, DO_NOEXEC))
799: if (isexec(target, statp)) {
800: debugmsg(DM_MISC, "%s is an executable\n", target);
801: return(US_NOTHING);
802: }
803:
804: /*
805: * Check to see if the file exists on the remote machine.
806: */
1.18 millert 807: ENCODE(ername, rname);
808: (void) sendcmd(C_QUERY, "%s", ername);
1.1 dm 809:
810: for (done = 0; !done;) {
811: n = remline(cp = respbuff, sizeof(respbuff), TRUE);
812: if (n <= 0) {
813: error("update: unexpected control record in response to query");
814: return(US_NOTHING);
815: }
816:
817: switch (*cp++) {
818: case QC_ONNFS: /* Resides on a NFS */
819: debugmsg(DM_MISC,
820: "update: %s is on a NFS. Skipping...\n",
821: rname);
822: return(US_NOTHING);
823:
824: case QC_SYM: /* Is a symbolic link */
825: debugmsg(DM_MISC,
826: "update: %s is a symlink. Skipping...\n",
827: rname);
828: return(US_NOTHING);
829:
830: case QC_ONRO: /* Resides on a Read-Only fs */
831: debugmsg(DM_MISC,
832: "update: %s is on a RO fs. Skipping...\n",
833: rname);
834: return(US_NOTHING);
835:
836: case QC_YES:
837: done = 1;
838: break;
839:
840: case QC_NO: /* file doesn't exist so install it */
841: return(US_NOENT);
842:
843: case C_ERRMSG:
844: if (cp)
845: message(MT_NERROR, "%s", cp);
846: return(US_NOTHING);
847:
848: case C_FERRMSG:
849: if (cp)
850: message(MT_FERROR, "%s", cp);
851: finish();
852:
853: case C_NOTEMSG:
854: if (cp)
855: message(MT_NOTICE, "%s", cp);
856: break;
857: /* Goto top of loop */
858:
859: default:
1.18 millert 860: error("update: unexpected response to query '%s'", respbuff);
1.1 dm 861: return(US_NOTHING);
862: }
863: }
864:
865: /*
866: * Target exists, but no other info passed
867: */
868: if (n <= 1 || !S_ISREG(statp->st_mode))
869: return(US_OUTDATE);
870:
871: if (IS_ON(opts, DO_COMPARE))
872: return(US_DOCOMP);
873:
874: /*
875: * Parse size
876: */
1.23 krw 877: size = (off_t) strtoll(cp, (char **)&cp, 10);
1.1 dm 878: if (*cp++ != ' ') {
879: error("update: size not delimited");
880: return(US_NOTHING);
881: }
882:
883: /*
884: * Parse mtime
885: */
1.7 millert 886: mtime = strtol(cp, (char **)&cp, 10);
1.1 dm 887: if (*cp++ != ' ') {
888: error("update: mtime not delimited");
889: return(US_NOTHING);
890: }
891:
892: /*
893: * Parse remote file mode
894: */
1.7 millert 895: rmode = strtol(cp, (char **)&cp, 8);
1.1 dm 896: if (cp && *cp)
897: ++cp;
898:
899: /*
900: * Be backwards compatible
901: */
902: if (cp && *cp != CNULL) {
903: /*
904: * Parse remote file owner
905: */
906: owner = strtok((char *)cp, " ");
907: if (owner == NULL) {
908: error("update: owner not delimited");
909: return(US_NOTHING);
910: }
911:
912: /*
913: * Parse remote file group
914: */
1.7 millert 915: group = strtok(NULL, " ");
1.1 dm 916: if (group == NULL) {
917: error("update: group not delimited");
918: return(US_NOTHING);
919: }
920: }
921:
922: /*
923: * File needs to be updated?
924: */
925: lmode = statp->st_mode & 07777;
926:
927: debugmsg(DM_MISC, "update(%s,) local mode %04o remote mode %04o\n",
928: rname, lmode, rmode);
1.24 guenther 929: debugmsg(DM_MISC, "update(%s,) size %lld mtime %lld owner '%s' grp '%s'"
930: "\n", rname, (long long) size, (long long)mtime, owner, group);
1.1 dm 931:
932: if (statp->st_mtime != mtime) {
933: if (statp->st_mtime < mtime && IS_ON(opts, DO_YOUNGER)) {
934: message(MT_WARNING,
935: "%s: Warning: remote copy is newer",
936: target);
937: return(US_NOTHING);
938: }
939: return(US_OUTDATE);
940: }
941:
1.18 millert 942: if (statp->st_size != size) {
1.23 krw 943: debugmsg(DM_MISC, "size does not match (%lld != %lld).\n",
944: (long long) statp->st_size, (long long) size);
1.18 millert 945: return(US_OUTDATE);
946: }
947:
1.1 dm 948: if (!IS_ON(opts, DO_NOCHKMODE) && lmode != rmode) {
949: debugmsg(DM_MISC, "modes do not match (%04o != %04o).\n",
950: lmode, rmode);
1.18 millert 951: return(US_CHMOG);
1.1 dm 952: }
953:
954:
955: /*
956: * Check ownership
957: */
958: if (!IS_ON(opts, DO_NOCHKOWNER) && owner) {
959: if (!IS_ON(opts, DO_NUMCHKOWNER)) {
960: /* Check by string compare */
961: if (strcmp(owner, getusername(statp->st_uid,
962: target, opts)) != 0) {
963: debugmsg(DM_MISC,
964: "owner does not match (%s != %s).\n",
965: getusername(statp->st_uid,
966: target, opts), owner);
1.18 millert 967: return(US_CHMOG);
1.1 dm 968: }
969: } else {
970: /*
971: * Check numerically.
972: * Allow negative numbers.
973: */
1.18 millert 974: while (*owner && !isdigit((unsigned char)*owner) &&
975: (*owner != '-'))
1.1 dm 976: ++owner;
1.18 millert 977: if (owner && (UID_T) atoi(owner) != statp->st_uid) {
1.1 dm 978: debugmsg(DM_MISC,
979: "owner does not match (%d != %s).\n",
980: statp->st_uid, owner);
1.18 millert 981: return(US_CHMOG);
1.1 dm 982: }
983: }
984: }
985:
986: if (!IS_ON(opts, DO_NOCHKGROUP) && group) {
987: if (!IS_ON(opts, DO_NUMCHKGROUP)) {
988: /* Check by string compare */
989: if (strcmp(group, getgroupname(statp->st_gid,
990: target, opts)) != 0) {
991: debugmsg(DM_MISC,
992: "group does not match (%s != %s).\n",
993: getgroupname(statp->st_gid,
994: target, opts), group);
1.18 millert 995: return(US_CHMOG);
1.1 dm 996: }
997: } else {
998: /* Check numerically */
999: /* Allow negative gid */
1.18 millert 1000: while (*group && !isdigit((unsigned char) *group) &&
1001: (*group != '-'))
1.1 dm 1002: ++group;
1.18 millert 1003: if (group && (UID_T) atoi(group) != statp->st_gid) {
1.1 dm 1004: debugmsg(DM_MISC,
1005: "group does not match (%d != %s).\n",
1006: statp->st_gid, group);
1.18 millert 1007: return(US_CHMOG);
1.1 dm 1008: }
1009: }
1010: }
1011:
1012: return(US_NOTHING);
1013: }
1014:
1015: /*
1016: * Stat a file
1017: */
1.18 millert 1018: static int
1019: dostat(char *file, struct stat *statbuf, opt_t opts)
1.1 dm 1020: {
1021: int s;
1022:
1023: if (IS_ON(opts, DO_FOLLOW))
1024: s = stat(file, statbuf);
1025: else
1026: s = lstat(file, statbuf);
1027:
1028: if (s < 0)
1029: error("%s: %s failed: %s", file,
1030: IS_ON(opts, DO_FOLLOW) ? "stat" : "lstat", SYSERR);
1031: return(s);
1032: }
1033:
1034: /*
1.18 millert 1035: * We need to just change file info.
1036: */
1037: static int
1038: statupdate(int u, char *target, opt_t opts, char *rname, int destdir,
1039: struct stat *st, char *user, char *group)
1040: {
1041: int rv = 0;
1042: char ername[MAXPATHLEN*4];
1043: int lmode = st->st_mode & 07777;
1044:
1045: if (u == US_CHMOG) {
1046: if (IS_ON(opts, DO_VERIFY)) {
1047: message(MT_INFO,
1048: "%s: need to change to perm %04o, owner %s, group %s",
1049: target, lmode, user, group);
1050: runspecial(target, opts, rname, destdir);
1051: }
1052: else {
1053: message(MT_CHANGE, "%s: change to perm %04o, owner %s, group %s",
1054: target, lmode, user, group);
1055: ENCODE(ername, rname);
1.24 guenther 1056: (void) sendcmd(C_CHMOG, "%lo %04o %s %s %s",
1.18 millert 1057: opts, lmode, user, group, ername);
1058: (void) response();
1059: }
1060: rv = 1;
1061: }
1062: return(rv);
1063: }
1064:
1065:
1066: /*
1067: * We need to install/update:
1068: */
1069: static int
1070: fullupdate(int u, char *target, opt_t opts, char *rname, int destdir,
1071: struct stat *st, char *user, char *group)
1072: {
1073: /*
1074: * No entry - need to install
1075: */
1076: if (u == US_NOENT) {
1077: if (IS_ON(opts, DO_VERIFY)) {
1078: message(MT_INFO, "%s: need to install", target);
1079: runspecial(target, opts, rname, destdir);
1080: return(1);
1081: }
1082: if (!IS_ON(opts, DO_QUIET))
1083: message(MT_CHANGE, "%s: installing", target);
1084: FLAG_OFF(opts, (DO_COMPARE|DO_REMOVE));
1085: }
1086:
1087: /*
1088: * Handle special file types, including directories and symlinks
1089: */
1090: if (S_ISDIR(st->st_mode)) {
1091: if (senddir(rname, opts, st, user, group, destdir) > 0)
1092: return(1);
1093: return(0);
1094: } else if (S_ISLNK(st->st_mode)) {
1095: if (u == US_NOENT)
1096: FLAG_ON(opts, DO_COMPARE);
1097: /*
1098: * Since we always send link info to the server
1099: * so the server can determine if the remote link
1.21 deraadt 1100: * is correct, we never get any acknowledgement
1.18 millert 1101: * from the server whether the link was really
1102: * updated or not.
1103: */
1104: (void) sendlink(rname, opts, st, user, group, destdir);
1105: return(0);
1106: } else if (S_ISREG(st->st_mode)) {
1107: if (u == US_OUTDATE) {
1108: if (IS_ON(opts, DO_VERIFY)) {
1109: message(MT_INFO, "%s: need to update", target);
1110: runspecial(target, opts, rname, destdir);
1111: return(1);
1112: }
1113: if (!IS_ON(opts, DO_QUIET))
1114: message(MT_CHANGE, "%s: updating", target);
1115: }
1116: return (sendfile(rname, opts, st, user, group, destdir) == 0);
1117: } else {
1118: message(MT_INFO, "%s: unknown file type 0%o", target,
1119: st->st_mode);
1120: return(0);
1121: }
1122: }
1123:
1124: /*
1.1 dm 1125: * Transfer the file or directory in target[].
1126: * rname is the name of the file on the remote host.
1127: *
1128: * Return < 0 on error.
1129: * Return 0 if nothing happened.
1130: * Return > 0 if anything is updated.
1131: */
1.18 millert 1132: static int
1133: sendit(char *rname, opt_t opts, int destdir)
1.1 dm 1134: {
1135: static struct stat stb;
1136: char *user, *group;
1137: int u, len;
1138:
1139: /*
1140: * Remove possible accidental newline
1141: */
1142: len = strlen(rname);
1143: if (len > 0 && rname[len-1] == '\n')
1144: rname[len-1] = CNULL;
1145:
1146: if (checkfilename(rname) != 0)
1147: return(-1);
1148:
1.24 guenther 1149: debugmsg(DM_CALL, "sendit(%s, 0x%lx) called\n", rname, opts);
1.1 dm 1150:
1151: if (except(target))
1152: return(0);
1153:
1154: if (dostat(target, &stb, opts) < 0)
1155: return(-1);
1156:
1157: /*
1158: * Does rname need updating?
1159: */
1160: u = update(rname, opts, &stb);
1.24 guenther 1161: debugmsg(DM_MISC, "sendit(%s, 0x%lx): update status of %s is %d\n",
1.1 dm 1162: rname, opts, target, u);
1163:
1164: /*
1165: * Don't need to update the file, but we may need to save hardlink
1166: * info.
1167: */
1168: if (u == US_NOTHING) {
1169: if (S_ISREG(stb.st_mode) && stb.st_nlink > 1)
1170: (void) linkinfo(&stb);
1171: return(0);
1172: }
1173:
1174: user = getusername(stb.st_uid, target, opts);
1175: group = getgroupname(stb.st_gid, target, opts);
1176:
1.18 millert 1177: if (u == US_CHMOG && IS_OFF(opts, DO_UPDATEPERM))
1178: u = US_OUTDATE;
1.1 dm 1179:
1.18 millert 1180: if (u == US_NOENT || u == US_OUTDATE || u == US_DOCOMP)
1181: return(fullupdate(u, target, opts, rname, destdir, &stb,
1182: user, group));
1183:
1184: if (u == US_CHMOG)
1185: return(statupdate(u, target, opts, rname, destdir, &stb,
1186: user, group));
1.1 dm 1187:
1.18 millert 1188: return(0);
1.1 dm 1189: }
1190:
1191: /*
1192: * Remove temporary files and do any cleanup operations before exiting.
1193: */
1.18 millert 1194: void
1195: cleanup(int dummy)
1.1 dm 1196: {
1197: char *file;
1198: #ifdef USE_STATDB
1199: extern char statfile[];
1200:
1201: (void) unlink(statfile);
1202: #endif
1203:
1.18 millert 1204: if ((file = getnotifyfile()) != NULL)
1.1 dm 1205: (void) unlink(file);
1206: }
1207:
1208: /*
1209: * Update the file(s) if they are different.
1210: * destdir = 1 if destination should be a directory
1211: * (i.e., more than one source is being copied to the same destination).
1212: *
1213: * Return < 0 on error.
1214: * Return 0 if nothing updated.
1215: * Return > 0 if something was updated.
1216: */
1.18 millert 1217: int
1218: install(char *src, char *dest, int ddir, int destdir, opt_t opts)
1.1 dm 1219: {
1220: static char destcopy[MAXPATHLEN];
1221: char *rname;
1222: int didupdate = 0;
1.18 millert 1223: char ername[MAXPATHLEN*4];
1.1 dm 1224:
1225: debugmsg(DM_CALL,
1.24 guenther 1226: "install(src=%s,dest=%s,ddir=%d,destdir=%d,opts=%ld) start\n",
1.1 dm 1227: (src?src:"NULL"), (dest?dest:"NULL"), ddir, destdir, opts);
1228: /*
1229: * Save source name
1230: */
1231: if (IS_ON(opts, DO_WHOLE))
1232: source[0] = CNULL;
1233: else
1.18 millert 1234: (void) strlcpy(source, src, sizeof(source));
1.1 dm 1235:
1236: if (dest == NULL) {
1237: FLAG_OFF(opts, DO_WHOLE); /* WHOLE only useful if renaming */
1238: dest = src;
1239: }
1240:
1241: if (checkfilename(dest) != 0)
1242: return(-1);
1243:
1244: if (nflag || debug) {
1245: static char buff[BUFSIZ];
1246: char *cp;
1247:
1248: cp = getondistoptlist(opts);
1.18 millert 1249: (void) snprintf(buff, sizeof(buff), "%s%s%s %s %s",
1.1 dm 1250: IS_ON(opts, DO_VERIFY) ? "verify" : "install",
1251: (cp) ? " -o" : "", (cp) ? cp : "",
1252: src, dest);
1253: if (nflag) {
1254: printf("%s\n", buff);
1255: return(0);
1256: } else
1257: debugmsg(DM_MISC, "%s\n", buff);
1258: }
1259:
1.16 millert 1260: rname = exptilde(target, src, sizeof(target));
1.1 dm 1261: if (rname == NULL)
1262: return(-1);
1263: ptarget = target;
1264: while (*ptarget)
1265: ptarget++;
1266: /*
1267: * If we are renaming a directory and we want to preserve
1.20 jsg 1268: * the directory hierarchy (-w), we must strip off the leading
1.1 dm 1269: * directory name and preserve the rest.
1270: */
1271: if (IS_ON(opts, DO_WHOLE)) {
1272: while (*rname == '/')
1273: rname++;
1274: ddir = 1;
1275: destdir = 1;
1276: } else {
1277: rname = strrchr(target, '/');
1278: /* Check if no '/' or target ends in '/' */
1279: if (rname == NULL ||
1280: rname+1 == NULL ||
1281: *(rname+1) == CNULL)
1282: rname = target;
1283: else
1284: rname++;
1285: }
1286:
1287: debugmsg(DM_MISC,
1288: "install: target=%s src=%s rname=%s dest='%s' destdir=%d, ddir=%d\n",
1289: target, source, rname, dest, destdir, ddir);
1290:
1291: /*
1292: * Pass the destination file/directory name to remote.
1293: */
1.18 millert 1294: ENCODE(ername, dest);
1.1 dm 1295: if (ddir)
1.24 guenther 1296: (void) sendcmd(C_DIRTARGET, "%lo %s", opts, ername);
1.1 dm 1297: else
1.24 guenther 1298: (void) sendcmd(C_TARGET, "%lo %s", opts, ername);
1.1 dm 1299: if (response() < 0)
1300: return(-1);
1301:
1302: /*
1303: * Save the name of the remote target destination if we are
1304: * in WHOLE mode (destdir > 0) or if the source and destination
1305: * are not the same. This info will be used later for maintaining
1306: * hardlink info.
1307: */
1308: if (destdir || (src && dest && strcmp(src, dest))) {
1.18 millert 1309: (void) strlcpy(destcopy, dest, sizeof(destcopy));
1.1 dm 1310: Tdest = destcopy;
1311: }
1312:
1313: didupdate = sendit(rname, opts, destdir);
1314: Tdest = 0;
1315:
1316: return(didupdate);
1317: }