Netcat as a inetd service on port 1815 and redirect incomming to /dev/ttyS2

0

I'm trying to let my Debian 7 system to listen on port 1815 with netcat and redirect the incoming traffic to a VacuumFluorecentDisplay. Iptables are open and I'm using inetd instead of xinetd(came withe the distro). But i can't get it to work like i want

added the following line to /etc/services

vfd             1815/tcp                      
vfd             1815/udp    

added the following line to /etc/inetd.conf.

vfd stream tcp nowait  root     /bin/nc "-l 1815 > /dev/ttyS2"

Steven

Posted 2019-06-27T12:43:22.190

Reputation: 1

Answers

1

This is wrong in almost every possible way.

  1. Command-line parameters in inetd need to be specified as individual words – not quoted as a single string. For example (only to demonstrate the syntax change; it still won't work as-is):

    -- vfd stream tcp nowait root /bin/nc "-l 1815 > /dev/ttyS2"
    ++ vfd stream tcp nowait root /bin/nc -l 1815 > /dev/ttyS2
    
  2. For historical reasons, inetd requires the service's binary and the 0th command-line argument to be specified separately, even though they're normally the same. For example (note that 'nc' is now specified twice):

    -- vfd stream tcp nowait root /bin/nc -l 1815 > /dev/ttyS2
    ++ vfd stream tcp nowait root /bin/nc nc -l 1815 > /dev/ttyS2
    
  3. inetd does not use a shell to start the services, so there is nothing that would interpret the > /dev/ttyS2 redirection; everything is simply passed to nc as a command-line argument, and nc doesn't know what to do with it.

    This one requires significant changes – either explicitly using a shell to run your command...

    -- vfd stream tcp nowait root /bin/nc nc -l 1815 > /dev/ttyS2
    ++ vfd stream tcp nowait root /bin/sh sh -c "nc -l 1815 > /dev/ttyS2"
    

    ...or using an entirely different tool, one that is able to open the file without relying on shell redirection:

    -- vfd stream tcp nowait root /bin/nc nc -l 1815 > /dev/ttyS2
    ++ vfd stream tcp nowait root /bin/socat socat -u tcp-listen:1815 file:/dev/ttyS2
    
  4. Finally, two programs cannot be told to individually listen on the same port. The whole point of inetd is that inetd and only inetd will create the initial "listening" socket, and the inetd-based services will only use the sockets that they've inherited from the "parent" inetd. (In 'wait' mode they inherit the "listening" socket, and in "nowait" mode they inherit the individual client-connection sockets.)

    In other words, it doesn't make sense to use nc -l as an inetd service, because you're asking it to duplicate everything that inetd has already done. Instead, the service needs to use the existing stdin/stdout (which inetd has attached to the connection socket).

    For example, this should finally work correctly:

    vfd stream tcp nowait root /bin/sh sh -c "cat > /dev/ttyS2"
    

    This should also work:

    vfd stream tcp nowait root /bin/socat socat -u stdio file:/dev/ttyS2
    

user1686

Posted 2019-06-27T12:43:22.190

Reputation: 283 655

I read your answers and sometimes I think you're the Nicolas Bourbaki of Super User. If you're just one man, all the more kudos to you.

– Kamil Maciorowski – 2019-06-27T17:06:18.583