Increase timeout for subsequent RPC calls
[webmin.git] / fastrpc.cgi
1 #!/usr/local/bin/perl
2 # Handles remote_* function calls by a faster method. When first called
3 # as a CGI, forks and starts listening on a port which is returned to the
4 # client. From then on, direct TCP connections can be made to this port
5 # to send requests and get replies.
6
7 do './web-lib.pl';
8 use POSIX;
9 use Socket;
10 $force_lang = $default_lang;
11 &init_config();
12 print "Content-type: text/plain\n\n";
13
14 # Can this user make remote calls?
15 %access = &get_module_acl();
16 if ($access{'rpc'} == 0 || $access{'rpc'} == 2 &&
17     $base_remote_user ne 'admin' && $base_remote_user ne 'root' &&
18     $base_remote_user ne 'sysadm') {
19         print "0 Invalid user for RPC\n";
20         exit;
21         }
22
23 # Find a free port
24 &get_miniserv_config(\%miniserv);
25 $port = $miniserv{'port'} || 10000;
26 $aerr = &allocate_socket(MAIN, \$port);
27 if ($aerr) {
28         print "0 $aerr\n";
29         exit;
30         }
31 if (open(RANDOM, "/dev/urandom")) {
32         local $tmpsid;
33         read(RANDOM, $tmpsid, 16);
34         $sid = lc(unpack('h*', $tmpsid));
35         close RANDOM;
36         }
37 else {
38         $sid = time()*$$;
39         }
40 $version = &get_webmin_version();
41 print "1 $port $sid $version\n";
42
43 # Fork and listen for calls ..
44 $pid = fork();
45 if ($pid < 0) {
46         die "fork() failed : $!";
47         }
48 elsif ($pid) {
49         exit;
50         }
51 untie(*STDIN);
52 untie(*STDOUT);
53
54 # Accept the TCP connection
55 $acptaddr = accept(SOCK, MAIN);
56 die "accept failed!" if (!$acptaddr);
57 select(SOCK); $| = 1;
58
59 $rcount = 0;
60 while(1) {
61         # Wait for the request. Wait longer if this isn't the first one
62         local $rmask;
63         vec($rmask, fileno(SOCK), 1) = 1;
64         local $sel = select($rmask, undef, undef, $rcount ? 360 : 60);
65         if ($sel <= 0) {
66                 print STDERR "fastrpc: session timed out\n"
67                         if ($gconfig{'rpcdebug'});
68                 last;
69                 }
70
71         local $line = <SOCK>;
72         last if (!$line);
73         local ($len, $auth) = split(/\s+/, $line);
74         die "Invalid session ID" if ($auth ne $sid);
75         local $rawarg;
76         while(length($rawarg) < $len) {
77                 local $got;
78                 local $rv = read(SOCK, $got, $len - length($rawarg));
79                 exit if ($rv <= 0);
80                 $rawarg .= $got;
81                 }
82         print STDERR "fastrpc: raw $rawarg\n" if ($gconfig{'rpcdebug'});
83         local $arg = &unserialise_variable($rawarg);
84
85         # Process it
86         local $rawrv;
87         if ($arg->{'action'} eq 'ping') {
88                 # Just respond with an OK
89                 print STDERR "fastrpc: ping\n" if ($gconfig{'rpcdebug'});
90                 $rawrv = &serialise_variable( { 'status' => 1 } );
91                 }
92         elsif ($arg->{'action'} eq 'check') {
93                 # Check if some module is supported
94                 print STDERR "fastrpc: check $arg->{'module'}\n" if ($gconfig{'rpcdebug'});
95                 $rawrv = &serialise_variable(
96                         { 'status' => 1,
97                           'rv' => &foreign_check($arg->{'module'}, undef, undef,
98                                                  $arg->{'api'}) } );
99                 }
100         elsif ($arg->{'action'} eq 'config') {
101                 # Get the config for some module
102                 print STDERR "fastrpc: config $arg->{'module'}\n" if ($gconfig{'rpcdebug'});
103                 local %config = &foreign_config($arg->{'module'});
104                 $rawrv = &serialise_variable(
105                         { 'status' => 1, 'rv' => \%config } );
106                 }
107         elsif ($arg->{'action'} eq 'write') {
108                 # Transfer data to a local temp file
109                 local $file = $arg->{'file'} ? $arg->{'file'} :
110                               $arg->{'name'} ? &tempname($arg->{'name'}) :
111                                                &tempname();
112                 print STDERR "fastrpc: write $file\n" if ($gconfig{'rpcdebug'});
113                 open(FILE, ">$file");
114                 binmode(FILE);
115                 print FILE $arg->{'data'};
116                 close(FILE);
117                 $rawrv = &serialise_variable(
118                         { 'status' => 1, 'rv' => $file } );
119                 }
120         elsif ($arg->{'action'} eq 'tcpwrite') {
121                 # Transfer data to a local temp file over TCP connection
122                 local $file = $arg->{'file'} ? $arg->{'file'} :
123                               $arg->{'name'} ? &tempname($arg->{'name'}) :
124                                                &tempname();
125                 print STDERR "fastrpc: tcpwrite $file\n" if ($gconfig{'rpcdebug'});
126                 local $tsock = time().$$;
127                 local $tport = $port + 1;
128                 &allocate_socket($tsock, \$tport);
129                 if (!fork()) {
130                         # Accept connection in separate process
131                         print STDERR "fastrpc: tcpwrite $file port $tport\n" if ($gconfig{'rpcdebug'});
132                         local $rmask;
133                         vec($rmask, fileno($tsock), 1) = 1;
134                         local $sel = select($rmask, undef, undef, 30);
135                         exit if ($sel <= 0);
136                         accept(TRANS, $tsock) || exit;
137                         print STDERR "fastrpc: tcpwrite $file accepted\n" if ($gconfig{'rpcdebug'});
138                         local $buf;
139                         local $err;
140                         if (open(FILE, ">$file")) {
141                                 binmode(FILE);
142                                 print STDERR "fastrpc: tcpwrite $file writing\n" if ($gconfig{'rpcdebug'});
143                                 while(read(TRANS, $buf, 1024) > 0) {
144                                         local $ok = (print FILE $buf);
145                                         if (!$ok) {
146                                                 $err = "Write to $file failed : $!";
147                                                 last;
148                                                 }
149                                         }
150                                 close(FILE);
151                                 print STDERR "fastrpc: tcpwrite $file written\n" if ($gconfig{'rpcdebug'});
152                                 }
153                         else {
154                                 print STDERR "fastrpc: tcpwrite $file open failed $!\n" if ($gconfig{'rpcdebug'});
155                                 $err = "Failed to open $file : $!";
156                                 }
157                         print TRANS $err ? "$err\n" : "OK\n";
158                         close(TRANS);
159                         exit;
160                         }
161                 close($tsock);
162                 print STDERR "fastrpc: tcpwrite $file done\n" if ($gconfig{'rpcdebug'});
163                 $rawrv = &serialise_variable(
164                         { 'status' => 1, 'rv' => [ $file, $tport ] } );
165                 }
166         elsif ($arg->{'action'} eq 'read') {
167                 # Transfer data from a file
168                 print STDERR "fastrpc: read $arg->{'file'}\n" if ($gconfig{'rpcdebug'});
169                 local ($data, $got);
170                 open(FILE, $arg->{'file'});
171                 binmode(FILE);
172                 while(read(FILE, $got, 1024) > 0) {
173                         $data .= $got;
174                         }
175                 close(FILE);
176                 $rawrv = &serialise_variable(
177                         { 'status' => 1, 'rv' => $data } );
178                 }
179         elsif ($arg->{'action'} eq 'tcpread') {
180                 # Transfer data from a file over TCP connection
181                 print STDERR "fastrpc: tcpread $arg->{'file'}\n" if ($gconfig{'rpcdebug'});
182                 if (!open(FILE, $arg->{'file'})) {
183                         $rawrv = &serialise_variable(
184                                 { 'status' => 1, 'rv' => [ undef, "Failed to open $arg->{'file'} : $!" ] } );
185                         }
186                 else {
187                         binmode(FILE);
188                         local $tsock = time().$$;
189                         local $tport = $port + 1;
190                         &allocate_socket($tsock, \$tport);
191                         if (!fork()) {
192                                 # Accept connection in separate process
193                                 local $rmask;
194                                 vec($rmask, fileno($tsock), 1) = 1;
195                                 local $sel = select($rmask, undef, undef, 30);
196                                 exit if ($sel <= 0);
197                                 accept(TRANS, $tsock) || exit;
198                                 local $buf;
199                                 while(read(FILE, $buf, 1024) > 0) {
200                                         print TRANS $buf;
201                                         }
202                                 close(FILE);
203                                 close(TRANS);
204                                 exit;
205                                 }
206                         close(FILE);
207                         close($tsock);
208                         print STDERR "fastrpc: tcpread $arg->{'file'} done\n" if ($gconfig{'rpcdebug'});
209                         $rawrv = &serialise_variable(
210                                 { 'status' => 1, 'rv' => [ $arg->{'file'}, $tport ] } );
211                         }
212                 }
213         elsif ($arg->{'action'} eq 'require') {
214                 # require a library
215                 print STDERR "fastrpc: require $arg->{'module'}/$arg->{'file'}\n" if ($gconfig{'rpcdebug'});
216                 eval {
217                         &foreign_require($arg->{'module'},
218                                          $arg->{'file'});
219                         };
220                 if ($@) {
221                         print STDERR "fastrpc: require error $@\n" if ($gconfig{'rpcdebug'});
222                         $rawrv = &serialise_variable( { 'status' => 0,
223                                                         'rv' => $@ });
224                         }
225                 else {
226                         print STDERR "fastrpc: require done\n" if ($gconfig{'rpcdebug'});
227                         $rawrv = &serialise_variable( { 'status' => 1 });
228                         }
229                 }
230         elsif ($arg->{'action'} eq 'call') {
231                 # execute a function
232                 print STDERR "fastrpc: call $arg->{'module'}::$arg->{'func'}(",join(",", @{$arg->{'args'}}),")\n" if ($gconfig{'rpcdebug'});
233                 local @rv;
234                 local $main::error_must_die = 1;
235                 eval {
236                         @rv = &foreign_call($arg->{'module'},
237                                             $arg->{'func'},
238                                             @{$arg->{'args'}});
239                         };
240                 if ($@) {
241                         print STDERR "fastrpc: call error $@\n" if ($gconfig{'rpcdebug'});
242                         $rawrv = &serialise_variable(
243                                 { 'status' => 0, 'rv' => $@ } );
244                         }
245                 elsif (@rv == 1) {
246                         $rawrv = &serialise_variable(
247                                 { 'status' => 1, 'rv' => $rv[0] } );
248                         }
249                 else {
250                         $rawrv = &serialise_variable(
251                                 { 'status' => 1, 'arv' => \@rv } );
252                         }
253                 print STDERR "fastrpc: call $arg->{'module'}::$arg->{'func'} done = ",join(",", @rv),"\n" if ($gconfig{'rpcdebug'});
254                 }
255         elsif ($arg->{'action'} eq 'eval') {
256                 # eval some perl code
257                 print STDERR "fastrpc: eval $arg->{'module'} $arg->{'code'}\n" if ($gconfig{'rpcdebug'});
258                 local $rv;
259                 if ($arg->{'module'}) {
260                         local $pkg = $arg->{'module'};
261                         $pkg =~ s/[^A-Za-z0-9]/_/g;
262                         $rv = eval "package $pkg;\n".
263                                    $arg->{'code'}."\n";
264                         }
265                 else {
266                         $rv = eval $arg->{'code'};
267                         }
268                 print STDERR "fastrpc: eval $arg->{'module'} $arg->{'code'} done = $rv error = $@\n" if ($gconfig{'rpcdebug'});
269                 $rawrv = &serialise_variable(
270                         { 'status' => 1, 'rv' => $rv } );
271                 }
272         elsif ($arg->{'action'} eq 'quit') {
273                 print STDERR "fastrpc: quit\n" if ($gconfig{'rpcdebug'});
274                 $rawrv = &serialise_variable( { 'status' => 1 } );
275                 }
276         else {
277                 print STDERR "fastrpc: unknown $arg->{'action'}\n" if ($gconfig{'rpcdebug'});
278                 $rawrv = &serialise_variable( { 'status' => 0 } );
279                 }
280
281         # Send back to the client
282         print SOCK length($rawrv),"\n";
283         print SOCK $rawrv;
284         last if ($arg->{'action'} eq 'quit');
285         $rcount++;
286         }
287
288 # allocate_socket(handle, &port)
289 sub allocate_socket
290 {
291 local ($fh, $port) = @_;
292 local $proto = getprotobyname('tcp');
293 if (!socket($fh, PF_INET, SOCK_STREAM, $proto)) {
294         return "socket failed : $!";
295         }
296 setsockopt($fh, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
297 while(1) {
298         $$port++;
299         last if (bind($fh, sockaddr_in($$port, INADDR_ANY)));
300         }
301 listen($fh, SOMAXCONN);
302 return undef;
303 }
304