Some cleaning up in includes and ifdefs.

This commit is contained in:
Sam Hocevar 2016-02-07 00:02:29 +01:00
parent 86cba0ad56
commit 53d670f9b1
2 changed files with 79 additions and 88 deletions

View File

@ -29,7 +29,7 @@ AC_FUNC_FORK
AC_FUNC_MALLOC AC_FUNC_MALLOC
AC_TYPE_SIGNAL AC_TYPE_SIGNAL
AC_FUNC_STRFTIME AC_FUNC_STRFTIME
AC_CHECK_FUNCS([gethostbyname memset select socket strstr daemon sigaction]) AC_CHECK_FUNCS([gethostbyname memset select socket strstr daemon fork sigaction])
AC_CONFIG_FILES([Makefile]) AC_CONFIG_FILES([Makefile])
AC_OUTPUT AC_OUTPUT

139
rinetd.c
View File

@ -6,7 +6,7 @@
# define RETSIGTYPE void # define RETSIGTYPE void
#endif #endif
#ifdef WIN32 #if WIN32
# include <windows.h> # include <windows.h>
# include <winsock.h> # include <winsock.h>
# include "getopt.h" # include "getopt.h"
@ -42,39 +42,27 @@
#endif #endif
#include <ctype.h> #include <ctype.h>
#ifndef WIN32 #if WIN32
/* Windows sockets compatibility defines */
# define INVALID_SOCKET (-1)
# define SOCKET_ERROR (-1)
static int closesocket(int s) {
return close(s);
}
# define ioctlsocket ioctl
# define MAKEWORD(a, b)
# define WSAStartup(a, b) (0)
# define WSACleanup()
# ifdef __MAC__
/* The constants for these are a little screwy in the prelinked
MSL GUSI lib and we can't rebuild it, so roll with it */
# define WSAEWOULDBLOCK EWOULDBLOCK
# define WSAEAGAIN EAGAIN
# define WSAEINPROGRESS EINPROGRESS
# else
# define WSAEWOULDBLOCK EWOULDBLOCK
# define WSAEAGAIN EAGAIN
# define WSAEINPROGRESS EINPROGRESS
# endif /* __MAC__ */
# define WSAEINTR EINTR
# define SOCKET int
# define GetLastError() (errno)
typedef struct {
int dummy;
} WSADATA;
#else
/* WIN32 doesn't really have WSAEAGAIN */ /* WIN32 doesn't really have WSAEAGAIN */
# ifndef WSAEAGAIN # ifndef WSAEAGAIN
# define WSAEAGAIN WSAEWOULDBLOCK # define WSAEAGAIN WSAEWOULDBLOCK
# endif # endif
#else
/* Windows sockets compatibility defines */
# define INVALID_SOCKET (-1)
# define SOCKET_ERROR (-1)
static inline int closesocket(int s) {
return close(s);
}
# define ioctlsocket ioctl
# define WSAEWOULDBLOCK EWOULDBLOCK
# define WSAEAGAIN EAGAIN
# define WSAEINPROGRESS EINPROGRESS
# define WSAEINTR EINTR
# define SOCKET int
static inline int GetLastError(void) {
return errno;
}
#endif /* WIN32 */ #endif /* WIN32 */
#ifdef DEBUG #ifdef DEBUG
@ -160,8 +148,12 @@ static void logEvent(ConnectionInfo const *cnx, int i, int result);
static struct tm *get_gmtoff(int *tz); static struct tm *get_gmtoff(int *tz);
/* Signal handlers */ /* Signal handlers */
#if !HAVE_SIGACTION && !WIN32
static RETSIGTYPE plumber(int s); static RETSIGTYPE plumber(int s);
#endif
#if !WIN32
static RETSIGTYPE hup(int s); static RETSIGTYPE hup(int s);
#endif
static RETSIGTYPE quit(int s); static RETSIGTYPE quit(int s);
@ -169,12 +161,7 @@ int main(int argc, char *argv[])
{ {
#ifdef WIN32 #ifdef WIN32
WSADATA wsaData; WSADATA wsaData;
#endif int result = WSAStartup(MAKEWORD(1, 1), &wsaData);
int result;
#ifndef WIN32
openlog("rinetd", LOG_PID, LOG_DAEMON);
#endif
result = WSAStartup(MAKEWORD(1, 1), &wsaData);
if (result != 0) { if (result != 0) {
fprintf(stderr, "Your computer was not connected " fprintf(stderr, "Your computer was not connected "
"to the Internet at the time that " "to the Internet at the time that "
@ -183,43 +170,45 @@ int main(int argc, char *argv[])
"connection to the Internet."); "connection to the Internet.");
exit(1); exit(1);
} }
readArgs(argc, argv, &options);
#ifndef WIN32
#ifdef DEBUG
{
#elif HAVE_DAEMON
if (options.foreground || !daemon(0, 0)) {
#else #else
if (options.foreground || !fork()) { openlog("rinetd", LOG_PID, LOG_DAEMON);
#endif #endif
#ifdef HAVE_SIGACTION
readArgs(argc, argv, &options);
#if HAVE_DAEMON && !DEBUG
if (!options.foreground && daemon(0, 0) != 0) {
exit(0);
}
#elif HAVE_FORK && !DEBUG
if (!options.foreground && fork() != 0) {
exit(0);
}
#endif
#if HAVE_SIGACTION
struct sigaction act; struct sigaction act;
act.sa_handler=SIG_IGN; act.sa_handler = SIG_IGN;
sigemptyset (&act.sa_mask); sigemptyset(&act.sa_mask);
act.sa_flags=SA_RESTART; act.sa_flags = SA_RESTART;
sigaction(SIGPIPE, &act, NULL); sigaction(SIGPIPE, &act, NULL);
act.sa_handler=&hup; act.sa_handler = &hup;
sigaction(SIGHUP, &act, NULL); sigaction(SIGHUP, &act, NULL);
#else #elif !WIN32
signal(SIGPIPE, plumber); signal(SIGPIPE, plumber);
signal(SIGHUP, hup); signal(SIGHUP, hup);
#endif #endif
#endif /* WIN32 */
signal(SIGINT, quit); signal(SIGINT, quit);
signal(SIGTERM, quit); signal(SIGTERM, quit);
readConfiguration(); readConfiguration();
registerPID(); registerPID();
syslog(LOG_INFO, "Starting redirections..."); syslog(LOG_INFO, "Starting redirections...");
while (1) { while (1) {
selectPass(); selectPass();
} }
#ifndef WIN32
#ifndef DEBUG
} else {
exit(0);
#endif
}
#endif /* WIN32 */
return 0; return 0;
} }
@ -263,24 +252,25 @@ static void readConfiguration(void) {
if (!getConfLine(in, line, sizeof(line), &lnum)) { if (!getConfLine(in, line, sizeof(line), &lnum)) {
break; break;
} }
char const *bindAddress = strtok(line, " \t\r\n"); char const *currentToken = strtok(line, " \t\r\n");
if (!bindAddress) { if (!currentToken) {
syslog(LOG_ERR, "no bind address specified " syslog(LOG_ERR, "no bind address specified "
"on file %s, line %d.\n", options.conf_file, lnum); "on file %s, line %d.\n", options.conf_file, lnum);
continue; continue;
} }
if (!strcmp(bindAddress, "allow") if (!strcmp(currentToken, "allow")
|| !strcmp(bindAddress, "deny")) { || !strcmp(currentToken, "deny")) {
char const *pattern = strtok(0, " \t\r\n"); char const *pattern = strtok(0, " \t\r\n");
if (!pattern) { if (!pattern) {
syslog(LOG_ERR, "nothing to %s " syslog(LOG_ERR, "nothing to %s "
"specified on file %s, line %d.\n", bindAddress, options.conf_file, lnum); "specified on file %s, line %d.\n", currentToken, options.conf_file, lnum);
continue; continue;
} }
int bad = 0; int bad = 0;
for (char const *p = pattern; *p; ++p) { for (char const *p = pattern; *p; ++p) {
if (!strchr("0123456789?*.", *p)) { if (!strchr("0123456789?*.", *p)) {
bad = 1; bad = 1;
break;
} }
} }
if (bad) { if (bad) {
@ -302,7 +292,7 @@ static void readConfiguration(void) {
if (!allRules[allRulesCount].pattern) { if (!allRules[allRulesCount].pattern) {
goto lowMemory; goto lowMemory;
} }
allRules[allRulesCount].type = bindAddress[0] == 'a' ? allowRule : denyRule; allRules[allRulesCount].type = currentToken[0] == 'a' ? allowRule : denyRule;
if (seTotal > 0) { if (seTotal > 0) {
if (seInfo[seTotal - 1].rulesStart == 0) { if (seInfo[seTotal - 1].rulesStart == 0) {
seInfo[seTotal - 1].rulesStart = allRulesCount; seInfo[seTotal - 1].rulesStart = allRulesCount;
@ -312,7 +302,7 @@ static void readConfiguration(void) {
++globalRulesCount; ++globalRulesCount;
} }
++allRulesCount; ++allRulesCount;
} else if (!strcmp(bindAddress, "logfile")) { } else if (!strcmp(currentToken, "logfile")) {
char const *nt = strtok(0, " \t\r\n"); char const *nt = strtok(0, " \t\r\n");
if (!nt) { if (!nt) {
syslog(LOG_ERR, "no log file name " syslog(LOG_ERR, "no log file name "
@ -323,7 +313,7 @@ static void readConfiguration(void) {
if (!logFileName) { if (!logFileName) {
goto lowMemory; goto lowMemory;
} }
} else if (!strcmp(bindAddress, "pidlogfile")) { } else if (!strcmp(currentToken, "pidlogfile")) {
char const *nt = strtok(0, " \t\r\n"); char const *nt = strtok(0, " \t\r\n");
if (!nt) { if (!nt) {
syslog(LOG_ERR, "no PID log file name " syslog(LOG_ERR, "no PID log file name "
@ -334,10 +324,11 @@ static void readConfiguration(void) {
if (!pidLogFileName) { if (!pidLogFileName) {
goto lowMemory; goto lowMemory;
} }
} else if (!strcmp(bindAddress, "logcommon")) { } else if (!strcmp(currentToken, "logcommon")) {
logFormatCommon = 1; logFormatCommon = 1;
} else { } else {
/* A regular forwarding rule. */ /* A regular forwarding rule. */
char const *bindAddress = currentToken;
char const *bindPortS = strtok(0, " \t\r\n"); char const *bindPortS = strtok(0, " \t\r\n");
if (!bindPortS) { if (!bindPortS) {
syslog(LOG_ERR, "no bind port " syslog(LOG_ERR, "no bind port "
@ -385,11 +376,6 @@ static void readConfiguration(void) {
"server socket! (%m)\n"); "server socket! (%m)\n");
continue; continue;
} }
#ifndef WIN32
if (fd > maxfd) {
maxfd = fd;
}
#endif
struct sockaddr_in saddr; struct sockaddr_in saddr;
saddr.sin_family = AF_INET; saddr.sin_family = AF_INET;
memcpy(&saddr.sin_addr, &iaddr, sizeof(iaddr)); memcpy(&saddr.sin_addr, &iaddr, sizeof(iaddr));
@ -445,6 +431,11 @@ static void readConfiguration(void) {
goto lowMemory; goto lowMemory;
} }
srv->toPort = connectPort; srv->toPort = connectPort;
#ifndef WIN32
if (fd > maxfd) {
maxfd = fd;
}
#endif
++seTotal; ++seTotal;
} }
} }
@ -939,8 +930,7 @@ static int getAddress(char const *host, struct in_addr *iaddr)
return -1; return -1;
} }
#ifndef WIN32 #if !HAVE_SIGACTION && !WIN32
#ifndef HAVE_SIGACTION
RETSIGTYPE plumber(int s) RETSIGTYPE plumber(int s)
{ {
/* Just reinstall */ /* Just reinstall */
@ -948,6 +938,7 @@ RETSIGTYPE plumber(int s)
} }
#endif #endif
#if !WIN32
RETSIGTYPE hup(int s) RETSIGTYPE hup(int s)
{ {
(void)s; (void)s;
@ -955,7 +946,7 @@ RETSIGTYPE hup(int s)
/* Learn the new rules */ /* Learn the new rules */
clearConfiguration(); clearConfiguration();
readConfiguration(); readConfiguration();
#ifndef HAVE_SIGACTION #if !HAVE_SIGACTION
/* And reinstall the signal handler */ /* And reinstall the signal handler */
signal(SIGHUP, hup); signal(SIGHUP, hup);
#endif #endif