Gossamer Forum
Home : Products : DBMan : Discussions :

Lookup corrupts default.pass file

Quote Reply
Lookup corrupts default.pass file
While the sub routines for setting a new e-mail address and password work fine, the routine 'lookup' results in the password file (with dozens of records) being overwritten with just the last record. I can't see what's wrong - having compared where my script has got to against the original.

Does anyone have any pointers as to what I should check?

--------- EXTRACT --------
open (PASS, ">$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
print PASS $output;
close PASS;

($userid, $pw, $view, $add, $del, $mod, $admin, $email) = split (/:/, $found);

$password = &generate_password;
srand( time() ^ ($$ + ($$ << 15)) ); # Seed Random Number
my @salt_chars = ('A' .. 'Z', 0 .. 9, 'a' .. 'z', '.', '/');
my $salt = join '', @salt_chars[rand 64, rand 64];
my $encrypted = crypt($password, $salt);

open (PASS, ">>$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
print PASS "$userid:$encrypted:$view:$add:$del:$mod:$admin:$in{'email'}\n";
close PASS;

Quote Reply
Re: Lookup corrupts default.pass file In reply to
You seem to be opening your .pass file in write mode rather than append mode:

open (PASS, ">>$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
print PASS $output;
close PASS;


Add the extra >, that should do it.

- Mark


Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: Lookup corrupts default.pass file In reply to
Thanks for picking up on this. I'm now correctly writing the old password records and appending the one amended in sub lookup.

Quote Reply
Re: Lookup corrupts default.pass file In reply to
I'm still getting occasional corruption caused, I think, by sub lookup. Usually, all works fine. But sometimes, the unchanged records are not written to the file, just the changed record is "appended" as the only record. A subsequent change results in the first changed record being written and the second being appended (but, of course, all the old records being lost).

Now, AFAIK, it's only the lookup and change_password sub-routines which could be causing the problem. I've checked that the unchanged records are written to the file - using (PASSWD, ">$auth_pw_file") - and then the changed record appended - using (PASSWD, ">>$auth_pw_file") - in both sub-routines. Just can't see the problem!

I've taken the liberty of including the key parts of the lookup sub-routine. Does a kind soul have any ideas?

TIA,

----- Key parts of sub lookup -----

open (PASSWD, "<$auth_pw_file") || &cgierr("unable to open password file. Reason: $!\n");
@passwds = <PASSWD>;
close PASSWD;
$found = '';
foreach $pass (@passwds) { # Go through each pass and see if we match.
chomp ($pass);
if ($pass =~ /$in{'email'}/) {
$found = $pass;
}
else {
$output .= $pass . "\n";
}
}
if (!$found) {
etc.;
return;
}
else {
open (PASS, ">$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
print PASS $output;
close PASS;

($userid, $pw, $view, $add, $del, $mod, $admin, $email) = split (/:/, $found);
$password = &generate_password;
srand( time() ^ ($$ + ($$ << 15)) ); # Seed Random Number
my @salt_chars = ('A' .. 'Z', 0 .. 9, 'a' .. 'z', '.', '/');
my $salt = join '', @salt_chars[rand 64, rand 64];
my $encrypted = crypt($password, $salt);

# Add the userid into the file with default permissions.
open (PASSWD, ">>$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASSWD, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
print PASSWD "$userid:$encrypted:$view:$add:$del:$mod:$admin:$email\n";
close PASSWD;

Quote Reply
Re: Lookup corrupts default.pass file In reply to
else {
open (PASS, ">$auth_pw_file") or &cgierr ("unable to open: $auth_pw_file.\nReason: $!");
if ($db_use_flock) {
flock(PASS, 2) or &cgierr("unable to get exclusive lock on $auth_pw_file.\nReason: $!");
}
print PASS $output;
close PASS;


Just fix that one up (add another ">")

- Mark


Astro-Boy!!
http://www.zip.com.au/~astroboy/
Quote Reply
Re: Lookup corrupts default.pass file In reply to
Thanks for that. I tried it, but it appends the entire contents of the file to itself. So I've now switched that one back to a single ">".

I'm a bit fresher today and spotted one difference that might just be relevant - the writing of the unchanged records uses PASS to reference the file and then the appending of the changed file used PASSWD. Yet in sub change_password and sub change_email, PASS is used on both occasions. I have changed sub lookup to be consistent. Is that OK to do and might it solve the problem?

Thanks,