Utilize shell scripts,linux commands, open source tools,java, to Maximize the Power of Linux.
Focused on working with linux and shell, search engine technology including Chinese segmenter
Any questions please contact me at gmail: david.ullua

1/11/2007

to implement SVN password changing function by shell and php

We have install SVN as our version control system. It doesn't provide a remote password changing function on the web. I googled on the internet and didn't found a good solution. So I wrote a script in bash and a php front end page to do it.
(if you need a jsp or perl front end page, you can write one yourself)

Here's the shell script to change password for svn:

#!/bin/sh
#changepass.sh , function: change password for svn
#usage: changepass.sh username oldpass newpass newpassretype
if [ ! "$#" = 4 ]
then
echo "Usage:`basename $0` username oldpass newpass newpassretype"
exit 1
fi

passfile="/home/svn/passwd"

username=
oldpass=
newpass1=
newpass2=

username=$1
oldpass=$2
newpass1=$3
newpass2=$4

if [ ! $newpass1 = $newpass2 ]
then
#echo "password does not match!";
echo "password does not match!"
exit 2
fi

export NEWPASS=$newpass1
export USERNAME=$username

i=`sed /^"$username"[" ""\t"]*=[" ""\t"]*$oldpass\$/p $passfile -n|wc -l`
if [ $i = 1 ]
then
awk '{ if(index($0,ENVIRON["USERNAME"])==1) {print ENVIRON["USERNAME"] " = " ENVIRON["NEWPASS"] }else {print $0} }' $passfile > /home/svn/newpass

rm /home/svn/newpass 2>&1 1> /dev/null
cp /home/svn/newpass $passfile

echo 'password changed!'
elif [ `sed /"$username"/p $passfile -n |wc -l` > 0 ]
then
echo ' username and password does not match'
else
echo 'could not find username'
fi


here's the php front end changepass.php , it is for php 5, if you use php 4, maybe you need use $HTTP_POST_VARS instead $_POST ( just guess, i haven't test it):



if(strlen($_POST["pwd1"]) >0 ){
if($_POST["pwd1"]!=$_POST["pwd2"]){
print("Passwords do not match, try again.

\n");


}else{

$LoginName = $_POST["loginname"];
$OldPassword = $_POST["oldpwd"];
$NewPass1 = $_POST["pwd1"];
$NewPass2 = $_POST["pwd2"];
$retval='';
#echo $LoginName;
#echo $OldPassword;
// user apache or nobody(who runs httpd daemon) needs the permission of passwd file and temp file
$command = "/mnt/apache/changepass.sh ".$LoginName." ".$OldPassword." ".$NewPass1." ".$NewPass2 ;

#$command = "/bin/cat /mnt/apache/changepass.sh "." 2>&1";
$command = $command." 2>&1";

#system("/bin/echo abc",$retval);
system($command, $retval);

echo "
" ;echo $retval;echo '
';


if($retval!=0){
print("
Error while setting password:
\n");

}

}
}
else{
;#echo "parameters not set!";
}
?>





Login Name:
Old Password:
New Password:
Repeat Password:


the php page and shell scripts should works ok, but the php page shows nothing after click "submit" in fedora, however in slackware it is ok.

After debug step by step, found that shell script error message would not be ouput on the web page, so i add this line: $command = $command." 2>&1";

And finally found that SELinux would block some function on the web server. I disabled SELinux in the firewall settings, and it works.



No comments:

About Me

I am a senior developer and a team leader with 3 years development experience in Suzhou, China, focus on mobile web search, linux, Java and machine learning in NLP (natural language processing). My goal is to improve people's life with computer technology.