| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | #--------------------------------------------------------------------- | ||
| 4 | # This program checks Perl code for validity. Perl syntax errors and | ||
| 5 | # pragma constraint violations are reported. It is designed to be | ||
| 6 | # called by another process using open2() to send chunks of Perl | ||
| 7 | # code to be checked and recieving the status of the validation. | ||
| 8 | # | ||
| 9 | # Perl code is read from STDIN this way: | ||
| 10 | # 0. Line what to do: 'check' or 'execute $filename' | ||
| 11 | # check : only syntax checking | ||
| 12 | # execute : full execution, write output | ||
| 13 | # to $filename | ||
| 14 | # 1. Line Directory, to chdir() to befor evaluating | ||
| 15 | # the Perl code. If this line is empty, | ||
| 16 | # the program exits. | ||
| 17 | # 2. Line Colon delimited list of additional library | ||
| 18 | # directories to be added to @INC (only added | ||
| 19 | # for the first request, ignored by subsequent | ||
| 20 | # requests) | ||
| 21 | # 3. Line Directory of new.spirit config files | ||
| 22 | # 4. Line Directory to use for temp. files | ||
| 23 | # 5. Line A delimiter string. This string marks the | ||
| 24 | # end of the Perl code, sent after this line. | ||
| 25 | # The Perl code must not contain this delimiter | ||
| 26 | # string itself. | ||
| 27 | # 6. Line Perl code | ||
| 28 | # ... - " - | ||
| 29 | # n. Line - " - | ||
| 30 | # n+1. Line The delimiter string from the third line. | ||
| 31 | # | ||
| 32 | # Validation status is reported to STDOUT this way: | ||
| 33 | # 1. Line A delimiter string. This string marks the | ||
| 34 | # end of the error messages, sent after this line. | ||
| 35 | # 2. Line Perl error messages | ||
| 36 | # ... - " - | ||
| 37 | # n. Line - " - | ||
| 38 | # n+1. Line The delimiter string from the first line. | ||
| 39 | # | ||
| 40 | # It is possible to send multiple chunks of code, using the input | ||
| 41 | # protocol stated above. If the first line of the request is | ||
| 42 | # empty, the program exits. | ||
| 43 | # | ||
| 44 | # Note: | ||
| 45 | # Due to limitation of the Perl compiler, 'use strict' violations | ||
| 46 | # are reported only one time per process, so subsequent usage | ||
| 47 | # (even in a new chunk of code) of the same non declared variables | ||
| 48 | # is not reported. | ||
| 49 | # | ||
| 50 | # Example: | ||
| 51 | # This input stream: | ||
| 52 | # /www/cgi-bin | ||
| 53 | # /home/perl/lib:/web/perl/lib | ||
| 54 | # /tmp | ||
| 55 | # /usr/projects/foo/installed/prod/config | ||
| 56 | # __DELIMITER__ | ||
| 57 | # use strict; | ||
| 58 | # $foo=42; | ||
| 59 | # if bar foo; | ||
| 60 | # __DELIMITER__ | ||
| 61 | # [empty line] | ||
| 62 | # | ||
| 63 | # results in this output stream: | ||
| 64 | # __DELIMITER__ | ||
| 65 | # Global symbol "foo" requires explicit package name \ | ||
| 66 | # at (eval 1) line 3, <STDIN> chunk 6. | ||
| 67 | # syntax error at (eval 1) line 3, near "if bar foo" | ||
| 68 | # __DELIMITER__ | ||
| 69 | # | ||
| 70 | #--------------------------------------------------------------------- | ||
| 71 | |||
| 72 | use strict; | ||
| 73 | use Cwd; | ||
| 74 | use Carp; | ||
| 75 | use FileHandle; | ||
| 76 | use CIPP::Runtime::Request; | ||
| 77 | |||
| 78 | main: { | ||
| 79 | $| = 1; | ||
| 80 | $SIG{__WARN__} = \&catch_warnings; | ||
| 81 | $SIG{__DIE__} = undef; | ||
| 82 | |||
| 83 | # otherwise the CGI module will prompt for input | ||
| 84 | $ENV{REQUEST_METHOD} = "GET"; | ||
| 85 | $ENV{QUERY_STRING} = "foo=1"; | ||
| 86 | |||
| 87 | perlcheck_loop(); | ||
| 88 | } | ||
| 89 | |||
| 90 | sub perlcheck_loop { | ||
| 91 | writelog ("started"); | ||
| 92 | |||
| 93 | my $first = 1; | ||
| 94 | while ( 1 ) { | ||
| 95 | writelog ("waiting on input..."); | ||
| 96 | |||
| 97 | # first: read what to do | ||
| 98 | my $what = <STDIN>; | ||
| 99 | chomp $what; | ||
| 100 | |||
| 101 | writelog ("got what='$what'"); | ||
| 102 | |||
| 103 | last if $what eq ''; | ||
| 104 | |||
| 105 | if ( $what !~ /^(check|execute)/ ) { | ||
| 106 | print STDERR "unknown action: $what\n"; | ||
| 107 | last; | ||
| 108 | } | ||
| 109 | |||
| 110 | # then read the directory, where the Perl code | ||
| 111 | # should be executed | ||
| 112 | |||
| 113 | my $execute_dir = <STDIN>; | ||
| 114 | chomp $execute_dir; | ||
| 115 | |||
| 116 | writelog ("got execute_dir='$execute_dir'"); | ||
| 117 | |||
| 118 | last if $execute_dir eq ''; | ||
| 119 | |||
| 120 | # additional library directories (may be empty) | ||
| 121 | my $add_lib_dirs = <STDIN>; | ||
| 122 | chomp $add_lib_dirs; | ||
| 123 | |||
| 124 | writelog ("got add_lib_dirs=$add_lib_dirs"); | ||
| 125 | |||
| 126 | # now read the temp dir | ||
| 127 | my $temp_dir = <STDIN>; | ||
| 128 | chomp $temp_dir; | ||
| 129 | |||
| 130 | writelog ("got temp_dir=$temp_dir"); | ||
| 131 | |||
| 132 | last if $temp_dir eq ''; | ||
| 133 | |||
| 134 | # read config dir | ||
| 135 | my $config_dir = <STDIN>; | ||
| 136 | chomp $config_dir; | ||
| 137 | |||
| 138 | writelog ("got config_dir=$config_dir"); | ||
| 139 | |||
| 140 | # now read the delimiter which marks the end of the | ||
| 141 | # perl code to be checked | ||
| 142 | |||
| 143 | my $delimiter = <STDIN>; | ||
| 144 | chomp $delimiter; | ||
| 145 | |||
| 146 | writelog ("got delimiter=$delimiter"); | ||
| 147 | |||
| 148 | last if $delimiter eq ''; | ||
| 149 | |||
| 150 | # now read the Perl code | ||
| 151 | my $perl_code; | ||
| 152 | perlcode: while (<STDIN>) { | ||
| 153 | chomp; | ||
| 154 | last perlcode if $_ eq $delimiter; | ||
| 155 | $perl_code .= "$_\n"; | ||
| 156 | } | ||
| 157 | |||
| 158 | # check the Perl code and write possible internal errors | ||
| 159 | # to $result_file | ||
| 160 | my $error; | ||
| 161 | my @OLD_INC = @INC; | ||
| 162 | unshift @INC, split(":", $add_lib_dirs); | ||
| 163 | if ( $what eq 'check' ) { | ||
| 164 | $error = perlcheck ( | ||
| 165 | $execute_dir, | ||
| 166 | $temp_dir, | ||
| 167 | \$perl_code | ||
| 168 | ); | ||
| 169 | } else { | ||
| 170 | my ($filename) = $what =~ /^execute\s+(.*)/; | ||
| 171 | $error = perlexecute ( | ||
| 172 | $filename, | ||
| 173 | $execute_dir, | ||
| 174 | $temp_dir, | ||
| 175 | $config_dir, | ||
| 176 | \$perl_code | ||
| 177 | ); | ||
| 178 | } | ||
| 179 | @INC = @OLD_INC; | ||
| 180 | |||
| 181 | my $delimiter = "__PERLCHECK_REQUEST_FINISHED__"; | ||
| 182 | while ( $error =~ /$delimiter/ ) { | ||
| 183 | $delimiter .= $$; | ||
| 184 | } | ||
| 185 | |||
| 186 | print "$delimiter\n"; | ||
| 187 | |||
| 188 | writelog("print error: $error"); | ||
| 189 | |||
| 190 | if ( $error ) { | ||
| 191 | print "$error\n"; | ||
| 192 | } | ||
| 193 | |||
| 194 | print "$delimiter\n"; | ||
| 195 | |||
| 196 | $first = 0; | ||
| 197 | } | ||
| 198 | |||
| 199 | writelog("leaving perlcheck_loop"); | ||
| 200 | } | ||
| 201 | |||
| 202 | sub perlcheck { | ||
| 203 | my ($dir, $temp_dir, $perl_sref) = @_; | ||
| 204 | |||
| 205 | # eventually change to another directory | ||
| 206 | |||
| 207 | my $cwd_dir = cwd(); | ||
| 208 | if ( $dir ) { | ||
| 209 | chdir $dir or return "Can't chdir to '$dir'"; | ||
| 210 | # .cgi is important here! | ||
| 211 | # Checked by CIPP::Runtime::NewSpirit! | ||
| 212 | $0 = "$dir/cipp_perlcheck.cgi"; | ||
| 213 | } | ||
| 214 | |||
| 215 | # some CIPP specific error handler stuff | ||
| 216 | |||
| 217 | $CIPP_Exec::_cipp_in_execute = 1; | ||
| 218 | $CIPP_Exec::_cipp_no_http = 1; | ||
| 219 | |||
| 220 | # disable BEGIN and END blocks, | ||
| 221 | # they'll be executed inside our eval, but don't | ||
| 222 | # want any code to execute. | ||
| 223 | |||
| 224 | # disable BEGIN and END blocks | ||
| 225 | $$perl_sref =~ s/BEGIN//gs; | ||
| 226 | $$perl_sref =~ s/END//gs; | ||
| 227 | |||
| 228 | writelog ($$perl_sref); | ||
| 229 | |||
| 230 | # evaluate Perl code and reset error handler | ||
| 231 | my $error = eval_perl_code ($perl_sref); | ||
| 232 | |||
| 233 | # change to old directory | ||
| 234 | chdir $cwd_dir; | ||
| 235 | |||
| 236 | return $error; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub perlexecute { | ||
| 240 | my ($catch_file, $dir, $temp_dir, $config_dir, $perl_sref) = @_; | ||
| 241 | |||
| 242 | writelog ("perlexecute request started"); | ||
| 243 | |||
| 244 | # eventually change to another directory | ||
| 245 | writelog ("cd $dir"); | ||
| 246 | my $cwd_dir = cwd(); | ||
| 247 | if ( $dir ) { | ||
| 248 | chdir $dir or return "Can't chdir to '$dir'"; | ||
| 249 | # .cgi is important here! | ||
| 250 | # Checked by CIPP::Runtime::NewSpirit! | ||
| 251 | $0 = "$dir/cipp_perlcheck.cgi"; | ||
| 252 | } | ||
| 253 | |||
| 254 | # redirect STDOUT | ||
| 255 | writelog ("save STDOUT"); | ||
| 256 | |||
| 257 | if ( ! open (SAVESTDOUT, ">&STDOUT") ) { | ||
| 258 | writelog ("error duping STDOUT"); | ||
| 259 | chdir $cwd_dir; | ||
| 260 | return "can't dup STDOUT"; | ||
| 261 | } | ||
| 262 | |||
| 263 | writelog ("close STDOUT"); | ||
| 264 | |||
| 265 | close STDOUT; | ||
| 266 | |||
| 267 | writelog ("open STDOUT > $catch_file"); | ||
| 268 | |||
| 269 | if ( ! open (STDOUT, "> $catch_file") ) { | ||
| 270 | open (STDOUT, ">&SAVESTDOUT"); | ||
| 271 | close SAVESTDOUT; | ||
| 272 | chdir $cwd_dir; | ||
| 273 | return "Can't write '$catch_file'"; | ||
| 274 | } | ||
| 275 | |||
| 276 | # some CIPP specific error handler stuff | ||
| 277 | $CIPP_Exec::_cipp_in_execute = 1; | ||
| 278 | $CIPP_Exec::_cipp_no_http = 1; | ||
| 279 | |||
| 280 | # disable BEGIN and END blocks | ||
| 281 | $$perl_sref =~ s/BEGIN//gs; | ||
| 282 | $$perl_sref =~ s/END//gs; | ||
| 283 | |||
| 284 | # evaluate Perl code and reset error handler | ||
| 285 | writelog ("execute perl code: $0"); | ||
| 286 | writelog ("perl code: ".$$perl_sref); | ||
| 287 | |||
| 288 | # probably set a different config directory | ||
| 289 | $CIPP::Runtime::NewSpirit::CONFIG_DIR = $config_dir | ||
| 290 | if $config_dir; | ||
| 291 | |||
| 292 | writelog ("config dir: $config_dir"); | ||
| 293 | |||
| 294 | my $error = exec_perl_code ($perl_sref); | ||
| 295 | |||
| 296 | # delete config dir setting | ||
| 297 | $CIPP::Runtime::NewSpirit::CONFIG_DIR = undef; | ||
| 298 | |||
| 299 | # change to old directory | ||
| 300 | writelog ("cd $cwd_dir"); | ||
| 301 | chdir $cwd_dir; | ||
| 302 | |||
| 303 | # restore STDOUT | ||
| 304 | writelog ("restore STDOUT"); | ||
| 305 | close STDOUT; | ||
| 306 | open (STDOUT, ">&SAVESTDOUT") | ||
| 307 | or crash("Can't restore STDOUT"); | ||
| 308 | close SAVESTDOUT; | ||
| 309 | |||
| 310 | writelog ("request finished"); | ||
| 311 | |||
| 312 | return $error; | ||
| 313 | } | ||
| 314 | |||
| 315 | sub crash { | ||
| 316 | my ($msg) = @_; | ||
| 317 | |||
| 318 | writelog ($msg); | ||
| 319 | |||
| 320 | exit 1; | ||
| 321 | } | ||
| 322 | |||
| 323 | sub writelog { | ||
| 324 | my ($msg) = @_; | ||
| 325 | return if not -f "/tmp/do.the.cipp3debug"; | ||
| 326 | my $date = scalar(localtime(time)); | ||
| 327 | open (LOG, ">> /tmp/perlcheck.log"); | ||
| 328 | select LOG; $| = 1; select STDOUT; | ||
| 329 | print LOG "-" x 80, "\n"; | ||
| 330 | print LOG "cipp_perlcheck.pl: $date $$\t$msg\n"; | ||
| 331 | close LOG; | ||
| 332 | |||
| 333 | 1; | ||
| 334 | } | ||
| 335 | |||
| 336 | { | ||
| 337 | my $__CATCHED__WARNINGS__; | ||
| 338 | |||
| 339 | sub eval_perl_code { | ||
| 340 | my ($__PERL_CODE_SREF__) = @_; | ||
| 341 | |||
| 342 | eval { | ||
| 343 | local $SIG{ALRM} = sub { die "CIPP-TIMEOUT" }; | ||
| 344 | alarm 10; | ||
| 345 | $__CATCHED__WARNINGS__=''; | ||
| 346 | no strict; | ||
| 347 | eval "return; ".$$__PERL_CODE_SREF__; | ||
| 348 | alarm 0; | ||
| 349 | $__CATCHED__WARNINGS__ .= $@ | ||
| 350 | if $@ !~ /CIPP-TIMEOUT/; | ||
| 351 | }; | ||
| 352 | |||
| 353 | return $__CATCHED__WARNINGS__; | ||
| 354 | } | ||
| 355 | |||
| 356 | sub exec_perl_code { | ||
| 357 | my ($__PERL_CODE_SREF__) = @_; | ||
| 358 | |||
| 359 | eval { | ||
| 360 | local $SIG{ALRM} = sub { die "CIPP-TIMEOUT" }; | ||
| 361 | alarm 20; | ||
| 362 | $__CATCHED__WARNINGS__=''; | ||
| 363 | no strict; | ||
| 364 | eval $$__PERL_CODE_SREF__; | ||
| 365 | alarm 0; | ||
| 366 | $__CATCHED__WARNINGS__ .= $@ | ||
| 367 | if $@ !~ /CIPP-TIMEOUT/; | ||
| 368 | }; | ||
| 369 | |||
| 370 | return $__CATCHED__WARNINGS__; | ||
| 371 | } | ||
| 372 | |||
| 373 | sub catch_warnings { | ||
| 374 | $__CATCHED__WARNINGS__.=$_[0]; | ||
| 375 | } | ||
| 376 | } | ||
| 377 |