OMatic/dispatch.pm untested

filter: Statementstmt 0.0% 0 / 72 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 115 CC 9 SCAR 38.3
CC
9
Cov
0%
main
90.0
meta
2.0
cgi
2.0
CRAP 46.0
1 ##############################################################################
2 # The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. #
3 # #
4 # This program is free software; you can redistribute it and/or #
5 # modify it under the terms of the GNU General Public License #
6 # as published by the Free Software Foundation; either version 2 #
7 # of the License, or (at your option) any later version. #
8 # #
9 # This program is distributed in the hope that it will be useful, #
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
12 # GNU General Public License for more details. #
13 # #
14 # You should have received a copy of the GNU General Public License #
15 # along with this program; if not, write to the Free Software #
16 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.#
17 # #
18 # Jon Howell can be contacted at: #
19 # 6211 Sudikoff Lab, Dartmouth College #
20 # Hanover, NH 03755-3510 #
21 # jonh@cs.dartmouth.edu #
22 # #
23 # An electronic copy of the GPL is available at: #
24 # http://www.gnu.org/copyleft/gpl.html #
25 # #
26 ##############################################################################
27  
28 use strict;
29  
30 ###
31 ### dispatch.pm
32 ###
33 ### This is the dispatch module; it calls one of the command modules. (Each
34 ### command module used to be a separate CGI, but this makes installation
35 ### a lot easer.) A nearly-empty CGI calls this file, which parses the
36 ### parameters to decide which module to load and run.
37  
38 package FAQ::OMatic::dispatch;
39  
40 #use FAQ::OMatic;
41 # --the fewer pieces we statically include, the more likely we can
42 # dynamically catch any compile errors and display them gracefully
43 # instead of giving an Infernal Server Error.
44  
45 use vars qw($meta $cgi); # to avoid mod_perl 'my' problem. But callers
46 # should access these using the corresponding accessor methods,
47 # not by accessing them directly.
48  
49 sub main {
50 $meta = shift; # The single adjustable parameter in the actual CGI
51 my $haveMeta=0;
52  
53 # it's not so important what this path is (though a good selection
54 # will help the install cmd make better suggestions for the mail and
55 # RCS commands), but that we set it so it's not tainted.
56 $ENV{'PATH'} = '/bin:/usr/bin:/usr/sbin:/usr/local/bin';
57  
58 if (-f "$meta/config") {
59 # note config file is not subject to 'use strict,' since it is
60 # inside its own file.
61 delete $INC{"$meta/config"}; # cause mod_perl to re-read config
62 # TODO: this doesn't really work. mod_perl doesn't reread.
63 # TODO: Jon needs to re-read mod_perl's docs.
64 require "$meta/config";
65 if ($meta eq ($FAQ::OMatic::Config::metaDir||'')) {
66 $haveMeta = 1;
67 } else {
68 print "Content-type: text/plain\n\n";
69 print "meta moved. I can't deal with this case automatically.\n";
70 print "You'll need to manually change your CGI stub to point\n";
71 print "at the new location of meta, plus fix \$metaDir in\n";
72 print "meta/config to match.\n";
73  
74 # This is a pretty uncommon case -- I think we can cope
75 # with killing off a mod_perl child process if this happens.
76 # (It beats trying to load FAQ::OMatic::myExit().)
77 exit 0;
78 }
79 } else {
80 # if unconfigured, the default behavior is to install.
81 # This could be bad -- it means if meta/config becomes inaccessible,
82 # the Internet can install a new faqomatic on your machine. Hmmm.
83 $haveMeta = 0;
84 }
85  
86 # The map trick is a way to make a hash act like a "set" -- we use it
87 # to test membership. This is the set of modules we know (prevents
88 # the user from making up some other module and getting it into
89 # our eval()).
90 my %knownModules = map { $_ => $_ } (
91 'faq', 'help', 'appearanceForm',
92 'search', 'searchForm', 'recent',
93 'recentrdf',
94 'stats', 'statgraph',
95 'authenticate', 'changePass', 'submitPass',
96 'editPart', 'submitPart', 'delPart',
97 'addItem', 'editItem', 'submitItem',
98 'editModOptions', 'submitModOptions',
99 'submitCatToAns', 'submitAnsToCat',
100 'moveItem', 'submitMove',
101 'selectBag', 'editBag', 'submitBag',
102 'install', 'maintenance',
103 'editGroups', 'submitGroup',
104 'img', 'mirrorServer', 'displaySlow'
105 );
106  
107 # functions that we need to run even if the versions mismatch.
108 # not sure every maintenance task can be run when there's
109 # a mismatch, but some need to, since they're accessed from
110 # the installer. Can add another version check in maintenance.pm
111 # if I need to later, I guess.
112 my %versionSafeFunc = map { $_ => $_ } (
113 'install', 'img',
114 'authenticate', 'maintenance',
115 'displaySlow', 'changePass',
116 'submitPass'
117 );
118
119 use CGI;
120
121 $cgi = new CGI;
122 my $cmd = ($haveMeta)
123 ? ($cgi->param('cmd') || 'faq')
124 : 'install';
125  
126 my $problemDesc = '';
127 my $severity = 'problem';
128 my $func;
129  
130 # notice we take the value of the hash lookup, rather than just
131 # testing it -- that handily untaints $func.
132 if ($func = $knownModules{$cmd}) {
133 # Require means we don't load the module until we need it.
134 # (But mod_perl will accumulate modules, and only load them
135 # if they haven't been loaded before, of course.)
136 # This invocation will call the $func module's main()
137  
138 # from here inside, catch warnings as errors.
139 local $SIG{'__WARN__'} = sub { die $_[0] };
140  
141 eval {
142 require FAQ::OMatic;
143 FAQ::OMatic::reset(); # reset the locals (for mod_perl)
144  
145 require "FAQ/OMatic/$func.pm";
146 # require comes before VERSION test because VERSION test
147 # depends on having included FAQ::OMatic.
148  
149 if (($FAQ::OMatic::Config::version || '') ne $FAQ::OMatic::VERSION
150 and not $versionSafeFunc{$func}) {
151 FAQ::OMatic::gripe('abort', "The scripts don't match the "
152 ."configured version number. Admin must run "
153 .FAQ::OMatic::makeAref('-command'=>'install')
154 ."installer</a>. "
155 ."This message has been sent to "
156 ."$FAQ::OMatic::Config::adminEmail.", {'noentify'=>1});
157 }
158  
159 $^T = time(); # when running in mod_perl, -M's get stale w/o this
160 eval "FAQ::OMatic::".$func."::main();";
161 die $@ if ($@); # pass internal errors out to next eval
162 };
163 $problemDesc = $@;
164 $severity = 'problem';
165 } else {
166 # THANKS to Bob Van Cleef <vancleef@microunit.com>
167 # and <superpetz@hushmail.com> for pointing out a
168 # cross-site scripting problem with this error report.
169 # I wonder how many other such helpful debugging features
170 # hide lurking CSS issues?
171 #
172 # Unknown commands seem to come from either broken robots
173 # or 3L33T H@X0RZ, so let's not bother sending mail to
174 # the admin about it.
175 # If you see errors in your log that you wonder about,
176 # then add the $cmd to the problemDesc to find out
177 # what's going on.
178 $problemDesc = 'Unknown command. Are you a confused robot or an 3l33t h@X0r? If neither, check with site admin to debug the problem.';
179 $severity = 'error';
180 }
181
182 if ($problemDesc ne '') {
183 # something broken happened. Let the admin know,
184 # lest it was a script that failed to compile, or a
185 # 'use strict' message or -w warning.
186 # try a nice presentation, else fall back on text:
187 # (unfortunately, text errors don't get mailed to $faqAdmin.)
188 eval {
189 $SIG{'__WARN__'} = sub { die "x"; }; # warnings => something's amok
190 require FAQ::OMatic;
191 FAQ::OMatic::gripe($severity,
192 "${severity}: ${problemDesc}");
193 # don't use 'abort', because in mod_perl that calls
194 # Apache::exit(), which looks like a die, which makes us
195 # think this eval failed.
196 # Squirt out the message:
197 print FAQ::OMatic::pageHeader();
198 print FAQ::OMatic::pageFooter();
199 };
200 if ($@) {
201 # can't use FAQ::OMatic::header() here because FAQ::OMatic
202 # isn't imported here.
203 print $cgi->header('-type'=>"text/html");
204 print "<tt>\n$problemDesc\n</tt><br><font color=blue>"
205 .($@ ne '')."</font>\n";
206 }
207 }
208 }
209  
210 sub meta {
211 return $meta;
212 }
213  
214 sub cgi {
215 return $cgi;
216 }
217  
218 1;