#ifndef lint
static const char copyright[] =
"@(#) Copyright (c) 1983, 1993\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)tftpd.c 8.1 (Berkeley) 6/4/93";
#endif
static const char rcsid[] =
"$FreeBSD: src/libexec/tftpd/tftpd.c,v 1.18 2001/02/02 10:52:58 asmodai Exp $";
#endif
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/tftp.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <pwd.h>
#include <setjmp.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <unistd.h>
#include "tftpsubs.h"
#define TIMEOUT 5
int peer;
int rexmtval = TIMEOUT;
int maxtimeout = 5*TIMEOUT;
#define PKTSIZE SEGSIZE+4
char buf[PKTSIZE];
char ackbuf[PKTSIZE];
struct sockaddr_in from;
int fromlen;
void tftp __P((struct tftphdr *, int));
#define MAXDIRS 20
static struct dirlist {
char *name;
int len;
} dirs[MAXDIRS+1];
static int suppress_naks;
static int logging;
static int ipchroot;
static char *errtomsg __P((int));
static void nak __P((int));
static char * __P(verifyhost(struct sockaddr_in *));
int
main(argc, argv)
int argc;
char *argv[];
{
register struct tftphdr *tp;
register int n;
int ch, on;
struct sockaddr_in sin;
char *chroot_dir = NULL;
struct passwd *nobody;
char *chuser = "nobody";
openlog("tftpd", LOG_PID | LOG_NDELAY, LOG_FTP);
while ((ch = getopt(argc, argv, "cClns:u:")) != -1) {
switch (ch) {
case 'c':
ipchroot = 1;
break;
case 'C':
ipchroot = 2;
break;
case 'l':
logging = 1;
break;
case 'n':
suppress_naks = 1;
break;
case 's':
chroot_dir = optarg;
break;
case 'u':
chuser = optarg;
break;
default:
syslog(LOG_WARNING, "ignoring unknown option -%c", ch);
}
}
if (optind < argc) {
struct dirlist *dirp;
for (dirp = dirs; optind < argc && dirp < &dirs[MAXDIRS];
optind++) {
if (argv[optind][0] == '/') {
dirp->name = argv[optind];
dirp->len = strlen(dirp->name);
dirp++;
}
}
}
else if (chroot_dir) {
dirs->name = "/";
dirs->len = 1;
}
if (ipchroot && chroot_dir == NULL) {
syslog(LOG_ERR, "-c requires -s");
exit(1);
}
on = 1;
if (ioctl(0, FIONBIO, &on) < 0) {
syslog(LOG_ERR, "ioctl(FIONBIO): %m");
exit(1);
}
fromlen = sizeof (from);
n = recvfrom(0, buf, sizeof (buf), 0,
(struct sockaddr *)&from, &fromlen);
if (n < 0) {
syslog(LOG_ERR, "recvfrom: %m");
exit(1);
}
{
int pid;
int i, j;
for (i = 1; i < 20; i++) {
pid = fork();
if (pid < 0) {
sleep(i);
j = sizeof from;
i = recvfrom(0, buf, sizeof (buf), 0,
(struct sockaddr *)&from, &j);
if (i > 0) {
n = i;
fromlen = j;
}
} else {
break;
}
}
if (pid < 0) {
syslog(LOG_ERR, "fork: %m");
exit(1);
} else if (pid != 0) {
exit(0);
}
}
if (chroot_dir) {
if (ipchroot) {
char tempchroot[MAXPATHLEN];
char *tempaddr;
struct stat sb;
int statret;
tempaddr = inet_ntoa(from.sin_addr);
snprintf(tempchroot, sizeof(tempchroot), "%s/%s", chroot_dir, tempaddr);
statret = stat(tempchroot, &sb);
if ((sb.st_mode & S_IFDIR) &&
(statret == 0 || (statret == -1 && ipchroot == 1)))
chroot_dir = tempchroot;
}
if ((nobody = getpwnam(chuser)) == NULL) {
syslog(LOG_ERR, "%s: no such user", chuser);
exit(1);
}
if (chroot(chroot_dir)) {
syslog(LOG_ERR, "chroot: %s: %m", chroot_dir);
exit(1);
}
chdir( "/" );
setuid(nobody->pw_uid);
}
from.sin_family = AF_INET;
alarm(0);
close(0);
close(1);
peer = socket(AF_INET, SOCK_DGRAM, 0);
if (peer < 0) {
syslog(LOG_ERR, "socket: %m");
exit(1);
}
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
if (bind(peer, (struct sockaddr *)&sin, sizeof (sin)) < 0) {
syslog(LOG_ERR, "bind: %m");
exit(1);
}
if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) {
syslog(LOG_ERR, "connect: %m");
exit(1);
}
tp = (struct tftphdr *)buf;
tp->th_opcode = ntohs(tp->th_opcode);
if (tp->th_opcode == RRQ || tp->th_opcode == WRQ)
tftp(tp, n);
exit(1);
}
struct formats;
int validate_access __P((char **, int));
void xmitfile __P((struct formats *));
void recvfile __P((struct formats *));
struct formats {
char *f_mode;
int (*f_validate) __P((char **, int));
void (*f_send) __P((struct formats *));
void (*f_recv) __P((struct formats *));
int f_convert;
} formats[] = {
{ "netascii", validate_access, xmitfile, recvfile, 1 },
{ "octet", validate_access, xmitfile, recvfile, 0 },
#ifdef notdef
{ "mail", validate_user, sendmail, recvmail, 1 },
#endif
{ 0 }
};
void
tftp(tp, size)
struct tftphdr *tp;
int size;
{
register char *cp;
int first = 1, ecode;
register struct formats *pf;
char *filename, *mode;
filename = cp = tp->th_stuff;
again:
while (cp < buf + size) {
if (*cp == '\0')
break;
cp++;
}
if (*cp != '\0') {
nak(EBADOP);
exit(1);
}
if (first) {
mode = ++cp;
first = 0;
goto again;
}
for (cp = mode; *cp; cp++)
if (isupper(*cp))
*cp = tolower(*cp);
for (pf = formats; pf->f_mode; pf++)
if (strcmp(pf->f_mode, mode) == 0)
break;
if (pf->f_mode == 0) {
nak(EBADOP);
exit(1);
}
ecode = (*pf->f_validate)(&filename, tp->th_opcode);
if (logging) {
syslog(LOG_INFO, "%s: %s request for %s: %s", verifyhost(&from),
tp->th_opcode == WRQ ? "write" : "read",
filename, errtomsg(ecode));
}
if (ecode) {
if (suppress_naks && *filename != '/' && ecode == ENOTFOUND)
exit(0);
nak(ecode);
exit(1);
}
if (tp->th_opcode == WRQ)
(*pf->f_recv)(pf);
else
(*pf->f_send)(pf);
exit(0);
}
FILE *file;
int
validate_access(filep, mode)
char **filep;
int mode;
{
struct stat stbuf;
int fd;
struct dirlist *dirp;
static char pathname[MAXPATHLEN];
char *filename = *filep;
if (strstr(filename, "/../"))
return (EACCESS);
if (*filename == '/') {
for (dirp = dirs; dirp->name != NULL; dirp++) {
if (dirp->len == 1 ||
(!strncmp(filename, dirp->name, dirp->len) &&
filename[dirp->len] == '/'))
break;
}
if (dirp->name == NULL && dirp != dirs)
return (EACCESS);
if (stat(filename, &stbuf) < 0)
return (errno == ENOENT ? ENOTFOUND : EACCESS);
if ((stbuf.st_mode & S_IFMT) != S_IFREG)
return (ENOTFOUND);
if (mode == RRQ) {
if ((stbuf.st_mode & S_IROTH) == 0)
return (EACCESS);
} else {
if ((stbuf.st_mode & S_IWOTH) == 0)
return (EACCESS);
}
} else {
int err;
if (!strncmp(filename, "../", 3))
return (EACCESS);
err = ENOTFOUND;
for (dirp = dirs; dirp->name != NULL; dirp++) {
snprintf(pathname, sizeof(pathname), "%s/%s",
dirp->name, filename);
if (stat(pathname, &stbuf) == 0 &&
(stbuf.st_mode & S_IFMT) == S_IFREG) {
if ((stbuf.st_mode & S_IROTH) != 0) {
break;
}
err = EACCESS;
}
}
if (dirp->name == NULL)
return (err);
*filep = filename = pathname;
}
fd = open(filename, mode == RRQ ? O_RDONLY : O_WRONLY|O_TRUNC);
if (fd < 0)
return (errno + 100);
file = fdopen(fd, (mode == RRQ)? "r":"w");
if (file == NULL) {
return errno+100;
}
return (0);
}
int timeout;
jmp_buf timeoutbuf;
void
timer()
{
timeout += rexmtval;
if (timeout >= maxtimeout)
exit(1);
longjmp(timeoutbuf, 1);
}
void
xmitfile(pf)
struct formats *pf;
{
struct tftphdr *dp, *r_init();
register struct tftphdr *ap;
register int size, n;
volatile unsigned short block;
signal(SIGALRM, timer);
dp = r_init();
ap = (struct tftphdr *)ackbuf;
block = 1;
do {
size = readit(file, &dp, pf->f_convert);
if (size < 0) {
nak(errno + 100);
goto abort;
}
dp->th_opcode = htons((u_short)DATA);
dp->th_block = htons((u_short)block);
timeout = 0;
(void)setjmp(timeoutbuf);
send_data:
if (send(peer, dp, size + 4, 0) != size + 4) {
syslog(LOG_ERR, "write: %m");
goto abort;
}
read_ahead(file, pf->f_convert);
for ( ; ; ) {
alarm(rexmtval);
n = recv(peer, ackbuf, sizeof (ackbuf), 0);
alarm(0);
if (n < 0) {
syslog(LOG_ERR, "read: %m");
goto abort;
}
ap->th_opcode = ntohs((u_short)ap->th_opcode);
ap->th_block = ntohs((u_short)ap->th_block);
if (ap->th_opcode == ERROR)
goto abort;
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);
abort:
(void) fclose(file);
}
void
justquit()
{
exit(0);
}
void
recvfile(pf)
struct formats *pf;
{
struct tftphdr *dp, *w_init();
register struct tftphdr *ap;
register int n, size;
volatile unsigned short block;
signal(SIGALRM, timer);
dp = w_init();
ap = (struct tftphdr *)ackbuf;
block = 0;
do {
timeout = 0;
ap->th_opcode = htons((u_short)ACK);
ap->th_block = htons((u_short)block);
block++;
(void) setjmp(timeoutbuf);
send_ack:
if (send(peer, ackbuf, 4, 0) != 4) {
syslog(LOG_ERR, "write: %m");
goto abort;
}
write_behind(file, pf->f_convert);
for ( ; ; ) {
alarm(rexmtval);
n = recv(peer, dp, PKTSIZE, 0);
alarm(0);
if (n < 0) {
syslog(LOG_ERR, "read: %m");
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(file, &dp, 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(file, pf->f_convert);
(void) fclose(file);
ap->th_opcode = htons((u_short)ACK);
ap->th_block = htons((u_short)(block));
(void) send(peer, ackbuf, 4, 0);
signal(SIGALRM, justquit);
alarm(rexmtval);
n = recv(peer, buf, sizeof (buf), 0);
alarm(0);
if (n >= 4 &&
dp->th_opcode == DATA &&
block == dp->th_block) {
(void) send(peer, ackbuf, 4, 0);
}
abort:
return;
}
struct errmsg {
int e_code;
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 char *
errtomsg(error)
int error;
{
static char buf[20];
register struct errmsg *pe;
if (error == 0)
return "success";
for (pe = errmsgs; pe->e_code >= 0; pe++)
if (pe->e_code == error)
return pe->e_msg;
snprintf(buf, sizeof(buf), "error %d", error);
return buf;
}
static void
nak(error)
int error;
{
register struct tftphdr *tp;
int length;
register 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 = strlen(pe->e_msg);
tp->th_msg[length] = '\0';
length += 5;
if (send(peer, buf, length, 0) != length)
syslog(LOG_ERR, "nak: %m");
}
static char *
verifyhost(fromp)
struct sockaddr_in *fromp;
{
struct hostent *hp;
hp = gethostbyaddr((char *)&fromp->sin_addr, sizeof(fromp->sin_addr),
fromp->sin_family);
if(hp)
return hp->h_name;
else
return inet_ntoa(fromp->sin_addr);
}