#!/usr/local/bin/perl # swat.cgi # Pass everything to samba's SWAT tool require './samba-lib.pl'; &ReadParse(); # check acls &error_setup("$text{'eacl_aviol'}"); &error("$text{'eacl_np'} $text{'eacl_pcswat'}") unless $access{'swat'}; # Check is hosts allow is in force &get_share("global"); if (&getval('allow hosts')) { &ui_print_header(undef, $text{'error'}, ""); print &text('swat_msg3', $text{'sec_onlyallow'}), "

\n"; &foreign_require("inetd", "inetd-lib.pl"); local @inets = &foreign_call("inetd", "list_inets"); foreach $i (@inets) { if ($i->[3] eq 'swat' && $i->[1]) { # swat is configured in inetd! local $p = getservbyname('swat', 'tcp'); $url = "http://$ENV{'SERVER_NAME'}:$p/"; print &text('swat_msg4', "$url"), "

\n"; } } &ui_print_footer("", $text{'index_sharelist'}); exit; } # Check if we have the password &read_file("$module_config_directory/swat", \%swat) || &ask_password(); # Execute SWAT process pipe(OUTr, OUTw); pipe(INr, INw); local $pid = fork(); if (!$pid) { untie(*STDIN); untie(*STDOUT); untie(*STDERR); open(STDIN, "<&INr"); open(STDOUT, ">&OUTw"); open(STDERR, ">&OUTw"); close(OUTr); close(INw); undef %ENV; exec($config{'swat_path'}); print "Exec failed : $!\n"; exit 1; } close(OUTw); close(INr); # Feed HTTP request and read output $auth = &encode_base64("$swat{'user'}:$swat{'pass'}"); $auth =~ s/\n//g; select(INw); $| = 1; select(STDOUT); $path = $ENV{'PATH_INFO'} || "/"; if ($ENV{'REQUEST_METHOD'} eq 'GET') { print INw "GET $path?$in HTTP/1.0\n"; print INw "Authorization: basic $auth\n"; print INw "\n"; } else { print INw "POST $path HTTP/1.0\r\n"; print INw "Authorization: basic $auth\r\n"; print INw "Content-length: ",length($in),"\r\n"; print INw "Content-type: application/x-www-form-urlencoded\r\n"; print INw "\r\n",$in; } close(INw); $reply = ; if ($reply =~ /\s401\s/) { &ask_password(); } if ($ENV{'PATH_INFO'} =~ /\.(gif|jpg|jpeg|png)$/i) { # An image .. just output it while() { print; } } else { # An HTML page .. fix up links $url = "$gconfig{'webprefix'}/$module_name/swat.cgi"; while() { s/src="(\/[^"]*)"/src="$url$1"/gi; s/href="(\/[^"]*)"/href="$url$1"/gi; s/action="(\/[^"]*)"/action="$url$1"/gi; s/"(\/status?[^"]*)"/"$url$1"/gi; print $_ if (!/<\/body>/i && !/<\/html>/i); } print "
\n"; &ui_print_footer("/$module_name/", $text{'index_sharelist'}, 1); print " ", "$text{'swat_logout'}
\n"; } sub ask_password { &ui_print_header(undef, $text{'swat_title'}, ""); if (%swat) { print $text{'swat_msg1'}, "
\n"; } else { print $text{'swat_msg2'}, "
\n"; } print "

\n"; print "
\n"; print "\n"; print "
$text{'swat_list'}
\n"; print "\n"; print "\n"; print "\n"; print "\n"; print "
$text{'swat_username'}
$text{'swat_password'}
\n"; print " \n"; print "
\n"; &ui_print_footer("", $text{'index_sharelist'}); exit; }