#include "setup.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <signal.h>
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#ifdef HAVE_ARPA_TFTP_H
#include <arpa/tftp.h>
#else
#include "tftp.h"
#endif
#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#include <setjmp.h>
#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
#define ENABLE_CURLX_PRINTF
#include "curlx.h"
#include "getpart.h"
#include "util.h"
#include "memdebug.h"
struct testcase {
char *buffer;
size_t bufsize;
char *rptr;
size_t rcount;
long num;
int ofile;
FILE *server;
};
static int synchnet(curl_socket_t);
static struct tftphdr *r_init(void);
static struct tftphdr *w_init(void);
static int readit(struct testcase *test, struct tftphdr **dpp, int convert);
static int writeit(struct testcase *test, struct tftphdr **dpp, int ct,
int convert);
static void mysignal(int, void (*func)(int));
#define TIMEOUT 5
#define PKTSIZE SEGSIZE+4
struct formats;
static int tftp(struct testcase *test, struct tftphdr *tp, int size);
static void nak(int error);
static void sendtftp(struct testcase *test, struct formats *pf);
static void recvtftp(struct testcase *test, struct formats *pf);
static int validate_access(struct testcase *test, const char *, int);
static curl_socket_t peer;
static int rexmtval = TIMEOUT;
static int maxtimeout = 5*TIMEOUT;
static char buf[PKTSIZE];
static char ackbuf[PKTSIZE];
static struct sockaddr_in from;
static socklen_t fromlen;
struct bf {
int counter;
char buf[PKTSIZE];
} bfs[2];
#define BF_ALLOC -3
#define BF_FREE -2
static int nextone;
static int current;
int newline = 0;
int prevchar = -1;
static void read_ahead(struct testcase *test,
int convert );
static ssize_t write_behind(struct testcase *test, int convert);
static struct tftphdr *rw_init(int);
static struct tftphdr *w_init(void) { return rw_init(0); }
static struct tftphdr *r_init(void) { return rw_init(1); }
static struct tftphdr *
rw_init(int x)
{
newline = 0;
prevchar = -1;
bfs[0].counter = BF_ALLOC;
current = 0;
bfs[1].counter = BF_FREE;
nextone = x;
return (struct tftphdr *)bfs[0].buf;
}
static int readit(struct testcase *test, struct tftphdr **dpp,
int convert )
{
struct bf *b;
bfs[current].counter = BF_FREE;
current = !current;
b = &bfs[current];
if (b->counter == BF_FREE)
read_ahead(test, convert);
*dpp = (struct tftphdr *)b->buf;
return b->counter;
}
#undef MIN
#define MIN(x,y) ((x)<(y)?(x):(y));
static void read_ahead(struct testcase *test,
int convert )
{
int i;
char *p;
int c;
struct bf *b;
struct tftphdr *dp;
b = &bfs[nextone];
if (b->counter != BF_FREE)
return;
nextone = !nextone;
dp = (struct tftphdr *)b->buf;
if (convert == 0) {
size_t copy_n = MIN(SEGSIZE, test->rcount);
memcpy(dp->th_data, test->rptr, copy_n);
test->rcount -= copy_n;
test->rptr += copy_n;
b->counter = (int)copy_n;
return;
}
p = dp->th_data;
for (i = 0 ; i < SEGSIZE; i++) {
if (newline) {
if (prevchar == '\n')
c = '\n';
else
c = '\0';
newline = 0;
}
else {
if(test->rcount) {
c=test->rptr[0];
test->rptr++;
test->rcount--;
}
else
break;
if (c == '\n' || c == '\r') {
prevchar = c;
c = '\r';
newline = 1;
}
}
*p++ = (char)c;
}
b->counter = (int)(p - dp->th_data);
}
static int writeit(struct testcase *test, struct tftphdr **dpp,
int ct, int convert)
{
bfs[current].counter = ct;
current = !current;
if (bfs[current].counter != BF_FREE)
write_behind(test, convert);
bfs[current].counter = BF_ALLOC;
*dpp = (struct tftphdr *)bfs[current].buf;
return ct;
}
static ssize_t write_behind(struct testcase *test, int convert)
{
char *buf;
int count;
int ct;
char *p;
int c;
struct bf *b;
struct tftphdr *dp;
b = &bfs[nextone];
if (b->counter < -1)
return 0;
if(!test->ofile) {
char outfile[256];
snprintf(outfile, sizeof(outfile), "log/upload.%ld", test->num);
test->ofile=open(outfile, O_CREAT|O_RDWR, 0777);
if(test->ofile == -1) {
logmsg("Couldn't create and/or open file %s for upload!", outfile);
return -1;
}
}
count = b->counter;
b->counter = BF_FREE;
dp = (struct tftphdr *)b->buf;
nextone = !nextone;
buf = dp->th_data;
if (count <= 0)
return -1;
if (convert == 0)
return write(test->ofile, buf, count);
p = buf;
ct = count;
while (ct--) {
c = *p++;
if (prevchar == '\r') {
if (c == '\n')
lseek(test->ofile, -1, SEEK_CUR);
else
if (c == '\0')
goto skipit;
}
write(test->ofile, &c, 1);
skipit:
prevchar = c;
}
return count;
}
static int synchnet(curl_socket_t f )
{
#if defined(HAVE_IOCTLSOCKET)
unsigned long i;
#else
int i;
#endif
int j = 0;
char rbuf[PKTSIZE];
struct sockaddr_in from;
socklen_t fromlen;
while (1) {
#if defined(HAVE_IOCTLSOCKET)
(void) ioctlsocket(f, FIONREAD, &i);
#else
(void) ioctl(f, FIONREAD, &i);
#endif
if (i) {
j++;
fromlen = sizeof from;
(void) recvfrom(f, rbuf, sizeof (rbuf), 0,
(struct sockaddr *)&from, &fromlen);
}
else
break;
}
return j;
}
#if defined(HAVE_ALARM) && defined(SIGALRM)
static void mysignal(int sig, void (*handler)(int))
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handler;
sigaction(sig, &sa, NULL);
}
#endif
#ifndef DEFAULT_LOGFILE
#define DEFAULT_LOGFILE "log/tftpd.log"
#endif
#define DEFAULT_PORT 8999
const char *serverlogfile = DEFAULT_LOGFILE;
#define REQUEST_DUMP "log/server.input"
char use_ipv6=FALSE;
int main(int argc, char **argv)
{
struct sockaddr_in me;
#ifdef ENABLE_IPV6
struct sockaddr_in6 me6;
#endif
struct tftphdr *tp;
int n = 0;
int arg = 1;
FILE *pidfile;
char *pidname= (char *)".tftpd.pid";
unsigned short port = DEFAULT_PORT;
curl_socket_t sock;
int flag;
int rc;
int error;
struct testcase test;
while(argc>arg) {
if(!strcmp("--version", argv[arg])) {
printf("tftpd IPv4%s\n",
#ifdef ENABLE_IPV6
"/IPv6"
#else
""
#endif
);
return 0;
}
else if(!strcmp("--pidfile", argv[arg])) {
arg++;
if(argc>arg)
pidname = argv[arg++];
}
else if(!strcmp("--ipv6", argv[arg])) {
#ifdef ENABLE_IPV6
use_ipv6=TRUE;
#endif
arg++;
}
else if(argc>arg) {
if(atoi(argv[arg]))
port = (unsigned short)atoi(argv[arg++]);
if(argc>arg)
path = argv[arg++];
}
}
#ifdef WIN32
win32_init();
atexit(win32_cleanup);
#endif
#ifdef ENABLE_IPV6
if(!use_ipv6)
#endif
sock = socket(AF_INET, SOCK_DGRAM, 0);
#ifdef ENABLE_IPV6
else
sock = socket(AF_INET6, SOCK_DGRAM, 0);
#endif
if (sock < 0) {
perror("opening stream socket");
logmsg("Error opening socket");
return 1;
}
flag = 1;
if (setsockopt
(sock, SOL_SOCKET, SO_REUSEADDR, (const void *) &flag,
sizeof(int)) < 0) {
perror("setsockopt(SO_REUSEADDR)");
}
#ifdef ENABLE_IPV6
if(!use_ipv6) {
#endif
me.sin_family = AF_INET;
me.sin_addr.s_addr = INADDR_ANY;
me.sin_port = htons(port);
rc = bind(sock, (struct sockaddr *) &me, sizeof(me));
#ifdef ENABLE_IPV6
}
else {
memset(&me6, 0, sizeof(struct sockaddr_in6));
me6.sin6_family = AF_INET6;
me6.sin6_addr = in6addr_any;
me6.sin6_port = htons(port);
rc = bind(sock, (struct sockaddr *) &me6, sizeof(me6));
}
#endif
if(rc < 0) {
perror("binding stream socket");
logmsg("Error binding socket");
return 1;
}
pidfile = fopen(pidname, "w");
if(pidfile) {
fprintf(pidfile, "%d\n", (int)getpid());
fclose(pidfile);
}
else {
error = ERRNO;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", pidname);
logmsg("Couldn't write pid file");
}
logmsg("Running IPv%d version on port UDP/%d",
#ifdef ENABLE_IPV6
(use_ipv6?6:4)
#else
4
#endif
, port );
do {
FILE *server;
fromlen = sizeof(from);
n = recvfrom(sock, buf, sizeof (buf), 0,
(struct sockaddr *)&from, &fromlen);
if (n < 0) {
logmsg("recvfrom:\n");
return 3;
}
from.sin_family = AF_INET;
peer = socket(AF_INET, SOCK_DGRAM, 0);
if (peer < 0) {
logmsg("socket:\n");
return 2;
}
if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
logmsg("connect: fail\n");
return 1;
}
maxtimeout = 5*TIMEOUT;
tp = (struct tftphdr *)buf;
tp->th_opcode = ntohs(tp->th_opcode);
if (tp->th_opcode == RRQ || tp->th_opcode == WRQ) {
memset(&test, 0, sizeof(test));
server = fopen(REQUEST_DUMP, "ab");
if(!server) {
error = ERRNO;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", REQUEST_DUMP);
break;
}
test.server = server;
tftp(&test, tp, n);
if(test.buffer)
free(test.buffer);
}
fclose(server);
sclose(peer);
} while(1);
return 0;
}
struct formats {
const char *f_mode;
int f_convert;
} formats[] = {
{ "netascii", 1 },
{ "octet", 0 },
{ NULL, 0 }
};
static int tftp(struct testcase *test, struct tftphdr *tp, int size)
{
char *cp;
int first = 1, ecode;
struct formats *pf;
char *filename, *mode = NULL;
fprintf(test->server, "opcode: %x\n", tp->th_opcode);
cp = (char *)&tp->th_stuff;
filename = cp;
again:
while (cp < buf + size) {
if (*cp == '\0')
break;
cp++;
}
if (*cp) {
nak(EBADOP);
return 3;
}
if (first) {
mode = ++cp;
first = 0;
goto again;
}
fprintf(test->server, "filename: %s\n", filename);
for (cp = mode; *cp; cp++)
if (isupper((int)*cp))
*cp = (char)tolower((int)*cp);
fprintf(test->server, "mode: %s\n", mode);
fflush(test->server);
for (pf = formats; pf->f_mode; pf++)
if (strcmp(pf->f_mode, mode) == 0)
break;
if (!pf->f_mode) {
nak(EBADOP);
return 2;
}
ecode = validate_access(test, filename, tp->th_opcode);
if (ecode) {
nak(ecode);
return 1;
}
if (tp->th_opcode == WRQ)
recvtftp(test, pf);
else
sendtftp(test, pf);
return 0;
}
static int validate_access(struct testcase *test,
const char *filename, int mode)
{
char *ptr;
long testno;
int error;
logmsg("trying to get file: %s mode %x", filename, mode);
if(!strncmp("verifiedserver", filename, 15)) {
char weare[128];
size_t count = sprintf(weare, "WE ROOLZ: %d\r\n", (int)getpid());
logmsg("Are-we-friendly question received");
test->buffer = strdup(weare);
test->rptr = test->buffer;
test->bufsize = count;
test->rcount = count;
return 0;
}
ptr = strrchr(filename, '/');
if(ptr) {
char *file;
ptr++;
while(*ptr && !ISDIGIT(*ptr))
ptr++;
testno = strtol(ptr, &ptr, 10);
logmsg("requested test number %d", testno);
test->num = testno;
file = test2file(testno);
if(file) {
FILE *stream=fopen(file, "rb");
if(!stream) {
error = ERRNO;
logmsg("fopen() failed with error: %d %s", error, strerror(error));
logmsg("Error opening file: %s", file);
logmsg("Couldn't open test file: %s", file);
return EACCESS;
}
else {
size_t count;
test->buffer = (char *)spitout(stream, "reply", "data", &count);
fclose(stream);
if(test->buffer) {
test->rptr = test->buffer;
test->bufsize = count;
test->rcount = count;
}
else
return EACCESS;
}
}
else
return EACCESS;
}
else {
logmsg("no slash found in path");
return EACCESS;
}
return 0;
}
int timeout;
#ifdef HAVE_SIGSETJMP
sigjmp_buf timeoutbuf;
#endif
static void timer(int signum)
{
(void)signum;
logmsg("alarm!");
timeout += rexmtval;
if (timeout >= maxtimeout)
exit(1);
#ifdef HAVE_SIGSETJMP
siglongjmp(timeoutbuf, 1);
#endif
}
static void sendtftp(struct testcase *test, struct formats *pf)
{
struct tftphdr *dp;
struct tftphdr *ap;
unsigned short block = 1;
int size;
ssize_t n;
#if defined(HAVE_ALARM) && defined(SIGALRM)
mysignal(SIGALRM, timer);
#endif
dp = r_init();
ap = (struct tftphdr *)ackbuf;
do {
size = readit(test, &dp, pf->f_convert);
if (size < 0) {
nak(ERRNO + 100);
return;
}
dp->th_opcode = htons((u_short)DATA);
dp->th_block = htons((u_short)block);
timeout = 0;
#ifdef HAVE_SIGSETJMP
(void) sigsetjmp(timeoutbuf, 1);
#endif
send_data:
if (swrite(peer, dp, size + 4) != size + 4) {
logmsg("write\n");
return;
}
read_ahead(test, pf->f_convert);
for ( ; ; ) {
#ifdef HAVE_ALARM
alarm(rexmtval);
#endif
n = sread(peer, ackbuf, sizeof (ackbuf));
#ifdef HAVE_ALARM
alarm(0);
#endif
if (n < 0) {
logmsg("read: fail\n");
return;
}
ap->th_opcode = ntohs((u_short)ap->th_opcode);
ap->th_block = ntohs((u_short)ap->th_block);
if (ap->th_opcode == ERROR) {
logmsg("got ERROR");
return;
}
if (ap->th_opcode == ACK) {
if (ap->th_block == block) {
break;
}
(void) synchnet(peer);
if (ap->th_block == (block -1)) {
goto send_data;
}
}
}
block++;
} while (size == SEGSIZE);
}
static void justtimeout(int signum)
{
(void)signum;
}
static void recvtftp(struct testcase *test, struct formats *pf)
{
struct tftphdr *dp;
struct tftphdr *ap;
unsigned short block = 0;
ssize_t n, size;
#if defined(HAVE_ALARM) && defined(SIGALRM)
mysignal(SIGALRM, timer);
#endif
dp = w_init();
ap = (struct tftphdr *)ackbuf;
do {
timeout = 0;
ap->th_opcode = htons((u_short)ACK);
ap->th_block = htons((u_short)block);
block++;
#ifdef HAVE_SIGSETJMP
(void) sigsetjmp(timeoutbuf, 1);
#endif
send_ack:
if (swrite(peer, ackbuf, 4) != 4) {
logmsg("write: fail\n");
goto abort;
}
write_behind(test, pf->f_convert);
for ( ; ; ) {
#ifdef HAVE_ALARM
alarm(rexmtval);
#endif
n = sread(peer, dp, PKTSIZE);
#ifdef HAVE_ALARM
alarm(0);
#endif
if (n < 0) {
logmsg("read: fail\n");
goto abort;
}
dp->th_opcode = ntohs((u_short)dp->th_opcode);
dp->th_block = ntohs((u_short)dp->th_block);
if (dp->th_opcode == ERROR)
goto abort;
if (dp->th_opcode == DATA) {
if (dp->th_block == block) {
break;
}
(void) synchnet(peer);
if (dp->th_block == (block-1))
goto send_ack;
}
}
size = writeit(test, &dp, (int)(n - 4), pf->f_convert);
if (size != (n-4)) {
if (size < 0)
nak(ERRNO + 100);
else
nak(ENOSPACE);
goto abort;
}
} while (size == SEGSIZE);
write_behind(test, pf->f_convert);
ap->th_opcode = htons((u_short)ACK);
ap->th_block = htons((u_short)(block));
(void) swrite(peer, ackbuf, 4);
#if defined(HAVE_ALARM) && defined(SIGALRM)
mysignal(SIGALRM, justtimeout);
alarm(rexmtval);
#endif
n = sread(peer, buf, sizeof(buf));
#ifdef HAVE_ALARM
alarm(0);
#endif
if (n >= 4 &&
dp->th_opcode == DATA &&
block == dp->th_block) {
(void) swrite(peer, ackbuf, 4);
}
abort:
return;
}
struct errmsg {
int e_code;
const char *e_msg;
} errmsgs[] = {
{ EUNDEF, "Undefined error code" },
{ ENOTFOUND, "File not found" },
{ EACCESS, "Access violation" },
{ ENOSPACE, "Disk full or allocation exceeded" },
{ EBADOP, "Illegal TFTP operation" },
{ EBADID, "Unknown transfer ID" },
{ EEXISTS, "File already exists" },
{ ENOUSER, "No such user" },
{ -1, 0 }
};
static void nak(int error)
{
struct tftphdr *tp;
int length;
struct errmsg *pe;
tp = (struct tftphdr *)buf;
tp->th_opcode = htons((u_short)ERROR);
tp->th_code = htons((u_short)error);
for (pe = errmsgs; pe->e_code >= 0; pe++)
if (pe->e_code == error)
break;
if (pe->e_code < 0) {
pe->e_msg = strerror(error - 100);
tp->th_code = EUNDEF;
}
strcpy(tp->th_msg, pe->e_msg);
length = (int)strlen(pe->e_msg);
tp->th_msg[length] = '\0';
length += 5;
if (swrite(peer, buf, length) != length)
logmsg("nak: fail\n");
}