]> andersk Git - openssh.git/blame - scp.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / scp.c
CommitLineData
c3773c6e 1/* $OpenBSD: scp.c,v 1.165 2009/12/20 07:28:36 guenther Exp $ */
8efc0c15 2/*
bcbf86ec 3 * scp - secure remote copy. This is basically patched BSD rcp which
4 * uses ssh to do the data transfer (instead of using rcmd).
6ae2364d 5 *
bcbf86ec 6 * NOTE: This version should NOT be suid root. (This uses ssh to
7 * do the transfer and ssh has the necessary privileges.)
6ae2364d 8 *
5260325f 9 * 1995 Timo Rinne <tri@iki.fi>, Tatu Ylonen <ylo@cs.hut.fi>
6ae2364d 10 *
bcbf86ec 11 * As far as I am concerned, the code I have written for this software
12 * can be used freely for any purpose. Any derived versions of this
13 * software must be clearly marked as such, and if the derived work is
14 * incompatible with the protocol description in the RFC file, it must be
15 * called by a name other than "ssh" or "Secure Shell".
16 */
17/*
f3c7c613 18 * Copyright (c) 1999 Theo de Raadt. All rights reserved.
19 * Copyright (c) 1999 Aaron Campbell. All rights reserved.
bcbf86ec 20 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 * 1. Redistributions of source code must retain the above copyright
25 * notice, this list of conditions and the following disclaimer.
26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in the
28 * documentation and/or other materials provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
35 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
39 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 */
8efc0c15 41
42/*
2e73a022 43 * Parts from:
44 *
8efc0c15 45 * Copyright (c) 1983, 1990, 1992, 1993, 1995
46 * The Regents of the University of California. All rights reserved.
47 *
48 * Redistribution and use in source and binary forms, with or without
49 * modification, are permitted provided that the following conditions
50 * are met:
51 * 1. Redistributions of source code must retain the above copyright
52 * notice, this list of conditions and the following disclaimer.
53 * 2. Redistributions in binary form must reproduce the above copyright
54 * notice, this list of conditions and the following disclaimer in the
55 * documentation and/or other materials provided with the distribution.
d981089c 56 * 3. Neither the name of the University nor the names of its contributors
8efc0c15 57 * may be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 * SUCH DAMAGE.
71 *
8efc0c15 72 */
73
74#include "includes.h"
aa2eae64 75
76#include <sys/types.h>
536c14e8 77#include <sys/param.h>
4095f623 78#ifdef HAVE_SYS_STAT_H
79# include <sys/stat.h>
80#endif
af8388bc 81#ifdef HAVE_POLL_H
82#include <poll.h>
83#else
84# ifdef HAVE_SYS_POLL_H
85# include <sys/poll.h>
86# endif
55b82b39 87#endif
e264ac72 88#ifdef HAVE_SYS_TIME_H
89# include <sys/time.h>
90#endif
aa2eae64 91#include <sys/wait.h>
31652869 92#include <sys/uio.h>
68e39d38 93
b6438382 94#include <ctype.h>
68e39d38 95#include <dirent.h>
028094f4 96#include <errno.h>
d3221cca 97#include <fcntl.h>
b1842393 98#include <pwd.h>
ada68823 99#include <signal.h>
4c72fcfd 100#include <stdarg.h>
cf851879 101#include <stdio.h>
ffa517a8 102#include <stdlib.h>
00146caa 103#include <string.h>
b0f6943a 104#include <time.h>
5188ba17 105#include <unistd.h>
5be98986 106#if defined(HAVE_STRNVIS) && defined(HAVE_VIS_H)
107#include <vis.h>
108#endif
8efc0c15 109
8efc0c15 110#include "xmalloc.h"
42f11eb2 111#include "atomicio.h"
112#include "pathnames.h"
113#include "log.h"
36a358ca 114#include "misc.h"
b65c3807 115#include "progressmeter.h"
8efc0c15 116
260d427b 117extern char *__progname;
260d427b 118
6484acf8 119#define COPY_BUFLEN 16384
120
ac93e676 121int do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout);
122
6ea3c52a 123void bwlimit(int);
124
8a624ebf 125/* Struct for addargs */
126arglist args;
94ec8c6b 127
6ea3c52a 128/* Bandwidth limit */
10adbb52 129off_t limit_rate = 0;
6ea3c52a 130
8efc0c15 131/* Name of current file being transferred. */
132char *curfile;
133
134/* This is set to non-zero to enable verbose mode. */
5260325f 135int verbose_mode = 0;
8efc0c15 136
8efc0c15 137/* This is set to zero if the progressmeter is not desired. */
138int showprogress = 1;
139
2e73a022 140/* This is the program to execute for the secured connection. ("ssh" or -S) */
4b565fda 141char *ssh_program = _PATH_SSH_PROGRAM;
2e73a022 142
252d1a24 143/* This is used to store the pid of ssh_program */
0f764b2f 144pid_t do_cmd_pid = -1;
145
146static void
147killchild(int signo)
148{
3c03ad3f 149 if (do_cmd_pid > 1) {
8f4ab41b 150 kill(do_cmd_pid, signo ? signo : SIGTERM);
3c03ad3f 151 waitpid(do_cmd_pid, NULL, 0);
152 }
0f764b2f 153
8f4ab41b 154 if (signo)
155 _exit(1);
156 exit(1);
0f764b2f 157}
252d1a24 158
4116f5c0 159static int
160do_local_cmd(arglist *a)
161{
162 u_int i;
163 int status;
164 pid_t pid;
165
166 if (a->num == 0)
167 fatal("do_local_cmd: no arguments");
168
169 if (verbose_mode) {
170 fprintf(stderr, "Executing:");
171 for (i = 0; i < a->num; i++)
172 fprintf(stderr, " %s", a->list[i]);
173 fprintf(stderr, "\n");
174 }
175 if ((pid = fork()) == -1)
176 fatal("do_local_cmd: fork: %s", strerror(errno));
177
178 if (pid == 0) {
179 execvp(a->list[0], a->list);
180 perror(a->list[0]);
181 exit(1);
182 }
183
184 do_cmd_pid = pid;
185 signal(SIGTERM, killchild);
186 signal(SIGINT, killchild);
187 signal(SIGHUP, killchild);
188
189 while (waitpid(pid, &status, 0) == -1)
190 if (errno != EINTR)
191 fatal("do_local_cmd: waitpid: %s", strerror(errno));
192
193 do_cmd_pid = -1;
194
195 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
196 return (-1);
197
198 return (0);
199}
200
aa3378df 201/*
202 * This function executes the given command as the specified user on the
203 * given host. This returns < 0 if execution fails, and >= 0 otherwise. This
204 * assigns the input and output file descriptors on success.
205 */
8efc0c15 206
6ae2364d 207int
ac93e676 208do_cmd(char *host, char *remuser, char *cmd, int *fdin, int *fdout)
8efc0c15 209{
5260325f 210 int pin[2], pout[2], reserved[2];
211
212 if (verbose_mode)
0b2988ca 213 fprintf(stderr,
214 "Executing: program %s host %s, user %s, command %s\n",
215 ssh_program, host,
216 remuser ? remuser : "(unspecified)", cmd);
5260325f 217
aa3378df 218 /*
219 * Reserve two descriptors so that the real pipes won't get
220 * descriptors 0 and 1 because that will screw up dup2 below.
221 */
2b8dc5e3 222 if (pipe(reserved) < 0)
223 fatal("pipe: %s", strerror(errno));
5260325f 224
225 /* Create a socket pair for communicating with ssh. */
226 if (pipe(pin) < 0)
227 fatal("pipe: %s", strerror(errno));
228 if (pipe(pout) < 0)
229 fatal("pipe: %s", strerror(errno));
230
231 /* Free the reserved descriptors. */
232 close(reserved[0]);
233 close(reserved[1]);
234
1fb23629 235 /* Fork a child to execute the command on the remote host using ssh. */
252d1a24 236 do_cmd_pid = fork();
237 if (do_cmd_pid == 0) {
5260325f 238 /* Child. */
239 close(pin[1]);
240 close(pout[0]);
241 dup2(pin[0], 0);
242 dup2(pout[1], 1);
243 close(pin[0]);
244 close(pout[1]);
245
4116f5c0 246 replacearg(&args, 0, "%s", ssh_program);
c3773c6e 247 if (remuser != NULL) {
248 addargs(&args, "-l");
249 addargs(&args, "%s", remuser);
250 }
251 addargs(&args, "--");
8a624ebf 252 addargs(&args, "%s", host);
253 addargs(&args, "%s", cmd);
5260325f 254
94ec8c6b 255 execvp(ssh_program, args.list);
2e73a022 256 perror(ssh_program);
5260325f 257 exit(1);
252d1a24 258 } else if (do_cmd_pid == -1) {
259 fatal("fork: %s", strerror(errno));
8efc0c15 260 }
5260325f 261 /* Parent. Close the other side, and return the local side. */
262 close(pin[0]);
263 *fdout = pin[1];
264 close(pout[1]);
265 *fdin = pout[0];
0f764b2f 266 signal(SIGTERM, killchild);
267 signal(SIGINT, killchild);
268 signal(SIGHUP, killchild);
5260325f 269 return 0;
8efc0c15 270}
271
8efc0c15 272typedef struct {
2ceb8101 273 size_t cnt;
8efc0c15 274 char *buf;
275} BUF;
276
5260325f 277BUF *allocbuf(BUF *, int, int);
5260325f 278void lostconn(int);
5260325f 279int okname(char *);
280void run_err(const char *,...);
281void verifydir(char *);
8efc0c15 282
8efc0c15 283struct passwd *pwd;
5260325f 284uid_t userid;
8efc0c15 285int errs, remin, remout;
286int pflag, iamremote, iamrecursive, targetshouldbedirectory;
287
288#define CMDNEEDS 64
289char cmd[CMDNEEDS]; /* must hold "rcp -r -p -d\0" */
290
5260325f 291int response(void);
292void rsource(char *, struct stat *);
293void sink(int, char *[]);
294void source(int, char *[]);
295void tolocal(int, char *[]);
296void toremote(char *, int, char *[]);
6484acf8 297size_t scpio(ssize_t (*)(int, void *, size_t), int, void *, size_t, off_t *);
5260325f 298void usage(void);
8efc0c15 299
300int
cf3248b8 301main(int argc, char **argv)
8efc0c15 302{
1879b184 303 int ch, fflag, tflag, status, n;
6ea3c52a 304 double speed;
1879b184 305 char *targ, *endp, **newargv;
8efc0c15 306 extern char *optarg;
307 extern int optind;
308
fd6168c1 309 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
310 sanitise_stdfd();
311
1879b184 312 /* Copy argv, because we modify it */
313 newargv = xcalloc(MAX(argc + 1, 1), sizeof(*newargv));
314 for (n = 0; n < argc; n++)
315 newargv[n] = xstrdup(argv[n]);
316 argv = newargv;
317
fda04d7d 318 __progname = ssh_get_progname(argv[0]);
260d427b 319
4116f5c0 320 memset(&args, '\0', sizeof(args));
94ec8c6b 321 args.list = NULL;
4116f5c0 322 addargs(&args, "%s", ssh_program);
8a624ebf 323 addargs(&args, "-x");
3a222388 324 addargs(&args, "-oForwardAgent no");
d20f3c9e 325 addargs(&args, "-oPermitLocalCommand no");
e1c5bfaf 326 addargs(&args, "-oClearAllForwardings yes");
94ec8c6b 327
8efc0c15 328 fflag = tflag = 0;
e6b15f23 329 while ((ch = getopt(argc, argv, "dfl:prtvBCc:i:P:q1246S:o:F:")) != -1)
5260325f 330 switch (ch) {
331 /* User-visible flags. */
e6b15f23 332 case '1':
333 case '2':
48e671d5 334 case '4':
48e671d5 335 case '6':
94ec8c6b 336 case 'C':
8a624ebf 337 addargs(&args, "-%c", ch);
48e671d5 338 break;
94ec8c6b 339 case 'o':
340 case 'c':
341 case 'i':
f1278af7 342 case 'F':
c3773c6e 343 addargs(&args, "-%c", ch);
344 addargs(&args, "%s", optarg);
8efc0c15 345 break;
346 case 'P':
c3773c6e 347 addargs(&args, "-p");
348 addargs(&args, "%s", optarg);
94ec8c6b 349 break;
350 case 'B':
8a624ebf 351 addargs(&args, "-oBatchmode yes");
94ec8c6b 352 break;
6ea3c52a 353 case 'l':
354 speed = strtod(optarg, &endp);
355 if (speed <= 0 || *endp != '\0')
356 usage();
10adbb52 357 limit_rate = speed * 1024;
6ea3c52a 358 break;
94ec8c6b 359 case 'p':
360 pflag = 1;
5260325f 361 break;
8efc0c15 362 case 'r':
363 iamrecursive = 1;
364 break;
2e73a022 365 case 'S':
94ec8c6b 366 ssh_program = xstrdup(optarg);
367 break;
368 case 'v':
4fc334a2 369 addargs(&args, "-v");
94ec8c6b 370 verbose_mode = 1;
371 break;
372 case 'q':
15c8e3fd 373 addargs(&args, "-q");
94ec8c6b 374 showprogress = 0;
2e73a022 375 break;
376
5260325f 377 /* Server options. */
8efc0c15 378 case 'd':
379 targetshouldbedirectory = 1;
380 break;
5260325f 381 case 'f': /* "from" */
8efc0c15 382 iamremote = 1;
383 fflag = 1;
384 break;
5260325f 385 case 't': /* "to" */
8efc0c15 386 iamremote = 1;
387 tflag = 1;
fe56c12b 388#ifdef HAVE_CYGWIN
389 setmode(0, O_BINARY);
390#endif
8efc0c15 391 break;
8efc0c15 392 default:
393 usage();
394 }
395 argc -= optind;
396 argv += optind;
397
398 if ((pwd = getpwuid(userid = getuid())) == NULL)
261bd618 399 fatal("unknown user %u", (u_int) userid);
8efc0c15 400
7df0afaf 401 if (!isatty(STDOUT_FILENO))
8efc0c15 402 showprogress = 0;
403
404 remin = STDIN_FILENO;
405 remout = STDOUT_FILENO;
406
2b87da3b 407 if (fflag) {
5260325f 408 /* Follow "protocol", send data. */
409 (void) response();
8efc0c15 410 source(argc, argv);
411 exit(errs != 0);
412 }
5260325f 413 if (tflag) {
414 /* Receive data. */
8efc0c15 415 sink(argc, argv);
416 exit(errs != 0);
417 }
8efc0c15 418 if (argc < 2)
419 usage();
420 if (argc > 2)
421 targetshouldbedirectory = 1;
422
423 remin = remout = -1;
252d1a24 424 do_cmd_pid = -1;
8efc0c15 425 /* Command to be executed on remote system using "ssh". */
0ed28836 426 (void) snprintf(cmd, sizeof cmd, "scp%s%s%s%s",
427 verbose_mode ? " -v" : "",
2e73a022 428 iamrecursive ? " -r" : "", pflag ? " -p" : "",
429 targetshouldbedirectory ? " -d" : "");
8efc0c15 430
5260325f 431 (void) signal(SIGPIPE, lostconn);
8efc0c15 432
433 if ((targ = colon(argv[argc - 1]))) /* Dest is remote host. */
434 toremote(targ, argc, argv);
435 else {
8efc0c15 436 if (targetshouldbedirectory)
437 verifydir(argv[argc - 1]);
16e8ab10 438 tolocal(argc, argv); /* Dest is local host. */
8efc0c15 439 }
252d1a24 440 /*
441 * Finally check the exit status of the ssh process, if one was forked
6503dc91 442 * and no error has occurred yet
252d1a24 443 */
444 if (do_cmd_pid != -1 && errs == 0) {
445 if (remin != -1)
446 (void) close(remin);
447 if (remout != -1)
448 (void) close(remout);
449 if (waitpid(do_cmd_pid, &status, 0) == -1)
450 errs = 1;
451 else {
452 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
453 errs = 1;
454 }
455 }
8efc0c15 456 exit(errs != 0);
457}
458
6484acf8 459/*
460 * atomicio-like wrapper that also applies bandwidth limits and updates
461 * the progressmeter counter.
462 */
463size_t
464scpio(ssize_t (*f)(int, void *, size_t), int fd, void *_p, size_t l, off_t *c)
465{
466 u_char *p = (u_char *)_p;
467 size_t offset;
468 ssize_t r;
469 struct pollfd pfd;
470
471 pfd.fd = fd;
472 pfd.events = f == read ? POLLIN : POLLOUT;
473 for (offset = 0; offset < l;) {
474 r = f(fd, p + offset, l - offset);
475 if (r == 0) {
476 errno = EPIPE;
477 return offset;
478 }
479 if (r < 0) {
480 if (errno == EINTR)
481 continue;
5a0c8771 482 if (errno == EAGAIN || errno == EWOULDBLOCK) {
6484acf8 483 (void)poll(&pfd, 1, -1); /* Ignore errors */
484 continue;
485 }
486 return offset;
487 }
488 offset += (size_t)r;
489 *c += (off_t)r;
490 if (limit_rate)
491 bwlimit(r);
492 }
493 return offset;
494}
495
8efc0c15 496void
cf3248b8 497toremote(char *targ, int argc, char **argv)
8efc0c15 498{
99dfaccc 499 char *bp, *host, *src, *suser, *thost, *tuser, *arg;
4116f5c0 500 arglist alist;
ac93e676 501 int i;
4116f5c0 502
503 memset(&alist, '\0', sizeof(alist));
504 alist.list = NULL;
8efc0c15 505
506 *targ++ = 0;
507 if (*targ == 0)
508 targ = ".";
509
ae0d2f42 510 arg = xstrdup(argv[argc - 1]);
99dfaccc 511 if ((thost = strrchr(arg, '@'))) {
8efc0c15 512 /* user@host */
513 *thost++ = 0;
99dfaccc 514 tuser = arg;
8efc0c15 515 if (*tuser == '\0')
516 tuser = NULL;
8efc0c15 517 } else {
99dfaccc 518 thost = arg;
8efc0c15 519 tuser = NULL;
520 }
521
4116f5c0 522 if (tuser != NULL && !okname(tuser)) {
523 xfree(arg);
524 return;
525 }
526
8efc0c15 527 for (i = 0; i < argc - 1; i++) {
528 src = colon(argv[i]);
5260325f 529 if (src) { /* remote to remote */
4116f5c0 530 freeargs(&alist);
531 addargs(&alist, "%s", ssh_program);
532 if (verbose_mode)
533 addargs(&alist, "-v");
534 addargs(&alist, "-x");
535 addargs(&alist, "-oClearAllForwardings yes");
536 addargs(&alist, "-n");
537
8efc0c15 538 *src++ = 0;
539 if (*src == 0)
540 src = ".";
15748b4d 541 host = strrchr(argv[i], '@');
4116f5c0 542
8efc0c15 543 if (host) {
544 *host++ = 0;
48e671d5 545 host = cleanhostname(host);
8efc0c15 546 suser = argv[i];
547 if (*suser == '\0')
548 suser = pwd->pw_name;
4116f5c0 549 else if (!okname(suser))
0ec7661a 550 continue;
4116f5c0 551 addargs(&alist, "-l");
552 addargs(&alist, "%s", suser);
48e671d5 553 } else {
554 host = cleanhostname(argv[i]);
48e671d5 555 }
c3773c6e 556 addargs(&alist, "--");
4116f5c0 557 addargs(&alist, "%s", host);
558 addargs(&alist, "%s", cmd);
559 addargs(&alist, "%s", src);
560 addargs(&alist, "%s%s%s:%s",
561 tuser ? tuser : "", tuser ? "@" : "",
562 thost, targ);
563 if (do_local_cmd(&alist) != 0)
815a8407 564 errs = 1;
5260325f 565 } else { /* local to remote */
8efc0c15 566 if (remin == -1) {
c3773c6e 567 xasprintf(&bp, "%s -t -- %s", cmd, targ);
48e671d5 568 host = cleanhostname(thost);
2e73a022 569 if (do_cmd(host, tuser, bp, &remin,
ac93e676 570 &remout) < 0)
5260325f 571 exit(1);
8efc0c15 572 if (response() < 0)
573 exit(1);
5260325f 574 (void) xfree(bp);
8efc0c15 575 }
5260325f 576 source(1, argv + i);
8efc0c15 577 }
578 }
2b8dc5e3 579 xfree(arg);
8efc0c15 580}
581
582void
cf3248b8 583tolocal(int argc, char **argv)
8efc0c15 584{
8efc0c15 585 char *bp, *host, *src, *suser;
4116f5c0 586 arglist alist;
ac93e676 587 int i;
4116f5c0 588
589 memset(&alist, '\0', sizeof(alist));
590 alist.list = NULL;
8efc0c15 591
592 for (i = 0; i < argc - 1; i++) {
5260325f 593 if (!(src = colon(argv[i]))) { /* Local to local. */
4116f5c0 594 freeargs(&alist);
595 addargs(&alist, "%s", _PATH_CP);
596 if (iamrecursive)
597 addargs(&alist, "-r");
598 if (pflag)
599 addargs(&alist, "-p");
c3773c6e 600 addargs(&alist, "--");
4116f5c0 601 addargs(&alist, "%s", argv[i]);
602 addargs(&alist, "%s", argv[argc-1]);
603 if (do_local_cmd(&alist))
8efc0c15 604 ++errs;
8efc0c15 605 continue;
606 }
607 *src++ = 0;
608 if (*src == 0)
609 src = ".";
15748b4d 610 if ((host = strrchr(argv[i], '@')) == NULL) {
8efc0c15 611 host = argv[i];
612 suser = NULL;
613 } else {
614 *host++ = 0;
615 suser = argv[i];
616 if (*suser == '\0')
617 suser = pwd->pw_name;
8efc0c15 618 }
48e671d5 619 host = cleanhostname(host);
c3773c6e 620 xasprintf(&bp, "%s -f -- %s", cmd, src);
ac93e676 621 if (do_cmd(host, suser, bp, &remin, &remout) < 0) {
5260325f 622 (void) xfree(bp);
623 ++errs;
624 continue;
8efc0c15 625 }
5260325f 626 xfree(bp);
8efc0c15 627 sink(1, argv + argc - 1);
5260325f 628 (void) close(remin);
8efc0c15 629 remin = remout = -1;
630 }
631}
632
633void
cf3248b8 634source(int argc, char **argv)
8efc0c15 635{
636 struct stat stb;
637 static BUF buffer;
638 BUF *bp;
a15e7da1 639 off_t i, statbytes;
640 size_t amt;
a056dfa2 641 int fd = -1, haderr, indx;
5be98986 642 char *last, *name, buf[2048], encname[MAXPATHLEN];
0fc791ba 643 int len;
8efc0c15 644
645 for (indx = 0; indx < argc; ++indx) {
5260325f 646 name = argv[indx];
8efc0c15 647 statbytes = 0;
0fc791ba 648 len = strlen(name);
649 while (len > 1 && name[len-1] == '/')
650 name[--len] = '\0';
4634ee16 651 if ((fd = open(name, O_RDONLY|O_NONBLOCK, 0)) < 0)
8efc0c15 652 goto syserr;
5be98986 653 if (strchr(name, '\n') != NULL) {
654 strnvis(encname, name, sizeof(encname), VIS_NL);
655 name = encname;
656 }
8efc0c15 657 if (fstat(fd, &stb) < 0) {
658syserr: run_err("%s: %s", name, strerror(errno));
659 goto next;
660 }
a15e7da1 661 if (stb.st_size < 0) {
662 run_err("%s: %s", name, "Negative file size");
663 goto next;
664 }
4634ee16 665 unset_nonblock(fd);
8efc0c15 666 switch (stb.st_mode & S_IFMT) {
667 case S_IFREG:
668 break;
669 case S_IFDIR:
670 if (iamrecursive) {
671 rsource(name, &stb);
672 goto next;
673 }
674 /* FALLTHROUGH */
675 default:
676 run_err("%s: not a regular file", name);
677 goto next;
678 }
679 if ((last = strrchr(name, '/')) == NULL)
680 last = name;
681 else
682 ++last;
683 curfile = last;
684 if (pflag) {
685 /*
686 * Make it compatible with possible future
687 * versions expecting microseconds.
688 */
0ed28836 689 (void) snprintf(buf, sizeof buf, "T%lu 0 %lu 0\n",
415e3743 690 (u_long) (stb.st_mtime < 0 ? 0 : stb.st_mtime),
691 (u_long) (stb.st_atime < 0 ? 0 : stb.st_atime));
692 if (verbose_mode) {
693 fprintf(stderr, "File mtime %ld atime %ld\n",
694 (long)stb.st_mtime, (long)stb.st_atime);
695 fprintf(stderr, "Sending file timestamps: %s",
696 buf);
697 }
dc54438a 698 (void) atomicio(vwrite, remout, buf, strlen(buf));
8efc0c15 699 if (response() < 0)
700 goto next;
701 }
702#define FILEMODEMASK (S_ISUID|S_ISGID|S_IRWXU|S_IRWXG|S_IRWXO)
bf1d27bd 703 snprintf(buf, sizeof buf, "C%04o %lld %s\n",
1e3b8b07 704 (u_int) (stb.st_mode & FILEMODEMASK),
419094c6 705 (long long)stb.st_size, last);
5260325f 706 if (verbose_mode) {
707 fprintf(stderr, "Sending file modes: %s", buf);
5260325f 708 }
dc54438a 709 (void) atomicio(vwrite, remout, buf, strlen(buf));
8efc0c15 710 if (response() < 0)
711 goto next;
6484acf8 712 if ((bp = allocbuf(&buffer, fd, COPY_BUFLEN)) == NULL) {
29accf74 713next: if (fd != -1) {
714 (void) close(fd);
715 fd = -1;
716 }
8efc0c15 717 continue;
718 }
b65c3807 719 if (showprogress)
720 start_progress_meter(curfile, stb.st_size, &statbytes);
6484acf8 721 set_nonblock(remout);
8efc0c15 722 for (haderr = i = 0; i < stb.st_size; i += bp->cnt) {
723 amt = bp->cnt;
a15e7da1 724 if (i + (off_t)amt > stb.st_size)
8efc0c15 725 amt = stb.st_size - i;
726 if (!haderr) {
6484acf8 727 if (atomicio(read, fd, bp->buf, amt) != amt)
05624c18 728 haderr = errno;
8efc0c15 729 }
6484acf8 730 /* Keep writing after error to retain sync */
731 if (haderr) {
732 (void)atomicio(vwrite, remout, bp->buf, amt);
733 continue;
8efc0c15 734 }
6484acf8 735 if (scpio(vwrite, remout, bp->buf, amt,
736 &statbytes) != amt)
737 haderr = errno;
8efc0c15 738 }
6484acf8 739 unset_nonblock(remout);
5260325f 740 if (showprogress)
b65c3807 741 stop_progress_meter();
8efc0c15 742
29accf74 743 if (fd != -1) {
744 if (close(fd) < 0 && !haderr)
745 haderr = errno;
746 fd = -1;
747 }
8efc0c15 748 if (!haderr)
dc54438a 749 (void) atomicio(vwrite, remout, "", 1);
8efc0c15 750 else
751 run_err("%s: %s", name, strerror(haderr));
5260325f 752 (void) response();
8efc0c15 753 }
754}
755
756void
cf3248b8 757rsource(char *name, struct stat *statp)
8efc0c15 758{
759 DIR *dirp;
760 struct dirent *dp;
761 char *last, *vect[1], path[1100];
762
763 if (!(dirp = opendir(name))) {
764 run_err("%s: %s", name, strerror(errno));
765 return;
766 }
767 last = strrchr(name, '/');
768 if (last == 0)
769 last = name;
770 else
771 last++;
772 if (pflag) {
0ed28836 773 (void) snprintf(path, sizeof(path), "T%lu 0 %lu 0\n",
1e3b8b07 774 (u_long) statp->st_mtime,
775 (u_long) statp->st_atime);
dc54438a 776 (void) atomicio(vwrite, remout, path, strlen(path));
8efc0c15 777 if (response() < 0) {
778 closedir(dirp);
779 return;
780 }
781 }
0ed28836 782 (void) snprintf(path, sizeof path, "D%04o %d %.1024s\n",
1e3b8b07 783 (u_int) (statp->st_mode & FILEMODEMASK), 0, last);
5260325f 784 if (verbose_mode)
785 fprintf(stderr, "Entering directory: %s", path);
dc54438a 786 (void) atomicio(vwrite, remout, path, strlen(path));
8efc0c15 787 if (response() < 0) {
788 closedir(dirp);
789 return;
790 }
e5ff6ecf 791 while ((dp = readdir(dirp)) != NULL) {
8efc0c15 792 if (dp->d_ino == 0)
793 continue;
794 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
795 continue;
796 if (strlen(name) + 1 + strlen(dp->d_name) >= sizeof(path) - 1) {
797 run_err("%s/%s: name too long", name, dp->d_name);
798 continue;
799 }
0ed28836 800 (void) snprintf(path, sizeof path, "%s/%s", name, dp->d_name);
8efc0c15 801 vect[0] = path;
802 source(1, vect);
803 }
5260325f 804 (void) closedir(dirp);
dc54438a 805 (void) atomicio(vwrite, remout, "E\n", 2);
5260325f 806 (void) response();
8efc0c15 807}
808
6ea3c52a 809void
810bwlimit(int amount)
811{
812 static struct timeval bwstart, bwend;
813 static int lamt, thresh = 16384;
ca75d7de 814 u_int64_t waitlen;
6ea3c52a 815 struct timespec ts, rm;
816
817 if (!timerisset(&bwstart)) {
818 gettimeofday(&bwstart, NULL);
819 return;
820 }
821
822 lamt += amount;
823 if (lamt < thresh)
824 return;
825
826 gettimeofday(&bwend, NULL);
827 timersub(&bwend, &bwstart, &bwend);
828 if (!timerisset(&bwend))
829 return;
830
831 lamt *= 8;
ca75d7de 832 waitlen = (double)1000000L * lamt / limit_rate;
6ea3c52a 833
ca75d7de 834 bwstart.tv_sec = waitlen / 1000000L;
835 bwstart.tv_usec = waitlen % 1000000L;
6ea3c52a 836
837 if (timercmp(&bwstart, &bwend, >)) {
838 timersub(&bwstart, &bwend, &bwend);
839
840 /* Adjust the wait time */
841 if (bwend.tv_sec) {
842 thresh /= 2;
843 if (thresh < 2048)
844 thresh = 2048;
6484acf8 845 } else if (bwend.tv_usec < 10000) {
6ea3c52a 846 thresh *= 2;
6484acf8 847 if (thresh > COPY_BUFLEN * 4)
848 thresh = COPY_BUFLEN * 4;
6ea3c52a 849 }
850
851 TIMEVAL_TO_TIMESPEC(&bwend, &ts);
852 while (nanosleep(&ts, &rm) == -1) {
853 if (errno != EINTR)
854 break;
855 ts = rm;
856 }
857 }
858
859 lamt = 0;
860 gettimeofday(&bwstart, NULL);
861}
862
8efc0c15 863void
cf3248b8 864sink(int argc, char **argv)
8efc0c15 865{
866 static BUF buffer;
867 struct stat stb;
5260325f 868 enum {
869 YES, NO, DISPLAYED
870 } wrerr;
8efc0c15 871 BUF *bp;
05624c18 872 off_t i;
2ceb8101 873 size_t j, count;
ac93e676 874 int amt, exists, first, ofd;
875 mode_t mode, omode, mask;
b65c3807 876 off_t size, statbytes;
14a9a859 877 int setimes, targisdir, wrerrno = 0;
8efc0c15 878 char ch, *cp, *np, *targ, *why, *vect[1], buf[2048];
188adeb2 879 struct timeval tv[2];
8efc0c15 880
36967a16 881#define atime tv[0]
882#define mtime tv[1]
007607ab 883#define SCREWUP(str) { why = str; goto screwup; }
8efc0c15 884
885 setimes = targisdir = 0;
886 mask = umask(0);
887 if (!pflag)
5260325f 888 (void) umask(mask);
8efc0c15 889 if (argc != 1) {
890 run_err("ambiguous target");
891 exit(1);
892 }
893 targ = *argv;
894 if (targetshouldbedirectory)
895 verifydir(targ);
5260325f 896
dc54438a 897 (void) atomicio(vwrite, remout, "", 1);
8efc0c15 898 if (stat(targ, &stb) == 0 && S_ISDIR(stb.st_mode))
899 targisdir = 1;
900 for (first = 1;; first = 0) {
901 cp = buf;
05624c18 902 if (atomicio(read, remin, cp, 1) != 1)
8efc0c15 903 return;
904 if (*cp++ == '\n')
905 SCREWUP("unexpected <newline>");
906 do {
1d1ffb87 907 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
8efc0c15 908 SCREWUP("lost connection");
909 *cp++ = ch;
910 } while (cp < &buf[sizeof(buf) - 1] && ch != '\n');
911 *cp = 0;
f09aa22c 912 if (verbose_mode)
913 fprintf(stderr, "Sink: %s", buf);
8efc0c15 914
915 if (buf[0] == '\01' || buf[0] == '\02') {
916 if (iamremote == 0)
dc54438a 917 (void) atomicio(vwrite, STDERR_FILENO,
71c0d06a 918 buf + 1, strlen(buf + 1));
8efc0c15 919 if (buf[0] == '\02')
920 exit(1);
921 ++errs;
922 continue;
923 }
924 if (buf[0] == 'E') {
dc54438a 925 (void) atomicio(vwrite, remout, "", 1);
8efc0c15 926 return;
927 }
8efc0c15 928 if (ch == '\n')
929 *--cp = 0;
930
8efc0c15 931 cp = buf;
932 if (*cp == 'T') {
933 setimes++;
934 cp++;
36967a16 935 mtime.tv_sec = strtol(cp, &cp, 10);
936 if (!cp || *cp++ != ' ')
8efc0c15 937 SCREWUP("mtime.sec not delimited");
36967a16 938 mtime.tv_usec = strtol(cp, &cp, 10);
939 if (!cp || *cp++ != ' ')
8efc0c15 940 SCREWUP("mtime.usec not delimited");
36967a16 941 atime.tv_sec = strtol(cp, &cp, 10);
942 if (!cp || *cp++ != ' ')
8efc0c15 943 SCREWUP("atime.sec not delimited");
36967a16 944 atime.tv_usec = strtol(cp, &cp, 10);
945 if (!cp || *cp++ != '\0')
8efc0c15 946 SCREWUP("atime.usec not delimited");
dc54438a 947 (void) atomicio(vwrite, remout, "", 1);
8efc0c15 948 continue;
949 }
950 if (*cp != 'C' && *cp != 'D') {
951 /*
952 * Check for the case "rcp remote:foo\* local:bar".
953 * In this case, the line "No match." can be returned
954 * by the shell before the rcp command on the remote is
955 * executed so the ^Aerror_message convention isn't
956 * followed.
957 */
958 if (first) {
959 run_err("%s", cp);
960 exit(1);
961 }
962 SCREWUP("expected control record");
963 }
964 mode = 0;
965 for (++cp; cp < buf + 5; cp++) {
966 if (*cp < '0' || *cp > '7')
967 SCREWUP("bad mode");
968 mode = (mode << 3) | (*cp - '0');
969 }
970 if (*cp++ != ' ')
971 SCREWUP("mode not delimited");
972
e5ff6ecf 973 for (size = 0; isdigit(*cp);)
8efc0c15 974 size = size * 10 + (*cp++ - '0');
975 if (*cp++ != ' ')
976 SCREWUP("size not delimited");
f09aa22c 977 if ((strchr(cp, '/') != NULL) || (strcmp(cp, "..") == 0)) {
978 run_err("error: unexpected filename: %s", cp);
979 exit(1);
980 }
8efc0c15 981 if (targisdir) {
982 static char *namebuf;
2ceb8101 983 static size_t cursize;
8efc0c15 984 size_t need;
985
986 need = strlen(targ) + strlen(cp) + 250;
0ed28836 987 if (need > cursize) {
988 if (namebuf)
989 xfree(namebuf);
5260325f 990 namebuf = xmalloc(need);
0ed28836 991 cursize = need;
992 }
993 (void) snprintf(namebuf, need, "%s%s%s", targ,
39f9599a 994 strcmp(targ, "/") ? "/" : "", cp);
8efc0c15 995 np = namebuf;
996 } else
997 np = targ;
998 curfile = cp;
999 exists = stat(np, &stb) == 0;
1000 if (buf[0] == 'D') {
1001 int mod_flag = pflag;
f09aa22c 1002 if (!iamrecursive)
1003 SCREWUP("received directory without -r");
8efc0c15 1004 if (exists) {
1005 if (!S_ISDIR(stb.st_mode)) {
1006 errno = ENOTDIR;
1007 goto bad;
1008 }
1009 if (pflag)
5260325f 1010 (void) chmod(np, mode);
8efc0c15 1011 } else {
5260325f 1012 /* Handle copying from a read-only
1013 directory */
8efc0c15 1014 mod_flag = 1;
1015 if (mkdir(np, mode | S_IRWXU) < 0)
1016 goto bad;
1017 }
5c470997 1018 vect[0] = xstrdup(np);
8efc0c15 1019 sink(1, vect);
1020 if (setimes) {
1021 setimes = 0;
e68225b2 1022 if (utimes(vect[0], tv) < 0)
5260325f 1023 run_err("%s: set times: %s",
e68225b2 1024 vect[0], strerror(errno));
8efc0c15 1025 }
1026 if (mod_flag)
e68225b2 1027 (void) chmod(vect[0], mode);
1028 if (vect[0])
1029 xfree(vect[0]);
8efc0c15 1030 continue;
1031 }
1032 omode = mode;
1033 mode |= S_IWRITE;
3e4fc5f9 1034 if ((ofd = open(np, O_WRONLY|O_CREAT, mode)) < 0) {
8efc0c15 1035bad: run_err("%s: %s", np, strerror(errno));
1036 continue;
1037 }
dc54438a 1038 (void) atomicio(vwrite, remout, "", 1);
6484acf8 1039 if ((bp = allocbuf(&buffer, ofd, COPY_BUFLEN)) == NULL) {
5260325f 1040 (void) close(ofd);
8efc0c15 1041 continue;
1042 }
1043 cp = bp->buf;
1044 wrerr = NO;
1045
8efc0c15 1046 statbytes = 0;
b65c3807 1047 if (showprogress)
1048 start_progress_meter(curfile, size, &statbytes);
6484acf8 1049 set_nonblock(remin);
1050 for (count = i = 0; i < size; i += bp->cnt) {
1051 amt = bp->cnt;
8efc0c15 1052 if (i + amt > size)
1053 amt = size - i;
1054 count += amt;
1055 do {
6484acf8 1056 j = scpio(read, remin, cp, amt, &statbytes);
05624c18 1057 if (j == 0) {
6484acf8 1058 run_err("%s", j != EPIPE ?
1059 strerror(errno) :
e5ff6ecf 1060 "dropped connection");
8efc0c15 1061 exit(1);
1062 }
1063 amt -= j;
1064 cp += j;
8efc0c15 1065 } while (amt > 0);
b6453d99 1066
8efc0c15 1067 if (count == bp->cnt) {
1068 /* Keep reading so we stay sync'd up. */
1069 if (wrerr == NO) {
05624c18 1070 if (atomicio(vwrite, ofd, bp->buf,
1071 count) != count) {
8efc0c15 1072 wrerr = YES;
05624c18 1073 wrerrno = errno;
8efc0c15 1074 }
1075 }
1076 count = 0;
1077 cp = bp->buf;
1078 }
1079 }
6484acf8 1080 unset_nonblock(remin);
8efc0c15 1081 if (showprogress)
b65c3807 1082 stop_progress_meter();
8efc0c15 1083 if (count != 0 && wrerr == NO &&
05624c18 1084 atomicio(vwrite, ofd, bp->buf, count) != count) {
8efc0c15 1085 wrerr = YES;
05624c18 1086 wrerrno = errno;
8efc0c15 1087 }
91044296 1088 if (wrerr == NO && (!exists || S_ISREG(stb.st_mode)) &&
1089 ftruncate(ofd, size) != 0) {
8efc0c15 1090 run_err("%s: truncate: %s", np, strerror(errno));
1091 wrerr = DISPLAYED;
1092 }
8efc0c15 1093 if (pflag) {
1094 if (exists || omode != mode)
77bb0bca 1095#ifdef HAVE_FCHMOD
7e693c81 1096 if (fchmod(ofd, omode)) {
77bb0bca 1097#else /* HAVE_FCHMOD */
7e693c81 1098 if (chmod(np, omode)) {
77bb0bca 1099#endif /* HAVE_FCHMOD */
8efc0c15 1100 run_err("%s: set mode: %s",
e5ff6ecf 1101 np, strerror(errno));
7e693c81 1102 wrerr = DISPLAYED;
1103 }
8efc0c15 1104 } else {
1105 if (!exists && omode != mode)
77bb0bca 1106#ifdef HAVE_FCHMOD
7e693c81 1107 if (fchmod(ofd, omode & ~mask)) {
77bb0bca 1108#else /* HAVE_FCHMOD */
7e693c81 1109 if (chmod(np, omode & ~mask)) {
77bb0bca 1110#endif /* HAVE_FCHMOD */
8efc0c15 1111 run_err("%s: set mode: %s",
e5ff6ecf 1112 np, strerror(errno));
7e693c81 1113 wrerr = DISPLAYED;
1114 }
8efc0c15 1115 }
704b1659 1116 if (close(ofd) == -1) {
1117 wrerr = YES;
1118 wrerrno = errno;
1119 }
5260325f 1120 (void) response();
8efc0c15 1121 if (setimes && wrerr == NO) {
1122 setimes = 0;
188adeb2 1123 if (utimes(np, tv) < 0) {
8efc0c15 1124 run_err("%s: set times: %s",
e5ff6ecf 1125 np, strerror(errno));
8efc0c15 1126 wrerr = DISPLAYED;
1127 }
1128 }
5260325f 1129 switch (wrerr) {
8efc0c15 1130 case YES:
1131 run_err("%s: %s", np, strerror(wrerrno));
1132 break;
1133 case NO:
dc54438a 1134 (void) atomicio(vwrite, remout, "", 1);
8efc0c15 1135 break;
1136 case DISPLAYED:
1137 break;
1138 }
1139 }
1140screwup:
1141 run_err("protocol error: %s", why);
1142 exit(1);
1143}
1144
1145int
d5bb9418 1146response(void)
8efc0c15 1147{
1148 char ch, *cp, resp, rbuf[2048];
1149
1d1ffb87 1150 if (atomicio(read, remin, &resp, sizeof(resp)) != sizeof(resp))
8efc0c15 1151 lostconn(0);
1152
1153 cp = rbuf;
5260325f 1154 switch (resp) {
1155 case 0: /* ok */
8efc0c15 1156 return (0);
1157 default:
1158 *cp++ = resp;
1159 /* FALLTHROUGH */
5260325f 1160 case 1: /* error, followed by error msg */
1161 case 2: /* fatal error, "" */
8efc0c15 1162 do {
1d1ffb87 1163 if (atomicio(read, remin, &ch, sizeof(ch)) != sizeof(ch))
8efc0c15 1164 lostconn(0);
1165 *cp++ = ch;
1166 } while (cp < &rbuf[sizeof(rbuf) - 1] && ch != '\n');
1167
1168 if (!iamremote)
dc54438a 1169 (void) atomicio(vwrite, STDERR_FILENO, rbuf, cp - rbuf);
8efc0c15 1170 ++errs;
1171 if (resp == 1)
1172 return (-1);
1173 exit(1);
1174 }
1175 /* NOTREACHED */
1176}
1177
1178void
d5bb9418 1179usage(void)
8efc0c15 1180{
f1278af7 1181 (void) fprintf(stderr,
433e60ac 1182 "usage: scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file]\n"
1183 " [-l limit] [-o ssh_option] [-P port] [-S program]\n"
d0ac0d65 1184 " [[user@]host1:]file1 ... [[user@]host2:]file2\n");
8efc0c15 1185 exit(1);
1186}
1187
1188void
5260325f 1189run_err(const char *fmt,...)
8efc0c15 1190{
1191 static FILE *fp;
1192 va_list ap;
8efc0c15 1193
1194 ++errs;
d6157b67 1195 if (fp != NULL || (remout != -1 && (fp = fdopen(remout, "w")))) {
1196 (void) fprintf(fp, "%c", 0x01);
1197 (void) fprintf(fp, "scp: ");
1198 va_start(ap, fmt);
1199 (void) vfprintf(fp, fmt, ap);
1200 va_end(ap);
1201 (void) fprintf(fp, "\n");
1202 (void) fflush(fp);
1203 }
5260325f 1204
1205 if (!iamremote) {
b92964b7 1206 va_start(ap, fmt);
5260325f 1207 vfprintf(stderr, fmt, ap);
b92964b7 1208 va_end(ap);
5260325f 1209 fprintf(stderr, "\n");
1210 }
8efc0c15 1211}
1212
8efc0c15 1213void
cf3248b8 1214verifydir(char *cp)
8efc0c15 1215{
1216 struct stat stb;
1217
1218 if (!stat(cp, &stb)) {
1219 if (S_ISDIR(stb.st_mode))
1220 return;
1221 errno = ENOTDIR;
1222 }
1223 run_err("%s: %s", cp, strerror(errno));
8f4ab41b 1224 killchild(0);
8efc0c15 1225}
1226
1227int
cf3248b8 1228okname(char *cp0)
8efc0c15 1229{
1230 int c;
1231 char *cp;
1232
1233 cp = cp0;
1234 do {
cb220a93 1235 c = (int)*cp;
8efc0c15 1236 if (c & 0200)
1237 goto bad;
0ec7661a 1238 if (!isalpha(c) && !isdigit(c)) {
1239 switch (c) {
1240 case '\'':
1241 case '"':
1242 case '`':
1243 case ' ':
1244 case '#':
1245 goto bad;
1246 default:
1247 break;
1248 }
1249 }
8efc0c15 1250 } while (*++cp);
1251 return (1);
1252
c8d54615 1253bad: fprintf(stderr, "%s: invalid user name\n", cp0);
8efc0c15 1254 return (0);
1255}
1256
1257BUF *
cf3248b8 1258allocbuf(BUF *bp, int fd, int blksize)
8efc0c15 1259{
1260 size_t size;
98a7c37b 1261#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
8efc0c15 1262 struct stat stb;
1263
1264 if (fstat(fd, &stb) < 0) {
1265 run_err("fstat: %s", strerror(errno));
1266 return (0);
1267 }
a0a14bcd 1268 size = roundup(stb.st_blksize, blksize);
1269 if (size == 0)
5260325f 1270 size = blksize;
98a7c37b 1271#else /* HAVE_STRUCT_STAT_ST_BLKSIZE */
2b87da3b 1272 size = blksize;
98a7c37b 1273#endif /* HAVE_STRUCT_STAT_ST_BLKSIZE */
8efc0c15 1274 if (bp->cnt >= size)
1275 return (bp);
5260325f 1276 if (bp->buf == NULL)
1277 bp->buf = xmalloc(size);
1278 else
c5d10563 1279 bp->buf = xrealloc(bp->buf, 1, size);
46660a9e 1280 memset(bp->buf, 0, size);
8efc0c15 1281 bp->cnt = size;
1282 return (bp);
1283}
1284
1285void
cf3248b8 1286lostconn(int signo)
8efc0c15 1287{
1288 if (!iamremote)
08c260ea 1289 write(STDERR_FILENO, "lost connection\n", 16);
1290 if (signo)
1291 _exit(1);
1292 else
1293 exit(1);
8efc0c15 1294}
This page took 0.566484 seconds and 5 git commands to generate.