Sharedwww / cgi-bin / openwebmail / preload.plOpen in CoCalc
Author: William A. Stein
1
#!/usr/bin/perl
2
#
3
# preload.pl - simple http client to active openwebmail service on web server
4
#
5
# 2003/03/24 tung.AT.turtle.ee.ncku.edu.tw
6
#
7
# This script can work as a CGI or command line tool.
8
# It is used to preload openwebmail scripts in persistent mode,
9
# so the user won't encounter the script startup delay.
10
#
11
use strict;
12
foreach (qw(ENV BASH_ENV CDPATH IFS TERM)) {delete $ENV{$_}}; $ENV{PATH}='/bin:/usr/bin'; # secure ENV
13
14
# encrypted password for cgi access
15
#
16
# To have a new encrypted passwd for this:
17
# 1. htpasswd -c /tmp/test guest
18
# 2. enter password
19
# 3. copy the encrypted field in /tmp/test to this
20
#
21
my $cgipwd="MW7y7p8tQsXpU"; # pwd=speedycgi, CHNANGE IT AS SOON AS POSSIBLE
22
23
# where the web server is
24
my $httphost="127.0.0.1";
25
my $httpport="80";
26
27
# url prefix of openwebmail scripts,
28
# it should be the same as option ow_cgiurl in openwebmail.conf
29
my $cgiurl="/cgi-bin/openwebmail";
30
31
########## No configuration required from here ###################
32
33
use Socket;
34
use IO::Socket;
35
36
# all openwebmail scripts to preload,
37
# used in cgi mode or if --all is specified in command mode
38
my @scripts=(
39
'openwebmail.pl',
40
'openwebmail-main.pl',
41
'openwebmail-prefs.pl',
42
'openwebmail-read.pl',
43
'openwebmail-viewatt.pl',
44
'openwebmail-send.pl',
45
'openwebmail-abook.pl',
46
'openwebmail-cal.pl',
47
'openwebmail-webdisk.pl',
48
'openwebmail-folder.pl',
49
'openwebmail-spell.pl',
50
'openwebmail-advsearch.pl',
51
);
52
53
# -q option set this to 1, then no output, useful for cronatb
54
my $quiet=0;
55
56
########## MAIN ##################################################
57
my %param=ReadParse();
58
59
if (defined($ENV{'GATEWAY_INTERFACE'})) { # CGI mode
60
local $|=1;
61
print qq|Content-type: text/html\n\n|.
62
qq|<html><body>\n|.
63
qq|<h2>Open WebMail Preload Page</h2>\n|;
64
if (defined($param{'password'}) &&
65
crypt($param{'password'},$cgipwd) eq $cgipwd){
66
print qq|<pre>\n|;
67
preload($quiet, $httphost, $httpport, $cgiurl, @scripts);
68
print qq|</pre>\n|;
69
print qq|<form method="post" action="/cgi-bin/openwebmail/preload.pl" enctype="application/x-www-form-urlencoded">\n|.
70
qq|<input type="submit" name=" Clear " value=" Clear " />\n|.
71
qq|</form>\n|;
72
} else {
73
sleep 8 if (defined($param{'password'}));
74
print qq|<form method="post" action="/cgi-bin/openwebmail/preload.pl" enctype="application/x-www-form-urlencoded">\n|.
75
qq|Access Password : \n|.
76
qq|<input type="password" name="password" size="16" />\n|.
77
qq|<br><br>\n|.
78
qq|<input type="submit" name=" Submit " value=" Submit " />\n|.
79
qq|</form>\n|;
80
}
81
print qq|<a href='/cgi-bin/openwebmail/openwebmail.pl'>Login Open WebMail</a>\n|.
82
qq|</body></html>\n|;
83
84
} else { # cmd mode
85
my @preloadscripts;
86
foreach (@ARGV) {
87
if (/^\-q/ || /^\-\-quiet/) {
88
$quiet=1;
89
} elsif (/^--all/) {
90
@preloadscripts=@scripts;
91
} elsif (/^openwebmail.+pl$/) {
92
push(@preloadscripts, $_);
93
}
94
}
95
if ($#preloadscripts>=0) {
96
exit preload($quiet, $httphost, $httpport, $cgiurl, @preloadscripts);
97
} else {
98
print "Syntax: preload.pl [-q] [--all]\n",
99
" preload.pl [-q] openwebmail_scriptnames...\n";
100
exit 1;
101
}
102
}
103
104
########## ROUTINES ##############################################
105
sub preload {
106
my ($quiet, $httphost, $httpport, $cgiurl, @scripts)=@_;
107
108
foreach my $script (@scripts) {
109
my $result='';
110
print "Loading $script..." if (!$quiet);
111
112
my $remote_sock=new IO::Socket::INET(Proto=>'tcp',
113
PeerAddr=>$httphost,
114
PeerPort=>$httpport);
115
if (! $remote_sock ) {
116
print "connect error!\n" if (!$quiet);
117
return -1;
118
}
119
$remote_sock->autoflush(1);
120
121
print $remote_sock "GET $cgiurl/$script HTTP/1.0\n\n";
122
while (<$remote_sock>) {
123
s/[\s\t]+$//;
124
$result .= "$_\n";
125
}
126
close($remote_sock);
127
128
print "done.\n" if (!$quiet);
129
}
130
return 0;
131
}
132
133
# routine from netjack.pm at http://www.the42.net/jack
134
# by PJ Goodwin <pj_at_the42.net>
135
sub ReadParse {
136
my (%param, $string);
137
138
if ($ENV{'REQUEST_METHOD'} eq "GET") {
139
$string = $ENV{'QUERY_STRING'};
140
} elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
141
read(STDIN, $string, $ENV{'CONTENT_LENGTH'});
142
} else {
143
$string = $ARGV[0];
144
}
145
$string =~ s/\+/ /g; # conv + to spaces
146
$string =~ s/%(..)/pack("c", hex($1))/ge;
147
148
foreach (split(/&/, $string)) {
149
s|[^\-a-zA-Z0-9_\.@=/+\/\,\(\)!\s]|_|g; # rm bad char
150
my ($key, $val)=split(/=/, $_, 2); # split into key and value.
151
$param{$key} .= '\0' if (defined($param{$key})); # \0 is multiple separator
152
$param{$key} .= $val;
153
}
154
return %param;
155
}
156