This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/mbsebbs/zmrecv.c

623 lines
14 KiB
C
Raw Normal View History

2004-11-08 12:24:36 +00:00
/*****************************************************************************
*
* $Id$
* Purpose ...............: Zmodem receive
*
*****************************************************************************
* Copyright (C) 1997-2003
*
* Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10
* 1971 BV IJmuiden
* the Netherlands
*
* This file is part of MBSE BBS.
*
* This BBS 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.
*
* MBSE BBS 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 MBSE BBS; see the file COPYING. If not, write to the Free
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*****************************************************************************/
#include "../config.h"
#include "../lib/mbselib.h"
#include "../lib/users.h"
#include "ttyio.h"
#include "transfer.h"
#include "zmmisc.h"
#include "zmrecv.h"
2004-11-23 20:46:21 +00:00
#include "ymrecv.h"
2004-11-20 13:30:13 +00:00
#include "openport.h"
2004-11-20 18:31:13 +00:00
#include "timeout.h"
#include "input.h"
2004-11-08 12:24:36 +00:00
static FILE *fout = NULL;
char *curfile = NULL;
2004-11-23 22:35:34 +00:00
off_t rxbytes;
2004-11-08 12:24:36 +00:00
static int Eofseen; /* indicates cpm eof (^Z) has been received */
static int errors;
2004-11-23 22:35:34 +00:00
long sbytes;
2004-11-08 12:24:36 +00:00
struct timeval starttime, endtime;
struct timezone tz;
#define DEFBYTL 2000000000L /* default rx file size */
2004-11-23 21:40:30 +00:00
long Bytesleft; /* number of bytes of incoming file left */
2004-11-08 12:24:36 +00:00
static long Modtime; /* Unix style mod time for incoming file */
static int Filemode; /* Unix style mode for incoming file */
2004-11-12 21:25:59 +00:00
static int Thisbinary = TRUE; /* current file is to be received in bin mode */
2004-11-23 21:40:30 +00:00
char *secbuf=0; /* "sector" buffer */
2004-11-08 12:24:36 +00:00
static int tryzhdrtype;
static char zconv; /* ZMODEM file conversion request */
static char zmanag; /* ZMODEM file management request */
static char ztrans; /* ZMODEM file transport request */
static int tryz(void);
static int rzfiles(void);
static int rzfile(void);
static void zmputs(char*);
static int procheader(char*);
static int ackbibi(void);
static long getfree(void);
extern unsigned long rcvdbytes;
2004-11-23 20:46:21 +00:00
extern int zmodem_requested;
2004-11-08 12:24:36 +00:00
2004-11-25 21:00:04 +00:00
int zmrcvfiles(int want1k)
2004-11-08 12:24:36 +00:00
{
2004-11-22 22:05:57 +00:00
int rc;
2004-11-12 21:25:59 +00:00
2004-11-23 20:56:05 +00:00
Syslog('+', "%s: start receive", protname());
2004-11-12 21:25:59 +00:00
2004-11-21 12:06:55 +00:00
zsendline_init();
2004-11-12 21:25:59 +00:00
if (secbuf == NULL)
secbuf = malloc(MAXBLOCK+1);
tryzhdrtype = ZRINIT;
2004-11-21 11:20:08 +00:00
2004-11-12 21:25:59 +00:00
if ((rc = tryz()) < 0) {
2004-11-23 20:56:05 +00:00
Syslog('+', "%s: could not initiate receive, rc=%d", protname(), rc);
2004-11-21 11:20:08 +00:00
} else {
if (rc == 0) {
2004-11-23 22:35:34 +00:00
if (protocol == ZM_ZMODEM) {
Syslog('+', "%s: switching to Ymodem", protname());
protocol = ZM_YMODEM;
2004-11-23 20:46:21 +00:00
}
for (;;) {
2004-11-23 22:35:34 +00:00
/*
* Ymodem or Xmodem receive loop
*/
rxbytes = 0l;
2004-11-25 21:00:04 +00:00
if (wcrxpn(secbuf, want1k) == TERROR) {
2004-11-23 22:35:34 +00:00
rc = 2;
goto fubar;
}
2004-11-23 20:46:21 +00:00
if (secbuf[0] == 0) {
2004-11-23 22:35:34 +00:00
Syslog('z', "%s: session seems complete", protname());
2004-11-23 20:46:21 +00:00
goto fubar;
}
if (procheader(secbuf) == ZFERR) {
rc = 2;
goto fubar;
}
if (wcrx() == TERROR) {
rc = 2;
goto fubar;
}
}
2004-11-21 11:20:08 +00:00
}
2004-11-23 20:46:21 +00:00
/*
* Zmodem receiver
*/
2004-11-12 21:25:59 +00:00
switch (rc) {
case ZCOMPL: rc = 0;
break;
case ZFILE: rc = rzfiles();
break;
2004-11-08 12:24:36 +00:00
}
2004-11-21 11:20:08 +00:00
}
2004-11-23 20:46:21 +00:00
fubar:
2004-11-12 21:25:59 +00:00
if (fout) {
if (closeit(0)) {
2004-11-23 20:56:05 +00:00
WriteError("%s: Error closing file", protname);
2004-11-12 21:25:59 +00:00
}
}
if (secbuf)
free(secbuf);
secbuf = NULL;
2004-11-08 12:24:36 +00:00
2004-11-20 18:31:13 +00:00
io_mode(0, 1); /* Normal raw mode */
/*
2004-11-21 11:20:08 +00:00
* Some programs send some garbage after the transfer, eat these.
2004-11-20 18:31:13 +00:00
*/
2004-11-22 22:05:57 +00:00
purgeline(100);
2004-11-20 18:31:13 +00:00
2004-11-23 20:56:05 +00:00
Syslog('z', "%s: receive rc=%d", protname(), rc);
2004-11-12 21:25:59 +00:00
return abs(rc);
2004-11-08 12:24:36 +00:00
}
/*
* Initialize for Zmodem receive attempt, try to activate Zmodem sender
* Handles ZSINIT frame
* Return ZFILE if Zmodem filename received, -1 on error,
2004-11-23 20:56:05 +00:00
* ZCOMPL if transaction finished, else 0: can be ymodem.
2004-11-08 12:24:36 +00:00
*/
int tryz(void)
{
2004-11-12 21:25:59 +00:00
int c, n;
int cmdzack1flg;
2004-11-21 11:20:08 +00:00
if (protocol != ZM_ZMODEM)
return 0;
2004-11-23 20:46:21 +00:00
for (n = zmodem_requested ?15:5; --n >= 0; ) {
2004-11-12 21:25:59 +00:00
/*
* Set buffer length (0) and capability flags
*/
Syslog('z', "tryz attempt %d", n);
stohdr(0L);
2004-11-20 13:30:13 +00:00
Txhdr[ZF0] = CANFC32|CANFDX|CANOVIO;
2004-11-12 21:25:59 +00:00
if (Zctlesc)
Txhdr[ZF0] |= TESCCTL;
2004-11-20 13:30:13 +00:00
zshhdr(tryzhdrtype, Txhdr);
2004-11-12 21:25:59 +00:00
if (tryzhdrtype == ZSKIP) /* Don't skip too far */
tryzhdrtype = ZRINIT; /* CAF 8-21-87 */
2004-11-08 12:24:36 +00:00
again:
2004-11-12 21:25:59 +00:00
switch (zgethdr(Rxhdr)) {
2004-11-20 18:31:13 +00:00
case ZRQINIT: continue;
2004-11-12 21:25:59 +00:00
case ZEOF: continue;
2004-11-21 13:43:51 +00:00
case TIMEOUT: Syslog('z', "Zmodem: tryz() timeout attempt %d", n);
2004-11-12 21:25:59 +00:00
continue;
case ZFILE: zconv = Rxhdr[ZF0];
if (!zconv) {
Syslog('z', "*** !zconv %d", zconv);
2004-11-20 13:30:13 +00:00
zconv = ZCBIN;
2004-11-12 21:25:59 +00:00
}
zmanag = Rxhdr[ZF1];
ztrans = Rxhdr[ZF2];
tryzhdrtype = ZRINIT;
c = zrdata(secbuf, MAXBLOCK);
2004-11-20 13:30:13 +00:00
io_mode(0, 3);
2004-11-12 21:25:59 +00:00
if (c == GOTCRCW) {
Syslog('z', "tryz return ZFILE");
2004-11-08 12:24:36 +00:00
return ZFILE;
2004-11-12 21:25:59 +00:00
}
2004-11-20 13:30:13 +00:00
zshhdr(ZNAK, Txhdr);
2004-11-12 21:25:59 +00:00
goto again;
case ZSINIT: /* this once was:
* Zctlesc = TESCCTL & Rxhdr[ZF0];
* trouble: if rz get --escape flag:
* - it sends TESCCTL to sz,
* get a ZSINIT _without_ TESCCTL (yeah - sender didn't know),
* overwrites Zctlesc flag ...
* - sender receives TESCCTL and uses "|=..."
* so: sz escapes, but rz doesn't unescape ... not good.
*/
Zctlesc |= TESCCTL & Rxhdr[ZF0];
if (zrdata(Attn, ZATTNLEN) == GOTCRCW) {
2004-11-08 12:24:36 +00:00
stohdr(1L);
2004-11-20 13:30:13 +00:00
zshhdr(ZACK, Txhdr);
2004-11-08 12:24:36 +00:00
goto again;
2004-11-12 21:25:59 +00:00
}
2004-11-20 13:30:13 +00:00
zshhdr(ZNAK, Txhdr);
2004-11-12 21:25:59 +00:00
goto again;
case ZFREECNT: stohdr(getfree());
2004-11-20 13:30:13 +00:00
zshhdr(ZACK, Txhdr);
2004-11-12 21:25:59 +00:00
goto again;
case ZCOMMAND: cmdzack1flg = Rxhdr[ZF0];
if (zrdata(secbuf, MAXBLOCK) == GOTCRCW) {
2004-11-08 12:24:36 +00:00
if (cmdzack1flg & ZCACK1)
2004-11-12 21:25:59 +00:00
stohdr(0L);
2004-11-08 12:24:36 +00:00
else
2004-11-12 21:25:59 +00:00
Syslog('+', "Zmodem: request for command \"%s\" ignored", printable(secbuf,-32));
2004-11-08 12:24:36 +00:00
stohdr(0L);
do {
2004-11-20 13:30:13 +00:00
zshhdr(ZCOMPL, Txhdr);
2004-11-08 12:24:36 +00:00
} while (++errors<20 && zgethdr(Rxhdr) != ZFIN);
return ackbibi();
2004-11-12 21:25:59 +00:00
}
2004-11-20 13:30:13 +00:00
zshhdr(ZNAK, Txhdr);
2004-11-12 21:25:59 +00:00
goto again;
case ZCOMPL: goto again;
case ZRINIT: Syslog('z', "tryz: got ZRINIT");
return TERROR;
case ZFIN: /* do not beleive in first ZFIN */
ackbibi();
return ZCOMPL;
case TERROR:
case HANGUP:
case ZCAN: return TERROR;
default: continue;
2004-11-08 12:24:36 +00:00
}
2004-11-12 21:25:59 +00:00
}
return 0;
2004-11-08 12:24:36 +00:00
}
/*
* Receive 1 or more files with ZMODEM protocol
*/
int rzfiles(void)
{
2004-11-12 21:25:59 +00:00
int c;
for (;;) {
switch (c = rzfile()) {
case ZEOF:
case ZSKIP:
case ZFERR: switch (tryz()) {
case ZCOMPL: return OK;
default: return TERROR;
case ZFILE: break;
}
continue;
default: return c;
case TERROR: return TERROR;
2004-11-08 12:24:36 +00:00
}
2004-11-12 21:25:59 +00:00
}
/* NOTREACHED */
2004-11-08 12:24:36 +00:00
}
/*
* Receive a file with ZMODEM protocol
* Assumes file name frame is in secbuf
*/
int rzfile(void)
{
2004-11-12 21:25:59 +00:00
int c, n;
2004-11-08 12:24:36 +00:00
2004-11-12 21:25:59 +00:00
Eofseen=FALSE;
rxbytes = 0l;
if ((c = procheader(secbuf))) {
return (tryzhdrtype = c);
}
2004-11-08 12:24:36 +00:00
2004-11-12 21:25:59 +00:00
n = 20;
2004-11-08 12:24:36 +00:00
2004-11-12 21:25:59 +00:00
for (;;) {
Syslog('z', "rxbytes %ld", rxbytes);
stohdr(rxbytes);
2004-11-20 13:30:13 +00:00
zshhdr(ZRPOS, Txhdr);
2004-11-08 12:24:36 +00:00
nxthdr:
2004-11-12 21:25:59 +00:00
switch (c = zgethdr(Rxhdr)) {
default: Syslog('z', "rzfile: Wrong header %d", c);
if ( --n < 0) {
2004-11-08 12:24:36 +00:00
Syslog('+', "Zmodem: wrong header %d", c);
return TERROR;
2004-11-12 21:25:59 +00:00
}
continue;
case ZCAN: Syslog('+', "Zmodem: sender CANcelled");
return TERROR;
case ZNAK: if ( --n < 0) {
2004-11-08 12:24:36 +00:00
Syslog('+', "Zmodem: Got ZNAK");
return TERROR;
2004-11-12 21:25:59 +00:00
}
continue;
case TIMEOUT: if ( --n < 0) {
2004-11-21 13:43:51 +00:00
Syslog('z', "Zmodem: TIMEOUT");
2004-11-08 12:24:36 +00:00
return TERROR;
2004-11-12 21:25:59 +00:00
}
continue;
case ZFILE: zrdata(secbuf, MAXBLOCK);
continue;
case ZEOF: if (rclhdr(Rxhdr) != rxbytes) {
2004-11-08 12:24:36 +00:00
/*
* Ignore eof if it's at wrong place - force
* a timeout because the eof might have gone
* out before we sent our zrpos.
*/
errors = 0;
goto nxthdr;
2004-11-12 21:25:59 +00:00
}
if (closeit(1)) {
2004-11-08 12:24:36 +00:00
tryzhdrtype = ZFERR;
Syslog('+', "Zmodem: error closing file");
return TERROR;
2004-11-12 21:25:59 +00:00
}
fout = NULL;
Syslog('z', "rzfile: normal EOF");
return c;
case HANGUP: Syslog('+', "Zmodem: Lost Carrier");
return TERROR;
case TERROR: /* Too much garbage in header search error */
if (--n < 0) {
2004-11-08 12:24:36 +00:00
Syslog('+', "Zmodem: Too many errors");
return TERROR;
2004-11-12 21:25:59 +00:00
}
zmputs(Attn);
continue;
case ZSKIP: Modtime = 1;
closeit(1);
Syslog('+', "Zmodem: Sender SKIPPED file");
return c;
case ZDATA: if (rclhdr(Rxhdr) != rxbytes) {
2004-11-08 12:24:36 +00:00
if ( --n < 0) {
2004-11-12 21:25:59 +00:00
Syslog('+', "Zmodem: Data has bad address");
return TERROR;
2004-11-08 12:24:36 +00:00
}
2004-11-12 21:25:59 +00:00
zmputs(Attn);
2004-11-08 12:24:36 +00:00
continue;
2004-11-12 21:25:59 +00:00
}
moredata:
2004-11-20 18:31:13 +00:00
Syslog('Z', "%7ld ZMODEM%s ", rxbytes, Crc32r?" CRC-32":"");
2004-11-12 21:25:59 +00:00
Nopper();
2004-11-20 18:31:13 +00:00
alarm_on();
2004-11-12 21:25:59 +00:00
switch (c = zrdata(secbuf, MAXBLOCK)) {
case ZCAN: Syslog('+', "Zmodem: sender CANcelled");
return TERROR;
case HANGUP: Syslog('+', "Zmodem: Lost Carrier");
return TERROR;
case TERROR: /* CRC error */
if (--n < 0) {
Syslog('+', "Zmodem: Too many errors");
return TERROR;
}
zmputs(Attn);
continue;
case TIMEOUT: if ( --n < 0) {
Syslog('+', "Zmodem: TIMEOUT");
return TERROR;
}
continue;
case GOTCRCW: n = 20;
putsec(secbuf, Rxcount);
rxbytes += Rxcount;
stohdr(rxbytes);
PUTCHAR(XON);
2004-11-20 13:30:13 +00:00
zshhdr(ZACK, Txhdr);
2004-11-12 21:25:59 +00:00
goto nxthdr;
case GOTCRCQ: n = 20;
putsec(secbuf, Rxcount);
rxbytes += Rxcount;
stohdr(rxbytes);
2004-11-20 13:30:13 +00:00
zshhdr(ZACK, Txhdr);
2004-11-12 21:25:59 +00:00
goto moredata;
case GOTCRCG: n = 20;
putsec(secbuf, Rxcount);
rxbytes += Rxcount;
goto moredata;
case GOTCRCE: n = 20;
putsec(secbuf, Rxcount);
rxbytes += Rxcount;
goto nxthdr;
}
2004-11-08 12:24:36 +00:00
}
2004-11-12 21:25:59 +00:00
}
2004-11-08 12:24:36 +00:00
}
/*
* Send a string to the modem, processing for \336 (sleep 1 sec)
* and \335 (break signal)
*/
void zmputs(char *s)
{
2004-11-12 21:25:59 +00:00
int c;
Syslog('z', "zmputs: \"%s\"", printable(s, strlen(s)));
while (*s) {
switch (c = *s++) {
case '\336': Syslog('z', "zmputs: sleep(1)");
sleep(1);
continue;
case '\335': Syslog('z', "zmputs: send break");
sendbrk();
continue;
default: PUTCHAR(c);
2004-11-08 12:24:36 +00:00
}
2004-11-12 21:25:59 +00:00
}
2004-11-08 12:24:36 +00:00
}
int closeit(int success)
{
struct utimbuf ut;
int rc;
2004-11-12 21:25:59 +00:00
Syslog('z', "closeit(%d)", success);
if ((fout == NULL) || (curfile == NULL)) {
Syslog('+', "closeit(), nothing to close");
return 1;
}
2004-11-08 12:24:36 +00:00
rc = fclose(fout);
fout = NULL;
if (rc == 0) {
ut.actime = Modtime;
ut.modtime = Modtime;
if ((rc = utime(curfile, &ut)))
WriteError("$utime failed");
}
free(curfile);
curfile = NULL;
sbytes = rxbytes - sbytes;
gettimeofday(&endtime, &tz);
if (success)
2004-11-23 22:35:34 +00:00
Syslog('+', "%s: OK %s", protname(), transfertime(starttime, endtime, sbytes, FALSE));
2004-11-08 12:24:36 +00:00
else
2004-11-23 22:35:34 +00:00
Syslog('+', "%s: dropped after %lu bytes", protname(), sbytes);
2004-11-08 12:24:36 +00:00
rcvdbytes += sbytes;
return rc;
}
/*
* Ack a ZFIN packet, let byegones be byegones
*/
int ackbibi(void)
{
2004-11-12 21:25:59 +00:00
int n;
int c;
Syslog('z', "ackbibi:");
stohdr(0L);
for (n=3; --n>=0; ) {
2004-11-20 13:30:13 +00:00
zshhdr(ZFIN, Txhdr);
2004-11-12 21:25:59 +00:00
switch ((c = GETCHAR(10))) {
case 'O': GETCHAR(1); /* Discard 2nd 'O' */
Syslog('z', "Zmodem: ackbibi complete");
return ZCOMPL;
case TERROR:
case HANGUP: Syslog('z', "Zmodem: ackbibi got %d, ignore",c);
return 0;
case TIMEOUT:
default: Syslog('z', "Zmodem: ackbibi got '%s', continue", printablec(c));
break;
2004-11-08 12:24:36 +00:00
}
2004-11-12 21:25:59 +00:00
}
return ZCOMPL;
2004-11-08 12:24:36 +00:00
}
/*
* Process incoming file information header
*/
int procheader(char *Name)
{
register char *openmode, *p;
static int dummy;
char ctt[32];
Syslog('z', "procheader \"%s\"",printable(Name,0));
/* set default parameters and overrides */
openmode = (char *)"w";
2004-11-12 21:25:59 +00:00
Syslog('z', "zmanag=%d", zmanag);
Syslog('z', "zconv=%d", zconv);
2004-11-08 12:24:36 +00:00
/*
* Process ZMODEM remote file management requests
*/
2004-11-12 21:25:59 +00:00
if (!Thisbinary && zconv == ZCNL) /* Remote ASCII override */
Thisbinary = FALSE;
if (zconv == ZCBIN) /* Remote Binary override */
Thisbinary = TRUE;
2004-11-08 12:24:36 +00:00
if (zmanag == ZMAPND)
openmode = (char *)"a";
2004-11-12 21:25:59 +00:00
Syslog('z', "Thisbinary %s", Thisbinary ?"TRUE":"FALSE");
2004-11-08 12:24:36 +00:00
Bytesleft = DEFBYTL;
Filemode = 0;
Modtime = 0L;
2004-11-12 21:25:59 +00:00
Eofseen = FALSE;
2004-11-08 12:24:36 +00:00
p = Name + 1 + strlen(Name);
2004-11-12 21:25:59 +00:00
// FIXME: Here we must add code that checks the Name for slashes, spaces and other
// illegal characters in the filename.
2004-11-08 12:24:36 +00:00
sscanf(p, "%ld%lo%o%o%d%d%d%d", &Bytesleft, &Modtime, &Filemode, &dummy, &dummy, &dummy, &dummy, &dummy);
strcpy(ctt, rfcdate(Modtime));
2004-11-23 22:35:34 +00:00
Syslog('+', "%s: \"%s\" %ld bytes, %s mode %o", protname(), Name, Bytesleft, ctt, Filemode);
2004-11-08 12:24:36 +00:00
if (curfile)
free(curfile);
curfile = NULL;
curfile = xstrcpy(CFG.bbs_usersdir);
curfile = xstrcat(curfile, (char *)"/");
curfile = xstrcat(curfile, exitinfo.Name);
curfile = xstrcat(curfile, (char *)"/upl/");
curfile = xstrcat(curfile, Name);
2004-11-12 21:25:59 +00:00
Syslog('z', "try open %s mode \"%s\"", curfile, openmode);
if ((fout = fopen(curfile, openmode)) == NULL) {
WriteError("$Can't open %s mode %s", curfile, openmode);
}
2004-11-08 12:24:36 +00:00
gettimeofday(&starttime, &tz);
2004-11-12 21:25:59 +00:00
sbytes = rxbytes = 0;
Syslog('z', "result %s", fout ? "Ok":"Failed");
2004-11-08 12:24:36 +00:00
/* if (Bytesleft == rxbytes) { FIXME: if file already received, use this.
Syslog('+', "Zmodem: Skipping %s", Name);
fout = NULL;
return ZSKIP;
} else */ if (!fout)
return ZFERR;
else
return 0;
}
/*
* Putsec writes the n characters of buf to receive file fout.
* If not in binary mode, carriage returns, and all characters
* starting with CPMEOF are discarded.
*/
int putsec(char *buf, int n)
{
2004-11-12 21:25:59 +00:00
register char *p;
2004-11-08 12:24:36 +00:00
2004-11-12 21:25:59 +00:00
if (n == 0)
2004-11-08 12:24:36 +00:00
return OK;
2004-11-12 21:25:59 +00:00
if (Thisbinary) {
2004-11-20 15:34:07 +00:00
if (fwrite(buf, n, 1, fout) != 1)
return ERROR;
2004-11-12 21:25:59 +00:00
} else {
if (Eofseen)
return OK;
for (p = buf; --n>=0; ++p ) {
if ( *p == '\r')
continue;
if (*p == SUB) {
Eofseen=TRUE;
return OK;
}
putc(*p ,fout);
}
}
return OK;
2004-11-08 12:24:36 +00:00
}
long getfree(void)
{
struct statfs sfs;
char *temp;
temp = calloc(PATH_MAX, sizeof(char));
sprintf(temp, "%s/%s/upl", CFG.bbs_usersdir, exitinfo.Name);
if (statfs(temp, &sfs) != 0) {
WriteError("$cannot statfs \"%s\", assume enough space", temp);
free(temp);
return -1L;
}
free(temp);
return (sfs.f_bsize * sfs.f_bfree);
}