`
isiqi
  • 浏览: 16705192 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

使用htpasswd.pl脚本远程管理apache密码文件

阅读更多
在搭建trac/svn系统时,一般都会采用apache的.htacces 认证方法 但trac本身并不提供修改密码的功能,修改密码只能通过htpasswd/htpasswd2命令来进行,这的确是一件相当不make sense的事。
其实,利用一个免费的perl脚本可以方便的通过http方式修改apache的认证文件。
  文件名:htpasswd.pl,获取地址http://home.xnet.com/~efflandt/pub/htpasswd.pl
该脚本可以通过web浏览器从你的htpasswd文件直接增加或者删除用户,管理者密码是经过加密的。该脚本本身并不保护一个目录,也不创建一个口令保护功能。它仅用来帮助你管理你的口令文件。这就是说在你的服务器上事先应有口令文件时,该脚本方可发挥作用。

安装&配置

step1.拷贝htpasswd.pl至cgi-bin目录
suse对应/srv/www/cgi-bin
fedora对应/var/www/cgi-bin
step2.改名
把htpasswd.pl改名为htpasswd.cgi
step3.用文本编辑器打开配置脚本(cfg.pl)
编辑如下变量:
#!/usr/local/bin/perl 修改为 #!/bin/perl
配置要修改的apache认证文件,找到以下几行
# Password file with full system path (where not accessible by URL).
$file = '/full_path_to/.htpasswd'; 修改为 $file='/etc/svn-auth-file'#假设你的验证文件是/etc/svn-auth-file
step4 chmod 755 htpasswd.cgi

不出意外的话,访问http://localhost/cgi-bin/htpasswd.cgi即可出现密码修改页面
有关密码文件创建,请参见 http://blog.csdn.net/forlinux/archive/2006/06/11/787703.aspx

-----htpasswd.pl-------
  1. #!/usr/local/bin/perl-T
  2. #htpasswd.cgibyDavidEfflandt(efflandt@xnet.com)8/97
  3. #Lastupdate10/4/99
  4. #
  5. #Updatepasswordfilefromthewebforusewithuserauthentication.
  6. #Storeseachlineintheformat:username:crypted_password
  7. #
  8. #Built-informisprovidedifyouGETthescript.
  9. #FormisprocessedifyouPOSTtothisscript.
  10. #
  11. #Ifyouwantyourpasswordstobesecure,itisbesttorunthis
  12. #suidasyou(chmod4705htpasswd.cgi)whichmayrequireCwrapper.
  13. #Alsokeepthisscriptinadirectorythatrequiresuserauthentication
  14. #unlessyouallownewuserstosettheirownpassword(see$allow_new).
  15. #
  16. #Ifnotrunningsuidyoushouldtouchthepasswordfilefirstand
  17. #chmod606(orwhateverisreq'dtoaccessitasyouandwebserver).
  18. #
  19. #Toaddorremoveusersbyanadministrator,createausercalled'admin'
  20. #withapassword.Enterusernameyouwanttoaddorremovewithadmin
  21. #passwordas"CurrentPassword"(plusnewpasswordsfornewusers).
  22. #
  23. #Anyonemayremovetheirownnamefromthepasswordfileiftheysupply
  24. #theircorrectpassword.
  25. ###Variables
  26. #Passwordfilewithfullsystempath(wherenotaccessiblebyURL).
  27. $file='/full_path_to/.htpasswd';
  28. #Allowanyonetoaddnewusers(1=yes,0=no)
  29. $allow_new=0;
  30. #Setuntaintedpathforsuidscripts
  31. $ENV{PATH}='/bin:/usr/bin:/usr/local/bin';
  32. $ENV{IFS}=""if$ENV{IFS}ne"";
  33. ###EndofVariables
  34. #CreateformandexitonGET
  35. &make_formunless($ENV{'REQUEST_METHOD'}eq"POST");
  36. #GetPOSTinput
  37. read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
  38. #Splitthename-valuepairs
  39. @pairs=split(/&/,$buffer);
  40. foreach$pair(@pairs)
  41. {
  42. ($name,$value)=split(/=/,$pair);
  43. $value=~tr/+//;
  44. $value=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  45. $name=~tr/+//;
  46. $name=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  47. $FORM{$name}=$value;
  48. }
  49. if($FORM{user}){
  50. $user=$FORM{user};
  51. }else{
  52. &error("Error","Usernamemissingfromform.");
  53. }
  54. $pwd=$FORM{old};
  55. $command=$FORM{command};
  56. unless(($commandeq'remove')
  57. ||($FORM{new}&&$FORM{new}eq$FORM{new2})){
  58. &error("PasswordMismatch","Newpasswordmismatchormissing.");
  59. }
  60. #Getexistingpasswords
  61. if(-e$file){
  62. open(IN,$file)or&error("Error","Can'topenpasswordfile:$!");
  63. flock(IN,2);
  64. seek(IN,0,0);
  65. while(<IN>){
  66. chomp;
  67. ($name,$value,$tail)=split(/:/,$_,3);
  68. $hash{$name}=$value;
  69. $tail{$name}=$tail;#maintainanyadditionalfields
  70. }
  71. closeIN;
  72. }
  73. #Saltforcrypt
  74. @range=('0'..'9','a'..'z','A'..'Z','.','/');
  75. srand(time()^($$+($$<<15)));
  76. $salt=$range[rand(int($#range)+1)].$range[rand(int($#range)+1)];
  77. #Checkforvalidpasswordorexistinguser
  78. $pass=$hash{$user}if$hash{$user};
  79. $cpwd=crypt($pwd,$pass);
  80. $admin=$hash{admin}&&crypt($pwd,$hash{admin})eq$hash{admin};
  81. if(($commandne'new')&&($admin||$pass&&$cpwdeq$pass)){
  82. if($commandeq'remove'){
  83. delete($hash{$user});delete($tail{$user});
  84. $msg="User<B>$user</B>wasremovedfrompasswordfile.";
  85. }elsif(!$pass){
  86. $msg="WARNING!'ChangePassword'checkedfornon-existinguser?\n"
  87. ."<P>Assigningpasswordfornewuser<B>$user</B>anyway.\n"
  88. ."<P>Ifthiswasanerror,gobackand'RemoveUser'";
  89. }else{
  90. $msg="Passwordhasbeenupdatedfor$user.";
  91. }
  92. }elsif($FORM{command}eq'new'){
  93. if($pass){
  94. &error("Sorry","User<B>$user</B>isalreadyassigned.");
  95. }elsif($allow_new||$admin){
  96. $msg="Passwordhasbeenassignedfornewuser$user.";
  97. }else{
  98. &begin_html("Sorry,NewUser");
  99. print"Contactfileownerforpasswordyoucanchangelater.";
  100. &end_html;
  101. exit;
  102. }
  103. }else{
  104. &error("PasswordError",
  105. "Invaliduserorpasswordorforgottocheck'NewUser'.");
  106. }
  107. #Assignnewpasswordtouserandwritetofile
  108. $hash{$user}=crypt($FORM{new},$salt)if$commandne'remove';
  109. if(open(OUT,">$file")){
  110. flock(OUT,2);
  111. seek(OUT,0,0);
  112. foreach$name(sortkeys%hash){
  113. printOUT"$name:$hash{$name}";
  114. printOUT":$tail{$name}"if$tail{$name};
  115. printOUT"\n";
  116. }
  117. }else{
  118. &error("Error","Can'tupdatepasswordfile:$!");
  119. }
  120. #PrintReturnHTML
  121. &begin_html("ThankYou");
  122. print"$msg\n";
  123. &end_html;
  124. ###Subroutines
  125. #subroutinebegin_html(title)
  126. subbegin_html{
  127. local($title)=@_;
  128. print"Content-type:text/html\n\n";
  129. print"<html><head><title>$title</title></head><body>\n";
  130. print"<center><h1>$title</h1></center>\n<hr><p>\n";
  131. }
  132. #subroutineend_html
  133. subend_html{
  134. #Addfooterlinkshere
  135. print"<P></body></html>\n";
  136. }
  137. #subroutinemake_form
  138. submake_form{
  139. &begin_html("ChangeorAddPassword");
  140. print<<NEW_FORM;
  141. Usethisformtochangeyourpasswordforaccesstorestricted
  142. directorieshere.Newuserswillbeinformedifpasswordwasassignedor
  143. iftheyneedtocontacttheownerofthesepages.
  144. <FORMMETHOD="POST"ACTION="$ENV{SCRIPT_NAME}">
  145. <DL>
  146. <DT>E-mailAddress(orusernameonthissystem):
  147. <DD><INPUTNAME="user">
  148. <DT>CurrentPassword(requiredunlessnewuser):
  149. <DD><INPUTTYPE=PASSWORDNAME="old">
  150. <DT>NewPassword:
  151. <DD><INPUTTYPE=PASSWORDNAME="new">
  152. <DT>ConfirmNewPassword:
  153. <DD><INPUTTYPE=PASSWORDNAME="new2">
  154. <DT>Request:
  155. <DD>
  156. <INPUTTYPE="radio"NAME="command"VALUE="change"CHECKED>ChangePassword
  157. <DD>
  158. <INPUTTYPE="radio"NAME="command"VALUE="new">NewUser
  159. <DD>
  160. <INPUTTYPE="radio"NAME="command"VALUE="remove">RemoveUser
  161. </DL>
  162. <P><INPUTTYPE="submit"VALUE="SubmitRequest">
  163. </FORM>
  164. NEW_FORM
  165. &end_html;
  166. exit;
  167. }
  168. suberror{
  169. local($title,$msg)=@_;
  170. &begin_html($title);
  171. print"<P>$msg\n";
  172. print"<P>Pleasecheckyournameandre-enterpasswords.\n";
  173. &end_html;
  174. exit;
  175. }


分享到:
评论

相关推荐

    Apache2认证部署.pdf

    为了创建用户密码文件,在步骤25中,使用`htpasswd`命令创建了用户名为`user03`的用户,并要求用户输入密码。这将密码哈希存储在`/opt/http`文件中,以便Apache2在用户尝试访问受保护的资源时进行身份验证。 然后,...

    nginx 目录密码保护的设置方法

    如果你的系统中已经安装了 Apache,可以直接使用 `htpasswd` 工具来生成加密后的密码文件。命令如下: ```bash htpasswd -cm /root/htpasswd 用户名 ``` 这里 `-c` 表示创建新文件,`-m` 表示使用 MD5 加密方式。 ...

    .htaccess

    个人档案 查看文章 .htaccess怎么用2007-05-16 14:04(文章来源)http://www.dnpark.com.cn/news/mm/www/1179329504375ZKlMSgYr.html&lt;br&gt;&lt;br&gt;Apache服务器的.htaccess是一个非常强大的分布式配置文件,学会使用....

    Redhatlinux下cvs的安装配置.pdf

    密码可以通过perl脚本`/home/cvsroot/passwdgen.pl`生成,或者使用Apache的htpasswd工具。 为了让其他系统用户使用CVS,需要将他们的主属组更改为cvs,这需要编辑`/etc/passwd`文件。 最后,确保CVS的可执行文件...

    安装nagios并且邮件报警

    其中`$USER1$/check_by_email.pl`是一个脚本,负责通过SMTP服务器发送邮件。这个脚本需要根据实际使用的SMTP服务器进行编写。 3. **测试邮件发送** 在完成以上配置之后,可以通过模拟故障来测试邮件是否能够成功...

    Oracle9i.iSqlplus

    5. **数据库管理操作**:登录成功后,用户可以通过iSQL*Plus执行各种数据库管理操作,如查询数据、执行PL/SQL脚本、创建表空间等。 #### 四、iSQL*Plus优势 1. **易于部署**:iSQL*Plus不需要额外的客户端软件安装...

Global site tag (gtag.js) - Google Analytics