设为首页 收藏本站
查看: 630|回复: 0

[经验分享] 使用htpasswd.pl脚本远程管理apache密码文件

[复制链接]

尚未签到

发表于 2017-1-11 09:06:04 | 显示全部楼层 |阅读模式
在搭建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-------

  • #!/usr/local/bin/perl-T

  • #htpasswd.cgibyDavidEfflandt(efflandt@xnet.com)8/97
  • #Lastupdate10/4/99
  • #
  • #Updatepasswordfilefromthewebforusewithuserauthentication.
  • #Storeseachlineintheformat:username:crypted_password
  • #
  • #Built-informisprovidedifyouGETthescript.
  • #FormisprocessedifyouPOSTtothisscript.
  • #
  • #Ifyouwantyourpasswordstobesecure,itisbesttorunthis
  • #suidasyou(chmod4705htpasswd.cgi)whichmayrequireCwrapper.
  • #Alsokeepthisscriptinadirectorythatrequiresuserauthentication
  • #unlessyouallownewuserstosettheirownpassword(see$allow_new).
  • #
  • #Ifnotrunningsuidyoushouldtouchthepasswordfilefirstand
  • #chmod606(orwhateverisreq'dtoaccessitasyouandwebserver).
  • #
  • #Toaddorremoveusersbyanadministrator,createausercalled'admin'
  • #withapassword.Enterusernameyouwanttoaddorremovewithadmin
  • #passwordas"CurrentPassword"(plusnewpasswordsfornewusers).
  • #
  • #Anyonemayremovetheirownnamefromthepasswordfileiftheysupply
  • #theircorrectpassword.

  • ###Variables

  • #Passwordfilewithfullsystempath(wherenotaccessiblebyURL).
  • $file='/full_path_to/.htpasswd';

  • #Allowanyonetoaddnewusers(1=yes,0=no)
  • $allow_new=0;

  • #Setuntaintedpathforsuidscripts
  • $ENV{PATH}='/bin:/usr/bin:/usr/local/bin';
  • $ENV{IFS}=""if$ENV{IFS}ne"";

  • ###EndofVariables

  • #CreateformandexitonGET
  • &make_formunless($ENV{'REQUEST_METHOD'}eq"POST");

  • #GetPOSTinput
  • read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});

  • #Splitthename-valuepairs
  • @pairs=split(/&/,$buffer);

  • foreach$pair(@pairs)
  • {
  • ($name,$value)=split(/=/,$pair);

  • $value=~tr/+//;
  • $value=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  • $name=~tr/+//;
  • $name=~s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;

  • $FORM{$name}=$value;
  • }

  • if($FORM{user}){
  • $user=$FORM{user};
  • }else{
  • &error("Error","Usernamemissingfromform.");
  • }
  • $pwd=$FORM{old};
  • $command=$FORM{command};
  • unless(($commandeq'remove')
  • ||($FORM{new}&&$FORM{new}eq$FORM{new2})){
  • &error("PasswordMismatch","Newpasswordmismatchormissing.");
  • }

  • #Getexistingpasswords
  • if(-e$file){
  • open(IN,$file)or&error("Error","Can'topenpasswordfile:$!");
  • flock(IN,2);
  • seek(IN,0,0);
  • while(<IN>){
  • chomp;
  • ($name,$value,$tail)=split(/:/,$_,3);
  • $hash{$name}=$value;
  • $tail{$name}=$tail;#maintainanyadditionalfields
  • }
  • closeIN;
  • }

  • #Saltforcrypt
  • @range=('0'..'9','a'..'z','A'..'Z','.','/');
  • srand(time()^($$+($$<<15)));
  • $salt=$range[rand(int($#range)+1)].$range[rand(int($#range)+1)];

  • #Checkforvalidpasswordorexistinguser
  • $pass=$hash{$user}if$hash{$user};
  • $cpwd=crypt($pwd,$pass);
  • $admin=$hash{admin}&&crypt($pwd,$hash{admin})eq$hash{admin};

  • if(($commandne'new')&&($admin||$pass&&$cpwdeq$pass)){
  • if($commandeq'remove'){
  • delete($hash{$user});delete($tail{$user});
  • $msg="User<B>$user</B>wasremovedfrompasswordfile.";
  • }elsif(!$pass){
  • $msg="WARNING!'ChangePassword'checkedfornon-existinguser?\n"
  • ."<P>Assigningpasswordfornewuser<B>$user</B>anyway.\n"
  • ."<P>Ifthiswasanerror,gobackand'RemoveUser'";
  • }else{
  • $msg="Passwordhasbeenupdatedfor$user.";
  • }
  • }elsif($FORM{command}eq'new'){
  • if($pass){
  • &error("Sorry","User<B>$user</B>isalreadyassigned.");
  • }elsif($allow_new||$admin){
  • $msg="Passwordhasbeenassignedfornewuser$user.";
  • }else{
  • &begin_html("Sorry,NewUser");
  • print"Contactfileownerforpasswordyoucanchangelater.";
  • &end_html;
  • exit;
  • }
  • }else{
  • &error("PasswordError",
  • "Invaliduserorpasswordorforgottocheck'NewUser'.");
  • }

  • #Assignnewpasswordtouserandwritetofile
  • $hash{$user}=crypt($FORM{new},$salt)if$commandne'remove';
  • if(open(OUT,">$file")){
  • flock(OUT,2);
  • seek(OUT,0,0);
  • foreach$name(sortkeys%hash){
  • printOUT"$name:$hash{$name}";
  • printOUT":$tail{$name}"if$tail{$name};
  • printOUT"\n";
  • }
  • }else{
  • &error("Error","Can'tupdatepasswordfile:$!");
  • }

  • #PrintReturnHTML
  • &begin_html("ThankYou");
  • print"$msg\n";
  • &end_html;

  • ###Subroutines

  • #subroutinebegin_html(title)
  • subbegin_html{
  • local($title)=@_;
  • print"Content-type:text/html\n\n";
  • print"<html><head><title>$title</title></head><body>\n";
  • print"<center><h1>$title</h1></center>\n<hr><p>\n";
  • }

  • #subroutineend_html
  • subend_html{
  • #Addfooterlinkshere
  • print"<P></body></html>\n";
  • }

  • #subroutinemake_form
  • submake_form{
  • &begin_html("ChangeorAddPassword");

  • print<<NEW_FORM;
  • Usethisformtochangeyourpasswordforaccesstorestricted
  • directorieshere.Newuserswillbeinformedifpasswordwasassignedor
  • iftheyneedtocontacttheownerofthesepages.

  • <FORMMETHOD="POST"ACTION="$ENV{SCRIPT_NAME}">

  • <DL>
  • <DT>E-mailAddress(orusernameonthissystem):
  • <DD><INPUTNAME="user">

  • <DT>CurrentPassword(requiredunlessnewuser):
  • <DD><INPUTTYPE=PASSWORDNAME="old">

  • <DT>NewPassword:
  • <DD><INPUTTYPE=PASSWORDNAME="new">

  • <DT>ConfirmNewPassword:
  • <DD><INPUTTYPE=PASSWORDNAME="new2">

  • <DT>Request:
  • <DD>
  • <INPUTTYPE="radio"NAME="command"VALUE="change"CHECKED>ChangePassword
  • <DD>
  • <INPUTTYPE="radio"NAME="command"VALUE="new">NewUser
  • <DD>
  • <INPUTTYPE="radio"NAME="command"VALUE="remove">RemoveUser
  • </DL>

  • <P><INPUTTYPE="submit"VALUE="SubmitRequest">
  • </FORM>
  • NEW_FORM

  • &end_html;
  • exit;
  • }

  • suberror{
  • local($title,$msg)=@_;
  • &begin_html($title);
  • print"<P>$msg\n";
  • print"<P>Pleasecheckyournameandre-enterpasswords.\n";
  • &end_html;
  • exit;
  • }




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-326784-1-1.html 上篇帖子: Apache License Version 2.0 英文内容及中文翻译 下篇帖子: Tomcat的Socket实现:org.apache.tomcat.util.net(一)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表