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.
10 $force_lang = $default_lang;
12 print "Content-type: text/plain\n\n";
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";
24 &get_miniserv_config(\%miniserv);
25 $port = $miniserv{'port'} || 10000;
26 $aerr = &allocate_socket(MAIN, \$port);
31 if (open(RANDOM, "/dev/urandom")) {
33 read(RANDOM, $tmpsid, 16);
34 $sid = lc(unpack('h*', $tmpsid));
40 $version = &get_webmin_version();
41 print "1 $port $sid $version\n";
43 # Fork and listen for calls ..
46 die "fork() failed : $!";
54 # Accept the TCP connection
55 $acptaddr = accept(SOCK, MAIN);
56 die "accept failed!" if (!$acptaddr);
60 # Wait for the request
62 vec($rmask, fileno(SOCK), 1) = 1;
63 local $sel = select($rmask, undef, undef, 60);
65 print STDERR "fastrpc: session timed out\n"
66 if ($gconfig{'rpcdebug'});
72 local ($len, $auth) = split(/\s+/, $line);
73 die "Invalid session ID" if ($auth ne $sid);
75 while(length($rawarg) < $len) {
77 local $rv = read(SOCK, $got, $len - length($rawarg));
81 print STDERR "fastrpc: raw $rawarg\n" if ($gconfig{'rpcdebug'});
82 local $arg = &unserialise_variable($rawarg);
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 } );
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(
96 'rv' => &foreign_check($arg->{'module'}, undef, undef,
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 } );
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'}) :
111 print STDERR "fastrpc: write $file\n" if ($gconfig{'rpcdebug'});
112 open(FILE, ">$file");
114 print FILE $arg->{'data'};
116 $rawrv = &serialise_variable(
117 { 'status' => 1, 'rv' => $file } );
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'}) :
124 print STDERR "fastrpc: tcpwrite $file\n" if ($gconfig{'rpcdebug'});
125 local $tsock = time().$$;
126 local $tport = $port + 1;
127 &allocate_socket($tsock, \$tport);
129 # Accept connection in separate process
130 print STDERR "fastrpc: tcpwrite $file port $tport\n" if ($gconfig{'rpcdebug'});
132 vec($rmask, fileno($tsock), 1) = 1;
133 local $sel = select($rmask, undef, undef, 30);
135 accept(TRANS, $tsock) || exit;
136 print STDERR "fastrpc: tcpwrite $file accepted\n" if ($gconfig{'rpcdebug'});
139 if (open(FILE, ">$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);
145 $err = "Write to $file failed : $!";
150 print STDERR "fastrpc: tcpwrite $file written\n" if ($gconfig{'rpcdebug'});
153 print STDERR "fastrpc: tcpwrite $file open failed $!\n" if ($gconfig{'rpcdebug'});
154 $err = "Failed to open $file : $!";
156 print TRANS $err ? "$err\n" : "OK\n";
161 print STDERR "fastrpc: tcpwrite $file done\n" if ($gconfig{'rpcdebug'});
162 $rawrv = &serialise_variable(
163 { 'status' => 1, 'rv' => [ $file, $tport ] } );
165 elsif ($arg->{'action'} eq 'read') {
166 # Transfer data from a file
167 print STDERR "fastrpc: read $arg->{'file'}\n" if ($gconfig{'rpcdebug'});
169 open(FILE, $arg->{'file'});
171 while(read(FILE, $got, 1024) > 0) {
175 $rawrv = &serialise_variable(
176 { 'status' => 1, 'rv' => $data } );
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'} : $!" ] } );
187 local $tsock = time().$$;
188 local $tport = $port + 1;
189 &allocate_socket($tsock, \$tport);
191 # Accept connection in separate process
193 vec($rmask, fileno($tsock), 1) = 1;
194 local $sel = select($rmask, undef, undef, 30);
196 accept(TRANS, $tsock) || exit;
198 while(read(FILE, $buf, 1024) > 0) {
207 print STDERR "fastrpc: tcpread $arg->{'file'} done\n" if ($gconfig{'rpcdebug'});
208 $rawrv = &serialise_variable(
209 { 'status' => 1, 'rv' => [ $arg->{'file'}, $tport ] } );
212 elsif ($arg->{'action'} eq 'require') {
214 print STDERR "fastrpc: require $arg->{'module'}/$arg->{'file'}\n" if ($gconfig{'rpcdebug'});
216 &foreign_require($arg->{'module'},
220 print STDERR "fastrpc: require error $@\n" if ($gconfig{'rpcdebug'});
221 $rawrv = &serialise_variable( { 'status' => 0,
225 print STDERR "fastrpc: require done\n" if ($gconfig{'rpcdebug'});
226 $rawrv = &serialise_variable( { 'status' => 1 });
229 elsif ($arg->{'action'} eq 'call') {
231 print STDERR "fastrpc: call $arg->{'module'}::$arg->{'func'}(",join(",", @{$arg->{'args'}}),")\n" if ($gconfig{'rpcdebug'});
233 local $main::error_must_die = 1;
235 @rv = &foreign_call($arg->{'module'},
240 print STDERR "fastrpc: call error $@\n" if ($gconfig{'rpcdebug'});
241 $rawrv = &serialise_variable(
242 { 'status' => 0, 'rv' => $@ } );
245 $rawrv = &serialise_variable(
246 { 'status' => 1, 'rv' => $rv[0] } );
249 $rawrv = &serialise_variable(
250 { 'status' => 1, 'arv' => \@rv } );
252 print STDERR "fastrpc: call $arg->{'module'}::$arg->{'func'} done = ",join(",", @rv),"\n" if ($gconfig{'rpcdebug'});
254 elsif ($arg->{'action'} eq 'eval') {
255 # eval some perl code
256 print STDERR "fastrpc: eval $arg->{'module'} $arg->{'code'}\n" if ($gconfig{'rpcdebug'});
258 if ($arg->{'module'}) {
259 local $pkg = $arg->{'module'};
260 $pkg =~ s/[^A-Za-z0-9]/_/g;
261 $rv = eval "package $pkg;\n".
265 $rv = eval $arg->{'code'};
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 } );
271 elsif ($arg->{'action'} eq 'quit') {
272 print STDERR "fastrpc: quit\n" if ($gconfig{'rpcdebug'});
273 $rawrv = &serialise_variable( { 'status' => 1 } );
276 print STDERR "fastrpc: unknown $arg->{'action'}\n" if ($gconfig{'rpcdebug'});
277 $rawrv = &serialise_variable( { 'status' => 0 } );
280 # Send back to the client
281 print SOCK length($rawrv),"\n";
283 last if ($arg->{'action'} eq 'quit');
286 # allocate_socket(handle, &port)
289 local ($fh, $port) = @_;
290 local $proto = getprotobyname('tcp');
291 if (!socket($fh, PF_INET, SOCK_STREAM, $proto)) {
292 return "socket failed : $!";
294 setsockopt($fh, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
297 last if (bind($fh, sockaddr_in($$port, INADDR_ANY)));
299 listen($fh, SOMAXCONN);