#!/usr/local/bin/perl use Apache::Htpasswd; use CGI qw(:cgi-lib); &ReadParse; use Net::FTP; # 基本ファイルの存在確認 $master_passwd = "/etc/master.passwd"; $httpd_conf = "/usr/local/apache/conf/httpd.conf"; $error_message = ""; unless (-e "$master_passwd") { $error_message = "Cannot find $master_passwd\n"; &unable; exit; } $error_message = ""; unless (-e "$httpd_conf") { $error_message = "Cannot find $httpd_conf\n"; &unable; exit; } # 入力データフォーマットチェック $non_user_format = "[^0-9a-zA-Z\.\-_]"; $admin = $in{admin}; $admin_pswd = $in{admin_pswd}; $domain = $in{domain}; $directory = $in{directory}; $comon = $in{comon}; $user_1 = $in{user_1}; $user_1_pswd = $in{user_1_pswd}; $user_1_pswd_confirm = $in{user_1_pswd_confirm}; $user_2 = $in{user_2}; $user_2_pswd = $in{user_2_pswd}; $user_2_pswd_confirm = $in{user_2_pswd_confirm}; $user_3 = $in{user_3}; $user_3_pswd = $in{user_3_pswd}; $user_3_pswd_confirm = $in{user_3_pswd_confirm}; $user_4 = $in{user_4}; $user_4_pswd = $in{user_4_pswd}; $user_4_pswd_confirm = $in{user_4_pswd_confirm}; $user_5 = $in{user_5}; $user_5_pswd = $in{user_5_pswd}; $user_5_pswd_confirm = $in{user_5_pswd_confirm}; unless ($admin) { $warning_title = "ドメイン管理ユーザー名が未入力です"; &warning; exit; } unless ($admin_pswd) { $warning_title = "ドメイン管理ユーザーパスワードが未入力です"; &warning; exit; } unless ($domain) { $warning_title = "アクセス制限を設定するドメイン名が未入力です"; &warning; exit; } if ($domain =~ /[^0-9a-zA-Z\.\-_]/) { $warning_title = "アクセス制限を設定するドメイン名が間違っています"; &warning; exit; } unless ($domain =~ /.+\..+/) { $warning_title = "アクセス制限を設定するドメイン名が間違っています"; &warning; exit; } $length_dom = length ($domain); if ($length_dom < 6) { $warning_title = "アクセス制限を設定するドメイン名が間違っています"; &warning; exit; } unless ($directory) { $warning_title = "アクセス制限を設定するディレクトリー名が未入力です"; &warning; exit; } unless ($directory =~ /^\//) { $warning_title = "ディレクトリー名は / から始めてください"; &warning; exit; } unless ($directory =~ /\/$/) { $warning_title = "ディレクトリー名は / で終わらせてください"; &warning; exit; } unless ($user_1) { $warning_title = "アクセスを許可するユーザー名 1 が未入力です"; &warning; exit; } if ($user_1 =~ /$non_user_format/) { $warning_title = "アクセスを許可するユーザー名 1 に使えない文字が含まれています"; &warning; exit; } unless ($user_1_pswd) { $warning_title = "ユーザー名 1 のパスワードが未入力です"; &warning; exit; } $length_user_1_pswd = length ($user_1_pswd); if ($length_user_1_pswd < 8) { $warning_title = "ユーザー名 1 のパスワードを 8 文字以上にしてください"; &warning; exit; } unless ($user_1_pswd_confirm) { $warning_title = "ユーザー名 1 のパスワード確認入力が未入力です"; &warning; exit; } unless ("$user_1_pswd_confirm" eq "$user_1_pswd") { $warning_title = "ユーザー名 1 のパスワードが一致しません"; &warning; exit; } $new_user{1} = $user_1; $new_user_pswd{1} = $user_1_pswd; if ($user_2) { unless ($user_1) { $warning_title = "上から順に記入してください"; &warning; exit; } if ("$user_2" eq "$user_1") { $warning_title = "ユーザー名に重複があります"; &warning; exit; } if ($user_2 =~ /$non_user_format/) { $warning_title = "アクセスを許可するユーザー名 2 に使えない文字が含まれています"; &warning; exit; } unless ($user_2_pswd) { $warning_title = "ユーザー名 2 のパスワードが未入力です"; &warning; exit; } $length_user_2_pswd = length ($user_2_pswd); if ($length_user_2_pswd < 8) { $warning_title = "ユーザー名 2 のパスワードを 8 文字以上にしてください"; &warning; exit; } unless ($user_2_pswd_confirm) { $warning_title = "ユーザー名 2 のパスワード確認入力が未入力です"; &warning; exit; } unless ("$user_2_pswd_confirm" eq "$user_2_pswd") { $warning_title = "ユーザー名 2 のパスワードが一致しません"; &warning; exit; } $new_user{2} = $user_2; $new_user_pswd{2} = $user_2_pswd; } if ($user_3) { unless ($user_2) { $warning_title = "上から順に記入してください"; &warning; exit; } if (("$user_3" eq "$user_1") || ("$user_3" eq "$user_2")) { $warning_title = "ユーザー名に重複があります"; &warning; exit; } if ($user_3 =~ /$non_user_format/) { $warning_title = "アクセスを許可するユーザー名 3 に使えない文字が含まれています"; &warning; exit; } unless ($user_3_pswd) { $warning_title = "ユーザー名 3 のパスワードが未入力です"; &warning; exit; } $length_user_3_pswd = length ($user_3_pswd); if ($length_user_3_pswd < 8) { $warning_title = "ユーザー名 3 のパスワードを 8 文字以上にしてください"; &warning; exit; } unless ($user_3_pswd_confirm) { $warning_title = "ユーザー名 3 のパスワード確認入力が未入力です"; &warning; exit; } unless ("$user_3_pswd_confirm" eq "$user_3_pswd") { $warning_title = "ユーザー名 3 のパスワードが一致しません"; &warning; exit; } $new_user{3} = $user_3; $new_user_pswd{3} = $user_3_pswd; } if ($user_4) { unless ($user_3) { $warning_title = "上から順に記入してください"; &warning; exit; } if (("$user_4" eq "$user_1") || ("$user_4" eq "$user_2") || ("$user_4" eq "$user_3")) { $warning_title = "ユーザー名に重複があります"; &warning; exit; } if ($user_4 =~ /$non_user_format/) { $warning_title = "アクセスを許可するユーザー名 4 に使えない文字が含まれています"; &warning; exit; } unless ($user_4_pswd) { $warning_title = "ユーザー名 4 のパスワードが未入力です"; &warning; exit; } $length_user_4_pswd = length ($user_4_pswd); if ($length_user_4_pswd < 8) { $warning_title = "ユーザー名 4 のパスワードを 8 文字以上にしてください"; &warning; exit; } unless ($user_4_pswd_confirm) { $warning_title = "ユーザー名 4 のパスワード確認入力が未入力です"; &warning; exit; } unless ("$user_4_pswd_confirm" eq "$user_4_pswd") { $warning_title = "ユーザー名 4 のパスワードが一致しません"; &warning; exit; } $new_user{4} = $user_4; $new_user_pswd{4} = $user_4_pswd; } if ($user_5) { unless ($user_4) { $warning_title = "上から順に記入してください"; &warning; exit; } if (("$user_5" eq "$user_1") || ("$user_5" eq "$user_2") || ("$user_5" eq "$user_3") || ("$user_5" eq "$user_4")) { $warning_title = "ユーザー名に重複があります"; &warning; exit; } if ($user_5 =~ /$non_user_format/) { $warning_title = "アクセスを許可するユーザー名 5 に使えない文字が含まれています"; &warning; exit; } unless ($user_5_pswd) { $warning_title = "ユーザー名 5 のパスワードが未入力です"; &warning; exit; } $length_user_5_pswd = length ($user_5_pswd); if ($length_user_5_pswd < 8) { $warning_title = "ユーザー名 5 のパスワードを 8 文字以上にしてください"; &warning; exit; } unless ($user_5_pswd_confirm) { $warning_title = "ユーザー名 5 のパスワード確認入力が未入力です"; &warning; exit; } unless ("$user_5_pswd_confirm" eq "$user_5_pswd") { $warning_title = "ユーザー名 5 のパスワードが一致しません"; &warning; exit; } $new_user{5} = $user_5; $new_user_pswd{5} = $user_5_pswd; } # 入力ドメイン名チェック if ($domain =~ /^www\./) { $domain =~ s/^www\.//; } $warning_title = "$httpd_conf を開けません"; $vhost_num = 0; $vhost_end_num = 0; $s_vhost_num = 0; $s_vhost_end_num = 0; $domains = ""; open (CONF, "$httpd_conf") || (&warning, exit); while () { chomp; unless (/^#/) { if (//i) { $vhost_num = $vhost_num + 1; } if (//i) { $s_vhost_num = $s_vhost_num + 1; } if (/<\/VirtualHost>/i) { if (($vhost_num - 1) == $vhost_end_num) { $vhost_end_num = $vhost_num; } elsif (($s_vhost_num - 1) == $s_vhost_end_num) { $s_vhost_end_num = $s_vhost_num; } else { $warning_title = "1 httpd.conf フォーマットが想定外です。サーバー管理者に連絡下さい"; &warning; exit; } } if (/DocumentRoot/i) { if ($vhost_num) { s/DocumentRoot//i; s/\s//g; if (($vhost_num - 1) == $vhost_end_num) { $vhost_dir{$vhost_num} = $_; } elsif (($s_vhost_num - 1) == $s_vhost_end_num) { $s_vhost_dir{$s_vhost_num} = $_; } else { $warning_title = "2 httpd.conf フォーマットが想定外です。サーバー管理者に連絡下さい"; &warning; exit; } } } if (/ServerName/i) { if ($vhost_num) { $dom_much = 0; s/ServerName//i; s/\s//g; if ($_) { $domains = "$domains" . "$_"; if ("$_" eq "$domain") { $dom_much = $dom_much + 1; if (($vhost_num - 1) == $vhost_end_num) { $vhost_target_num = $vhost_num; } elsif (($s_vhost_num - 1) == $s_vhost_end_num) { $s_vhost_target_num = $s_vhost_num; } else { $warning_title = "3 httpd.conf フォーマットが想定外です。サーバー管理者に連絡下さい"; &warning; exit; } } } } } if (/User/i) { if ($vhost_num) { s/User//i; s/\s//g; if (($vhost_num - 1) == $vhost_end_num) { $vhost_user{$vhost_num} = $_; } elsif (($s_vhost_num - 1) == $s_vhost_end_num) { $s_vhost_user{$s_vhost_num} = $_; } else { $warning_title = "4 httpd.conf フォーマットが想定外です。サーバー管理者に連絡下さい"; &warning; exit; } } } } } unless ($dom_much) { unless ($s_vhost_target_num) { $warning_title = "1 このサーバーに $domain は設定されていません $domains"; &warning; exit; } } unless ($vhost_target_num) { unless ($s_vhost_target_num) { $warning_title = "このサーバーに $domain は設定されていません"; &warning; exit; } } unless ("$vhost_user{$vhost_target_num}" eq "$admin") { unless ("$s_vhost_user{$s_vhost_target_num}" eq "$admin") { unless ($vhost_dir{$vhost_target_num} =~ /$admin/) { unless ($s_vhost_dir{$s_vhost_target_num} =~ /$admin/) { $warning_title = "5 httpd.conf フォーマットが想定外です。サーバー管理者に連絡下さい"; &warning; exit; } } } } # 入力ユーザーチェック $warning_title = "サーバーに接続できません"; $ftp = Net::FTP->new("$domain", Debug => 0) || (&warning, exit); $result = $ftp->login("$admin","$admin_pswd"); $ftp->quit; unless ($result) { $warning_title = "ドメイン名管理者以外は利用できません"; &warning; exit; } # 入力ディレクトリーチェック if ($vhost_dir{$vhost_target_num}) { $file_path = "$vhost_dir{$vhost_target_num}" . "$directory"; } elsif ($s_vhost_dir{$s_vhost_target_num}) { $file_path = "$s_vhost_dir{$s_vhost_target_num}" . "$directory"; } else { $warning_title = "6 httpd.conf フォーマットが想定外です。サーバー管理者に連絡下さい"; &warning; exit; } if ($file_path) { $file_path =~ s#//#/#; } unless (-e "$file_path") { $warning_title = "指定されたディレクトリーは存在しません"; &warning; exit; } # .htaccess passwd ファイル名決定 if ($comon eq "yes") { $passwd_file_name = "\.ht_passwd"; } else { $add_data = $directory; $add_data =~ s/\///g; unless ($add_data) { $passwd_file_name = "\.ht_passwd"; } else { $passwd_file_name = "\.ht_" . "$add_data" . "_passwd"; } } # .htaccess 作成 $access_file = "$file_path" . ".htaccess"; unless (-e "$access_file") { $warning_title = ".htaccess を作成できません"; $passwd_path = "/home/" . "$admin" . "/" . "$passwd_file_name"; open (ACCESS, ">$access_file") || (&warning, exit); print ACCESS "AuthUserFile $passwd_path\n"; print ACCESS "AuthGroupFile /dev/null\n"; print ACCESS "AuthName \"$admin\"\n"; print ACCESS "AuthType Basic\n"; print ACCESS "\n"; print ACCESS "require valid-user\n"; print ACCESS "\n"; } # パスワード設定 foreach $id (keys %new_user) { if ($new_user{$id}) { unless ($new_user_pswd{$id}) { $warning_title = "No_user_passwd $new_user{$id} 想定外エラーです。サーバー管理者に連絡下さい"; &warning; exit; } unless (-e "$passwd_path") { $warning_title = "Cannot create .htpasswd file 想定外エラーです。サーバー管理者に連絡下さい"; open (NEW, ">$passwd_path") || (&warning, exit); } $warning_title = "Cannot open .htpasswd 想定外エラーです。サーバー管理者に連絡下さい"; $foo = new Apache::Htpasswd("$passwd_path") || (&warning, exit); $already = $foo->fetchPass("$new_user{$id}"); if ($already) { $warning_title = "Cannot change password of $new_user{$id} 想定外エラーです。サーバー管理者に連絡下さい"; $foo->htpasswd("$new_user{$id}", "$new_user_pswd{$id}", 1) || (&warning, exit); } else { # Add an entry $warning_title = "Cannot add password of $new_user{$id} 想定外エラーです。サーバー管理者に連絡下さい"; $foo->htpasswd("$new_user{$id}", "$new_user_pswd{$id}") || (&warning, exit); } } } # 結果表示 &finished; # エラー表示 ######################################### #htmlトップの表示 sub make_top { print "Content-type: text/html\n\n"; print "\n"; print "\n"; #print "$type\n"; print "\n"; print "$title\n"; print "\n"; print "\n"; print "\n"; } ######################################### #html_table_formatトップの表示 sub make_table_format_top { print "
\n"; print "\n"; print " \n"; print " \n"; print " \n"; print "
\n"; } ######################################### #html_table_formatボトムの表示 sub make_table_format_bottom { print "
\n"; print "
\n"; } ######################################### #htmlボトムの表示 sub make_bottom { print "\n"; print "\n"; } ############################################### #間違いの告知sub sub warning { ################################################# #htmlトップの表示 #$type = ""; $title = "TOROKU_Wrong_$warning_title"; &make_top; print "
\n"; print "
\n"; print "$warning_title
\n"; print "
\n"; print "ブラウザの戻るボタンで登録画面に戻り訂正してください。
\n"; print "
\n"; ################################################# #htmlボトムの表示 &make_bottom; } sub unable { ################################################# #htmlトップの表示 #$type = ""; $title = "unable"; &make_top; print "
\n"; print "
\n"; print "利用不能です
\n"; print "$error_message
\n"; print "サーバー管理者にお問い合わせ下さい
\n"; print "
\n"; ################################################# #htmlボトムの表示 &make_bottom; } ################################################# sub finished { ################################################# #htmlトップの表示 #$type = ""; $title = "finished"; &make_top; print "
\n"; print "
\n"; print "設定完了
\n"; print "
\n"; print "アクセスコントロールを設定しました。
\n"; print "
\n"; ################################################# #htmlボトムの表示 &make_bottom; }