74 Commits

Author SHA1 Message Date
Sam Hocevar
d4e0a60119 Fix a parsing bug in the rules that would ignore the very first rule. See #23. 2021-03-02 15:25:39 +01:00
Sam Hocevar
af88e6fd4f Release version 0.73. 2021-02-19 09:56:52 +01:00
Sam Hocevar
ff41723590 Improve error reporting. Fixes #15.
- Add an error message when the configuration file could not be found.
 - Log to stderr when running in the foreground.
2021-02-19 09:12:29 +01:00
Sam Hocevar
d3dd35327f Do not log to syslog when running in the foreground. Addresses #15. 2021-02-19 08:55:45 +01:00
Sam Hocevar
49bf544bad Fix another IPv6 parsing issue. Addresses #20. 2021-02-17 14:15:54 +01:00
Sam Hocevar
1060048fe8 Refactor getaddrinfo error reporting. 2021-02-17 08:21:58 +01:00
Sam Hocevar
e3b47d086a Fix some hostnames (e.g. 123.example.com) being wrongfully parsed as IPv6. 2021-02-17 08:17:03 +01:00
Sam Hocevar
f91e34c3af Release version 0.72. 2021-02-16 09:27:25 +01:00
Sam Hocevar
5e8be95d8a Fix a configuration parsing bug. 2021-02-16 09:22:25 +01:00
Sam Hocevar
7887c17096 Fix Mac OS X build. 2021-02-14 14:41:15 +01:00
Sam Hocevar
432aa4d230 Release version 0.71. 2021-02-14 12:12:53 +01:00
Sam Hocevar
b00f175fa4 Fix HTML documentation generation. 2021-02-14 12:09:26 +01:00
Sam Hocevar
6a1e7e75a3 Prepare version 0.71. 2021-02-14 12:06:31 +01:00
Sam Hocevar
7f76652af9 Improve documentation. 2021-02-14 12:05:29 +01:00
Sam Hocevar
bb0d64ed5d Refactor code to avoid duplication. 2021-02-14 11:59:16 +01:00
Sam Hocevar
3b33f45925 Merge branch 'feature-ipv6-support' into main 2021-02-14 11:41:54 +01:00
Sam Hocevar
878c804d32 Update changelog for upcoming version 0.71. 2021-02-14 11:39:04 +01:00
Sam Hocevar
f9f5aeda80 Properly release the lexer context after reading the configuration. 2021-02-14 11:20:37 +01:00
Sam Hocevar
655584ad78 Fix a format string issue on Windows. 2021-02-14 10:10:47 +01:00
Sam Hocevar
4c5a7a1a22 Fix deny/allow rules to support IPv6 addresses. 2021-02-14 10:08:09 +01:00
Sam Hocevar
7790a617d8 Remove all legacy IPv4 code. 2021-02-14 10:08:09 +01:00
Sam Hocevar
7781e97734 Remove deprecated getAddress() function now that we use getaddrinfo() ubiquitously. 2021-02-14 09:18:49 +01:00
Sam Hocevar
79bafee414 Allow connecting to remote IPv6 addresses. 2021-02-14 09:18:07 +01:00
Sam Hocevar
b7fcf65a97 Use getaddrinfo() to resolve source address. 2021-02-14 08:45:03 +01:00
Sam Hocevar
c9de417f2e Use sockaddr_storage for IPv6 compatibility. 2021-02-14 08:45:03 +01:00
Sam Hocevar
26b0e63bf5 Get rid of useless fromProtocol struct member. 2021-02-14 08:45:03 +01:00
Sam Hocevar
f58db7ea8d Add a helper function to retrieve socket type from protocol. 2021-02-14 08:09:08 +01:00
Sam Hocevar
7ef50421c0 Remove the protocolType enum in favour of IPPROTO_TCP/IPPROTO_UDP. 2021-02-14 08:06:02 +01:00
Sam Hocevar
0ef4f1042f Add a helper function to retrieve port from addrinfo. 2021-02-14 07:53:40 +01:00
Sam Hocevar
f7cd21f91f Fix getaddrinfo() hint initialisation issues. 2021-02-14 07:51:46 +01:00
Sam Hocevar
a5905bbcea Fix Windows build. 2021-02-14 00:52:15 +01:00
Sam Hocevar
a1d2c297ba Merge branch 'main' into feature-ipv6-support 2021-02-13 23:11:35 +01:00
Sam Hocevar
5a70f2bf4e Allow to build application with Visual Studio. 2021-02-13 23:02:03 +01:00
Sam Hocevar
7e287f5913 Create log file if it does not exist already. 2021-02-13 21:52:15 +01:00
Sam Hocevar
4f64342ed5 Allow listening on IPv6 addresses. 2021-02-13 21:48:51 +01:00
Sam Hocevar
44f973d9fd Allow DNS labels to start with an underscore. 2021-02-13 18:43:33 +01:00
Sam Hocevar
a8ca595620 Merge branch 'master' into feature-ipv6-support 2021-02-13 12:52:44 +01:00
Sam Hocevar
2f8fd87518 Allow hostnames to start with a ‘_’, e.g. ‘_http.example.com’. 2021-02-13 12:15:39 +01:00
Sam Hocevar
04d1b74e24 Allow service names that contain dashes, e.g. snmp-trap. 2021-02-13 12:02:27 +01:00
Sam Hocevar
2681d17eb4 Fix minor compilation warnings on mingw64. 2021-02-13 11:44:26 +01:00
Sam Hocevar
8bde6f6259 Distribute COPYING file with source tarballs. 2021-01-08 18:41:05 +01:00
Sam Hocevar
ae9af9d030 Add GPLv2 license text. Fixes #19. 2021-01-08 18:39:12 +01:00
Sam Hocevar
ebd31cc72e Improve code readability by using enum typedefs. 2019-07-05 10:35:39 +02:00
Sam Hocevar
da497404ab Use uint64_t for data usage statistics.
It’s not uncommon nowadays that a single connection uses more than 2GiB
of data. This bug was only affecting logging and did not affect proper
behaviour of the program.
2019-07-05 10:28:46 +02:00
Sam Hocevar
d9f661ee5d Use uint16_t for port numbers.
This cleans up the mix of int and unsigned short for these variables.
2019-07-05 10:14:15 +02:00
Sam Hocevar
0ea83281e3 Merge branch 'master' into feature-ipv6-support 2017-11-28 09:33:48 +01:00
Sam Hocevar
a37e13c910 Forgot to put src/Makefile.am in source control, lol. Fixes #13. 2017-11-28 09:27:19 +01:00
Sam Hocevar
3e18951d7a Merge branch 'master' into feature-ipv6-support 2017-09-11 19:28:53 +02:00
Sam Hocevar
9d140d8f87 Move C source files to an src/ subdirectory.
It was a bit crowded in here. This makes the source tree a bit nicer.
2017-09-11 19:25:26 +02:00
Sam Hocevar
b5f75eae99 Version 0.70. 2017-09-09 19:28:12 +02:00
Sam Hocevar
be9f46e7f1 Rewrite a lot of the documentation. 2017-09-09 19:25:06 +02:00
Sam Hocevar
dd17267afe Start working on IPv6 support. 2017-09-08 19:42:18 +02:00
Sam Hocevar
5d0e44c451 Support for source address binding.
This feature was independently implemented by SuSE as part of the changes in
https://build.opensuse.org/package/view_file/server:proxy/rinetd/rinetd-syslog.patch?expand=1
and as mentioned in #9, but our new configuration parser makes the change a
lot less intrusive.
2017-09-08 19:22:19 +02:00
Sam Hocevar
173a738be0 Fix regression: hostnames in bind address were broken. 2017-09-08 19:12:53 +02:00
Sam Hocevar
37437a5c5b Improve UDP connection handling.
Send a zero-sized UDP packet when first accessing the server; this makes
TCP→UDP→TCP redirects work in both directions.

Properly close UDP connections when the other end disconnects (TCP) or
timeouts (UDP).
2017-09-08 13:28:09 +02:00
Sam Hocevar
b833d456dd Fix “make dist” by adding a few missing filenames. 2017-09-08 13:10:53 +02:00
Sam Hocevar
f64f42ba81 Merge branch 'feature-udp-support'. Closes #8. 2017-09-08 13:02:13 +02:00
Sam Hocevar
efc3474d1b Add proper destination address when answering an UDP client. 2017-09-08 12:56:48 +02:00
Sam Hocevar
f04eada6a9 Add UDP client timeout support. 2017-09-08 12:56:45 +02:00
Sam Hocevar
17d17e233b Merge some code into a new net.c source file. 2017-09-07 17:13:58 +02:00
Sam Hocevar
6750f65c35 Only call set FIONBIO on TCP sockets. 2017-09-07 17:04:05 +02:00
Sam Hocevar
6bb27c18a6 Support multiple UDP clients with connection tracking.
If an incoming UDP packet shares the same host and source port as
an existing connection, we consider it part of the input stream.
2017-09-07 16:29:23 +02:00
Sam Hocevar
b5206eab4e Add \n to all syslog calls because on Windows it will use fprintf. 2017-09-07 13:26:04 +02:00
Sam Hocevar
759cd83724 Use BUILT_SOURCES, not GENERATED_SOURCES. 2017-09-07 12:11:06 +02:00
Sam Hocevar
577a3be97a Improve code clarity in get_gmtoff(). 2017-09-06 18:19:57 +02:00
Sam Hocevar
e35403e112 Minor #ifdef cleanup. 2017-09-06 18:17:33 +02:00
Sam Hocevar
ff3974440e Get rid of useless refuse() function. 2017-09-06 18:05:25 +02:00
Sam Hocevar
79f2e14e22 Do not always try to write PID file in foreground mode.
This sounds like a reasonable default behaviour: if in foreground mode,
AND pidlogfile is not specified in the configuration file, then we most
probably do not want to write a PID file.
2017-09-06 18:02:05 +02:00
Sam Hocevar
00dec123da Use recvfrom() in UDP listening mode to get remote address.
Also move a few #ifdef from .c to .h to make the code slightly more readable.
2017-09-06 17:57:22 +02:00
Sam Hocevar
6909d2c3de Move connection authorisation logic to a separate function. 2017-09-06 14:56:35 +02:00
Sam Hocevar
b0511c6ab9 Merge branch 'master' into feature-udp-support 2017-09-06 13:01:30 +02:00
Sam Hocevar
7b62e576cf Reorganise some code for clarity and add license headers. 2017-09-05 17:29:10 +02:00
Sam Hocevar
a613195ad9 Do not call listen() on UDP listeners. 2017-09-05 17:28:04 +02:00
Sam Hocevar
33e8b8d7f9 Start playing around with UDP. 2017-09-05 17:27:16 +02:00
26 changed files with 3226 additions and 2120 deletions

5
.gitignore vendored
View File

@@ -1,5 +1,6 @@
.auto
.deps
.dirstamp
Makefile
Makefile.in
stamp-*
@@ -13,4 +14,8 @@ configure
*.o
rinetd
rinetd.exe
*.vcxproj.user
Debug
Release

View File

@@ -1,4 +1,29 @@
## Version 0.63
## Version 0.73 (2021/02/19)
* improve error reporting
* fixed another configuration parsing bug
## Version 0.72 (2021/02/16)
* fixed a configuration parsing bug making 0.71 almost unusable
## Version 0.71 (2021/02/14)
* IPv6 support
* fixed a display overflow in data usage statistics
* made the configuration parser more tolerant with service names that contain
dashes (e.g. snmp-trap) or hostnames with underscores (e.g. \_http.example.com).
* create the log file if it does not exist
* new Visual Studio project file
## Version 0.70 (2017/09/09)
* UDP support
* source address binding support
* use a real grammar for the configuration file and get rid of the hand-made
parser
## Version 0.63 (2017/07/19)
* added a `-f` flag to run in the foreground
* quit cleanly when `SIGINT` is received

339
COPYING Normal file
View File

@@ -0,0 +1,339 @@
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.

View File

@@ -1,25 +1,18 @@
AUTOMAKE_OPTIONS = foreign dist-bzip2
EXTRA_DIST = bootstrap CHANGES.md README.md index.html \
getopt.c getopt.h $(man_MANS) $(sysconf_DATA)
SUBDIRS = src
EXTRA_DIST = bootstrap COPYING CHANGES.md README.md index.html rinetd.vcxproj \
$(man_MANS) $(sysconf_DATA)
man_MANS = rinetd.8
sysconf_DATA = rinetd.conf
noinst_DATA = index.html
sbin_PROGRAMS = rinetd
rinetd_SOURCES = rinetd.c rinetd.h parse.h match.c match.h
GENERATED_SOURCES = parse.h
# If peg/leg is available, regenerate parse.h
parse.h: parse.peg
if which leg >/dev/null 2>&1; then leg -o $@ $^; else touch $@; fi
# _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.
# If roffit is available, regenerate index.html
index.html: rinetd.8
if which roffit >/dev/null 2>&1; then roffit < $^ > $@; else touch $@; fi
# This is for Travis CI; we dont have a testsuite yet.
test:

View File

@@ -1,9 +1,9 @@
# Process this file with autoconf to produce a configure script.
AC_PREREQ(2.52)
AC_INIT(rinetd, 0.63, sam@hocevar.net)
AC_INIT(rinetd, 0.73, sam@hocevar.net)
AC_CONFIG_AUX_DIR(.auto)
AC_CONFIG_SRCDIR([getopt.h])
AC_CONFIG_HEADER([config.h])
AC_CONFIG_SRCDIR([src/rinetd.c])
AC_CONFIG_HEADER([src/config.h])
AM_INIT_AUTOMAKE([no-define tar-ustar silent-rules])
# Checks for programs.
@@ -21,6 +21,8 @@ AC_CHECK_HEADERS([arpa/inet.h errno.h fcntl.h netdb.h netinet/in.h stdlib.h stri
AC_C_CONST
AC_HEADER_TIME
AC_STRUCT_TM
AC_CHECK_TYPES(ssize_t, [], [],
[#include <sys/types.h>])
AC_CHECK_TYPES(socklen_t, [], [],
[#include <sys/types.h>
#include <sys/socket.h>])
@@ -34,4 +36,5 @@ AC_FUNC_STRFTIME
AC_CHECK_FUNCS([gethostbyname memset select socket strstr daemon fork sigaction])
AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([src/Makefile])
AC_OUTPUT

View File

@@ -1,232 +1,140 @@
<html>
<head>
<title>RINETD(8)</title>
<style>
body {
background-color: #fffff0;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<title>rinetd man page</title>
<meta name="generator" content="roffit">
<STYLE type="text/css">
P.level0 {
padding-left: 2em;
}
h3 {
color: #ff8888;
P.level1 {
padding-left: 4em;
}
blockquote,code {
font-family: consolas, monospace, fixed;
font-size: 0.9em;
font-weight: bold;
color: #555555;
P.level2 {
padding-left: 6em;
}
</style>
</head>
<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>
</table>
<h3>NAME</h3>
span.emphasis {
font-style: italic;
}
<p> rinetd -- internet “redirection server”</p>
span.bold {
font-weight: bold;
}
<h3>SYNOPSIS</h3>
span.manpage {
font-weight: bold;
}
<p> <code>/usr/sbin/rinetd</code></p>
h2.nroffsh {
background-color: #e0e0e0;
}
<h3>VERSION</h3>
span.nroffip {
font-weight: bold;
font-size: 120%;
font-family: monospace;
}
<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>
p.roffit {
text-align: center;
font-size: 80%;
}
</STYLE>
</head><body>
<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>
<p class="level0">
<p class="level0"><a name="NAME"></a><h2 class="nroffsh">NAME</h2>
<p class="level0">rinetd - internet redirection server
<p class="level0"><a name="SYNOPSIS"></a><h2 class="nroffsh">SYNOPSIS</h2>
<p class="level0"><span Class="bold">rinetd</span> [<span Class="bold">-f</span>] [<span Class="bold">-c</span> <span Class="emphasis">configuration</span>]
<p class="level0"><span Class="bold">rinetd -h</span>
<p class="level0"><span Class="bold">rinetd -v</span>
<p class="level0"><a name="DESCRIPTION"></a><h2 class="nroffsh">DESCRIPTION</h2>
<p class="level0">
<p class="level0"><span Class="bold">rinetd</span> redirects TCP or UDP connections from one IP address and port to another. <span Class="bold">rinetd</span> is a single-process server which handles any number of connections to the address/port pairs specified in the file <span Class="emphasis">/etc/rinetd.conf</span>. Since <span Class="bold">rinetd</span> 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 services on machines inside an IP masquerading firewall.
<p class="level0"><span Class="bold">rinetd</span> is typically launched at boot time, using the following syntax:
<p class="level0"><span Class="bold"> /usr/sbin/rinetd</span>
<p class="level0">The configuration file is found in the file <span Class="emphasis">/etc/rinetd.conf</span>, unless another file is specified using the <span Class="bold">-c</span> command line option.
<p class="level0"><a name="OPTIONS"></a><h2 class="nroffsh">OPTIONS</h2>
<p class="level0">
<p class="level0"><span Class="bold">-f</span> Run <span Class="bold">rinetd</span> in the foreground, without forking to the background.
<p class="level0"><span Class="bold">-c</span> <span Class="emphasis">configuration</span> Specify an alternate configuration file.
<p class="level0"><span Class="bold">-v</span> Display the version number and exit.
<p class="level0"><span Class="bold">-h</span> Produce a short help message and exit.
<p class="level0"><a name="FORWARDING"></a><h2 class="nroffsh">FORWARDING RULES</h2>
<p class="level0">
<p class="level0">Most entries in the configuration file are forwarding rules. The format of a forwarding rule is as follows:
<p class="level0"><span Class="bold"> bindaddress bindport connectaddress connectport [options...]</span>
<p class="level0">For example:
<p class="level0"><span Class="bold"> 206.125.69.81 80 10.1.1.2 80</span>
<p class="level0">Would redirect all connections to port 80 of the "real" IP address 206.125.69.81, which could be a virtual interface, through <span Class="bold">rinetd</span> 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 class="level0">
<p class="level0">is one of <span Class="bold">rinetd</span>'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:
<p class="level0">
<p class="level0"><span Class="bold"> 0.0.0.0 23 10.1.1.2 23</span>
<p class="level0">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 class="level0">Ports default to TCP. To specify the protocol, append <span Class="emphasis">/udp</span> or <span Class="emphasis">/tcp</span> to the port number:
<p class="level0"><span Class="bold"> 206.125.69.81 80/tcp 10.1.1.2 8000/udp</span>
<p class="level0">Service names can be specified instead of port numbers. On most systems, service names are defined in the file <span Class="emphasis">/etc/services</span>.
<p class="level0">Both IP addresses and hostnames are accepted for bindaddress and connectaddress, including IPv6.
<p class="level0">
<p class="level0"><a name="UDP"></a><h2 class="nroffsh">UDP timeout option</h2>
<p class="level0">Since UDP is a connectionless protocol, a timeout is necessary or forwarding connections may accumulate with time and exhaust resources. By default, if no data is sent or received on a UDP connection for 72 seconds, the other connection is closed. This value can be changed using the <span Class="emphasis">timeout</span> option:
<p class="level0"><span Class="bold"> 0.0.0.0 8000/udp 10.1.1.2 80 [timeout=3600]</span>
<p class="level0">This rule will forward all data received on UDP port 8000 to host 10.1.1.2 on TCP port 80, and will close the connection after no data is received on the UDP port for 3600 seconds.
<p class="level0"><a name="Source"></a><h2 class="nroffsh">Source address option</h2>
<p class="level0">A forwarding rule option allows to bind to a specific local address when sending data to the other end. This is done using the <span Class="emphasis">src</span> option:
<p class="level0"><span Class="bold"> 192.168.1.1 80 10.1.1.127 80 [src=10.1.1.2]</span>
<p class="level0">Assuming the local host has two IP addresses, 10.1.1.1 and 10.1.1.2, this rule ensures that forwarded packets are sent using source address 10.1.1.2.
<p class="level0"><a name="ALLOW"></a><h2 class="nroffsh">ALLOW AND DENY RULES</h2>
<p class="level0">Configuration files can also contain allow and deny rules.
<p class="level0">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 class="level0">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 class="level0">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 class="level0">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 class="level0">The format of an allow rule is as follows:
<p class="level0"><span Class="bold"> allow|deny pattern</span>
<p class="level0">Patterns can contain the following characters: <span Class="bold">0</span>, <span Class="bold">1</span>, <span Class="bold">2</span>, <span Class="bold">3</span>, <span Class="bold">4</span>, <span Class="bold">5</span>, <span Class="bold">6</span>, <span Class="bold">7</span>, <span Class="bold">8</span>, <span Class="bold">9</span>, <span Class="bold">.</span> (period), <span Class="bold">?</span>, and <span Class="bold">*</span>. The <span Class="bold">?</span> wildcard matches any one character. The <span Class="bold">*</span> wildcard matches any number of characters, including zero.
<p class="level0">For example:
<p class="level0"><span Class="bold"> allow 206.125.69.*</span>
<p class="level0">This allow rule matches all IP addresses in the 206.125.69 class C domain.
<p class="level0">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 <span Class="bold">rinetd</span> is a single process server, all other connections would be forced to pause during the address lookup.
<p class="level0"><a name="LOGGING"></a><h2 class="nroffsh">LOGGING</h2>
<p class="level0"><span Class="bold">rinetd</span> is able to produce a log file in either of two formats: tab-delimited and web server-style "common log format".
<p class="level0">By default, <span Class="bold">rinetd</span> does not produce a log file. To activate logging, add the following line to the configuration file:
<p class="level0"><span Class="bold"> logfile log-file-location</span>
<p class="level0">Example:
<p class="level0"><span Class="bold"> logfile /var/log/rinetd.log</span>
<p class="level0">By default, <span Class="bold">rinetd</span> logs in a simple tab-delimited format containing the following information:
<p class="level0">&nbsp; Date and time
<p class="level0">&nbsp; Client address
<p class="level0">&nbsp; Listening host
<p class="level0">&nbsp; Listening port
<p class="level0">&nbsp; Forwarded-to host
<p class="level0">&nbsp; Forwarded-to port
<p class="level0">&nbsp; Bytes received from client
<p class="level0">&nbsp; Bytes sent to client
<p class="level0">&nbsp; Result message
<p class="level0">To activate web server-style "common log format" logging, add the following line to the configuration file:
<p class="level0"><span Class="bold"> logcommon</span>
<p class="level0"><a name="REINITIALIZING"></a><h2 class="nroffsh">REINITIALIZING RINETD</h2>
<p class="level0">
<p class="level0">The <span Class="bold">kill -1</span> signal (<span Class="emphasis">SIGHUP</span>) can be used to cause <span Class="bold">rinetd</span> to reload its configuration file without interrupting existing connections.
<p class="level0">Under Linux(tm the process id is saved in the file <span Class="emphasis">/var/run/rinetd.pid</span> to facilitate the <span Class="bold">kill -HUP</span>. An alternate filename can be provided by using the pidlogfile configuration file option.
<p class="level0"><a name="BUGS"></a><h2 class="nroffsh">BUGS AND LIMITATIONS</h2>
<p class="level0">
<p class="level0"><span Class="bold">rinetd</span> only redirects protocols which use a single TCP or UDP socket. This rules out FTP.
<p class="level0">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 <span Class="bold">rinetd</span> 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, alternate code which uses a different trick to work around blocking close() is provided, but this code is untested.
<p class="level0">The logging is inadequate. The duration of each connection should be logged. <a name="LICENSE"></a><h2 class="nroffsh">LICENSE</h2>
<p class="level0">Copyright (c) 1997, 1998, 1999, Thomas Boutell and Boutell.Com, Inc.
<p class="level0">Copyright (c) 2003-2021 Sam Hocevar
<p class="level0">This software is released for free use under the terms of the GNU General Public License, version 2 or higher. NO WARRANTY IS EXPRESSED OR IMPLIED. USE THIS SOFTWARE AT YOUR OWN RISK. <a name="CONTACT"></a><h2 class="nroffsh">CONTACT INFORMATION</h2>
<p class="level0">See https://github.com/samhocevar/rinetd/releases for the latest release.
<p class="level0">Thomas Boutell can be reached by email: boutell@boutell.com
<p class="level0">Sam Hocevar can be reached by email: sam@hocevar.net <a name="THANKS"></a><h2 class="nroffsh">THANKS</h2>
<p class="level0">Thanks are due to Bill Davidsen, Libor Pechachek, Sascha Ziemann, the Apache Group, and many others who have contributed advice and/or source code to this and other free software projects. <p class="roffit">
This HTML page was made with <a href="http://daniel.haxx.se/projects/roffit/">roffit</a>.
</body></html>

View File

@@ -1,9 +0,0 @@
#ifndef MATCH_H
#define MATCH_H 1
extern int match(char const *s, char const *p);
extern int matchNoCase(char const *s, char const *p);
extern int matchBody(char const *s, char const *p, int nocase);
#endif /* MATCH_H */

1120
parse.h

File diff suppressed because it is too large Load Diff

182
parse.peg
View File

@@ -1,182 +0,0 @@
%{
#define YY_CTX_LOCAL 1
#define YY_CTX_MEMBERS \
FILE *fp; \
int currentLine; \
int isAuthAllow; \
unsigned int port, bindPort, connectPort; \
char *bindAddress, *connectAddress;
#define YY_INPUT(yyctx, buf, result, max_size) \
{ \
int yyc = fgetc(yyctx->fp); \
result = (EOF == yyc) ? 0 : (*(buf) = yyc, 1); \
}
#define PARSE_ERROR exit(1);
%}
file = (sol (line eol | invalid_syntax))*
line = -? (rule | auth | logfile | pidlogfile | logcommon )? -? comment?
comment = "#" (!eol .)*
rule = bind_address - bind_port - connect_address - connect_port {
/* Turn all of this stuff into reasonable addresses */
struct in_addr iaddr;
if (getAddress(yy->bindAddress, &iaddr) < 0) {
fprintf(stderr, "rinetd: host %s could not be resolved.\n",
yy->bindAddress);
PARSE_ERROR;
}
/* Make a server socket */
SOCKET fd = socket(PF_INET, SOCK_STREAM, 0);
if (fd == INVALID_SOCKET) {
syslog(LOG_ERR, "couldn't create "
"server socket! (%m)\n");
PARSE_ERROR;
}
struct sockaddr_in saddr;
saddr.sin_family = AF_INET;
memcpy(&saddr.sin_addr, &iaddr, sizeof(iaddr));
saddr.sin_port = htons(yy->bindPort);
int tmp = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
(const char *) &tmp, sizeof(tmp));
if (bind(fd, (struct sockaddr *)
&saddr, sizeof(saddr)) == SOCKET_ERROR)
{
/* Warn -- don't exit. */
syslog(LOG_ERR, "couldn't bind to "
"address %s port %d (%m)\n",
yy->bindAddress, yy->bindPort);
closesocket(fd);
PARSE_ERROR;
}
if (listen(fd, RINETD_LISTEN_BACKLOG) == SOCKET_ERROR) {
/* Warn -- don't exit. */
syslog(LOG_ERR, "couldn't listen to "
"address %s port %d (%m)\n",
yy->bindAddress, yy->bindPort);
closesocket(fd);
PARSE_ERROR;
}
#if _WIN32
u_long ioctltmp;
#else
int ioctltmp;
#endif
ioctlsocket(fd, FIONBIO, &ioctltmp);
if (getAddress(yy->connectAddress, &iaddr) < 0) {
/* Warn -- don't exit. */
syslog(LOG_ERR, "host %s could not be resolved.\n",
yy->bindAddress);
closesocket(fd);
PARSE_ERROR;
}
/* Allocate server info */
seInfo = (ServerInfo *)
realloc(seInfo, sizeof(ServerInfo) * (seTotal + 1));
if (!seInfo) {
PARSE_ERROR;
}
ServerInfo *srv = &seInfo[seTotal];
memset(srv, 0, sizeof(*srv));
srv->fd = fd;
srv->localAddr = iaddr;
srv->localPort = htons(yy->connectPort);
srv->fromHost = yy->bindAddress;
if (!srv->fromHost) {
PARSE_ERROR;
}
srv->fromPort = yy->bindPort;
srv->toHost = yy->connectAddress;
if (!srv->toHost) {
PARSE_ERROR;
}
srv->toPort = yy->connectPort;
#ifndef _WIN32
if (fd > maxfd) {
maxfd = fd;
}
#endif
++seTotal;
yy->bindAddress = yy->connectAddress = NULL;
}
bind_address = < ipv4 > { yy->bindAddress = strdup(yytext); }
connect_address = < ipv4 > { yy->connectAddress = strdup(yytext); }
bind_port = port { yy->bindPort = yy->port; }
connect_port = port { yy->connectPort = yy->port; }
port = < (number | service) > {
struct servent *bindService = getservbyname(yytext, "tcp");
yy->port = bindService ? ntohs(bindService->s_port) : atoi(yytext);
if (yy->port == 0 || yy->port >= 65536) {
syslog(LOG_ERR, "bind port missing or out of range\n");
PARSE_ERROR;
}
}
auth = auth_key - < pattern > {
allRules = (Rule *)
realloc(allRules, sizeof(Rule) * (allRulesCount + 1));
if (!allRules) {
PARSE_ERROR;
}
allRules[allRulesCount].pattern = strdup(yytext);
if (!allRules[allRulesCount].pattern) {
PARSE_ERROR;
}
allRules[allRulesCount].type = yy->isAuthAllow ? allowRule : denyRule;
if (seTotal > 0) {
if (seInfo[seTotal - 1].rulesStart == 0) {
seInfo[seTotal - 1].rulesStart = allRulesCount;
}
++seInfo[seTotal - 1].rulesCount;
} else {
++globalRulesCount;
}
++allRulesCount;
}
auth_key = < ("allow" | "deny") > { yy->isAuthAllow = (yytext[0] == 'a'); }
logfile = "logfile" - < filename > {
logFileName = strdup(yytext);
if (!logFileName) {
PARSE_ERROR;
}
}
pidlogfile = "pidlogfile" - < filename > {
pidLogFileName = strdup(yytext);
if (!pidLogFileName) {
PARSE_ERROR;
}
}
logcommon = "logcommon" {
logFormatCommon = 1;
}
invalid_syntax = < (!eol .)+ > eol {
fprintf(stderr, "rinetd: invalid syntax at line %d: %s\n",
yy->currentLine, yytext);
PARSE_ERROR; /* FIXME */
}
service = name
ipv4 = number [.] number [.] number [.] number | '0'
pattern = [0-9*?]+ ('.' [0-9*?]+ ('.' [0-9*?]+ ('.' [0-9*?]+)?)?)?
number = digit+
name = [a-zA-Z][a-zA-Z0-9_]*
filename = '"' [^"]+ '"'
| [^ \t\r\n]+
- = [ \t]+
digit = [0-9]
sol = { ++yy->currentLine; }
eol = '\r'? '\n' | eof
eof = '\0'

328
rinetd.8
View File

@@ -1,199 +1,233 @@
.\" Copyright (c) 1997, 1998, 1999, Thomas Boutell and Boutell.Com, Inc.
.\" This software is released for free use under the terms of
.\" the GNU Public License, version 2 or higher.
.\"
.Dd July 19, 2017
.Dt RINETD 8
.Os LINUX
.Sh NAME
.Nm rinetd
.Nd internet
.Dq redirection server
.Sh SYNOPSIS
.Nm /usr/sbin/rinetd
.Sh VERSION
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.
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 does not
redirect FTP, because FTP requires more than one socket.
.Pp
rinetd is typically launched at boot time, using the following syntax:
.Pp
/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.
.Sh FORWARDING RULES
.TH rinetd 8 "2021-02-19" "rinetd 0.73"
.SH NAME
rinetd \- internet redirection server
.SH SYNOPSIS
\fBrinetd\fR
[\fB\-f\fR]
[\fB\-c\fR \fIconfiguration\fR]
.br
\fBrinetd \-h\fR
.br
\fBrinetd \-v\fR
.SH DESCRIPTION
.PP
\fBrinetd\fR
redirects TCP or UDP connections from one IP address and port to another.
\fBrinetd\fR is a single-process server which handles any number of connections to
the address/port pairs specified in the file \fI/etc/rinetd.conf\fR. Since
\fBrinetd\fR 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 services on machines inside an IP masquerading firewall.
.PP
\fBrinetd\fR is typically launched at boot time, using the following syntax:
.PP
\fB /usr/sbin/rinetd\fR
.PP
The configuration file is found in the file \fI/etc/rinetd.conf\fR, unless
another file is specified using the \fB\-c\fR command line option.
.SH OPTIONS
.TP
\fB\-f\fR
Run \fBrinetd\fR in the foreground, without forking to the background.
.TP
\fB\-c\fR \fIconfiguration\fR
Specify an alternate configuration file.
.TP
\fB\-v\fR
Display the version number and exit.
.TP
\fB\-h\fR
Produce a short help message and exit.
.SH FORWARDING RULES
.PP
Most entries in the configuration file are forwarding rules. The
format of a forwarding rule is as follows:
.Pp
bindaddress bindport connectaddress connectport
.Pp
.PP
\fB bindaddress bindport connectaddress connectport [options...]\fR
.PP
For example:
.Pp
206.125.69.81 80 10.1.1.2 80
.Pp
.PP
\fB 206.125.69.81 80 10.1.1.2 80\fR
.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
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
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:
.Pp
0.0.0.0 23 10.1.1.2 23
.Pp
206.125.69.81, which could be a virtual interface, through \fBrinetd\fR 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 \fBrinetd\fR'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:
.PP
\fB 0.0.0.0 23 10.1.1.2 23\fR
.PP
Would redirect all connections to port 23, for all IP addresses
assigned to the server. This is the default behavior for most
other programs.
.Pp
.PP
Ports default to TCP. To specify the protocol, append \fI/udp\fR
or \fI/tcp\fR to the port number:
.PP
\fB 206.125.69.81 80/tcp 10.1.1.2 8000/udp\fR
.PP
Service names can be specified instead of port numbers. On most systems,
service names are defined in the file /etc/services.
.Pp
service names are defined in the file \fI/etc/services\fR.
.PP
Both IP addresses and hostnames are accepted for
bindaddress and connectaddress.
.Pp
.Sh ALLOW AND DENY RULES
bindaddress and connectaddress, including IPv6.
.PP
.SS UDP timeout option
Since UDP is a connectionless protocol, a timeout is necessary or forwarding
connections may accumulate with time and exhaust resources. By default, if
no data is sent or received on a UDP connection for 72 seconds, the other
connection is closed. This value can be changed using the \fItimeout\fR option:
.PP
\fB 0.0.0.0 8000/udp 10.1.1.2 80 [timeout=3600]\fR
.PP
This rule will forward all data received on UDP port 8000 to host 10.1.1.2
on TCP port 80, and will close the connection after no data is received on
the UDP port for 3600 seconds.
.SS Source address option
A forwarding rule option allows to bind to a specific local address when
sending data to the other end. This is done using the \fIsrc\fR option:
.PP
\fB 192.168.1.1 80 10.1.1.127 80 [src=10.1.1.2]\fR
.PP
Assuming the local host has two IP addresses, 10.1.1.1 and 10.1.1.2, this
rule ensures that forwarded packets are sent using source address 10.1.1.2.
.SH ALLOW AND DENY RULES
Configuration files can also contain allow and deny rules.
.Pp
.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.
.Pp
.PP
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.
.Pp
.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 deny rules, that connection
is immediately rejected, regardless of any other rules.
.Pp
.PP
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.
.Pp
.PP
The format of an allow rule is as follows:
.Pp
allow pattern
.Pp
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.
.Pp
.PP
\fB allow|deny pattern\fR
.PP
Patterns can contain the following characters: \fB0\fR, \fB1\fR, \fB2\fR,
\fB3\fR, \fB4\fR, \fB5\fR, \fB6\fR, \fB7\fR, \fB8\fR, \fB9\fR, \fB.\fR
(period), \fB?\fR, and \fB*\fR. The \fB?\fR wildcard matches any one character.
The \fB*\fR wildcard matches any number of characters, including zero.
.PP
For example:
.Pp
allow 206.125.69.*
.Pp
.PP
\fB allow 206.125.69.*\fR
.PP
This allow rule matches all IP addresses in the 206.125.69 class C domain.
.Pp
.PP
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
is prohibitive. Since \fBrinetd\fR is a single process server, all other
connections would be forced to pause during the address lookup.
.Pp
.Sh LOGGING
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
.SH LOGGING
\fBrinetd\fR is able to produce a log file in either of two formats:
tab-delimited and web server-style "common log format".
.PP
By default, \fBrinetd\fR does not produce a log file. To activate logging, add
the following line to the configuration file:
.Pp
logfile log-file-location
.Pp
Example: logfile /var/log/rinetd.log
.Pp
By default, rinetd logs in a simple tab-delimited format containing
.PP
\fB logfile log-file-location\fR
.PP
Example:
.PP
\fB logfile /var/log/rinetd.log\fR
.PP
By default, \fBrinetd\fR logs in a simple tab-delimited format containing
the following information:
.Pp
Date and time
.Pp
Client address
.Pp
Listening host
.Pp
Listening port
.Pp
Forwarded-to host
.Pp
Forwarded-to port
.Pp
Bytes received from client
.Pp
Bytes sent to client
.Pp
Result message
.Pp
.PP
Date and time
.PP
Client address
.PP
Listening host
.PP
Listening port
.PP
Forwarded-to host
.PP
Forwarded-to port
.PP
Bytes received from client
.PP
Bytes sent to client
.PP
Result message
.PP
To activate web server-style "common log format" logging,
add the following line to the configuration file:
.Pp
logcommon
.Sh COMMAND LINE OPTIONS
The -c command line option is used to specify an alternate
configuration file.
.Pp
The -f command line option is used to run rinetd in the
foreground, without forking to the background.
.Pp
The -h command line option produces a short help message.
.Pp
The -v command line option displays the version number.
.Sh REINITIALIZING RINETD
The kill -1 signal (SIGHUP) can be used to cause rinetd
.PP
\fB logcommon\fR
.SH REINITIALIZING RINETD
.PP
The \fBkill -1\fR signal (\fISIGHUP\fR) can be used to cause \fBrinetd\fR
to reload its configuration file without interrupting existing
connections.
.PP
Under Linux\(tm the process id is saved in the file \fI/var/run/rinetd.pid\fR
to facilitate the kill -HUP. An alternate
filename can be provided by using the <code>pidlogfile</code>
configuration file option.
to facilitate the \fBkill -HUP\fR. An alternate filename can be provided by
using the pidlogfile configuration file option.
.Sh LIMITATIONS
rinetd redirects TCP connections only. There is
no support for UDP. rinetd only redirects protocols which
use a single TCP socket. This rules out FTP.
.Sh BUGS
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,
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
.SH BUGS AND LIMITATIONS
.PP
\fBrinetd\fR only redirects protocols which use a single TCP or UDP
socket. This rules out FTP.
.PP
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 \fBrinetd\fR
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, alternate code which uses a different trick to
work around blocking close() is provided, but this code is untested.
.PP
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
.PP
Copyright (c) 2003\-2021 Sam Hocevar
.PP
This software is released for free use under the terms of
the GNU Public License, version 2 or higher. NO WARRANTY
the GNU General Public License, version 2 or higher. NO WARRANTY
IS EXPRESSED OR IMPLIED. USE THIS SOFTWARE AT YOUR OWN RISK.
.Sh CONTACT INFORMATION
.SH CONTACT INFORMATION
See https://github.com/samhocevar/rinetd/releases for the latest release.
.Pp
.PP
Thomas Boutell can be reached by email: boutell@boutell.com
.Pp
.PP
Sam Hocevar can be reached by email: sam@hocevar.net
.Sh THANKS
.SH THANKS
Thanks are due to Bill Davidsen, Libor Pechachek, Sascha Ziemann, the
Apache Group, and many others who have contributed advice
and/or source code to this and other free software projects.

View File

@@ -7,6 +7,8 @@
#
# allow 192.168.2.*
# deny 192.168.2.1?
# allow fe80:*
# deny 2001:618:*:e43f
#
@@ -15,9 +17,13 @@
# you may specify allow and deny rules after a specific forwarding rule
# to apply to only that forwarding rule
#
# bindadress bindport connectaddress connectport
127.0.0.1 4000 127.0.0.1 22
#127.0.0.1 4000/udp 127.0.0.1 22
# bindadress bindport connectaddress connectport options...
# 0.0.0.0 80 192.168.1.2 80
# ::1 80 192.168.1.2 80
# 0.0.0.0 80 fe80::1 80
# 127.0.0.1 4000 127.0.0.1 3000
# 127.0.0.1 4000/udp 127.0.0.1 22 [timeout=1200]
# 127.0.0.1 8000/udp 192.168.1.2 8000/udp [src=192.168.1.2,timeout=1200]
# logging information
logfile /var/log/rinetd.log

View File

@@ -1,69 +0,0 @@
/* 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;
};

110
rinetd.vcxproj Normal file
View File

@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\getopt.c" />
<ClCompile Include="src\match.c" />
<ClCompile Include="src\net.c" />
<ClCompile Include="src\parse.c" />
<ClCompile Include="src\rinetd.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\getopt.h" />
<ClInclude Include="src\match.h" />
<ClInclude Include="src\net.h" />
<ClInclude Include="src\parse.h" />
<ClInclude Include="src\rinetd.h" />
<ClInclude Include="src\types.h" />
</ItemGroup>
<ItemGroup>
<None Include="src\parse.peg" />
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{3F1D9D58-3205-48E9-9C93-557291A45D74}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>rinetd</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Debug'" Label="Configuration">
<UseDebugLibraries>true</UseDebugLibraries>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'" Label="Configuration">
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)'=='Debug'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)'=='Release'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup>
<ClCompile>
<PrecompiledHeader>NotUsing</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<CompileAs>CompileAsC</CompileAs>
<ConformanceMode>true</ConformanceMode>
<PreprocessorDefinitions>_CONSOLE;PACKAGE_VERSION="0.73";_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_DEPRECATE;_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<AdditionalDependencies>ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
<ClCompile>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)'=='Release'">
<ClCompile>
<Optimization>MaxSpeed</Optimization>
<PreprocessorDefinitions>NDEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

24
src/Makefile.am Normal file
View File

@@ -0,0 +1,24 @@
EXTRA_DIST = parse.peg getopt.c getopt.h
sbin_PROGRAMS = ../rinetd
___rinetd_SOURCES = rinetd.c rinetd.h \
parse.c parse.h \
match.c match.h \
net.c net.h \
types.h
BUILT_SOURCES = parse.c
# If peg/leg is available, regenerate parse.h
parse.c: parse.peg
if which leg >/dev/null 2>&1; then leg -o $@ $^; else touch $@; fi
# _POSIX_C_SOURCE is for SA_RESTART and others
# _DARWIN_C_SOURCE is for NI_MAXHOST on OS X
# _XOPEN_SOURCE is for struct sigaction
# _GNU_SOURCE is for h_errno and gethostbyname-related macros
___rinetd_CFLAGS = -std=c99 -D_POSIX_C_SOURCE=200809L \
-D_XOPEN_SOURCE -D_GNU_SOURCE -D_DARWIN_C_SOURCE \
-Wall -Wextra -Wwrite-strings

View File

@@ -1,5 +1,13 @@
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
and Boutell.Com, Inc.
© 20032017 Sam Hocevar <sam@hocevar.net>
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. */
#if HAVE_CONFIG_H
#include <config.h>
#include <config.h>
#endif
#include <string.h>

14
src/match.h Normal file
View File

@@ -0,0 +1,14 @@
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
and Boutell.Com, Inc.
© 2003—2017 Sam Hocevar <sam@hocevar.net>
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. */
#pragma once
extern int match(char const *s, char const *p);
extern int matchNoCase(char const *s, char const *p);
extern int matchBody(char const *s, char const *p, int nocase);

85
src/net.c Normal file
View File

@@ -0,0 +1,85 @@
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
and Boutell.Com, Inc.
© 2003—2021 Sam Hocevar <sam@hocevar.net>
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. */
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include "net.h"
void setSocketDefaults(SOCKET fd) {
/* Make socket non-blocking (FIXME: this uses legacy API) */
FIONBIO_ARG_T ioctltmp = 1;
#if _WIN32
ioctlsocket(fd, FIONBIO, &ioctltmp);
#else
ioctl(fd, FIONBIO, &ioctltmp);
#endif
#if defined __linux__
int tmp = 0;
setsockopt(fd, SOL_SOCKET, SO_LINGER, &tmp, sizeof(tmp));
#endif
#if !defined __linux__ && !defined _WIN32
int tmp = 1024;
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &tmp, sizeof(tmp));
#endif
}
int getAddrInfoWithProto(char *address, char *port, int protocol, struct addrinfo **ai)
{
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_protocol = protocol,
.ai_socktype = protocol == IPPROTO_UDP ? SOCK_DGRAM : SOCK_STREAM,
.ai_flags = AI_PASSIVE,
};
int ret = getaddrinfo(address, port, &hints, ai);
if (ret != 0) {
fprintf(stderr, "rinetd: cannot resolve host \"%s\" port %s "
"(getaddrinfo() error: %s)\n",
address, port ? port : "<null>", gai_strerror(ret));
}
return ret;
}
int sameSocketAddress(struct sockaddr_storage *a, struct sockaddr_storage *b) {
if (a->ss_family != b->ss_family)
return 0;
switch (a->ss_family) {
case AF_INET: {
struct sockaddr_in *a4 = (struct sockaddr_in *)a;
struct sockaddr_in *b4 = (struct sockaddr_in *)b;
return a4->sin_port == b4->sin_port
&& a4->sin_addr.s_addr == b4->sin_addr.s_addr;
}
case AF_INET6: {
struct sockaddr_in6 *a6 = (struct sockaddr_in6 *)a;
struct sockaddr_in6 *b6 = (struct sockaddr_in6 *)b;
return a6->sin6_port == b6->sin6_port
&& a6->sin6_addr.s6_addr == b6->sin6_addr.s6_addr;
}
}
return 0;
}
uint16_t getPort(struct addrinfo* ai) {
switch (ai->ai_family) {
case AF_INET:
return ntohs(((struct sockaddr_in*)ai->ai_addr)->sin_port);
case AF_INET6:
return ntohs(((struct sockaddr_in6*)ai->ai_addr)->sin6_port);
default:
return 0;
}
}

90
src/net.h Normal file
View File

@@ -0,0 +1,90 @@
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
and Boutell.Com, Inc.
© 2003—2021 Sam Hocevar <sam@hocevar.net>
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. */
#pragma once
#if _WIN32
/* Define this to a reasonably large value */
# define FD_SETSIZE 4096
# include <winsock2.h>
# include <ws2ipdef.h>
# include <ws2tcpip.h>
# include <windows.h>
#else
# include <sys/types.h>
# include <sys/socket.h>
# include <sys/ioctl.h>
# include <netdb.h>
# include <netinet/in.h>
# include <arpa/inet.h>
#endif
#include <stdint.h>
#if defined HAVE_ERRNO_H
# include <errno.h>
#endif
#if defined HAVE_UNISTD_H
# include <unistd.h>
#endif
/* We've got to get FIONBIO from somewhere. Try the Solaris location
if it isn't defined yet by the above includes. */
#ifndef FIONBIO
# include <sys/filio.h>
#endif /* FIONBIO */
#if HAVE_SSIZE_T
# define SSIZE_T ssize_t
#else
# define SSIZE_T int
#endif
#if HAVE_SOCKLEN_T
# define SOCKLEN_T socklen_t
#else
# define SOCKLEN_T int
#endif
#if _WIN32
# define FIONBIO_ARG_T u_long
#else
# define FIONBIO_ARG_T int
#endif
#if _WIN32
/* _WIN32 doesn't really have WSAEAGAIN */
# ifndef WSAEAGAIN
# define WSAEAGAIN WSAEWOULDBLOCK
# endif
# ifdef gai_strerror
# undef gai_strerror
# define gai_strerror gai_strerrorA
# endif
#else
/* Windows sockets compatibility defines */
# define INVALID_SOCKET (-1)
# define SOCKET_ERROR (-1)
static inline int closesocket(int s) {
return close(s);
}
# define WSAEWOULDBLOCK EWOULDBLOCK
# define WSAEAGAIN EAGAIN
# define WSAEINPROGRESS EINPROGRESS
# define WSAEINTR EINTR
# define SOCKET int
static inline int GetLastError(void) {
return errno;
}
#endif /* _WIN32 */
void setSocketDefaults(SOCKET fd);
int sameSocketAddress(struct sockaddr_storage *a, struct sockaddr_storage *b);
int getAddrInfoWithProto(char *address, char *port, int protocol, struct addrinfo **ai);
uint16_t getPort(struct addrinfo* ai);

1431
src/parse.c Normal file

File diff suppressed because it is too large Load Diff

12
src/parse.h Normal file
View File

@@ -0,0 +1,12 @@
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
and Boutell.Com, Inc.
© 2003—2017 Sam Hocevar <sam@hocevar.net>
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. */
#pragma once
void parseConfiguration(char const *in);

189
src/parse.peg Normal file
View File

@@ -0,0 +1,189 @@
# Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
# and Boutell.Com, Inc.
# © 2003—2021 Sam Hocevar <sam@hocevar.net>
#
# 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. */
%{
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "net.h"
#include "types.h"
#include "rinetd.h"
#include "parse.h"
#define YY_CTX_LOCAL 1
#define YY_CTX_MEMBERS \
FILE *fp; \
int currentLine; \
int isAuthAllow; \
char *tmpPort, *bindPort, *connectPort; \
int tmpProto, bindProto, connectProto; \
int serverTimeout; \
char *bindAddress, *connectAddress, *sourceAddress;
#define YY_INPUT(yyctx, buf, result, max_size) \
{ \
int yyc = fgetc(yyctx->fp); \
result = (EOF == yyc) ? 0 : (*(buf) = yyc, 1); \
}
#define PARSE_ERROR exit(1);
#define MEMORY_ERROR { \
logError("could not allocate memory when parsing configuration.\n"); \
exit(1); \
}
#if defined __clang__
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wunused-label"
#elif defined __GNUC__
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-label"
#endif
%}
file = (sol (line eol | invalid-syntax))*
line = -? (command -?)? comment?
command = (server-rule | auth-rule | logfile | pidlogfile | logcommon)
comment = "#" (!eol .)*
server-rule = bind-address - bind-port - connect-address - connect-port (-? server-options)?
{
addServer(yy->bindAddress, yy->bindPort, yy->bindProto,
yy->connectAddress, yy->connectPort, yy->connectProto,
yy->serverTimeout > 0 ? yy->serverTimeout : RINETD_DEFAULT_UDP_TIMEOUT,
yy->sourceAddress);
free(yy->bindAddress);
free(yy->connectAddress);
free(yy->sourceAddress);
yy->bindAddress = yy->connectAddress = yy->sourceAddress = NULL;
free(yy->bindPort);
free(yy->connectPort);
yy->bindPort = yy->connectPort = NULL;
yy->serverTimeout = 0;
}
bind-address = < address > { yy->bindAddress = strdup(yytext); }
connect-address = < address > { yy->connectAddress = strdup(yytext); }
bind-port = full-port { yy->bindPort = yy->tmpPort; yy->bindProto = yy->tmpProto; }
connect-port = full-port { yy->connectPort = yy->tmpPort; yy->connectProto = yy->tmpProto; }
server-options = "[" -? option-list -? "]"
option-list = option (-? ',' -? option-list)?
option = (option-timeout | option-source)
option-timeout = "timeout" -? "=" -? < number > { yy->serverTimeout = atoi(yytext); }
option-source = "src" -? "=" -? < address > { yy->sourceAddress = strdup(yytext); }
full-port = port proto
port = < (number | service) > { yy->tmpPort = strdup(yytext); }
proto = '/tcp' { yy->tmpProto = IPPROTO_TCP; }
| '/udp' { yy->tmpProto = IPPROTO_UDP; }
| '' { yy->tmpProto = IPPROTO_TCP; }
auth-rule = auth-key - < pattern >
{
allRules = (Rule *)
realloc(allRules, sizeof(Rule) * (allRulesCount + 1));
if (!allRules) {
MEMORY_ERROR;
}
allRules[allRulesCount].pattern = strdup(yytext);
if (!allRules[allRulesCount].pattern) {
MEMORY_ERROR;
}
allRules[allRulesCount].type = yy->isAuthAllow ? allowRule : denyRule;
if (seTotal > 0) {
if (seInfo[seTotal - 1].rulesStart == 0 && seInfo[seTotal - 1].rulesCount == 0) {
seInfo[seTotal - 1].rulesStart = allRulesCount;
}
++seInfo[seTotal - 1].rulesCount;
} else {
++globalRulesCount;
}
++allRulesCount;
}
auth-key = < ("allow" | "deny") > { yy->isAuthAllow = (yytext[0] == 'a'); }
logfile = "logfile" - < filename >
{
logFileName = strdup(yytext);
if (!logFileName) {
MEMORY_ERROR;
}
}
pidlogfile = "pidlogfile" - < filename >
{
pidLogFileName = strdup(yytext);
if (!pidLogFileName) {
MEMORY_ERROR;
}
}
logcommon = "logcommon"
{
logFormatCommon = 1;
}
invalid_syntax = < (!eol .)+ > eol
{
logError("invalid syntax at line %d: %s\n",
yy->currentLine, yytext);
PARSE_ERROR; /* FIXME */
}
service = name
address = ipv4 | ipv6 | hostname
pattern = (hexdigit | '[' | ']' | ':' | '.' | [*?] )+
number = digit+
ipv4 = number '.' number '.' number '.' number | '0'
ipv6 = '[' bare_ipv6 ']' | bare_ipv6
bare_ipv6 = hexdigit* ':' (hexdigit | ':')* # Must have at least one ':'
hostname = (label '.')* name '.'?
name = id ('-' | id | digit)*
label = (id | digit) ('-' | id | digit)* # DNS labels may have digits
filename = '"' [^"]+ '"'
| [^ \t\r\n]+
- = [ \t]+
digit = [0-9]
hexdigit = [0-9a-fA-F]
id = [a-zA-Z_]
sol = { ++yy->currentLine; }
eol = '\r'? '\n' | eof
eof = '\0'
%%
void parseConfiguration(char const *file)
{
FILE *in = fopen(file, "r");
if (!in) {
logError("could not open configuration file %s.\n", file);
exit(1);
}
yycontext ctx;
memset(&ctx, 0, sizeof(yycontext));
ctx.fp = in;
if (!yyparse(&ctx)) {
logError("invalid syntax in file %s, line %d.\n", file, -1);
exit(1);
}
yyrelease(&ctx);
fclose(in);
/* Avoid warnings for these unused functions */
(void)yySet; (void)yyPush; (void)yyPop; (void)yyAccept;
}

File diff suppressed because it is too large Load Diff

42
src/rinetd.h Normal file
View File

@@ -0,0 +1,42 @@
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
and Boutell.Com, Inc.
© 2003—2021 Sam Hocevar <sam@hocevar.net>
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. */
#pragma once
#include <stdint.h>
/* Constants */
static int const RINETD_BUFFER_SIZE = 16384;
static int const RINETD_LISTEN_BACKLOG = 128;
static int const RINETD_DEFAULT_UDP_TIMEOUT = 72;
#define RINETD_CONFIG_FILE "/etc/rinetd.conf"
#define RINETD_PID_FILE "/var/run/rinetd.pid"
/* Global configuration */
extern Rule *allRules;
extern int allRulesCount;
extern int globalRulesCount;
extern ServerInfo *seInfo;
extern int seTotal;
extern char *logFileName;
extern char *pidLogFileName;
extern int logFormatCommon;
extern FILE *logFile;
/* Functions */
void logError(char const *fmt, ...);
void logInfo(char const *fmt, ...);
void addServer(char *bindAddress, char *bindPort, int bindProtocol,
char *connectAddress, char *connectPort, int connectProtocol,
int serverTimeout, char *sourceAddress);

76
src/types.h Normal file
View File

@@ -0,0 +1,76 @@
/* Copyright © 1997—1999 Thomas Boutell <boutell@boutell.com>
and Boutell.Com, Inc.
© 2003—2021 Sam Hocevar <sam@hocevar.net>
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. */
#pragma once
#include <time.h>
#include <stdint.h>
typedef enum _rule_type ruleType;
enum _rule_type {
allowRule,
denyRule,
};
typedef struct _rule Rule;
struct _rule
{
char *pattern;
ruleType type;
};
typedef struct _server_info ServerInfo;
struct _server_info {
SOCKET fd;
/* In network order, for network purposes */
struct addrinfo *fromAddrInfo, *toAddrInfo, *sourceAddrInfo;
/* In ASCII, for logging purposes */
char *fromHost, *toHost;
/* Offset and count into list of allow and deny rules. Any rules
prior to globalAllowRules and globalDenyRules are global rules. */
int rulesStart, rulesCount;
/* Timeout for UDP traffic before we consider the connection
was dropped by the remote host. */
int serverTimeout;
};
typedef struct _socket Socket;
struct _socket
{
SOCKET fd;
int family, protocol;
/* recv: received on this socket
sent: sent through this socket from the other buffer */
int recvPos, sentPos;
uint64_t totalBytesIn, totalBytesOut;
char *buffer;
};
typedef struct _connection_info ConnectionInfo;
struct _connection_info
{
Socket remote, local;
struct sockaddr_storage remoteAddress;
time_t remoteTimeout;
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;
};