#!/usr/local/bin/perl # cgi2args # Convert cgi's POST contents to arguments for some process. # (c ) 1995-1997 by HIROSE Yuuji [yuuji@ae.keio.ac.jp] # Last modified Tue Sep 2 18:34:42 1997 on crx # $Id: cgi2args,v 1.2 1997/02/21 08:01:36 yuuji Exp yuuji $ # # How to use this package: # This script converts cgi variables to option vs. contents pairs. # You must specify cgi variable `execcmd' set to command to execute. # And the contents of cgi variable `args' is passed to the command # Ex.)
# # # # will execute /cgi-bin/./foo with args as; # ./foo -hoge InputValue bar # Note that `execcmd' must start with "./" # # [cgi-bin の引数を普通のコマンドライン引数に展開してコマンドを起動する] # # ↑のようにすることで、cgi-binで利用するプログラムのデバッグが容易にな # ります。cgi2args で利用する特殊変数は execcmd, args の二つで、 # # execcmd 実際に呼び出すプログラム("./" で始まるカレントディレクト # リのコマンドを指定しなければならない) # args そのプログラムに渡す(オプションではない)引数。 # # となっています。例えば、 # # ./foo -year 1995 -month 11 bar # # のようにプログラムでは、-year オプションに1995を指定し、-month オプショ # ンに11を指定し、引数として bar を渡しています。このような呼び出しをす # るためには、html の入力フォームで以下のように記述します。 # # # # # # #
# # この入力フォームで現れる3つの入力窓に対し、x, y, z と答えた場合、 # ./foo -year "x" -month "y" "z" # と起動されます。execcmd で呼び出されるコマンドは、から始まるhtml # テキストを吐き出すものにします。 # # require "jcode.pl"; # して、 # # print "HTTP-unchara"; # # の前に # # foreach (@opts){ # &jcode::convert(*_,'euc'); # } # # をいれると漢字のアーギュメントもちゃんと処理してくれます。 require "./jcode.pl" if (-f "./jcode.pl"); package cgi2args; $log = 1; # 出力logを取る行数 local($stream, $var, $val); if ($ENV{"REQUEST_METHOD"} =~ /POST/i) { sysread(STDIN, $stream, $ENV{"CONTENT_LENGTH"}); } else { $stream=$ENV{"QUERY_STRING"}; } foreach (split('&', $stream)) { ($var, $val) = split('=', $_, 2); $val =~ s/\+/ /g; $val =~ s/%(..)/pack("c", hex($1))/ge; # $val =~ s,\`,\',g; # disable backquoting if ($var eq "execcmd") { $execcmd = "$val"; next; } elsif ($var eq "args") { push(@args, "\"$val\""); next; } # eval "\$$var=$val"; # Set variable directly @opts = (@opts, "-$var ", "\"$val\"") if ("$val"); } if (-f "./jcode.pl") { foreach (@opts){ &jcode'convert(*_,'euc'); #' } } # print "HTTP/1.0 200\n"; #外しとこう… print "Content-type: text/html\n\n" ;# unless ($0 =~ /nph-/); #print "Pragma: no-cache\n\n"; # print "Call `$execcmd @opts @args'
\n"; if (&check($execcmd)) { $line=0; open(CMD, "$execcmd @opts @args |"); $|=1; select((select(CMD), $|=1)[0]); if ($log) { if (open(LOG, ">>log/cgi2args.log")) { $date = `date`; print LOG "Date: $date"; print LOG "Exec: $execcmd @opts @args\n"; print LOG "Output: [[[\n"; } } while () { print; if (!/^\s*$/ && !/<(html|title|head|body)\b/ && $line < $log) { print LOG $_; $line++; } } if ($log) { print LOG "]]]\n"; } close LOG; close CMD; } else { print "cgi2args error; illegal command [$execcmd]
\n"; } sub check { local($command) = @_; local($mydir, $myname) = ($0 =~ m,(.*)/(.*),); return 0 if ($command !~ m,^\./,); local($dirname); ($dirname) = ($command =~ m,(.*)/.*,); local($dev1, $ino1) = (stat($mydir))[0,1]; local($dev2, $ino2) = (stat($dirname))[0,1]; if ($dev1==$dev2 && $ino1==$ino2) { return 1; } else { print STDERR "Illegal execcmd: $execcmd\n"; print "cgi2args: Only current directory's commands are allowed.\n"; return 0; } } 1; __END__ # Local variables: # fill-prefix: "# " # End: