well, i'm setting up a script that display html page thru a socket.
but as basic it is, it permit to go up in directories and let display dangerous data like /etc/passwd etc.. So i intended to chroot the directory to secure it .this where i'm getting trouble i tried to put such lines:
my $user="nobody"; unless ($uid =(getpwnam($user))2){ die "tentative de lancer user inexistant ou root :p\n";
and just before the treatment : chroot($docroot) or die " chroot() a échoué : $!\n"; $> = $uid ;
here is the script hope that you may help me..
#! /usr/bin/perl use Socket; #config serveur my $docroot = "/var/www/html"; my $addr_serv =""; my $port_serv ="34000"; my $protocole ="tcp"; my $user="nobody"; $SIG{CHLD}="IGNORE"; unless ($uid =(getpwnam($user))[2]){ die "tentative de lancer user inexistant ou root :p\n"; } #definition socket my $proto =getprotobyname ($protocole); $proto = getprotobynumber ($protocole) unless defined ($proto); die "Protocole : $!\n" unless (defined ($proto)); my $port = $port_serv if ($port_serv !~ /\D/); $port = getservbyname ($port_serv, $proto) unless (defined ($port)); die "Services : $!\n" unless (defined ($port)); my $adr = gethostbyname ($addr_serv); $adr = gethostbyaddr ($addr_serv, AF_INET ) unless (defined ($adr)); $adr = INADDR_ANY unless (defined ($adr)); socket SOCK_SRV, PF_INET, SOCK_STREAM, $proto or die "socket : $!\n"; #bind le port et l'addresse setsockopt (SOCK_SRV , SOL_SOCKET, SO_REUSEADDR, pack ("l" , 1)); bind (SOCK_SRV, sockaddr_in ($port, $adr)) or die "bind : $!\n"; listen (SOCK_SRV, 5); chroot('/var/www/html') or die " chroot() a échoué : $!\n"; $> = $uid ; #ouvre le père jusqu'a ctrl+C for (;;) { accept SOCK, SOCK_SRV or last; ($port, $adr) = unpack_sockaddr_in getpeername SOCK; #fork le fils if (fork !=0) { close SOCK ; next; } close SOCK_SERV; select SOCK; $| = 1; chroot($docroot) or die " chroot() a échoué : $!\n"; $> = $uid ; #traitement de la requete while (<SOCK>) { #ignore tt requete sauf celle précédé de GET last if (/^\s*$/); next unless ( /^GET /); $path = (split (/\s+/)) [1]; # ouvre le fichier demandé if (open (FILE, "$docroot$path")) { my @lines = <FILE>; foreach $l(@lines){ print $l; } close (FILE); print "\n"; } # ou affiche une erreur si inexistant else { print "error" ; exit (0); } } exit (0) ; }
In reply to chroot a directory... by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |