SecretKey is now one-time pad, documentation
[CGIscriptor.git] / CGIscriptor.html
blob94b445bc52cedf98c305527cc5071d76f0fe965d
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2 <HTML>
4 <HEAD>
6 <TITLE>CGIscriptor 2.0 Manual</TITLE>
9 </HEAD>
11 <BODY>
13 <H1 ALIGN="CENTER">
14 <I>CGIscriptor 2.0</I>: An implementation of integrated server side CGI scripts
15 </H1>
17 <UL>
18 <P>
19 <LI><A HREF="#HYPE">HYPE</A>
20 <LI><A HREF="#HOWITWORKS">THIS IS HOW IT WORKS</A>
21 <LI><A HREF="#HTML4">HTML 4 COMPLIANCE</A>
22 <LI><A HREF="#SECURITY">SECURITY</A>
23 </P>
25 <P>
26 <LI><A HREF="#MANUAL">USER MANUAL</A>
27 <UL>
28 <LI><A HREF="#INTRODUCTION">INTRODUCTION</A>
29 <LI><A HREF="#NON-HTML">NON-HTML CONTENT TYPES</A>
30 <LI><A HREF="#BINFILES">NON-HTML FILES</A>
31 <LI><A HREF="#META">THE META TAG</A>
32 <LI><A HREF="#DIV">THE DIV/INS TAG</A>
33 <LI><A HREF="#IFUNLESS">CONDITIONAL PROCESSING: THE 'IF' AND 'UNLESS' ATTRIBUTES</A>
34 <LI><A HREF="#SRC">THE MAGIC SOURCE ATTRIBUTE (SRC=)</A>
35 <LI><A HREF="#ROOT">THE CGISCRIPTOR ROOT DIRECTORIES ~/ AND ./</A>
36 <LI><A HREF="#OSSHELL">OS SHELL SCRIPT EVALUATION (CONTENT-TYPE=TEXT/OSSHELL)</A>
37 <LI><A HREF="#TRANSLATIONS">RUN TIME TRANSLATION OF INPUT FILES</A>
38 <LI><A HREF="#LANGUAGES">EVALUATION OF OTHER SCRIPTING LANGUAGES</A>
39 <LI><A HREF="#APPLIC">APPLICATION MIME TYPES</A>
40 <LI><A HREF="#PIPES">SHELL SCRIPT PIPING</A>
41 <LI><A HREF="#SSPERL">PERL CODE EVALUATION (CONTENT-TYPE=TEXT/SSPERL)</A>
42 <LI><A HREF="#SESSIONTICKETS">SERVER SIDE SESSIONS AND ACCESS CONTROL (LOGIN)</A>
43 <LI><A HREF="#USEREXTENSIONS">USER EXTENSIONS</A>
44 <LI><A HREF="#RESULTSSTACK">THE RESULTS STACK: @CGIscriptorResults</A>
45 <LI><A HREF="#CGIPREDEFINED">USEFULL CGI PREDEFINED VARIABLES</A>
46 <LI><A HREF="#ENVIRONMENT">USEFULL CGI ENVIRONMENT VARIABLES</A>
47 <LI><A HREF="#RUNNING">INSTRUCTIONS FOR RUNNING CGIscriptor ON UNIX</A>
48 <LI><A HREF="#NON-UNIX">NON-UNIX OS-PLATFORMS</A>
49 </UL>
50 <LI><A HREF="#license">license</A>
51 </P>
53 </UL>
55 <A NAME="HYPE"><H2 ALIGN="CENTER">HYPE</H2></A>
57 <P>
58 CGIscriptor merges plain ASCII HTML files transparantly and safely
59 with CGI variables, in-line PERL code, shell commands, and executable
60 scripts in many languages (on-line and real-time). It combines the
61 "ease of use" of HTML files with the versatillity of specialized
62 scripts and PERL programs. It hides all the specifics and
63 idiosyncrasies of correct output and CGI coding and naming. Scripts
64 do not have to be aware of HTML, HTTP, or CGI conventions just as HTML
65 files can be ignorant of scripts and the associated values. CGIscriptor
66 complies with the W3C HTML 4.0 recommendations.
67 </P>
69 <P>
70 In addition to its use as a WWW embeded CGI processor, it can
71 be used as a command-line document preprocessor (text-filter).
72 </P>
74 <A NAME="HOWITWORKS"><H2 ALIGN="CENTER">THIS IS HOW IT WORKS</H2></A>
76 <P>
77 The aim of CGIscriptor is to execute "plain" scripts inside a text file
78 using any required CGIparameters and environment variables. It
79 is optimized to transparantly process HTML files inside a WWW server.
80 The native language is Perl, but many other scripting languages
81 can be used.
82 </P>
84 <P>
85 CGIscriptor reads text files from the requested input file (i.e., from
86 $YOUR_HTML_FILES$PATH_INFO) and writes them to &lt;STDOUT&gt; (i.e., the client
87 requesting the service) preceded by the obligatory
88 "Content-type: text/html\n\n" or "Content-type: text/plain\n\n" string
89 (except for "raw" files which supply their own Content-type message
90 and only if the SERVER_PROTOCOL contains HTTP, FTP, GOPHER, MAIL, or MIME).
91 </P>
93 <P>
94 When CGIscriptor encounters an embedded script, indicated by an HTML4 tag
95 </P>
97 <PRE>
98 &lt;SCRIPT TYPE="text/ssperl" [CGI="$name='default value'"] [SRC="ScriptSource"]&gt;
99 PERL script
100 &lt;/SCRIPT&gt;
101 </PRE>
105 <PRE>
106 &lt;SCRIPT TYPE="text/osshell" [CGI="$name='default value'"] [SRC="ScriptSource"]&gt;
107 OS Shell script
108 &lt;/SCRIPT&gt;
109 </PRE>
112 construct (anything between []-brackets is optional, other MIME-types are
113 supported), the embedded script is removed and both the contents of the
114 source file (i.e., "do 'ScriptSource'") AND the script are evaluated as a
115 PERL program (i.e., by eval()), a shell script (i.e., by a "safe" version
116 of `Command`, qx) or an external interpreter. The output of the eval()
117 function takes the place of the original &lt;SCRIPT&gt;&lt;/SCRIPT&gt;
118 construct in the output string. Any CGI parameters declared by the CGI
119 attribute are available as simple perl variables, and can subsequently
120 be made available as variables to other scripting languages (e.g., bash,
121 python, or lisp).
122 </P>
125 Example: printing "Hello World"
126 </P>
128 <PRE>
129 &lt;HTML>&lt;HEAD>&lt;TITLE>Hello World&lt;/TITLE&gt;
130 &lt;BODY&gt;
131 &lt;H1&gt;&lt;SCRIPT TYPE="text/ssperl"&gt;"Hello World"&lt;/SCRIPT&gt;&lt;/H1&gt;
132 &lt;/BODY&gt;&lt;/HTML&gt;
133 </PRE>
136 Save this in a file, hello.html, in the directory you indicated with
137 $YOUR_HTML_FILES and access http://your_server/SHTML/hello.html
138 (or to whatever name you use as an alias for CGIscriptor.pl).
139 This is realy ALL you need to do to get going.
140 </P>
143 You can use any values that are delivered in CGI-compliant form (i.e.,
144 the "?name=value" type URL additions) transparently as "$name" variables
145 in your scripts IFF you have declared them in a META or SCRIPT tag before e.g.:
146 </P>
148 <PRE>
149 &lt;META CONTENT="text/ssperl; CGI='$name = `default value`'
150 [SRC='ScriptSource']"&gt;
151 </PRE>
153 <PRE>
154 &lt;SCRIPT TYPE=text/ssperl CGI="$name = 'default value'"
155 [SRC='ScriptSource']&gt;
156 </PRE>
159 After such a 'CGI' attribute, you can use $name as an ordinary PERL variable
160 (the ScriptSource file is immediately evaluated with "do 'ScriptSource'").
161 The CGIscriptor script allows you to write ordinary HTML files which will
162 include dynamic CGI aware (run time) features, such as on-line answers
163 to specific CGI requests, queries, or the results of calculations.
164 </P>
167 For example, if you wanted to answer questions of clients, you could write
168 a Perl program called "Answer.pl" with a function "AnswerQuestion()"
169 that prints out the answer to requests given as arguments. You then write
170 a HTML page "Respond.html" containing the following fragment:
171 </P>
173 <hr>
174 <PRE>
175 &lt;CENTER&gt;
176 The Answer to your question
177 &lt;META CONTENT="text/ssperl; CGI='$Question'"&gt;
178 &lt;h3&gt;&lt;SCRIPT TYPE="text/ssperl"&gt;$Question&lt;/SCRIPT&gt;&lt;/h3&gt;
180 &lt;h3&gt;&lt;SCRIPT TYPE="text/ssperl" SRC="./PATH/Answer.pl"&gt;
181 AnswerQuestion($Question);
182 &lt;/SCRIPT&gt;&lt;/h3&gt;
183 &lt;CENTER&gt;
184 &lt;FORM ACTION=Respond.html METHOD=GET&gt;
185 Next question: &lt;INPUT NAME="Question" TYPE=TEXT SIZE=40&gt;&lt;br&gt;
186 &lt;INPUT TYPE=SUBMIT VALUE="Ask"&gt;
187 &lt;/FORM&gt;
188 </PRE>
189 <hr>
192 The output could look like the following (in HTML-speak):
193 </P>
195 <hr>
196 <PRE>
197 <CENTER>
198 The Answer to your question
199 <h3>What is the capital of the Netherlands?</h3>
201 <h3>Amsterdam</h3>
202 </CENTER>
203 <FORM ACTION=Respond.html METHOD=GET>
204 Next question: <INPUT NAME="Question" TYPE=TEXT SIZE=40><br>
205 <INPUT TYPE=SUBMIT VALUE="Ask">
206 </PRE>
207 <hr>
210 Note that the function "Answer.pl" does know nothing about CGI or HTML,
211 it just prints out answers to arguments. Likewise, the text has no
212 provisions for scripts or CGI like constructs. Also, it is completely
213 trivial to extend this "program" to use the "Answer" later in the page
214 to call up other information or pictures/sounds. The final text never
215 shows any cue as to what the original "source" looked like, i.e.,
216 where you store your scripts and how they are called.
217 </P>
220 There are some extra's. The argument of the files called in a SRC= tag
221 can access the CGI variables declared in the preceding META tag from
222 the @ARGV array. Executable files are called as:
223 `file '$ARGV[0]' ... ` (e.g., `Answer.pl \'$Question\'`;)
224 The files called from SRC can even be (CGIscriptor) html files which are
225 processed in-line. Furthermore, the SRC= tag can contain a perl block
226 that is evaluated. That is,
227 </P>
229 <PRE>
230 &lt;META CONTENT="text/ssperl; CGI='$Question' SRC='{$Question}'"&gt;
231 </PRE>
234 will result in the evaluation of "print do {$Question};" and the VALUE
235 of $Question will be printed. Note that these "SRC-blocks" can be
236 preceded and followed by other file names, but only a single block is
237 allowed in a SRC= tag.
238 </P>
241 One of the major hassles of dynamic WWW pages is the fact that several
242 mutually incompatible browsers and platforms must be supported. For example,
243 the way sound is played automatically is different for Netscape and
244 Internet Explorer, and for each browser it is different again on
245 Unix, MacOS, and Windows. Realy dangerous is processing user-supplied
246 (form-) values to construct email addresses, file names, or database
247 queries. All Apache WWW-server exploits reported in the media are
248 based on faulty CGI-scripts that didn't check their user-data properly.
249 </p>
252 There is no panacee for these problems, but a lot of work and problems
253 can be safed by allowing easy and transparent control over which
254 &lt;SCRIPT&gt;&lt;/SCRIPT&gt; blocks are executed on what CGI-data. CGIscriptor
255 supplies such a method in the form of a pair of attributes:
256 IF='...condition..' and UNLESS='...condition...'. When added to a
257 script tag, the whole block (including the SRC attribute) will be
258 ignored if the condition is false (IF) or true (UNLESS).
259 For example, the following block will NOT be evaluated if the value
260 of the CGI variable FILENAME is NOT a valid filename:
261 </p>
263 <pre>
264 &lt;SCRIPT TYPE='text/ssperl' CGI='$FILENAME' IF='CGIscriptor::CGIsafeFileName($FILENAME)'&gt;
265 .....
266 &lt;/SCRIPT&gt;
267 </pre>
270 (the function CGIsafeFileName(String) returns an empty string ("")
271 if the String argument is not a valid filename).
272 The UNLESS attribute is the mirror image of IF.
273 </p>
276 A user manual follows the HTML 4 and security paragraphs below.
277 </P>
280 <A NAME="HTML4"><H2 ALIGN="CENTER">HTML 4 COMPLIANCE</H2></A>
283 In general, CGIscriptor.pl complies with the HTML 4 recommendations of
284 the W3C. This means that any software to manage Web sites will be able
285 to handle CGIscriptor files, as will web agents.
286 </P>
289 All script code should be placed between &lt;SCRIPT&gt;&lt;/SCRIPT&gt; tags, the
290 script type is indicated with TYPE="mime-type", the LANGUAGE
291 feature is ignored, and a SRC feature is implemented. All CGI specific
292 features are delegated to the CGI attribute.
293 </P>
296 However, the behavior deviates from the W3C recommendations at some
297 points. Most notably:
298 </P>
300 <DL>
301 <dt>0- The scripts are executed at the server side, invisible to the
302 client (i.e., the browser)
303 <dt>1- The mime-types are personal and idiosyncratic, but can be adapted.
304 <dt>2- Code in the body of a &lt;SCRIPT&gt;&lt;/SCRIPT&gt; tag-pair is still evaluated
305 when a SRC feature is present.
306 <dt>3- The SRC feature reads a list of files.
307 <dt>4- The files in a SRC feature are processed according to file type.
308 <dt>5- The SRC feature evaluates inline Perl code.
309 <dt>6- Processed META, INS, and DIV tags are removed from the output document.
310 <dt>7- All attributes of the processed META tags, except CONTENT, are ignored
311 (i.e., deleted from the output).
312 <dt>8- META tags can be placed ANYWHERE in the document.
313 <dt>9- Through the SRC feature, META tags can have visible output in the
314 document.
315 <dt>10- The CGI attribute that declares CGI parameters, can be used
316 inside the &lt;SCRIPT&gt; tag.
317 <dt>11- Use of an extended quote set, i.e., '', "", ``, (), {}, []
318 and their \-slashed combinations: \'\', \"\", \`\`, \(\),
319 \{\}, \[\].
320 <dt>12- IF and UNLESS attributes to &lt;SCRIPT&gt;, &lt;META&gt;,
321 &lt;INS&gt;, and &lt;DIV&gt; tags.
322 <dt>13- &lt;DIV&gt; tags cannot be nested, &lt;DIV&gt; tags are not
323 rendered with new-lines.
324 <dt>14- The XML style &lt;TAG .... /&gt; is recognized and handled correctly.
325 (i.e., no content is processed)
326 </DL>
329 The reasons for these choices are:
330 </P>
333 You can still write completely HTML4 compliant documents. CGIscriptor
334 will not force you to write "deviant" code. However, it allows you to
335 do so (which is, in fact, just as bad). The prime design principle
336 was to allow users to include plain Perl code. The code itself should
337 be "enhancement free". Therefore, extra features were needed to
338 supply easy access to CGI and Web site components. For security
339 reasons these have to be declared explicitly. The SRC feature
340 transparently manages access to external files, especially the safe
341 use of executable files.
342 </P>
345 The CGI attribute handles the declarations of external (CGI) variables
346 in the SCRIPT and META tag's.<BR>
347 EVERYTHING THE CGI ATTRIBUTE AND THE META TAG DO CAN BE DONE INSIDE
348 A &lt;SCRIPT&gt;&lt;/SCRIPT&gt; TAG CONSTRUCT.
349 </P>
352 The reason for the IF, UNLESS, and SRC attributes (and its Perl code evaluation)
353 were build into the META and SCRIPT tags is part laziness, part security. The SRC
354 blocks allows more compact documents and easier debugging. The values of the
355 CGI variables can be immediately screened for security by IF or UNLESS
356 conditions, and even SRC attributes (e.g., email addresses and file names), and
357 a few commands can be called without having to add another Perl TAG pair.
358 This is especially important for documents that require the use of other
359 (restricted) "scripting" languages that lag transparent control structures.
360 </P>
363 <A NAME="SECURITY"><H2 ALIGN="CENTER">SECURITY</H2></A>
366 Your WWW site is a few keystrokes away from a few hundred million internet
367 users. A fair percentage of these users knows more about your computer
368 than you do. And some of these just might have bad intentions.
369 </P>
372 To ensure uncompromized operation of your server and platform, several
373 features are incorporated in CGIscriptor.pl to enhance security.
374 First of all, you should check the source of this program. No security
375 measures will help you when you download programs from anonymous sources.
376 If you want to use THIS file, please make sure that it is uncompromized.
377 The best way to do this is to contact the source and try to determine
378 whether s/he is reliable (and accountable).
379 </P>
382 BE AWARE THAT ANY PROGRAMMER CAN CHANGE THIS PROGRAM IN SUCH A WAY THAT
383 IT WILL SET THE DOORS TO YOUR SYSTEM WIDE OPEN
384 </P>
387 I would like to ask any user who finds bugs that could compromise
388 security to report them to me (and any other bug too,
389 Email: R.J.J.H.vanSon@uva.nl or ifa@hum.uva.nl).
390 </P>
392 <H2 ALIGN="CENTER">Security features</H2>
394 <dl>
395 <dt>1 Invisibility
396 <dd>The inner workings of the HTML source files are completely hidden
397 from the client. Only the HTTP header and the ever changing content
398 of the output distinguish it from the output of a plain, fixed HTML
399 file. Names, structures, and arguments of the "embedded" scripts
400 are invisible to the client. Error output is suppressed except
401 during debugging (user configurable).
403 <dt>2 Separate directory trees
404 <dd>Directories containing Inline text and script files can reside on
405 separate trees, distinct from those of the HTTP server. This means
406 that NEITHER the text files, NOR the script files can be read by
407 clients other than through CGIscriptor.pl, UNLESS they are
408 EXPLICITELY made available.
410 <dt>3 Requests are NEVER "evaluated"
411 <dd>All client supplied values are used as literal values (''-quoted).
412 Client supplied ''-quotes are ALWAYS removed. Therefore, as long as the
413 embedded scripts do NOT themselves evaluate these values, clients CANNOT
414 supply executable commands. Be sure to AVOID scripts like:
416 <PRE>
417 &lt;META CONTENT="text/ssperl; CGI='$UserValue'"&gt;
418 &lt;SCRIPT TYPE="text/ssperl"&gt;$dir = `ls -1 $UserValue`;&lt;/SCRIPT&gt;
419 </PRE>
422 These are a recipe for disaster. However, the following quoted
423 form should be save (but is still not adviced):
424 </P>
426 <PRE>
427 &lt;SCRIPT TYPE="text/ssperl"&gt;$dir = `ls -1 \'$UserValue\'`;&lt;/SCRIPT&gt;
428 </PRE>
431 A special function, SAFEqx(), will automatically do exactly this,
432 e.g., SAFEqx('ls -1 $UserValue') will execute `ls -1 \'$UserValue\'`
433 with $UserValue interpolated. I recommend to use SAFEqx() instead
434 of backticks whenever you can. The OS shell scripts inside
435 </P>
437 <PRE>
438 &lt;SCRIPT TYPE="text/osshell"&gt;ls -1 $UserValue&lt;/SCRIPT&gt;
439 </PRE>
442 are handeld by SAFEqx and automatically ''-quoted.
443 </P>
445 <dt>4 Logging of requests
446 <dd>All requests can be logged separate from the Host server. The level of
447 detail is user configurable: Including or excluding the actual queries.
448 This allows for the inspection of (im-) proper use.
450 <dt>5 Access control: Clients
451 <dd>The Remote addresses can be checked against a list of authorized
452 (i.e., accepted) or non-authorized (i.e., rejected) clients. Both
453 REMOTE_HOST and REMOTE_ADDR are tested so clients without a proper
454 HOST name can be (in-) excluded by their IP-address. Client patterns
455 containing all numbers and dots are considered IP-addresses, all others
456 domain names. No wild-cards or regexp's are allowed, only partial
457 addresses.<br>
458 Matching of names is done from the back to the front (domain first,
459 i.e., $REMOTE_HOST =~ /\Q$pattern\E$/is), so including ".edu" will
460 accept or reject all clients from the domain EDU. Matching of
461 IP-addresses is done from the front to the back (domain first, i.e.,
462 $REMOTE_ADDR =~ /^\Q$pattern\E/is), so including "128." will (in-)
463 exclude all clients whose IP-address starts with 128.
464 There are two special symbols: "-" matches HOSTs with no name and "*"
465 matches ALL HOSTS/clients.<br>
468 For those needing more expressional power, lines starting with
469 "-e" are evaluated by the perl eval() function. E.g.,
470 '-e $REMOTE_HOST =~ /\.edu$/is;' will accept/reject clients from the
471 domain '.edu'.
472 </P>
474 <dt>6 Access control: Files
475 <dd>In principle, CGIscriptor could read ANY file in the directory
476 tree as discussed in 1. However, for security reasons this is
477 restricted to text files. It can be made more restricted by entering
478 a global file pattern (e.g., ".html"). This is done by default.
479 For each client requesting access, the file pattern(s) can be made
480 more restrictive than the global pattern by entering client specific
481 file patterns in the Access Control files (see 5).
482 For example: if the ACCEPT file contained the lines
484 <PRE>
485 * DEMO
486 .hum.uva.nl LET
487 145.18.230.
488 </PRE>
491 Then all clients could request paths containing "DEMO" or "demo", e.g.
492 "/my/demo/file.html" ($PATH_INFO =~ /\Q$pattern\E/), Clients from
493 *.hum.uva.nl could also request paths containing "LET or "let", e.g.
494 "/my/let/file.html", and clients from the local cluster
495 145.18.230.[0-9]+ could access ALL files.
496 Again, for those needing more expressional power, lines starting with
497 "-e" are evaluated. For instance: <br />
498 '-e $REMOTE_HOST =~ /\.edu$/is && $PATH_INFO =~ m@/DEMO/@is;' <br />
499 will accept/reject requests for files from the directory "/demo/" from
500 clients from the domain '.edu'.
501 </P>
503 <dt>7 Access control: Server side session tickets
504 <dd>Specific paths can be controlled by Session Tickets which must be
505 present as a CGI or Cookie value in the request. These paths
506 are defined in %TicketRequiredPatterns as pairs of:<br />
507 ('regexp' =&gt; 'SessionPath\tPasswordPath\tLogin.html\tExpiration').<br />
508 Session Tickets are stored in a separate directory (SessionPath, e.g.,
509 "Private/.Session") as files with the exact same name of the TICKET
510 variable value.
511 The following is an example of a SESSION ticket:
512 <pre>
513 Type: SESSION
514 IPaddress: 127.0.0.1
515 AllowedPaths: ^/Private/Name/
516 DeniedPaths: ^/Private/CreateUser\.
517 Expires: +3600
518 Username: test
520 </pre>
521 Other content can follow. <br />
522 <br />
523 It is adviced that Session Tickets should expire and be deleted
524 after some (idle) time. The IP address should be the IP number at login, and
525 the ticket will be rejected if it is presented from another IP address.
526 AllowedPaths and DeniedPaths are perl regexps. Be careful how they match. Make sure to delimit
527 the names to prevent access to overlapping names, eg, "^/Private/Rob" will also
528 match "^/Private/Robert", however, "^/Private/Rob/" will not. Expires is the
529 time the ticket will remain valid after creation (file ctime). Time can be given
530 in s[econds] (default), m[inutes], h[hours], or d[ays], eg, "24h" means 24 hours.
531 Only the <em>Type:</em> field needs be present.<br />
532 <br />
533 Next to Session Tickets, there are four other type of ticket files:<br />
534 - LOGIN tickets store information about a current login request<br />
535 - PASSWORD tickets store account information to authorize login requests<br />
536 - IPADDRESS tickets for IP address-only checks<br />
537 - CHALLENGE tickets for challenge tasks for every request<br />
538 </p>
540 <dt>8 Query length limiting
541 <dd>The length of the Query string can be limited. If CONTENT_LENGTH is larger
542 than this limit, the request is rejected. The combined length of the
543 Query string and the POST input is checked before any processing is done.
544 This will prevent clients from overloading the scripts.
545 The actual, combined, Query Size is accessible as a variable through
546 $CGI_Content_Length.
547 </P>
550 <dt>9 Illegal filenames, paths, and protected directories
551 <dd>One of the primary security concerns in handling CGI-scripts is the
552 use of "funny" characters in the requests that con scripts in executing
553 malicious commands. Examples are inserting ';', null bytes, or &lt;newline&gt; characters
554 in URL's and filenames, followed by executable commands. A special
555 variable $FileAllowedChars stores a string of all allowed characters.
556 Any request that translates to a filename with a character OUTSIDE
557 this set will be rejected.<br>
558 In general, all (readable files) in the ServerRoot tree are accessible.
559 This might not be what you want. For instance, your ServerRoot directory
560 might be the working directory of a CVS project and contain sensitive
561 information (e.g., the password to get to the repository). You can block
562 access to these subdirectories by adding the corresponding patterns to
563 the $BlockPathAccess variable. For instance, $BlockPathAccess = '/CVS/'
564 will block any request that contains '/CVS/' or:<br>
565 <pre>
566 die if $BlockPathAccess && $ENV{'PATH_INFO'} =~ m@$BlockPathAccess@;
567 </pre>
568 </P>
571 <dt>10 The execution of code blocks can be controlled in a transparent way
572 by adding IF or UNLESS conditions in the tags themselves.
573 <dd>That is, a simple check of the validity of filenames or email
574 addresses can be done before any code is executed.
575 </p>
577 </dl>
579 <hr>
581 <A NAME="MANUAL"><H1 ALIGN="CENTER">USER MANUAL</H1></A>
583 <UL>
584 <LI><A HREF="#INTRODUCTION">INTRODUCTION</A>
585 <LI><A HREF="#NON-HTML">NON-HTML CONTENT TYPES</A>
586 <LI><A HREF="#BINFILES">NON-HTML FILES</A>
587 <LI><A HREF="#META">THE META TAG</A>
588 <LI><A HREF="#DIV">THE DIV/INS TAG</A>
589 <LI><A HREF="#IFUNLESS">CONDITIONAL PROCESSING: THE 'IF' AND 'UNLESS' ATTRIBUTES</A>
590 <LI><A HREF="#SRC">THE MAGIC SOURCE ATTRIBUTE (SRC=)</A>
591 <LI><A HREF="#ROOT">THE CGISCRIPTOR ROOT DIRECTORIES ~/ AND ./</A>
592 <LI><A HREF="#OSSHELL">OS SHELL SCRIPT EVALUATION (CONTENT-TYPE=TEXT/OSSHELL)</A>
593 <LI><A HREF="#TRANSLATIONS">RUN TIME TRANSLATION OF INPUT FILES</A>
594 <LI><A HREF="#LANGUAGES">EVALUATION OF OTHER SCRIPTING LANGUAGES</A>
595 <LI><A HREF="#PIPES">SHELL SCRIPT PIPING</A>
596 <LI><A HREF="#SSPERL">PERL CODE EVALUATION (CONTENT-TYPE=TEXT/SSPERL)</A>
597 <LI><A HREF="#SESSIONTICKETS">SERVER SIDE SESSIONS AND ACCESS CONTROL (LOGIN)</A>
598 <LI><A HREF="#USEREXTENSIONS">USER EXTENSIONS</A>
599 <LI><A HREF="#RESULTSSTACK">THE RESULTS STACK: @CGIscriptorResults</A>
600 <LI><A HREF="#CGIPREDEFINED">USEFULL CGI PREDEFINED VARIABLES</A>
601 <LI><A HREF="#ENVIRONMENT">USEFULL CGI ENVIRONMENT VARIABLES</A>
602 <LI><A HREF="#RUNNING">INSTRUCTIONS FOR RUNNING CGIscriptor ON UNIX</A>
603 <LI><A HREF="#NON-UNIX">NON-UNIX OS-PLATFORMS</A>
604 </UL>
606 <A NAME="INTRODUCTION"><H2 ALIGN="CENTER">INTRODUCTION</H2></A>
609 CGIscriptor removes embedded scripts, indicated by an HTML 4 type
610 &lt;SCRIPT TYPE='text/ssperl'&gt; &lt;/SCRIPT&gt; or &lt;SCRIPT TYPE='text/osshell'&gt;
611 &lt;/SCRIPT&gt; constructs. The contents of the directive are executed by
612 the PERL eval() and `` functions (in a separate name space). The
613 result of the eval() function replaces the &lt;SCRIPT&gt; &lt;/SCRIPT&gt; construct
614 in the output file. You can use the values that are delivered in
615 CGI-compliant form (i.e., the "?name=value&.." type URL additions)
616 transparently as "$name" variables in your directives after they are
617 defined in a &lt;META&gt; or &lt;SCRIPT&gt; tag.
618 If you define the variable "$CGIscriptorResults" in a CGI attribute, all
619 subsequent &lt;SCRIPT&gt; and &lt;META&gt; results (including the defining
620 tag) will also be pushed onto a stack: @CGIscriptorResults. This list
621 behaves like any other, ordinary list and can be manipulated.
622 </P>
625 Both GET and POST requests are accepted. These two methods are treated
626 equal. Variables, i.e., those values that are determined when a file is
627 processed, are indicated in the CGI attribute by $&lt;name&gt; or
628 $&lt;name&gt;=&lt;default&gt; in which &lt;name&gt; is the name of the
629 variable and &lt;default&gt; is the value used when there is NO current CGI
630 value for &lt;name&gt; (you can use white-spaces in
631 $&lt;name&gt;=&lt;default&gt; but really DO make sure that the default
632 value is followed by white space or is quoted). Names can contain any
633 alphanumeric characters and _ (i.e., names match /[\w]+/).<br>
634 If the <i>Content-type:</i> is 'multipart/*', the input is treated as a
635 MIME multipart message and automatically delimited. CGI variables get the
636 "raw" (i.e., undecoded) body of the corresponding message part.
637 </P>
640 Variables can be CGI variables, i.e., those from the QUERY_STRING,
641 environment variables, e.g., REMOTE_USER, REMOTE_HOST, or REMOTE_ADDR,
642 or predefined values, e.g., CGI_Decoded_QS (The complete, decoded,
643 query string), CGI_Content_Length (the length of the decoded query
644 string), CGI_Year, CGI_Month, CGI_Time, and CGI_Hour (the current
645 date and time).
646 </P>
649 All these are available when defined in a CGI attribute. All environment
650 variables are accessible as $ENV{'name'}. So, to access the REMOTE_HOST
651 and the REMOTE_USER, use, e.g.:
652 </P>
654 <PRE>
655 &lt;SCRIPT TYPE='text/ssperl'&gt;
656 ($ENV{'REMOTE_HOST'}||"-")." $ENV{'REMOTE_USER'}"
657 &lt;/SCRIPT&gt;
658 </PRE>
661 (This will print a "-" if REMOTE_HOST is not known)
662 Another way to do this is:
663 </P>
665 <PRE>
666 &lt;META CONTENT="text/ssperl; CGI='$REMOTE_HOST = - $REMOTE_USER'"&gt;
667 &lt;SCRIPT TYPE='text/ssperl'&gt;"$REMOTE_HOST $REMOTE_USER"&lt;/SCRIPT&gt;
668 </PRE>
672 <PRE>
673 &lt;META CONTENT='text/ssperl; CGI="$REMOTE_HOST = - $REMOTE_USER"
674 SRC={"$REMOTE_HOST $REMOTE_USER\n"}'&gt;
675 </PRE>
678 This is possible because ALL environment variables are available as
679 CGI variables. The environment variables take precedence over CGI
680 names in case of a "name clash". For instance:
681 </P>
683 <PRE>
684 &lt;META CONTENT="text/ssperl; CGI='$HOME' SRC={$HOME}"&gt;
685 </PRE>
688 Will print the current HOME directory (environment) irrespective whether
689 there is a CGI variable from the query
690 (e.g., Where do you live? &lt;INPUT TYPE="TEXT" NAME="HOME"&gt;)
691 THIS IS A SECURITY FEATURE. It prevents clients from changing
692 the values of defined environment variables (e.g., by supplying
693 a bogus $REMOTE_ADDR). Although $ENV{} is not changed by the META tags,
694 it would make the use of declared variables insecure. You can still
695 access CGI variables after a name clash with
696 CGIscriptor::CGIparseValue(&lt;name&gt;).
697 </P>
700 Some CGI variables are present several times in the query string
701 (e.g., from multiple selections). These should be defined as
702 @VARIABLENAME=default in the CGI attribute. The list @VARIABLENAME
703 will contain ALL VARIABLENAME values from the query, or a single
704 default value. If there is an ENVIRONMENT variable of the
705 same name, it will be used instead of the default AND the query
706 values. The corresponding function is
707 CGIscriptor::CGIparseValueList(&lt;name&gt;)
708 </P>
711 CGI variables collected in a @VARIABLENAME list are unordered.
712 When more structured variables are needed, a hash table can be used.
713 A variable defined as %VARIABLE=default will collect all
714 CGI-parameter values whose name start with 'VARIABLE' in a hash table
715 with the remainder of the name as a key. For instance, %PERSON will
716 collect PERSONname='John Doe', PERSONbirthdate='01 Jan 00', and
717 PERSONspouse='Alice' into a hash table %PERSON such that
718 $PERSON{'spouse'} equals 'Alice'. Any default value or environment
719 value will be stored under the "" key. If there is an ENVIRONMENT
720 variable of the same name, it will be used instead of the default
721 AND the query values. The corresponding function is
722 CGIscriptor::CGIparseValueHash(&lt;name&gt;)
723 </P>
726 This method of first declaring your environment and CGI variables
727 before being able to use them in the scripts might seem somewhat
728 clumsy, but it protects you from inadvertedly printing out the values of
729 system environment variables when their names coincide with those used
730 in the CGI forms. It also prevents "clients" from supplying CGI parameter
731 values for your private variables.
732 THIS IS A SECURITY FEATURE!
733 </P>
735 <A NAME="NON-HTML"><H2 ALIGN="CENTER">NON-HTML CONTENT TYPES</H2></A>
738 Normally, CGIscriptor prints the standard "Content-type: text/html\n\n"
739 message before anything is printed. This has been extended to include
740 plain text (.txt) files, for which the Content-type (MIME type)
741 'text/plain' is printed. In all other respects, text files are treated as
742 HTML files (this can be switched off by removing '.txt' from the
743 $FilePattern variable). When the content type should be something else,
744 e.g., with multipart files, use the $RawFilePattern (.xmr, see also next
745 item). CGIscriptor will not print a Content-type message for this file type
746 (which must supply its OWN Content-type message). Raw files must still
747 conform to the &lt;SCRIPT&gt;&lt;/SCRIPT&gt; and &lt;META&gt; tag
748 specifications.
749 </P>
751 <A NAME="BINFILES"><H2 ALIGN="CENTER">NON-HTML FILES</H2></A>
754 CGIscriptor is intended to process HTML and text files only. You can
755 create documents of any mime-type on-the-fly using "raw" text files, e.g.,
756 with the .xmr extension. However, CGIscriptor will not process binary files
757 of any type, e.g., pictures or sounds. Given the sheer number of formats, I
758 do not have any intention to do so. However, an escape route has been
759 provided. You can construct a genuine raw (.xmr) text file that contains
760 the perl code to service any file type you want. If the global
761 $BinaryMapFile variable contains the path to this file (e.g.,
762 /BinaryMapFile.xmr), this file will be called whenever an unsupported
763 (non-HTML) file type is requested. The path to the requested binary file
764 is stored in $ENV('CGI_BINARY_FILE') and can be used like any other
765 CGI-variable. Servicing binary files then becomes supplying the correct
766 Content-type (e.g., print "Content-type: image/jpeg\n\n";) and reading the
767 file and writing it to STDOUT (e.g., using sysread() and syswrite()).
768 </P>
770 <A NAME="META"><H2 ALIGN="CENTER">THE META TAG</H2></A>
773 All attributes of a META tag are ignored, except the
774 CONTENT='text/ssperl; CGI=" ... " [SRC=" ... "]' attribute. The string
775 inside the quotes following the CONTENT= indication (white-space is
776 ignored, "'` (){}[]-quotes are allowed, plus their \ versions) MUST
777 start with any of the CGIscriptor mime-types (e.g.: text/ssperl or
778 text/osshell) and a comma or semicolon.
779 The quoted string following CGI= contains a white-space separated list
780 of declarations of the CGI (and Environment) values and default values
781 used when no CGI values are supplied by the query string.
782 </P>
785 If the default value is a longer string containing special characters,
786 possibly spanning several lines, the string must be enclosed in quotes.
787 You may use any pair of quotes or brackets from the list '', "", ``, (),
788 [], or {} to distinguish default values (or preceded by \, e.g., \(...\)
789 is different from (...)). The outermost pair will always be used and any
790 other quotes inside the string are considered to be part of the string
791 value, e.g.,
792 </P>
794 <PRE>
795 $Value = {['this'
796 "and" (this)]}
797 </PRE>
800 will result in $Value getting the default value
801 </P>
803 <PRE>
804 ['this'
805 "and" (this)]
806 </PRE>
809 (NOTE that the newline is part of the default value!).
810 </P>
813 Internally, for defining and initializing CGI (ENV) values, the META
814 and SCRIPT tags use the function "defineCGIvariable($name, $default)"
815 (scalars) and "defineCGIvariableList($name, $default)" (lists).
816 These functions can be used inside scripts as
817 "CGIscriptor::defineCGIvariable($name, $default)" and
818 "CGIscriptor::defineCGIvariableList($name, $default)".
819 </P>
822 The CGI attribute will be processed exactly identical when used inside
823 the &lt;SCRIPT&gt; tag. However, this use is not according to the
824 HTML 4.0 specifications of the W3C.
825 </P>
827 <A NAME="DIV"><H2 ALIGN="CENTER">THE DIV/INS TAG</H2></A>
830 There is a problem when constructing html files containing
831 server-side perl scripts with standard HTML tools. These
832 tools will refuse to process any text between
833 &lt;SCRIPT&gt;&lt;/SCRIPT&gt;
834 tags. This is quite annoying when you want to use large
835 HTML templates where you will fill in values.
836 </P>
839 For this purpose, CGIscriptor will read the neutral
840 &lt;DIV CLASS="ssperl" ID="varname"&gt;&lt;/DIV&gt;
841 &lt;INS CLASS="ssperl" ID="varname"&gt;&lt;/INS&gt;
842 tag (in Cascading Style Sheet manner) Note that "varname" has
843 NO '$' before it, it is a bare name. Any text between
844 these &lt;DIV ...&gt;&lt;/DIV&gt; or
845 &lt;INS ...&gt;&lt;/INS&gt; tags will be assigned
846 to '$varname' as is (e.g., as a literal). No
847 processing or interpolation will be performed.
848 There is also NO nesting possible. Do NOT nest
849 &lt;/DIV&gt; inside a &lt;DIV&gt;&lt;/DIV&gt;!
850 Moreover, DIV tags do NOT ensure a block structure in
851 the final rendering (i.e., no empty lines).
852 </P>
855 Note that &lt;DIV CLASS="ssperl" ID="varname"/&gt;
856 is handled the XML way. No content is processed,
857 but varname is defined, and any SRC directives are
858 processed.
859 </P>
862 You can use $varname like any other variable name.
863 However, $varname is NOT a CGI variable and will be
864 completely internal to your script. There is NO
865 interaction between $varname and the outside world.
866 </P>
869 To interpolate a DIV derived text, you can use:
870 <pre>
871 $varname =~ s/([\]])/\\\1/g; # Mark ']'-quotes
872 $varname = eval("qq[$varname]"); # Interpolate all values
873 </pre>
874 </P>
877 The DIV tag will process IF, UNLESS, CGI and SRC attributes.
878 The SRC files will be pre-pended to the body
879 text of the tag.
880 </p>
882 <A NAME="IFUNLESS"><H2 ALIGN="CENTER">
883 CONDITIONAL PROCESSING: THE 'IF' AND 'UNLESS' ATTRIBUTES
884 </H2></A>
887 It is often necessary to include code-blocks that should be executed
888 conditionally, e.g., only for certain browsers or operating system.
889 Furthermore, quite often sanity and security checks are necessary
890 before user (form) data can be processed, e.g., with respect to
891 email addresses and filenames.
892 </p>
895 Checks added to the code are often difficult to find, interpret or
896 maintain and in general mess up the code flow. This kind of confussion
897 is dangerous. Also, for many of the supported "foreign" scripting
898 languages, adding these checks is cumbersome or even impossible.
899 </p>
902 As a uniform method for asserting the correctness of "context", two
903 attributes are added to all supported tags: IF and UNLESS.
904 They both evaluate their value and block execution when the
905 result is &lt;FALSE&gt; (IF) or &lt;TRUE&gt; (UNLESS) in Perl, e.g.,
906 UNLESS='$NUMBER \&gt; 100;' blocks execution if $NUMBER &lt;= 100. Note that
907 the backslash in the '\&gt;' is removed and only used to differentiate
908 this conditional '&gt;' from the tag-closing '&gt;'. For symmetry, the
909 backslash in '\&lt;' is also removed. Inside these conditionals,
910 ~/ and ./ are expanded to their respective directory root paths.
911 </p>
914 For example, the following tag will be ignored when the filename is
915 invalid:
916 </p>
918 <pre>
919 &lt;SCRIPT TYPE='text/ssperl' CGI='$FILENAME'
920 IF='CGIscriptor::CGIsafeFileName($FILENAME);'&gt;
922 &lt;/SCRIPT&gt;
923 </pre>
926 The IF and UNLESS values must be quoted. The same quotes are supported
927 as with the other attributes. The SRC attribute is ignored when IF and
928 UNLESS block execution.
929 </p>
931 <A NAME="SRC"><H2 ALIGN="CENTER">
932 THE MAGIC SOURCE ATTRIBUTE (SRC=)</H2></A>
935 The SRC attribute inside tags accepts a list of filenames and URL's
936 separated by "," comma's (or ";" semicolons).
937 </P>
940 ALL the variable values defined in the CGI attribute are available in
941 @ARGV as if the file was executed from the command line, in
942 the exact order in which they were declared in the preceding CGI
943 attribute.
944 </P>
947 First, a SRC={}-block will be evaluated as if the code inside the
948 block was part of a &lt;SCRIPT&gt;&lt;/SCRIPT&gt; construct, i.e.,
949 "print do { code };'';" or `code` (i.e., SAFEqx('code)).
950 Only a single block is evaluated. Note that this is processed less
951 efficiently than &lt;SCRIPT&gt; &lt;/SCRIPT&gt; blocks. Type of evaluation
952 depends on the content-type: Perl for text/ssperl and OS shell for
953 text/osshell. For other mime types (scripting languages), anything in
954 the source block is put in front of the code block "inside" the tag.
955 </P>
958 Second, executable files (i.e., -x filename != 0) are evaluated as:
959 print `filename \'$ARGV[0]\' \'$ARGV[1]\' ...`
960 That is, you can actually call executables savely from the SRC tag.
961 </P>
964 Third, text files that match the file pattern, used by CGIscriptor to
965 check whether files should be processed ($FilePattern), are
966 processed in-line (i.e., recursively) by CGIscriptor as if the code
967 was inserted in the original source file. Recursions, i.e., calling
968 a file inside itself, are blocked. If you need them, you have to code
969 them explicitely using "main::ProcessFile($file_path)".
970 </P>
973 Fourth, Perl text files (i.e., -T filename != 0) are evaluated as:
974 "do FileName;'';".
975 </P>
978 Last, URL's (i.e., starting with 'HTTP://', 'FTP://', 'GOPHER://', 'TELNET://',
979 'WHOIS://' etc.) are loaded and printed. The loading and handling of &lt;BASE&gt;
980 and document header is done by main::GET_URL($URL [, 0]). You can enter your own
981 code (default is <i>curl</i>, <i>snarf</i>, or <i>wget</i> and some
982 post-processing to add a &lt;BASE&gt; tag).
983 </P>
986 There are two pseudo-file names: PREFIX and POSTFIX. These implement
987 a switch from prefixing the SRC code/files (PREFIX, default) before the content of
988 the tag to appending the code after the content of the tag (POSTFIX). The switches
989 are done in the order in which the PREFIX and POSTFIX labels are encountered.
990 You can mix PREFIX and POSTFIX labels in any order with the SRC files.
991 Note that the ORDER of file execution is determined for prefixed and
992 postfixed files seperately.
996 File paths can be preceded by the URL protocol prefix "file://". This
997 is simply STRIPPED from the name.
998 </P>
1001 Example:
1002 </P>
1005 The request
1006 "http://cgi-bin/Action_Forms.pl/Statistics/Sign_Test.html?positive=8&negative=22
1007 will result in printing "${SS_PUB}/Statistics/Sign_Test.html"
1008 With QUERY_STRING = "positive=8&negative=22"
1009 </P>
1012 on encountering the lines:
1013 </P>
1015 <PRE>
1016 &lt;META CONTENT="text/osshell; CGI='$positive=11 $negative=3'"&gt;
1017 &lt;b&gt;&lt;SCRIPT TYPE="text/ssperl" SRC="./Statistics/SignTest.pl"&gt;
1018 &lt;/SCRIPT&gt;&lt;/b&gt;&lt;p&gt;"
1019 </PRE>
1021 This line will be processed as:
1023 <PRE>
1024 "&lt;b&gt;`${SS_SCRIPT}/Statistics/SignTest.pl '8' '22'`&lt;/b&gt;&lt;p&gt;"
1025 </PRE>
1028 In which "${SS_SCRIPT}/Statistics/SignTest.pl" is an executable script,
1029 This line will end up printed as:
1030 </P>
1032 <PRE>
1033 "&lt;b&gt;p &lt;= 0.0161&lt;/b&gt;&lt;p&gt;"
1034 </PRE>
1037 Note that the META tag itself will never be printed, and is invisible to
1038 the outside world.
1039 </P>
1042 The SRC files in a DIV/INS tag will be added (pre-pended) to the body
1043 of the &lt;DIV&gt;&lt;/DIV&gt; tag. Blocks are NOT executed!
1044 </P>
1046 <A NAME="ROOT"><H2 ALIGN="CENTER">THE CGISCRIPTOR ROOT DIRECTORIES ~/ AND ./</H2></A>
1049 Inside &lt;SCRIPT&gt;&lt;/SCRIPT&gt; tags, filepaths starting
1050 with "~/" are replaced by "$YOUR_HTML_FILES/", this way files in the
1051 public directories can be accessed without direct reference to the
1052 actual paths. Filepaths starting with "./" are replaced by
1053 "$YOUR_SCRIPTS/" and this should only be used for scripts.
1054 The "$YOUR_SCRIPTS" directory is added to @INC so, e.g., the
1055 'require' command will load from the "$YOUR_SCRIPTS" directory.
1056 </P>
1059 <b>Note:</b> this replacement can seriously affect Perl scripts. Watch
1060 out for constructs like $a =~ s/aap\./noot./g, use
1061 $a =~ s@aap\.@noot.@g instead.
1062 </P>
1065 CGIscriptor.pl will assign the values of $SS_PUB and $SS_SCRIPT
1066 (i.e., $YOUR_HTML_FILES and $YOUR_SCRIPTS) to the environment variables
1067 $SS_PUB and $SS_SCRIPT. These can be accessed by the scripts that are
1068 executed. The "$SS_SCRIPT" ($YOUR_SCRIPTS) directory is added to
1069 @INC so, e.g., the 'require' command will load from the "$SS_SCRIPT"
1070 directory.<br>
1071 Values not preceded by $, ~/, or ./ are used as literals
1072 </P>
1074 <A NAME="OSSHELL"><H2 ALIGN="CENTER">OS SHELL SCRIPT EVALUATION (CONTENT-TYPE=TEXT/OSSHELL)</H2></A>
1077 OS scripts are executed by a "safe" version of the `` operator (i.e.,
1078 SAFEqx(), see also below) and any output is printed. CGIscriptor will
1079 interpolate the script and replace all user-supplied CGI-variables by
1080 their ''-quoted values (actually, all variables defined in CGI attributes are
1081 quoted). Other Perl variables are interpolated in a simple fasion, i.e.,
1082 $scalar by their value, @list by join(' ', @list), and %hash by their
1083 name=value pairs. Complex references, e.g., @$variable, are all
1084 evaluated in a scalar context. Quotes should be used with care.
1085 NOTE: the results of the shell script evaluation will appear in the
1086 @CGIscriptorResults stack just as any other result.
1087 </P>
1090 All occurrences of $@% that should NOT be interpolated must be
1091 preceeded by a "\". Interpolation can be switched off completely by
1092 setting $CGIscriptor::NoShellScriptInterpolation = 1
1093 (set to 0 or undef to switch interpolation on again)
1094 i.e.,
1095 </P>
1097 <PRE>
1098 &lt;SCRIPT TYPE="text/ssperl"&gt;
1099 $CGIscriptor::NoShellScriptInterpolation = 1;
1100 &lt;/SCRIPT&gt;
1101 </PRE>
1103 <A NAME="TRANSLATIONS">
1104 <H2 ALIGN="CENTER">RUN TIME TRANSLATION OF INPUT FILES</h2>
1107 Allows general and global conversions of files using Regular Expressions.
1108 Very handy (but costly) to rewrite legacy pages to a new format.
1109 Select files to use it on with <br>
1110 my $TranslationPaths = 'filepattern';<br>
1111 This is costly. For efficiency, define:<br>
1112 $TranslationPaths = ''; when not using translations.<br>
1113 Accepts general regular expressions: [$pattern, $replacement]
1114 </p>
1117 Define:</p>
1118 <pre>
1119 my $TranslationPaths = 'filepattern'; # Pattern matching PATH_INFO
1121 push(@TranslationTable, ['pattern', 'replacement']);
1122 # e.g. (for Ruby Rails):
1123 push(@TranslationTable, ['&lt;%=', '&lt;SCRIPT TYPE="text/ssruby"&gt;']);
1124 push(@TranslationTable, ['%&gt;', '&lt;/SCRIPT&gt;']);
1126 # Runs:
1127 my $currentRegExp;
1128 foreach $currentRegExp (@TranslationTable)
1130 my ($pattern, $replacement) = @$currentRegExp;
1131 $$text =~ s!$pattern!$replacement!msg;
1133 </pre>
1135 <A NAME="LANGUAGES">
1136 <H2 ALIGN="CENTER">EVALUATION OF OTHER SCRIPTING LANGUAGES</H2>
1137 </A>
1140 Adding a MIME-type and an interpreter command to
1141 %ScriptingLanguages automatically will catch any other
1142 scripting language in the standard
1143 &lt;SCRIPT TYPE="[mime]"&gt;&lt;/SCRIPT&gt; manner.
1144 E.g., adding: $ScriptingLanguages{'text/sspython'} = 'python';
1145 will actually execute the folowing code in an HTML page
1146 (ignore 'REMOTE_HOST' for the moment):
1147 </P>
1149 <PRE>
1150 &lt;SCRIPT TYPE="text/sspython"&gt;
1151 # A Python script
1152 x = ["A","real","python","script","Hello","World","and", REMOTE_HOST]
1153 print x[4:8] # Prints the list ["Hello","World","and", REMOTE_HOST]
1154 &lt;/SCRIPT&gt;
1155 </PRE>
1158 The script code is NOT interpolated by perl, EXCEPT for those
1159 interpreters that cannot handle variables themselves.
1160 Currently, several interpreters are pre-installed:
1161 </P>
1163 <PRE>
1164 Perl test - "text/testperl" =&gt; 'perl',
1165 Python - "text/sspython" =&gt; 'python',
1166 Ruby - "text/ssruby" =&gt; 'ruby',
1167 Tcl - "text/sstcl" =&gt; 'tcl',
1168 Awk - "text/ssawk" =&gt; 'awk -f-',
1169 Gnu Lisp - "text/sslisp" =&gt; 'rep | tail +5 '.
1170 # "| egrep -v '&gt; |^rep. |^nil\\\$'",
1171 Gnu Prolog- "text/ssprolog" =&gt; 'gprolog',
1172 M4 macro's- "text/ssm4" =&gt; 'm4',
1173 Born shell- "text/sh" =&gt; 'sh',
1174 Bash - "text/bash" =&gt; 'bash',
1175 C-shell - "text/csh" =&gt; 'csh',
1176 Korn shell- "text/ksh" =&gt; 'ksh',
1177 Praat - "text/sspraat" =&gt; "praat - | sed 's/Praat &gt; //g'",
1178 R - "text/ssr" =&gt; "R --vanilla --slave | sed 's/^[\[0-9\]*] //g'",
1179 REBOL - "text/ssrebol" =&gt;
1180 "rebol --quiet|egrep -v '^[&gt; ]* == '|sed 's/^\s*\[&gt; \]* //g'",
1181 PostgreSQL- "text/postgresql" =&gt; 'psql 2&gt;/dev/null',
1182 (psql)
1183 </PRE>
1186 Note that the "value" of $ScriptingLanguages{mime} must be a command
1187 that reads Standard Input and writes to standard output. Any extra
1188 output of interactive interpreters (banners, echo's, prompts)
1189 should be removed by piping the output through 'tail', 'grep',
1190 'sed', or even 'awk' or 'perl'.
1191 </P>
1194 For access to CGI variables there is a special hashtable:
1195 %ScriptingCGIvariables.
1196 CGI variables can be accessed in three ways.
1197 <dl>
1198 <dt>1. If the mime type is not present in %ScriptingCGIvariables,
1199 nothing is done and the script itself should parse the relevant
1200 environment variables.
1201 <dt>2. If the mime type IS present in %ScriptingCGIvariables, but it's
1202 value is empty, e.g., $ScriptingCGIvariables{"text/sspraat"} = '';,
1203 the script text is interpolated by perl. That is, all $var, @array,
1204 %hash, and \-slashes are replaced by their respective values.
1205 <dt>3. In all other cases, the CGI and environment variables are added
1206 in front of the script according to the format stored in
1207 %ScriptingCGIvariables. That is, the following (pseudo-)code is
1208 executed for each CGI- or Environment variable defined in the CGI-tag:
1209 printf(INTERPRETER, $ScriptingCGIvariables{$mime}, $CGI_NAME, $CGI_VALUE);
1210 </dl>
1211 </P>
1214 For instance, "text/testperl" =&gt; '$%s = "%s";' defines variable
1215 definitions for Perl, and "text/sspython" =&gt; '%s = "%s"' for Python
1216 (note that these definitions are not save, the real ones contain '-quotes).
1217 </P>
1220 THIS WILL NOT WORK FOR @VARIABLES, the (empty) $VARIABLES will be used
1221 instead.
1222 </P>
1225 The $CGI_VALUE parameters are "shrubed" of all control characters
1226 and quotes (by &shrubCGIparameter($CGI_VALUE)). Control characters
1227 are replaced by \0&lt;octal ascii value&gt; and quotes by their HTML character
1228 value (&#8217; -&gt; &amp;#8217; &#8216; -&gt; &amp;#8216;
1229 &quot; -&gt; &amp;quot;). For example:
1230 if a client would supply the string value (in standard perl)
1231 </P>
1234 <PRE>"/dev/null';\nrm -rf *;\necho '"</PRE>
1235 it would be processed as
1236 <PRE>'/dev/null&amp;#8217;;\015rm -rf *;\015echo &amp;#8217;'</PRE>
1237 (e.g., sh or bash would process the latter more according to your
1238 intentions).<br>
1239 If your intepreter requires different protection measures, you will
1240 have to supply these in %main::SHRUBcharacterTR (string =&gt; translation),
1241 e.g.,
1243 <PRE>
1244 $SHRUBcharacterTR{"\'"} = "&amp;#8217;";
1245 </PRE>
1246 </P>
1249 Currently, the following definitions are used:
1250 </P>
1252 <PRE>
1253 %ScriptingCGIvariables = (
1254 "text/testperl" =&gt; "\$\%s = '\%s';", # Perl $VAR = 'value' (for testing)
1255 "text/sspython" =&gt; "\%s = '\%s'", # Python VAR = 'value'
1256 "text/ssruby" =&gt; '@%s = "%s"', # Ruby @VAR = "value"
1257 "text/sstcl" =&gt; 'set %s "%s"', # TCL set VAR "value"
1258 "text/ssawk" =&gt; '%s = "%s";', # Awk VAR = "value";
1259 "text/sslisp" =&gt; '(setq %s "%s")', # Gnu lisp (rep) (setq VAR "value")
1260 "text/ssprolog" =&gt; '', # Gnu prolog (interpolated)
1261 "text/ssm4" =&gt; "define(`\%s', `\%s')", # M4 macro's define(`VAR', `value')
1262 "text/sh" =&gt; "\%s='\%s';", # Born shell VAR='value';
1263 "text/bash" =&gt; "\%s='\%s';", # Born again shell VAR='value';
1264 "text/csh" =&gt; "\$\%s = '\%s';", # C shell $VAR = 'value';
1265 "text/ksh" =&gt; "\$\%s = '\%s';", # Korn shell $VAR = 'value';
1266 "text/sspraat" =&gt; '', # Praat (interpolation)
1267 "text/ssr" =&gt; '%s &lt;- "%s";', # R VAR &lt;- "value";
1268 "text/ssrebol" =&gt; '%s: copy "%s"', # REBOL VAR: copy "value"
1269 "text/postgresql" =&gt; '', # PostgreSQL (interpolation)
1270 "" =&gt; ""
1272 </PRE>
1275 Four tables allow fine-tuning of interpreter with code that should be
1276 added before and after each code block:
1277 </P>
1280 Code added before each script block
1281 </P>
1283 <PRE>
1284 %ScriptingPrefix = (
1285 "text/testperl" =&gt; "\# Prefix Code;", # Perl script testing
1286 "text/ssm4" =&gt; 'divert(0)' # M4 macro's (open STDOUT)
1288 </PRE>
1291 Code added at the end of each script block
1292 </P>
1294 <PRE>
1295 %ScriptingPostfix = (
1296 "text/testperl" =&gt; "\# Postfix Code;", # Perl script testing
1297 "text/ssm4" =&gt; 'divert(-1)' # M4 macro's (block STDOUT)
1299 </PRE>
1302 Initialization code, inserted directly after opening (NEVER interpolated)
1303 </P>
1305 <PRE>
1306 %ScriptingInitialization = (
1307 "text/testperl" =&gt; "\# Initialization Code;", # Perl script testing
1308 "text/ssawk" =&gt; 'BEGIN {', # Server Side awk scripts
1309 "text/sslisp" =&gt; '(prog1 nil ', # Lisp (rep)
1310 "text/ssm4" =&gt; 'divert(-1)' # M4 macro's (block STDOUT)
1312 </PRE>
1315 Cleanup code, inserted before closing (NEVER interpolated)
1316 </P>
1318 <PRE>
1319 %ScriptingCleanup = (
1320 "text/testperl" =&gt; "\# Cleanup Code;", # Perl script testing
1321 "text/sspraat" =&gt; 'Quit',
1322 "text/ssawk" =&gt; '};', # Server Side awk scripts
1323 "text/sslisp" =&gt; '(princ "\n" standard-output)).' # Closing print to rep
1324 "text/postgresql" =&gt; '\q',
1326 </PRE>
1329 The SRC attribute is NOT magical for these interpreters. In short,
1330 all code inside a source file or {} block is written verbattim
1331 to the interpreter. No (pre-)processing or executional magic is done.
1332 </P>
1335 A serious shortcomming of the described mechanism for handling other
1336 (scripting) languages, with respect to standard perl scripts
1337 (i.e., 'text/ssperl'), is that the code is only executed when
1338 the pipe to the interpreter is closed. So the pipe has to be
1339 closed at the end of each block. This means that the state of the
1340 interpreter (e.g., all variable values) is lost after the closing of
1341 the next &lt;/SCRIPT&gt; tag. The standard 'text/ssperl' scripts retain
1342 all values and definitions.
1343 </P>
1346 <A NAME="APPLIC"><H2 ALIGN="CENTER">APPLICATION MIME TYPES</H2></A>
1349 To ease some important auxilliary functions from within the
1350 html pages I have added them as MIME types. This uses
1351 the mechanism that is also used for the evaluation of
1352 other scripting languages, with interpolation of CGI
1353 parameters (and perl-variables). Actually, these are
1354 defined exactly like any other "scripting language".
1355 </P>
1357 <dl>
1358 <dt>text/ssdisplay:
1359 <dd>display some (HTML) text with interpolated
1360 variables (uses `cat`).
1361 <dt>text/sslogfile:
1362 <dd>write (append) the interpolated block to the file
1363 mentioned on the first, non-empty line
1364 (the filename can be preceded by 'File: ',
1365 note the space after the ':',
1366 uses `awk .... &gt;&gt; &lt;filename&gt;`).
1367 <dt>text/ssmailto:
1368 <dd>send email directly from within the script block.
1369 The first line of the body must contain
1370 To:Name@Valid.Email.Address
1371 (note: NO space between 'To:' and the email adres)
1372 For other options see the mailto man pages.
1373 It works by directly sending the (interpolated)
1374 content of the text block to a pipe into the
1375 Linux program 'mailto'.
1376 </dl>
1379 In these script blocks, all Perl variables will be
1380 replaced by their values. All CGI variables are cleaned before
1381 they are used. These CGI variables must be redefined with a
1382 CGI attribute to restore their original values.
1383 In general, this will be more secure than constructing
1384 e.g., your own email command lines. For instance, Mailto will
1385 not execute any odd (forged) email address, but just stops
1386 when the email address is invalid and awk will construct
1387 any filename you give it (e.g. '&lt;File;rm\\\040-f' would end up
1388 as a "valid" UNIX filename). Note that it will also gladly
1389 store this file anywhere (/../../../etc/passwd will work!).
1390 Use the CGIscriptor::CGIsafeFileName() function to clean the
1391 filename.
1392 </P>
1394 <A NAME="PIPES"><H2 ALIGN="CENTER">SHELL SCRIPT PIPING</H2></A>
1397 If a shell script starts with the UNIX style "#! &lt;shell command&gt; \n"
1398 line, the rest of the shell script is piped into the indicated command,
1399 i.e.,
1400 open(COMMAND, "| command");print COMMAND $RestOfScript;
1401 </P>
1404 In many ways this is equivalent to the MIME-type profiling for
1405 evaluating other scripting languages as discussed above. The
1406 difference breaks down to convenience. Shell script piping is a
1407 "raw" implementation. It allows you to control all aspects of
1408 execution. Using the MIME-type profiling is easier, but has a
1409 lot of defaults built in that might get in the way. Another
1410 difference is that shell script piping uses the SAFEqx() function,
1411 and MIME-type profiling does not.
1412 </P>
1415 Execution of shell scripts is under the control of the Perl Script blocks
1416 in the document. The MIME-type triggered execution of <SCRIPT></SCRIPT>
1417 blocks can be simulated easily. You can switch to a different shell, e.g. tcl,
1418 completely by executing the following Perl commands inside your document:
1419 </P>
1421 <PRE>
1422 &lt;SCRIPT TYPE="text/ssperl"&gt;
1423 $main::ShellScriptContentType = "text/ssTcl"; # Yes, you can do this
1424 CGIscriptor::RedirectShellScript('/usr/bin/tcl'); # Pipe to Tcl
1425 $CGIscriptor::NoShellScriptInterpolation = 1;
1426 &lt;/SCRIPT&gt;
1427 </PRE>
1430 After this script is executed, CGIscriptor will parse scripts of
1431 TYPE="text/ssTcl" and pipe their contents into '|/usr/bin/tcl'
1432 WITHOUT interpolation (i.e., NO substitution of Perl variables).
1433 The crucial function is :
1434 </P>
1436 <PRE>
1437 CGIscriptor::RedirectShellScript('/usr/bin/tcl')
1438 </PRE>
1441 After executing this function, all shell scripts AND all
1442 calls to SAFEqx()) are piped into '|/usr/bin/tcl'. If the argument
1443 of RedirectShellScript is empty, e.g., '', the original (default)
1444 value is reset.
1445 </P>
1448 The standard output, STDOUT, of any pipe is send to the client.
1449 Currently, you should be carefull with quotes in such a piped script.
1450 The results of a pipe is NOT put on the @CGIscriptorResults stack.
1451 As a result, you do not have access to the output of any piped (#!)
1452 process! If you want such access, execute
1453 </P>
1455 <PRE>
1456 &lt;SCRIPT TYPE="text/ssperl"&gt;echo "script"|command&lt;/SCRIPT&gt;
1457 </PRE>
1461 </P>
1463 <PRE>
1464 &lt;SCRIPT TYPE="text/ssperl"&gt;
1465 $resultvar = SAFEqx('echo "script"|command');
1466 &lt;/SCRIPT&gt;.
1467 </PRE>
1470 Safety is never complete. Although SAFEqx() prevents some of the
1471 most obvious forms of attacks and security slips, it cannot prevent
1472 them all. Especially, complex combinations of quotes and intricate
1473 variable references cannot be handled safely by SAFEqx. So be on
1474 guard.
1475 </P>
1477 <A NAME="SSPERL"><H2 ALIGN="CENTER">PERL CODE EVALUATION (CONTENT-TYPE=TEXT/SSPERL)</H2></A>
1480 All PERL scripts are evaluated inside a PERL package. This package
1481 has a separate name space. This isolated name space protects the
1482 CGIscriptor.pl program against interference from user code. However,
1483 some variables, e.g., $_, are global and cannot be protected. You are
1484 advised NOT to use such global variable names. You CAN write
1485 directives that directly access the variables in the main program.
1486 You do so at your own risk (there is definitely enough rope available
1487 to hang yourself). The behavior of CGIscriptor becomes undefined if
1488 you change its private variables during run time. The PERL code
1489 directives are used as in:
1490 </P>
1492 <PRE>
1493 $Result = eval($directive); print $Result;'';
1494 </PRE>
1497 ($directive contains all text between &lt;SCRIPT&gt;&lt;/SCRIPT&gt;).
1498 That is, the &lt;directive&gt; is treated as ''-quoted string and
1499 the result is treated as a scalar. To prevent the VALUE of the code
1500 block from appearing on the client's screen, end the directive with
1501 ';""&lt;/SCRIPT&gt;'. Evaluated directives return the last value, just as
1502 eval(), blocks, and subroutines, but only as a scalar.
1503 </P>
1506 IMPORTANT: All PERL variables defined are persistent. Each &lt;SCRIPT&gt;
1507 &lt;/SCRIPT&gt; construct is evaluated as a {}-block with associated scope
1508 (e.g., for "my $var;" declarations). This means that values assigned
1509 to a PERL variable can be used throughout the document unless they
1510 were declared with "my". The following will actually work as intended
1511 (note that the ``-quotes in this example are NOT evaluated, but used
1512 as simple quotes):
1513 </P>
1515 <PRE>
1516 &lt;META CONTENT="text/ssperl; CGI=`$String='abcdefg'`"&gt;
1517 anything ...
1518 &lt;SCRIPT TYPE="text/ssperl"&gt;@List = split('', $String);&lt;/SCRIPT&gt;
1519 anything ...
1520 &lt;SCRIPT TYPE="text/ssperl"&gt;join(", ", @List[1..$#List]);&lt;/SCRIPT&gt;
1521 </PRE>
1524 The first &lt;SCRIPT TYPE="text/ssperl"&gt;&lt;/SCRIPT&gt; construct will return the
1525 value scalar(@List), the second &lt;SCRIPT TYPE="text/ssperl"&gt;&lt;/SCRIPT&gt;
1526 construct will print the elements of $String separated by commas, leaving
1527 out the first element, i.e., $List[0].
1528 </P>
1531 Another warning: './' and '~/' are ALWAYS replaced by the values of
1532 $YOUR_SCRIPTS and $YOUR_HTML_FILES, respectively . This can interfere
1533 with pattern matching, e.g., $a =~ s/aap\./noot\./g will result in the
1534 evaluations of $a =~ s/aap\\${YOUR_SCRIPTS}noot\./g. Use
1535 s@<i>regexp</i>@<i>replacement</i>@g instead.
1536 </p>
1538 <A NAME="SESSIONTICKETS"><H2 ALIGN="CENTER">SERVER SIDE SESSIONS AND ACCESS CONTROL (LOGIN)</H2></A>
1540 An infrastructure for user acount authorization and file access control
1541 is available. Each request is matched against a list of URL path patterns.
1542 If the request matches, a Session Ticket is required to access the URL.
1543 This Session Ticket should be present as a CGI parameter or Cookie, eg:
1544 </p>
1546 CGI: SESSIONTICKET=&lt;value&gt;<br />
1547 Cookie: CGIscriptorSESSION=&lt;value&gt;</p>
1549 The example implementation stores Session Tickets as files in a local
1550 directory. To create Session Tickets, a Login request must be given
1551 with a LOGIN=&lt;value&gt; CGI parameter, a user name and a (doubly hashed)
1552 password. The user name and (singly hashed) password are stored in a
1553 PASSWORD ticket with the same name as the user account (name cleaned up
1554 for security).
1555 </p>
1557 The example session model implements 4 functions:
1558 <ol>
1559 <li>Login<br />
1560 The password is hashed with the user name and server side salt, and then
1561 hashed with a random salt. Client and Server both perform these actions
1562 and the Server only grants access if restults are the same. The server
1563 side only stores the password hashed with the user name and
1564 server side salt. Neither the plain password, nor the hashed password is
1565 ever exchanged. Only values hashed with the one-time salt are exchanged.
1566 </li>
1567 <li>Session<br />
1568 For every access to a restricted URL, the Session Ticket is checked before
1569 access is granted. There are three session modes. The first uses a fixed
1570 Session Ticket that is stored as a cookie value in the browser (actually,
1571 as a sessionStorage value). The second uses only the IP address at login
1572 to authenticate requests. The third
1573 is a Challenge mode, where the client has to calculate the value of the
1574 next one-time Session Ticket from a value derived from the password and
1575 a random string.
1576 </li>
1577 <li>Password Change<br />
1578 A new password is hashed with the user name and server side salt, and
1579 then encrypted (XORed)
1580 with the old password hashed with the user name and salt and rehashed with
1581 the login ticket number. Ath the server side this operation is reversed.
1582 Again, the stored password value is never exchanged unencrypted.
1583 </li>
1584 <li>New Account<br />
1585 The text of a new account (Type: PASSWORD) file is constructed from
1586 the new username (CGI: <em>NEWUSERNAME</em>, converted to lowercase) and
1587 hashed new password (CGI: <em>NEWPASSWORD</em>).
1588 The same process is used to encrypt
1589 the new password as is used for the Password Change function.
1590 Again, the stored password value is never exchanged unencrypted.
1591 Some default setting are encoded. For display in the browser, the new password
1592 is reencrypted (XORed) with a special key, the old password hash
1593 hashed with a session specific random hex value sent initially with the
1594 session login ticket ($RANDOMSALT).
1595 <br />For example for user <em>NewUser</em>
1596 and password <em>NewPassword</em>:
1597 <pre>
1598 Type: PASSWORD
1599 Username: newuser
1600 Password: 84b26fd2aaacae1c2e42fe07da1793e8232ffe548eceb519b46646fe9ff32612
1601 Salt: 970e68017413fb0ea84d7fe3c463077636dd6d53486910d4a53c693dd4109b1a
1602 AllowedPaths: ^/Private/[\w\-]*\.html?
1603 AllowedPaths: ^/Private/newuser/
1604 Session: SESSION
1605 Date: Thu Jun 14 12:34:40 2012 UTC
1606 Time: 1339677280
1607 </pre>
1608 The password is created with the Unix commands:
1609 <pre>
1610 printf '%s' '970e68017413fb0ea84d7fe3c463077636dd6d53486910d4a53c693dd4109b1aNewPasswordnewuser'|shasum -a 256
1611 </pre>
1612 </li>
1613 </ol>
1614 </p>
1615 <H3 ALIGN="CENTER">Implementation</H3>
1617 The session authentication mechanism is based on the exchange of ticket
1618 identifiers. A ticket identifier is just a string of characters, a name
1619 or a random 64 character hexadecimal string. Ticket identifiers should be
1620 "safe" filenames (except user names). There are four types of tickets:
1621 <ul>
1622 <li>PASSWORD: User account descriptors, including a user name and password</li>
1623 <li>LOGIN: Temporary anonymous tickets used during login</li>
1624 <li>IPADDRESS: Authetication tokens that allow access based on the IP address of the request</li>
1625 <li>SESSION: Reusable authetication tokens</li>
1626 <li>CHALLENGE: One-time authetication tokens</li>
1627 </ul>
1628 All tickets can have an expiration date in the form of a time duration
1629 from creation, in seconds, minutes, hours, or days (<em>+duration</em>[smhd]).
1630 An absolute time can be given in seconds since the epoch of the server host.
1631 Note that expiration times of CHALLENGE authetication tokens are calculated
1632 from the last access time. Accounts can include a maximal lifetime
1633 for session tickets (MaxLifetime).
1634 </p>
1636 A Login page should create a LOGIN ticket file locally and send a
1637 server specific salt, a Random salt, and a LOGIN ticket
1638 identifier. The server side compares the username and hashed password,
1639 actually hashed(Random salt+hashed(serversalt+password)) from the client with
1640 the values it calculates from the stored Random salt from the LOGIN
1641 ticket and the hashed(serversalt+password) from the PASSWORD ticket. If
1642 successful, a new SESSION ticket is generated as a hash sum of the LOGIN
1643 ticket and the stored password. This SESSION ticket should also be
1644 generated by the client and stored as sessionStorage and cookie values
1645 as needed. The Username, IP address and Path are available as
1646 $LoginUsername, $LoginIPaddress, and $LoginPath, respectively.
1647 </p>
1649 The CHALLENGE protocol stores the same value as the SESSION tickets.
1650 However, this value is not exchanged, but kept secret in the JavaScript
1651 <em>sessionStorage</em> object. Instead, every page returned from the
1652 server will contain a one-time Challenge value ($CHALLENGETICKET) which
1653 has to be hashed with the stored value to return the current ticket
1654 id string.
1655 </p>
1657 In the current example implementation, all random values are created as
1658 full, 256 bit SHA256 hash values (Hex strings) of 64 bytes read from
1659 /dev/urandom.
1660 </p>
1662 <H3 ALIGN="CENTER">Security considerations with Session tickets</H3>
1664 For strong security, please use end-to-end encryption. This can be
1665 achieved using a VPN (Virtual Private Network), SSH tunnel, or a HTTPS
1666 capable server with OpenSSL. The session ticket system of CGIscriptor.pl
1667 is intended to be used as a simple authentication mechanism WITHOUT
1668 END-TO-END ENCRYPTION. The authenticating mechanism tries to use some
1669 simple means to protect the authentication process from eavesdropping.
1670 For this it uses a secure hash function, SHA256. For all practial purposes,
1671 it is impossible to "decrypt" a SHA256 sum. But this login scheme is
1672 only as secure as your browser. Which, in general, is not very secure.
1673 </p>
1675 Humans tend to reuse passwords. A compromise of a site running
1676 CGIscriptor.pl could therefore lead to a compromise of user accounts at
1677 other sites. Therefore, plain text passwords are never stored, used, or
1678 exchanged. Instead, a server site salt value is "encrypted" with
1679 the plain password and user name. Actually, all are concatenated and hashed
1680 with a one-way secure hash function (SHA256) into a single string.
1681 Whenever the word "password" is used, this hash sum is meant. Note that
1682 the salts are generated from /dev/urandom. You should check whether the
1683 implementation of /dev/urandom on your platform is secure before
1684 relying on it. This might be a problem when running CGIscriptor under
1685 Cygwin on MS Windows.<br />
1686 <em>Note: no attempt is made to slow down the password hash, so bad
1687 passwords can be cracked by brute force</em>
1688 </p>
1690 For the authentication and a change of password, the (old) password
1691 is used to "encrypt" a random one-time token or the new password,
1692 respectively. For authentication, decryption is not needed, so a secure
1693 hash function (SHA256) is used to create a one-way hash sum "encryption".
1694 A new password must be decrypted. New passwords are encryped by XORing
1695 them with the old password.
1696 </p>
1698 <h3 align=CENTER>Strong Passwords: It is so easy</h3>
1699 <p align=CENTER><em>If you only could see what you are typing</em></p>
1700 <p >
1701 Your password might be vulnerable to
1702 <a href="https://en.wikipedia.org/wiki/Brute_force_attack">
1703 <em>brute force</em></a> guessing. Protections against such attacks are
1704 costly in terms of code complexity, bugs, and execution time.
1705 However, there is a very simple and secure counter measure. See the
1706 <a href="http://xkcd.com/936/" target="_blank">XKCD comic</a>. The phrase,
1707 <em>There is no password like more password</em> would
1708 be both much easier to remember, and still stronger than
1709 <em>h4]D%@m:49</em>, at least before this phrase was pasted as an example
1710 on the Internet.<br />
1711 For the procedures used at this site, a basic computer setup can check
1712 in the order of a billion passwords per second. You need a password (or
1713 phrase) strength in the order of 56 bits to be a little secure (one year
1714 on a single computer).<br />
1715 Please be so kind and add the name of your favorite flower, dish,
1716 fictional character, or small town to your password. Say,
1717 <em>Oleander</em>, <em>Curry</em>, <em>Sherlock</em>, or <em>Bath</em> (each adds
1718 ~12 bits) or even the phrase <em>Sherlock investigates oleander curry in
1719 Bath</em> (adds &gt; 56 bits, note that oleander is <em>poisonous</em>, so
1720 do not try this curry at home). That would be more effective than adding
1721 a thousand rounds of encryption.
1722 Typing long passwords without seeing what you are typing is problematic.
1723 So a button should be included to make password visible.
1724 </p>
1725 <h3 align=CENTER>Technical matters</h3>
1727 Client side JavaScript code definitions. Varaible names starting with '$'
1728 are CGIscriptor CGI variables.
1729 </p>
1730 <pre>
1731 // On Login
1732 HashPlaintextPassword() {
1733 var plaintextpassword = document.getElementById('PASSWORD');
1734 var serversalt = document.getElementById('SERVERSALT');
1735 var username = document.getElementById('CGIUSERNAME');
1736 return hex_sha256(serversalt.value+plaintextpassword.value+username.value.toLowerCase());
1738 var randomsalt = $RANDOMSALT; // From CGIscriptor
1739 var loginticket = $LOGINTICKET; // From CGIscriptor
1740 // Hash plaintext password
1741 var password = HashPlaintextPassword();
1742 // Authorize login
1743 var hashedpassword = hex_sha256(randomsalt+password);
1744 // Sessionticket
1745 var sessionticket = hex_sha256(loginticket+password);
1746 sessionStorage.setItem("CGIscriptorPRIVATE", sessionticket);
1747 // Secretkey for encrypting new passwords, acts like a one-time pad
1748 // Is set anew with <em>every</em> login, ie, also whith password changes
1749 // and for each create new user request
1750 var secretkey = hex_sha256(randomsalt+loginticket+password);
1751 sessionStorage.setItem("CGIscriptorSECRET", secretkey);
1753 // For a SESSION type request
1754 sessionticket = sessionStorage.getItem("CGIscriptorPRIVATE");
1755 createCookie("CGIscriptorSESSION",sessionticket, 0, "");
1757 // For a CHALLENGE type request
1758 var sessionset = "$CHALLENGETICKET"; // From CGIscriptor
1759 var sessionkey = sessionStorage.getItem("CGIscriptorPRIVATE");
1760 sessionticket = hex_sha256(sessionset+sessionkey);
1761 createCookie("CGIscriptorCHALLENGE",sessionticket, 0, "");
1763 // For transmitting a new password
1764 HashPlaintextNewPassword() {
1765 var plaintextpassword = document.getElementById('NEWPASSWORD');
1766 var serversalt = document.getElementById('SERVERSALT');
1767 var username = document.getElementById('NEWUSERNAME');
1768 return hex_sha256(serversalt.value+plaintextpassword.value+username.value.toLowerCase());
1771 var newpassword = document.getElementById('NEWPASSWORD');
1772 var newpasswordrep = document.getElementById('NEWPASSWORDREP');
1773 // Hash plaintext password
1774 newpassword.value = HashPlaintextNewPassword();
1775 var secretkey = sessionStorage.getItem("CGIscriptorSECRET");
1777 var encrypted = XOR_hex_strings(secretkey, newpassword.value);
1778 newpassword.value = encrypted;
1779 newpasswordrep.value = encrypted;
1781 // XOR of hexadecimal strings of equal length
1782 function XOR_hex_strings(hex1, hex2) {
1783 var resultHex = "";
1784 var maxlength = Math.max(hex1.length, hex2.length);
1786 for(var i=0; i &lt; maxlength; ++i) {
1787 var h1 = hex1.charAt(i);
1788 if(! h1) h1='0';
1789 var h2 = hex2.charAt(i);
1790 if(! h2) h2 ='0';
1791 var d1 = parseInt(h1,16);
1792 var d2 = parseInt(h2,16);
1793 var resultD = d1^d2;
1794 resultHex = resultHex+resultD.toString(16);
1796 return resultHex;
1800 </pre>
1802 <A NAME="USEREXTENSIONS"><H2 ALIGN="CENTER">USER EXTENSIONS</H2></A>
1805 A CGIscriptor package is attached to the bottom of this file. With
1806 this package you can personalize your version of CGIscriptor by
1807 including often used perl routines. These subroutines can be
1808 accessed by prefixing their names with CGIscriptor::, e.g.,
1809 </P>
1811 <PRE>
1812 &lt;SCRIPT TYPE="text/ssperl"&gt;
1813 CGIscriptor::ListDocs("/Books/*") # List all documents in /Books
1814 &lt;/SCRIPT&gt;
1815 </PRE>
1818 It already contains some useful subroutines for Document Management.
1819 As it is a separate package, it has its own namespace, isolated from
1820 both the evaluator and the main program. To access variables from
1821 the document &lt;SCRIPT&gt;&lt;/SCRIPT&gt; blocks, use $CGIexecute::&lt;var&gt;.
1822 </P>
1825 Currently, the following functions are implemented
1826 (precede them with CGIscriptor::, see below for more information)
1827 </P>
1829 <UL>
1830 <LI>SAFEqx ('String') -&gt; result of qx/"String"/ # Safe application of ``-quotes<br>
1831 Is used by text/osshell Shell scripts. Protects all CGI
1832 (client-supplied) values with single quotes before executing the
1833 commands (one of the few functions that also works WITHOUT CGIscriptor::
1834 in front)
1835 <LI>defineCGIvariable ($name[, $default) -&gt; 0/1 (i.e.,
1836 failure/success)<br>
1837 Is used by the META tag to define and initialize CGI and ENV
1838 name/value pairs. Tries to obtain an initializing value from (in
1839 order):<br>
1840 $ENV{$name}<br>
1841 The Query string<br>
1842 The default value given (if any)<br>
1843 (one of the few functions that also works WITHOUT CGIscriptor::
1844 in front)
1845 <LI>CGIsafeFileName (FileName) -> FileName or ""<br>
1846 Check a string against the Allowed File Characters (and ../ /..).
1847 Returns an empty string for unsafe filenames.
1848 <LI>CGIsafeEmailAddress (Email) -> Email or ""<br>
1849 Check a string against correct email address pattern.
1850 Returns an empty string for unsafe addresses.
1851 <LI>RedirectShellScript ('CommandString') -&gt; FILEHANDLER or undef<br>
1852 Open a named PIPE for SAFEqx to receive ALL shell scripts
1853 <LI>URLdecode (URL encoded string) -&gt; plain string # Decode URL encoded argument<br>
1854 <LI>URLencode (plain string) -&gt; URL encoded string # Encode argument as URL code<br>
1855 <LI>CGIparseValue (ValueName [, URL_encoded_QueryString]) -&gt; Decoded value<br>
1856 Extract the value of a CGI variable from the global or a private
1857 URL-encoded query (multipart POST raw, NOT decoded)
1858 <li>CGIparseValueList (ValueName [, URL_encoded_QueryString])
1859 -&gt; List of decoded values.<br>
1860 As CGIparseValue, but now assembles ALL values of ValueName into a list.
1861 <LI>CGIparseHeader (ValueName [, URL_encoded_QueryString]) -> Header<br>
1862 Extract the header of a multipart CGI variable from the global or a private
1863 URL-encoded query ("" when not a multipart variable or absent)
1864 <LI>CGIparseForm ([URL_encoded_QueryString]) -&gt; Decoded Form<br>
1865 Decode the complete global URL-encoded query or a private
1866 URL-encoded query
1867 <LI>read_url(URL)<br>
1868 Returns the page from URL (with added base tag, both FTP and HTTP)
1869 Uses main::GET_URL(URL, 1) to get at the command to read the URL.
1870 <LI>BrowseDirs(RootDirectory [, Pattern, Startdir, CGIname]) # print browsable directories
1871 <LI>ListDocs(Pattern [,ListType]) # Prints a nested HTML directory listing of
1872 all documents, e.g., ListDocs("/*", "dl");.<br>
1873 <LI>HTMLdocTree(Pattern [,ListType]) # Prints a nested HTML listing of all
1874 local links starting from a given document, e.g.,
1875 HTMLdocTree("/Welcome.html", "dl");<br>
1876 </UL>
1878 <A NAME="RESULTSSTACK"><H2 ALIGN="CENTER">THE RESULTS STACK: @CGIscriptorResults</H2></A>
1881 If the pseudo-variable "$CGIscriptorResults" has been defined in a
1882 META tag, all subsequent SCRIPT and META results are pushed
1883 on the @CGIscriptorResults stack. This list is just another
1884 Perl variable and can be used and manipulated like any other list.
1885 $CGIscriptorResults[-1] is always the last result.
1886 This is only of limited use, e.g., to use the results of an OS shell
1887 script inside a Perl script. Will NOT contain the results of Pipes
1888 or code from MIME-profiling.
1889 </P>
1891 <A NAME="CGIPREDEFINED"><H2 ALIGN="CENTER">USEFULL CGI PREDEFINED VARIABLES (DO NOT ASSIGN TO THESE)</H2></A>
1893 <ul>
1894 <li>$CGI_HOME - The ServerRoot directory
1895 <li>$CGI_Decoded_QS - The complete decoded Query String
1896 <li>$CGI_Content_Length - The ACTUAL length of the Query String
1897 <li>$CGI_Date - Current date and time
1898 <li>$CGI_Year $CGI_Month $CGI_Day $CGI_WeekDay - Current Date
1899 <li>$CGI_Time - Current Time
1900 <li>$CGI_Hour $CGI_Minutes $CGI_Seconds - Current Time, split
1901 GMT Date/Time:
1902 <li>$CGI_GMTYear $CGI_GMTMonth $CGI_GMTDay $CGI_GMTWeekDay $CGI_GMTYearDay
1903 <li>$CGI_GMTHour $CGI_GMTMinutes $CGI_GMTSeconds $CGI_GMTisdst
1904 </ul>
1906 <A NAME="ENVIRONMENT"><H2 ALIGN="CENTER">USEFULL CGI ENVIRONMENT VARIABLES</H2></A>
1909 Variables accessible (in APACHE) as $ENV{"&lt;name&gt;"}
1910 (see: "http://hoohoo.ncsa.uiuc.edu/cgi/env.html"):
1911 </P>
1913 <UL>
1914 <LI>QUERY_STRING - The query part of URL, that is, everything that follows the
1915 question mark.
1916 <LI>PATH_INFO - Extra path information given after the script name
1917 <LI>PATH_TRANSLATED - Extra pathinfo translated through the rule system.
1918 (This doesn't always make sense.)
1919 <LI>REMOTE_USER - If the server supports user authentication, and the script is
1920 protected, this is the username they have authenticated as.
1921 <LI>REMOTE_HOST - The hostname making the request. If the server does not have
1922 this information, it should set REMOTE_ADDR and leave this unset
1923 <LI>REMOTE_ADDR - The IP address of the remote host making the request.
1924 <LI>REMOTE_IDENT - If the HTTP server supports RFC 931 identification, then this
1925 variable will be set to the remote user name retrieved from
1926 the server. Usage of this variable should be limited to logging
1927 only.
1928 <LI>AUTH_TYPE - If the server supports user authentication, and the script
1929 is protected, this is the protocol-specific authentication
1930 method used to validate the user.
1931 <LI>CONTENT_TYPE - For queries which have attached information, such as HTTP
1932 POST and PUT, this is the content type of the data.
1933 <LI>CONTENT_LENGTH - The length of the said content as given by the client.
1934 <LI>SERVER_SOFTWARE - The name and version of the information server software
1935 answering the request (and running the gateway).
1936 Format: name/version
1937 <LI>SERVER_NAME - The server's hostname, DNS alias, or IP address as it
1938 would appear in self-referencing URLs
1939 <LI>GATEWAY_INTERFACE - The revision of the CGI specification to which this
1940 server complies. Format: CGI/revision
1941 <LI>SERVER_PROTOCOL - The name and revision of the information protocol this
1942 request came in with. Format: protocol/revision
1943 <LI>SERVER_PORT - The port number to which the request was sent.
1944 <LI>REQUEST_METHOD - The method with which the request was made. For HTTP,
1945 this is "GET", "HEAD", "POST", etc.
1946 <LI>SCRIPT_NAME - A virtual path to the script being executed, used for
1947 self-referencing URLs.
1948 <LI>HTTP_ACCEPT - The MIME types which the client will accept, as given by
1949 HTTP headers. Other protocols may need to get this
1950 information from elsewhere. Each item in this list should
1951 be separated by commas as per the HTTP spec.
1952 Format: type/subtype, type/subtype
1953 <LI>HTTP_USER_AGENT - The browser the client is using to send the request.
1954 General format: software/version library/version.
1955 </UL>
1957 <A NAME="RUNNING"><H2 ALIGN="CENTER">INSTRUCTIONS FOR RUNNING CGIscriptor ON UNIX</H2></A>
1960 CGIscriptor.pl will run on any WWW server that runs Perl scripts,
1961 just add a line like the following to your srm.conf file
1962 (Apache example):
1963 </P>
1965 <pre>
1966 ScriptAlias /SHTML/ /real-path/CGIscriptor.pl/
1967 </pre>
1970 URL's that refer to http://www.your.address/SHTML/... will now be handled
1971 by CGIscriptor.pl, which can use a private directory tree (default is the
1972 DOCUMENT_ROOT directory tree, but it can be anywhere, see manual).
1973 </P>
1976 If your hosting ISP won't let you add ScriptAlias lines you can use
1977 the following "rewrite"-based "scriptalias" in .htaccess
1978 (from Gerd Franke)
1979 </P>
1981 <pre>
1982 RewriteEngine On
1983 RewriteBase /
1984 RewriteCond %{REQUEST_FILENAME} .html$
1985 RewriteCond %{SCRIPT_FILENAME} !cgiscriptor.pl$
1986 RewriteCond %{REQUEST_FILENAME} -f
1987 RewriteRule ^(.*)$ /cgi-bin/cgiscriptor.pl/$1?%{QUERY_STRING}
1988 </Pre>
1991 Everthing with the extension ".html" and not including "cgiscriptor.pl"
1992 in the url and where the file "path/filename.html" exists is redirected
1993 to "/cgi.bin/cgiscriptor.pl/path/filename.html?query".
1994 The user configuration should get the same path-level as the
1995 .htaccess-file:
1996 </P>
1998 <pre>
1999 # Just enter your own directory path here
2000 $YOUR_HTML_FILES = "$ENV{'DOCUMENT_ROOT'}";
2001 # use DOCUMENT_ROOT only, if .htaccess lies in the root-directory.
2002 </Pre>
2005 If this .htaccess goes in a specific directory, the path to this
2006 directory must be added to $ENV{'DOCUMENT_ROOT'}.
2007 </p>
2010 The CGIscriptor file contains all documentation as comments. These comments
2011 can be removed to speed up loading (e.g., `egrep -v '^#' CGIscriptor.pl` >
2012 leanScriptor.pl). A bare bones version of CGIscriptor.pl, lacking
2013 documentation, most comments, access control, example functions etc.
2014 (but still with the copyright notice and some minimal documentation)
2015 can be obtained by calling CGIscriptor.pl on the command line with the
2016 '-slim' command line argument, e.g.,
2017 </p>
2019 <PRE>
2020 &gt;CGIscriptor.pl -slim &gt; slimCGIscriptor.pl
2021 </PRE>
2024 CGIscriptor.pl can be run from the command line with &lt;path&gt; and &lt;query&gt; as
2025 arguments, as `CGIscriptor.pl &lt;path&gt; &lt;query&gt;`, inside a perl script with
2026 'do CGIscriptor.pl' after setting $ENV{PATH_INFO} and $ENV{QUERY_STRING},
2027 or CGIscriptor.pl can be loaded with 'require "/real-path/CGIscriptor.pl"'.
2028 In the latter case, requests are processed by 'Handle_Request();'
2029 (again after setting $ENV{PATH_INFO} and $ENV{QUERY_STRING}).
2030 </P>
2033 The --help command line switch will print the manual.
2034 </p>
2037 Using the command line execution option, CGIscriptor.pl can be used as a document
2038 (meta-)preprocessor. If the first argument is '-', STDIN will be read. For example:
2039 </P>
2041 <PRE>
2042 &gt; cat MyDynamicDocument.html | CGIscriptor.pl - '[QueryString]' &gt; MyStaticFile.html
2043 </PRE>
2046 This command line will produce a STATIC file with the DYNAMIC content of
2047 MyDocument.html "interpolated". This option would be very dangerous when
2048 available over the internet. If someone could sneak a
2049 'http://www.your.domain/-' URL past your server, CGIscriptor could EXECUTE
2050 any POSTED contend. Therefore, for security reasons, STDIN will NOT
2051 be read if ANY of the HTTP server environment variables is set (e.g., SERVER_PORT,
2052 SERVER_PROTOCOL, SERVER_NAME, SERVER_SOFTWARE, HTTP_USER_AGENT,
2053 REMOTE_ADDR).<br>
2054 This block on processing STDIN on HTTP requests can be lifted by setting
2055 <pre>
2056 $BLOCK_STDIN_HTTP_REQUEST = 0;
2057 </pre>
2058 In the security configuration. But be carefull when doing this.
2059 It can be very dangerous.
2060 </P>
2063 Running demo's and more information can be found at
2064 http://www.fon.hum.uva.nl/~rob/OSS/OSS.html
2065 </P>
2068 A pocket-size HTTP daemon, CGIservlet.pl, is available from my web site
2069 or CPAN that can use CGIscriptor.pl as the base of a µWWW server and
2070 demonstrates its use.
2071 </P>
2073 <A NAME="NON-UNIX"><H2 ALIGN="CENTER">NON-UNIX PLATFORMS</H2></A>
2076 CGIscriptor.pl was mainly developed and tested on UNIX. However, as I
2077 coded part of the time on an Apple Macintosh under MacPerl, I made sure
2078 CGIscriptor did run under MacPerl (with command line options). But only as
2079 an independend script, not as part of a HTTP server. I have used it
2080 under Apache in Windows XP.
2081 </P>
2083 <A NAME="license"><H2 ALIGN="CENTER">license</H2></A>
2086 This program is free software; you can redistribute it and/or
2087 modify it under the terms of the GNU General Public License
2088 as published by the Free Software Foundation; either version 2
2089 of the License, or (at your option) any later version.
2090 </P>
2093 This program is distributed in the hope that it will be useful,
2094 but WITHOUT ANY WARRANTY; without even the implied warranty of
2095 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2096 GNU General Public License for more details.
2097 </P>
2100 You should have received a copy of the GNU General Public License
2101 along with this program; if not, write to the Free Software
2102 Foundation, Inc., 59 Temple Place - Suite 330,
2103 Boston, MA 02111-1307, USA.
2104 </P>
2106 <PRE>
2107 Author: Rob van Son
2108 email:
2109 R.J.J.H.vanSon@uva.nl
2110 University of Amsterdam
2112 Date: May 22, 2000
2113 Ver: 2.0
2114 Env: Perl 5.002
2115 </PRE>
2116 </BODY>
2118 </HTML>