Committed missed files
[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 while(1) {
60         # Wait for the request
61         local $rmask;
62         vec($rmask, fileno(SOCK), 1) = 1;
63         local $sel = select($rmask, undef, undef, 60);
64         if ($sel <= 0) {
65                 print STDERR "fastrpc: session timed out\n"
66                         if ($gconfig{'rpcdebug'});
67                 last;
68                 }
69
70         local $line = <SOCK>;
71         last if (!$line);
72         local ($len, $auth) = split(/\s+/, $line);
73         die "Invalid session ID" if ($auth ne $sid);
74         local $rawarg;
75         while(length($rawarg) < $len) {
76                 local $got;
77                 local $rv = read(SOCK, $got, $len - length($rawarg));
78                 exit if ($rv <= 0);
79                 $rawarg .= $got;
80                 }
81         print STDERR "fastrpc: raw $rawarg\n" if ($gconfig{'rpcdebug'});
82         local $arg = &unserialise_variable($rawarg);
83
84         # Process it
85         local $rawrv;
86         if ($arg->{'action'} eq 'ping') {
87                 # Just respond with an OK
88                 print STDERR "fastrpc: ping\n" if ($gconfig{'rpcdebug'});
89                 $rawrv = &serialise_variable( { 'status' => 1 } );
90                 }
91         elsif ($arg->{'action'} eq 'check') {
92                 # Check if some module is supported
93                 print STDERR "fastrpc: check $arg->{'module'}\n" if ($gconfig{'rpcdebug'});
94                 $rawrv = &serialise_variable(
95                         { 'status' => 1,
96                           'rv' => &foreign_check($arg->{'module'}) } );
97                 }
98         elsif ($arg->{'action'} eq 'config') {
99                 # Get the config for some module
100                 print STDERR "fastrpc: config $arg->{'module'}\n" if ($gconfig{'rpcdebug'});
101                 local %config = &foreign_config($arg->{'module'});
102                 $rawrv = &serialise_variable(
103                         { 'status' => 1, 'rv' => \%config } );
104                 }
105         elsif ($arg->{'action'} eq 'write') {
106                 # Transfer data to a local temp file
107                 local $file = $arg->{'file'} ? $arg->{'file'} :
108                               $arg->{'name'} ? &tempname($arg->{'name'}) :
109                                                &tempname();
110                 print STDERR "fastrpc: write $file\n" if ($gconfig{'rpcdebug'});
111                 open(FILE, ">$file");
112                 binmode(FILE);
113                 print FILE $arg->{'data'};
114                 close(FILE);
115                 $rawrv = &serialise_variable(
116                         { 'status' => 1, 'rv' => $file } );
117                 }
118         elsif ($arg->{'action'} eq 'tcpwrite') {
119                 # Transfer data to a local temp file over TCP connection
120                 local $file = $arg->{'file'} ? $arg->{'file'} :
121                               $arg->{'name'} ? &tempname($arg->{'name'}) :
122                                                &tempname();
123                 print STDERR "fastrpc: tcpwrite $file\n" if ($gconfig{'rpcdebug'});
124                 local $tsock = time().$$;
125                 local $tport = $port + 1;
126                 &allocate_socket($tsock, \$tport);
127                 if (!fork()) {
128                         # Accept connection in separate process
129                         print STDERR "fastrpc: tcpwrite $file port $tport\n" if ($gconfig{'rpcdebug'});
130                         local $rmask;
131                         vec($rmask, fileno($tsock), 1) = 1;
132                         local $sel = select($rmask, undef, undef, 30);
133                         exit if ($sel <= 0);
134                         accept(TRANS, $tsock) || exit;
135                         print STDERR "fastrpc: tcpwrite $file accepted\n" if ($gconfig{'rpcdebug'});
136                         local $buf;
137                         local $err;
138                         if (open(FILE, ">$file")) {
139                                 binmode(FILE);
140                                 print STDERR "fastrpc: tcpwrite $file writing\n" if ($gconfig{'rpcdebug'});
141                                 while(read(TRANS, $buf, 1024) > 0) {
142                                         local $ok = (print FILE $buf);
143                                         if (!$ok) {
144                                                 $err = "Write to $file failed : $!";
145                                                 last;
146                                                 }
147                                         }
148                                 close(FILE);
149                                 print STDERR "fastrpc: tcpwrite $file written\n" if ($gconfig{'rpcdebug'});
150                                 }
151                         else {
152                                 print STDERR "fastrpc: tcpwrite $file open failed $!\n" if ($gconfig{'rpcdebug'});
153                                 $err = "Failed to open $file : $!";
154                                 }
155                         print TRANS $err ? "$err\n" : "OK\n";
156                         close(TRANS);
157                         exit;
158                         }
159                 close($tsock);
160                 print STDERR "fastrpc: tcpwrite $file done\n" if ($gconfig{'rpcdebug'});
161                 $rawrv = &serialise_variable(
162                         { 'status' => 1, 'rv' => [ $file, $tport ] } );
163                 }
164         elsif ($arg->{'action'} eq 'read') {
165                 # Transfer data from a file
166                 print STDERR "fastrpc: read $arg->{'file'}\n" if ($gconfig{'rpcdebug'});
167                 local ($data, $got);
168                 open(FILE, $arg->{'file'});
169                 binmode(FILE);
170                 while(read(FILE, $got, 1024) > 0) {
171                         $data .= $got;
172                         }
173                 close(FILE);
174                 $rawrv = &serialise_variable(
175                         { 'status' => 1, 'rv' => $data } );
176                 }
177         elsif ($arg->{'action'} eq 'tcpread') {
178                 # Transfer data from a file over TCP connection
179                 print STDERR "fastrpc: tcpread $arg->{'file'}\n" if ($gconfig{'rpcdebug'});
180                 if (!open(FILE, $arg->{'file'})) {
181                         $rawrv = &serialise_variable(
182                                 { 'status' => 1, 'rv' => [ undef, "Failed to open $arg->{'file'} : $!" ] } );
183                         }
184                 else {
185                         binmode(FILE);
186                         local $tsock = time().$$;
187                         local $tport = $port + 1;
188                         &allocate_socket($tsock, \$tport);
189                         if (!fork()) {
190                                 # Accept connection in separate process
191                                 local $rmask;
192                                 vec($rmask, fileno($tsock), 1) = 1;
193                                 local $sel = select($rmask, undef, undef, 30);
194                                 exit if ($sel <= 0);
195                                 accept(TRANS, $tsock) || exit;
196                                 local $buf;
197                                 while(read(FILE, $buf, 1024) > 0) {
198                                         print TRANS $buf;
199                                         }
200                                 close(FILE);
201                                 close(TRANS);
202                                 exit;
203                                 }
204                         close(FILE);
205                         close($tsock);
206                         print STDERR "fastrpc: tcpread $arg->{'file'} done\n" if ($gconfig{'rpcdebug'});
207                         $rawrv = &serialise_variable(
208                                 { 'status' => 1, 'rv' => [ $arg->{'file'}, $tport ] } );
209                         }
210                 }
211         elsif ($arg->{'action'} eq 'require') {
212                 # require a library
213                 print STDERR "fastrpc: require $arg->{'module'}/$arg->{'file'}\n" if ($gconfig{'rpcdebug'});
214                 eval {
215                         &foreign_require($arg->{'module'},
216                                          $arg->{'file'});
217                         };
218                 if ($@) {
219                         print STDERR "fastrpc: require error $@\n" if ($gconfig{'rpcdebug'});
220                         $rawrv = &serialise_variable( { 'status' => 0,
221                                                         'rv' => $@ });
222                         }
223                 else {
224                         print STDERR "fastrpc: require done\n" if ($gconfig{'rpcdebug'});
225                         $rawrv = &serialise_variable( { 'status' => 1 });
226                         }
227                 }
228         elsif ($arg->{'action'} eq 'call') {
229                 # execute a function
230                 print STDERR "fastrpc: call $arg->{'module'}::$arg->{'func'}(",join(",", @{$arg->{'args'}}),")\n" if ($gconfig{'rpcdebug'});
231                 local @rv;
232                 local $main::error_must_die = 1;
233                 eval {
234                         @rv = &foreign_call($arg->{'module'},
235                                             $arg->{'func'},
236                                             @{$arg->{'args'}});
237                         };
238                 if ($@) {
239                         print STDERR "fastrpc: call error $@\n" if ($gconfig{'rpcdebug'});
240                         $rawrv = &serialise_variable(
241                                 { 'status' => 0, 'rv' => $@ } );
242                         }
243                 elsif (@rv == 1) {
244                         $rawrv = &serialise_variable(
245                                 { 'status' => 1, 'rv' => $rv[0] } );
246                         }
247                 else {
248                         $rawrv = &serialise_variable(
249                                 { 'status' => 1, 'arv' => \@rv } );
250                         }
251                 print STDERR "fastrpc: call $arg->{'module'}::$arg->{'func'} done = ",join(",", @rv),"\n" if ($gconfig{'rpcdebug'});
252                 }
253         elsif ($arg->{'action'} eq 'eval') {
254                 # eval some perl code
255                 print STDERR "fastrpc: eval $arg->{'module'} $arg->{'code'}\n" if ($gconfig{'rpcdebug'});
256                 local $rv;
257                 if ($arg->{'module'}) {
258                         local $pkg = $arg->{'module'};
259                         $pkg =~ s/[^A-Za-z0-9]/_/g;
260                         $rv = eval "package $pkg;\n".
261                                    $arg->{'code'}."\n";
262                         }
263                 else {
264                         $rv = eval $arg->{'code'};
265                         }
266                 print STDERR "fastrpc: eval $arg->{'module'} $arg->{'code'} done = $rv error = $@\n" if ($gconfig{'rpcdebug'});
267                 $rawrv = &serialise_variable(
268                         { 'status' => 1, 'rv' => $rv } );
269                 }
270         elsif ($arg->{'action'} eq 'quit') {
271                 print STDERR "fastrpc: quit\n" if ($gconfig{'rpcdebug'});
272                 $rawrv = &serialise_variable( { 'status' => 1 } );
273                 }
274         else {
275                 print STDERR "fastrpc: unknown $arg->{'action'}\n" if ($gconfig{'rpcdebug'});
276                 $rawrv = &serialise_variable( { 'status' => 0 } );
277                 }
278
279         # Send back to the client
280         print SOCK length($rawrv),"\n";
281         print SOCK $rawrv;
282         last if ($arg->{'action'} eq 'quit');
283         }
284
285 # allocate_socket(handle, &port)
286 sub allocate_socket
287 {
288 local ($fh, $port) = @_;
289 local $proto = getprotobyname('tcp');
290 if (!socket($fh, PF_INET, SOCK_STREAM, $proto)) {
291         return "socket failed : $!";
292         }
293 setsockopt($fh, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
294 while(1) {
295         $$port++;
296         last if (bind($fh, sockaddr_in($$port, INADDR_ANY)));
297         }
298 listen($fh, SOMAXCONN);
299 return undef;
300 }
301