Fork before performing tcpwrappers check.

Don't rely on nonstandard bsd_signal() function, instead
 require that the platform has sigaction().  This is 2001,
 after all.  This may resolve some potential portability
 problems.

Log a message if memory allocation fails, instead of dying silently.

Clean up the main dispatch loop.

Use <sysexits.h> for exit codes, if it exists.

Reformat tftpd.c to match the other files.
This commit is contained in:
hpa 2001-07-20 06:59:54 +00:00
parent e4650ab86f
commit 81822ea738
10 changed files with 862 additions and 711 deletions

16
README
View file

@ -4,6 +4,22 @@ This is tftp-hpa-0.18; this version was put out by H. Peter Anvin
===> IMPORTANT: SEE THE FILE "README.security" FOR IMPORTANT SECURITY ===> IMPORTANT: SEE THE FILE "README.security" FOR IMPORTANT SECURITY
===> CHANGES IN THIS VERSION!!!!!!!!! ===> CHANGES IN THIS VERSION!!!!!!!!!
Changes in 0.19:
Fork before performing tcpwrappers check.
Don't rely on nonstandard bsd_signal() function, instead
require that the platform has sigaction(). This is 2001,
after all. This may resolve some potential portability
problems.
Log a message if memory allocation fails, instead of dying
silently.
Clean up the main dispatch loop.
Use <sysexits.h> for exit codes, if it exists.
Changes in 0.18: Changes in 0.18:
Support (almost) arbitrary filename remappings via regular Support (almost) arbitrary filename remappings via regular
expression-based rulesets. expression-based rulesets.

View file

@ -22,4 +22,5 @@
#undef HAVE_STRUCT_IN_PKTINFO #undef HAVE_STRUCT_IN_PKTINFO
#undef HAVE_SETREUID #undef HAVE_SETREUID
#undef HAVE_SETREGID #undef HAVE_SETREGID
#undef HAVE_SYSEXITS_H
#undef WITH_REGEX #undef WITH_REGEX

View file

@ -49,6 +49,8 @@ PA_ADD_CFLAGS(-pipe)
PA_SIGSETJMP([AC_DEFINE(HAVE_SIGSETJMP)]) PA_SIGSETJMP([AC_DEFINE(HAVE_SIGSETJMP)])
AC_CHECK_HEADERS(sysexits.h)
LIBXTRA=false LIBXTRA=false
AC_SEARCH_LIBS(xmalloc, iberty, , LIBXTRA=true LIBOBJS="$LIBOBJS xmalloc.o") AC_SEARCH_LIBS(xmalloc, iberty, , LIBXTRA=true LIBOBJS="$LIBOBJS xmalloc.o")

View file

@ -42,6 +42,9 @@
* Prototypes for read-ahead/write-behind subroutines for tftp user and * Prototypes for read-ahead/write-behind subroutines for tftp user and
* server. * server.
*/ */
#ifndef TFTPSUBS_H
#define TFTPSUBS_H
#include <stdlib.h> #include <stdlib.h>
struct tftphdr *r_init(void); struct tftphdr *r_init(void);
@ -62,3 +65,5 @@ extern int segsize;
*/ */
extern void *xmalloc(size_t); extern void *xmalloc(size_t);
extern char *xstrdup(const char *); extern char *xstrdup(const char *);
#endif

View file

@ -3,7 +3,7 @@ all: tftpd
-include ../MCONFIG -include ../MCONFIG
-include ../MRULES -include ../MRULES
OBJS = tftpd.o tftpsubs.o recvfrom.o $(TFTPDOBJS) OBJS = tftpd.o tftpsubs.o recvfrom.o misc.o $(TFTPDOBJS)
tftpd: $(OBJS) tftpd: $(OBJS)
$(CC) $(LDFLAGS) $^ $(LIBS) -o $@ $(CC) $(LDFLAGS) $^ $(LIBS) -o $@

71
tftpd/misc.c Normal file
View file

@ -0,0 +1,71 @@
/* $Id$ */
/* ----------------------------------------------------------------------- *
*
* Copyright 2001 H. Peter Anvin - All Rights Reserved
*
* This program is free software available under the same license
* as the "OpenBSD" operating system, distributed at
* http://www.openbsd.org/.
*
* ----------------------------------------------------------------------- */
/*
* misc.c
*
* Minor help routines.
*/
#include <syslog.h>
#include <signal.h>
#include <string.h>
#include "tftpd.h"
/*
* Set the signal handler and flags. Basically a user-friendly
* wrapper around sigaction().
*/
void set_signal(int signum, void (*handler)(int), int flags)
{
struct sigaction sa;
memset(&sa, 0, sizeof sa);
sa.sa_handler = handler;
sigemptyset(&sa.sa_mask);
sa.sa_flags = flags;
if ( sigaction(signum, &sa, NULL) ) {
syslog(LOG_ERR, "sigaction: %m");
exit(EX_OSERR);
}
}
/*
* malloc() that syslogs an error message and bails if it fails.
*/
void *tfmalloc(size_t size)
{
void *p = malloc(size);
if ( !p ) {
syslog(LOG_ERR, "malloc: %m");
exit(EX_OSERR);
}
return p;
}
/*
* strdup() that does the equivalent
*/
char *tfstrdup(const char *str)
{
char *p = strdup(str);
if ( !p ) {
syslog(LOG_ERR, "strdup: %m");
exit(EX_OSERR);
}
return p;
}

View file

@ -22,7 +22,7 @@
#include <syslog.h> #include <syslog.h>
#include <regex.h> #include <regex.h>
#include "tftpsubs.h" #include "tftpd.h"
#include "remap.h" #include "remap.h"
#define DEADMAN_MAX_STEPS 1024 /* Timeout after this many steps */ #define DEADMAN_MAX_STEPS 1024 /* Timeout after this many steps */
@ -197,7 +197,7 @@ static int parseline(char *line, struct rule *r, int lineno)
/* Read the rewrite pattern, if any */ /* Read the rewrite pattern, if any */
if ( readescstring(buffer, &line) ) { if ( readescstring(buffer, &line) ) {
r->pattern = xstrdup(buffer); r->pattern = tfstrdup(buffer);
} else { } else {
r->pattern = ""; r->pattern = "";
} }
@ -212,7 +212,7 @@ struct rule *parserulefile(FILE *f)
char line[LINE_MAX]; char line[LINE_MAX];
struct rule *first_rule = NULL; struct rule *first_rule = NULL;
struct rule **last_rule = &first_rule; struct rule **last_rule = &first_rule;
struct rule *this_rule = xmalloc(sizeof(struct rule)); struct rule *this_rule = tfmalloc(sizeof(struct rule));
int rv; int rv;
int lineno = 0; int lineno = 0;
int err = 0; int err = 0;
@ -224,7 +224,7 @@ struct rule *parserulefile(FILE *f)
if ( rv > 0 ) { if ( rv > 0 ) {
*last_rule = this_rule; *last_rule = this_rule;
last_rule = &this_rule->next; last_rule = &this_rule->next;
this_rule = xmalloc(sizeof(struct rule)); this_rule = tfmalloc(sizeof(struct rule));
} }
} }
@ -232,7 +232,7 @@ struct rule *parserulefile(FILE *f)
if ( err ) { if ( err ) {
/* Bail on error, we have already logged an error message */ /* Bail on error, we have already logged an error message */
exit(1); exit(EX_CONFIG);
} }
return first_rule; return first_rule;
@ -241,7 +241,7 @@ struct rule *parserulefile(FILE *f)
/* Execute a rule set on a string; returns a malloc'd new string. */ /* Execute a rule set on a string; returns a malloc'd new string. */
char *rewrite_string(const char *input, const struct rule *rules, int is_put) char *rewrite_string(const char *input, const struct rule *rules, int is_put)
{ {
char *current = xstrdup(input); char *current = tfstrdup(input);
char *newstr; char *newstr;
const struct rule *ruleptr = rules; const struct rule *ruleptr = rules;
regmatch_t pmatch[10]; regmatch_t pmatch[10];
@ -272,7 +272,7 @@ char *rewrite_string(const char *input, const struct rule *rules, int is_put)
if ( ruleptr->rule_flags & RULE_REWRITE ) { if ( ruleptr->rule_flags & RULE_REWRITE ) {
len = genmatchstring(NULL, ruleptr->pattern, current, pmatch); len = genmatchstring(NULL, ruleptr->pattern, current, pmatch);
newstr = xmalloc(len+1); newstr = tfmalloc(len+1);
genmatchstring(newstr, ruleptr->pattern, current, pmatch); genmatchstring(newstr, ruleptr->pattern, current, pmatch);
free(current); free(current);
current = newstr; current = newstr;

View file

@ -15,6 +15,9 @@
* Prototypes for regular-expression based filename remapping. * Prototypes for regular-expression based filename remapping.
*/ */
#ifndef TFTPD_REMAP_H
#define TFTPD_REMAP_H
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "../config.h" #include "../config.h"
@ -30,4 +33,6 @@ struct rule *parserulefile(FILE *);
/* Execute a rule set on a string; returns a malloc'd new string. */ /* Execute a rule set on a string; returns a malloc'd new string. */
char *rewrite_string(const char *, const struct rule *, int); char *rewrite_string(const char *, const struct rule *, int);
#endif #endif /* WITH_REGEX */
#endif /* TFTPD_REMAP_H */

File diff suppressed because it is too large Load diff

54
tftpd/tftpd.h Normal file
View file

@ -0,0 +1,54 @@
/* $Id$ */
/* ----------------------------------------------------------------------- *
*
* Copyright 2001 H. Peter Anvin - All Rights Reserved
*
* This program is free software available under the same license
* as the "OpenBSD" operating system, distributed at
* http://www.openbsd.org/.
*
* ----------------------------------------------------------------------- */
/*
* tftpd.h
*
* Prototypes for various functions that are part of the tftpd server.
*/
#ifndef TFTPD_TFTPD_H
#define TFTPD_TFTPD_H
#include <stdlib.h>
#include <stdio.h>
#include "../config.h"
#ifdef HAVE_SYSEXITS_H
#include <sysexits.h>
#else
#define EX_USAGE 64 /* command line usage error */
#define EX_DATAERR 65 /* data format error */
#define EX_NOINPUT 66 /* cannot open input */
#define EX_NOUSER 67 /* addressee unknown */
#define EX_NOHOST 68 /* host name unknown */
#define EX_UNAVAILABLE 69 /* service unavailable */
#define EX_SOFTWARE 70 /* internal software error */
#define EX_OSERR 71 /* system error (e.g., can't fork) */
#define EX_OSFILE 72 /* critical OS file missing */
#define EX_CANTCREAT 73 /* can't create (user) output file */
#define EX_IOERR 74 /* input/output error */
#define EX_TEMPFAIL 75 /* temp failure; user is invited to retry */
#define EX_PROTOCOL 76 /* remote error in protocol */
#define EX_NOPERM 77 /* permission denied */
#define EX_CONFIG 78 /* configuration error */
#endif
#ifndef HAVE_SIGSETJMP
#define sigsetjmp(x,y) setjmp(x)
#define siglongjmp(x,y) longjmp(x,y)
#define sigjmp_buf jmp_buf
#endif
void set_signal(int, void (*)(int), int);
void *tfmalloc(size_t);
char *tfstrdup(const char *);
#endif