This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
clloader.c
2854 lines (2628 loc) · 67.7 KB
/
clloader.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
clloader receive files with x/y/zmodem
Copyright (C) until 1988 Chuck Forsberg (Omen Technology INC)
Copyright (C) 1994 Matt Porter, Michael D. Black
Copyright (C) 1996, 1997 Uwe Ohse
**** Modifed for Clanton Ardunio loading.
Copyright (C) 2013, Intel Corporation, All rights reserved.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
02111-1307, USA.
originally written by Chuck Forsberg
*/
#include "zglobal.h"
#define SS_NORMAL 0
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <ctype.h>
#include <errno.h>
#include <getopt.h>
#ifdef HAVE_UTIME_H
#include <utime.h>
#endif
#include "timing.h"
#include "long-options.h"
#include "xstrtoul.h"
#include "error.h"
#ifndef STRICT_PROTOTYPES
extern time_t time();
extern char *strerror();
extern char *strstr();
#endif
#ifndef HAVE_ERRNO_DECLARATION
extern int errno;
#endif
#define MAX_BLOCK 8192
/*
* Max value for HOWMANY is 255 if NFGVMIN is not defined.
* A larger value reduces system overhead but may evoke kernel bugs.
* 133 corresponds to an XMODEM/CRC sector
*/
#ifndef HOWMANY
#ifdef NFGVMIN
#define HOWMANY MAX_BLOCK
#else
#define HOWMANY 255
#endif
#endif
#ifndef CLANTON_LOADER
#define CLANTON_LOADER
#endif
#ifdef CLANTON_LOADER
#include "clloader.h"
static struct daemon_state mystate;
#endif
unsigned Baudrate = 2400;
FILE *fout;
int Lastrx;
int Crcflg;
int Firstsec;
int errors;
int Restricted=0; /* restricted; no /.. or ../ in filenames */
int Readnum = HOWMANY; /* Number of bytes to ask for in read() from modem */
int skip_if_not_found;
char *Pathname;
const char *program_name; /* the name by which we were called */
static int Topipe=0;
int MakeLCPathname=TRUE; /* make received pathname lower case */
int Verbose=0;
int Quiet=0; /* overrides logic that would otherwise set verbose */
int Nflag = 0; /* Don't really transfer files */
int Rxclob=FALSE; /* Clobber existing file */
int Rxbinary=TRUE; /* receive all files in bin mode */
int Rxascii=FALSE; /* receive files in ascii (translate) mode */
int Thisbinary; /* current file is to be received in bin mode */
int try_resume=FALSE;
int allow_remote_commands=TRUE;
int junk_path=FALSE;
int no_timeout=FALSE;
enum zm_type_enum protocol;
int under_rsh=FALSE;
int zmodem_requested=FALSE;
#ifdef CLANTON_LOADER
static int clantonLoaderCmdDetected=0;
char *clantonLoaderCmdStr=0;
int clantonLoaderExecCommand(char *);
int clantonLeavePassThroughState=0;
#endif
#ifdef SEGMENTS
int chinseg = 0; /* Number of characters received in this data seg */
char secbuf[1+(SEGMENTS+1)*MAX_BLOCK];
#else
char secbuf[MAX_BLOCK + 1];
#endif
#ifdef ENABLE_TIMESYNC
int timesync_flag=0;
int in_timesync=0;
#endif
int in_tcpsync=0;
int tcpsync_flag=1;
int tcp_socket=-1;
int tcp_flag=0;
char *tcp_server_address=NULL;
char tcp_buf[256]="";
#if defined(F_GETFD) && defined(F_SETFD) && defined(O_SYNC)
static int o_sync = 0;
#endif
static int rzfiles __P ((struct zm_fileinfo *));
static int tryz __P ((void));
static void checkpath __P ((const char *name));
static void chkinvok __P ((const char *s));
static void report __P ((int sct));
static void uncaps __P ((char *s));
static int IsAnyLower __P ((const char *s));
static int putsec __P ((struct zm_fileinfo *zi, char *buf, size_t n));
static int make_dirs __P ((char *pathname));
static int procheader __P ((char *name, struct zm_fileinfo *));
static int wcgetsec __P ((size_t *Blklen, char *rxbuf, unsigned int maxtime));
static int wcrx __P ((struct zm_fileinfo *));
static int wcrxpn __P ((struct zm_fileinfo *, char *rpn));
static int wcreceive __P ((int argc, char **argp));
static int rzfile __P ((struct zm_fileinfo *));
static void usage __P ((int exitcode, const char *what));
static void usage1 __P ((int exitcode));
static void exec2 __P ((const char *s));
static int closeit __P ((struct zm_fileinfo *));
static void ackbibi __P ((void));
static int sys2 __P ((const char *s));
static void zmputs __P ((const char *s));
static size_t getfree __P ((void));
static long buffersize=32768;
static unsigned long min_bps=0;
static long min_bps_time=120;
char Lzmanag; /* Local file management request */
char zconv; /* ZMODEM file conversion request */
char zmanag; /* ZMODEM file management request */
char ztrans; /* ZMODEM file transport request */
int Zctlesc; /* Encode control characters */
int Zrwindow = 1400; /* RX window size (controls garbage count) */
int tryzhdrtype=ZRINIT; /* Header type to send corresponding to Last rx close */
time_t stop_time;
#ifdef ENABLE_SYSLOG
# if defined(ENABLE_SYSLOG_FORCE) || defined(ENABLE_SYSLOG_DEFAULT)
int enable_syslog=TRUE;
# else
int enable_syslog=FALSE;
# endif
#define DO_SYSLOG_FNAME(message) do { \
if (enable_syslog) { \
const char *shortname; \
if (!zi->fname) \
shortname="no.name"; \
else { \
shortname=strrchr(zi->fname,'/'); \
if (!shortname) \
shortname=zi->fname; \
else \
shortname++; \
} \
lsyslog message ; \
} \
} while(0)
#define DO_SYSLOG(message) do { \
if (enable_syslog) { \
lsyslog message ; \
} \
} while(0)
#else
#define DO_SYSLOG_FNAME(message) do { } while(0)
#define DO_SYSLOG(message) do { } while(0)
#endif
int my_fclose(FILE *fp)
{
int ret=0;
if (Verbose)
fprintf(stderr,"\nClloader: fclose: %p\n",(void*)fp);
if (fp) {
ret = fclose(fp);
/* on line errors, attempt to close fout twice results in segfault. */
if ( fp == fout)
fout=NULL;
}
else
{
if (Verbose)
fprintf(stderr,"\nClloader: fclose attempt on closed file\n");
}
return(ret);
}
/* called by signal interrupt or terminate to clean things up */
RETSIGTYPE
bibi(int n)
{
if (zmodem_requested)
zmputs(Attn);
canit(STDOUT_FILENO);
io_mode(0,0);
if ( mystate.slave_pid) {
int wait_status;
if( kill(mystate.slave_pid,SIGKILL )){
fprintf(stderr,"Attempt to kill child process:pid %d failed\n",mystate.slave_pid);
perror("Child kill fail");
}
waitpid(mystate.slave_pid, &wait_status, WNOHANG); /* get rid of zombe from process table */
}
error(128+n,0,_("caught signal %d; exiting"), n);
}
/* called by signal hup to trigger state change of bootloader */
RETSIGTYPE
hup_signal(int n)
{
if ( Verbose )
fprintf(stderr,"signal- hup signal rxed.. go back to wait for command ... \n",mystate.slave_pid);
clantonLeavePassThroughState++;
}
RETSIGTYPE
chld_signal_ignore(int n)
{
if ( Verbose )
fprintf(stderr,"signal- child ignore signal rxed\n");
}
/* called by signal sketch process died to trigger state change of bootloader */
RETSIGTYPE
chld_signal(int n)
{
if ( mystate.slave_pid != 0 )
{
if ( Verbose )
fprintf(stderr,"sigchld signal rxed.. child pid:%d terminated, go back to wait for command ... \n",mystate.slave_pid);
clantonLeavePassThroughState++;
mystate.slave_pid = 0 ; // Since the child termianted, no need to kill todo: get info in signal to make sure it was the sketch
}
else
{
if ( Verbose )
fprintf(stderr,"sigchld signal rxed.. terminated. Do not nothing pid:%d \n",mystate.slave_pid);
}
}
static struct option const long_options[] =
{
{"append", no_argument, NULL, '+'},
{"ascii", no_argument, NULL, 'a'},
{"binary", no_argument, NULL, 'b'},
{"bufsize", required_argument, NULL, 'B'},
{"allow-commands", no_argument, NULL, 'C'},
{"allow-remote-commands", no_argument, NULL, 'C'},
{"escape", no_argument, NULL, 'e'},
{"rename", no_argument, NULL, 'E'},
{"help", no_argument, NULL, 'h'},
{"crc-check", no_argument, NULL, 'H'},
{"junk-path", no_argument, NULL, 'j'},
{"errors", required_argument, NULL, 3},
{"disable-timeouts", no_argument, NULL, 'O'},
{"disable-timeout", no_argument, NULL, 'O'}, /* i can't get it right */
{"min-bps", required_argument, NULL, 'm'},
{"min-bps-time", required_argument, NULL, 'M'},
{"newer", no_argument, NULL, 'n'},
{"newer-or-longer", no_argument, NULL, 'N'},
{"protect", no_argument, NULL, 'p'},
{"resume", no_argument, NULL, 'r'},
{"restricted", no_argument, NULL, 'R'},
{"quiet", no_argument, NULL, 'q'},
{"stop-at", required_argument, NULL, 's'},
{"timesync", no_argument, NULL, 'S'},
{"timeout", required_argument, NULL, 't'},
{"keep-uppercase", no_argument, NULL, 'u'},
{"unrestrict", no_argument, NULL, 'U'},
{"verbose", no_argument, NULL, 'v'},
{"windowsize", required_argument, NULL, 'w'},
{"with-crc", no_argument, NULL, 'c'},
{"xmodem", no_argument, NULL, 'X'},
{"ymodem", no_argument, NULL, 1},
{"zmodem", no_argument, NULL, 'Z'},
{"overwrite", no_argument, NULL, 'y'},
{"null", no_argument, NULL, 'D'},
{"syslog", optional_argument, NULL , 2},
{"delay-startup", required_argument, NULL, 4},
{"o-sync", no_argument, NULL, 5},
{"o_sync", no_argument, NULL, 5},
{"tcp-server", no_argument, NULL, 6},
{"tcp-client", required_argument, NULL, 7},
{NULL,0,NULL,0}
};
static void
show_version(void)
{
printf ("%s (%s) %s\n", program_name, PACKAGE, VERSION);
}
int
main(int argc, char *argv[])
{
register char *cp;
register int npats;
char **patts=NULL; /* keep compiler quiet */
int exitcode=0;
int c;
unsigned int startup_delay=0;
Rxtimeout = 100;
if ((cp=getenv("SHELL")) && (strstr(cp, "rsh") || strstr(cp, "rksh")
|| strstr(cp,"rbash") || strstr(cp, "rshell")))
under_rsh=TRUE;
if ((cp=getenv("ZMODEM_RESTRICTED"))!=NULL)
Restricted=2;
/* make temporary and unfinished files */
umask(0077);
from_cu();
chkinvok(argv[0]); /* if called as [-]rzCOMMAND set flag */
#ifdef ENABLE_SYSLOG
openlog(program_name,LOG_PID,ENABLE_SYSLOG);
#endif
setlocale (LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
parse_long_options (argc, argv, show_version, usage1);
while ((c = getopt_long (argc, argv,
"a+bB:cCDeEhm:M:OprRqs:St:uUvw:XZy",
long_options, (int *) 0)) != EOF)
{
unsigned long int tmp;
char *tmpptr;
enum strtol_error s_err;
switch (c)
{
case 0:
break;
case '+': Lzmanag = ZF1_ZMAPND; break;
case 'a': Rxascii=TRUE; break;
case 'b': Rxbinary=TRUE; break;
case 'B':
if (strcmp(optarg,"auto")==0)
buffersize=-1;
else
buffersize=strtol(optarg,NULL,10);
break;
case 'c': Crcflg=TRUE; break;
case 'C': allow_remote_commands=TRUE; break;
case 'D': Nflag = TRUE; break;
case 'E': Lzmanag = ZF1_ZMCHNG; break;
case 'e': Zctlesc = 1; break;
case 'h': usage(0,NULL); break;
case 'H': Lzmanag= ZF1_ZMCRC; break;
case 'j': junk_path=TRUE; break;
case 'm':
s_err = xstrtoul (optarg, &tmpptr, 0, &tmp, "km");
min_bps = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("min_bps"), s_err);
break;
case 'M':
s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
min_bps_time = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("min_bps_time"), s_err);
if (min_bps_time<=1)
usage(2,_("min_bps_time must be > 1"));
break;
case 'N': Lzmanag = ZF1_ZMNEWL; break;
case 'n': Lzmanag = ZF1_ZMNEW; break;
case 'O': no_timeout=TRUE; break;
case 'p': Lzmanag = ZF1_ZMPROT; break;
case 'q': Quiet=TRUE; Verbose=0; break;
case 's':
if (isdigit((unsigned char) (*optarg))) {
struct tm *tm;
time_t t;
int hh,mm;
char *nex;
hh = strtoul (optarg, &nex, 10);
if (hh>23)
usage(2,_("hour to large (0..23)"));
if (*nex!=':')
usage(2, _("unparsable stop time\n"));
nex++;
mm = strtoul (optarg, &nex, 10);
if (mm>59)
usage(2,_("minute to large (0..59)"));
t=time(NULL);
tm=localtime(&t);
tm->tm_hour=hh;
tm->tm_min=hh;
stop_time=mktime(tm);
if (stop_time<t)
stop_time+=86400L; /* one day more */
if (stop_time - t <10)
usage(2,_("stop time to small"));
} else {
s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
stop_time = tmp + time(0);
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("stop-at"), s_err);
if (tmp<10)
usage(2,_("stop time to small"));
}
break;
case 'r':
if (try_resume)
Lzmanag= ZF1_ZMCRC;
else
try_resume=TRUE;
break;
case 'R': Restricted++; break;
case 'S':
#ifdef ENABLE_TIMESYNC
timesync_flag++;
if (timesync_flag==2) {
#ifdef HAVE_SETTIMEOFDAY
error(0,0,_("don't have settimeofday, will not set time\n"));
#endif
if (getuid()!=0)
error(0,0,
_("not running as root (this is good!), can not set time\n"));
}
#endif
break;
case 't':
s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
Rxtimeout = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("timeout"), s_err);
if (Rxtimeout<10 || Rxtimeout>1000)
usage(2,_("timeout out of range 10..1000"));
break;
case 'w':
s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
Zrwindow = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("window size"), s_err);
break;
case 'u':
MakeLCPathname=FALSE; break;
case 'U':
if (!under_rsh)
Restricted=0;
else {
DO_SYSLOG((LOG_INFO,"--unrestrict option used under restricted shell"));
error(1,0,
_("security violation: can't do that under restricted shell\n"));
}
break;
case 'v':
++Verbose; break;
case 'X': protocol=ZM_XMODEM; break;
case 1: protocol=ZM_YMODEM; break;
case 'Z': protocol=ZM_ZMODEM; break;
case 'y':
Rxclob=TRUE; break;
case 2:
#ifdef ENABLE_SYSLOG
# ifndef ENABLE_SYSLOG_FORCE
if (optarg && (!strcmp(optarg,"off") || !strcmp(optarg,"no"))) {
if (under_rsh)
error(0,0, _("cannot turnoff syslog"));
else
enable_syslog=FALSE;
}
else
enable_syslog=TRUE;
# else
error(0,0, _("cannot turnoff syslog"));
# endif
#endif
case 3:
s_err = xstrtoul (optarg, NULL, 0, &tmp, "km");
bytes_per_error = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("bytes_per_error"), s_err);
if (bytes_per_error<100)
usage(2,_("bytes-per-error should be >100"));
break;
case 4:
s_err = xstrtoul (optarg, NULL, 0, &tmp, NULL);
startup_delay = tmp;
if (s_err != LONGINT_OK)
STRTOL_FATAL_ERROR (optarg, _("startup delay"), s_err);
break;
case 5:
#if defined(F_GETFD) && defined(F_SETFD) && defined(O_SYNC)
o_sync=1;
#else
error(0,0, _("O_SYNC not supported by the kernel"));
#endif
break;
case 6:
tcp_flag=2;
break;
case 7:
tcp_flag=3;
tcp_server_address=(char *)strdup(optarg);
if (!tcp_server_address)
error(1,0,_("out of memory"));
break;
default:
usage(2,NULL);
}
}
if (getuid()!=geteuid()) {
error(1,0,
_("this program was never intended to be used setuid\n"));
}
/* initialize zsendline tab */
zsendline_init();
#ifdef HAVE_SIGINTERRUPT
siginterrupt(SIGALRM,1);
#endif
if (startup_delay)
sleep(startup_delay);
npats = argc - optind;
patts=&argv[optind];
if (npats > 1)
usage(2,_("garbage on commandline"));
if (protocol!=ZM_XMODEM && npats)
usage(2, _("garbage on commandline"));
if (Restricted && allow_remote_commands) {
allow_remote_commands=FALSE;
}
if (Verbose == 0)
setbuf(stderr, NULL);
if (Fromcu && !Quiet) {
if (Verbose == 0)
Verbose = 2;
}
vfile("%s %s\n", program_name, VERSION);
if (tcp_flag==2) {
char buf[256];
#ifdef MAXHOSTNAMELEN
char hn[MAXHOSTNAMELEN];
#else
char hn[256];
#endif
char *p,*q;
int d;
/* tell receiver to receive via tcp */
d=tcp_server(buf);
p=strchr(buf+1,'<');
p++;
q=strchr(p,'>');
*q=0;
if (gethostname(hn,sizeof(hn))==-1) {
error(1,0, _("hostname too long\n"));
}
fprintf(stdout,"connect with lrz --tcp-client \"%s:%s\"\n",hn,p);
fflush(stdout);
/* ok, now that this file is sent we can switch to tcp */
tcp_socket=tcp_accept(d);
dup2(tcp_socket,0);
dup2(tcp_socket,1);
}
if (tcp_flag==3) {
char buf[256];
char *p;
p=strchr(tcp_server_address,':');
if (!p)
error(1,0, _("illegal server address\n"));
*p++=0;
sprintf(buf,"[%s] <%s>\n",tcp_server_address,p);
fprintf(stdout,"connecting to %s\n",buf);
fflush(stdout);
/* we need to switch to tcp mode */
tcp_socket=tcp_connect(buf);
dup2(tcp_socket,0);
dup2(tcp_socket,1);
}
io_mode(0,1);
readline_setup(0, HOWMANY, MAX_BLOCK*2);
if (signal(SIGINT, bibi) == SIG_IGN)
signal(SIGINT, SIG_IGN);
else
signal(SIGINT, bibi);
signal(SIGTERM, bibi);
signal(SIGPIPE, bibi);
signal(SIGHUP, hup_signal);
signal(SIGCHLD, chld_signal_ignore);
#ifndef CLANTON_LOADER
if (wcreceive(npats, patts)==ERROR) {
exitcode=0200;
canit(STDOUT_FILENO);
}
#else
if (clantonLoaderFSM(npats, patts)==ERROR) {
exitcode=0200;
canit(STDOUT_FILENO);
}
#endif
io_mode(0,0);
if (exitcode && !zmodem_requested) /* bellow again with all thy might. */
canit(STDOUT_FILENO);
if (Verbose)
{
fputs("\r\n",stderr);
if (exitcode)
fputs(_("Transfer incomplete\n"),stderr);
else
fputs(_("Transfer complete\n"),stderr);
}
exit(exitcode);
}
static void
usage1(int exitcode)
{
usage(exitcode,NULL);
}
static void
usage(int exitcode, const char *what)
{
FILE *f=stdout;
if (exitcode)
{
if (what)
fprintf(stderr, "%s: %s\n",program_name,what);
fprintf (stderr, _("Try `%s --help' for more information.\n"),
program_name);
exit(exitcode);
}
fprintf(f, _("%s version %s\n"), program_name,
VERSION);
fprintf(f,_("Usage: %s [options] [filename.if.xmodem]\n"), program_name);
fputs(_("Receive files with ZMODEM/YMODEM/XMODEM protocol\n"),f);
fputs(_(
" (X) = option applies to XMODEM only\n"
" (Y) = option applies to YMODEM only\n"
" (Z) = option applies to ZMODEM only\n"
),f);
fputs(_(
" -+, --append append to existing files\n"
" -a, --ascii ASCII transfer (change CR/LF to LF)\n"
" -b, --binary binary transfer\n"
" -B, --bufsize N buffer N bytes (N==auto: buffer whole file)\n"
" -c, --with-crc Use 16 bit CRC (X)\n"
" -C, --allow-remote-commands allow execution of remote commands (Z)\n"
" -D, --null write all received data to /dev/null\n"
" --delay-startup N sleep N seconds before doing anything\n"
" -e, --escape Escape control characters (Z)\n"
" -E, --rename rename any files already existing\n"
" --errors N generate CRC error every N bytes (debugging)\n"
" -h, --help Help, print this usage message\n"
" -m, --min-bps N stop transmission if BPS below N\n"
" -M, --min-bps-time N for at least N seconds (default: 120)\n"
" -O, --disable-timeouts disable timeout code, wait forever for data\n"
" --o-sync open output file(s) in synchronous write mode\n"
" -p, --protect protect existing files\n"
" -q, --quiet quiet, no progress reports\n"
" -r, --resume try to resume interrupted file transfer (Z)\n"
" -R, --restricted restricted, more secure mode\n"
" -s, --stop-at {HH:MM|+N} stop transmission at HH:MM or in N seconds\n"
" -S, --timesync request remote time (twice: set local time)\n"
" --syslog[=off] turn syslog on or off, if possible\n"
" -t, --timeout N set timeout to N tenths of a second\n"
" -u, --keep-uppercase keep upper case filenames\n"
" -U, --unrestrict disable restricted mode (if allowed to)\n"
" -v, --verbose be verbose, provide debugging information\n"
" -w, --windowsize N Window is N bytes (Z)\n"
" -X --xmodem use XMODEM protocol\n"
" -y, --overwrite Yes, clobber existing file if any\n"
" --ymodem use YMODEM protocol\n"
" -Z, --zmodem use ZMODEM protocol\n"
"\n"
"short options use the same arguments as the long ones\n"
),f);
exit(exitcode);
}
/*
* Let's receive something already.
*/
static int
wcreceive(int argc, char **argp)
{
int c;
struct zm_fileinfo zi;
#ifdef ENABLE_SYSLOG
const char *shortname=NULL;;
#endif
zi.fname=NULL;
zi.modtime=0;
zi.mode=0;
zi.bytes_total=0;
zi.bytes_sent=0;
zi.bytes_received=0;
zi.bytes_skipped=0;
zi.eof_seen=0;
if (protocol!=ZM_XMODEM || argc==0) {
Crcflg=1;
if ( !Quiet)
vstringf(_("%s waiting to receive."), program_name);
if ((c=tryz())!=0) {
if (c == ZCOMPL)
return OK;
if (c == ERROR)
goto fubar;
c = rzfiles(&zi);
#ifdef ENABLE_SYSLOG
shortname=NULL;
#endif
if (c)
goto fubar;
} else {
for (;;) {
if (Verbose > 1
#ifdef ENABLE_SYSLOG
|| enable_syslog
#endif
)
timing(1,NULL);
#ifdef ENABLE_SYSLOG
shortname=NULL;
#endif
if (wcrxpn(&zi,secbuf)== ERROR)
goto fubar;
if (secbuf[0]==0)
return OK;
if (procheader(secbuf, &zi) == ERROR)
goto fubar;
#ifdef ENABLE_SYSLOG
shortname=strrchr(zi.fname,'/');
if (shortname)
shortname++;
else
shortname=zi.fname;
#endif
if (wcrx(&zi)==ERROR)
goto fubar;
if (Verbose > 1
#ifdef ENABLE_SYSLOG
|| enable_syslog
#endif
) {
double d;
long bps;
d=timing(0,NULL);
if (d==0)
d=0.5; /* can happen if timing uses time() */
bps=(zi.bytes_received-zi.bytes_skipped)/d;
if (Verbose>1) {
vstringf(
_("\rBytes received: %7ld/%7ld BPS:%-6ld \r\n"),
(long) zi.bytes_received, (long) zi.bytes_total, bps);
}
#ifdef ENABLE_SYSLOG
if (enable_syslog)
lsyslog(LOG_INFO,"%s/%s: %ld Bytes, %ld BPS",
shortname,protname(),zi.bytes_received, bps);
#endif
}
}
}
} else {
char dummy[128];
dummy[0]='\0'; /* pre-ANSI HPUX cc demands this */
dummy[1]='\0'; /* procheader uses name + 1 + strlen(name) */
zi.bytes_total = DEFBYTL;
if (Verbose > 1
#ifdef ENABLE_SYSLOG
|| enable_syslog
#endif
)
timing(1,NULL);
procheader(dummy, &zi);
if (Pathname)
free(Pathname);
errno=0;
Pathname=malloc(PATH_MAX+1);
if (!Pathname)
error(1,0,_("out of memory"));
strcpy(Pathname, *argp);
checkpath(Pathname);
#ifdef ENABLE_SYSLOG
shortname=strrchr(*argp,'/');
if (shortname)
shortname++;
else
shortname=*argp;
#endif
vchar('\n');
vstringf(_("%s: ready to receive %s"), program_name, Pathname);
vstring("\r\n");
if ((fout=fopen(Pathname, "w")) == NULL) {
#ifdef ENABLE_SYSLOG
if (enable_syslog)
lsyslog(LOG_ERR,"%s/%s: cannot open: %m",
shortname,protname());
#endif
return ERROR;
}
if (wcrx(&zi)==ERROR) {
goto fubar;
}
if (Verbose > 1
#ifdef ENABLE_SYSLOG
|| enable_syslog
#endif
) {
double d;
long bps;
d=timing(0,NULL);
if (d==0)
d=0.5; /* can happen if timing uses time() */
bps=(zi.bytes_received-zi.bytes_skipped)/d;
if (Verbose) {
vstringf(
_("\rBytes received: %7ld BPS:%-6ld \r\n"),
(long) zi.bytes_received, bps);
}
#ifdef ENABLE_SYSLOG
if (enable_syslog)
lsyslog(LOG_INFO,"%s/%s: %ld Bytes, %ld BPS",
shortname,protname(),zi.bytes_received, bps);
#endif
}
}
return OK;
fubar:
#ifdef ENABLE_SYSLOG
if (enable_syslog)
lsyslog(LOG_ERR,"%s/%s: got error",
shortname ? shortname : "no.name", protname());
#endif
canit(STDOUT_FILENO);
if (Topipe && fout) {
pclose(fout); return ERROR;
}
if (fout)
my_fclose(fout);
if (Restricted && Pathname) {
unlink(Pathname);
vstringf(_("\r\n%s: %s removed.\r\n"), program_name, Pathname);
}
return ERROR;
}
/*
* Fetch a pathname from the other end as a C ctyle ASCIZ string.
* Length is indeterminate as long as less than Blklen
* A null string represents no more files (YMODEM)
*/
static int
wcrxpn(struct zm_fileinfo *zi, char *rpn)
{
register int c;
size_t Blklen=0; /* record length of received packets */
#ifdef NFGVMIN
READLINE_PF(1);
#else
purgeline(0);
#endif
et_tu:
Firstsec=TRUE;
zi->eof_seen=FALSE;
sendline(Crcflg?WANTCRC:NAK);
flushmo();
purgeline(0); /* Do read next time ... */
while ((c = wcgetsec(&Blklen, rpn, 100)) != 0) {
if (c == WCEOT) {
zperr( _("Pathname fetch returned EOT"));
sendline(ACK);
flushmo();
purgeline(0); /* Do read next time ... */
READLINE_PF(1);
goto et_tu;
}
return ERROR;
}
sendline(ACK);
flushmo();
return OK;
}
/*
* Adapted from CMODEM13.C, written by
* Jack M. Wierda and Roderick W. Hart
*/
static int
wcrx(struct zm_fileinfo *zi)
{
register int sectnum, sectcurr;
register char sendchar;
size_t Blklen;
Firstsec=TRUE;sectnum=0;
zi->eof_seen=FALSE;
sendchar=Crcflg?WANTCRC:NAK;
for (;;) {
sendline(sendchar); /* send it now, we're ready! */
flushmo();
purgeline(0); /* Do read next time ... */
sectcurr=wcgetsec(&Blklen, secbuf,
(unsigned int) ((sectnum&0177) ? 50 : 130));
report(sectcurr);
if (sectcurr==((sectnum+1) &0377)) {