Newer
Older
casp / NetBSD8 / adduser / adduser
@HIROSE Yuuji HIROSE Yuuji on 17 Sep 2019 1 KB Required files located
#!/bin/sh
final() {
  stty echo
  exit
}

trap final INT QUIT TERM TERM EXIT

readp() {
  prompt=${1:-"Password: "}
  while true; do
    stty -echo
    echo -n "$prompt" >&2
    read x
    case "$x" in
      ????????*) ;;
      *)	echo "Password too short.  Longer one please." >&2
		continue ;;
    esac
    case "$x" in
      *[A-Za-z]**) ;;
      *)	echo "Password should have some Alphabet." >&2
		continue ;;
    esac
    case "$x" in
      *[0-9]**) ;;
      *)	echo "Password should have some number(digit)." >&2
		continue ;;
    esac
    break
  done
  stty echo
  echo "$x"
  echo >&2
}

# Reading user
while true; do
  echo -n "New user name: "
  read user
  if getent passwd "$user" >/dev/null 2>&1; then
    echo "User [$user] already exists.  Try another name."
    continue
  fi
  break
done
# Read password
while true; do
  p1=`readp "New Password: "`
  p2=`readp "Password Again: "`
  if [ x"$p1" != x"$p2" ]; then
    echo "Password mismatch!! Try again..." >&2
    continue
  fi
  enc=`pwhash "$p1"`
  if useradd -p "$enc" -g users -G operator -m -s /bin/zsh $user; then
    chmod 755 /home/$user			# Workaround for NetBSD8
    cat<<-EOF
	=====================================================
		User [$user] successfully created
	=====================================================
	EOF
    sleep 4
    break
  fi
done