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);
68 local ($len, $auth) = split(/\s+/, $line);
69 die "Invalid session ID" if ($auth ne $sid);
71 while(length($rawarg) < $len) {
73 local $rv = read(SOCK, $got, $len - length($rawarg));
77 print STDERR "fastrpc: raw $rawarg\n" if ($gconfig{'rpcdebug'});
78 local $arg = &unserialise_variable($rawarg);
82 if ($arg->{'action'} eq 'ping') {
83 # Just respond with an OK
84 print STDERR "fastrpc: ping\n" if ($gconfig{'rpcdebug'});
85 $rawrv = &serialise_variable( { 'status' => 1 } );
87 elsif ($arg->{'action'} eq 'check') {
88 # Check if some module is supported
89 print STDERR "fastrpc: check $arg->{'module'}\n" if ($gconfig{'rpcdebug'});
90 $rawrv = &serialise_variable(
92 'rv' => &foreign_check($arg->{'module'}) } );
94 elsif ($arg->{'action'} eq 'config') {
95 # Get the config for some module
96 print STDERR "fastrpc: config $arg->{'module'}\n" if ($gconfig{'rpcdebug'});
97 local %config = &foreign_config($arg->{'module'});
98 $rawrv = &serialise_variable(
99 { 'status' => 1, 'rv' => \%config } );
101 elsif ($arg->{'action'} eq 'write') {
102 # Transfer data to a local temp file
103 local $file = $arg->{'file'} ? $arg->{'file'} :
104 $arg->{'name'} ? &tempname($arg->{'name'}) :
106 print STDERR "fastrpc: write $file\n" if ($gconfig{'rpcdebug'});
107 open(FILE, ">$file");
109 print FILE $arg->{'data'};
111 $rawrv = &serialise_variable(
112 { 'status' => 1, 'rv' => $file } );
114 elsif ($arg->{'action'} eq 'tcpwrite') {
115 # Transfer data to a local temp file over TCP connection
116 local $file = $arg->{'file'} ? $arg->{'file'} :
117 $arg->{'name'} ? &tempname($arg->{'name'}) :
119 print STDERR "fastrpc: tcpwrite $file\n" if ($gconfig{'rpcdebug'});
120 local $tsock = time().$$;
121 local $tport = $port + 1;
122 &allocate_socket($tsock, \$tport);
124 # Accept connection in separate process
125 print STDERR "fastrpc: tcpwrite $file port $tport\n" if ($gconfig{'rpcdebug'});
127 vec($rmask, fileno($tsock), 1) = 1;
128 local $sel = select($rmask, undef, undef, 30);
130 accept(TRANS, $tsock) || exit;
131 print STDERR "fastrpc: tcpwrite $file accepted\n" if ($gconfig{'rpcdebug'});
134 if (open(FILE, ">$file")) {
136 print STDERR "fastrpc: tcpwrite $file writing\n" if ($gconfig{'rpcdebug'});
137 while(read(TRANS, $buf, 1024) > 0) {
138 local $ok = (print FILE $buf);
140 $err = "Write to $file failed : $!";
145 print STDERR "fastrpc: tcpwrite $file written\n" if ($gconfig{'rpcdebug'});
148 print STDERR "fastrpc: tcpwrite $file open failed $!\n" if ($gconfig{'rpcdebug'});
149 $err = "Failed to open $file : $!";
151 print TRANS $err ? "$err\n" : "OK\n";
156 print STDERR "fastrpc: tcpwrite $file done\n" if ($gconfig{'rpcdebug'});
157 $rawrv = &serialise_variable(
158 { 'status' => 1, 'rv' => [ $file, $tport ] } );
160 elsif ($arg->{'action'} eq 'read') {
161 # Transfer data from a file
162 print STDERR "fastrpc: read $arg->{'file'}\n" if ($gconfig{'rpcdebug'});
164 open(FILE, $arg->{'file'});
166 while(read(FILE, $got, 1024) > 0) {
170 $rawrv = &serialise_variable(
171 { 'status' => 1, 'rv' => $data } );
173 elsif ($arg->{'action'} eq 'tcpread') {
174 # Transfer data from a file over TCP connection
175 print STDERR "fastrpc: tcpread $arg->{'file'}\n" if ($gconfig{'rpcdebug'});
176 if (!open(FILE, $arg->{'file'})) {
177 $rawrv = &serialise_variable(
178 { 'status' => 1, 'rv' => [ undef, "Failed to open $arg->{'file'} : $!" ] } );
182 local $tsock = time().$$;
183 local $tport = $port + 1;
184 &allocate_socket($tsock, \$tport);
186 # Accept connection in separate process
188 vec($rmask, fileno($tsock), 1) = 1;
189 local $sel = select($rmask, undef, undef, 30);
191 accept(TRANS, $tsock) || exit;
193 while(read(FILE, $buf, 1024) > 0) {
202 print STDERR "fastrpc: tcpread $arg->{'file'} done\n" if ($gconfig{'rpcdebug'});
203 $rawrv = &serialise_variable(
204 { 'status' => 1, 'rv' => [ $arg->{'file'}, $tport ] } );
207 elsif ($arg->{'action'} eq 'require') {
209 print STDERR "fastrpc: require $arg->{'module'}/$arg->{'file'}\n" if ($gconfig{'rpcdebug'});
211 &foreign_require($arg->{'module'},
215 print STDERR "fastrpc: require error $@\n" if ($gconfig{'rpcdebug'});
216 $rawrv = &serialise_variable( { 'status' => 0,
220 print STDERR "fastrpc: require done\n" if ($gconfig{'rpcdebug'});
221 $rawrv = &serialise_variable( { 'status' => 1 });
224 elsif ($arg->{'action'} eq 'call') {
226 print STDERR "fastrpc: call $arg->{'module'}::$arg->{'func'}(",join(",", @{$arg->{'args'}}),")\n" if ($gconfig{'rpcdebug'});
229 @rv = &foreign_call($arg->{'module'},
234 print STDERR "fastrpc: call error $@\n" if ($gconfig{'rpcdebug'});
235 $rawrv = &serialise_variable(
236 { 'status' => 0, 'rv' => $@ } );
239 $rawrv = &serialise_variable(
240 { 'status' => 1, 'rv' => $rv[0] } );
243 $rawrv = &serialise_variable(
244 { 'status' => 1, 'arv' => \@rv } );
246 print STDERR "fastrpc: call $arg->{'module'}::$arg->{'func'} done = ",join(",", @rv),"\n" if ($gconfig{'rpcdebug'});
248 elsif ($arg->{'action'} eq 'eval') {
249 # eval some perl code
250 print STDERR "fastrpc: eval $arg->{'module'} $arg->{'code'}\n" if ($gconfig{'rpcdebug'});
252 if ($arg->{'module'}) {
253 local $pkg = $arg->{'module'};
254 $pkg =~ s/[^A-Za-z0-9]/_/g;
255 $rv = eval "package $pkg;\n".
259 $rv = eval $arg->{'code'};
261 print STDERR "fastrpc: eval $arg->{'module'} $arg->{'code'} done = $rv error = $@\n" if ($gconfig{'rpcdebug'});
262 $rawrv = &serialise_variable(
263 { 'status' => 1, 'rv' => $rv } );
265 elsif ($arg->{'action'} eq 'quit') {
266 print STDERR "fastrpc: quit\n" if ($gconfig{'rpcdebug'});
267 $rawrv = &serialise_variable( { 'status' => 1 } );
270 print STDERR "fastrpc: unknown $arg->{'action'}\n" if ($gconfig{'rpcdebug'});
271 $rawrv = &serialise_variable( { 'status' => 0 } );
274 # Send back to the client
275 print SOCK length($rawrv),"\n";
277 last if ($arg->{'action'} eq 'quit');
280 # allocate_socket(handle, &port)
283 local ($fh, $port) = @_;
284 local $proto = getprotobyname('tcp');
285 if (!socket($fh, PF_INET, SOCK_STREAM, $proto)) {
286 return "socket failed : $!";
288 setsockopt($fh, SOL_SOCKET, SO_REUSEADDR, pack("l", 1));
291 last if (bind($fh, sockaddr_in($$port, INADDR_ANY)));
293 listen($fh, SOMAXCONN);