#! c:/perl/bin/perl
#
# cryppage.cgi
# HTMLファイルをキーでエンコードしてサーバーにアップします。
# サーバー上にテキストでファイルが残らないのでキーを破られない限り
# 誰にも(サーバー管理者にさえ)内容を見られることがありません。
# また、キーを知っていればそのページを閲覧することができます。
# 仲間内の住所録など限定された人のみに公開するのに向いてます。
#
# Crypt::CBC, Crypt::Blowfishを使用しています。
#
# 4.016 : 8/28/05 : jcode.plとcgi-lib.plを削除
# 4.015 : 7/23/06 : イニシャルキーをランダムに変更
# 4.013 : 3/22/05 : イニシャルキーを管理設定に追加。
# 4.012 : 2/21/05 : 管理用セットアップのパスワード認証でまれにミスするバグを修正
# 4.011 : 2/8/05 : CGI::Carpをコメントアウト
# 4.01 : 2/7/05 : Location:の後にスペースを追加。AnHTTPD対応。
# CGI::Carpを追加。
# 4.0 : 1/8/03 : CryptをPerlのpackage, Crypt::CBCに変更。
#
# $Id: cryppage.cgi,v 1.11 2005/08/27 22:50:54 Hideki Kanayama Exp $
use POSIX;
use CGI::Carp qw(fatalsToBrowser);
use Crypt::CBC;
use CGI qw(:cgi-lib);
#管理人パスワードファイル
$admindat = "adminpwd.dat";
#セットアップファイル
$setupfile = "cryppage_setup.pl";
$version = "4.016";
$last_updated_year = "2005";
$program = $0;
$program =~ s/^.+[\/\\]([^\/\\]+)$/$1/;
$script = "$program";
#### 環境設定 ###### ここから ############
#クリプトファイルのディレクトリ
$crdir = ".";
#クリプト後のデフォルトファイル名
$crfile = "$crdir/cryptfile.doc";
#タイトル
$title_logo_en = 0;
$title_logo = '';
$title = 'クリプトページ';
$title_color='blue';
#トップへのリンク 1: on 0:off
$toplink_en = 1;
$toplink_title = 'トップへ';
$toplink_link = "../cryppage.html";
#管理人セットアップ表示 1: on, 0: off
$setup_en = 1;
$setup_icon_en = 0;
$setup_icon = '';
$setup_title = '管理用';
#アクセスログ 1: on, 0: off
$accesslog_en = 1;
$accesslog = "$crdir/access.log";
#最終更新表示 1: on, 0: off
$lastupdate_en = 1;
$lastupdate = 'Last Update : ';
$lastupdate_color = '#3333ff';
#エントリーでの説明
$explain1 = '
キーをご存知ない方は、管理人までメールをください。
';
#時間設定
$offset = 9;
#### 環境設定 ###### ここまで ############
if (-e "$setupfile"){
require "$setupfile";
}
$bgset = "bgcolor=\"#ffffff\"";
$q = CGI->new;
$cgierror = $q->cgi_error;
&error($cgierror) if ($cgierror);
%in = $q->Vars;
if (! -e "$admindat"){
if ($in{mode} ne 'adminpwd'){
&setadminpwd;
} else {
&wradminpwd;
}
}
open(ADMIN,"< $admindat");
$adminpwd = ;
close(ADMIN);
if ($in{mode} eq 'setup'){
&setup;
} elsif ($in{mode} eq 'setform'){
&setform
} elsif ($in{mode} eq 'wrsetup'){
&wrsetup
} elsif ($in{mode} eq 'upcrypt'){
&upcrypt;
} elsif ($in{mode} eq 'display'){
&display;
} elsif ($in{mode} eq 'bfcrypt'){
&bfcrypt;
} elsif ($in{mode} eq 'entry'){
&entry;
} else {
&entry;
}
################ エントリー #########################################
sub entry {
($d_dev,$d_ino,$d_mode,$d_nlink,$d_uid,$d_gid,$d_rdev,$d_size,$d_atime,$d_mtime,$d_ctime,$d_blksize,$d_blocks)=stat("$crfile");
@monarray=(Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=gmtime($d_mtime + $offset * 3600);
$date_str = POSIX::strftime("%Y年%m月%d日%H時%M分",$sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
&beginning;
print "";
if ($title_logo_en == 1){
print "
";
} else {
print "$title
";
}
print "\n";
print "
\n";
if ($lastupdate_en == 1){
print "$lastupdate $date_str\n";
}
if ($toplink_en == 1){
if ($toplink_logo_en == 1){
print "
\n";
} else {
print "$toplink_title";
}
}
print " ";
if ($setup_en == 1){
if ($setup_icon_en == 1){
print "
\n";
} else {
print "$setup_title";
}
}
print "
\n";
print "
\n";
print "
\n";
print "$explain1\n";
print "
\n";
&ending;
}
################ ページディスプレイ #######################
sub display {
$remote_host=$ENV{'REMOTE_HOST'};
$remote_addr=$ENV{'REMOTE_ADDR'};
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time + $offset * 3600);
@wday_array = ('日','月','火','水','木','金','土');
$date_now = sprintf("%04d年%01d月%01d日(%s)%02d時%02d分",$year+1900,$mon +1,$mday,$wday_array[$wday],$hour,$min);
if (! -e "$crfile"){&error("$crfileが存在しません。");}
$cipher = &init_cipher;
print "Content-Type: text/html\n\n";
$success = 0;
open(CRFILE, "< $crfile");
@tmp = ;
close(CRFILE);
$wholefile = join '', @tmp;
$plaintext = $cipher->decrypt($wholefile);
print "$plaintext";
if ($accesslog_en == 1){
open(LOG,">> $accesslog");
printf(LOG "%s\t%s\t%s\n",$remote_addr,$date_now,$remote_host);
close(LOG);
chmod(0666,"$accesslog");
}
exit;
}
################ アップクリプト #######################
sub upcrypt {
if (! &checkcrypt($in{pwd},"$adminpwd")){
&error('パスワードが違います。');
}
if ($in{upfile} eq ''){
&error('アップファイルが指定されていません。');
}
if ($in{key} eq ''){
&error('クリプトキーが指定されていません。');
}
if (! -e "$crfile"){&error("$crfileが存在しません。");}
$fh = $q->upload('upfile');
$upfiletext = join '', <$fh>;
$cipher = &init_cipher;
$crypttext = $cipher->encrypt($upfiletext);
open(FILE,"> $crfile");
binmode FILE;
print FILE "$crypttext";
close(FILE);
print "Location: $script\n\n";
}
################ 管理用ページ #######################
sub setup {
&beginning;
print <
管理ページ
\n";
&ending;
}
################ セットアップファイル作成 #######################
sub wrsetup {
if (open(FILE,"< $admindat")){;
$filepwd = ;
close(FILE);
$inpwd = crypt($in{pwd},$filepwd);
} else {
&error('パスワードファイルが存在しません。');
}
if ("$inpwd" ne "$filepwd"){
&error('パスワードが違います。');
}
foreach (keys(%in)){
$in{$_} =~ s/,/,/g;
$in{$_} =~ s/<//g;
}
open(SETUP,"> $setupfile");
print SETUP <";
print "管理者用パスワードを設定してください。
";
print "";
print "";
print "";
print "";
&ending;
exit;
}
################ セットアップ用パスワード作成 #######################
sub wradminpwd {
$passwd = &makecrypt($in{pwd});
if (open(FILE,"> $admindat")){
print FILE "$passwd";
close(FILE);
} else {
&error('パスワードファイル作成に失敗しました。');
}
print "Location: $script\n\n";
}
sub checkcrypt {
local($pwd,$encpwd)=@_;
return(crypt($pwd,$encpwd) eq "$encpwd");
}
sub makecrypt {
my $plain = shift;
my $salt = &salt;
my $result = crypt($plain,$salt);
$result=crypt($plain,'$1$'.$salt.'$') if ($result eq '');
return $result;
}
sub salt {
my $length = 2;
$length = $_[0] if exists $_[0];
return join "", ('.', '/', 0..9, 'A'..'Z', 'a'..'z')[map {rand 64} (1..$length)];
}
sub beginning {
print "Content-Type: text/html\n\n";
print "\n";
print <
$title
HEADPRINT
print "\n";
print "\n";
}
sub ending {
print "$script Ver. $version
Copyright(C) 1997-$last_updated_year, Hideki
\n";
print "\n";
print "