Score:0

Strange record in /etc/shadow after useradd -p and crypt function in bash script

jp flag

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.

fo flag
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.
jp flag
I have added missed quotes but it is not the reason of my problem.
dave_thompson_085 avatar
jp flag
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`.
jp flag
@dave_thompson_085 Thank you! Your comment has dispelled my doubts.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.