54 Commits

Author SHA1 Message Date
Sam Hocevar
b3550b8e77 Release 0.63 seems ready. 2017-07-19 19:33:15 +02:00
Sam Hocevar
9cd2114bcb Rename CHANGES and README to CHANGES.md and README.md. 2017-07-19 19:18:01 +02:00
Sam Hocevar
982c14f46f Properly handle FD_SET on Windows. Fixes #5.
On Windows, select() ignores its first argument and instead relies on a
slightly different fd_set structure that can hold file descriptors with
arbitrary values. We use the Microsoft recommended way and just increase
FD_SETSIZE at compilation time.
2017-07-19 19:18:01 +02:00
Sam Hocevar
8feb3e0513 Fix a memory corruption in the configuration file parsing. Fixes: #11. 2017-07-17 11:57:12 +02:00
Sam Hocevar
96d60bcabb Remove old debug code. Not sure why it was there. 2017-07-05 15:06:24 +02:00
Sam Hocevar
8b071acbe4 Fix a few issues in the Windows port. 2016-02-09 02:28:23 +01:00
Sam Hocevar
cb60037d58 Fix Windows cross-compilation.
Test with: ./configure --host=i686-w64-mingw32
2016-02-08 09:20:55 +01:00
Sam Hocevar
a259a67e2d Fix a probably harmless uninitialised memory access. 2016-02-08 02:49:24 +01:00
Sam Hocevar
c92c0cf4bb Stop passing server indices around.
Also fix a possible file descriptor leak when reloading configuration,
and ensure we no longer reference freed objects after reloading.
2016-02-08 02:36:23 +01:00
Sam Hocevar
53d670f9b1 Some cleaning up in includes and ifdefs. 2016-02-07 00:05:09 +01:00
Sam Hocevar
86cba0ad56 Remove patternBad() and inline its code in its only caller. 2016-02-06 23:35:43 +01:00
Sam Hocevar
e3c2b09c01 Reformat and reorganise CHANGES file with newest entries on top. 2016-02-06 18:45:16 +01:00
Sam Hocevar
65273124f3 Free all memory when quitting, and quit when SIGINT is received. 2016-02-06 18:40:02 +01:00
Sam Hocevar
830b7e5fd1 Allow to shrink the number of connections.
This will let us implement clean exit.
2016-02-06 18:31:50 +01:00
Sam Hocevar
f65f4d2506 Pedantic C fixes. 2016-02-06 18:23:50 +01:00
Sam Hocevar
f2d3ecd7f2 Reorganise log categories. 2016-02-06 18:14:14 +01:00
Sam Hocevar
d4c4c99ec7 Move a few declarations and definitions to rinetd.h. 2016-02-06 18:10:44 +01:00
Sam Hocevar
e365bf65f0 Increase buffer size to 16k instead of 1k. 2016-02-06 18:01:36 +01:00
Sam Hocevar
31f320a74d Merge openLocalFd() into handleAccept().
Also move the connection search code out of this function.
2016-02-06 18:01:36 +01:00
Sam Hocevar
568b04acd6 Merge some similar functions. 2016-02-06 18:01:36 +01:00
Sam Hocevar
af28930cd7 Merge similar ConnectionInfo members into another struct. 2016-02-06 16:54:17 +01:00
Sam Hocevar
da1fbfbf4f Make a few pointer types const. 2016-02-06 15:37:29 +01:00
Sam Hocevar
ad8874362c Merge allow and deny rules in a single array.
We now just differentiate between them with an enum.
2016-02-06 15:34:53 +01:00
Sam Hocevar
e9f4d50051 Move some magic numbers and strings to constants. 2016-02-06 15:31:32 +01:00
Sam Hocevar
13d8d29093 Merge handleCloseFromLocal() and handleCloseFromRemote(). 2016-02-06 14:58:31 +01:00
Sam Hocevar
bbf92c9e85 OS X compilation fixes in getAddress().
Also change the semantics for this function to return 0 on success.
2016-02-06 14:28:54 +01:00
Sam Hocevar
cefb09eddd Use FD_SETSIZE instead of __FD_SETSIZE. 2016-02-06 14:23:02 +01:00
Sam Hocevar
cd1de600aa Get rid of ConnectionInfo::coClosed.
What we actually care about is whether the socket file descriptors
are valid.
2016-02-06 14:20:21 +01:00
Sam Hocevar
990a368df0 Get rid of ConnectionInfo::loClosed. 2016-02-06 14:20:21 +01:00
Sam Hocevar
10935c8dc6 Get rid of ConnectionInfo::reClosed.
The variable ConnectionInfo::reFd already tells us whether the
connection is closed. This seems safer to me.
2016-02-06 14:20:20 +01:00
Sam Hocevar
4feb1ecd4f Minor refactoring in getAddress(). 2016-02-06 14:20:20 +01:00
Sam Hocevar
ed7fe3ce9d Fix patternBad() behaviour.
This function was only ever checking the first character in the string.
2016-02-06 14:20:20 +01:00
Sam Hocevar
77548712d1 Fix a memory leak on configuration reload.
This old bug was apparently introduced in rinetd 0.52.
2016-02-06 14:20:20 +01:00
Sam Hocevar
6c385facd2 Various code improvements.
Remove useless if() statements, try to factor common code, use
NULL instead of 0 where it seems to make sense, fix indentation
in switch/case for readability.
2016-02-06 14:20:20 +01:00
Sam Hocevar
bf11a9a4b5 Do the configuration parsing in a single pass. 2016-02-06 14:20:20 +01:00
Sam Hocevar
f0e2370747 Forgot to upgrade package version. 2016-02-06 14:20:20 +01:00
Sam Hocevar
1a4306008c Add Travis CI configuration file. 2016-02-06 14:20:20 +01:00
Sam Hocevar
b1d6ce5356 Fix a possible inconsistency in configuration reading.
The configuration is read in two passes (which we should stop doing one
day) and there was no check that we found the same number of forward rules
during each pass.
2016-02-03 18:54:33 +01:00
Sam Hocevar
bf40f07764 Declare a few variables closer to their actual use. 2016-02-03 18:37:19 +01:00
Sam Hocevar
51d251776b Merge pull request #4 from iwebhosting/gnu_source
Change _DEFAULT_SOURCE for _GNU_SOURCE to enable building on glibc 2.15
2016-02-03 17:35:57 +01:00
Aaron Brady
fb7eb33697 Change _DEFAULT_SOURCE for _GNU_SOURCE to enable building on glibc 2.15 2016-02-03 14:47:01 +00:00
Sam Hocevar
b7c30a067e Handle more than __FD_SETSIZE file descriptors. Fixes #1. 2016-01-13 07:47:12 +01:00
Sam Hocevar
edeb1685d2 Improve connection reallocation code.
Merge allocation and reallocation as they’re pretty much the same
thing. Make the growing factor less aggressive. Remove a goto for
immediate good karma.
2016-01-13 06:36:14 +01:00
Sam Hocevar
64a9c56c36 Reduce allocation count for connection buffers. 2016-01-13 06:12:43 +01:00
Sam Hocevar
a01d7520ec Fix a small error in the manual. 2016-01-13 05:24:44 +01:00
Sam Hocevar
9f490d550e Clean up manpage and HTML documentation.
Mostly a refactoring of HTML tags, removal of trailing spaces,
some CSS styles…
2016-01-13 05:20:45 +01:00
Sam Hocevar
54cf9badb2 Group all signal handlers together. 2016-01-12 11:46:31 +01:00
Sam Hocevar
e69b28d9a0 Get rid of safeRealloc().
This function was only used in one place, had a memory leak, and is not
type-safe. It’s better to avoid it altogether.
2016-01-12 11:44:32 +01:00
Sam Hocevar
401a67b569 Fix error reporting for out-of-range port numbers.
This was found by simply adding -Wextra to the compilation flags.
2016-01-12 11:39:36 +01:00
Sam Hocevar
9ceb167e51 Try to declare more variable in a tighter scope.
This reduces the chances of a variable being reused by an unrelated
piece of code without proper initialisaion.
2016-01-12 11:36:42 +01:00
Sam Hocevar
0090b40252 Compile with -std=c99. 2016-01-12 11:35:08 +01:00
Sam Hocevar
15ed538dcc Add indentation to preprocessor directives and remove macros. 2016-01-12 11:35:08 +01:00
Sam Hocevar
8890e03ae9 Use “srv” and “cnx” for servers and connections instead of “s” and “c”. 2016-01-12 02:54:28 +01:00
Sam Hocevar
54ca0fbd34 Better memory initialisation. 2016-01-12 02:49:29 +01:00
12 changed files with 1108 additions and 1294 deletions

21
.travis.yml Normal file
View File

@@ -0,0 +1,21 @@
sudo: false
language: c
env: VERBOSE=1
addons:
apt:
packages:
- build-essential
- pkg-config
before_install:
- ./bootstrap
os:
- linux
- osx
compiler:
- gcc
- clang

55
CHANGES
View File

@@ -1,55 +0,0 @@
Version 0.1: original version.
Version 0.2: fixed bug when several reads are necessary
on one end or the other before a write flushes them.
Fixed bug which threw away data not yet sent to the
other side on close, when running under Linux. Fixed
associated bugs that probably affected other operating
systems as well. Fixed bug causing long, perhaps
indefinite pauses when a possible connection to a
server socket went away before the accept() call,
resulting in a blocking call.
Version 0.3: fixed additional bugs relating to
the code previously used only by non-Linux OSes.
This should fix problems such as connections not
going away when they should or connections being
mysteriously closed. Most of that code is now used by
Linux also, so it is likely that rinetd is much closer
to bug-free on non-Linux platforms. Of course, I don't
actually have any to play with it on.
Version 0.4: added support for kill -1 (SIGHUP)
and specification of service names instead of
port numbers. Removed calls to realloc(), replacing
them with code that should fail gracefully without
crashing the program or breaking existing connections
when another application is hogging memory.
Version 0.5: added logging in both tab-delimited
and web-server-style formats. No longer exits if
an individual configuration file line generates
an error. Added allow and deny rules. Added
-c command line option to specify a configuration file.
Version 0.51: fixed failure to check for an open
log file before writing log entries.
Version 0.52: documentation added regarding the
ability to bind to all IP addresses, if desired,
using the special address 0.0.0.0.
Version 0.6: ported to Win32. Various compatibility
fixes were made and some minor oversights without
functional consequences were corrected.
Version 0.61: fixed a bug in 0.6 which completely
broke rinetd under Linux. Oops.
Version 0.62: fixed a potential buffer overrun;
prior versions failed to reallocate one of the
arrays correctly when reallocating memory to
accommodate more connections. Thanks to
Sam Hocevar.

73
CHANGES.md Normal file
View File

@@ -0,0 +1,73 @@
## Version 0.63
* added a `-f` flag to run in the foreground
* quit cleanly when `SIGINT` is received
* increased data buffer size for better performance
* fixed random uninitialised data accesses
* fixed a memory leak in connection reallocation
* fixed a file descriptor leak in configuration reload
* fixed configuration file parsing (many bugs including a buffer overflow)
* fixed configuration error reporting (line numbers were wrong)
* log accepted connections in addition to denied ones
* log DNS errors
* code quality refactoring: got rid of a lot of old code, used C
library functions instead of custom ones, enforced `const` correctness
## Version 0.62
fixed a potential buffer overrun; prior versions failed to reallocate one of
the arrays correctly when reallocating memory to accommodate more connections.
Thanks to Sam Hocevar.
## Version 0.61
fixed a bug in 0.6 which completely broke rinetd under Linux. Oops.
## Version 0.6
ported to Win32. Various compatibility fixes were made and some minor
oversights without functional consequences were corrected.
## Version 0.52
documentation added regarding the ability to bind to all IP addresses, if
desired, using the special address 0.0.0.0.
## Version 0.51
fixed failure to check for an open log file before writing log entries.
## Version 0.5
added logging in both tab-delimited and web-server-style formats. No longer
exits if an individual configuration file line generates an error. Added allow
and deny rules. Added -c command line option to specify a configuration file.
## Version 0.4
added support for kill -1 (SIGHUP) and specification of service names instead
of port numbers. Removed calls to realloc(), replacing them with code that
should fail gracefully without crashing the program or breaking existing
connections when another application is hogging memory.
## Version 0.3
fixed additional bugs relating to the code previously used only by non-Linux
OSes. This should fix problems such as connections not going away when they
should or connections being mysteriously closed. Most of that code is now
used by Linux also, so it is likely that rinetd is much closer to bug-free on
non-Linux platforms. Of course, I don't actually have any to play with it on.
## Version 0.2
fixed bug when several reads are necessary on one end or the other before a
write flushes them. Fixed bug which threw away data not yet sent to the other
side on close, when running under Linux. Fixed associated bugs that probably
affected other operating systems as well. Fixed bug causing long, perhaps
indefinite pauses when a possible connection to a server socket went away
before the accept() call, resulting in a blocking call.
## Version 0.1
original version.

View File

@@ -1,13 +1,20 @@
AUTOMAKE_OPTIONS = foreign dist-bzip2
EXTRA_DIST = bootstrap CHANGES index.html \
EXTRA_DIST = bootstrap CHANGES.md README.md index.html \
getopt.c getopt.h $(man_MANS) $(sysconf_DATA)
man_MANS = rinetd.8
sysconf_DATA = rinetd.conf
sbin_PROGRAMS = rinetd
rinetd_SOURCES = rinetd.c match.c match.h
rinetd_CFLAGS = -Wall -Wwrite-strings -I.
rinetd_SOURCES = rinetd.c rinetd.h match.c match.h
# _POSIX_C_SOURCE is for SA_RESTART and others
# _XOPEN_SOURCE is for struct sigaction
# _GNU_SOURCE is for h_errno and gethostbyname-related macros
rinetd_CFLAGS = -std=c99 -D_XOPEN_SOURCE -D_GNU_SOURCE -D_POSIX_C_SOURCE=200809L -Wall -Wextra -Wwrite-strings -I.
# This is for Travis CI; we dont have a testsuite yet.
test:

14
README
View File

@@ -1,14 +0,0 @@
rinetd, by Thomas Boutell. Released under the terms of the GNU General
Public License, version 2 or later.
This program is used to efficiently redirect connections from one IP
address/port combination to another. It is useful when operating virtual
servers, firewalls and the like.
To build under Unix, run "./bootstrap" to create the configuration
files, then "./configure" to create the build files, and then type
"make" to build rinetd. To install, type "make install" as root.
For documentation run "make install", then type "man rinetd" for
details. Or, read index.html in your browser.

14
README.md Normal file
View File

@@ -0,0 +1,14 @@
rinetd, by Thomas Boutell and Sam Hocevar. Released under the terms
of the GNU General Public License, version 2 or later.
This program is used to efficiently redirect connections from one IP
address/port combination to another. It is useful when operating virtual
servers, firewalls and the like.
To build under Unix, run `./bootstrap` to create the configuration
files, then `./configure` to create the build files, and then type
`make` to build rinetd. To install, type `make install` as root.
For documentation run `make install`, then type `man rinetd` for
details. Or, read `index.html` in your browser.

View File

@@ -1,6 +1,6 @@
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.52)
AC_INIT(rinetd, 0.62.0sam, sam@hocevar.net)
AC_INIT(rinetd, 0.63, sam@hocevar.net)
AC_CONFIG_AUX_DIR(.auto)
AC_CONFIG_SRCDIR([getopt.h])
AC_CONFIG_HEADER([config.h])
@@ -11,6 +11,7 @@ AC_PROG_CC
AC_PROG_INSTALL
# Checks for libraries.
AC_CHECK_LIB(ws2_32, main, [LIBS="${LIBS} -lws2_32"])
# Checks for header files.
AC_HEADER_STDC
@@ -26,10 +27,11 @@ AC_CHECK_TYPES(socklen_t, [], [],
# Checks for library functions.
AC_FUNC_FORK
AC_FUNC_MALLOC
#XXX: disabled because of cross-compilation issues
#AC_FUNC_MALLOC
AC_TYPE_SIGNAL
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_OUTPUT

View File

@@ -1,244 +1,232 @@
<html>
<head>
<title>RINETD(8)</title>
<style>
body {
background-color: #fffff0;
}
h3 {
color: #ff8888;
}
blockquote,code {
font-family: consolas, monospace, fixed;
font-size: 0.9em;
font-weight: bold;
color: #555555;
}
</style>
</head>
<body bgcolor="#FFFFF0">
<body>
<!--
Copyright (c) 1997, 1998, 1999, Thomas Boutell and Boutell.Com, Inc.
This software is released for free use under the terms of
the GNU General Public License, version 2 or higher.
-->
<table>
<tr>
<th width=33% align=left>RINETD(8)
<th width=33% align=right>Unix System Manager's Manual
<th width=33% align=right>RINETD(8)
</tr>
<tr>
<th width=33% align=left>RINETD(8)
<th width=33% align=right>Unix System Manager's Manual
<th width=33% align=right>RINETD(8)
</tr>
</table>
<p>
<font color="#FF8888" size="4">NAME</font>
<p>
rinetd -- internet ``redirection server''
<p>
<font color="#FF8888" size="4">SYNOPSIS</font>
<p>
<code>/usr/sbin/rinetd</code>
<p>
<font color="#FF8888" size="4">VERSION</font>
<p>
Version 0.62, 04/13/2003. Version 0.62 corrects a potential
buffer overflow when reallocating memory to accommodate more
connections. Upgrading is strongly recommended.
<p>
<font color="#FF8888" size="4">WHERE TO GET</font>
<p>
<strong>For Linux:</strong>
<a href="ftp://ftp.boutell.com/pub/boutell/rinetd/rinetd.tar.gz">By
anonymous FTP from ftp.boutell.com</a> in the subdirectory
<code>boutell/rinetd</code> as the file <code>rinetd.tar.gz</code>.
<p>
<strong>For Windows 95/98/NT:</strong>
<a href="ftp://ftp.boutell.com/pub/boutell/rinetd/rinetd.zip">By
anonymous FTP from ftp.boutell.com</a> in the subdirectory
<code>boutell/rinetd</code> as the file <code>rinetd.zip</code>.
<p>
<font color="#FF8888" size="4">DESCRIPTION</font>
<p>
Redirects TCP connections from one IP address and port to another. rinetd
is a single-process server which handles any number of connections to
the address/port pairs specified in the file <code>/etc/rinetd.conf</code>.
Since rinetd runs as a single process using nonblocking I/O, it is
able to redirect a large number of connections without a severe
impact on the machine. This makes it practical to run TCP services
on machines inside an IP masquerading firewall. rinetd <strong>does not
redirect FTP,</strong> because FTP requires more than one socket.
<p>
rinetd is typically launched at boot time, using the following syntax:
<p>
<code>/usr/sbin/rinetd</code>
<p>
The configuration file is found in the file
<code>/etc/rinetd.conf</code>, unless
another file is specified using the <code>-c</code> command line option.
<p>
<font color="#FF8888" size="4">FORWARDING RULES</font>
<p>
Most entries in the configuration file are forwarding rules. The
format of a forwarding rule is as follows:
<pre>
bindaddress bindport connectaddress connectport
</pre>
For example:
<pre>
206.125.69.81 80 10.1.1.2 80
</pre>
Would redirect all connections to port 80 of the "real" IP address
206.125.69.81, which could be a virtual interface, through
rinetd to port 80 of the address 10.1.1.2, which would typically
be a machine on the inside of a firewall which has no
direct routing to the outside world.
<p>
Although responding on individual interfaces rather than on all
interfaces is one of rinetd's primary features, sometimes it is
preferable to respond on all IP addresses that belong to the server.
In this situation, the special IP address <code>0.0.0.0</code>
can be used. For example:
<pre>
0.0.0.0 23 10.1.1.2 23
</pre>
Would redirect all connections to port 23, for all IP addresses
assigned to the server. This is the default behavior for most
other programs.
<p>
Service names can be specified instead of port numbers. On most systems,
service names are defined in the file /etc/services.
<p>
Both IP addresses and hostnames are accepted for
bindaddress and connectaddress.
<p>
<font color="#FF8888" size="4">ALLOW AND DENY RULES</font>
<p>
Configuration files can also contain allow and deny rules.
<p>
Allow rules which appear before the first forwarding rule are
applied globally: if at least one global allow rule exists,
and the address of a new connection does not
satisfy at least one of the global allow rules, that connection
is immediately rejected, regardless of any other rules.
<p>
Allow rules which appear after a specific forwarding rule apply
to that forwarding rule only. If at least one allow rule
exists for a particular forwarding rule, and the address of a new
connection does not satisfy at least one of the allow rules
for that forwarding rule, that connection is immediately
rejected, regardless of any other rules.
<p>
Deny rules which appear before the first forwarding rule are
applied globally: if the address of a new connection satisfies
any of the global allow rules, that connection
is immediately rejected, regardless of any other rules.
<p>
Deny rules which appear after a specific forwarding rule apply
to that forwarding rule only. If the address of a new
connection satisfies any of the deny rules for that forwarding rule,
that connection is immediately rejected, regardless of any other rules.
<p>
The format of an allow rule is as follows:
<pre>
allow pattern
</pre>
Patterns can contain the following characters: 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, . (period), ?, and *. The ? wildcard matches any one
character. The * wildcard matches any number of characters, including
zero.
<p>
For example:
<p>
<pre>
allow 206.125.69.*
</pre>
This allow rule matches all IP addresses in the 206.125.69 class C domain.
<p>
Host names are NOT permitted in allow and deny rules. The performance
cost of looking up IP addresses to find their corresponding names
is prohibitive. Since rinetd is a single process server, all other
connections would be forced to pause during the address lookup.
<p>
<font color="#FF8888" size="4">LOGGING</font>
<p>
rinetd is able to produce a log file in either of two formats:
tab-delimited and web server-style "common log format."
<p>
By default, rinetd does not produce a log file. To activate logging, add
the following line to the configuration file:
<pre>
logfile log-file-location
</pre>
Example:
<pre>
logfile /var/log/rinetd.log
</pre>
By default, rinetd logs in a simple tab-delimited format containing
the following information:
<p>
Date and time<br>
Client address<br>
Listening host
<br>
Listening port
<br>
Forwarded-to host
<br>
Forwarded-to port
<br>
Bytes received from client
<br>
Bytes sent to client
<br>
Result message
<p>
To activate web server-style "common log format" logging,
add the following line to the configuration file:
<pre>
logcommon
</pre>
<p>
<font color="#FF8888" size="4">COMMAND LINE OPTIONS</font>
<p>
The -c command line option is used to specify an alternate
configuration file.
<p>
The -f command line option is used to run rinetd in the
foreground, without forking to the background.
<p>
The -h command line option produces a short help message.
<p>
The -v command line option displays the version number.
<p>
<font color="#FF8888" size="4">REINITIALIZING RINETD</font>
<p>
The kill -1 signal (SIGHUP) can be used to cause rinetd
to reload its configuration file <strong>without</strong> interrupting existing
connections. Under Linux(tm) the process id
is saved in the file <code>/var/run/rinetd.pid</code>
to facilitate the kill -HUP. An alternate
file name can be provided by using the <code>pidlogfile</code>
configuration file option.
<p>
<font color="#FF8888" size="4">BUGS</font>
<p>
The server redirected to is not able to identify the host the
client really came from. This cannot be corrected; however,
the log produced by rinetd provides a way to obtain this
information. Under Unix, sockets would theoretically lose data when closed
with <code>SO_LINGER</code> turned off, but in Linux this is not the case
(kernel source comments support this belief on my part). On non-Linux Unix
platforms, alternate code which uses a different trick to work around
blocking <code>close()</code> is provided, but this code is untested.
<p>
The logging is inadequate. The duration of the connection should be logged.
<p>
<font color="#FF8888" size="4">LICENSE</font>
<p>
Copyright (c) 1997, 1998, 1999,
<a href="http://www.boutell.com/boutell">Thomas Boutell</a> and
<a href="http://www.boutell.com/">Boutell.Com, Inc.</a>
This software is released for free use under the terms of
the GNU General Public License, version 2 or higher.
<p>
<font color="#FF8888" size="4">CONTACT INFORMATION</font>
<p>
See <a href="http://www.boutell.com/rinetd">the rinetd web page</a>
for the latest release.
Thomas Boutell can be reached by email:
<a href="mailto:boutell@boutell.com">boutell@boutell.com</a>
<p>
<font color="#FF8888" size="4">THANKS</font>
<p>
Thanks are due to Bill Davidsen, Libor Pechachek, Sascha Ziemann,
Joel S. Noble, the Apache Group, and many others who have contributed
advice, encouragement and/or source code to this and other open
software projects.
<h3>NAME</h3>
<p> rinetd -- internet “redirection server”</p>
<h3>SYNOPSIS</h3>
<p> <code>/usr/sbin/rinetd</code></p>
<h3>VERSION</h3>
<p> Version 0.63, 2017/07/19. Version 0.63 corrects several bugs including a
denial of service security issue, and greatly improves performance, ease of
use, and portability. </p>
<h3>WHERE TO GET</h3>
<p> <a href="https://github.com/samhocevar/rinetd/releases">By HTTP on
GitHub</a> in the releases section. </p>
<h3>DESCRIPTION</h3>
<p> Redirects TCP connections from one IP address and port to another. rinetd
is a single-process server which handles any number of connections to the
address/port pairs specified in the file <code>/etc/rinetd.conf</code>. Since
rinetd runs as a single process using nonblocking I/O, it is able to redirect
a large number of connections without a severe impact on the machine. This
makes it practical to run TCP services on machines inside an IP masquerading
firewall. rinetd <strong>does not redirect FTP</strong>, because FTP requires
more than one socket. </p>
<p> rinetd is typically launched at boot time, using the following syntax: </p>
<blockquote>/usr/sbin/rinetd</blockquote>
<p> The configuration file is found in the file <code>/etc/rinetd.conf</code>,
unless another file is specified using the <code>-c</code> command line option.
</p>
<h3>FORWARDING RULES</h3>
<p> Most entries in the configuration file are forwarding rules. The format of
a forwarding rule is as follows:
<blockquote>bindaddress bindport connectaddress connectport</blockquote>
For example:
<blockquote>206.125.69.81 80 10.1.1.2 80</blockquote>
Would redirect all connections to port 80 of the “real” IP address
206.125.69.81, which could be a virtual interface, through rinetd to port 80
of the address 10.1.1.2, which would typically be a machine on the inside of a
firewall which has no direct routing to the outside world. </p>
<p> Although responding on individual interfaces rather than on all interfaces
is one of rinetd's primary features, sometimes it is preferable to respond on
all IP addresses that belong to the server. In this situation, the special IP
address <code>0.0.0.0</code> can be used. For example:
<blockquote>0.0.0.0 23 10.1.1.2 23</blockquote>
Would redirect all connections to port 23, for all IP addresses assigned to the
server. This is the default behavior for most other programs. </p>
<p> Service names can be specified instead of port numbers. On most systems,
service names are defined in the file /etc/services. </p>
<p> Both IP addresses and hostnames are accepted for bindaddress and
connectaddress. </p>
<h3>ALLOW AND DENY RULES</h3>
<p> Configuration files can also contain allow and deny rules. </p>
<p> Allow rules which appear before the first forwarding rule are applied
globally: if at least one global allow rule exists, and the address of a new
connection does not satisfy at least one of the global allow rules, that
connection is immediately rejected, regardless of any other rules. </p>
<p> Allow rules which appear after a specific forwarding rule apply to that
forwarding rule only. If at least one allow rule exists for a particular
forwarding rule, and the address of a new connection does not satisfy at least
one of the allow rules for that forwarding rule, that connection is immediately
rejected, regardless of any other rules. </p>
<p> Deny rules which appear before the first forwarding rule are applied
globally: if the address of a new connection satisfies any of the global deny
rules, that connection is immediately rejected, regardless of any other rules.
</p>
<p> Deny rules which appear after a specific forwarding rule apply to that
forwarding rule only. If the address of a new connection satisfies any of the
deny rules for that forwarding rule, that connection is immediately rejected,
regardless of any other rules. </p>
<p> The format of an allow rule is as follows:
<blockquote>allow pattern</blockquote>
Patterns can contain the following characters: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, .
(period), ?, and *. The ? wildcard matches any one character. The * wildcard
matches any number of characters, including zero. </p>
<p> For example: </p>
<p> <blockquote>allow 206.125.69.*</blockquote>
This allow rule matches all IP addresses in the 206.125.69 class C domain. </p>
<p> Host names are NOT permitted in allow and deny rules. The performance cost
of looking up IP addresses to find their corresponding names is prohibitive.
Since rinetd is a single process server, all other connections would be forced
to pause during the address lookup. </p>
<h3>LOGGING</h3>
<p> rinetd is able to produce a log file in either of two formats:
tab-delimited and web server-style “common log format.” </p>
<p> By default, rinetd does not produce a log file. To activate logging, add
the following line to the configuration file:
<blockquote>logfile log-file-location</blockquote>
Example:
<blockquote>logfile /var/log/rinetd.log</blockquote>
By default, rinetd logs in a simple tab-delimited format containing the
following information: </p>
<ul>
<li> Date and time </li>
<li> Client address<br />
<li> Listening host </li>
<li> Listening port </li>
<li> Forwarded-to host </li>
<li> Forwarded-to port </li>
<li> Bytes received from client </li>
<li> Bytes sent to client </li>
<li> Result message </li>
</ul>
<p> To activate web server-style “common log format” logging, add the following
line to the configuration file:
<blockquote>logcommon</blockquote>
</p>
<h3>COMMAND LINE OPTIONS</h3>
<p> The -c command line option is used to specify an alternate configuration
file. </p>
<p> The -f command line option is used to run rinetd in the foreground, without
forking to the background. </p>
<p> The -h command line option produces a short help message. </p>
<p> The -v command line option displays the version number. </p>
<h3>REINITIALIZING RINETD</h3>
<p> The kill -1 signal (SIGHUP) can be used to cause rinetd to reload
its configuration file <strong>without</strong> interrupting existing
connections. Under Linux(tm) the process id is saved in the file
<code>/var/run/rinetd.pid</code> to facilitate the kill -HUP. An alternate file
name can be provided by using the <code>pidlogfile</code> configuration file
option. </p>
<h3>BUGS</h3>
<p> The server redirected to is not able to identify the host the client
really came from. This cannot be corrected; however, the log produced by
rinetd provides a way to obtain this information. Under Unix, sockets would
theoretically lose data when closed with <code>SO_LINGER</code> turned off, but
in Linux this is not the case (kernel source comments support this belief on
my part). On non-Linux Unix platforms, alternate code which uses a different
trick to work around blocking <code>close()</code> is provided, but this code
is untested. </p>
<p> The logging is inadequate. The duration of the connection should be logged.
</p>
<h3>LICENSE</h3>
<p> Copyright (c) 1997, 1998, 1999,
<a href="http://www.boutell.com/boutell">Thomas Boutell</a> and
<a href="http://www.boutell.com/">Boutell.Com, Inc.</a> </p>
<p> Copyright (c) 2003&mdash;2017 <a href="https://github.com/samhocevar">Sam
Hocevar</a> </p>
<p> This software is released for free use under the terms of the GNU
General Public License, version 2 or higher. </p>
<h3>CONTACT INFORMATION</h3>
<p> See <a href="https://github.com/samhocevar/rinetd/releases">the rinetd web
page</a> for the latest release. </p>
<p> Thomas Boutell can be reached by email: <a
href="mailto:boutell@boutell.com">boutell@boutell.com</a> </p>
<p> Sam Hocevar can be reached by email: <a
href="mailto:sam@hocevar.net">sam@hocevar.net</a> </p>
<h3>THANKS</h3>
<p> Thanks are due to Bill Davidsen, Libor Pechachek, Sascha Ziemann, Joel
S. Noble, the Apache Group, and many others who have contributed advice,
encouragement and/or source code to this and other open software projects. </p>
</body>
</html>

View File

@@ -20,7 +20,6 @@ int matchNoCase(char const *sorig, char const *p)
int matchBody(char const *sorig, char const *p, int nocase)
{
static int dummy = 0;
/* Algorithm:
Word separator: *. End-of-string
@@ -56,11 +55,6 @@ int matchBody(char const *sorig, char const *p, int nocase)
char const *s = sorig;
int escaped = 0;
if (strstr(p, "WS-0000")) {
if (strstr(s, "ws_ftp_pro.html")) {
dummy = 1;
}
}
while (1) {
char const *word;
int wordLen;

View File

@@ -2,7 +2,7 @@
.\" This software is released for free use under the terms of
.\" the GNU Public License, version 2 or higher.
.\"
.Dd February 18, 1999
.Dd July 19, 2017
.Dt RINETD 8
.Os LINUX
.Sh NAME
@@ -12,12 +12,12 @@
.Sh SYNOPSIS
.Nm /usr/sbin/rinetd
.Sh VERSION
Version 0.62, 04/14/2003.
Version 0.63, 2017/07/19.
.Sh DESCRIPTION
.Nm rinetd
redirects TCP connections from one IP address and port to another. rinetd
is a single-process server which handles any number of connections to
the address/port pairs specified in the file /etc/rinetd.conf.
the address/port pairs specified in the file /etc/rinetd.conf.
Since rinetd runs as a single process using nonblocking I/O, it is
able to redirect a large number of connections without a severe
impact on the machine. This makes it practical to run TCP services
@@ -29,7 +29,7 @@ rinetd is typically launched at boot time, using the following syntax:
/usr/sbin/rinetd
.Pp
The configuration file is found in the file /etc/rinetd.conf, unless
another file is specified using the -c command line option.
another file is specified using the -c command line option.
.Sh FORWARDING RULES
Most entries in the configuration file are forwarding rules. The
format of a forwarding rule is as follows:
@@ -42,12 +42,12 @@ For example:
.Pp
Would redirect all connections to port 80 of the "real" IP address
206.125.69.81, which could be a virtual interface, through
rinetd to port 80 of the address 10.1.1.2, which would typically
rinetd to port 80 of the address 10.1.1.2, which would typically
be a machine on the inside of a firewall which has no
direct routing to the outside world.
.Pp
Although responding on individual interfaces rather than on all
interfaces is one of rinetd's primary features, sometimes it is
interfaces is one of rinetd's primary features, sometimes it is
preferable to respond on all IP addresses that belong to the server.
In this situation, the special IP address 0.0.0.0
can be used. For example:
@@ -65,15 +65,15 @@ Both IP addresses and hostnames are accepted for
bindaddress and connectaddress.
.Pp
.Sh ALLOW AND DENY RULES
Configuration files can also contain allow and deny rules.
Configuration files can also contain allow and deny rules.
.Pp
Allow rules which appear before the first forwarding rule are
applied globally: if at least one global allow rule exists,
and the address of a new connection does not
satisfy at least one of the global allow rules, that connection
is immediately rejected, regardless of any other rules.
is immediately rejected, regardless of any other rules.
.Pp
Allow rules which appear after a specific forwarding rule apply
Allow rules which appear after a specific forwarding rule apply
to that forwarding rule only. If at least one allow rule
exists for a particular forwarding rule, and the address of a new
connection does not satisfy at least one of the allow rules
@@ -82,12 +82,12 @@ rejected, regardless of any other rules.
.Pp
Deny rules which appear before the first forwarding rule are
applied globally: if the address of a new connection satisfies
any of the global allow rules, that connection
is immediately rejected, regardless of any other rules.
any of the global deny rules, that connection
is immediately rejected, regardless of any other rules.
.Pp
Deny rules which appear after a specific forwarding rule apply
Deny rules which appear after a specific forwarding rule apply
to that forwarding rule only. If the address of a new
connection satisfies any of the deny rules for that forwarding rule,
connection satisfies any of the deny rules for that forwarding rule,
that connection is immediately rejected, regardless of any other rules.
.Pp
The format of an allow rule is as follows:
@@ -97,7 +97,7 @@ allow pattern
Patterns can contain the following characters: 0, 1, 2, 3, 4, 5,
6, 7, 8, 9, . (period), ?, and *. The ? wildcard matches any one
character. The * wildcard matches any number of characters, including
zero.
zero.
.Pp
For example:
.Pp
@@ -114,7 +114,7 @@ connections would be forced to pause during the address lookup.
rinetd is able to produce a log file in either of two formats:
tab-delimited and web server-style "common log format."
.Pp
By default, rinetd does not produce a log file. To activate logging, add
By default, rinetd does not produce a log file. To activate logging, add
the following line to the configuration file:
.Pp
logfile log-file-location
@@ -173,20 +173,26 @@ use a single TCP socket. This rules out FTP.
The server redirected to is not able to identify the host the
client really came from. This cannot be corrected; however,
the log produced by rinetd provides a way to obtain this
information. Under Unix, Sockets would theoretically lose data when closed
with SO_LINGER turned off, but in Linux this is not the case (kernel
source comments support this belief on my part). On non-Linux Unix platforms,
information. Under Unix, Sockets would theoretically lose data when closed
with SO_LINGER turned off, but in Linux this is not the case (kernel
source comments support this belief on my part). On non-Linux Unix platforms,
alternate code which uses a different trick to work around blocking close()
is provided, but this code is untested. The logging is inadequate.
The duration of each connection should be logged.
.Sh LICENSE
Copyright (c) 1997, 1998, 1999, Thomas Boutell and Boutell.Com, Inc.
.Pp
Copyright (c) 2003\-2017 Sam Hocevar
.Pp
This software is released for free use under the terms of
the GNU Public License, version 2 or higher. NO WARRANTY
IS EXPRESSED OR IMPLIED. USE THIS SOFTWARE AT YOUR OWN RISK.
.Sh CONTACT INFORMATION
See http://www.boutell.com/rinetd/ for the latest release.
See https://github.com/samhocevar/rinetd/releases for the latest release.
.Pp
Thomas Boutell can be reached by email: boutell@boutell.com
.Pp
Sam Hocevar can be reached by email: sam@hocevar.net
.Sh THANKS
Thanks are due to Bill Davidsen, Libor Pechachek, Sascha Ziemann, the
Apache Group, and many others who have contributed advice

1637
rinetd.c

File diff suppressed because it is too large Load Diff

69
rinetd.h Normal file
View File

@@ -0,0 +1,69 @@
/* Constants */
static int const RINETD_BUFFER_SIZE = 16384;
static int const RINETD_LISTEN_BACKLOG = 128;
#define RINETD_CONFIG_FILE "/etc/rinetd.conf"
#define RINETD_PID_FILE "/var/run/rinetd.pid"
/* Program state */
enum ruleType {
allowRule,
denyRule,
};
typedef struct _rule Rule;
struct _rule
{
char *pattern;
int type;
};
typedef struct _server_info ServerInfo;
struct _server_info {
SOCKET fd;
/* In network order, for network purposes */
struct in_addr localAddr;
unsigned short localPort;
/* In ASCII and local byte order, for logging purposes */
char *fromHost, *toHost;
int fromPort, toPort;
/* Offset and count into list of allow and deny rules. Any rules
prior to globalAllowRules and globalDenyRules are global rules. */
int rulesStart, rulesCount;
};
typedef struct _socket Socket;
struct _socket
{
SOCKET fd;
/* recv: received on this socket
sent: sent to this socket from the other buffer */
int recvPos, sentPos;
int recvBytes, sentBytes;
char *buffer;
};
typedef struct _connection_info ConnectionInfo;
struct _connection_info
{
Socket remote, local;
struct in_addr reAddresses;
int coClosing;
int coLog;
ServerInfo const *server; // only useful for logEvent
};
/* Option parsing */
typedef struct _rinetd_options RinetdOptions;
struct _rinetd_options
{
char const *conf_file;
int foreground;
};