OpenWrt Forum Archive

Topic: Alternative /etc/init.d/S50telnetd

The content of this topic has been archived on 6 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I'm using this script to avoid telnetd running when it isn't needed at all, ie: a) no failsafe mode, b) root passwd is set.

#!/bin/sh
FSAFE=0
PWDSET=0
# test failsafe mode
test -n "$FAILSAFE" && FSAFE=1
# check if root passwd is set
if [ -f /etc/passwd ]; then
  RPWD=$(cat /etc/passwd | grep root | cut -d":" -f2)
  if [ ".$RPWD" != ".*" ]; then
    PWDSET=1
  fi
fi
if [ ".$FSAFE" = ".1" ] || [ ".$PWDSET" = ".0" ]; then
  telnetd -l /bin/login
fi

Any side-effect?

Regards

Wallace

I would write it more like this, but it's more a style preference.

#!/bin/sh
PWDSET=0
# check if root passwd is set
if [ -f /etc/passwd ]; then
  RPWD=$(grep root /etc/passwd | cut -d":" -f2)
  if [ ".$RPWD" != ".*" ]; then
    PWDSET=1
  fi
fi
if [ -n "$FAILSAFE" ] || [ ".$PWDSET" = ".0" ]; then
  telnetd -l /bin/login
fi

This way should avoid an extra variable assignment for FSAFE, and process creation for grep

Wallace78 wrote:
# test failsafe mode
test -n "$FAILSAFE" && FSAFE=1

Why don't you use $FAILSAFE directly?

Wallace78 wrote:
# check if root passwd is set
if [ -f /etc/passwd ]; then
  RPWD=$(cat /etc/passwd | grep root | cut -d":" -f2)
  if [ ".$RPWD" != ".*" ]; then
    PWDSET=1
  fi
fi

You can do this in one go with awk. Using cat | grep | cut for this isn't necessary.

Here's my version:

#!/bin/sh
if awk -F: '/^root:/ && $2 !~ /\*/ {exit 1}' /etc/passwd 2>/dev/null || test $FAILSAFE; then telnetd -l /bin/login; fi

The discussion might have continued from here.