* Copyright (c) 1983 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
"@(#) Copyright (c) 1983 Regents of the University of California.\n\
static char sccsid
[] = "@(#)tftpd.c 5.4 (Berkeley) %G%";
* Trivial file transfer protocol server.
* This version includes many modifications by Jim Guyton <guyton@rand-unix>
struct sockaddr_in sin
= { AF_INET
};
int maxtimeout
= 5*TIMEOUT
;
#define PKTSIZE SEGSIZE+4
register struct tftphdr
*tp
;
openlog("tftpd", LOG_PID
, LOG_DAEMON
);
n
= recvfrom(0, buf
, sizeof (buf
), 0,
(caddr_t
)&from
, &fromlen
);
perror("tftpd: recvfrom");
from
.sin_family
= AF_INET
;
peer
= socket(AF_INET
, SOCK_DGRAM
, 0);
syslog(LOG_ERR
, "socket: %m");
if (bind(peer
, (caddr_t
)&sin
, sizeof (sin
)) < 0) {
syslog(LOG_ERR
, "bind: %m");
if (connect(peer
, (caddr_t
)&from
, sizeof(from
)) < 0) {
syslog(LOG_ERR
, "connect: %m");
tp
= (struct tftphdr
*)buf
;
tp
->th_opcode
= ntohs(tp
->th_opcode
);
if (tp
->th_opcode
== RRQ
|| tp
->th_opcode
== WRQ
)
int sendfile(), recvfile();
{ "netascii", validate_access
, sendfile
, recvfile
, 1 },
{ "octet", validate_access
, sendfile
, recvfile
, 0 },
{ "mail", validate_user
, sendmail
, recvmail
, 1 },
* Handle initial connection protocol.
register struct formats
*pf
;
filename
= cp
= tp
->th_stuff
;
while (cp
< buf
+ size
) {
for (cp
= mode
; *cp
; cp
++)
for (pf
= formats
; pf
->f_mode
; pf
++)
if (strcmp(pf
->f_mode
, mode
) == 0)
ecode
= (*pf
->f_validate
)(filename
, tp
->th_opcode
);
if (tp
->th_opcode
== WRQ
)
* Validate file access. Since we
* have no uid or gid, for now require
* file to exist and be publicly
* Note also, full path name must be
* given as we have no login directory.
validate_access(filename
, mode
)
if (stat(filename
, &stbuf
) < 0)
return (errno
== ENOENT
? ENOTFOUND
: EACCESS
);
if ((stbuf
.st_mode
&(S_IREAD
>> 6)) == 0)
if ((stbuf
.st_mode
&(S_IWRITE
>> 6)) == 0)
fd
= open(filename
, mode
== RRQ
? 0 : 1);
file
= fdopen(fd
, (mode
== RRQ
)? "r":"w");
if (timeout
>= maxtimeout
)
* Send the requested file.
struct tftphdr
*dp
, *r_init();
register struct tftphdr
*ap
; /* ack packet */
register int block
= 1, size
, n
;
ap
= (struct tftphdr
*)ackbuf
;
size
= readit(file
, &dp
, pf
->f_convert
);
dp
->th_opcode
= htons((u_short
)DATA
);
dp
->th_block
= htons((u_short
)block
);
(void) setjmp(timeoutbuf
);
/* Now, we flush anything pending to be read */
/* This is to try to keep in synch between the two sides */
(void) ioctl(peer
, FIONREAD
, &i
);
n
= recvfrom(peer
, rbuf
, sizeof (rbuf
), 0,
(caddr_t
)&from
, &fromlen
);
if (send(peer
, dp
, size
+ 4, 0) != size
+ 4) {
read_ahead(file
, pf
->f_convert
);
alarm(rexmtval
); /* read the ack */
n
= recv(peer
, ackbuf
, sizeof (ackbuf
), 0);
ap
->th_opcode
= ntohs((u_short
)ap
->th_opcode
);
ap
->th_block
= ntohs((u_short
)ap
->th_block
);
if (ap
->th_opcode
== ERROR
)
if (ap
->th_opcode
== ACK
) {
if (ap
->th_block
== block
) {
if (ap
->th_block
== (block
-1)) {
} while (size
== SEGSIZE
);
struct tftphdr
*dp
, *w_init();
register struct tftphdr
*ap
; /* ack buffer */
register int block
= 0, n
, size
;
ap
= (struct tftphdr
*)ackbuf
;
ap
->th_opcode
= htons((u_short
)ACK
);
ap
->th_block
= htons((u_short
)block
);
(void) setjmp(timeoutbuf
);
/* Now, we flush anything pending to be read */
/* This is to try to keep in synch between the two sides */
(void) ioctl(peer
, FIONREAD
, &i
);
n
= recvfrom(peer
, rbuf
, sizeof (rbuf
), 0,
(caddr_t
)&from
, &fromlen
);
if (send(peer
, ackbuf
, 4, 0) != 4) {
write_behind(file
, pf
->f_convert
);
n
= recv(peer
, dp
, PKTSIZE
, 0);
if (n
< 0) { /* really? */
dp
->th_opcode
= ntohs((u_short
)dp
->th_opcode
);
dp
->th_block
= ntohs((u_short
)dp
->th_block
);
if (dp
->th_opcode
== ERROR
)
if (dp
->th_opcode
== DATA
) {
if (dp
->th_block
== block
) {
if (dp
->th_block
== (block
-1))
goto send_ack
; /* rexmit */
/* size = write(file, dp->th_data, n - 4); */
size
= writeit(file
, &dp
, n
- 4, pf
->f_convert
);
if (size
!= (n
-4)) { /* ahem */
if (size
< 0) nak(errno
+ 100);
} while (size
== SEGSIZE
);
write_behind(file
, pf
->f_convert
);
(void) fclose(file
); /* close data file */
ap
->th_opcode
= htons((u_short
)ACK
); /* send the "final" ack */
ap
->th_block
= htons((u_short
)(block
));
(void) send(peer
, ackbuf
, 4, 0);
signal(SIGALRM
, justquit
); /* just quit on timeout */
n
= recv(peer
, buf
, sizeof (buf
), 0); /* normally times out and quits */
if (n
>= 4 && /* if read some data */
dp
->th_opcode
== DATA
&& /* and got a data block */
block
== dp
->th_block
) { /* then my last ack was lost */
(void) send(peer
, ackbuf
, 4, 0); /* resend final ack */
{ 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" },
* Send a nak packet (error message).
* Error code passed in is one of the
* standard TFTP codes, or a UNIX errno
register struct tftphdr
*tp
;
register struct errmsg
*pe
;
extern char *sys_errlist
[];
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
++)
pe
->e_msg
= sys_errlist
[error
- 100];
tp
->th_code
= EUNDEF
; /* set 'undef' errorcode */
strcpy(tp
->th_msg
, pe
->e_msg
);
length
= strlen(pe
->e_msg
);
tp
->th_msg
[length
] = '\0';
if (send(peer
, buf
, length
, 0) != length
)