jambocool has asked for the wisdom of the Perl Monks concerning the following question:

Hey Guys, Well im trying to create a user management system using Perl. Ive so far got it add users, and delete users without a problem. But the Update user part is causing me problems galore! So can anyone help me out with some pointers as to why the script is not performing properly please? I am able to input the user i want to update, but sometimes it says my input password is wrong, when its not.. and it also is asking for another input when it shouldn't?

#!/usr/bin/perl -w print "\n\n\n\n\n\n\n------------------------------------------------- +---------------------"; print "\nWelcome to Jamie's User Management System. \n\nPlease select +the option that you wish to use:\n -a -- To create a new username and + password.\n -u -- To update a users password.\n -d -- To delete a us +er from the system.\n -h -- For more help!\n\n -q -- To quit this sys +tem\n\n "; # Take initial input from user print "\nPlease input your desired first Option --> : "; chomp ($option = <>); # #Determine what option is selected # Add in a new user # if ($option eq "-a") { print "\n\n\n--- Add User Menu --- \n"; print "\nEnter User Name --> : "; chomp ($userName = <>); open my $fh, "<", 'passwords.txt' or die "Can't open password file: $! +"; while (<$fh>) { chomp; ($name,$passwd) = split(/:/); if ($userName eq $name) { print "Sorry That Username already Exists"; print "\nEnter User Name --> : "; chomp ($userName = <>); } } print ("Enter a secret password:"); system ("stty -echo"); $inputline1 = <STDIN>; system ("stty echo"); chop ($inputline1); print ("\nPlease enter your password in again:"); system ("stty -echo"); $inputline2 = <STDIN>; system ("stty echo"); chop ($inputline2); #Verify that password has been entered correctly $outputline = $inputline1 eq $inputline2 ? "\n\nYes, that is the correct password!\n" : "\n\nNo, that is not the correct password.\n"; print ($outputline); #Encrypt the password $salt = $userName; $encryptpw = crypt ($inputline2,$salt); #Write username and password to the passwords.txt file open (FILE, ">>passwords.txt"); print FILE "$userName:$encryptpw\n"; close (FILE); print ("Your Username and Password have been saved!\n"); # Take input from user print "\nPlease input your next Option ( -u,-d,-h,-q ) --> : "; chomp ($option = <>); } # # Update existing user # if ($option eq "-u") { print "\n\n\n--- Update User Password Menu --- \n"; print "\nEnter Your Current User Name --> : "; chomp ($userName2 = <STDIN>); print ("Enter Your Current Password:"); system ("stty -echo"); $inputline3 = <STDIN>; system ("stty echo"); chop ($inputline3); #Encrypt the password $salt = $userName2; $encryptpw2 = crypt ($inputline3,$salt); print "$encryptpw2\n"; open my $fh, "<", 'passwords.txt' or die "Can't open password file: $! +"; while (<$fh>) { chomp; ($name,$passwd) = split(/:/); print "$passwd\n"; if ($encryptpw2 eq $passwd) { print ("\nPlease Enter Your new password:"); system ("stty -echo"); $newpass = <STDIN>; system ("stty echo"); chop ($newpass); print ("\nPlease enter your new password in again:"); system ("stty -echo"); $newpass2 = <STDIN>; system ("stty echo"); chop ($newpass2); } elsif ($encryptpw2 ne $passwd) { die ("\n\nYour Passwords Did Not Match, Please Try running the + script again. \n\n"); } if ($newpass eq $newpass2) { #Encrypt the password $salt2 = $userName2; $encryptpw3 = crypt ($newpass2,$salt2); use Tie::File; my $file_name = 'passwords.txt'; tie @array, 'Tie::File', $file_name or die; for (@array) { s/$inputline3/$encryptpw3/g; print ("\n\nYour Username and Password have been updated and s +aved!\n\n"); } untie @array; } elsif ($newpass ne $newpass2) { die ("\n\nYour Passwords Did Not Match, Please Try again. \n\n"); } } # Take input from user print "\nPlease input your next Option ( -u,-d,-h,-q ) --> : "; chomp ($option = <>); }

This is whatshappening when i try to run it, showing me the encrypted passwords as i input and its reads them too...
---------------------------------------------------------------------- Welcome to Jamie's User Management System. Please select the option that you wish to use: -a -- To create a new username and password. -u -- To update a users password. -d -- To delete a user from the system. -h -- For more help! -q -- To quit this system Please input your desired first Option --> : -u --- Update User Password Menu --- Enter Your Current User Name --> : test Enter Your Current Password:teH0wLIpW0gyQ jrsWIWyBMGLdM Your Passwords Did Not Match, Please Try running the script again. % cat passwords.txt jrojas:jrsWIWyBMGLdM test:teH0wLIpW0gyQ

Replies are listed 'Best First'.
Re: User management system; file input not working?
by elmex (Friar) on Apr 20, 2008 at 06:52 UTC

    Hi! First of all, the code is a bit chaotic/messy IMO. I suggest that you work with a bit more whitespace and subroutines. Thats of course a matter of style, but a cleaner style often leads to easier discoverable bugs.

    To the code: I've read through it mostly and one line was IMO fishy (see around line 109):

    for (@array) { s/$inputline3/$encryptpw3/g; print ("\n\nYour Username and Password have been updated a +nd saved!\n\n"); }

    $inputline3 contains the entered password and not the encrypted password. Is that maybe a mistake? Also note that if the username is the same as an encrypted password, it will also be replaced with the new password. I suggest to make the substitution more precise:

    s/^([^:]):\Q$encryptpw2\E/$1:$encryptpw2/

    The \Q...\E is just for the case that $encryptpw2 contains '.' or '\s+' or '[' or whatever character might have some other meaning in a regex.

Re: User management system; file input not working?
by alexm (Chaplain) on Apr 20, 2008 at 14:48 UTC
    Maybe, you shoud consider System Tools Backends which is written in Perl and already installed in most GNU+Linux distributions (also available for *BSD and other Unices).
Re: User management system; file input not working?
by walto (Pilgrim) on Apr 20, 2008 at 13:27 UTC
    I would use
    chomp $inputline3
    if you want to get rid of or trailing newline.
Re: User management system; file input not working?
by apl (Monsignor) on Apr 20, 2008 at 13:13 UTC
    I'm very interested in how people approach debugging... What steps did you take in trying to determine the cause of the problem before you posted?