0

Ubuntu 20.04 LTS.
There is a simple bash script to add a new user via command line in interactive mode:

#!/bin/bash
# Script to add a user to Linux system
if [ "$(id -u)" -eq 0 ]; then
    read -p "Enter username : " username
    read -s -p "Enter password : " password
    egrep "^$username" /etc/passwd >/dev/null
    if [ $? -eq 0 ]; then
        echo "$username exists!"
        exit 1
    else
        pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
        useradd -m -p $pass $username
        [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
    fi
else
    echo "Only root may add a user to the system"
    exit 2
fi

The result inside the /etc/shadow file after adding the user through that script looks pretty weird. For example for username demo and password demo, the useradd command adding to /etc/shadow file:

demo:paR7EXftedvjA:19081:0:99999:7:::

There is no information about id, param, and salt as it should be described in the currently accepted form. Looks like it's just a hash or I don't know what is that paR7EXftedvjA. I've tried to get it back using demo as salt and demo as password in commands like mkpasswd or openssl but the result is not the same.

Skuld
  • 1
  • 1
  • I don't know how to resolve the problem. However, there are quite a few problems with this code. Paste it into https://www.shellcheck.net for some assistance. It's possible the plaintext or encrypted password got mangled due to the missing quotes. – glenn jackman Mar 30 '22 at 21:54
  • I have added missed quotes but it is not the reason of my problem. – Skuld Mar 30 '22 at 22:13
  • 2
    Your perl (or more likely the platform or OS it is running on, which you didn't mention) is using the 'traditional' tweaked-DES method [described here](https://perldoc.perl.org/functions/crypt) which was many years before the invention of [the 'modular' syntax](https://en.wikipedia.org/wiki/Crypt_(C)). `openssl passwd -crypt -salt pa demo` produces exactly `paR7EXftedvjA`. – dave_thompson_085 Mar 31 '22 at 03:32
  • @dave_thompson_085 Thank you! Your comment has dispelled my doubts. – Skuld Mar 31 '22 at 15:31

0 Answers0