#!/usr/bin/perl -wTI. # # Copyright (C) 2001 Klaus Wriessnegger, kw@sap.com # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details, published at # http://www.gnu.ai.mit.edu/copyleft/gpl.html #usage example: #R e s e t #
#Username
#New password #retype New password # #C h a n g e #
#
#Username
#Old password #New password #retype New password # # #
use CGI; use wiki; #print "Content-type: text/html\n\n"; $query= new CGI; &main(); sub main { # get all parameters from the form my $wikiName = $query->param( 'username' ); my $passwordA = $query->param( 'password' ); my $passwordB = $query->param( 'passwordA' ); #initialize my $topicName = $query->param( 'TopicName' ); my $thePathInfo = $query->path_info(); my $theUrl = $query->url; ( $topic, $webName ) = &wiki::initialize( $thePathInfo, $wikiName, $topicName, $theUrl ); my $text = ""; my $url = ""; # check if required fields are filled in if( ! $wikiName || ! $passwordA ) { $url = &wiki::getOopsUrl( $webName, $topic, "oopsregrequ", ); print $query->redirect( $url ); return; } # check if user entry exists if( ( $wikiName ) && (! htpasswdExistUser( $wikiName ) ) ){ # PTh 20 Jun 2000: changed to getOopsUrl $url = &wiki::getOopsUrl( $webName, $topic, "oopsnotwikiuser", $wikiName ); print $query->redirect( $url ); return; } # check if passwords are identical if( $passwordA ne $passwordB ) { $url = &wiki::getOopsUrl( $webName, $topic, "oopsregpasswd" ); print $query->redirect( $url ); return; } my $theCryptPassword = &htpasswdGeneratePasswd( $wikiName, $passwordA ); my $change = $query->param( "change" ) || ""; if( $change eq "on" ){ # c h a n g e my $oldpassword = $query->param( 'oldpassword' ); # check if required fields are filled in if( ! $oldpassword ) { $url = &wiki::getOopsUrl( $webName, $topic, "oopsregrequ" ); print $query->redirect( $url ); return; } # check password my $oldcrypt = htpasswdReadPasswd( $wikiName ); my $pw = htpasswdCheckPasswd( $oldpassword, $oldcrypt ); if (! $pw ){ # NO - wrong old password $url = &wiki::getOopsUrl( $webName, $topic, "oopswrongpassword"); print $query->redirect( $url ); return; } # OK - password may be changed my $oldCryptPassword = "$wikiName\:$oldcrypt"; htpasswdAddUser( $oldCryptPassword, $theCryptPassword ); # OK - password changed $url = &wiki::getOopsUrl( $webName, $topic, "oopschangepasswd" ); print $query->redirect( $url ); return; } else{ # r e s e t # and finally display the reset password page $url = &wiki::getOopsUrl( $webName, $wikiName, "oopsresetpasswd", $theCryptPassword ); print $query->redirect( $url ); return; } } sub htpasswdCheckPasswd { my ( $old, $oldcrypt ) = @_; my $salt = substr($oldcrypt, 0, 2); my $pwd = crypt( $old, $salt ); # OK if( $pwd eq $oldcrypt ){ return "1"; } # NO return ""; } sub htpasswdReadPasswd { my ( $user ) = @_; if( ! $user ) { return ""; } my $text = &wiki::readFile( $wiki::htpasswdFilename ); if( $text =~ /$user\:(\S+)/ ) { return $1; } return ""; } sub htpasswdExistUser { my ( $user ) = @_; if( ! $user ) { return ""; } my $text = &wiki::readFile( $wiki::htpasswdFilename ); if( $text =~ /$user\:/go ) { return "1"; } return ""; } sub htpasswdGeneratePasswd { my ( $user, $passwd ) = @_; # by David Levy, Internet Channel, 1997 # found at http://world.inch.com/Scripts/htpasswd.pl.html srand( $$|time ); my @saltchars = ( 'a'..'z', 'A'..'Z', '0'..'9', '.', '/' ); my $salt = $saltchars[ int( rand( $#saltchars+1 ) ) ]; $salt .= $saltchars[ int( rand( $#saltchars+1 ) ) ]; my $passwdcrypt = crypt( $passwd, $salt ); return "$user\:$passwdcrypt"; } sub htpasswdAddUser { my ( $oldUserEntry, $newUserEntry ) = @_; # can't use `htpasswd $wikiName` because htpasswd doesn't understand stdin # simply add name to file, but this is a security issue my $text = &wiki::readFile( $wiki::htpasswdFilename ); $text =~ s/$oldUserEntry/$newUserEntry/; &wiki::saveFile( $wiki::htpasswdFilename, $text ); }