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