🍗 Wiki

Internet Service Daemon

Internet Service Daemon

Internet Service Daemon, as known as inetd is a daemon to manage network services. It was used to manage telnet and ftp daemon, and some custom programs by server operators.

1. First introduced

The inetd was first introduced in 4.3 BSD.

2. Successors

But due to limitation of original inetd, modern Linux and MacOS uses xinetd. The x in xinetd means 'eXtended'.

xinetd-org/xinetd - GitHub: xinetd is a secure replacement for inetd. (Dead project) openSUSE/xinetd: openSUSE fork to contain all the patches we(editor: openSUSE project maintainers and contributors) had.

The xinetd successfully replaced the original inetd in Linux and MacOS. But less and less people are using inetd and xinetd nowadays. Systemd is a well maintained and stable enough.

There are ynetds, yet another inetd I think.

Also, you can try Socat to bind standard I/O of programs to TCP sockets.

3. Example: Setting up a simple echo server

In this section, we will try to build a simple echo server, written in C. Be sure, the program is extremely vulnerable.

The example is written in C, but any programming language that supports standard inputs and outputs can be used instead.

The example was tested on Ubuntu 22.04 + WSL. Install xinetd using apt or dnf, whatever package manager you want.

$ sudo apt install xinetd

3.1. Write code in C

The concept is really simple. It takes user inputs from stdin(scanf), and print to the stdout(printf).

$ cat echo.c
#include <stdio.h>

int main() {
  char input[0x40] = {0,};
  scanf("%s", input);
  printf("%s", input);
  return 0;
}

Then compile it with the gcc. I haven’t tested yet, the clang would work, too.

$ gcc echo.c -o /home/ch1keen/myecho -fno-stack-protector

Then test the program.

$ /home/ch1keen/myecho
aaaabbbb
aaaabbbb$

It is shabby though, it works as I expected. Let’s serve it through the internet.

3.2. Edit inetd.conf to make the program work

You can define the myecho service in the xinetd configuration file. You can specify which file to be run, port to be used, and even choose to serve the service in a single threaded or a multi threaded.

Create a file called myecho, which would be the configuration file of the myecho program that you compiled. You do not need to append the file extension, .conf for example.

# cat > /etc/xinetd.d/myecho
service myecho
{
  disable = no          # The service is disabled or not.
                        # Switch it to 'yes' if you want to stop the service temporarily.
  flags = REUSE         # SO_REUSEADDR socket option
  socket_type = stream
  wait = no             # Multi thread the service.
                        # Switch it to 'yes' if you want to serve it in a single thread.
  user = root
  server = /home/ch1keen/myecho
  port = 3333           # The same port number should be described in /etc/services
}

Specify the port number that you want to serve the program in port line. And make sure the server option is pointing the program that you compiled in the previous section.

Add the line to the /etc/services. Remember the port 3333 would be used to serve it.

echo "myecho  3333/tcp" | sudo tee -a /etc/services

Then restart the xinetd service.

systemctl restart xinetd

Check if the service is well configured.

$ nc localhost 3333
aaaabbbb
aaaabbbb
$

Sometimes it chews the enter input, whatever, it works!