Debian Codegraph @

src/lrz.c

raw ◀ ▶
Source reconstructed at patch state 0 of 8 — not the installed (fully patched) source.
8 Debian patches touch this file
1 /*
2 lrz - receive files with x/y/zmodem
3 Copyright (C) until 1988 Chuck Forsberg (Omen Technology INC)
4 Copyright (C) 1994 Matt Porter, Michael D. Black
5 Copyright (C) 1996, 1997 Uwe Ohse
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21
22 originally written by Chuck Forsberg
23 */
24
25 #include "zglobal.h"
26
27 #define SS_NORMAL 0
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <getopt.h>
35
36 #ifdef HAVE_UTIME_H
37 #include <utime.h>
38 #endif
39
40 #include "timing.h"
41 #include "long-options.h"
42 #include "xstrtoul.h"
43 #include "error.h"
44
45 #ifndef STRICT_PROTOTYPES
46 extern time_t time();
47 extern char *strerror();
48 #endif
49
50 #ifndef HAVE_ERRNO_DECLARATION
51 extern int errno;
52 #endif
53
54 #define MAX_BLOCK 8192
55
56 /*
57 * Max value for HOWMANY is 255 if NFGVMIN is not defined.
58 * A larger value reduces system overhead but may evoke kernel bugs.
59 * 133 corresponds to an XMODEM/CRC sector
60 */
61 #ifndef HOWMANY
62 #ifdef NFGVMIN
63 #define HOWMANY MAX_BLOCK
64 #else
65 #define HOWMANY 255
66 #endif
67 #endif
68
69 unsigned Baudrate = 2400;
70
71 FILE *fout;
72 char *dszlogpath;
73
74
75 int Lastrx;
76 int Crcflg;
77 int Firstsec;
78 int errors;
79 int Restricted=1; /* restricted; no /.. or ../ in filenames */
80 int Readnum = HOWMANY; /* Number of bytes to ask for in read() from modem */
81 int skip_if_not_found;
82
83 char *Pathname;
84 const char *program_name; /* the name by which we were called */
85
86 int Topipe=0;
87 int MakeLCPathname=TRUE; /* make received pathname lower case */
88 int Verbose=0;
89 int Quiet=0; /* overrides logic that would otherwise set verbose */
90 int Nflag = 0; /* Don't really transfer files */
91 int Rxclob=FALSE; /* Clobber existing file */
92 int Rxbinary=FALSE; /* receive all files in bin mode */
93 int Rxascii=FALSE; /* receive files in ascii (translate) mode */
94 int Thisbinary; /* current file is to be received in bin mode */
95 int try_resume=FALSE;
96 int allow_remote_commands=FALSE;
97 int junk_path=FALSE;
98 int no_timeout=FALSE;
99 enum zm_type_enum protocol;
100 int under_rsh=FALSE;
101 int zmodem_requested=FALSE;
102
103 #ifdef SEGMENTS
104 static int chinseg = 0; /* Number of characters received in this data seg */
105 #endif
106 static char *secbuf;
107
108 #ifdef ENABLE_TIMESYNC
109 static int timesync_flag=0;
110 static int in_timesync=0;
111 #endif
112 int tcp_socket=-1;
113 int tcp_flag=0;
114 char *tcp_server_address=NULL;
115
116 #if defined(F_GETFD) && defined(F_SETFD) && defined(O_SYNC)
117 static int o_sync = 0;
118 #endif
119 static int rzfiles __P ((struct zm_fileinfo *));
120 static int tryz __P ((void));
121 static void checkpath __P ((const char *name));
122 static void chkinvok __P ((const char *s));
123 static void report __P ((int sct));
124 static void uncaps __P ((char *s));
125 static int IsAnyLower __P ((const char *s));
126 static int putsec __P ((struct zm_fileinfo *zi, char *buf, size_t n));
127 static int make_dirs __P ((char *pathname));
128 static int procheader __P ((char *name, struct zm_fileinfo *));
129 static int wcgetsec __P ((size_t *Blklen, char *rxbuf, unsigned int maxtime));
130 static int wcrx __P ((struct zm_fileinfo *));
131 static int wcrxpn __P ((struct zm_fileinfo *, char *rpn));
132 static int wcreceive __P ((int argc, char **argp));
133 static int rzfile __P ((struct zm_fileinfo *));
134 static void usage __P ((int exitcode, const char *what));
135 static void usage1 __P ((int exitcode));
136 static void exec2 __P ((const char *s));
137 static int closeit __P ((struct zm_fileinfo *));
138 static void ackbibi __P ((void));
139 static int sys2 __P ((const char *s));
140 static void zmputs __P ((const char *s));
141 static size_t getfree __P ((void));
142
143 static long buffersize=32768;
144 static unsigned long min_bps=0;
145 static long min_bps_time=120;
146
147 char Lzmanag; /* Local file management request */
148 char zconv; /* ZMODEM file conversion request */
149 char zmanag; /* ZMODEM file management request */
150 char ztrans; /* ZMODEM file transport request */
151 int Zctlesc; /* Encode control characters */
152 int Zrwindow = 1400; /* RX window size (controls garbage count) */
153
154 int tryzhdrtype=ZRINIT; /* Header type to send corresponding to Last rx close */
155 time_t stop_time;
156
157 #ifdef ENABLE_SYSLOG
158 # if defined(ENABLE_SYSLOG_FORCE) || defined(ENABLE_SYSLOG_DEFAULT)
159 int enable_syslog=TRUE;
160 # else
161 int enable_syslog=FALSE;
162 # endif
163 #define DO_SYSLOG_FNAME(message) do { \
164 if (enable_syslog) { \
165 const char *shortname; \
166 if (!zi->fname) \
167 shortname="no.name"; \
168 else { \
169 shortname=strrchr(zi->fname,'/'); \
170 if (!shortname) \
171 shortname=zi->fname; \
172 else \
173 shortname++; \
174 } \
175 lsyslog message ; \
176 } \
177 } while(0)
178 #define DO_SYSLOG(message) do { \
179 if (enable_syslog) { \
180 lsyslog message ; \
181 } \
182 } while(0)
183 #else
184 #define DO_SYSLOG_FNAME(message) do { } while(0)
185 #define DO_SYSLOG(message) do { } while(0)
186 #endif
187
188
189 /* called by signal interrupt or terminate to clean things up */
190 RETSIGTYPE
191 bibi(int n)
192 {
193 if (zmodem_requested)
194 zmputs(Attn);
195 canit(STDOUT_FILENO);
196 io_mode(0,0);
197 if (dszlogpath) {
198 dszlog(dszlogpath, 'L', 0, 0, 0, errors, 0, 0);
199 }
200 error(128+n,0,_("caught signal %d; exiting"), n);
201 }
202
203 static struct option const long_options[] =
204 {
205 {"append", no_argument, NULL, '+'},
206 {"ascii", no_argument, NULL, 'a'},
207 {"binary", no_argument, NULL, 'b'},
208 {"bufsize", required_argument, NULL, 'B'},
209 {"allow-commands", no_argument, NULL, 'C'},
210 {"allow-remote-commands", no_argument, NULL, 'C'},
211 {"escape", no_argument, NULL, 'e'},
212 {"rename", no_argument, NULL, 'E'},
213 {"help", no_argument, NULL, 'h'},
214 {"crc-check", no_argument, NULL, 'H'},
215 {"junk-path", no_argument, NULL, 'j'},
216 {"errors", required_argument, NULL, 3},
217 {"disable-timeouts", no_argument, NULL, 'O'},
218 {"disable-timeout", no_argument, NULL, 'O'}, /* i can't get it right */
219 {"min-bps", required_argument, NULL, 'm'},
220 {"min-bps-time", required_argument, NULL, 'M'},
221 {"newer", no_argument, NULL, 'n'},
222 {"newer-or-longer", no_argument, NULL, 'N'},
223 {"protect", no_argument, NULL, 'p'},
224 {"resume", no_argument, NULL, 'r'},
225 {"restricted", no_argument, NULL, 'R'},
226 {"quiet", no_argument, NULL, 'q'},
227 {"stop-at", required_argument, NULL, 's'},
228 {"timesync", no_argument, NULL, 'S'},
229 {"timeout", required_argument, NULL, 't'},
230 {"keep-uppercase", no_argument, NULL, 'u'},
231 {"unrestrict", no_argument, NULL, 'U'},
232 {"verbose", no_argument, NULL, 'v'},
233 {"windowsize", required_argument, NULL, 'w'},
234 {"with-crc", no_argument, NULL, 'c'},
235 {"xmodem", no_argument, NULL, 'X'},
236 {"ymodem", no_argument, NULL, 1},
237 {"zmodem", no_argument, NULL, 'Z'},
238 {"overwrite", no_argument, NULL, 'y'},
239 {"null", no_argument, NULL, 'D'},
240 {"syslog", optional_argument, NULL , 2},
241 {"delay-startup", required_argument, NULL, 4},
242 {"o-sync", no_argument, NULL, 5},
243 {"o_sync", no_argument, NULL, 5},
244 {"tcp-server", no_argument, NULL, 6},
245 {"tcp-client", required_argument, NULL, 7},
246 {NULL,0,NULL,0}
247 };
248
249 static void
250 show_version(void)
251 {
252 printf ("%s (GNU %s) %s\n", program_name, PACKAGE, VERSION);
253 }
254
255 int
256 main(int argc, char *argv[])
257 {
258 register char *cp;
259 register int npats;
260 char **patts=NULL; /* keep compiler quiet */
261 int exitcode=0;
262 int c;
263 unsigned int startup_delay=0;
264
265 Rxtimeout = 100;
266 setbuf(stderr, NULL);
267 if ((cp=getenv("SHELL")) && (strstr(cp, "rsh") || strstr(cp, "rksh")
268 || strstr(cp,"rbash") || strstr(cp, "rshell")))
269 under_rsh=TRUE;
270 if ((cp=getenv("ZMODEM_RESTRICTED"))!=NULL)
271 Restricted=2;
272
273 dszlogpath = getenv("DSZLOG");
274 if (access(dszlogpath, F_OK) == 0) {
275 if (unlink(dszlogpath) != 0) {
276 perror("unlink dszlog");
277 }
278 }
279
280 /* make temporary and unfinished files */
281 umask(0077);
282
283 from_cu();
284 chkinvok(argv[0]); /* if called as [-]rzCOMMAND set flag */
285
286 #ifdef ENABLE_SYSLOG
287 openlog(program_name,LOG_PID,ENABLE_SYSLOG);
288 #endif
289
290 setlocale (LC_ALL, "");
291 setlocale (LC_TIME, "C"); /* keep syslog timestamps untranslated, #57884 */
292 bindtextdomain (PACKAGE, LOCALEDIR);
293 textdomain (PACKAGE);
294
295 parse_long_options (argc, argv, show_version, usage1);
296
297 while ((c = getopt_long (argc, argv,
298 "a+bB:cCDeEhm:M:OprRqs:St:uUvw:XZy",
299 long_options, (int *) 0)) != EOF)
300 {
301 unsigned long int tmp;
302 char *tmpptr;
303 enum strtol_error s_err;
304
305 switch (c)
306 {
307 case 0:
308 break;
309 case '+': Lzmanag = ZF1_ZMAPND; break;
310 case 'a': Rxascii=TRUE; break;
311 case 'b': Rxbinary=TRUE; break;
312 case 'B':
313 if (strcmp(optarg,"auto")==0)
314 buffersize=-1;
315 else
316 buffersize=strtol(optarg,NULL,10);
317 break;
318 case 'c': Crcflg=TRUE; break;
319 case 'C': allow_remote_commands=TRUE; break;
320 case 'D': Nflag = TRUE; break;
321 case 'E': Lzmanag = ZF1_ZMCHNG; break;
322 case 'e': Zctlesc = 1; break;
323 case 'h': usage(0,NULL); break;
324 case 'H': Lzmanag= ZF1_ZMCRC; break;
325 case 'j': junk_path=TRUE; break;
326 case 'm':
327 s_err = xstrtoul (optarg, &tmpptr, 0, &tmp, "km");
328 min_bps = tmp;
329 if (s_err != LONGINT_OK)
330 STRTOL_FATAL_ERROR (optarg, _("min_bps"), s_err);
331 break;
332 case 'M':
333 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
334 min_bps_time = tmp;
335 if (s_err != LONGINT_OK)
336 STRTOL_FATAL_ERROR (optarg, _("min_bps_time"), s_err);
337 if (min_bps_time<=1)
338 usage(2,_("min_bps_time must be > 1"));
339 break;
340 case 'N': Lzmanag = ZF1_ZMNEWL; break;
341 case 'n': Lzmanag = ZF1_ZMNEW; break;
342 case 'O': no_timeout=TRUE; break;
343 case 'p': Lzmanag = ZF1_ZMPROT; break;
344 case 'q': Quiet=TRUE; Verbose=0; break;
345 case 's':
346 if (isdigit((unsigned char) (*optarg))) {
347 struct tm *tm;
348 time_t t;
349 int hh,mm;
350 char *nex;
351
352 hh = strtoul (optarg, &nex, 10);
353 if (hh>23)
354 usage(2,_("hour to large (0..23)"));
355 if (*nex!=':')
356 usage(2, _("unparsable stop time\n"));
357 nex++;
358 mm = strtoul (optarg, &nex, 10);
359 if (mm>59)
360 usage(2,_("minute to large (0..59)"));
361
362 t=time(NULL);
363 tm=localtime(&t);
364 tm->tm_hour=hh;
365 tm->tm_min=hh;
366 stop_time=mktime(tm);
367 if (stop_time<t)
368 stop_time+=86400L; /* one day more */
369 if (stop_time - t <10)
370 usage(2,_("stop time to small"));
371 } else {
372 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
373 stop_time = tmp + time(0);
374 if (s_err != LONGINT_OK)
375 STRTOL_FATAL_ERROR (optarg, _("stop-at"), s_err);
376 if (tmp<10)
377 usage(2,_("stop time to small"));
378 }
379 break;
380
381
382 case 'r':
383 if (try_resume)
384 Lzmanag= ZF1_ZMCRC;
385 else
386 try_resume=TRUE;
387 break;
388 case 'R': Restricted++; break;
389 case 'S':
390 #ifdef ENABLE_TIMESYNC
391 timesync_flag++;
392 if (timesync_flag==2) {
393 #ifdef HAVE_SETTIMEOFDAY
394 error(0,0,_("don't have settimeofday, will not set time\n"));
395 #endif
396 if (getuid()!=0)
397 error(0,0,
398 _("not running as root (this is good!), can not set time\n"));
399 }
400 #endif
401 break;
402 case 't':
403 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
404 Rxtimeout = tmp;
405 if (s_err != LONGINT_OK)
406 STRTOL_FATAL_ERROR (optarg, _("timeout"), s_err);
407 if (Rxtimeout<10 || Rxtimeout>1000)
408 usage(2,_("timeout out of range 10..1000"));
409 break;
410 case 'w':
411 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
412 Zrwindow = tmp;
413 if (s_err != LONGINT_OK)
414 STRTOL_FATAL_ERROR (optarg, _("window size"), s_err);
415 break;
416 case 'u':
417 MakeLCPathname=FALSE; break;
418 case 'U':
419 if (!under_rsh)
420 Restricted=0;
421 else {
422 DO_SYSLOG((LOG_INFO,"--unrestrict option used under restricted shell"));
423 error(1,0,
424 _("security violation: can't do that under restricted shell\n"));
425 }
426 break;
427 case 'v':
428 ++Verbose; break;
429 case 'X': protocol=ZM_XMODEM; break;
430 case 1: protocol=ZM_YMODEM; break;
431 case 'Z': protocol=ZM_ZMODEM; break;
432 case 'y':
433 Rxclob=TRUE; break;
434 case 2:
435 #ifdef ENABLE_SYSLOG
436 # ifndef ENABLE_SYSLOG_FORCE
437 if (optarg && (!strcmp(optarg,"off") || !strcmp(optarg,"no"))) {
438 if (under_rsh)
439 error(0,0, _("cannot turnoff syslog"));
440 else
441 enable_syslog=FALSE;
442 }
443 else
444 enable_syslog=TRUE;
445 # else
446 error(0,0, _("cannot turnoff syslog"));
447 # endif
448 #endif
449 case 3:
450 s_err = xstrtoul (optarg, NULL, 0, &tmp, "km");
451 bytes_per_error = tmp;
452 if (s_err != LONGINT_OK)
453 STRTOL_FATAL_ERROR (optarg, _("bytes_per_error"), s_err);
454 if (bytes_per_error<100)
455 usage(2,_("bytes-per-error should be >100"));
456 break;
457 case 4:
458 s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
459 startup_delay = tmp;
460 if (s_err != LONGINT_OK)
461 STRTOL_FATAL_ERROR (optarg, _("startup delay"), s_err);
462 break;
463 case 5:
464 #if defined(F_GETFD) && defined(F_SETFD) && defined(O_SYNC)
465 o_sync=1;
466 #else
467 error(0,0, _("O_SYNC not supported by the kernel"));
468 #endif
469 break;
470 case 6:
471 tcp_flag=2;
472 break;
473 case 7:
474 tcp_flag=3;
475 tcp_server_address=(char *)strdup(optarg);
476 if (!tcp_server_address)
477 error(1,0,_("out of memory"));
478 break;
479 default:
480 usage(2,NULL);
481 }
482
483 }
484
485 if (getuid()!=geteuid()) {
486 error(1,0,
487 _("this program was never intended to be used setuid\n"));
488 }
489
490 #ifdef SEGMENTS
491 secbuf=malloc(1+(SEGMENTS+1)*MAX_BLOCK);
492 #else
493 secbuf=malloc(MAX_BLOCK+1);
494 #endif
495 if (!secbuf) error(1,0,_("out of memory"));
496
497 /* initialize zsendline tab */
498 zsendline_init();
499 if (startup_delay)
500 sleep(startup_delay);
501
502 npats = argc - optind;
503 patts=&argv[optind];
504
505 if (npats > 1)
506 usage(2,_("garbage on commandline"));
507 if (protocol!=ZM_XMODEM && npats)
508 usage(2, _("garbage on commandline"));
509 if (Restricted && allow_remote_commands) {
510 allow_remote_commands=FALSE;
511 }
512 if (Fromcu && !Quiet) {
513 if (Verbose == 0)
514 Verbose = 2;
515 }
516
517 vfile("%s %s\n", program_name, VERSION);
518
519 if (tcp_flag==2) {
520 char buf[256];
521 #ifdef MAXHOSTNAMELEN
522 char hn[MAXHOSTNAMELEN];
523 #else
524 char hn[256];
525 #endif
526 char *p,*q;
527 int d;
528
529 /* tell receiver to receive via tcp */
530 d=tcp_server(buf);
531 p=strchr(buf+1,'<');
532 p++;
533 q=strchr(p,'>');
534 *q=0;
535 if (gethostname(hn,sizeof(hn))==-1) {
536 error(1,0, _("hostname too long\n"));
537 }
538 fprintf(stdout,"connect with lrz --tcp-client \"%s:%s\"\n",hn,p);
539 fflush(stdout);
540 /* ok, now that this file is sent we can switch to tcp */
541
542 tcp_socket=tcp_accept(d);
543 dup2(tcp_socket,0);
544 dup2(tcp_socket,1);
545 }
546 if (tcp_flag==3) {
547 char buf[256];
548 char *p;
549 p=strchr(tcp_server_address,':');
550 if (!p)
551 error(1,0, _("illegal server address\n"));
552 *p++=0;
553 sprintf(buf,"[%s] <%s>\n",tcp_server_address,p);
554
555 fprintf(stdout,"connecting to %s\n",buf);
556 fflush(stdout);
557
558 /* we need to switch to tcp mode */
559 tcp_socket=tcp_connect(buf);
560 dup2(tcp_socket,0);
561 dup2(tcp_socket,1);
562 }
563
564 io_mode(0,1);
565 readline_setup(0, HOWMANY, MAX_BLOCK*2);
566 if (signal(SIGINT, bibi) == SIG_IGN)
567 signal(SIGINT, SIG_IGN);
568 else
569 signal(SIGINT, bibi);
570 signal(SIGTERM, bibi);
571 signal(SIGPIPE, bibi);
572 signal(SIGHUP, bibi);
573 if (wcreceive(npats, patts)==ERROR) {
574 exitcode=0200;
575 canit(STDOUT_FILENO);
576 }
577 io_mode(0,0);
578 if (exitcode && !zmodem_requested) /* bellow again with all thy might. */
579 canit(STDOUT_FILENO);
580 if (Verbose)
581 {
582 fputs("\r\n",stderr);
583 if (exitcode)
584 fputs(_("Transfer incomplete\n"),stderr);
585 else
586 fputs(_("Transfer complete\n"),stderr);
587 }
588 exit(exitcode);
589 }
590
591 static void
592 usage1(int exitcode)
593 {
594 usage(exitcode,NULL);
595 }
596
597 static void
598 usage(int exitcode, const char *what)
599 {
600 FILE *f=stdout;
601
602 if (exitcode)
603 {
604 if (what)
605 fprintf(stderr, "%s: %s\n",program_name,what);
606 fprintf (stderr, _("Try `%s --help' for more information.\n"),
607 program_name);
608 exit(exitcode);
609 }
610
611 fprintf(f, _("%s version %s\n"), program_name,
612 VERSION);
613
614 fprintf(f,_("Usage: %s [options] [filename.if.xmodem]\n"), program_name);
615 fputs(_("Receive files with ZMODEM/YMODEM/XMODEM protocol\n"),f);
616 fputs(_(
617 " (X) = option applies to XMODEM only\n"
618 " (Y) = option applies to YMODEM only\n"
619 " (Z) = option applies to ZMODEM only\n"
620 ),f);
621 fputs(_(
622 " -+, --append append to existing files\n"
623 " -a, --ascii ASCII transfer (change CR/LF to LF)\n"
624 " -b, --binary binary transfer\n"
625 " -B, --bufsize N buffer N bytes (N==auto: buffer whole file)\n"
626 " -c, --with-crc Use 16 bit CRC (X)\n"
627 " -C, --allow-remote-commands allow execution of remote commands (Z)\n"
628 " -D, --null write all received data to /dev/null\n"
629 " --delay-startup N sleep N seconds before doing anything\n"
630 " -e, --escape Escape control characters (Z)\n"
631 " -E, --rename rename any files already existing\n"
632 " --errors N generate CRC error every N bytes (debugging)\n"
633 " -h, --help Help, print this usage message\n"
634 " -m, --min-bps N stop transmission if BPS below N\n"
635 " -M, --min-bps-time N for at least N seconds (default: 120)\n"
636 " -O, --disable-timeouts disable timeout code, wait forever for data\n"
637 " --o-sync open output file(s) in synchronous write mode\n"
638 " -p, --protect protect existing files\n"
639 " -q, --quiet quiet, no progress reports\n"
640 " -r, --resume try to resume interrupted file transfer (Z)\n"
641 " -R, --restricted restricted, more secure mode\n"
642 " -s, --stop-at {HH:MM|+N} stop transmission at HH:MM or in N seconds\n"
643 " -S, --timesync request remote time (twice: set local time)\n"
644 " --syslog[=off] turn syslog on or off, if possible\n"
645 " -t, --timeout N set timeout to N tenths of a second\n"
646 " --tcp-server open socket, wait for connection (Z)\n"
647 " --tcp-client ADDR:PORT open socket, connect to ... (Z)\n"
648 " -u, --keep-uppercase keep upper case filenames\n"
649 " -U, --unrestrict disable restricted mode (if allowed to)\n"
650 " -v, --verbose be verbose, provide debugging information\n"
651 " -w, --windowsize N Window is N bytes (Z)\n"
652 " -X --xmodem use XMODEM protocol\n"
653 " -y, --overwrite Yes, clobber existing file if any\n"
654 " --ymodem use YMODEM protocol\n"
655 " -Z, --zmodem use ZMODEM protocol\n"
656 "\n"
657 "short options use the same arguments as the long ones\n"
658 ),f);
659 exit(exitcode);
660 }
661
662 /*
663 * Let's receive something already.
664 */
665
666 static int
667 wcreceive(int argc, char **argp)
668 {
669 int c;
670 struct zm_fileinfo zi;
671 #ifdef ENABLE_SYSLOG
672 const char *shortname=NULL;;
673 #endif
674 zi.fname=NULL;
675 zi.modtime=0;
676 zi.mode=0;
677 zi.bytes_total=0;
678 zi.bytes_sent=0;
679 zi.bytes_received=0;
680 zi.bytes_skipped=0;
681 zi.eof_seen=0;
682
683 if (protocol!=ZM_XMODEM || argc==0) {
684 Crcflg=1;
685 if ( !Quiet)
686 vstringf(_("%s waiting to receive."), program_name);
687 if ((c=tryz())!=0) {
688 if (c == ZCOMPL)
689 return OK;
690 if (c == ERROR)
691 goto fubar;
692 c = rzfiles(&zi);
693
694 #ifdef ENABLE_SYSLOG
695 shortname=NULL;
696 #endif
697 if (c)
698 goto fubar;
699 } else { /* Not Xmodem, not Zmodem, so Ymodem */
700 for (;;) {
701 if (Verbose > 1 || dszlogpath
702 #ifdef ENABLE_SYSLOG
703 || enable_syslog
704 #endif
705 )
706 timing(1,NULL);
707 #ifdef ENABLE_SYSLOG
708 shortname=NULL;
709 #endif
710 if (wcrxpn(&zi,secbuf)== ERROR)
711 goto fubar;
712 if (secbuf[0]==0)
713 return OK;
714 if (procheader(secbuf, &zi) == ERROR)
715 goto fubar;
716 #ifdef ENABLE_SYSLOG
717 shortname=strrchr(zi.fname,'/');
718 if (shortname)
719 shortname++;
720 else
721 shortname=zi.fname;
722 #endif
723 if (wcrx(&zi)==ERROR)
724 goto fubar;
725
726 if (Verbose > 1 || dszlogpath
727 #ifdef ENABLE_SYSLOG
728 || enable_syslog
729 #endif
730 ) {
731 double d;
732 long bps;
733 d=timing(0,NULL);
734 if (d==0)
735 d=0.5; /* can happen if timing uses time() */
736 bps=(zi.bytes_received-zi.bytes_skipped)/d;
737
738 if (Verbose>1) {
739 vstringf(
740 _("\rBytes received: %7ld/%7ld BPS:%-6ld \r\n"),
741 (long) zi.bytes_received, (long) zi.bytes_total, bps);
742 }
743 #ifdef ENABLE_SYSLOG
744 if (enable_syslog)
745 lsyslog(LOG_INFO,"%s/%s: %ld Bytes, %ld BPS",
746 shortname,protname(),zi.bytes_received, bps);
747 #endif
748 if (dszlogpath) dszlog(dszlogpath, 'R',
749 zi.bytes_received, zi.fname,
750 bps, errors, 0, 0);
751 }
752 }
753 }
754 } else { /* Xmodem */
755 char dummy[128];
756 dummy[0]='\0'; /* pre-ANSI HPUX cc demands this */
757 dummy[1]='\0'; /* procheader uses name + 1 + strlen(name) */
758 zi.bytes_total = DEFBYTL;
759
760 if (Verbose > 1 || dszlogpath
761 #ifdef ENABLE_SYSLOG
762 || enable_syslog
763 #endif
764 )
765 timing(1,NULL);
766 procheader(dummy, &zi);
767
768 if (Pathname)
769 free(Pathname);
770 errno=0;
771 Pathname=malloc(PATH_MAX+1);
772 if (!Pathname)
773 error(1,0,_("out of memory"));
774
775 strcpy(Pathname, *argp);
776 checkpath(Pathname);
777 #ifdef ENABLE_SYSLOG
778 shortname=strrchr(*argp,'/');
779 if (shortname)
780 shortname++;
781 else
782 shortname=*argp;
783 #endif
784 vchar('\n');
785 vstringf(_("%s: ready to receive %s"), program_name, Pathname);
786 vstring("\r\n");
787
788 if ((fout=fopen(Pathname, "w")) == NULL) {
789 #ifdef ENABLE_SYSLOG
790 if (enable_syslog)
791 lsyslog(LOG_ERR,"%s/%s: cannot open: %m",
792 shortname,protname());
793 #endif
794 return ERROR;
795 }
796 if (wcrx(&zi)==ERROR) {
797 goto fubar;
798 }
799 if (Verbose > 1 || dszlogpath
800 #ifdef ENABLE_SYSLOG
801 || enable_syslog
802 #endif
803 ) {
804 double d;
805 long bps;
806 d=timing(0,NULL);
807 if (d==0)
808 d=0.5; /* can happen if timing uses time() */
809 bps=(zi.bytes_received-zi.bytes_skipped)/d;
810 if (Verbose) {
811 vstringf(
812 _("\rBytes received: %7ld BPS:%-6ld \r\n"),
813 (long) zi.bytes_received, bps);
814 }
815 #ifdef ENABLE_SYSLOG
816 if (enable_syslog)
817 lsyslog(LOG_INFO,"%s/%s: %ld Bytes, %ld BPS",
818 shortname,protname(),zi.bytes_received, bps);
819 #endif
820 if (dszlogpath) dszlog(dszlogpath, 'R',
821 zi.bytes_received, zi.fname,
822 bps, errors, 0, 0);
823
824 }
825 }
826 return OK;
827 fubar:
828 #ifdef ENABLE_SYSLOG
829 if (enable_syslog)
830 lsyslog(LOG_ERR,"%s/%s: got error",
831 shortname ? shortname : "no.name", protname());
832 #endif
833 if (dszlogpath) {
834 double d = timing(0,NULL);
835 dszlog(dszlogpath, 'E', zi.bytes_received, zi.fname,
836 (zi.bytes_received-zi.bytes_skipped)/((d) ? d : 0.5),
837 errors, 0, 0);
838 }
839
840 canit(STDOUT_FILENO);
841 if (Topipe && fout) {
842 pclose(fout); return ERROR;
843 }
844 if (fout)
845 fclose(fout);
846
847 if (Restricted && Pathname) {
848 unlink(Pathname);
849 vstringf(_("\r\n%s: %s removed.\r\n"), program_name, Pathname);
850 }
851 return ERROR;
852 }
853
854
855 /*
856 * Fetch a pathname from the other end as a C ctyle ASCIZ string.
857 * Length is indeterminate as long as less than Blklen
858 * A null string represents no more files (YMODEM)
859 */
860 static int
861 wcrxpn(struct zm_fileinfo *zi, char *rpn)
862 {
863 register int c;
864 size_t Blklen=0; /* record length of received packets */
865
866 #ifdef NFGVMIN
867 READLINE_PF(1);
868 #else
869 purgeline(0);
870 #endif
871
872 et_tu:
873 Firstsec=TRUE;
874 zi->eof_seen=FALSE;
875 sendline(Crcflg?WANTCRC:NAK);
876 flushmo();
877 purgeline(0); /* Do read next time ... */
878 while ((c = wcgetsec(&Blklen, rpn, 100)) != 0) {
879 if (c == WCEOT) {
880 zperr( _("Pathname fetch returned EOT"));
881 sendline(ACK);
882 flushmo();
883 purgeline(0); /* Do read next time ... */
884 READLINE_PF(1);
885 goto et_tu;
886 }
887 return ERROR;
888 }
889 sendline(ACK);
890 flushmo();
891 return OK;
892 }
893
894 /*
895 * Adapted from CMODEM13.C, written by
896 * Jack M. Wierda and Roderick W. Hart
897 */
898 static int
899 wcrx(struct zm_fileinfo *zi)
900 {
901 register int sectnum, sectcurr;
902 register char sendchar;
903 size_t Blklen;
904
905 Firstsec=TRUE;sectnum=0;
906 zi->eof_seen=FALSE;
907 sendchar=Crcflg?WANTCRC:NAK;
908
909 for (;;) {
910 sendline(sendchar); /* send it now, we're ready! */
911 flushmo();
912 purgeline(0); /* Do read next time ... */
913 sectcurr=wcgetsec(&Blklen, secbuf,
914 (unsigned int) ((sectnum&0177) ? 50 : 130));
915 if (sectcurr==((sectnum+1) &0377)) {
916 sectnum++;
917 report(sectnum);
918 /* if using xmodem we don't know how long a file is */
919 if (zi->bytes_total && R_BYTESLEFT(zi) < Blklen)
920 Blklen=R_BYTESLEFT(zi);
921 zi->bytes_received+=Blklen;
922 if (putsec(zi, secbuf, Blklen)==ERROR)
923 return ERROR;
924 sendchar=ACK;
925 }
926 else if (sectcurr==(sectnum&0377)) {
927 zperr( _("Received dup Sector"));
928 sendchar=ACK;
929 }
930 else if (sectcurr==WCEOT) {
931 if (closeit(zi))
932 return ERROR;
933 sendline(ACK);
934 flushmo();
935 purgeline(0); /* Do read next time ... */
936 return OK;
937 }
938 else if (sectcurr==ERROR)
939 return ERROR;
940 else {
941 zperr( _("Sync Error"));
942 return ERROR;
943 }
944 }
945 }
946
947 /*
948 * Wcgetsec fetches a Ward Christensen type sector.
949 * Returns sector number encountered or ERROR if valid sector not received,
950 * or CAN CAN received
951 * or WCEOT if eot sector
952 * time is timeout for first char, set to 4 seconds thereafter
953 ***************** NO ACK IS SENT IF SECTOR IS RECEIVED OK **************
954 * (Caller must do that when he is good and ready to get next sector)
955 */
956 static int
957 wcgetsec(size_t *Blklen, char *rxbuf, unsigned int maxtime)
958 {
959 register int checksum, wcj, firstch;
960 register unsigned short oldcrc;
961 register char *p;
962 int sectcurr;
963
964 for (Lastrx=errors=0; errors<RETRYMAX; errors++) {
965
966 if ((firstch=READLINE_PF(maxtime))==STX) {
967 *Blklen=1024; goto get2;
968 }
969 if (firstch==SOH) {
970 *Blklen=128;
971 get2:
972 sectcurr=READLINE_PF(1);
973 if ((sectcurr+(oldcrc=READLINE_PF(1)))==0377) {
974 oldcrc=checksum=0;
975 for (p=rxbuf,wcj=*Blklen; --wcj>=0; ) {
976 if ((firstch=READLINE_PF(1)) < 0)
977 goto bilge;
978 oldcrc=updcrc(firstch, oldcrc);
979 checksum += (*p++ = firstch);
980 }
981 if ((firstch=READLINE_PF(1)) < 0)
982 goto bilge;
983 if (Crcflg) {
984 oldcrc=updcrc(firstch, oldcrc);
985 if ((firstch=READLINE_PF(1)) < 0)
986 goto bilge;
987 oldcrc=updcrc(firstch, oldcrc);
988 if (oldcrc & 0xFFFF)
989 zperr( _("CRC"));
990 else {
991 Firstsec=FALSE;
992 return sectcurr;
993 }
994 }
995 else if (((checksum-firstch)&0377)==0) {
996 Firstsec=FALSE;
997 return sectcurr;
998 }
999 else
1000 zperr( _("Checksum"));
1001 }
1002 else
1003 zperr(_("Sector number garbled"));
1004 }
1005 /* make sure eot really is eot and not just mixmash */
1006 #ifdef NFGVMIN
1007 else if (firstch==EOT && READLINE_PF(1)==TIMEOUT)
1008 return WCEOT;
1009 #else
1010 else if (firstch==EOT && READLINE_PF>0)
1011 return WCEOT;
1012 #endif
1013 else if (firstch==CAN) {
1014 if (Lastrx==CAN) {
1015 zperr( _("Sender Cancelled"));
1016 return ERROR;
1017 } else {
1018 Lastrx=CAN;
1019 continue;
1020 }
1021 }
1022 else if (firstch==TIMEOUT) {
1023 if (Firstsec)
1024 goto humbug;
1025 bilge:
1026 zperr( _("TIMEOUT"));
1027 }
1028 else
1029 zperr( _("Got 0%o sector header"), firstch);
1030
1031 humbug:
1032 Lastrx=0;
1033 {
1034 int cnt=1000;
1035 while(cnt-- && READLINE_PF(1)!=TIMEOUT)
1036 ;
1037 }
1038 if (Firstsec) {
1039 sendline(Crcflg?WANTCRC:NAK);
1040 flushmo();
1041 purgeline(0); /* Do read next time ... */
1042 } else {
1043 maxtime=40;
1044 sendline(NAK);
1045 flushmo();
1046 purgeline(0); /* Do read next time ... */
1047 }
1048 }
1049 /* try to stop the bubble machine. */
1050 canit(STDOUT_FILENO);
1051 return ERROR;
1052 }
1053
1054 #define ZCRC_DIFFERS (ERROR+1)
1055 #define ZCRC_EQUAL (ERROR+2)
1056 /*
1057 * do ZCRC-Check for open file f.
1058 * check at most check_bytes bytes (crash recovery). if 0 -> whole file.
1059 * remote file size is remote_bytes.
1060 */
1061 static int
1062 do_crc_check(FILE *f, size_t remote_bytes, size_t check_bytes)
1063 {
1064 struct stat st;
1065 unsigned long crc;
1066 unsigned long rcrc;
1067 size_t n;
1068 int c;
1069 int t1=0,t2=0;
1070 if (-1==fstat(fileno(f),&st)) {
1071 DO_SYSLOG((LOG_ERR,"cannot fstat open file: %s",strerror(errno)));
1072 return ERROR;
1073 }
1074 if (check_bytes==0 && ((size_t) st.st_size)!=remote_bytes)
1075 return ZCRC_DIFFERS; /* shortcut */
1076
1077 crc=0xFFFFFFFFL;
1078 n=check_bytes;
1079 if (n==0)
1080 n=st.st_size;
1081 while (n-- && ((c = getc(f)) != EOF))
1082 crc = UPDC32(c, crc);
1083 crc = ~crc;
1084 clearerr(f); /* Clear EOF */
1085 fseek(f, 0L, 0);
1086
1087 while (t1<3) {
1088 stohdr(check_bytes);
1089 zshhdr(ZCRC, Txhdr);
1090 while(t2<3) {
1091 size_t tmp;
1092 c = zgethdr(Rxhdr, 0, &tmp);
1093 rcrc=(unsigned long) tmp;
1094 switch (c) {
1095 default: /* ignore */
1096 break;
1097 case ZFIN:
1098 return ERROR;
1099 case ZRINIT:
1100 return ERROR;
1101 case ZCAN:
1102 if (Verbose)
1103 vstringf(_("got ZCAN"));
1104 return ERROR;
1105 break;
1106 case ZCRC:
1107 if (crc!=rcrc)
1108 return ZCRC_DIFFERS;
1109 return ZCRC_EQUAL;
1110 break;
1111 }
1112 }
1113 }
1114 return ERROR;
1115 }
1116
1117 /*
1118 * Process incoming file information header
1119 */
1120 static int
1121 procheader(char *name, struct zm_fileinfo *zi)
1122 {
1123 const char *openmode;
1124 char *p;
1125 static char *name_static=NULL;
1126 char *nameend;
1127
1128 if (name_static)
1129 free(name_static);
1130 if (junk_path) {
1131 p=strrchr(name,'/');
1132 if (p) {
1133 p++;
1134 if (!*p) {
1135 /* alert - file name ended in with a / */
1136 if (Verbose)
1137 vstringf(_("file name ends with a /, skipped: %s\n"),name);
1138 DO_SYSLOG((LOG_ERR,"file name ends with a /, skipped: %s", name));
1139 return ERROR;
1140 }
1141 name=p;
1142 }
1143 }
1144 name_static=malloc(strlen(name)+1);
1145 if (!name_static)
1146 error(1,0,_("out of memory"));
1147 strcpy(name_static,name);
1148 zi->fname=name_static;
1149
1150 if (Verbose>2) {
1151 vstringf(_("zmanag=%d, Lzmanag=%d\n"),zmanag,Lzmanag);
1152 vstringf(_("zconv=%d\n"),zconv);
1153 }
1154
1155 /* set default parameters and overrides */
1156 openmode = "w";
1157 Thisbinary = (!Rxascii) || Rxbinary;
1158 if (Lzmanag)
1159 zmanag = Lzmanag;
1160
1161 /*
1162 * Process ZMODEM remote file management requests
1163 */
1164 if (!Rxbinary && zconv == ZCNL) /* Remote ASCII override */
1165 Thisbinary = 0;
1166 if (zconv == ZCBIN) /* Remote Binary override */
1167 Thisbinary = TRUE;
1168 if (Thisbinary && zconv == ZCBIN && try_resume)
1169 zconv=ZCRESUM;
1170 if (zmanag == ZF1_ZMAPND && zconv!=ZCRESUM)
1171 openmode = "a";
1172 if (skip_if_not_found)
1173 openmode="r+";
1174
1175 #ifdef ENABLE_TIMESYNC
1176 in_timesync=0;
1177 if (timesync_flag && 0==strcmp(name,"$time$.t"))
1178 in_timesync=1;
1179 #endif
1180
1181 zi->bytes_total = DEFBYTL;
1182 zi->mode = 0;
1183 zi->eof_seen = 0;
1184 zi->modtime = 0;
1185
1186 nameend = name + 1 + strlen(name);
1187 if (*nameend) { /* file coming from Unix or DOS system */
1188 long modtime;
1189 long bytes_total;
1190 int mode;
1191 sscanf(nameend, "%ld%lo%o", &bytes_total, &modtime, &mode);
1192 zi->modtime=modtime;
1193 zi->bytes_total=bytes_total;
1194 zi->mode=mode;
1195 if (zi->mode & UNIXFILE)
1196 ++Thisbinary;
1197 }
1198
1199 /* Check for existing file */
1200 if (zconv != ZCRESUM && !Rxclob && (zmanag&ZF1_ZMMASK) != ZF1_ZMCLOB
1201 && (zmanag&ZF1_ZMMASK) != ZF1_ZMAPND
1202 #ifdef ENABLE_TIMESYNC
1203 && !in_timesync
1204 #endif
1205 && (fout=fopen(name, "r"))) {
1206 struct stat sta;
1207 char *tmpname;
1208 char *ptr;
1209 int i;
1210 if (zmanag == ZF1_ZMNEW || zmanag==ZF1_ZMNEWL) {
1211 if (-1==fstat(fileno(fout),&sta)) {
1212 #ifdef ENABLE_SYSLOG
1213 int e=errno;
1214 #endif
1215 if (Verbose)
1216 vstringf(_("file exists, skipped: %s\n"),name);
1217 DO_SYSLOG((LOG_ERR,"cannot fstat open file %s: %s",
1218 name,strerror(e)));
1219 return ERROR;
1220 }
1221 if (zmanag == ZF1_ZMNEW) {
1222 if (sta.st_mtime > zi->modtime) {
1223 DO_SYSLOG((LOG_INFO,"skipping %s: newer file exists", name));
1224 return ERROR; /* skips file */
1225 }
1226 } else {
1227 /* newer-or-longer */
1228 if (((size_t) sta.st_size) >= zi->bytes_total
1229 && sta.st_mtime > zi->modtime) {
1230 DO_SYSLOG((LOG_INFO,"skipping %s: longer+newer file exists", name));
1231 return ERROR; /* skips file */
1232 }
1233 }
1234 fclose(fout);
1235 } else if (zmanag==ZF1_ZMCRC) {
1236 int r=do_crc_check(fout,zi->bytes_total,0);
1237 if (r==ERROR) {
1238 fclose(fout);
1239 return ERROR;
1240 }
1241 if (r!=ZCRC_DIFFERS) {
1242 return ERROR; /* skips */
1243 }
1244 fclose(fout);
1245 } else {
1246 size_t namelen;
1247 fclose(fout);
1248 if ((zmanag & ZF1_ZMMASK)!=ZF1_ZMCHNG) {
1249 if (Verbose)
1250 vstringf(_("file exists, skipped: %s\n"),name);
1251 return ERROR;
1252 }
1253 /* try to rename */
1254 namelen=strlen(name);
1255 tmpname=alloca(namelen+5);
1256 memcpy(tmpname,name,namelen);
1257 ptr=tmpname+namelen;
1258 *ptr++='.';
1259 i=0;
1260 do {
1261 sprintf(ptr,"%d",i++);
1262 } while (i<1000 && stat(tmpname,&sta)==0);
1263 if (i==1000)
1264 return ERROR;
1265 free(name_static);
1266 name_static=malloc(strlen(tmpname)+1);
1267 if (!name_static)
1268 error(1,0,_("out of memory"));
1269 strcpy(name_static,tmpname);
1270 zi->fname=name_static;
1271 }
1272 }
1273
1274 if (!*nameend) { /* File coming from CP/M system */
1275 for (p=name_static; *p; ++p) /* change / to _ */
1276 if ( *p == '/')
1277 *p = '_';
1278
1279 if ( *--p == '.') /* zap trailing period */
1280 *p = 0;
1281 }
1282
1283 #ifdef ENABLE_TIMESYNC
1284 if (in_timesync)
1285 {
1286 long t=time(0);
1287 long d=t-zi->modtime;
1288 if (d<0)
1289 d=0;
1290 if ((Verbose && d>60) || Verbose > 1)
1291 vstringf(_("TIMESYNC: here %ld, remote %ld, diff %ld seconds\n"),
1292 (long) t, (long) zi->modtime, d);
1293 #ifdef HAVE_SETTIMEOFDAY
1294 if (timesync_flag > 1 && d > 10)
1295 {
1296 struct timeval tv;
1297 tv.tv_sec=zi->modtime;
1298 tv.tv_usec=0;
1299 if (settimeofday(&tv,NULL))
1300 vstringf(_("TIMESYNC: cannot set time: %s\n"),
1301 strerror(errno));
1302 }
1303 #endif
1304 return ERROR; /* skips file */
1305 }
1306 #endif /* ENABLE_TIMESYNC */
1307
1308 if (!zmodem_requested && MakeLCPathname && !IsAnyLower(name_static)
1309 && !(zi->mode&UNIXFILE))
1310 uncaps(name_static);
1311 if (Topipe > 0) {
1312 if (Pathname)
1313 free(Pathname);
1314 Pathname=malloc((PATH_MAX)*2);
1315 if (!Pathname)
1316 error(1,0,_("out of memory"));
1317 sprintf(Pathname, "%s %s", program_name+2, name_static);
1318 if (Verbose) {
1319 vstringf("%s: %s %s\n",
1320 _("Topipe"),
1321 Pathname, Thisbinary?"BIN":"ASCII");
1322 }
1323 if ((fout=popen(Pathname, "w")) == NULL)
1324 return ERROR;
1325 } else {
1326 if (protocol==ZM_XMODEM)
1327 /* we don't have the filename yet */
1328 return OK; /* dummy */
1329 if (Pathname)
1330 free(Pathname);
1331 Pathname=malloc((PATH_MAX)*2);
1332 if (!Pathname)
1333 error(1,0,_("out of memory"));
1334 strcpy(Pathname, name_static);
1335 if (Verbose) {
1336 /* overwrite the "waiting to receive" line */
1337 vstring("\r \r");
1338 vstringf(_("Receiving: %s\n"), name_static);
1339 }
1340 checkpath(name_static);
1341 if (Nflag)
1342 {
1343 /* cast because we might not have a prototyp for strdup :-/ */
1344 free(name_static);
1345 name_static=(char *) strdup("/dev/null");
1346 if (!name_static)
1347 {
1348 fprintf(stderr,"%s: %s\n", program_name, _("out of memory"));
1349 exit(1);
1350 }
1351 }
1352 #ifdef OMEN
1353 /* looks like a security hole -- uwe */
1354 if (name_static[0] == '!' || name_static[0] == '|') {
1355 if ( !(fout = popen(name_static+1, "w"))) {
1356 return ERROR;
1357 }
1358 Topipe = -1; return(OK);
1359 }
1360 #endif
1361 if (Thisbinary && zconv==ZCRESUM) {
1362 struct stat st;
1363 fout = fopen(name_static, "r+");
1364 if (fout && 0==fstat(fileno(fout),&st))
1365 {
1366 int can_resume=TRUE;
1367 if (zmanag==ZF1_ZMCRC) {
1368 int r=do_crc_check(fout,zi->bytes_total,st.st_size);
1369 if (r==ERROR) {
1370 fclose(fout);
1371 return ZFERR;
1372 }
1373 if (r==ZCRC_DIFFERS) {
1374 can_resume=FALSE;
1375 }
1376 }
1377 if ((unsigned long)st.st_size > zi->bytes_total) {
1378 can_resume=FALSE;
1379 }
1380 /* retransfer whole blocks */
1381 zi->bytes_skipped = st.st_size & ~(1023);
1382 if (can_resume) {
1383 if (fseek(fout, (long) zi->bytes_skipped, SEEK_SET)) {
1384 fclose(fout);
1385 return ZFERR;
1386 }
1387 }
1388 else
1389 zi->bytes_skipped=0; /* resume impossible, file has changed */
1390 goto buffer_it;
1391 }
1392 zi->bytes_skipped=0;
1393 if (fout)
1394 fclose(fout);
1395 }
1396 fout = fopen(name_static, openmode);
1397 #ifdef ENABLE_MKDIR
1398 if ( !fout && Restricted < 2) {
1399 if (make_dirs(name_static))
1400 fout = fopen(name_static, openmode);
1401 }
1402 #endif
1403 if ( !fout)
1404 {
1405 #ifdef ENABLE_SYSLOG
1406 int e=errno;
1407 #endif
1408 zpfatal(_("cannot open %s"), name_static);
1409 DO_SYSLOG((LOG_ERR,"%s: cannot open: %s",
1410 protname(),strerror(e)));
1411 return ERROR;
1412 }
1413 }
1414 buffer_it:
1415 if (Topipe == 0) {
1416 static char *s=NULL;
1417 static size_t last_length=0;
1418 #if defined(F_GETFD) && defined(F_SETFD) && defined(O_SYNC)
1419 if (o_sync) {
1420 int oldflags;
1421 oldflags = fcntl (fileno(fout), F_GETFD, 0);
1422 if (oldflags>=0 && !(oldflags & O_SYNC)) {
1423 oldflags|=O_SYNC;
1424 fcntl (fileno(fout), F_SETFD, oldflags); /* errors don't matter */
1425 }
1426 }
1427 #endif
1428
1429 if (buffersize==-1 && s) {
1430 if (zi->bytes_total>last_length) {
1431 free(s);
1432 s=NULL;
1433 last_length=0;
1434 }
1435 }
1436 if (!s && buffersize) {
1437 last_length=32768;
1438 if (buffersize==-1) {
1439 if (zi->bytes_total>0)
1440 last_length=zi->bytes_total;
1441 } else
1442 last_length=buffersize;
1443 /* buffer `4096' bytes pages */
1444 last_length=(last_length+4095)&0xfffff000;
1445 s=malloc(last_length);
1446 if (!s) {
1447 zpfatal(_("out of memory"));
1448 exit(1);
1449 }
1450 }
1451 if (s) {
1452 #ifdef SETVBUF_REVERSED
1453 setvbuf(fout,_IOFBF,s,last_length);
1454 #else
1455 setvbuf(fout,s,_IOFBF,last_length);
1456 #endif
1457 }
1458 }
1459 zi->bytes_received=zi->bytes_skipped;
1460
1461 return OK;
1462 }
1463
1464 #ifdef ENABLE_MKDIR
1465 /*
1466 * Directory-creating routines from Public Domain TAR by John Gilmore
1467 */
1468
1469 /*
1470 * After a file/link/symlink/dir creation has failed, see if
1471 * it's because some required directory was not present, and if
1472 * so, create all required dirs.
1473 */
1474 static int
1475 make_dirs(char *pathname)
1476 {
1477 register char *p; /* Points into path */
1478 int madeone = 0; /* Did we do anything yet? */
1479 int save_errno = errno; /* Remember caller's errno */
1480
1481 if (errno != ENOENT)
1482 return 0; /* Not our problem */
1483
1484 for (p = strchr(pathname, '/'); p != NULL; p = strchr(p+1, '/')) {
1485 /* Avoid mkdir of empty string, if leading or double '/' */
1486 if (p == pathname || p[-1] == '/')
1487 continue;
1488 /* Avoid mkdir where last part of path is '.' */
1489 if (p[-1] == '.' && (p == pathname+1 || p[-2] == '/'))
1490 continue;
1491 *p = 0; /* Truncate the path there */
1492 if ( !mkdir(pathname, 0777)) { /* Try to create it as a dir */
1493 vfile("Made directory %s\n", pathname);
1494 madeone++; /* Remember if we made one */
1495 *p = '/';
1496 continue;
1497 }
1498 *p = '/';
1499 if (errno == EEXIST) /* Directory already exists */
1500 continue;
1501 /*
1502 * Some other error in the mkdir. We return to the caller.
1503 */
1504 break;
1505 }
1506 errno = save_errno; /* Restore caller's errno */
1507 return madeone; /* Tell them to retry if we made one */
1508 }
1509
1510 #endif /* ENABLE_MKDIR */
1511
1512 /*
1513 * Putsec writes the n characters of buf to receive file fout.
1514 * If not in binary mode, carriage returns, and all characters
1515 * starting with CPMEOF are discarded.
1516 */
1517 static int
1518 putsec(struct zm_fileinfo *zi, char *buf, size_t n)
1519 {
1520 register char *p;
1521
1522 if (n == 0)
1523 return OK;
1524 if (Thisbinary) {
1525 if (fwrite(buf,n,1,fout)!=1)
1526 return ERROR;
1527 }
1528 else {
1529 if (zi->eof_seen)
1530 return OK;
1531 for (p=buf; n>0; ++p,n-- ) {
1532 if ( *p == '\r')
1533 continue;
1534 if (*p == CPMEOF) {
1535 zi->eof_seen=TRUE;
1536 return OK;
1537 }
1538 putc(*p ,fout);
1539 }
1540 }
1541 return OK;
1542 }
1543
1544 /* make string s lower case */
1545 static void
1546 uncaps(char *s)
1547 {
1548 for ( ; *s; ++s)
1549 if (isupper((unsigned char)(*s)))
1550 *s = tolower(*s);
1551 }
1552 /*
1553 * IsAnyLower returns TRUE if string s has lower case letters.
1554 */
1555 static int
1556 IsAnyLower(const char *s)
1557 {
1558 for ( ; *s; ++s)
1559 if (islower((unsigned char)(*s)))
1560 return TRUE;
1561 return FALSE;
1562 }
1563
1564 static void
1565 report(int sct)
1566 {
1567 if (Verbose>1)
1568 {
1569 vstringf(_("Blocks received: %d"),sct);
1570 vchar('\r');
1571 }
1572 }
1573
1574 /*
1575 * If called as [-][dir/../]vrzCOMMAND set Verbose to 1
1576 * If called as [-][dir/../]rzCOMMAND set the pipe flag
1577 * If called as rb use YMODEM protocol
1578 */
1579 static void
1580 chkinvok(const char *s)
1581 {
1582 const char *p;
1583
1584 p = s;
1585 while (*p == '-')
1586 s = ++p;
1587 while (*p)
1588 if (*p++ == '/')
1589 s = p;
1590 if (*s == 'v') {
1591 Verbose=1; ++s;
1592 }
1593 program_name = s;
1594 if (*s == 'l')
1595 s++; /* lrz -> rz */
1596 protocol=ZM_ZMODEM;
1597 if (s[0]=='r' && s[1]=='x')
1598 protocol=ZM_XMODEM;
1599 if (s[0]=='r' && (s[1]=='b' || s[1]=='y'))
1600 protocol=ZM_YMODEM;
1601 if (s[2] && protocol!=ZM_XMODEM)
1602 Topipe = 1;
1603 }
1604
1605 /*
1606 * Totalitarian Communist pathname processing
1607 */
1608 static void
1609 checkpath(const char *name)
1610 {
1611 if (Restricted) {
1612 const char *p;
1613 p=strrchr(name,'/');
1614 if (p)
1615 p++;
1616 else
1617 p=name;
1618 /* don't overwrite any file in very restricted mode.
1619 * don't overwrite hidden files in restricted mode */
1620 if ((Restricted==2 || *name=='.') && fopen(name, "r") != NULL) {
1621 canit(STDOUT_FILENO);
1622 vstring("\r\n");
1623 vstringf(_("%s: %s exists\n"),
1624 program_name, name);
1625 bibi(-1);
1626 }
1627 /* restrict pathnames to current tree or uucppublic */
1628 if ( strstr(name, "../")
1629 #ifdef PUBDIR
1630 || (name[0]== '/' && strncmp(name, PUBDIR,
1631 strlen(PUBDIR)))
1632 #endif
1633 ) {
1634 canit(STDOUT_FILENO);
1635 vstring("\r\n");
1636 vstringf(_("%s:\tSecurity Violation"),program_name);
1637 vstring("\r\n");
1638 bibi(-1);
1639 }
1640 if (Restricted > 1) {
1641 if (name[0]=='.' || strstr(name,"/.")) {
1642 canit(STDOUT_FILENO);
1643 vstring("\r\n");
1644 vstringf(_("%s:\tSecurity Violation"),program_name);
1645 vstring("\r\n");
1646 bibi(-1);
1647 }
1648 }
1649 }
1650 }
1651
1652 /*
1653 * Initialize for Zmodem receive attempt, try to activate Zmodem sender
1654 * Handles ZSINIT frame
1655 * Return ZFILE if Zmodem filename received, -1 on error,
1656 * ZCOMPL if transaction finished, else 0
1657 */
1658 static int
1659 tryz(void)
1660 {
1661 register int c, n;
1662 register int cmdzack1flg;
1663 int zrqinits_received=0;
1664 size_t bytes_in_block=0;
1665
1666 if (protocol!=ZM_ZMODEM) /* Check for "rb" program name */
1667 return 0;
1668
1669 for (n=zmodem_requested?15:5;
1670 (--n + zrqinits_received) >=0 && zrqinits_received<10; ) {
1671 /* Set buffer length (0) and capability flags */
1672 #ifdef SEGMENTS
1673 stohdr(SEGMENTS*MAX_BLOCK);
1674 #else
1675 stohdr(0L);
1676 #endif
1677 #ifdef CANBREAK
1678 Txhdr[ZF0] = CANFC32|CANFDX|CANOVIO|CANBRK;
1679 #else
1680 Txhdr[ZF0] = CANFC32|CANFDX|CANOVIO;
1681 #endif
1682 #ifdef ENABLE_TIMESYNC
1683 if (timesync_flag)
1684 Txhdr[ZF1] |= ZF1_TIMESYNC;
1685 #endif
1686 if (Zctlesc)
1687 Txhdr[ZF0] |= TESCCTL; /* TESCCTL == ESCCTL */
1688 zshhdr(tryzhdrtype, Txhdr);
1689
1690 if (tryzhdrtype == ZSKIP) /* Don't skip too far */
1691 tryzhdrtype = ZRINIT; /* CAF 8-21-87 */
1692 again:
1693 switch (zgethdr(Rxhdr, 0, NULL)) {
1694 case ZRQINIT:
1695 /* getting one ZRQINIT is totally ok. Normally a ZFILE follows
1696 * (and might be in our buffer, so don't purge it). But if we
1697 * get more ZRQINITs than the sender has started up before us
1698 * and sent ZRQINITs while waiting.
1699 */
1700 zrqinits_received++;
1701 continue;
1702
1703 case ZEOF:
1704 continue;
1705 case TIMEOUT:
1706 continue;
1707 case ZFILE:
1708 zconv = Rxhdr[ZF0];
1709 if (!zconv)
1710 /* resume with sz -r is impossible (at least with unix sz)
1711 * if this is not set */
1712 zconv=ZCBIN;
1713 if (Rxhdr[ZF1] & ZF1_ZMSKNOLOC) {
1714 Rxhdr[ZF1] &= ~(ZF1_ZMSKNOLOC);
1715 skip_if_not_found=TRUE;
1716 }
1717 zmanag = Rxhdr[ZF1];
1718 ztrans = Rxhdr[ZF2];
1719 tryzhdrtype = ZRINIT;
1720 c = zrdata(secbuf, MAX_BLOCK,&bytes_in_block);
1721 io_mode(0,3);
1722 if (c == GOTCRCW)
1723 return ZFILE;
1724 zshhdr(ZNAK, Txhdr);
1725 goto again;
1726 case ZSINIT:
1727 /* this once was:
1728 * Zctlesc = TESCCTL & Rxhdr[ZF0];
1729 * trouble: if rz get --escape flag:
1730 * - it sends TESCCTL to sz,
1731 * get a ZSINIT _without_ TESCCTL (yeah - sender didn't know),
1732 * overwrites Zctlesc flag ...
1733 * - sender receives TESCCTL and uses "|=..."
1734 * so: sz escapes, but rz doesn't unescape ... not good.
1735 */
1736 Zctlesc |= TESCCTL & Rxhdr[ZF0];
1737 if (zrdata(Attn, ZATTNLEN,&bytes_in_block) == GOTCRCW) {
1738 stohdr(1L);
1739 zshhdr(ZACK, Txhdr);
1740 goto again;
1741 }
1742 zshhdr(ZNAK, Txhdr);
1743 goto again;
1744 case ZFREECNT:
1745 stohdr(getfree());
1746 zshhdr(ZACK, Txhdr);
1747 goto again;
1748 case ZCOMMAND:
1749 cmdzack1flg = Rxhdr[ZF0];
1750 if (zrdata(secbuf, MAX_BLOCK,&bytes_in_block) == GOTCRCW) {
1751 if (Verbose)
1752 {
1753 vstringf("%s: %s\n", program_name,
1754 _("remote command execution requested"));
1755 vstringf("%s: %s\n", program_name, secbuf);
1756 }
1757 if (!allow_remote_commands)
1758 {
1759 if (Verbose)
1760 vstringf("%s: %s\n", program_name,
1761 _("not executed"));
1762 zshhdr(ZCOMPL, Txhdr);
1763 DO_SYSLOG((LOG_INFO,"rexec denied: %s",secbuf));
1764 return ZCOMPL;
1765 }
1766 DO_SYSLOG((LOG_INFO,"rexec allowed: %s",secbuf));
1767 if (cmdzack1flg & ZCACK1)
1768 stohdr(0L);
1769 else
1770 stohdr((size_t)sys2(secbuf));
1771 purgeline(0); /* dump impatient questions */
1772 do {
1773 zshhdr(ZCOMPL, Txhdr);
1774 }
1775 while (++errors<20 && zgethdr(Rxhdr,1, NULL) != ZFIN);
1776 ackbibi();
1777 if (cmdzack1flg & ZCACK1)
1778 exec2(secbuf);
1779 return ZCOMPL;
1780 }
1781 zshhdr(ZNAK, Txhdr);
1782 goto again;
1783 case ZCOMPL:
1784 goto again;
1785 default:
1786 continue;
1787 case ZFIN:
1788 ackbibi();
1789 return ZCOMPL;
1790 case ZRINIT:
1791 if (Verbose)
1792 vstringf(_("got ZRINIT"));
1793 return ERROR;
1794 case ZCAN:
1795 if (Verbose)
1796 vstringf(_("got ZCAN"));
1797 return ERROR;
1798 }
1799 }
1800 return 0;
1801 }
1802
1803
1804 /*
1805 * Receive 1 or more files with ZMODEM protocol
1806 */
1807 static int
1808 rzfiles(struct zm_fileinfo *zi)
1809 {
1810 register int c;
1811 long bps;
1812
1813 for (;;) {
1814 timing(1,NULL);
1815 c = rzfile(zi);
1816 {
1817 double d;
1818 d=timing(0,NULL);
1819 if (d==0)
1820 d=0.5; /* can happen if timing uses time() */
1821 bps=(zi->bytes_received-zi->bytes_skipped)/d;
1822 }
1823 switch (c) {
1824 case ZEOF:
1825 if (Verbose > 1
1826 #ifdef ENABLE_SYSLOG
1827 || enable_syslog
1828 #endif
1829 ) {
1830 if (Verbose > 1) {
1831 vstringf(
1832 _("\rBytes received: %7ld/%7ld BPS:%-6ld \r\n"),
1833 (long) zi->bytes_received, (long) zi->bytes_total, bps);
1834 }
1835 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: %ld Bytes, %ld BPS",shortname,
1836 protname(), (long) zi->bytes_total,bps));
1837 if (dszlogpath) dszlog(dszlogpath, 'z', zi->bytes_received,
1838 zi->fname, bps, errors, 0, 0);
1839 }
1840 /* FALL THROUGH */
1841 case ZSKIP:
1842 if (c==ZSKIP)
1843 {
1844 if (Verbose)
1845 vstringf(_("Skipped"));
1846 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: skipped",shortname,protname()));
1847 if (dszlogpath) dszlog(dszlogpath, 'E', zi->bytes_received,
1848 zi->fname, bps, errors, 0, 0);
1849 }
1850 switch (tryz()) {
1851 case ZCOMPL:
1852 return OK;
1853 default:
1854 return ERROR;
1855 case ZFILE:
1856 break;
1857 }
1858 continue;
1859 default:
1860 return c;
1861 case ERROR:
1862 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: error",shortname,protname()));
1863 if (dszlogpath) dszlog(dszlogpath, 'E', zi->bytes_received,
1864 zi->fname, bps, errors, 0, 0);
1865 return ERROR;
1866 }
1867 }
1868 }
1869
1870 /* "OOSB" means Out Of Sync Block. I once thought that if sz sents
1871 * blocks a,b,c,d, of which a is ok, b fails, we might want to save
1872 * c and d. But, alas, i never saw c and d.
1873 */
1874 #define SAVE_OOSB
1875 #ifdef SAVE_OOSB
1876 typedef struct oosb_t {
1877 size_t pos;
1878 size_t len;
1879 char *data;
1880 struct oosb_t *next;
1881 } oosb_t;
1882 struct oosb_t *anker=NULL;
1883 #endif
1884
1885 /*
1886 * Receive a file with ZMODEM protocol
1887 * Assumes file name frame is in secbuf
1888 */
1889 static int
1890 rzfile(struct zm_fileinfo *zi)
1891 {
1892 register int c, n;
1893 long last_rxbytes=0;
1894 unsigned long last_bps=0;
1895 long not_printed=0;
1896 time_t low_bps=0;
1897 size_t bytes_in_block=0;
1898
1899 zi->eof_seen=FALSE;
1900
1901 n = 20;
1902
1903 if (procheader(secbuf,zi) == ERROR) {
1904 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: procheader error",
1905 shortname,protname()));
1906 return (tryzhdrtype = ZSKIP);
1907 }
1908
1909 for (;;) {
1910 #ifdef SEGMENTS
1911 chinseg = 0;
1912 #endif
1913 stohdr(zi->bytes_received);
1914 zshhdr(ZRPOS, Txhdr);
1915 goto skip_oosb;
1916 nxthdr:
1917 #ifdef SAVE_OOSB
1918 if (anker) {
1919 oosb_t *akt,*last,*next;
1920 for (akt=anker,last=NULL;akt;last= akt ? akt : last ,akt=next) {
1921 if (akt->pos==zi->bytes_received) {
1922 putsec(zi, akt->data, akt->len);
1923 zi->bytes_received += akt->len;
1924 vfile("using saved out-of-sync-paket %lx, len %ld",
1925 akt->pos,akt->len);
1926 goto nxthdr;
1927 }
1928 next=akt->next;
1929 if (akt->pos<zi->bytes_received) {
1930 vfile("removing unneeded saved out-of-sync-paket %lx, len %ld",
1931 akt->pos,akt->len);
1932 if (last)
1933 last->next=akt->next;
1934 else
1935 anker=akt->next;
1936 free(akt->data);
1937 free(akt);
1938 akt=NULL;
1939 }
1940 }
1941 }
1942 #endif
1943 skip_oosb:
1944 c = zgethdr(Rxhdr, 0, NULL);
1945 switch (c) {
1946 default:
1947 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: error: zgethdr returned %d",shortname,
1948 protname(),c));
1949 vfile("rzfile: zgethdr returned %d", c);
1950 return ERROR;
1951 case ZNAK:
1952 case TIMEOUT:
1953 #ifdef SEGMENTS
1954 putsec(secbuf, chinseg);
1955 chinseg = 0;
1956 #endif
1957 if ( --n < 0) {
1958 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: error: zgethdr returned %s",shortname,
1959 protname(),c == ZNAK ? "ZNAK" : "TIMEOUT"));
1960 vfile("rzfile: zgethdr returned %d", c);
1961 return ERROR;
1962 }
1963 case ZFILE:
1964 zrdata(secbuf, MAX_BLOCK,&bytes_in_block);
1965 continue;
1966 case ZEOF:
1967 #ifdef SEGMENTS
1968 putsec(secbuf, chinseg);
1969 chinseg = 0;
1970 #endif
1971 if (rclhdr(Rxhdr) != (long) zi->bytes_received) {
1972 /*
1973 * Ignore eof if it's at wrong place - force
1974 * a timeout because the eof might have gone
1975 * out before we sent our zrpos.
1976 */
1977 errors = 0; goto nxthdr;
1978 }
1979 if (closeit(zi)) {
1980 tryzhdrtype = ZFERR;
1981 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: error: closeit return <>0",
1982 shortname, protname()));
1983 vfile("rzfile: closeit returned <> 0");
1984 return ERROR;
1985 }
1986 vfile("rzfile: normal EOF");
1987 return c;
1988 case ERROR: /* Too much garbage in header search error */
1989 #ifdef SEGMENTS
1990 putsec(secbuf, chinseg);
1991 chinseg = 0;
1992 #endif
1993 if ( --n < 0) {
1994 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: error: zgethdr returned %d",
1995 shortname, protname(),c));
1996 vfile("rzfile: zgethdr returned %d", c);
1997 return ERROR;
1998 }
1999 zmputs(Attn);
2000 continue;
2001 case ZSKIP:
2002 #ifdef SEGMENTS
2003 putsec(secbuf, chinseg);
2004 chinseg = 0;
2005 #endif
2006 closeit(zi);
2007 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: error: sender skipped",
2008 shortname, protname()));
2009 vfile("rzfile: Sender SKIPPED file");
2010 return c;
2011 case ZDATA:
2012 if (rclhdr(Rxhdr) != (long) zi->bytes_received) {
2013 #if defined(SAVE_OOSB)
2014 oosb_t *neu;
2015 size_t pos=rclhdr(Rxhdr);
2016 #endif
2017 if ( --n < 0) {
2018 vfile("rzfile: out of sync");
2019 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: error: out of sync",
2020 shortname, protname()));
2021 return ERROR;
2022 }
2023 #if defined(SAVE_OOSB)
2024 switch (c = zrdata(secbuf, MAX_BLOCK,&bytes_in_block))
2025 {
2026 case GOTCRCW:
2027 case GOTCRCG:
2028 case GOTCRCE:
2029 case GOTCRCQ:
2030 if (pos>zi->bytes_received) {
2031 neu=malloc(sizeof(oosb_t));
2032 if (neu)
2033 neu->data=malloc(bytes_in_block);
2034 if (neu && neu->data) {
2035 #ifdef ENABLE_SYSLOG
2036 /* call syslog to tell me if this happens */
2037 lsyslog(LOG_ERR,
2038 "saving out-of-sync-block %lx, len %lu",
2039 pos, (unsigned long) bytes_in_block);
2040 #endif
2041 vfile("saving out-of-sync-block %lx, len %lu",pos,
2042 (unsigned long) bytes_in_block);
2043 memcpy(neu->data,secbuf,bytes_in_block);
2044 neu->pos=pos;
2045 neu->len=bytes_in_block;
2046 neu->next=anker;
2047 anker=neu;
2048 }
2049 else if (neu)
2050 free(neu);
2051 }
2052 }
2053 #endif
2054 #ifdef SEGMENTS
2055 putsec(secbuf, chinseg);
2056 chinseg = 0;
2057 #endif
2058 zmputs(Attn); continue;
2059 }
2060 moredata:
2061 if ((Verbose>1 || min_bps || stop_time)
2062 && (not_printed > (min_bps ? 3 : 7)
2063 || zi->bytes_received > last_bps / 2 + last_rxbytes)) {
2064 int minleft = 0;
2065 int secleft = 0;
2066 time_t now;
2067 double d;
2068 d=timing(0,&now);
2069 if (d==0)
2070 d=0.5; /* timing() might use time() */
2071 last_bps=zi->bytes_received/d;
2072 if (last_bps > 0) {
2073 minleft = (R_BYTESLEFT(zi))/last_bps/60;
2074 secleft = ((R_BYTESLEFT(zi))/last_bps)%60;
2075 }
2076 if (min_bps) {
2077 if (low_bps) {
2078 if (last_bps<min_bps) {
2079 if (now-low_bps>=min_bps_time) {
2080 /* too bad */
2081 vfile(_("rzfile: bps rate %ld below min %ld"),
2082 last_bps, min_bps);
2083 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: bps rate low: %ld < %ld",
2084 shortname, protname(), last_bps, min_bps));
2085 return ERROR;
2086 }
2087 }
2088 else
2089 low_bps=0;
2090 } else if (last_bps<min_bps) {
2091 low_bps=now;
2092 }
2093 }
2094 if (stop_time && now>=stop_time) {
2095 /* too bad */
2096 vfile(_("rzfile: reached stop time"));
2097 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: reached stop time",
2098 shortname, protname()));
2099 return ERROR;
2100 }
2101
2102 if (Verbose > 1) {
2103 vstringf(_("\rBytes received: %7ld/%7ld BPS:%-6ld ETA %02d:%02d "),
2104 (long) zi->bytes_received, (long) zi->bytes_total,
2105 last_bps, minleft, secleft);
2106 last_rxbytes=zi->bytes_received;
2107 not_printed=0;
2108 }
2109 } else if (Verbose)
2110 not_printed++;
2111 #ifdef SEGMENTS
2112 if (chinseg >= (MAX_BLOCK * SEGMENTS)) {
2113 putsec(secbuf, chinseg);
2114 chinseg = 0;
2115 }
2116 switch (c = zrdata(secbuf+chinseg, MAX_BLOCK,&bytes_in_block))
2117 #else
2118 switch (c = zrdata(secbuf, MAX_BLOCK,&bytes_in_block))
2119 #endif
2120 {
2121 case ZCAN:
2122 #ifdef SEGMENTS
2123 putsec(secbuf, chinseg);
2124 chinseg = 0;
2125 #endif
2126 vfile("rzfile: zrdata returned %d", c);
2127 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: zrdata returned ZCAN",
2128 shortname, protname()));
2129 return ERROR;
2130 case ERROR: /* CRC error */
2131 #ifdef SEGMENTS
2132 putsec(secbuf, chinseg);
2133 chinseg = 0;
2134 #endif
2135 if ( --n < 0) {
2136 vfile("rzfile: zgethdr returned %d", c);
2137 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: zrdata returned ERROR",
2138 shortname, protname()));
2139 return ERROR;
2140 }
2141 zmputs(Attn);
2142 continue;
2143 case TIMEOUT:
2144 #ifdef SEGMENTS
2145 putsec(secbuf, chinseg);
2146 chinseg = 0;
2147 #endif
2148 if ( --n < 0) {
2149 DO_SYSLOG_FNAME((LOG_INFO, "%s/%s: zrdata returned TIMEOUT",
2150 shortname, protname()));
2151 vfile("rzfile: zgethdr returned %d", c);
2152 return ERROR;
2153 }
2154 continue;
2155 case GOTCRCW:
2156 n = 20;
2157 #ifdef SEGMENTS
2158 chinseg += bytes_in_block;
2159 putsec(zi, secbuf, chinseg);
2160 chinseg = 0;
2161 #else
2162 putsec(zi, secbuf, bytes_in_block);
2163 #endif
2164 zi->bytes_received += bytes_in_block;
2165 stohdr(zi->bytes_received);
2166 zshhdr(ZACK | 0x80, Txhdr);
2167 goto nxthdr;
2168 case GOTCRCQ:
2169 n = 20;
2170 #ifdef SEGMENTS
2171 chinseg += bytes_in_block;
2172 #else
2173 putsec(zi, secbuf, bytes_in_block);
2174 #endif
2175 zi->bytes_received += bytes_in_block;
2176 stohdr(zi->bytes_received);
2177 zshhdr(ZACK, Txhdr);
2178 goto moredata;
2179 case GOTCRCG:
2180 n = 20;
2181 #ifdef SEGMENTS
2182 chinseg += bytes_in_block;
2183 #else
2184 putsec(zi, secbuf, bytes_in_block);
2185 #endif
2186 zi->bytes_received += bytes_in_block;
2187 goto moredata;
2188 case GOTCRCE:
2189 n = 20;
2190 #ifdef SEGMENTS
2191 chinseg += bytes_in_block;
2192 #else
2193 putsec(zi, secbuf, bytes_in_block);
2194 #endif
2195 zi->bytes_received += bytes_in_block;
2196 goto nxthdr;
2197 }
2198 }
2199 }
2200 }
2201
2202 /*
2203 * Send a string to the modem, processing for \336 (sleep 1 sec)
2204 * and \335 (break signal)
2205 */
2206 static void
2207 zmputs(const char *s)
2208 {
2209 const char *p;
2210
2211 while (s && *s)
2212 {
2213 p=strpbrk(s,"\335\336");
2214 if (!p)
2215 {
2216 if (write(1,s,strlen(s)) < 0) { /* best-effort console output; ignore */ }
2217 return;
2218 }
2219 if (p!=s)
2220 {
2221 if (write(1,s,(size_t) (p-s)) < 0) { /* best-effort console output; ignore */ }
2222 s=p;
2223 }
2224 if (*p=='\336')
2225 sleep(1);
2226 else
2227 sendbrk(0);
2228 p++;
2229 }
2230 }
2231
2232 /*
2233 * Close the receive dataset, return OK or ERROR
2234 */
2235 static int
2236 closeit(struct zm_fileinfo *zi)
2237 {
2238 int ret;
2239 if (Topipe) {
2240 if (pclose(fout)) {
2241 return ERROR;
2242 }
2243 return OK;
2244 }
2245 ret=fclose(fout);
2246 if (ret) {
2247 zpfatal(_("file close error"));
2248 /* this may be any sort of error, including random data corruption */
2249
2250 unlink(Pathname);
2251 return ERROR;
2252 }
2253 if (zi->modtime) {
2254 #ifdef HAVE_STRUCT_UTIMBUF
2255 struct utimbuf timep;
2256 timep.actime = time(NULL);
2257 timep.modtime = zi->modtime;
2258 utime(Pathname, &timep);
2259 #else
2260 time_t timep[2];
2261 timep[0] = time(NULL);
2262 timep[1] = zi->modtime;
2263 utime(Pathname, timep);
2264 #endif
2265 }
2266 #ifdef S_ISREG
2267 if (S_ISREG(zi->mode)) {
2268 #else
2269 if ((zi->mode&S_IFMT) == S_IFREG) {
2270 #endif
2271 /* we must not make this program executable if running
2272 * under rsh, because the user might have uploaded an
2273 * unrestricted shell.
2274 */
2275 if (under_rsh)
2276 chmod(Pathname, (00666 & zi->mode));
2277 else
2278 chmod(Pathname, (07777 & zi->mode));
2279 }
2280 return OK;
2281 }
2282
2283 /*
2284 * Ack a ZFIN packet, let byegones be byegones
2285 */
2286 static void
2287 ackbibi(void)
2288 {
2289 int n;
2290
2291 vfile("ackbibi:");
2292 Readnum = 1;
2293 stohdr(0L);
2294 for (n=3; --n>=0; ) {
2295 purgeline(0);
2296 zshhdr(ZFIN, Txhdr);
2297 switch (READLINE_PF(100)) {
2298 case 'O':
2299 READLINE_PF(1); /* Discard 2nd 'O' */
2300 vfile("ackbibi complete");
2301 return;
2302 case RCDO:
2303 return;
2304 case TIMEOUT:
2305 default:
2306 break;
2307 }
2308 }
2309 }
2310
2311 /*
2312 * Strip leading ! if present, do shell escape.
2313 */
2314 static int
2315 sys2(const char *s)
2316 {
2317 if (*s == '!')
2318 ++s;
2319 return system(s);
2320 }
2321
2322 /*
2323 * Strip leading ! if present, do exec.
2324 */
2325 static void
2326 exec2(const char *s)
2327 {
2328 if (*s == '!')
2329 ++s;
2330 io_mode(0,0);
2331 execl("/bin/sh", "sh", "-c", s, NULL);
2332 zpfatal("execl");
2333 exit(1);
2334 }
2335
2336 /*
2337 * Routine to calculate the free bytes on the current file system
2338 * ~0 means many free bytes (unknown)
2339 */
2340 static size_t
2341 getfree(void)
2342 {
2343 return((size_t) (~0L)); /* many free bytes ... */
2344 }
2345
2346 /* End of lrz.c */