I have a Perl script that creates a daemon listening from a socket. When I execute the client to send data to the socket gets connected without problems. But the daemon should be queried from other users and it is not possible.
This is the daemon Perl script (assume that readFile() returns a predefined string):
#!/usr/bin/perl
use POSIX qw(setsid);
use strict;
use warnings;
use IO::Socket;
use Term::ANSIColor;
&daemonize;
my $port = '7070';
my $sock = new IO::Socket::INET(
LocalHost => '127.0.0.1',
LocalPort => '7070',
Proto => 'tcp',
Listen => 1,
Reuse => 1,
);
die `date` . "Could not create socket: $!\n" unless $sock;
while (1) {
my $new_sock = $sock->accept();
my $data = "";
$new_sock->recv( $data, 1024 );
print $data;
my $result = &readFile($data);
$new_sock->send($result);
shutdown( $new_sock, 1 );
}
close($sock);
sub daemonize {
chdir '/' or die "Can't chdir to /: $!";
umask 0;
open STDIN, '/dev/null' or die `date` . "Can't read /dev/null: $!";
open STDOUT, '>/home/bsc99/bsc99871/ACCO/pid' or die `date` . "Can't write to SDTOUT: $!";
open STDERR, '>>', '/home/bsc99/bsc99871/ACCO/ACCOUNT_ERROR.log' or die `date` . "Can't write to STDERR: $!";
defined( my $pid = fork ) or die `date` . "Can't fork: $!";
exit if $pid;
print $$;
setsid or die "Can't start a new session: $!";
}
This is the client:
my $sock = new IO::Socket::INET(
PeerAddr => '127.0.0.1',
PeerPort => '7070',
Proto => 'tcp',
Timeout => 3,
) or die "Could not create socket: $!\n";
# data to send to a server
my $req = $ARGV[0];
my $size = $sock->send($req);
# notify server that request has been sent
shutdown( $sock, 1 );
# receive a response of up to 1024 characters from server
my $response = "";
$sock->recv( $response, 1024 );
print $response;
$sock->close();
if I login as other user different than the one who start the daemon. I can run netstat command and see:
user@login4:> netstat -putan or lsof | grep 7070
(No info could be read for "-p": geteuid()=1801 but you should be root.)
I understand the message but my doubt is if there is any solution like a creating a public socket other than running the daemon as root, because I haven't root access.