testserver.pl 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. #!/usr/bin/env perl -w
  2. # -*- Mode: perl; indent-tabs-mode: nil -*-
  3. #
  4. # The contents of this file are subject to the Mozilla Public
  5. # License Version 1.1 (the "License"); you may not use this file
  6. # except in compliance with the License. You may obtain a copy of
  7. # the License at http://www.mozilla.org/MPL/
  8. #
  9. # Software distributed under the License is distributed on an "AS
  10. # IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  11. # implied. See the License for the specific language governing
  12. # rights and limitations under the License.
  13. #
  14. # Contributor(s): Joel Peshkin <bugreport@peshkin.net>
  15. # Byron Jones <byron@glob.com.au>
  16. # testserver.pl is invoked with the baseurl of the Bugzilla installation
  17. # as its only argument. It attempts to troubleshoot as many installation
  18. # issues as possible.
  19. use strict;
  20. use lib qw(. lib);
  21. BEGIN {
  22. my $envpath = $ENV{'PATH'};
  23. require Bugzilla;
  24. # $ENV{'PATH'} is required by the 'ps' command to run correctly.
  25. $ENV{'PATH'} = $envpath;
  26. }
  27. use Bugzilla::Constants;
  28. use Socket;
  29. my $datadir = bz_locations()->{'datadir'};
  30. eval "require LWP; require LWP::UserAgent;";
  31. my $lwp = $@ ? 0 : 1;
  32. if ((@ARGV != 1) || ($ARGV[0] !~ /^https?:/))
  33. {
  34. print "Usage: $0 <URL to this Bugzilla installation>\n";
  35. print "e.g.: $0 http://www.mycompany.com/bugzilla\n";
  36. exit(1);
  37. }
  38. # Try to determine the GID used by the web server.
  39. my @pscmds = ('ps -eo comm,gid', 'ps -acxo command,gid', 'ps -acxo command,rgid');
  40. my $sgid = 0;
  41. if ($^O !~ /MSWin32/i) {
  42. foreach my $pscmd (@pscmds) {
  43. open PH, "$pscmd 2>/dev/null |";
  44. while (my $line = <PH>) {
  45. if ($line =~ /^(?:\S*\/)?(?:httpd|apache)2?\s+(\d+)$/) {
  46. $sgid = $1 if $1 > $sgid;
  47. }
  48. }
  49. close(PH);
  50. }
  51. }
  52. # Determine the numeric GID of $webservergroup
  53. my $webgroupnum = 0;
  54. my $webservergroup = Bugzilla->localconfig->{webservergroup};
  55. if ($webservergroup =~ /^(\d+)$/) {
  56. $webgroupnum = $1;
  57. } else {
  58. eval { $webgroupnum = (getgrnam $webservergroup) || 0; };
  59. }
  60. # Check $webservergroup against the server's GID
  61. if ($sgid > 0) {
  62. if ($webservergroup eq "") {
  63. print
  64. "WARNING \$webservergroup is set to an empty string.
  65. That is a very insecure practice. Please refer to the
  66. Bugzilla documentation.\n";
  67. } elsif ($webgroupnum == $sgid) {
  68. print "TEST-OK Webserver is running under group id in \$webservergroup.\n";
  69. } else {
  70. print
  71. "TEST-WARNING Webserver is running under group id not matching \$webservergroup.
  72. This if the tests below fail, this is probably the problem.
  73. Please refer to the web server configuration section of the Bugzilla guide.
  74. If you are using virtual hosts or suexec, this warning may not apply.\n";
  75. }
  76. } elsif ($^O !~ /MSWin32/i) {
  77. print
  78. "TEST-WARNING Failed to find the GID for the 'httpd' process, unable
  79. to validate webservergroup.\n";
  80. }
  81. # Try to fetch a static file (front.png)
  82. $ARGV[0] =~ s/\/$//;
  83. my $url = $ARGV[0] . "/skins/standard/index/front.png";
  84. if (fetch($url)) {
  85. print "TEST-OK Got front picture.\n";
  86. } else {
  87. print
  88. "TEST-FAILED Fetch of skins/standard/index/front.png failed
  89. Your web server could not fetch $url.
  90. Check your web server configuration and try again.\n";
  91. exit(1);
  92. }
  93. # Try to execute a cgi script
  94. my $response = fetch($ARGV[0] . "/testagent.cgi");
  95. if ($response =~ /^OK (.*)$/) {
  96. print "TEST-OK Webserver is executing CGIs via $1.\n";
  97. } elsif ($response =~ /^#!/) {
  98. print
  99. "TEST-FAILED Webserver is fetching rather than executing CGI files.
  100. Check the AddHandler statement in your httpd.conf file.\n";
  101. exit(1);
  102. } else {
  103. print "TEST-FAILED Webserver is not executing CGI files.\n";
  104. }
  105. # Make sure that the web server is honoring .htaccess files
  106. my $localconfig = bz_locations()->{'localconfig'};
  107. $localconfig =~ s~^\./~~;
  108. $url = $ARGV[0] . "/$localconfig";
  109. $response = fetch($url);
  110. if ($response) {
  111. print
  112. "TEST-FAILED Webserver is permitting fetch of $url.
  113. This is a serious security problem.
  114. Check your web server configuration.\n";
  115. exit(1);
  116. } else {
  117. print "TEST-OK Webserver is preventing fetch of $url.\n";
  118. }
  119. # Test chart generation
  120. eval 'use GD';
  121. if ($@ eq '') {
  122. undef $/;
  123. # Ensure major versions of GD and libgd match
  124. # Windows's GD module include libgd.dll, guaranteed to match
  125. if ($^O !~ /MSWin32/i) {
  126. my $gdlib = `gdlib-config --version 2>&1` || "";
  127. $gdlib =~ s/\n$//;
  128. if (!$gdlib) {
  129. print "TEST-WARNING Failed to run gdlib-config; can't compare " .
  130. "GD versions.\n";
  131. }
  132. else {
  133. my $gd = $GD::VERSION;
  134. my $verstring = "GD version $gd, libgd version $gdlib";
  135. $gdlib =~ s/^([^\.]+)\..*/$1/;
  136. $gd =~ s/^([^\.]+)\..*/$1/;
  137. if ($gdlib == $gd) {
  138. print "TEST-OK $verstring; Major versions match.\n";
  139. } else {
  140. print "TEST-FAILED $verstring; Major versions do not match.\n";
  141. }
  142. }
  143. }
  144. # Test GD
  145. eval {
  146. my $image = new GD::Image(100, 100);
  147. my $black = $image->colorAllocate(0, 0, 0);
  148. my $white = $image->colorAllocate(255, 255, 255);
  149. my $red = $image->colorAllocate(255, 0, 0);
  150. my $blue = $image->colorAllocate(0, 0, 255);
  151. $image->transparent($white);
  152. $image->rectangle(0, 0, 99, 99, $black);
  153. $image->arc(50, 50, 95, 75, 0, 360, $blue);
  154. $image->fill(50, 50, $red);
  155. if ($image->can('png')) {
  156. create_file("$datadir/testgd-local.png", $image->png);
  157. check_image("$datadir/testgd-local.png", 'GD');
  158. } else {
  159. print "TEST-FAILED GD doesn't support PNG generation.\n";
  160. }
  161. };
  162. if ($@ ne '') {
  163. print "TEST-FAILED GD returned: $@\n";
  164. }
  165. # Test Chart
  166. eval 'use Chart::Lines';
  167. if ($@) {
  168. print "TEST-FAILED Chart::Lines is not installed.\n";
  169. } else {
  170. eval {
  171. my $chart = Chart::Lines->new(400, 400);
  172. $chart->add_pt('foo', 30, 25);
  173. $chart->add_pt('bar', 16, 32);
  174. $chart->png("$datadir/testchart-local.png");
  175. check_image("$datadir/testchart-local.png", "Chart");
  176. };
  177. if ($@ ne '') {
  178. print "TEST-FAILED Chart returned: $@\n";
  179. }
  180. }
  181. eval 'use Template::Plugin::GD::Image';
  182. if ($@) {
  183. print "TEST-FAILED Template::Plugin::GD is not installed.\n";
  184. }
  185. else {
  186. print "TEST-OK Template::Plugin::GD is installed.\n";
  187. }
  188. }
  189. sub fetch {
  190. my $url = shift;
  191. my $rtn;
  192. if ($lwp) {
  193. my $req = HTTP::Request->new(GET => $url);
  194. my $ua = LWP::UserAgent->new;
  195. my $res = $ua->request($req);
  196. $rtn = ($res->is_success ? $res->content : undef);
  197. } elsif ($url =~ /^https:/i) {
  198. die("You need LWP installed to use https with testserver.pl");
  199. } else {
  200. my($host, $port, $file) = ('', 80, '');
  201. if ($url =~ m#^http://([^:]+):(\d+)(/.*)#i) {
  202. ($host, $port, $file) = ($1, $2, $3);
  203. } elsif ($url =~ m#^http://([^/]+)(/.*)#i) {
  204. ($host, $file) = ($1, $2);
  205. } else {
  206. die("Cannot parse url");
  207. }
  208. my $proto = getprotobyname('tcp');
  209. socket(SOCK, PF_INET, SOCK_STREAM, $proto);
  210. my $sin = sockaddr_in($port, inet_aton($host));
  211. if (connect(SOCK, $sin)) {
  212. binmode SOCK;
  213. select((select(SOCK), $| = 1)[0]);
  214. # get content
  215. print SOCK "GET $file HTTP/1.0\015\012host: $host:$port\015\012\015\012";
  216. my $header = '';
  217. while (defined(my $line = <SOCK>)) {
  218. last if $line eq "\015\012";
  219. $header .= $line;
  220. }
  221. my $content = '';
  222. while (defined(my $line = <SOCK>)) {
  223. $content .= $line;
  224. }
  225. my ($status) = $header =~ m#^HTTP/\d+\.\d+ (\d+)#;
  226. $rtn = (($status =~ /^2\d\d/) ? $content : undef);
  227. }
  228. }
  229. return($rtn);
  230. }
  231. sub check_image {
  232. my ($local_file, $library) = @_;
  233. my $filedata = read_file($local_file);
  234. if ($filedata =~ /^\x89\x50\x4E\x47\x0D\x0A\x1A\x0A/) {
  235. print "TEST-OK $library library generated a good PNG image.\n";
  236. unlink $local_file;
  237. } else {
  238. print "TEST-WARNING $library library did not generate a good PNG.\n";
  239. }
  240. }
  241. sub create_file {
  242. my ($filename, $content) = @_;
  243. open(FH, ">$filename")
  244. or die "Failed to create $filename: $!\n";
  245. binmode FH;
  246. print FH $content;
  247. close FH;
  248. }
  249. sub read_file {
  250. my ($filename) = @_;
  251. open(FH, $filename)
  252. or die "Failed to open $filename: $!\n";
  253. binmode FH;
  254. my $content = <FH>;
  255. close FH;
  256. return $content;
  257. }