/src/pdsh/pcp_server.c
C | 464 lines | 325 code | 54 blank | 85 comment | 108 complexity | a3f50f6dcd3302fdcd0852a92680ed74 MD5 | raw file
1/*****************************************************************************\ 2 * $Id$ 3 ***************************************************************************** 4 * Copyright (C) 2001-2006 The Regents of the University of California. 5 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). 6 * Written by Jim Garlick <garlick@llnl.gov>. 7 * UCRL-CODE-2003-005. 8 * 9 * This file is part of Pdsh, a parallel remote shell program. 10 * For details, see <http://www.llnl.gov/linux/pdsh/>. 11 * 12 * Pdsh is free software; you can redistribute it and/or modify it under 13 * the terms of the GNU General Public License as published by the Free 14 * Software Foundation; either version 2 of the License, or (at your option) 15 * any later version. 16 * 17 * Pdsh is distributed in the hope that it will be useful, but WITHOUT ANY 18 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 19 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 20 * details. 21 * 22 * You should have received a copy of the GNU General Public License along 23 * with Pdsh; if not, write to the Free Software Foundation, Inc., 24 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 25\*****************************************************************************/ 26 27/* 28 * Copyright (c) 1983, 1990 The Regents of the University of California. 29 * All rights reserved. 30 * 31 * Redistribution and use in source and binary forms, with or without 32 * modification, are permitted provided that the following conditions 33 * are met: 34 * 1. Redistributions of source code must retain the above copyright 35 * notice, this list of conditions and the following disclaimer. 36 * 2. Redistributions in binary form must reproduce the above copyright 37 * notice, this list of conditions and the following disclaimer in the 38 * documentation and/or other materials provided with the distribution. 39 * 3. Advertising clause removed per the following letter: 40 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change 41 * 4. Neither the name of the University nor the names of its contributors 42 * may be used to endorse or promote products derived from this software 43 * without specific prior written permission. 44 * 45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 55 * SUCH DAMAGE. 56 */ 57 58char copyright[] = 59 "@(#) Copyright (c) 1983, 1990 The Regents of the University of California.\n" 60 "All rights reserved.\n"; 61 62/* 63 * From: @(#)rcp.c 5.32 (Berkeley) 2/25/91 64 */ 65char rcsid[] = "$Id$"; 66/* #include "../version.h" */ 67 68#if HAVE_CONFIG_H 69# include "config.h" 70#endif 71 72#include <sys/param.h> /* roundup() */ 73#if HAVE_SYS_SYSMACROS_H 74# include <sys/sysmacros.h> 75#endif 76#include <sys/stat.h> 77#include <sys/time.h> 78#include <sys/ioctl.h> 79#include <sys/socket.h> 80#include <sys/types.h> 81#include <sys/wait.h> 82#include <netinet/in.h> 83#include <dirent.h> 84#include <fcntl.h> 85#include <signal.h> 86#include <pwd.h> 87#include <netdb.h> 88#include <errno.h> 89#include <unistd.h> 90#include <stdio.h> 91#include <stdarg.h> 92#include <stdlib.h> 93#include <string.h> 94#include <ctype.h> 95#include <stdio.h> 96 97#include "src/common/err.h" 98#include "pcp_server.h" 99#include "opt.h" 100 101#ifndef roundup 102# define roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) 103#endif 104 105/* The majority of the code below is unchanged from the original 106 * rcp code. Changes include: 107 * - rcp bug fix 108 * - removal of conditions that are impossible to hit in pdcp 109 * - update error messages to use pdcp error functions 110 * - minor changes to enhance readability and fit style to rest 111 * of pdsh/pdcp code. 112 * - pass infd/outfd through structure rather than global 113 * - don't exit on error, just return 114 */ 115 116typedef struct _buf { 117 int cnt; 118 char *buf; 119} BUF; 120 121static int _verifydir(struct pcp_server *s, const char *cp); 122static int _response(struct pcp_server *s); 123static BUF *_allocbuf(struct pcp_server *s, BUF *bp, int fd, int blksize); 124static void _error(struct pcp_server *s, const char *fmt, ...); 125static void _sink(struct pcp_server *s, char *targ, BUF *bufp); 126 127static int 128_verifydir(struct pcp_server *s, const char *cp) 129{ 130 struct stat stb; 131 132 if (stat(cp, &stb) >= 0) { 133 if ((stb.st_mode & S_IFMT) == S_IFDIR) 134 return 0; 135 errno = ENOTDIR; 136 } 137 _error(s, "%s not a directory\n", cp); 138 return -1; 139} 140 141static int 142_response(struct pcp_server *s) 143{ 144 char resp; 145 146 if (read(s->infd, &resp, sizeof(resp)) != sizeof(resp)) { 147 _error(s, "lost connection\n"); 148 return -1; 149 } 150 151 switch(resp) { 152 case 0: /* ok */ 153 return 0; 154 default: 155 _error(s, "invalid response received\n"); 156 return -1; 157 } 158 159 /*NOTREACHED*/ 160 return 0; 161} 162 163static BUF * 164_allocbuf(struct pcp_server *s, BUF *bp, int fd, int blksize) 165{ 166 struct stat stb; 167 int size; 168 169 if (fstat(fd, &stb) < 0) { 170 _error(s, "fstat: %m\n"); 171 return NULL; 172 } 173 174 size = roundup(stb.st_blksize, blksize); 175 if (size == 0) 176 size = blksize; 177 if (bp->cnt < size) { 178 if (bp->buf != 0) 179 free(bp->buf); 180 bp->buf = malloc(size); 181 if (!bp->buf) { 182 _error(s, "malloc: out of memory\n"); 183 bp->cnt = 0; 184 return NULL; 185 } 186 } 187 bp->cnt = size; 188 return(bp); 189} 190 191static void 192_error(struct pcp_server *s, const char *fmt, ...) 193{ 194 static FILE *fp = NULL; 195 char newfmt[1000]; 196 va_list ap; 197 int save_errno = errno; /* errno could be changed by fopen */ 198 199 if (!(fp = fdopen(s->outfd, "w"))) 200 return; 201 202 va_start(ap, fmt); 203 204 /* must put "1" at beginning of the format to indicate an error */ 205 snprintf(newfmt, 1000, "%c%s", 0x01, fmt); 206 errno = save_errno; 207 errf(fp, newfmt, ap); 208 va_end(ap); 209 210 fflush(fp); 211} 212 213static void 214_sink(struct pcp_server *svr, char *targ, BUF *bufp) { 215 register char *cp; 216 struct stat stb; 217 struct timeval tv[2]; 218 enum { YES, NO, DISPLAYED } wrerr; 219 BUF *bp; 220 off_t i, j, size; 221 char ch; 222 const char *why = "failed to set 'why' string"; 223 int amt, count, exists, mask, mode; 224 int ofd, setimes, targisdir, cursize = 0; 225 char *np, *buf = NULL, *namebuf = NULL; 226 227#define atime tv[0] 228#define mtime tv[1] 229#define SCREWUP(str) { why = str; goto screwup; } 230 231 if (!(buf = malloc(BUFSIZ))) { 232 _error(svr, "out of memory for buf: %m\n"); 233 return; 234 } 235 236 setimes = targisdir = 0; 237 mask = umask(0); 238 if (!svr->preserve) 239 (void)umask(mask); 240 241 if (svr->target_is_dir) { 242 if (_verifydir(svr, svr->outfile) < 0) 243 return; 244 } 245 246 (void)write(svr->outfd, "", 1); 247 if (stat(targ, &stb) == 0 && (stb.st_mode & S_IFMT) == S_IFDIR) 248 targisdir = 1; 249 250 while (1) { 251 int rc; 252 cp = buf; 253 if ((rc = read(svr->infd, cp, 1)) <= 0) 254 goto end_server; 255 if (*cp++ == '\n') 256 SCREWUP("unexpected <newline>"); 257 258 do { 259 if (read(svr->infd, &ch, sizeof(ch)) != sizeof(ch)) 260 SCREWUP("lost connection"); 261 *cp++ = ch; 262 } while (cp < &buf[BUFSIZ - 1] && ch != '\n'); 263 *cp = 0; 264 265 if (buf[0] == '\01' || buf[0] == '\02') { 266 if (buf[0] == '\02') 267 goto end_server; 268 continue; 269 } 270 271 if (buf[0] == 'E') { 272 (void)write(svr->outfd, "", 1); 273 goto end_server; 274 } 275 276 if (ch == '\n') 277 *--cp = 0; 278 279#define getnum(t) (t) = 0; while (isdigit(*cp)) (t) = (t) * 10 + (*cp++ - '0'); 280 cp = buf; 281 if (*cp == 'T') { 282 setimes++; 283 cp++; 284 getnum(mtime.tv_sec); 285 if (*cp++ != ' ') 286 SCREWUP("mtime.sec not delimited"); 287 getnum(mtime.tv_usec); 288 if (*cp++ != ' ') 289 SCREWUP("mtime.usec not delimited"); 290 getnum(atime.tv_sec); 291 if (*cp++ != ' ') 292 SCREWUP("atime.sec not delimited"); 293 getnum(atime.tv_usec); 294 if (*cp++ != '\0') 295 SCREWUP("atime.usec not delimited"); 296 (void)write(svr->outfd, "", 1); 297 continue; 298 } 299 if (*cp != 'C' && *cp != 'D') 300 SCREWUP("expected control record"); 301 302 mode = 0; 303 for (++cp; cp < buf + 5; cp++) { 304 if (*cp < '0' || *cp > '7') 305 SCREWUP("bad mode"); 306 mode = (mode << 3) | (*cp - '0'); 307 } 308 if (*cp++ != ' ') 309 SCREWUP("mode not delimited"); 310 size = 0; 311 while (isdigit(*cp)) 312 size = size * 10 + (*cp++ - '0'); 313 if (*cp++ != ' ') 314 SCREWUP("size not delimited"); 315 316 /* filename is "retrieved" in this if/else block */ 317 if (targisdir) { 318 319 /* achu: The original rcp code here was completely whack. 320 * Memory was allocated for every file, memory was never 321 * freed, cursize was never set to the current buffer 322 * size, and a code path existed that could write past 323 * allocated memory boundaries. Lots and lots of fixes 324 * here. Atleast one person on google-groups concurs with 325 * my thoughts. 326 */ 327 328 int need; 329 330 need = strlen(targ) + strlen(cp) + 250; 331 if (need > cursize) { 332 if (namebuf) 333 free(namebuf); 334 335 if (!(namebuf = malloc(need))) { 336 _error(svr, "out of memory\n"); 337 cursize = 0; 338 339 /* original rcp may not work with a continue here, 340 * but it will work with pdcp protocol. 341 */ 342 continue; 343 } 344 345 cursize = need; 346 } 347 (void)snprintf(namebuf, cursize, "%s%s%s", targ, 348 *targ ? "/" : "", cp); 349 np = namebuf; 350 } 351 else 352 np = targ; 353 354 exists = stat(np, &stb) == 0; 355 if (buf[0] == 'D') { 356 if (exists) { 357 if ((stb.st_mode & S_IFMT) != S_IFDIR) { 358 errno = ENOTDIR; 359 goto bad; 360 } 361 if (svr->preserve) 362 (void)chmod(np, mode); 363 } else if (mkdir(np, mode) < 0) 364 goto bad; 365 366 /* recursively go down a directory */ 367 _sink(svr, np, bufp); 368 369 if (setimes) { 370 setimes = 0; 371 if (utimes(np, tv) < 0) 372 _error(svr, "can't set times on %s: %m\n", np); 373 } 374 continue; 375 } 376 377 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) { 378bad: 379 _error(svr, "%s: %m\n", np); 380 continue; 381 } 382 if (exists && svr->preserve) 383 (void)fchmod(ofd, mode); 384 385 (void)write(svr->outfd, "", 1); 386 if ((bp = _allocbuf(svr, bufp, ofd, BUFSIZ)) == NULL) { 387 (void)close(ofd); 388 continue; 389 } 390 cp = bp->buf; 391 count = 0; 392 wrerr = NO; 393 for (i = 0; i < size; i += BUFSIZ) { 394 amt = BUFSIZ; 395 if (i + amt > size) 396 amt = size - i; 397 count += amt; 398 do { 399 j = read(svr->infd, cp, amt); 400 if (j <= 0) { 401 _error(svr, "%m\n"); 402 goto end_server; 403 } 404 amt -= j; 405 cp += j; 406 } while (amt > 0); 407 if (count == bp->cnt) { 408 if (wrerr == NO && write(ofd, bp->buf, count) != count) 409 wrerr = YES; 410 count = 0; 411 cp = bp->buf; 412 } 413 } 414 if (count != 0 && wrerr == NO && write(ofd, bp->buf, count) != count) 415 wrerr = YES; 416 if (ftruncate(ofd, size)) { 417 _error(svr, "can't truncate %s: %m\n", np); 418 wrerr = DISPLAYED; 419 } 420 (void)close(ofd); 421 if (_response(svr) < 0) 422 goto end_server; 423 if (setimes && wrerr == NO) { 424 setimes = 0; 425 if (utimes(np, tv) < 0) { 426 _error(svr, "can't set times on %s: %m\n", np); 427 wrerr = DISPLAYED; 428 } 429 } 430 switch(wrerr) { 431 case YES: 432 _error(svr, "%s: %m\n", np); 433 break; 434 case NO: 435 (void)write(svr->outfd, "", 1); 436 break; 437 case DISPLAYED: 438 break; 439 } 440 } 441 442screwup: 443 _error(svr, "protocol screwup: %s\n", why); 444 445end_server: 446 if (buf) 447 free(buf); 448 if (namebuf) 449 free(namebuf); 450 return; 451} 452 453int pcp_server(struct pcp_server *svr) 454{ 455 BUF buffer; 456 memset (&buffer, 0, sizeof (buffer)); 457 458 /* If reverse copy, outfile is always a directory. */ 459 _sink (svr, svr->outfile, &buffer); 460 461 if (buffer.buf) 462 free (buffer.buf); 463 return 0; 464}