#!/usr/local/bin/perl # # CGIでアクセス制限2 vers.2.00 # (c)rescue.ne.jp # http://www.rescue.ne.jp/ # #--------------------------------------------------------------------- # このCGIスクリプト名は(CGIが実行できれば)何でも結構です。 # __END__より下の行を表示させる為のアクセス制限です。 # 認証された後に表示する「HTML」を、__END__より下に記述してください。 # なお、HTMLではなく「プレーンテキスト」として表示させたい場合は、 # print "Content-type: text/html\n\n"; #※# の部分を、 # print "Content-type: text/plain\n\n"; #※# に修正してください。 #--------------------------------------------------------------------- #■ 設定 $body = ''; #■ パスワードセットの設定 # crypt.cgiで作成したパスワードセットをそのままコピーする. # 1件目の rescue:8KNmim/Pj.4gc は記入例ですので削除してください. # 何件でも設定可能です. @passwordset = ( 'rescue:8KNmim/Pj.4gc', '', '', '', '', ); #■ 認証ログファイルの設定 # 見られたくない場合は、WWW上から直接見えない場所に置くか、 # 認証ログファイル名の拡張子を .cgi にするなどして回避させる. # 蓄積ログなので、どんどん記録されて大きくなっていきます. # 適宜バックアップしてリセット(空にする)などのメンテナンスが必要です. $log = 0; # 蓄積ログを 1:残す 0:記録しない $file = './log.csv'; # 残す場合のみファイルを用意する(ファイルのパーミッションは666または同等の値にする) #■入力方式 POST=1 GET=0 (仕方なくpostが使えないサーバはgetにする) $method = 1; #--------------------------------------------------------------------- ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); @wday_array = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); $date_now = sprintf("%04d/%01d/%01d(%s)%02d:%02d:%02d",$year +1900,$mon +1,$mday,$wday_array[$wday],$hour,$min,$sec); if ($method eq '1' && $ENV{'QUERY_STRING'} ne '') { &error('Bad Request',""); } if ($method eq '1') { $method = 'post'; read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'}); } else { $method = 'get'; $buffer = $ENV{'QUERY_STRING'}; } @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; $FORM{$name} = $value; } if ($buffer eq '') { print <<"EOF"; Content-type: text/html\n Username and Password Required $body

Username and Password Required

Enter Username(ID) for Password at $ENV{'SCRIPT_NAME'}:

Username(ID):
Password:

EOF exit; } if ($FORM{'username'} eq '' || $FORM{'username'} =~ /\,/ || $FORM{'username'} =~ /\/ || $FORM{'password'} eq '') { &error('Authorization Required',"This server could not verify that you are authorized to access the document you requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required."); } foreach (@passwordset) { ($id,$pwd) = split(/\:/); if ($id ne $FORM{'username'}) { next; } if ($pwd =~ /^\$1\$/) { $salt = 3; } else { $salt = 0; } if (crypt($FORM{'password'}, substr($pwd,$salt,2)) eq $pwd) { $request = 1; } } if (!$request) { if ($log && open(OUT,">> $file")) { print OUT "$date_now\,$FORM{'username'}\,$ENV{'SCRIPT_NAME'}\,FALSE\n"; close(OUT); } &error('Authorization Required',"This server could not verify that you are authorized to access the document you requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required."); } if ($log && open(OUT,">> $file")) { print OUT "$date_now\,$FORM{'username'}\,$ENV{'SCRIPT_NAME'}\,TRUE\n"; close(OUT); } print "Content-type: text/html\n\n"; #※# while () { print; } exit; sub error { print "Content-type: text/html\n\n"; print "Username and Password Required\n"; print "$body\n"; print "

$_[0]

\n"; print "$_[1]

\n"; print "\n"; exit; } __END__ OK

ようこそ

認証されましたのでこのページの内容が表示されました.