1

I've put this script in /etc/init.d, when some user changes password, this script updates it on another file data.txt, It is working fine, but when i shut down my computer it was not doing so showing ubutnu screen at shutting down, n when i shut down it manually it automatically restarts itself,

By removing this script and commenting outer while loop, computer can easily be shut down.

Here is my code

while inotifywait -e attrib /etc/shadow; do
         #edit user
            while IFS=: read -r f1 f2
                do
                user=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 1);
                pwd=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 2);
                    if [ "$f2" != "$pwd" ]; then
                    #echo "changed";
                    #search for password, and repalce it with new one
                    sed -i 's@'$f2'@'$pwd'@' $file;
                    #upload file data.txt to server         
                    fi
            done < $file
        #end edit user
    done

Please tell me what I'm doing wrong?

Robin Green
  • 32,079
  • 16
  • 104
  • 187
Fatima Zohra
  • 2,929
  • 2
  • 17
  • 17

1 Answers1

0

I believe the above will not work for what you're trying to achieve. That script is waiting on an "attrib" event on /etc/shadow, but your file might have been modified long before the user restarted/stopped the machine, hence it will hang indefinitely. If you want to use it as an init script you will have to do the check asynchronously, for example by storing the /etc/shadow md5sum in a file and then comparing it at shutdown. This might be a possible solution:

#Define some useful variables
SHADOWFILE="/etc/shadow"
SHADOWFILE_OLD_SUM="/root/.sum5_shadow_prev"
DATAFILE="/root/.data.txt" # Or /var/pwdchange/data.txt, ...

# If the file exists, extract previous checksum
if [[ -f "$SHADOWFILE_OLD_SUM" ]]; then
    # Extract previous checksum
    OLDSUM="`cat $SHADOWFILE_OLD_SUM`"
    #Calculate current checksum
    CURSUM="`md5sum $SHADOWFILE | sed 's: .*::'`"
    if [[ "$OLDSUM" == "$CURSUM" ]]; then
        # No modification, exit gracefully
        exit 0
    else
        # Do file parsing and update data accordingly
        while IFS=: read -r f1 f2; do
            user=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 1);
            pwd=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 2);
            if [ "$f2" != "$pwd" ]; then
                #echo "changed";
                #search for password, and repalce it with new one
                sed -i 's@'$f2'@'$pwd'@' $DATAFILE;
                #upload file data.txt to server         
            fi
        done < $DATAFILE
    fi
else
    # First run, calculate current hash, store it, and do an update pass (extract from $SHADOWFILE, as $DATAFILE might not exist)
    md5sum $SHADOWFILE | sed 's: .*::' > $SHADOWFILE_OLD_SUM
    # Remove $DATAFILE regardless whether it exists (avoids duplicates if $DATAFILE exists, as later we do append-only)
    rm -f $DATAFILE
    # Generate first datafile
    while IFS=: read -r f1 f2; do
        user=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 1);
        pwd=$(sudo grep "$f1" /etc/shadow | cut -d':' -f 2);
        echo "$user:$pwd" >> $DATAFILE
    done < $SHADOWFILE
fi