1

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.

Community
  • 1
  • 1
ShakMR
  • 115
  • 2
  • 10
  • If you run the client as a different user, does it work or not? Even if netstat doesn't find it, that may not matter. – John Zwinck Sep 09 '14 at 10:30
  • 2
    This is netstat complaining insufficient rights to show name of the program which binds to the port, not that port is private (there is such thing as private socket) – mpapec Sep 09 '14 at 10:30
  • Yesterday wasn't working, because of my lack of knowledge about the system. The user (not me) runs in login4, and I have launch the deamon on login1, so it's not visible from login4. If I launch in login4 directly it works properly. I'm working to configure the client to connect to login1, if I can do it. Thank you for you answers. – ShakMR Sep 10 '14 at 08:12

0 Answers0