home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
UUCP_Blars.lzh
/
dcpxfer.c
< prev
next >
Wrap
Text File
|
1991-11-02
|
11KB
|
517 lines
/*
* dcpxfer.c
*
* Revised edition of dcp
*
* Stuart Lynne May/87
*
* Copyright (c) Richard H. Lamb 1985, 1986, 1987
* Changes Copyright (c) Stuart Lynne 1987
* Ported to OS-9/68000: Wolfgang Ocker January 1988
* further fix by Bob Larson
*/
/*
* "DCP" a uucp clone. Copyright Richard H. Lamb 1985,1986,1987
* file send routines
*/
#include "dcp.h"
#include "uucp.h"
char *mfgets();
static unsigned char rpacket[MAXPACK];
static unsigned char spacket[MAXPACK];
static time_t starttime, endtime;
static char id[40];
extern char s_publicdir[];
static char send_delete = 0;
/*
* s d a t a
* Send File Data
*/
sdata()
{
for(;;) {
if ((size = bufill(spacket)) == 0)
return seof();
if ((*sendpkt) (spacket, size, 0))
return (0);
}
}
/*
* b u f i l l
*
* Get a bufferful of data from the file that's being sent.
* Only control-quoting is done; 8-bit & repeat count prefixes are
* not handled.
*/
bufill(buffer)
char *buffer; /* Buffer */
{
int i, len;
len = read(fp, buffer, pktsize); /* Handle partial buffer */
return len;
}
/*
* s b r e a k
*
* Send Break (EOT)
*/
sbreak()
{
int len, i;
sprintf(spacket, "H");
if ((*sendpkt) (spacket, 0, 1))
return (0);
if ((*getpkt) (spacket, &len))
return (0);
printmsg(2, "Switch modes");
if (spacket[1] == 'N')
return ('G');
return ('Y');
}
/*
* s e o f
*
* Send End-Of-File.
*/
seof()
{
int len, i, size;
int Tick;
short Day;
unsigned char Date[4], Time[4];
if ((*sendpkt) (spacket, 0, 0))
return (0);
if ((*getpkt) (spacket, &len))
return (0); /* rec CY or CN */
if (strncmp(spacket, "CY", 2))
return (0); /* cant send file */
size = _gs_size(fp);
time(&endtime); /* Ende-Zeit */
close(fp);
fp = (-1);
if (send_delete) {
if (unlink(fromfile) < 0) {
printmsg(3, "delete of \"%s\" failed error %d", fromfile, errno);
} else {
printmsg(3, "delete of \"%s\" done", fromfile);
}
send_delete = 0;
}
printmsg(0, "Transfer of %s (%s) completed", fromfile, hostfile);
_sysdate(0, Time, Date, &Day, &Tick); /* Zeit und Datum holen */
fprintf(syslog, "%s!%s (%02d/%02d-%02d:%02d:%02d) -> %ld / %ld secs (transmit)\n",
rmtname, id, Date[2], Date[3], Time[1], Time[2], Time[3],
size, endtime - starttime);
return 'F'; /* go get the next file to send */
}
#define PKTSIZE 64
/*
* s f i l e
*
* Send File Header.
*/
sfile()
{
int i, len;
char *cp;
char line[132];
char *flds[10];
if (fp != -1) {
printmsg(0, "sfile: file already open?");
return ('A'); /* If somethings already open. were in trouble */
}
printmsg(3, "sfile: looking for next file...");
/*
* read the next line from the presently open workfile
* (cfile) and determines from this the next file to be sent
* (file) or requested.
*/
if (mfgets(line, 132, fw) == NULL) {
fclose(fw);
if (unlink(cfile) < 0) /* close and delete completed workfile */
printmsg(0, "unlink work file \"%s\" failed error %d", cfile, errno);
else printmsg(2, "work file \"%s\" unlinked", cfile);
fw = NULL;
return ('B'); /* end sending session */
}
strcpy(hostfile, line);
printmsg(7, " getfile: hostfile=\"%s\".", hostfile);
i = dgetargs(line, flds);
if (i < 3) {
printmsg(0, "Bad line \"%s\" in work file \"%s\"", hostfile, cfile);
fclose(fw);
fw = NULL;
return 'B';
}
strcpy(fromfile, flds[1]);
strcpy(tofile, flds[2]);
if (i >= 4) strcpy(id, flds[3]);
else *id = '\0';
printmsg(3, " getfile: fromfile=%s, tofile=%s, id=%s", fromfile, tofile, id);
switch (flds[0][0]) {
case 'S': /* send file */
if (*fromfile != '/') sprintf(fromfile, "%s/%s", rmtname, flds[1]);
send_delete = (i >= 5) && index(flds[4], 'c') == NULL;
printmsg(3, "sfile: Opening %s (%s) for sending.", fromfile, hostfile);
fp = open(fromfile, S_IREAD); /* open the file to be sent */
if (fp == -1) { /* If bad file pointer, give up */
printmsg(0, "sfile: Cannot open file %s (%s).", fromfile, hostfile);
fclose(fw);
fw = NULL;
return 'B'; /* We want to continue anyway */
}
time(&starttime);
printmsg(0, "sfile: Sending %s (%s) as %s", fromfile, hostfile, tofile);
strcpy(spacket, hostfile);
for (len = strlen(spacket), cp = (char *) spacket;
len >= PKTSIZE; len -= PKTSIZE, cp += PKTSIZE)
if ((*sendpkt) (cp, PKTSIZE, 1))
return 0; /* send S fromfile tofile */
if ((*sendpkt) (cp, 0, 1))
return 0; /* send S fromfile tofile */
if ((*getpkt) (spacket, &len))
return 0; /* user - tofile 0666. */
if (spacket[1] != 'Y')
return 'A'; /* If otherside says no-quit */
return 'D';
case 'R': /* request file */
printmsg(3, "sfile: Opening %s for request.", tofile);
if(openrec(fromfile, tofile) != 'Y') return 'A';
time(&starttime);
printmsg(0, "sfile: Requesting %s (%s) as %s", fromfile, hostfile, tofile);
strcpy(spacket, hostfile);
/* send R fromfile tofile user */
for (len = strlen(spacket), cp = (char *)spacket;
len >= PKTSIZE; len -= PKTSIZE, cp += PKTSIZE)
if((*sendpkt)(cp, PKTSIZE, 1))
return 0;
if ((*sendpkt) (cp, 0, 1))
return 0;
if((*getpkt) (spacket, &len))
return 0;
if (spacket[1] != 'Y') {
close(fp);
fp = -1;
return 'A';
}
return 'R';
case '\0':
case '#': /* comment */
return 'F';
default: /* Defective control file */
printmsg(0, "sfile: Unknown request \"%s\" in work file \"%s\"",
hostfile, cfile);
fclose(fw);
fw = NULL;
return 'B';
}
}
/*
* s i n i t
*
* Send Initiate: send this host's parameters and get other side's back.
*/
sinit()
{
if ((*openpk) ())
return ('A');
return ('B');
}
/*
* SUB SUB PROTOCOL
*
* schkdir
* scan the dir
*/
schkdir()
{
char c;
c = scandir(TRUE);
if (c == 'Q')
return ('Y');
if (c == 'S') {
sprintf(rpacket, "HN");
if ((*sendpkt) (rpacket, 0, 1))
return (0);
}
return ('B');
}
/*
* endp() end protocol
*/
endp()
{
sprintf(rpacket, "HY");
(*sendpkt) (rpacket, 0, 2); /* dont wait for ACK */
(*closepk) ();
return ('P');
}
/*
* RECIEVE PROTOCOL
*
* r d a t a
*
* Receive Data
*/
rdata()
{
int i, len;
int Tick;
short Day;
unsigned char Date[4], Time[4];
if ((*getpkt) (rpacket, &len))
return (0);
if (len == 0) {
size = _gs_size(fp);
time(&endtime); /* Ende-Zeit */
close(fp);
fp = -1;
sprintf(rpacket, "CY");
if ((*sendpkt) (rpacket, 0, 1))
return (0);
_sysdate(0, Time, Date, &Day, &Tick); /* Zeit und Datum holen */
fprintf(syslog, "%s!%s (%02d/%02d-%02d:%02d:%02d) <- %ld / %ld secs (receive)\n",
rmtname, id, Date[2], Date[3], Time[1], Time[2], Time[3],
size, endtime - starttime);
printmsg(0, "transfer complete");
return ('F');
}
write(fp, rpacket, len); /* Write the data to the file */
return 'R'; /* Remain in data state */
}
/*
* r f i l e
*
* Receive File Header
*/
rfile()
{
char buf[256];
char *flds[10];
int numflds;
int len, i;
char *cp;
printmsg(3, "rfile entered");
cp = buf;
while (TRUE) {
if ((*getpkt) (rpacket, &len))
return (0);
strncpy(cp, rpacket, len);
cp += len;
if (*(cp - 1) == '\0')
break;
}
if ((buf[0] & 0x7f) == 'H')
return ('C');
printmsg(3, "rfile: buf %d \"%s\"", len, buf);
numflds = dgetargs(buf, flds);
switch(flds[0][0]) {
case 'S': /* remote system sending file */
printmsg(3, "rfile: receive file \"%s\"", flds[2]);
if(openrec(flds[1], flds[2]) != 'Y') return 'A';
printmsg(0, "Receiving %s as %s", flds[1], tofile);
time(&starttime);
strcpy(id, flds[3]);
strcpy(rpacket, "SY");
if ((*sendpkt) (rpacket, 0, 1))
return 0;
return 'R'; /* Switch to receive data state */
case 'R': /* remote system requesting file */
send_delete = 0;
printmsg(3, "rfile: send file \"%s\"", flds[1]);
/* There should be some kind of a permissions file to restrict what
* can be requested. Currently allow only files in uucppublic.
* Specifying a directory also needs to be fixed.
*/
if(flds[1][0]=='~' && flds[1][1]=='/') {
strcpy(fromfile, s_publicdir);
strcat(fromfile, flds[1]+1);
fp = open(fromfile, S_IREAD);
if (fp < 0) {
printmsg(3, "rfile: error %d opening \"%s\"", errno, fromfile);
strcpy(rpacket, "RN2");
} else {
strcpy(rpacket, "RY");
}
} else { /* don't allow getting other files */
printmsg(3, "rfile: not allowed");
strcpy(rpacket, "RN2");
}
time(&starttime);
if ((*sendpkt) (rpacket, 0, 1))
return 0;
if (rpacket[1] != 'Y') return 'B';
return 'D';
default:
printmsg(0, "rfile: got unknown request '%c'", flds[0][0]);
return 'A';
}
}
openrec(ffile, tfile)
char *ffile, *tfile;
{
register char *cp;
char tmpfilename[256]; /* Holds the converted file name */
/* check for ~/ destination -> /usr/spool/uucppublic */
if (strncmp(tfile, "~/", 2) == SAME)
sprintf(tmpfilename, "%s%s", s_publicdir, tfile + 1);
else if (index(cp, '/') != NULL) {
printmsg(0, "rfile: illegal filename \"%s\"", tfile);
sprintf(tmpfilename, "%s/illegalXXXXXX", s_publicdir);
mktemp(tmpfilename);
printmsg(0, "rfile: received as \"%s\"", tmpfilename);
} else
strcpy(tmpfilename, tfile);
makedirectories(tmpfilename);
/* check for dirname only */
cp = tmpfilename + strlen(tmpfilename) - 1;
if (*cp == '\n' || *cp == '\l')
*cp-- = '\0';
printmsg(3, "openrec: receive file \"%s\"", tmpfilename);
/* let host munge filename as appropriate */
strcpy(tofile, tmpfilename);
/* Try to open a new file */
if ((fp = create(tofile, S_IWRITE, S_IREAD + S_IWRITE)) == -1) {
if (errno == E_CEF) { /* Must be a trunctated, old file */
cp = rindex(tofile, '/');
if(cp) cp++;
else cp = tofile;
sprintf(tmpfilename, "%s/.ERROR/%sXXXXXX", SPOOLDIR, cp);
mktemp(tmpfilename);
printmsg(0, "moving %s to %s", tofile, tmpfilename);
if (rename(tofile, tmpfilename) < 0) {
printmsg(0, "cannot rename %s", tofile);
return 'A';
}
if ((fp = create(tofile, S_IWRITE, S_IREAD + S_IWRITE)) == -1) {
printmsg(0, "cannot create %s", tofile); /* Give up if can't */
return 'A';
}
} else {
printmsg(0, "cannot create %s", tofile); /* Give up if can't */
return 'A';
}
}
return 'Y';
}
/*
* r i n i t
*
* Receive Initialization
*/
rinit()
{
if ((*openpk) ())
return (0);
return ('F');
}
makedirectories(pathname)
char *pathname;
{
register char *cp;
char pn[250];
strcpy(pn, pathname);
cp = pn;
while ((cp = index(cp, '/')) != NULL) {
*cp = '\0';
makdir(pn, S_IREAD + S_IWRITE);
*cp++ = '/';
}
}