#!/usr/bin/perl -w use strict; use Socket; sub usage { die "Usage: $0 [-u uid] [-g gid] port prog-to-exec [args ...]\n"; } # Parse Options use vars qw(%opt); while (@ARGV && $ARGV[0] =~ /^-([ugh])$/) { shift; $opt{$1}=shift; } $opt{h} && usage(); $opt{u} = 2 # daemon unless $opt{u}; $opt{g} = 2 # daemon unless $opt{g}; # Get port and verify it my $port = shift; $port && $port =~ /^\d+$/ or usage(); # Make sure there's something to exec @ARGV >= 1 or usage(); # We're going to give the client app the socket on STDIN, so close the # old one... close(STDIN); # Create the socket and bind it to the port socket(STDIN,PF_INET,SOCK_STREAM,getprotobyname('tcp')) or die "Socket error: $!"; setsockopt(STDIN,SOL_SOCKET, SO_REUSEADDR, pack("l",1)) or die "setsockopt error: $!"; bind(STDIN,sockaddr_in($port,INADDR_ANY)) or die "bind error: $!"; # Switch to our new user and group $( = $) = $opt{g}; $< = $> = $opt{u}; if ($( != $opt{g} or $) != $opt{g}) { die "setgid failed\n"}; if ($> != $opt{u} or $< != $opt{u}) { die "setuid failed\n"}; # And get on with the show! exec(@ARGV) or die "exec error: $!\n"; #### #!/usr/bin/perl -Tw use Socket; use strict; listen(STDIN,SOMAXCONN) or die "listen error: $!\n"; for(;my $paddr=accept(CLIENT,STDIN); close(CLIENT)) { print CLIENT "The time is now ",scalar localtime,"\n"; }