From 68c0102aa6a7defde48910226b863b8e581b8897 Mon Sep 17 00:00:00 2001 From: hpa Date: Tue, 7 Aug 2001 19:19:29 +0000 Subject: [PATCH] Portability improvements. --- README | 3 +++ config.h.in | 1 + configure.in | 1 + lib/bsdsignal.c | 8 +++++--- tftp/tftpsubs.c | 1 + tftpd/tftpd.c | 42 ++++++++++++++++++++++++------------------ 6 files changed, 35 insertions(+), 21 deletions(-) diff --git a/README b/README index e1c2012..1711d7d 100644 --- a/README +++ b/README @@ -21,6 +21,9 @@ improvements. Changes in 0.21: Support running in standalone mode, without inetd. + Even more portability improvements. Now known to compile + and run on Solaris 5, 5.1, 6 and 7 as well as 8. + Changes in 0.20: Portability improvements. Now known to compile and run on diff --git a/config.h.in b/config.h.in index 1b328fe..7cd48c0 100644 --- a/config.h.in +++ b/config.h.in @@ -22,6 +22,7 @@ #undef HAVE_STRUCT_IN_PKTINFO #undef HAVE_SETREUID #undef HAVE_SETREGID +#undef HAVE_SETSID #undef HAVE_SYSEXITS_H #undef HAVE_STRINGS_H #undef HAVE_LIBGEN_H diff --git a/configure.in b/configure.in index 1df071e..e1e19b1 100644 --- a/configure.in +++ b/configure.in @@ -19,6 +19,7 @@ AC_SEARCH_LIBS(gethostbyname, [nsl resolv], , [AC_MSG_ERROR(gethostbyname not fo AC_SEARCH_LIBS(inet_aton, [nsl resolv], , [AC_MSG_ERROR(inet_aton not found)]) AC_SEARCH_LIBS(herror, [nsl resolv], , [AC_MSG_ERROR(herror not found)]) +AC_CHECK_FUNCS(setsid) AC_CHECK_FUNCS(recvmsg) AC_CHECK_FUNCS(setreuid) AC_CHECK_FUNCS(setregid) diff --git a/lib/bsdsignal.c b/lib/bsdsignal.c index 27e3a34..bc3ceb5 100644 --- a/lib/bsdsignal.c +++ b/lib/bsdsignal.c @@ -8,17 +8,19 @@ #include #include -void (*bsd_signal(int signum, void (*handler)(int)))(int); +void (*bsd_signal(int, void (*)(int)))(int); + +void (*bsd_signal(int signum, void (*handler)(int)))(int) { struct sigaction action, oldaction; memset(&action, 0, sizeof action); action.sa_handler = handler; sigemptyset(&action.sa_mask); - sigaddset(&act.sa_mask, signum); + sigaddset(&action.sa_mask, signum); action.sa_flags = SA_RESTART; - if (sigaction(hander, action, &oldaction) == -1) { + if (sigaction(signum, &action, &oldaction) == -1) { #ifdef SIG_ERR return SIG_ERR; #else diff --git a/tftp/tftpsubs.c b/tftp/tftpsubs.c index 8a6e654..8b739cf 100644 --- a/tftp/tftpsubs.c +++ b/tftp/tftpsubs.c @@ -63,6 +63,7 @@ static const char *rcsid UNUSED = #include #include #include +#include #include #include "../config.h" diff --git a/tftpd/tftpd.c b/tftpd/tftpd.c index 2a34501..6684afd 100644 --- a/tftpd/tftpd.c +++ b/tftpd/tftpd.c @@ -352,7 +352,7 @@ main(int argc, char **argv) } } - if ( bind(fd, &bindaddr, sizeof bindaddr) ) { + if (bind(fd, (struct sockaddr *)&bindaddr, sizeof bindaddr) < 0) { syslog(LOG_ERR, "cannot bind to local socket: %m"); exit(EX_OSERR); } @@ -367,8 +367,13 @@ main(int argc, char **argv) exit(EX_OSERR); } close(0); close(1); close(2); +#ifdef HAVE_SETSID setsid(); +#endif } + } else { + /* 0 is our socket descriptor */ + close(1); close(2); } /* This means we don't want to wait() for children */ @@ -445,6 +450,9 @@ main(int argc, char **argv) /* Child process: handle the actual request here */ + /* Ignore SIGHUP */ + set_signal(SIGHUP, SIG_IGN, 0); + #ifdef HAVE_TCPWRAPPERS /* Verify if this was a legal request for us. This has to be done before the chroot, while /etc is still accessible. */ @@ -465,7 +473,19 @@ main(int argc, char **argv) inet_ntoa(from.sin_addr)); } #endif + + /* Close file descriptors we don't need */ + close(fd); + /* Get a socket. This has to be done before the chroot(), since + some systems require access to /dev to create a socket. */ + + peer = socket(AF_INET, SOCK_DGRAM, 0); + if (peer < 0) { + syslog(LOG_ERR, "socket: %m"); + exit(EX_IOERR); + } + /* Chroot and drop privileges */ if (secure && chroot(".")) { @@ -492,32 +512,18 @@ main(int argc, char **argv) exit(EX_OSERR); } - /* Ignore SIGHUP */ - set_signal(SIGHUP, SIG_IGN, 0); - - /* Close file descriptors we don't need */ - close(fd); - close(0); - close(1); - close(2); - /* Other basic setup */ from.sin_family = AF_INET; alarm(0); /* Process the request... */ - peer = socket(AF_INET, SOCK_DGRAM, 0); - if (peer < 0) { - syslog(LOG_ERR, "socket: %m"); - exit(EX_IOERR); - } myaddr.sin_port = htons(0); /* We want a new local port */ - if (bind(peer, (struct sockaddr *)&myaddr, sizeof (myaddr)) < 0) { + if (bind(peer, (struct sockaddr *)&myaddr, sizeof myaddr) < 0) { syslog(LOG_ERR, "bind: %m"); exit(EX_IOERR); } - if (connect(peer, (struct sockaddr *)&from, sizeof(from)) < 0) { + if (connect(peer, (struct sockaddr *)&from, sizeof from) < 0) { syslog(LOG_ERR, "connect: %m"); exit(EX_IOERR); } @@ -634,7 +640,7 @@ tftp(struct tftphdr *tp, int size) else (*pf->f_send)(pf, NULL, 0); } - exit(EX_SOFTWARE); /* We should never get here */ + exit(0); /* Request completed */ } static int blksize_set;