Reformat the source code

The source code was a mix of different styles; normalize on NASM
style; basically K&R style with 4 space indentation.
This commit is contained in:
H. Peter Anvin 2008-07-08 17:14:44 -04:00
parent 62533e7441
commit 22accddda0
18 changed files with 2794 additions and 2820 deletions

View file

@ -41,8 +41,7 @@
#ifndef lint #ifndef lint
/* static char sccsid[] = "@(#)tftpsubs.c 8.1 (Berkeley) 6/6/93"; */ /* static char sccsid[] = "@(#)tftpsubs.c 8.1 (Berkeley) 6/6/93"; */
/* static char rcsid[] = "$OpenBSD: tftpsubs.c,v 1.2 1996/06/26 05:40:36 deraadt Exp $"; */ /* static char rcsid[] = "$OpenBSD: tftpsubs.c,v 1.2 1996/06/26 05:40:36 deraadt Exp $"; */
static const char *rcsid UNUSED = static const char *rcsid UNUSED = "tftp-hpa: $Id$";
"tftp-hpa: $Id$";
#endif /* not lint */ #endif /* not lint */
/* Simple minded read-ahead/write-behind subroutines for tftp user and /* Simple minded read-ahead/write-behind subroutines for tftp user and
@ -81,13 +80,19 @@ int prevchar = -1; /* putbuf: previous char (cr check) */
static struct tftphdr *rw_init(int); static struct tftphdr *rw_init(int);
struct tftphdr *w_init() { return rw_init(0); } /* write-behind */ struct tftphdr *w_init()
struct tftphdr *r_init() { return rw_init(1); } /* read-ahead */ {
return rw_init(0);
} /* write-behind */
struct tftphdr *r_init()
{
return rw_init(1);
} /* read-ahead */
/* init for either read-ahead or write-behind */ /* init for either read-ahead or write-behind */
/* x == zero for write-behind, one for read-head */ /* x == zero for write-behind, one for read-head */
static struct tftphdr * static struct tftphdr *rw_init(int x)
rw_init(int x)
{ {
newline = 0; /* init crlf flag */ newline = 0; /* init crlf flag */
prevchar = -1; prevchar = -1;
@ -98,12 +103,10 @@ rw_init(int x)
return (struct tftphdr *)bfs[0].buf; return (struct tftphdr *)bfs[0].buf;
} }
/* Have emptied current buffer by sending to net and getting ack. /* Have emptied current buffer by sending to net and getting ack.
Free it and return next buffer filled with data. Free it and return next buffer filled with data.
*/ */
int int readit(FILE * file, struct tftphdr **dpp, int convert)
readit(FILE *file, struct tftphdr **dpp, int convert)
{ {
struct bf *b; struct bf *b;
@ -122,8 +125,7 @@ readit(FILE *file, struct tftphdr **dpp, int convert)
* fill the input buffer, doing ascii conversions if requested * fill the input buffer, doing ascii conversions if requested
* conversions are lf -> cr,lf and cr -> cr, nul * conversions are lf -> cr,lf and cr -> cr, nul
*/ */
void void read_ahead(FILE * file, int convert)
read_ahead(FILE *file, int convert)
{ {
int i; int i;
char *p; char *p;
@ -148,12 +150,13 @@ read_ahead(FILE *file, int convert)
if (newline) { if (newline) {
if (prevchar == '\n') if (prevchar == '\n')
c = '\n'; /* lf to cr,lf */ c = '\n'; /* lf to cr,lf */
else c = '\0'; /* cr to cr,nul */ else
c = '\0'; /* cr to cr,nul */
newline = 0; newline = 0;
} } else {
else {
c = getc(file); c = getc(file);
if (c == EOF) break; if (c == EOF)
break;
if (c == '\n' || c == '\r') { if (c == '\n' || c == '\r') {
prevchar = c; prevchar = c;
c = '\r'; c = '\r';
@ -169,8 +172,7 @@ read_ahead(FILE *file, int convert)
from the queue. Calls write_behind only if next buffer not from the queue. Calls write_behind only if next buffer not
available. available.
*/ */
int int writeit(FILE * file, struct tftphdr **dpp, int ct, int convert)
writeit(FILE *file, struct tftphdr **dpp, int ct, int convert)
{ {
bfs[current].counter = ct; /* set size of data to write */ bfs[current].counter = ct; /* set size of data to write */
current = !current; /* switch to other buffer */ current = !current; /* switch to other buffer */
@ -187,8 +189,7 @@ writeit(FILE *file, struct tftphdr **dpp, int ct, int convert)
* Note spec is undefined if we get CR as last byte of file or a * Note spec is undefined if we get CR as last byte of file or a
* CR followed by anything else. In this case we leave it alone. * CR followed by anything else. In this case we leave it alone.
*/ */
int int write_behind(FILE * file, int convert)
write_behind(FILE *file, int convert)
{ {
char *buf; char *buf;
int count; int count;
@ -208,7 +209,8 @@ write_behind(FILE *file, int convert)
nextone = !nextone; /* incr for next time */ nextone = !nextone; /* incr for next time */
buf = dp->th_data; buf = dp->th_data;
if (count <= 0) return -1; /* nak logic? */ if (count <= 0)
return -1; /* nak logic? */
if (convert == 0) if (convert == 0)
return write(fileno(file), buf, count); return write(fileno(file), buf, count);
@ -220,8 +222,7 @@ write_behind(FILE *file, int convert)
if (prevchar == '\r') { /* if prev char was cr */ if (prevchar == '\r') { /* if prev char was cr */
if (c == '\n') /* if have cr,lf then just */ if (c == '\n') /* if have cr,lf then just */
fseek(file, -1, 1); /* smash lf on top of the cr */ fseek(file, -1, 1); /* smash lf on top of the cr */
else else if (c == '\0') /* if have cr,nul then */
if (c == '\0') /* if have cr,nul then */
goto skipit; /* just skip over the putc */ goto skipit; /* just skip over the putc */
/* else just fall through and allow it */ /* else just fall through and allow it */
} }
@ -232,7 +233,6 @@ skipit:
return count; return count;
} }
/* When an error has occurred, it is possible that the two sides /* When an error has occurred, it is possible that the two sides
* are out of synch. Ie: that what I think is the other side's * are out of synch. Ie: that what I think is the other side's
* response to packet N is really their response to packet N-1. * response to packet N is really their response to packet N-1.
@ -244,9 +244,8 @@ skipit:
* when trace is active). * when trace is active).
*/ */
int int synchnet(int f)
synchnet(int f) /* socket to flush */ { /* socket to flush */
{
int pktcount = 0; int pktcount = 0;
char rbuf[PKTSIZE]; char rbuf[PKTSIZE];
struct sockaddr_in from; struct sockaddr_in from;
@ -273,8 +272,9 @@ synchnet(int f) /* socket to flush */
return pktcount; /* Return packets drained */ return pktcount; /* Return packets drained */
} }
int pick_port_bind(int sockfd, struct sockaddr_in *myaddr,
int pick_port_bind(int sockfd, struct sockaddr_in *myaddr, unsigned int port_range_from, unsigned int port_range_to) unsigned int port_range_from,
unsigned int port_range_to)
{ {
unsigned int port, firstport; unsigned int port, firstport;
int port_range = 0; int port_range = 0;

View file

@ -62,6 +62,7 @@ int writeit(FILE *, struct tftphdr **, int, int);
extern int segsize; extern int segsize;
#define MAX_SEGSIZE 65464 #define MAX_SEGSIZE 65464
int pick_port_bind(int sockfd, struct sockaddr_in *myaddr, unsigned int from, unsigned int to); int pick_port_bind(int sockfd, struct sockaddr_in *myaddr,
unsigned int from, unsigned int to);
#endif #endif

View file

@ -6,8 +6,7 @@
#include "config.h" #include "config.h"
void (*bsd_signal(int signum, void (*handler)(int)))(int) void (*bsd_signal(int signum, void (*handler) (int))) (int) {
{
struct sigaction action, oldaction; struct sigaction action, oldaction;
memset(&action, 0, sizeof action); memset(&action, 0, sizeof action);

View file

@ -17,8 +17,7 @@ int daemon(int nochdir, int noclose)
if (!noclose) { if (!noclose) {
if ((nullfd = open("/dev/null", O_RDWR)) < 0 || if ((nullfd = open("/dev/null", O_RDWR)) < 0 ||
dup2(nullfd, 0) < 0 || dup2(nullfd, 0) < 0 ||
dup2(nullfd, 1) < 0 || dup2(nullfd, 1) < 0 || dup2(nullfd, 2) < 0)
dup2(nullfd, 2) < 0)
return -1; return -1;
close(nullfd); close(nullfd);
} }

View file

@ -21,5 +21,3 @@ int dup2(int oldfd, int newfd)
return rv; return rv;
} }

View file

@ -37,13 +37,11 @@
#include "common/tftpsubs.h" #include "common/tftpsubs.h"
#ifndef lint #ifndef lint
static const char *copyright UNUSED = static const char *copyright UNUSED = "@(#) Copyright (c) 1983, 1993\n\
"@(#) Copyright (c) 1983, 1993\n\
The Regents of the University of California. All rights reserved.\n"; The Regents of the University of California. All rights reserved.\n";
/* static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; */ /* static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93"; */
/* static char rcsid[] = "$OpenBSD: main.c,v 1.4 1997/01/17 07:13:30 millert Exp $"; */ /* static char rcsid[] = "$OpenBSD: main.c,v 1.4 1997/01/17 07:13:30 millert Exp $"; */
static const char *rcsid UNUSED = static const char *rcsid UNUSED = "tftp-hpa $Id$";
"tftp-hpa $Id$";
#endif /* not lint */ #endif /* not lint */
/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
@ -80,6 +78,7 @@ static const struct modes modes[] = {
{"image", "octet", O_BINARY}, {"image", "octet", O_BINARY},
{0, 0, 0} {0, 0, 0}
}; };
#define MODE_OCTET (&modes[2]) #define MODE_OCTET (&modes[2])
#define MODE_NETASCII (&modes[0]) #define MODE_NETASCII (&modes[0])
#define MODE_DEFAULT MODE_NETASCII #define MODE_DEFAULT MODE_NETASCII
@ -195,12 +194,13 @@ const char *program;
static inline void usage(int errcode) static inline void usage(int errcode)
{ {
fprintf(stderr, "Usage: %s [-v][-l][-m mode] [host [port]] [-c command]\n", program); fprintf(stderr,
"Usage: %s [-v][-l][-m mode] [host [port]] [-c command]\n",
program);
exit(errcode); exit(errcode);
} }
int int main(int argc, char *argv[])
main(int argc, char *argv[])
{ {
struct sockaddr_in s_in; struct sockaddr_in s_in;
int arg; int arg;
@ -244,7 +244,8 @@ main(int argc, char *argv[])
if (p->m_name) { if (p->m_name) {
settftpmode(p); settftpmode(p);
} else { } else {
fprintf(stderr, "%s: invalid mode: %s\n", argv[0], argv[arg]); fprintf(stderr, "%s: invalid mode: %s\n",
argv[0], argv[arg]);
exit(EX_USAGE); exit(EX_USAGE);
} }
} }
@ -255,8 +256,11 @@ main(int argc, char *argv[])
case 'R': case 'R':
if (++arg >= argc) if (++arg >= argc)
usage(EX_USAGE); usage(EX_USAGE);
if ( sscanf(argv[arg], "%u:%u", &portrange_from, &portrange_to) != 2 || if (sscanf
portrange_from > portrange_to || portrange_to > 65535 ) { (argv[arg], "%u:%u", &portrange_from,
&portrange_to) != 2
|| portrange_from > portrange_to
|| portrange_to > 65535) {
fprintf(stderr, "Bad port range: %s\n", argv[arg]); fprintf(stderr, "Bad port range: %s\n", argv[arg]);
exit(EX_USAGE); exit(EX_USAGE);
} }
@ -282,7 +286,8 @@ main(int argc, char *argv[])
if (sp == 0) { if (sp == 0) {
/* Use canned values */ /* Use canned values */
if (verbose) if (verbose)
fprintf(stderr, "tftp: tftp/udp: unknown service, faking it...\n"); fprintf(stderr,
"tftp: tftp/udp: unknown service, faking it...\n");
sp = xmalloc(sizeof(struct servent)); sp = xmalloc(sizeof(struct servent));
sp->s_name = (char *)"tftp"; sp->s_name = (char *)"tftp";
sp->s_aliases = NULL; sp->s_aliases = NULL;
@ -319,13 +324,13 @@ main(int argc, char *argv[])
c = getcmd(pargv[0]); c = getcmd(pargv[0]);
if (c == (struct cmd *)-1 || c == (struct cmd *)0) { if (c == (struct cmd *)-1 || c == (struct cmd *)0) {
fprintf(stderr, "%s: invalid command: %s\n", argv[0], pargv[1]); fprintf(stderr, "%s: invalid command: %s\n", argv[0],
pargv[1]);
exit(EX_USAGE); exit(EX_USAGE);
} }
(*c->handler) (pargc, pargv); (*c->handler) (pargc, pargv);
exit(0); exit(0);
} }
#ifdef WITH_READLINE #ifdef WITH_READLINE
#ifdef HAVE_READLINE_HISTORY_H #ifdef HAVE_READLINE_HISTORY_H
using_history(); using_history();
@ -343,8 +348,7 @@ char *hostname;
/* Called when a command is incomplete; modifies /* Called when a command is incomplete; modifies
the global variable "line" */ the global variable "line" */
static void static void getmoreargs(const char *partial, const char *mprompt)
getmoreargs(const char *partial, const char *mprompt)
{ {
#ifdef WITH_READLINE #ifdef WITH_READLINE
char *eline; char *eline;
@ -380,8 +384,7 @@ getmoreargs(const char *partial, const char *mprompt)
#endif #endif
} }
void void setpeer(int argc, char *argv[])
setpeer(int argc, char *argv[])
{ {
struct hostent *host; struct hostent *host;
@ -433,8 +436,7 @@ setpeer(int argc, char *argv[])
connected = 1; connected = 1;
} }
void void modecmd(int argc, char *argv[])
modecmd(int argc, char *argv[])
{ {
const struct modes *p; const struct modes *p;
const char *sep; const char *sep;
@ -466,34 +468,31 @@ modecmd(int argc, char *argv[])
return; return;
} }
void void setbinary(int argc, char *argv[])
setbinary(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
settftpmode(MODE_OCTET); settftpmode(MODE_OCTET);
} }
void void setascii(int argc, char *argv[])
setascii(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
settftpmode(MODE_NETASCII); settftpmode(MODE_NETASCII);
} }
static void static void settftpmode(const struct modes *newmode)
settftpmode(const struct modes *newmode)
{ {
mode = newmode; mode = newmode;
if (verbose) if (verbose)
printf("mode set to %s\n", mode->m_mode); printf("mode set to %s\n", mode->m_mode);
} }
/* /*
* Send file(s). * Send file(s).
*/ */
void void put(int argc, char *argv[])
put(int argc, char *argv[])
{ {
int fd; int fd;
int n; int n;
@ -540,7 +539,8 @@ put(int argc, char *argv[])
cp = argc == 2 ? tail(targ) : argv[1]; cp = argc == 2 ? tail(targ) : argv[1];
fd = open(cp, O_RDONLY | mode->m_openflags); fd = open(cp, O_RDONLY | mode->m_openflags);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "tftp: "); perror(cp); fprintf(stderr, "tftp: ");
perror(cp);
return; return;
} }
if (verbose) if (verbose)
@ -558,7 +558,8 @@ put(int argc, char *argv[])
strcpy(cp, tail(argv[n])); strcpy(cp, tail(argv[n]));
fd = open(argv[n], O_RDONLY | mode->m_openflags); fd = open(argv[n], O_RDONLY | mode->m_openflags);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "tftp: "); perror(argv[n]); fprintf(stderr, "tftp: ");
perror(argv[n]);
continue; continue;
} }
if (verbose) if (verbose)
@ -569,8 +570,7 @@ put(int argc, char *argv[])
} }
} }
static void static void putusage(char *s)
putusage(char *s)
{ {
printf("usage: %s file ... host:target, or\n", s); printf("usage: %s file ... host:target, or\n", s);
printf(" %s file ... target (when already connected)\n", s); printf(" %s file ... target (when already connected)\n", s);
@ -579,8 +579,7 @@ putusage(char *s)
/* /*
* Receive file(s). * Receive file(s).
*/ */
void void get(int argc, char *argv[])
get(int argc, char *argv[])
{ {
int fd; int fd;
int n; int n;
@ -618,17 +617,18 @@ get(int argc, char *argv[])
herror((char *)NULL); herror((char *)NULL);
continue; continue;
} }
bcopy(hp->h_addr, (caddr_t)&peeraddr.sin_addr, bcopy(hp->h_addr, (caddr_t) & peeraddr.sin_addr, hp->h_length);
hp->h_length);
peeraddr.sin_family = hp->h_addrtype; peeraddr.sin_family = hp->h_addrtype;
connected = 1; connected = 1;
hostname = xstrdup(hp->h_name); hostname = xstrdup(hp->h_name);
} }
if (argc < 4) { if (argc < 4) {
cp = argc == 3 ? argv[2] : tail(src); cp = argc == 3 ? argv[2] : tail(src);
fd = open(cp, O_WRONLY|O_CREAT|O_TRUNC|mode->m_openflags, 0666); fd = open(cp, O_WRONLY | O_CREAT | O_TRUNC | mode->m_openflags,
0666);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "tftp: "); perror(cp); fprintf(stderr, "tftp: ");
perror(cp);
return; return;
} }
if (verbose) if (verbose)
@ -639,9 +639,11 @@ get(int argc, char *argv[])
break; break;
} }
cp = tail(src); /* new .. jdg */ cp = tail(src); /* new .. jdg */
fd = open(cp, O_WRONLY|O_CREAT|O_TRUNC|mode->m_openflags, 0666); fd = open(cp, O_WRONLY | O_CREAT | O_TRUNC | mode->m_openflags,
0666);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "tftp: "); perror(cp); fprintf(stderr, "tftp: ");
perror(cp);
continue; continue;
} }
if (verbose) if (verbose)
@ -652,8 +654,7 @@ get(int argc, char *argv[])
} }
} }
static void static void getusage(char *s)
getusage(char *s)
{ {
printf("usage: %s host:file host:file ... file, or\n", s); printf("usage: %s host:file host:file ... file, or\n", s);
printf(" %s file file ... file if connected\n", s); printf(" %s file file ... file if connected\n", s);
@ -661,8 +662,7 @@ getusage(char *s)
int rexmtval = TIMEOUT; int rexmtval = TIMEOUT;
void void setrexmt(int argc, char *argv[])
setrexmt(int argc, char *argv[])
{ {
int t; int t;
@ -685,8 +685,7 @@ setrexmt(int argc, char *argv[])
int maxtimeout = 5 * TIMEOUT; int maxtimeout = 5 * TIMEOUT;
void void settimeout(int argc, char *argv[])
settimeout(int argc, char *argv[])
{ {
int t; int t;
@ -707,30 +706,30 @@ settimeout(int argc, char *argv[])
maxtimeout = t; maxtimeout = t;
} }
void void setliteral(int argc, char *argv[])
setliteral(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
literal = !literal; literal = !literal;
printf("Literal mode %s.\n", literal ? "on" : "off"); printf("Literal mode %s.\n", literal ? "on" : "off");
} }
void void status(int argc, char *argv[])
status(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
if (connected) if (connected)
printf("Connected to %s.\n", hostname); printf("Connected to %s.\n", hostname);
else else
printf("Not connected.\n"); printf("Not connected.\n");
printf("Mode: %s Verbose: %s Tracing: %s Literal: %s\n", mode->m_mode, printf("Mode: %s Verbose: %s Tracing: %s Literal: %s\n", mode->m_mode,
verbose ? "on" : "off", trace ? "on" : "off", literal ? "on" : "off"); verbose ? "on" : "off", trace ? "on" : "off",
literal ? "on" : "off");
printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n", printf("Rexmt-interval: %d seconds, Max-timeout: %d seconds\n",
rexmtval, maxtimeout); rexmtval, maxtimeout);
} }
void void intr(int sig)
intr(int sig)
{ {
(void)sig; /* Quiet unused warning */ (void)sig; /* Quiet unused warning */
@ -739,8 +738,7 @@ intr(int sig)
siglongjmp(toplevel, -1); siglongjmp(toplevel, -1);
} }
char * char *tail(char *filename)
tail(char *filename)
{ {
char *s; char *s;
@ -758,8 +756,7 @@ tail(char *filename)
/* /*
* Command parser. * Command parser.
*/ */
static void static void command(void)
command(void)
{ {
struct cmd *c; struct cmd *c;
@ -806,8 +803,7 @@ command(void)
} }
} }
struct cmd * struct cmd *getcmd(char *name)
getcmd(char *name)
{ {
const char *p; const char *p;
char *q; char *q;
@ -838,8 +834,7 @@ getcmd(char *name)
/* /*
* Slice a string up into argc/argv. * Slice a string up into argc/argv.
*/ */
static void static void makeargv(void)
makeargv(void)
{ {
char *cp; char *cp;
char **argp = margv; char **argp = margv;
@ -861,18 +856,17 @@ makeargv(void)
*argp++ = 0; *argp++ = 0;
} }
void void quit(int argc, char *argv[])
quit(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
exit(0); exit(0);
} }
/* /*
* Help command. * Help command.
*/ */
void void help(int argc, char *argv[])
help(int argc, char *argv[])
{ {
struct cmd *c; struct cmd *c;
@ -897,19 +891,19 @@ help(int argc, char *argv[])
} }
} }
void void settrace(int argc, char *argv[])
settrace(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
trace = !trace; trace = !trace;
printf("Packet tracing %s.\n", trace ? "on" : "off"); printf("Packet tracing %s.\n", trace ? "on" : "off");
} }
void void setverbose(int argc, char *argv[])
setverbose(int argc, char *argv[])
{ {
(void)argc; (void)argv; /* Quiet unused warning */ (void)argc;
(void)argv; /* Quiet unused warning */
verbose = !verbose; verbose = !verbose;
printf("Verbose mode %s.\n", verbose ? "on" : "off"); printf("Verbose mode %s.\n", verbose ? "on" : "off");

View file

@ -39,8 +39,7 @@
#ifndef lint #ifndef lint
/* static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93"; */ /* static char sccsid[] = "@(#)tftp.c 8.1 (Berkeley) 6/6/93"; */
/* static char rcsid[] = "$OpenBSD: tftp.c,v 1.4 1997/08/06 06:43:45 deraadt Exp $"; */ /* static char rcsid[] = "$OpenBSD: tftp.c,v 1.4 1997/08/06 06:43:45 deraadt Exp $"; */
static const char *rcsid UNUSED = static const char *rcsid UNUSED = "tftp-hpa $Id$";
"tftp-hpa $Id$";
#endif /* not lint */ #endif /* not lint */
/* Many bug fixes are from Jim Guyton <guyton@rand-unix> */ /* Many bug fixes are from Jim Guyton <guyton@rand-unix> */
@ -74,8 +73,7 @@ static void tpacket(const char *, struct tftphdr *, int);
/* /*
* Send the requested file. * Send the requested file.
*/ */
void void tftp_sendfile(int fd, const char *name, const char *mode)
tftp_sendfile(int fd, const char *name, const char *mode)
{ {
struct tftphdr *ap; /* data and ack packets */ struct tftphdr *ap; /* data and ack packets */
struct tftphdr *dp; struct tftphdr *dp;
@ -143,8 +141,7 @@ tftp_sendfile(int fd, const char *name, const char *mode)
ap_opcode = ntohs((u_short) ap->th_opcode); ap_opcode = ntohs((u_short) ap->th_opcode);
ap_block = ntohs((u_short) ap->th_block); ap_block = ntohs((u_short) ap->th_block);
if (ap_opcode == ERROR) { if (ap_opcode == ERROR) {
printf("Error code %d: %s\n", ap_block, printf("Error code %d: %s\n", ap_block, ap->th_msg);
ap->th_msg);
goto abort; goto abort;
} }
if (ap_opcode == ACK) { if (ap_opcode == ACK) {
@ -158,8 +155,7 @@ tftp_sendfile(int fd, const char *name, const char *mode)
*/ */
j = synchnet(f); j = synchnet(f);
if (j && trace) { if (j && trace) {
printf("discarded %d packets\n", printf("discarded %d packets\n", j);
j);
} }
/* /*
* RFC1129/RFC1350: We MUST NOT re-send the DATA * RFC1129/RFC1350: We MUST NOT re-send the DATA
@ -183,8 +179,7 @@ abort:
/* /*
* Receive a file. * Receive a file.
*/ */
void void tftp_recvfile(int fd, const char *name, const char *mode)
tftp_recvfile(int fd, const char *name, const char *mode)
{ {
struct tftphdr *ap; struct tftphdr *ap;
struct tftphdr *dp; struct tftphdr *dp;
@ -307,8 +302,7 @@ makerequest(int request, const char *name,
return (cp - (char *)tp); return (cp - (char *)tp);
} }
static const char * const errmsgs[] = static const char *const errmsgs[] = {
{
"Undefined error code", /* 0 - EUNDEF */ "Undefined error code", /* 0 - EUNDEF */
"File not found", /* 1 - ENOTFOUND */ "File not found", /* 1 - ENOTFOUND */
"Access denied", /* 2 - EACCESS */ "Access denied", /* 2 - EACCESS */
@ -319,6 +313,7 @@ static const char * const errmsgs[] =
"No such user", /* 7 - ENOUSER */ "No such user", /* 7 - ENOUSER */
"Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */ "Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */
}; };
#define ERR_CNT (sizeof(errmsgs)/sizeof(const char *)) #define ERR_CNT (sizeof(errmsgs)/sizeof(const char *))
/* /*
@ -327,8 +322,7 @@ static const char * const errmsgs[] =
* standard TFTP codes, or a UNIX errno * standard TFTP codes, or a UNIX errno
* offset by 100. * offset by 100.
*/ */
static void static void nak(int error, const char *msg)
nak(int error, const char *msg)
{ {
struct tftphdr *tp; struct tftphdr *tp;
int length; int length;
@ -363,8 +357,7 @@ nak(int error, const char *msg)
perror("nak"); perror("nak");
} }
static void static void tpacket(const char *s, struct tftphdr *tp, int n)
tpacket(const char *s, struct tftphdr *tp, int n)
{ {
static const char *opcodes[] = static const char *opcodes[] =
{ "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" }; { "#0", "RRQ", "WRQ", "DATA", "ACK", "ERROR", "OACK" };
@ -402,21 +395,18 @@ tpacket(const char *s, struct tftphdr *tp, int n)
struct timeval tstart; struct timeval tstart;
struct timeval tstop; struct timeval tstop;
static void static void startclock(void)
startclock(void)
{ {
(void)gettimeofday(&tstart, NULL); (void)gettimeofday(&tstart, NULL);
} }
static void static void stopclock(void)
stopclock(void)
{ {
(void)gettimeofday(&tstop, NULL); (void)gettimeofday(&tstop, NULL);
} }
static void static void printstats(const char *direction, unsigned long amount)
printstats(const char *direction, unsigned long amount)
{ {
double delta; double delta;
@ -429,8 +419,7 @@ printstats(const char *direction, unsigned long amount)
} }
} }
static void static void timer(int sig)
timer(int sig)
{ {
int save_errno = errno; int save_errno = errno;

View file

@ -66,4 +66,3 @@ char *tfstrdup(const char *str)
return p; return p;
} }

View file

@ -90,7 +90,6 @@ static int address_is_local(const struct sockaddr_in *addr)
return rv; return rv;
} }
int int
myrecvfrom(int s, void *buf, int len, unsigned int flags, myrecvfrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, socklen_t * fromlen, struct sockaddr *from, socklen_t * fromlen,
@ -161,8 +160,10 @@ myrecvfrom(int s, void *buf, int len, unsigned int flags,
#ifdef IP_PKTINFO #ifdef IP_PKTINFO
if (cmptr->cmsg_level == IPPROTO_IP && if (cmptr->cmsg_level == IPPROTO_IP &&
cmptr->cmsg_type == IP_PKTINFO) { cmptr->cmsg_type == IP_PKTINFO) {
memcpy(&pktinfo, CMSG_DATA(cmptr), sizeof(struct in_pktinfo)); memcpy(&pktinfo, CMSG_DATA(cmptr),
memcpy(&myaddr->sin_addr, &pktinfo.ipi_addr, sizeof(struct in_addr)); sizeof(struct in_pktinfo));
memcpy(&myaddr->sin_addr, &pktinfo.ipi_addr,
sizeof(struct in_addr));
} }
#endif #endif
@ -180,8 +181,7 @@ myrecvfrom(int s, void *buf, int len, unsigned int flags,
int int
myrecvfrom(int s, void *buf, int len, unsigned int flags, myrecvfrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, int *fromlen, struct sockaddr *from, int *fromlen, struct sockaddr_in *myaddr)
struct sockaddr_in *myaddr)
{ {
/* There is no way we can get the local address, fudge it */ /* There is no way we can get the local address, fudge it */

View file

@ -58,8 +58,9 @@ static int xform_tolower(int c)
} }
/* Do \-substitution. Call with string == NULL to get length only. */ /* Do \-substitution. Call with string == NULL to get length only. */
static int genmatchstring(char *string, const char *pattern, const char *input, static int genmatchstring(char *string, const char *pattern,
const regmatch_t *pmatch, match_pattern_callback macrosub) const char *input, const regmatch_t * pmatch,
match_pattern_callback macrosub)
{ {
int (*xform) (int) = xform_null; int (*xform) (int) = xform_null;
int len = 0; int len = 0;
@ -81,8 +82,16 @@ static int genmatchstring(char *string, const char *pattern, const char *input,
if (*pattern == '\\' && pattern[1] != '\0') { if (*pattern == '\\' && pattern[1] != '\0') {
char macro = pattern[1]; char macro = pattern[1];
switch (macro) { switch (macro) {
case '0': case '1': case '2': case '3': case '4': case '0':
case '5': case '6': case '7': case '8': case '9': case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
n = pattern[1] - '0'; n = pattern[1] - '0';
if (pmatch[n].rm_so != -1) { if (pmatch[n].rm_so != -1) {
@ -109,8 +118,7 @@ static int genmatchstring(char *string, const char *pattern, const char *input,
break; break;
default: default:
if ( macrosub && if (macrosub && (sublen = macrosub(macro, string)) >= 0) {
(sublen = macrosub(macro, string)) >= 0 ) {
while (sublen--) { while (sublen--) {
len++; len++;
if (string) { if (string) {
@ -223,7 +231,8 @@ static int parseline(char *line, struct rule *r, int lineno)
r->rule_flags |= RULE_INVERSE; r->rule_flags |= RULE_INVERSE;
break; break;
default: default:
syslog(LOG_ERR, "Remap command \"%s\" on line %d contains invalid char \"%c\"", syslog(LOG_ERR,
"Remap command \"%s\" on line %d contains invalid char \"%c\"",
buffer, lineno, *p); buffer, lineno, *p);
return -1; /* Error */ return -1; /* Error */
break; break;
@ -236,7 +245,8 @@ static int parseline(char *line, struct rule *r, int lineno)
if ((r->rule_flags & (RULE_INVERSE | RULE_REWRITE)) == if ((r->rule_flags & (RULE_INVERSE | RULE_REWRITE)) ==
(RULE_INVERSE | RULE_REWRITE)) { (RULE_INVERSE | RULE_REWRITE)) {
syslog(LOG_ERR, "r rules cannot be inverted, line %d: %s\n", lineno, line); syslog(LOG_ERR, "r rules cannot be inverted, line %d: %s\n",
lineno, line);
return -1; /* Error */ return -1; /* Error */
} }
@ -249,7 +259,8 @@ static int parseline(char *line, struct rule *r, int lineno)
if ((rv = regcomp(&r->rx, buffer, rxflags)) != 0) { if ((rv = regcomp(&r->rx, buffer, rxflags)) != 0) {
char errbuf[BUFSIZ]; char errbuf[BUFSIZ];
regerror(rv, &r->rx, errbuf, BUFSIZ); regerror(rv, &r->rx, errbuf, BUFSIZ);
syslog(LOG_ERR, "Bad regex in remap line %d: %s\n", lineno, errbuf); syslog(LOG_ERR, "Bad regex in remap line %d: %s\n", lineno,
errbuf);
return -1; /* Error */ return -1; /* Error */
} }
@ -343,8 +354,9 @@ char *rewrite_string(const char *input, const struct rule *rules,
} }
if (!deadman--) { if (!deadman--) {
syslog(LOG_WARNING, "remap: Breaking loop, input = %s, last = %s", syslog(LOG_WARNING,
input, current); "remap: Breaking loop, input = %s, last = %s", input,
current);
free(current); free(current);
return NULL; /* Did not terminate! */ return NULL; /* Did not terminate! */
} }
@ -369,7 +381,8 @@ char *rewrite_string(const char *input, const struct rule *rules,
} }
if (ruleptr->pattern[0]) { if (ruleptr->pattern[0]) {
/* Custom error message */ /* Custom error message */
len = genmatchstring(NULL, ruleptr->pattern, current, len =
genmatchstring(NULL, ruleptr->pattern, current,
pmatch, macrosub); pmatch, macrosub);
newstr = tfmalloc(len + 1); newstr = tfmalloc(len + 1);
genmatchstring(newstr, ruleptr->pattern, current, genmatchstring(newstr, ruleptr->pattern, current,
@ -406,13 +419,15 @@ char *rewrite_string(const char *input, const struct rule *rules,
if (ruleptr->rule_flags & RULE_EXIT) { if (ruleptr->rule_flags & RULE_EXIT) {
if (verbosity >= 3) { if (verbosity >= 3) {
syslog(LOG_INFO, "remap: rule %d: exit", ruleptr->nrule); syslog(LOG_INFO, "remap: rule %d: exit",
ruleptr->nrule);
} }
return current; /* Exit here, we're done */ return current; /* Exit here, we're done */
} else if (ruleptr->rule_flags & RULE_RESTART) { } else if (ruleptr->rule_flags & RULE_RESTART) {
ruleptr = rules; /* Start from the top */ ruleptr = rules; /* Start from the top */
if (verbosity >= 3) { if (verbosity >= 3) {
syslog(LOG_INFO, "remap: rule %d: restart", ruleptr->nrule); syslog(LOG_INFO, "remap: rule %d: restart",
ruleptr->nrule);
} }
} }
} }

View file

@ -40,4 +40,3 @@ char *rewrite_string(const char *, const struct rule *, int,
#endif /* WITH_REGEX */ #endif /* WITH_REGEX */
#endif /* TFTPD_REMAP_H */ #endif /* TFTPD_REMAP_H */

View file

@ -36,16 +36,6 @@
#include "config.h" /* Must be included first */ #include "config.h" /* Must be included first */
#include "tftpd.h" #include "tftpd.h"
#ifndef lint
static const char *copyright UNUSED =
"@(#) Copyright (c) 1983 Regents of the University of California.\n\
All rights reserved.\n";
/*static char sccsid[] = "from: @(#)tftpd.c 5.13 (Berkeley) 2/26/91";*/
/*static char rcsid[] = "$OpenBSD: tftpd.c,v 1.13 1999/06/23 17:01:36 deraadt Exp $: tftpd.c,v 1.6 1997/02/16 23:49:21 deraadt Exp $";*/
static const char *rcsid UNUSED =
"tftp-hpa $Id$";
#endif /* not lint */
/* /*
* Trivial file transfer protocol server. * Trivial file transfer protocol server.
* *
@ -130,12 +120,13 @@ struct options {
const char *o_opt; const char *o_opt;
int (*o_fnc) (char *, char **); int (*o_fnc) (char *, char **);
} options[] = { } options[] = {
{ "blksize", set_blksize }, {
{ "blksize2", set_blksize2 }, "blksize", set_blksize}, {
{ "tsize", set_tsize }, "blksize2", set_blksize2}, {
{ "timeout", set_timeout }, "tsize", set_tsize}, {
{ "utimeout", set_utimeout }, "timeout", set_timeout}, {
{ NULL, NULL } "utimeout", set_utimeout}, {
NULL, NULL}
}; };
/* Simple handler for SIGHUP */ /* Simple handler for SIGHUP */
@ -146,10 +137,8 @@ static void handle_sighup(int sig)
caught_sighup = 1; caught_sighup = 1;
} }
/* Handle timeout signal or timeout event */ /* Handle timeout signal or timeout event */
void void timer(int sig)
timer(int sig)
{ {
(void)sig; /* Suppress unused warning */ (void)sig; /* Suppress unused warning */
timeout <<= 1; timeout <<= 1;
@ -158,18 +147,8 @@ timer(int sig)
siglongjmp(timeoutbuf, 1); siglongjmp(timeoutbuf, 1);
} }
static void
usage(void)
{
syslog(LOG_ERR, "Usage: %s [-vcl][-a address][-m mappings][-u user][-t inetd_timeout][-T pkt_timeout][-r option...] [-s] [directory ...]",
__progname);
exit(EX_USAGE);
}
#ifdef WITH_REGEX #ifdef WITH_REGEX
static struct rule * static struct rule *read_remap_rules(const char *file)
read_remap_rules(const char *file)
{ {
FILE *f; FILE *f;
struct rule *rulep; struct rule *rulep;
@ -186,15 +165,16 @@ read_remap_rules(const char *file)
} }
#endif #endif
static void static void set_socket_nonblock(int fd, int flag)
set_socket_nonblock(int fd, int flag)
{ {
int err; int err;
int flags; int flags;
#if defined(HAVE_FCNTL) && defined(HAVE_O_NONBLOCK_DEFINITION) #if defined(HAVE_FCNTL) && defined(HAVE_O_NONBLOCK_DEFINITION)
/* Posixly correct */ /* Posixly correct */
err = ((flags = fcntl(fd, F_GETFL, 0)) < 0) || err = ((flags = fcntl(fd, F_GETFL, 0)) < 0) ||
(fcntl(fd, F_SETFL, flag ? flags|O_NONBLOCK : flags&~O_NONBLOCK) < 0); (fcntl
(fd, F_SETFL,
flag ? flags | O_NONBLOCK : flags & ~O_NONBLOCK) < 0);
#else #else
flags = flag ? 1 : 0; flags = flag ? 1 : 0;
err = (ioctl(fd, FIONBIO, &flags) < 0); err = (ioctl(fd, FIONBIO, &flags) < 0);
@ -205,8 +185,7 @@ set_socket_nonblock(int fd, int flag)
} }
} }
static void static void pmtu_discovery_off(int fd)
pmtu_discovery_off(int fd)
{ {
#if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT) #if defined(IP_MTU_DISCOVER) && defined(IP_PMTUDISC_DONT)
int pmtu = IP_PMTUDISC_DONT; int pmtu = IP_PMTUDISC_DONT;
@ -244,8 +223,10 @@ static int recv_time(int s, void *rbuf, int len, unsigned int flags,
gettimeofday(&t1, NULL); gettimeofday(&t1, NULL);
dt = (t1.tv_sec - t0.tv_sec)*1000000 + (t1.tv_usec - t0.tv_usec); dt = (t1.tv_sec - t0.tv_sec) * 1000000 + (t1.tv_usec -
*timeout_us_p = timeout_left = ( dt >= timeout_us ) ? 1 : (timeout_us - dt); t0.tv_usec);
*timeout_us_p = timeout_left =
(dt >= timeout_us) ? 1 : (timeout_us - dt);
} while (rv == -1 && err == EINTR); } while (rv == -1 && err == EINTR);
if (rv == 0) { if (rv == 0) {
@ -271,9 +252,7 @@ static int recv_time(int s, void *rbuf, int len, unsigned int flags,
} }
} }
int main(int argc, char **argv)
int
main(int argc, char **argv)
{ {
struct tftphdr *tp; struct tftphdr *tp;
struct passwd *pw; struct passwd *pw;
@ -336,7 +315,8 @@ main(int argc, char **argv)
char *vp; char *vp;
max_blksize = (unsigned int)strtoul(optarg, &vp, 10); max_blksize = (unsigned int)strtoul(optarg, &vp, 10);
if (max_blksize < 512 || max_blksize > MAX_SEGSIZE || *vp) { if (max_blksize < 512 || max_blksize > MAX_SEGSIZE || *vp) {
syslog(LOG_ERR, "Bad maximum blocksize value (range 512-%d): %s", syslog(LOG_ERR,
"Bad maximum blocksize value (range 512-%d): %s",
MAX_SEGSIZE, optarg); MAX_SEGSIZE, optarg);
exit(EX_USAGE); exit(EX_USAGE);
} }
@ -356,8 +336,9 @@ main(int argc, char **argv)
break; break;
case 'R': case 'R':
{ {
if ( sscanf(optarg, "%u:%u", &portrange_from, &portrange_to) != 2 || if (sscanf(optarg, "%u:%u", &portrange_from, &portrange_to)
portrange_from > portrange_to || portrange_to >= 65535 ) { != 2 || portrange_from > portrange_to
|| portrange_to >= 65535) {
syslog(LOG_ERR, "Bad port range: %s", optarg); syslog(LOG_ERR, "Bad port range: %s", optarg);
exit(EX_USAGE); exit(EX_USAGE);
} }
@ -405,7 +386,7 @@ main(int argc, char **argv)
exit(0); exit(0);
break; break;
default: default:
usage(); syslog(LOG_ERR, "Unknown option: '%c'", optopt);
break; break;
} }
@ -473,10 +454,13 @@ main(int argc, char **argv)
if (*address) { if (*address) {
hostent = gethostbyname(address); hostent = gethostbyname(address);
if (!hostent || hostent->h_addrtype != AF_INET) { if (!hostent || hostent->h_addrtype != AF_INET) {
syslog(LOG_ERR, "cannot resolve local bind address: %s", address); syslog(LOG_ERR,
"cannot resolve local bind address: %s",
address);
exit(EX_NOINPUT); exit(EX_NOINPUT);
} }
memcpy(&bindaddr.sin_addr, hostent->h_addr, hostent->h_length); memcpy(&bindaddr.sin_addr, hostent->h_addr,
hostent->h_length);
} else { } else {
/* Default to using INADDR_ANY */ /* Default to using INADDR_ANY */
} }
@ -485,12 +469,14 @@ main(int argc, char **argv)
servent = getservbyname(portptr, "udp"); servent = getservbyname(portptr, "udp");
if (servent) { if (servent) {
bindaddr.sin_port = servent->s_port; bindaddr.sin_port = servent->s_port;
} else if ( (port = strtoul(portptr, &eportptr, 0)) && !*eportptr ) { } else if ((port = strtoul(portptr, &eportptr, 0))
&& !*eportptr) {
bindaddr.sin_port = htons(port); bindaddr.sin_port = htons(port);
} else if (!strcmp(portptr, "tftp")) { } else if (!strcmp(portptr, "tftp")) {
/* It's TFTP, we're OK */ /* It's TFTP, we're OK */
} else { } else {
syslog(LOG_ERR, "cannot resolve local bind port: %s", portptr); syslog(LOG_ERR, "cannot resolve local bind port: %s",
portptr);
exit(EX_NOINPUT); exit(EX_NOINPUT);
} }
} }
@ -510,7 +496,8 @@ main(int argc, char **argv)
} }
} else { } else {
/* 0 is our socket descriptor */ /* 0 is our socket descriptor */
close(1); close(2); close(1);
close(2);
} }
/* Disable path MTU discovery */ /* Disable path MTU discovery */
@ -560,7 +547,8 @@ main(int argc, char **argv)
#endif #endif
/* Never time out if we're in standalone mode */ /* Never time out if we're in standalone mode */
rv = select(fd+1, &readset, NULL, NULL, standalone ? NULL : &tv_waittime); rv = select(fd + 1, &readset, NULL, NULL,
standalone ? NULL : &tv_waittime);
if (rv == -1 && errno == EINTR) if (rv == -1 && errno == EINTR)
continue; /* Signal caught, reloop */ continue; /* Signal caught, reloop */
if (rv == -1) { if (rv == -1) {
@ -569,15 +557,13 @@ main(int argc, char **argv)
} else if (rv == 0) { } else if (rv == 0) {
exit(0); /* Timeout, return to inetd */ exit(0); /* Timeout, return to inetd */
} }
#ifdef __CYGWIN__ #ifdef __CYGWIN__
set_socket_nonblock(fd, 1); set_socket_nonblock(fd, 1);
#endif #endif
fromlen = sizeof(from); fromlen = sizeof(from);
n = myrecvfrom(fd, buf, sizeof(buf), 0, n = myrecvfrom(fd, buf, sizeof(buf), 0,
(struct sockaddr *)&from, &fromlen, (struct sockaddr *)&from, &fromlen, &myaddr);
&myaddr);
if (n < 0) { if (n < 0) {
if (E_WOULD_BLOCK(errno) || errno == EINTR) { if (E_WOULD_BLOCK(errno) || errno == EINTR) {
@ -589,14 +575,16 @@ main(int argc, char **argv)
} }
if (from.sin_family != AF_INET) { if (from.sin_family != AF_INET) {
syslog(LOG_ERR, "received address was not AF_INET, please check your inetd config"); syslog(LOG_ERR,
"received address was not AF_INET, please check your inetd config");
exit(EX_PROTOCOL); exit(EX_PROTOCOL);
} }
if (standalone && myaddr.sin_addr.s_addr == INADDR_ANY) { if (standalone && myaddr.sin_addr.s_addr == INADDR_ANY) {
/* myrecvfrom() didn't capture the source address; but we might /* myrecvfrom() didn't capture the source address; but we might
have bound to a specific address, if so we should use it */ have bound to a specific address, if so we should use it */
memcpy(&myaddr.sin_addr, &bindaddr.sin_addr, sizeof bindaddr.sin_addr); memcpy(&myaddr.sin_addr, &bindaddr.sin_addr,
sizeof bindaddr.sin_addr);
} }
/* /*
@ -622,9 +610,7 @@ main(int argc, char **argv)
request_init(&wrap_request, request_init(&wrap_request,
RQ_DAEMON, __progname, RQ_DAEMON, __progname,
RQ_FILE, fd, RQ_FILE, fd,
RQ_CLIENT_SIN, &from, RQ_CLIENT_SIN, &from, RQ_SERVER_SIN, &myaddr, 0);
RQ_SERVER_SIN, &myaddr,
0);
sock_methods(&wrap_request); sock_methods(&wrap_request);
if (hosts_access(&wrap_request) == 0) { if (hosts_access(&wrap_request) == 0) {
if (deny_severity != -1) if (deny_severity != -1)
@ -675,7 +661,6 @@ main(int argc, char **argv)
chdir("/"); /* Cygwin chroot() bug workaround */ chdir("/"); /* Cygwin chroot() bug workaround */
#endif #endif
} }
#ifdef HAVE_SETREGID #ifdef HAVE_SETREGID
setrv = setregid(pw->pw_gid, pw->pw_gid); setrv = setregid(pw->pw_gid, pw->pw_gid);
#else #else
@ -732,16 +717,18 @@ struct formats {
void (*f_recv) (struct formats *, struct tftphdr *, int); void (*f_recv) (struct formats *, struct tftphdr *, int);
int f_convert; int f_convert;
} formats[] = { } formats[] = {
{ "netascii", rewrite_access, validate_access, tftp_sendfile, tftp_recvfile, 1 }, {
{ "octet", rewrite_access, validate_access, tftp_sendfile, tftp_recvfile, 0 }, "netascii", rewrite_access, validate_access, tftp_sendfile,
{ NULL, NULL, NULL, NULL, NULL, 0 } tftp_recvfile, 1}, {
"octet", rewrite_access, validate_access, tftp_sendfile,
tftp_recvfile, 0}, {
NULL, NULL, NULL, NULL, NULL, 0}
}; };
/* /*
* Handle initial connection protocol. * Handle initial connection protocol.
*/ */
int int tftp(struct tftphdr *tp, int size)
tftp(struct tftphdr *tp, int size)
{ {
char *cp, *end; char *cp, *end;
int argn, ecode; int argn, ecode;
@ -786,21 +773,26 @@ tftp(struct tftphdr *tp, int size)
exit(0); exit(0);
} }
if (!(filename = if (!(filename =
(*pf->f_rewrite)(origfilename, tp_opcode, &errmsgptr)) ) { (*pf->f_rewrite) (origfilename, tp_opcode,
&errmsgptr))) {
nak(EACCESS, errmsgptr); /* File denied by mapping rule */ nak(EACCESS, errmsgptr); /* File denied by mapping rule */
exit(0); exit(0);
} }
if (verbosity >= 1) { if (verbosity >= 1) {
if ( filename == origfilename || !strcmp(filename, origfilename) ) if (filename == origfilename
|| !strcmp(filename, origfilename))
syslog(LOG_NOTICE, "%s from %s filename %s\n", syslog(LOG_NOTICE, "%s from %s filename %s\n",
tp_opcode == WRQ ? "WRQ" : "RRQ", tp_opcode == WRQ ? "WRQ" : "RRQ",
inet_ntoa(from.sin_addr), filename); inet_ntoa(from.sin_addr), filename);
else else
syslog(LOG_NOTICE, "%s from %s filename %s remapped to %s\n", syslog(LOG_NOTICE,
"%s from %s filename %s remapped to %s\n",
tp_opcode == WRQ ? "WRQ" : "RRQ", tp_opcode == WRQ ? "WRQ" : "RRQ",
inet_ntoa(from.sin_addr), origfilename, filename); inet_ntoa(from.sin_addr), origfilename,
filename);
} }
ecode = (*pf->f_validate)(filename, tp_opcode, pf, &errmsgptr); ecode =
(*pf->f_validate) (filename, tp_opcode, pf, &errmsgptr);
if (ecode) { if (ecode) {
nak(ecode, errmsgptr); nak(ecode, errmsgptr);
exit(0); exit(0);
@ -838,8 +830,7 @@ static int blksize_set;
/* /*
* Set a non-standard block size (c.f. RFC2348) * Set a non-standard block size (c.f. RFC2348)
*/ */
int int set_blksize(char *val, char **ret)
set_blksize(char *val, char **ret)
{ {
static char b_ret[6]; static char b_ret[6];
unsigned int sz; unsigned int sz;
@ -866,8 +857,7 @@ set_blksize(char *val, char **ret)
/* /*
* Set a power-of-two block size (nonstandard) * Set a power-of-two block size (nonstandard)
*/ */
int int set_blksize2(char *val, char **ret)
set_blksize2(char *val, char **ret)
{ {
static char b_ret[6]; static char b_ret[6];
unsigned int sz; unsigned int sz;
@ -905,8 +895,7 @@ set_blksize2(char *val, char **ret)
* For netascii mode, we don't know the size ahead of time; * For netascii mode, we don't know the size ahead of time;
* so reject the option. * so reject the option.
*/ */
int int set_tsize(char *val, char **ret)
set_tsize(char *val, char **ret)
{ {
static char b_ret[sizeof(uintmax_t) * CHAR_BIT / 3 + 2]; static char b_ret[sizeof(uintmax_t) * CHAR_BIT / 3 + 2];
uintmax_t sz; uintmax_t sz;
@ -929,8 +918,7 @@ set_tsize(char *val, char **ret)
* to be the (default) retransmission timeout, but being an * to be the (default) retransmission timeout, but being an
* integer in seconds it seems a bit limited. * integer in seconds it seems a bit limited.
*/ */
int int set_timeout(char *val, char **ret)
set_timeout(char *val, char **ret)
{ {
static char b_ret[4]; static char b_ret[4];
unsigned long to; unsigned long to;
@ -949,8 +937,7 @@ set_timeout(char *val, char **ret)
} }
/* Similar, but in microseconds. We allow down to 10 ms. */ /* Similar, but in microseconds. We allow down to 10 ms. */
int int set_utimeout(char *val, char **ret)
set_utimeout(char *val, char **ret)
{ {
static char b_ret[4]; static char b_ret[4];
unsigned long to; unsigned long to;
@ -967,11 +954,11 @@ set_utimeout(char *val, char **ret)
sprintf(*ret = b_ret, "%lu", to); sprintf(*ret = b_ret, "%lu", to);
return (1); return (1);
} }
/* /*
* Parse RFC2347 style options * Parse RFC2347 style options
*/ */
void void do_opt(char *opt, char *val, char **ap)
do_opt(char *opt, char *val, char **ap)
{ {
struct options *po; struct options *po;
char *ret; char *ret;
@ -1010,11 +997,9 @@ do_opt(char *opt, char *val, char **ap)
* *
* Return -1 on failure. * Return -1 on failure.
*/ */
int int rewrite_macros(char macro, char *output);
rewrite_macros(char macro, char *output);
int int rewrite_macros(char macro, char *output)
rewrite_macros(char macro, char *output)
{ {
char *p; char *p;
@ -1027,7 +1012,8 @@ rewrite_macros(char macro, char *output)
case 'x': case 'x':
if (output) if (output)
sprintf(output, "%08lX", (unsigned long)ntohl(from.sin_addr.s_addr)); sprintf(output, "%08lX",
(unsigned long)ntohl(from.sin_addr.s_addr));
return 8; return 8;
default: default:
@ -1038,11 +1024,11 @@ rewrite_macros(char macro, char *output)
/* /*
* Modify the filename, if applicable. If it returns NULL, deny the access. * Modify the filename, if applicable. If it returns NULL, deny the access.
*/ */
char * char *rewrite_access(char *filename, int mode, const char **msg)
rewrite_access(char *filename, int mode, const char **msg)
{ {
if (rewrite_rules) { if (rewrite_rules) {
char *newname = rewrite_string(filename, rewrite_rules, mode != RRQ, char *newname =
rewrite_string(filename, rewrite_rules, mode != RRQ,
rewrite_macros, msg); rewrite_macros, msg);
filename = newname; filename = newname;
} }
@ -1050,8 +1036,7 @@ rewrite_access(char *filename, int mode, const char **msg)
} }
#else #else
char * char *rewrite_access(char *filename, int mode, const char **msg)
rewrite_access(char *filename, int mode, const char **msg)
{ {
(void)mode; /* Avoid warning */ (void)mode; /* Avoid warning */
(void)msg; (void)msg;
@ -1119,10 +1104,8 @@ validate_access(char *filename, int mode,
*/ */
wmode = O_WRONLY | wmode = O_WRONLY |
(cancreate ? O_CREAT : 0) | (cancreate ? O_CREAT : 0) |
(unixperms ? O_TRUNC : 0) | (unixperms ? O_TRUNC : 0) | (pf->f_convert ? O_TEXT : O_BINARY);
(pf->f_convert ? O_TEXT : O_BINARY); rmode = O_RDONLY | (pf->f_convert ? O_TEXT : O_BINARY);
rmode = O_RDONLY |
(pf->f_convert ? O_TEXT : O_BINARY);
fd = open(filename, mode == RRQ ? rmode : wmode, 0666); fd = open(filename, mode == RRQ ? rmode : wmode, 0666);
if (fd < 0) { if (fd < 0) {
@ -1183,8 +1166,7 @@ validate_access(char *filename, int mode,
/* /*
* Send the requested file. * Send the requested file.
*/ */
void void tftp_sendfile(struct formats *pf, struct tftphdr *oap, int oacklen)
tftp_sendfile(struct formats *pf, struct tftphdr *oap, int oacklen)
{ {
struct tftphdr *dp; struct tftphdr *dp;
struct tftphdr *ap; /* ack packet */ struct tftphdr *ap; /* ack packet */
@ -1213,7 +1195,8 @@ tftp_sendfile(struct formats *pf, struct tftphdr *oap, int oacklen)
ap_block = ntohs((u_short) ap->th_block); ap_block = ntohs((u_short) ap->th_block);
if (ap_opcode == ERROR) { if (ap_opcode == ERROR) {
syslog(LOG_WARNING, "tftp: client does not accept options\n"); syslog(LOG_WARNING,
"tftp: client does not accept options\n");
goto abort; goto abort;
} }
if (ap_opcode == ACK) { if (ap_opcode == ACK) {
@ -1278,19 +1261,16 @@ tftp_sendfile(struct formats *pf, struct tftphdr *oap, int oacklen)
} }
/* Bail out signal handler */ /* Bail out signal handler */
void void justquit(int sig)
justquit(int sig)
{ {
(void)sig; /* Suppress unused warning */ (void)sig; /* Suppress unused warning */
exit(0); exit(0);
} }
/* /*
* Receive a file. * Receive a file.
*/ */
void void tftp_recvfile(struct formats *pf, struct tftphdr *oap, int oacklen)
tftp_recvfile(struct formats *pf, struct tftphdr *oap, int oacklen)
{ {
struct tftphdr *dp; struct tftphdr *dp;
int n, size; int n, size;
@ -1346,8 +1326,10 @@ tftp_recvfile(struct formats *pf, struct tftphdr *oap, int oacklen)
/* size = write(file, dp->th_data, n - 4); */ /* size = write(file, dp->th_data, n - 4); */
size = writeit(file, &dp, n - 4, pf->f_convert); size = writeit(file, &dp, n - 4, pf->f_convert);
if (size != (n - 4)) { /* ahem */ if (size != (n - 4)) { /* ahem */
if (size < 0) nak(errno + 100, NULL); if (size < 0)
else nak(ENOSPACE, NULL); nak(errno + 100, NULL);
else
nak(ENOSPACE, NULL);
goto abort; goto abort;
} }
} while (size == segsize); } while (size == segsize);
@ -1371,8 +1353,7 @@ tftp_recvfile(struct formats *pf, struct tftphdr *oap, int oacklen)
return; return;
} }
static const char * const errmsgs[] = static const char *const errmsgs[] = {
{
"Undefined error code", /* 0 - EUNDEF */ "Undefined error code", /* 0 - EUNDEF */
"File not found", /* 1 - ENOTFOUND */ "File not found", /* 1 - ENOTFOUND */
"Access denied", /* 2 - EACCESS */ "Access denied", /* 2 - EACCESS */
@ -1383,6 +1364,7 @@ static const char * const errmsgs[] =
"No such user", /* 7 - ENOUSER */ "No such user", /* 7 - ENOUSER */
"Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */ "Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */
}; };
#define ERR_CNT (sizeof(errmsgs)/sizeof(const char *)) #define ERR_CNT (sizeof(errmsgs)/sizeof(const char *))
/* /*
@ -1391,8 +1373,7 @@ static const char * const errmsgs[] =
* standard TFTP codes, or a UNIX errno * standard TFTP codes, or a UNIX errno
* offset by 100. * offset by 100.
*/ */
static void static void nak(int error, const char *msg)
nak(int error, const char *msg)
{ {
struct tftphdr *tp; struct tftphdr *tp;
int length; int length;