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
/* 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 const char *rcsid UNUSED =
"tftp-hpa: $Id$";
static const char *rcsid UNUSED = "tftp-hpa: $Id$";
#endif /* not lint */
/* 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);
struct tftphdr *w_init() { return rw_init(0); } /* write-behind */
struct tftphdr *r_init() { return rw_init(1); } /* read-ahead */
struct tftphdr *w_init()
{
return rw_init(0);
} /* write-behind */
struct tftphdr *r_init()
{
return rw_init(1);
} /* read-ahead */
/* init for either read-ahead or write-behind */
/* x == zero for write-behind, one for read-head */
static struct tftphdr *
rw_init(int x)
static struct tftphdr *rw_init(int x)
{
newline = 0; /* init crlf flag */
prevchar = -1;
@ -98,12 +103,10 @@ rw_init(int x)
return (struct tftphdr *)bfs[0].buf;
}
/* Have emptied current buffer by sending to net and getting ack.
Free it and return next buffer filled with data.
*/
int
readit(FILE *file, struct tftphdr **dpp, int convert)
int readit(FILE * file, struct tftphdr **dpp, int convert)
{
struct bf *b;
@ -122,8 +125,7 @@ readit(FILE *file, struct tftphdr **dpp, int convert)
* fill the input buffer, doing ascii conversions if requested
* conversions are lf -> cr,lf and cr -> cr, nul
*/
void
read_ahead(FILE *file, int convert)
void read_ahead(FILE * file, int convert)
{
int i;
char *p;
@ -148,12 +150,13 @@ read_ahead(FILE *file, int convert)
if (newline) {
if (prevchar == '\n')
c = '\n'; /* lf to cr,lf */
else c = '\0'; /* cr to cr,nul */
else
c = '\0'; /* cr to cr,nul */
newline = 0;
}
else {
} else {
c = getc(file);
if (c == EOF) break;
if (c == EOF)
break;
if (c == '\n' || c == '\r') {
prevchar = c;
c = '\r';
@ -169,8 +172,7 @@ read_ahead(FILE *file, int convert)
from the queue. Calls write_behind only if next buffer not
available.
*/
int
writeit(FILE *file, struct tftphdr **dpp, int ct, int convert)
int writeit(FILE * file, struct tftphdr **dpp, int ct, int convert)
{
bfs[current].counter = ct; /* set size of data to write */
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
* CR followed by anything else. In this case we leave it alone.
*/
int
write_behind(FILE *file, int convert)
int write_behind(FILE * file, int convert)
{
char *buf;
int count;
@ -208,7 +209,8 @@ write_behind(FILE *file, int convert)
nextone = !nextone; /* incr for next time */
buf = dp->th_data;
if (count <= 0) return -1; /* nak logic? */
if (count <= 0)
return -1; /* nak logic? */
if (convert == 0)
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 (c == '\n') /* if have cr,lf then just */
fseek(file, -1, 1); /* smash lf on top of the cr */
else
if (c == '\0') /* if have cr,nul then */
else if (c == '\0') /* if have cr,nul then */
goto skipit; /* just skip over the putc */
/* else just fall through and allow it */
}
@ -232,7 +233,6 @@ skipit:
return count;
}
/* 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
* response to packet N is really their response to packet N-1.
@ -244,9 +244,8 @@ skipit:
* when trace is active).
*/
int
synchnet(int f) /* socket to flush */
{
int synchnet(int f)
{ /* socket to flush */
int pktcount = 0;
char rbuf[PKTSIZE];
struct sockaddr_in from;
@ -273,8 +272,9 @@ synchnet(int f) /* socket to flush */
return pktcount; /* Return packets drained */
}
int pick_port_bind(int sockfd, struct sockaddr_in *myaddr, unsigned int port_range_from, unsigned int port_range_to)
int pick_port_bind(int sockfd, struct sockaddr_in *myaddr,
unsigned int port_range_from,
unsigned int port_range_to)
{
unsigned int port, firstport;
int port_range = 0;

View file

@ -62,6 +62,7 @@ int writeit(FILE *, struct tftphdr **, int, int);
extern int segsize;
#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

View file

@ -6,8 +6,7 @@
#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;
memset(&action, 0, sizeof action);

View file

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

View file

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

View file

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

View file

@ -39,8 +39,7 @@
#ifndef lint
/* 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 const char *rcsid UNUSED =
"tftp-hpa $Id$";
static const char *rcsid UNUSED = "tftp-hpa $Id$";
#endif /* not lint */
/* 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.
*/
void
tftp_sendfile(int fd, const char *name, const char *mode)
void tftp_sendfile(int fd, const char *name, const char *mode)
{
struct tftphdr *ap; /* data and ack packets */
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_block = ntohs((u_short) ap->th_block);
if (ap_opcode == ERROR) {
printf("Error code %d: %s\n", ap_block,
ap->th_msg);
printf("Error code %d: %s\n", ap_block, ap->th_msg);
goto abort;
}
if (ap_opcode == ACK) {
@ -158,8 +155,7 @@ tftp_sendfile(int fd, const char *name, const char *mode)
*/
j = synchnet(f);
if (j && trace) {
printf("discarded %d packets\n",
j);
printf("discarded %d packets\n", j);
}
/*
* RFC1129/RFC1350: We MUST NOT re-send the DATA
@ -183,8 +179,7 @@ abort:
/*
* Receive a file.
*/
void
tftp_recvfile(int fd, const char *name, const char *mode)
void tftp_recvfile(int fd, const char *name, const char *mode)
{
struct tftphdr *ap;
struct tftphdr *dp;
@ -307,8 +302,7 @@ makerequest(int request, const char *name,
return (cp - (char *)tp);
}
static const char * const errmsgs[] =
{
static const char *const errmsgs[] = {
"Undefined error code", /* 0 - EUNDEF */
"File not found", /* 1 - ENOTFOUND */
"Access denied", /* 2 - EACCESS */
@ -319,6 +313,7 @@ static const char * const errmsgs[] =
"No such user", /* 7 - ENOUSER */
"Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */
};
#define ERR_CNT (sizeof(errmsgs)/sizeof(const char *))
/*
@ -327,8 +322,7 @@ static const char * const errmsgs[] =
* standard TFTP codes, or a UNIX errno
* offset by 100.
*/
static void
nak(int error, const char *msg)
static void nak(int error, const char *msg)
{
struct tftphdr *tp;
int length;
@ -363,8 +357,7 @@ nak(int error, const char *msg)
perror("nak");
}
static void
tpacket(const char *s, struct tftphdr *tp, int n)
static void tpacket(const char *s, struct tftphdr *tp, int n)
{
static const char *opcodes[] =
{ "#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 tstop;
static void
startclock(void)
static void startclock(void)
{
(void)gettimeofday(&tstart, NULL);
}
static void
stopclock(void)
static void stopclock(void)
{
(void)gettimeofday(&tstop, NULL);
}
static void
printstats(const char *direction, unsigned long amount)
static void printstats(const char *direction, unsigned long amount)
{
double delta;
@ -429,8 +419,7 @@ printstats(const char *direction, unsigned long amount)
}
}
static void
timer(int sig)
static void timer(int sig)
{
int save_errno = errno;

View file

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

View file

@ -90,7 +90,6 @@ static int address_is_local(const struct sockaddr_in *addr)
return rv;
}
int
myrecvfrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, socklen_t * fromlen,
@ -161,8 +160,10 @@ myrecvfrom(int s, void *buf, int len, unsigned int flags,
#ifdef IP_PKTINFO
if (cmptr->cmsg_level == IPPROTO_IP &&
cmptr->cmsg_type == IP_PKTINFO) {
memcpy(&pktinfo, CMSG_DATA(cmptr), sizeof(struct in_pktinfo));
memcpy(&myaddr->sin_addr, &pktinfo.ipi_addr, sizeof(struct in_addr));
memcpy(&pktinfo, CMSG_DATA(cmptr),
sizeof(struct in_pktinfo));
memcpy(&myaddr->sin_addr, &pktinfo.ipi_addr,
sizeof(struct in_addr));
}
#endif
@ -180,8 +181,7 @@ myrecvfrom(int s, void *buf, int len, unsigned int flags,
int
myrecvfrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, int *fromlen,
struct sockaddr_in *myaddr)
struct sockaddr *from, int *fromlen, struct sockaddr_in *myaddr)
{
/* 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. */
static int genmatchstring(char *string, const char *pattern, const char *input,
const regmatch_t *pmatch, match_pattern_callback macrosub)
static int genmatchstring(char *string, const char *pattern,
const char *input, const regmatch_t * pmatch,
match_pattern_callback macrosub)
{
int (*xform) (int) = xform_null;
int len = 0;
@ -81,8 +82,16 @@ static int genmatchstring(char *string, const char *pattern, const char *input,
if (*pattern == '\\' && pattern[1] != '\0') {
char macro = pattern[1];
switch (macro) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
n = pattern[1] - '0';
if (pmatch[n].rm_so != -1) {
@ -109,8 +118,7 @@ static int genmatchstring(char *string, const char *pattern, const char *input,
break;
default:
if ( macrosub &&
(sublen = macrosub(macro, string)) >= 0 ) {
if (macrosub && (sublen = macrosub(macro, string)) >= 0) {
while (sublen--) {
len++;
if (string) {
@ -223,7 +231,8 @@ static int parseline(char *line, struct rule *r, int lineno)
r->rule_flags |= RULE_INVERSE;
break;
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);
return -1; /* Error */
break;
@ -236,7 +245,8 @@ static int parseline(char *line, struct rule *r, int lineno)
if ((r->rule_flags & (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 */
}
@ -249,7 +259,8 @@ static int parseline(char *line, struct rule *r, int lineno)
if ((rv = regcomp(&r->rx, buffer, rxflags)) != 0) {
char 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 */
}
@ -343,8 +354,9 @@ char *rewrite_string(const char *input, const struct rule *rules,
}
if (!deadman--) {
syslog(LOG_WARNING, "remap: Breaking loop, input = %s, last = %s",
input, current);
syslog(LOG_WARNING,
"remap: Breaking loop, input = %s, last = %s", input,
current);
free(current);
return NULL; /* Did not terminate! */
}
@ -369,7 +381,8 @@ char *rewrite_string(const char *input, const struct rule *rules,
}
if (ruleptr->pattern[0]) {
/* Custom error message */
len = genmatchstring(NULL, ruleptr->pattern, current,
len =
genmatchstring(NULL, ruleptr->pattern, current,
pmatch, macrosub);
newstr = tfmalloc(len + 1);
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 (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 */
} else if (ruleptr->rule_flags & RULE_RESTART) {
ruleptr = rules; /* Start from the top */
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 /* TFTPD_REMAP_H */

View file

@ -36,16 +36,6 @@
#include "config.h" /* Must be included first */
#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.
*
@ -130,12 +120,13 @@ struct options {
const char *o_opt;
int (*o_fnc) (char *, char **);
} options[] = {
{ "blksize", set_blksize },
{ "blksize2", set_blksize2 },
{ "tsize", set_tsize },
{ "timeout", set_timeout },
{ "utimeout", set_utimeout },
{ NULL, NULL }
{
"blksize", set_blksize}, {
"blksize2", set_blksize2}, {
"tsize", set_tsize}, {
"timeout", set_timeout}, {
"utimeout", set_utimeout}, {
NULL, NULL}
};
/* Simple handler for SIGHUP */
@ -146,10 +137,8 @@ static void handle_sighup(int sig)
caught_sighup = 1;
}
/* Handle timeout signal or timeout event */
void
timer(int sig)
void timer(int sig)
{
(void)sig; /* Suppress unused warning */
timeout <<= 1;
@ -158,18 +147,8 @@ timer(int sig)
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
static struct rule *
read_remap_rules(const char *file)
static struct rule *read_remap_rules(const char *file)
{
FILE *f;
struct rule *rulep;
@ -186,15 +165,16 @@ read_remap_rules(const char *file)
}
#endif
static void
set_socket_nonblock(int fd, int flag)
static void set_socket_nonblock(int fd, int flag)
{
int err;
int flags;
#if defined(HAVE_FCNTL) && defined(HAVE_O_NONBLOCK_DEFINITION)
/* Posixly correct */
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
flags = flag ? 1 : 0;
err = (ioctl(fd, FIONBIO, &flags) < 0);
@ -205,8 +185,7 @@ set_socket_nonblock(int fd, int flag)
}
}
static void
pmtu_discovery_off(int fd)
static void pmtu_discovery_off(int fd)
{
#if defined(IP_MTU_DISCOVER) && defined(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);
dt = (t1.tv_sec - t0.tv_sec)*1000000 + (t1.tv_usec - t0.tv_usec);
*timeout_us_p = timeout_left = ( dt >= timeout_us ) ? 1 : (timeout_us - dt);
dt = (t1.tv_sec - t0.tv_sec) * 1000000 + (t1.tv_usec -
t0.tv_usec);
*timeout_us_p = timeout_left =
(dt >= timeout_us) ? 1 : (timeout_us - dt);
} while (rv == -1 && err == EINTR);
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 passwd *pw;
@ -336,7 +315,8 @@ main(int argc, char **argv)
char *vp;
max_blksize = (unsigned int)strtoul(optarg, &vp, 10);
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);
exit(EX_USAGE);
}
@ -356,8 +336,9 @@ main(int argc, char **argv)
break;
case 'R':
{
if ( sscanf(optarg, "%u:%u", &portrange_from, &portrange_to) != 2 ||
portrange_from > portrange_to || portrange_to >= 65535 ) {
if (sscanf(optarg, "%u:%u", &portrange_from, &portrange_to)
!= 2 || portrange_from > portrange_to
|| portrange_to >= 65535) {
syslog(LOG_ERR, "Bad port range: %s", optarg);
exit(EX_USAGE);
}
@ -405,7 +386,7 @@ main(int argc, char **argv)
exit(0);
break;
default:
usage();
syslog(LOG_ERR, "Unknown option: '%c'", optopt);
break;
}
@ -473,10 +454,13 @@ main(int argc, char **argv)
if (*address) {
hostent = gethostbyname(address);
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);
}
memcpy(&bindaddr.sin_addr, hostent->h_addr, hostent->h_length);
memcpy(&bindaddr.sin_addr, hostent->h_addr,
hostent->h_length);
} else {
/* Default to using INADDR_ANY */
}
@ -485,12 +469,14 @@ main(int argc, char **argv)
servent = getservbyname(portptr, "udp");
if (servent) {
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);
} else if (!strcmp(portptr, "tftp")) {
/* It's TFTP, we're OK */
} else {
syslog(LOG_ERR, "cannot resolve local bind port: %s", portptr);
syslog(LOG_ERR, "cannot resolve local bind port: %s",
portptr);
exit(EX_NOINPUT);
}
}
@ -510,7 +496,8 @@ main(int argc, char **argv)
}
} else {
/* 0 is our socket descriptor */
close(1); close(2);
close(1);
close(2);
}
/* Disable path MTU discovery */
@ -560,7 +547,8 @@ main(int argc, char **argv)
#endif
/* 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)
continue; /* Signal caught, reloop */
if (rv == -1) {
@ -569,15 +557,13 @@ main(int argc, char **argv)
} else if (rv == 0) {
exit(0); /* Timeout, return to inetd */
}
#ifdef __CYGWIN__
set_socket_nonblock(fd, 1);
#endif
fromlen = sizeof(from);
n = myrecvfrom(fd, buf, sizeof(buf), 0,
(struct sockaddr *)&from, &fromlen,
&myaddr);
(struct sockaddr *)&from, &fromlen, &myaddr);
if (n < 0) {
if (E_WOULD_BLOCK(errno) || errno == EINTR) {
@ -589,14 +575,16 @@ main(int argc, char **argv)
}
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);
}
if (standalone && myaddr.sin_addr.s_addr == INADDR_ANY) {
/* myrecvfrom() didn't capture the source address; but we might
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,
RQ_DAEMON, __progname,
RQ_FILE, fd,
RQ_CLIENT_SIN, &from,
RQ_SERVER_SIN, &myaddr,
0);
RQ_CLIENT_SIN, &from, RQ_SERVER_SIN, &myaddr, 0);
sock_methods(&wrap_request);
if (hosts_access(&wrap_request) == 0) {
if (deny_severity != -1)
@ -675,7 +661,6 @@ main(int argc, char **argv)
chdir("/"); /* Cygwin chroot() bug workaround */
#endif
}
#ifdef HAVE_SETREGID
setrv = setregid(pw->pw_gid, pw->pw_gid);
#else
@ -732,16 +717,18 @@ struct formats {
void (*f_recv) (struct formats *, struct tftphdr *, int);
int f_convert;
} formats[] = {
{ "netascii", rewrite_access, validate_access, tftp_sendfile, tftp_recvfile, 1 },
{ "octet", rewrite_access, validate_access, tftp_sendfile, tftp_recvfile, 0 },
{ NULL, NULL, NULL, NULL, NULL, 0 }
{
"netascii", rewrite_access, validate_access, tftp_sendfile,
tftp_recvfile, 1}, {
"octet", rewrite_access, validate_access, tftp_sendfile,
tftp_recvfile, 0}, {
NULL, NULL, NULL, NULL, NULL, 0}
};
/*
* Handle initial connection protocol.
*/
int
tftp(struct tftphdr *tp, int size)
int tftp(struct tftphdr *tp, int size)
{
char *cp, *end;
int argn, ecode;
@ -786,21 +773,26 @@ tftp(struct tftphdr *tp, int size)
exit(0);
}
if (!(filename =
(*pf->f_rewrite)(origfilename, tp_opcode, &errmsgptr)) ) {
(*pf->f_rewrite) (origfilename, tp_opcode,
&errmsgptr))) {
nak(EACCESS, errmsgptr); /* File denied by mapping rule */
exit(0);
}
if (verbosity >= 1) {
if ( filename == origfilename || !strcmp(filename, origfilename) )
if (filename == origfilename
|| !strcmp(filename, origfilename))
syslog(LOG_NOTICE, "%s from %s filename %s\n",
tp_opcode == WRQ ? "WRQ" : "RRQ",
inet_ntoa(from.sin_addr), filename);
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",
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) {
nak(ecode, errmsgptr);
exit(0);
@ -838,8 +830,7 @@ static int blksize_set;
/*
* Set a non-standard block size (c.f. RFC2348)
*/
int
set_blksize(char *val, char **ret)
int set_blksize(char *val, char **ret)
{
static char b_ret[6];
unsigned int sz;
@ -866,8 +857,7 @@ set_blksize(char *val, char **ret)
/*
* Set a power-of-two block size (nonstandard)
*/
int
set_blksize2(char *val, char **ret)
int set_blksize2(char *val, char **ret)
{
static char b_ret[6];
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;
* so reject the option.
*/
int
set_tsize(char *val, char **ret)
int set_tsize(char *val, char **ret)
{
static char b_ret[sizeof(uintmax_t) * CHAR_BIT / 3 + 2];
uintmax_t sz;
@ -929,8 +918,7 @@ set_tsize(char *val, char **ret)
* to be the (default) retransmission timeout, but being an
* integer in seconds it seems a bit limited.
*/
int
set_timeout(char *val, char **ret)
int set_timeout(char *val, char **ret)
{
static char b_ret[4];
unsigned long to;
@ -949,8 +937,7 @@ set_timeout(char *val, char **ret)
}
/* Similar, but in microseconds. We allow down to 10 ms. */
int
set_utimeout(char *val, char **ret)
int set_utimeout(char *val, char **ret)
{
static char b_ret[4];
unsigned long to;
@ -967,11 +954,11 @@ set_utimeout(char *val, char **ret)
sprintf(*ret = b_ret, "%lu", to);
return (1);
}
/*
* Parse RFC2347 style options
*/
void
do_opt(char *opt, char *val, char **ap)
void do_opt(char *opt, char *val, char **ap)
{
struct options *po;
char *ret;
@ -1010,11 +997,9 @@ do_opt(char *opt, char *val, char **ap)
*
* Return -1 on failure.
*/
int
rewrite_macros(char macro, char *output);
int rewrite_macros(char macro, char *output);
int
rewrite_macros(char macro, char *output)
int rewrite_macros(char macro, char *output)
{
char *p;
@ -1027,7 +1012,8 @@ rewrite_macros(char macro, char *output)
case 'x':
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;
default:
@ -1038,11 +1024,11 @@ rewrite_macros(char macro, char *output)
/*
* Modify the filename, if applicable. If it returns NULL, deny the access.
*/
char *
rewrite_access(char *filename, int mode, const char **msg)
char *rewrite_access(char *filename, int mode, const char **msg)
{
if (rewrite_rules) {
char *newname = rewrite_string(filename, rewrite_rules, mode != RRQ,
char *newname =
rewrite_string(filename, rewrite_rules, mode != RRQ,
rewrite_macros, msg);
filename = newname;
}
@ -1050,8 +1036,7 @@ rewrite_access(char *filename, int mode, const char **msg)
}
#else
char *
rewrite_access(char *filename, int mode, const char **msg)
char *rewrite_access(char *filename, int mode, const char **msg)
{
(void)mode; /* Avoid warning */
(void)msg;
@ -1119,10 +1104,8 @@ validate_access(char *filename, int mode,
*/
wmode = O_WRONLY |
(cancreate ? O_CREAT : 0) |
(unixperms ? O_TRUNC : 0) |
(pf->f_convert ? O_TEXT : O_BINARY);
rmode = O_RDONLY |
(pf->f_convert ? O_TEXT : O_BINARY);
(unixperms ? O_TRUNC : 0) | (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);
if (fd < 0) {
@ -1183,8 +1166,7 @@ validate_access(char *filename, int mode,
/*
* Send the requested file.
*/
void
tftp_sendfile(struct formats *pf, struct tftphdr *oap, int oacklen)
void tftp_sendfile(struct formats *pf, struct tftphdr *oap, int oacklen)
{
struct tftphdr *dp;
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);
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;
}
if (ap_opcode == ACK) {
@ -1278,19 +1261,16 @@ tftp_sendfile(struct formats *pf, struct tftphdr *oap, int oacklen)
}
/* Bail out signal handler */
void
justquit(int sig)
void justquit(int sig)
{
(void)sig; /* Suppress unused warning */
exit(0);
}
/*
* Receive a file.
*/
void
tftp_recvfile(struct formats *pf, struct tftphdr *oap, int oacklen)
void tftp_recvfile(struct formats *pf, struct tftphdr *oap, int oacklen)
{
struct tftphdr *dp;
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 = writeit(file, &dp, n - 4, pf->f_convert);
if (size != (n - 4)) { /* ahem */
if (size < 0) nak(errno + 100, NULL);
else nak(ENOSPACE, NULL);
if (size < 0)
nak(errno + 100, NULL);
else
nak(ENOSPACE, NULL);
goto abort;
}
} while (size == segsize);
@ -1371,8 +1353,7 @@ tftp_recvfile(struct formats *pf, struct tftphdr *oap, int oacklen)
return;
}
static const char * const errmsgs[] =
{
static const char *const errmsgs[] = {
"Undefined error code", /* 0 - EUNDEF */
"File not found", /* 1 - ENOTFOUND */
"Access denied", /* 2 - EACCESS */
@ -1383,6 +1364,7 @@ static const char * const errmsgs[] =
"No such user", /* 7 - ENOUSER */
"Failure to negotiate RFC2347 options" /* 8 - EOPTNEG */
};
#define ERR_CNT (sizeof(errmsgs)/sizeof(const char *))
/*
@ -1391,8 +1373,7 @@ static const char * const errmsgs[] =
* standard TFTP codes, or a UNIX errno
* offset by 100.
*/
static void
nak(int error, const char *msg)
static void nak(int error, const char *msg)
{
struct tftphdr *tp;
int length;