View Full Version : system() function returning a value 256 ?
ganesh_mak
09-08-2006, 07:39 AM
Hi,
i have test.cgi file in cgi-bin directory .
test.cgi
#! /usr/bin/perl
use CGI;
print start_html();
system('sudo ls');
print end_html;
even i have added the apache user with certain permission to execute the ls command
in /etc/sudoers file.
when i run it through command line its working perfectly fine.but when i run it through web browser i am getting no o/p as system() is returnning a value of 256.
i am using apache on fedora core -3
can anybody help me out in solving this ?
thanks
FishMonger
09-08-2006, 07:56 AM
Why are you using sudo? The ls command doesn't require sudo access.
i am getting no o/p as system() is returnning a value of 256That's because a system call returns the return code of the command you execute. If you want the output of the command use backticks.
#!/usr/bin/perl
use CGI qw(:standard);
print header(), start_html();
print `ls`;
print end_html();
ganesh_mak
09-08-2006, 08:45 AM
using the backticks also i am not getting the output on the web browser.its blank.
and if need to call an c executable which has got root as owner through the cgi .
should i have to use system() and sudo . or is there any other way ?
thanks
ganesh_mak
09-08-2006, 11:03 AM
using the backticks also i am not getting the output on the web browser.its blank.
and if need to call an c executable which has got root as owner through the cgi .
should i have to use system() and sudo . or is there any other way ?
thanks
i found one more thing
print `pwd`;
system('pwd');
both are working fine.
but
print `ls`;
system(ls');
are not working .....?
FishMonger
09-08-2006, 05:15 PM
The ls command works in my test and should work in yours, We could add a little debugging code to see what's wrong, but first lets take a step back and find out what you're trying to accomplish and why you're using the shell commands instead of Perl methods.
ganesh_mak
09-11-2006, 07:35 AM
The ls command works in my test and should work in yours, We could add a little debugging code to see what's wrong, but first lets take a step back and find out what you're trying to accomplish and why you're using the shell commands instead of Perl methods.
my $cmd = ("ls");
if (system($cmd)){
print "<p>\n$cmd->Reason: ($?) $!\n\n</p>";
}
if ($? == -1) {
print "failed to execute: $!\n";
}
elsif ($? & 127) {
printf "\nChild died with signal %d, %s coredump\n\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $? >> 8;
}
the web browser o/p for the above code is
ls->Reason: (-1) Permission denied
failed to execute: Permission denied
ganesh_mak
09-11-2006, 11:45 AM
my $cmd = ("ls");
if (system($cmd)){
print "<p>\n$cmd->Reason: ($?) $!\n\n</p>";
}
if ($? == -1) {
print "failed to execute: $!\n";
}
elsif ($? & 127) {
printf "\nChild died with signal %d, %s coredump\n\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $? >> 8;
}
the web browser o/p for the above code is
ls->Reason: (-1) Permission denied
failed to execute: Permission denied
can anybody help me out in solving the above one ?
ganesh_mak
09-11-2006, 11:51 AM
can anybody help me out in solving the above one ?
FishMonger
09-11-2006, 10:56 PM
The only reason I can think of why the ls command is not working in the script is because its permissions have been altered. Have you verified its permissions?
[root@fc4dev ~]# ls -la /bin/ls
-rwxr-xr-x 1 root root 91012 Jul 25 2005 /bin/ls
What are you really trying to accomplish? Do you actually need to get a lising of the files in the directory or are you just using the ls command to test the usage of the system call?
ganesh_mak
09-12-2006, 07:09 AM
The only reason I can think of why the ls command is not working in the script is because its permissions have been altered. Have you verified its permissions?
What are you really trying to accomplish? Do you actually need to get a lising of the files in the directory or are you just using the ls command to test the usage of the system call?
i have to call a c executable in cgi passing optional arguments to the executable in system function in cgi. c executable has got root as owner .
ls -al /bin/ls
-rwxr-xr-x 1 root root 85232 Oct 5 2004 /bin/ls
for this case : permission denied
FishMonger
09-12-2006, 08:08 AM
Given the permission settings of your ls command, there is no reason for it to return "permission denied". There must be something else going on that your not telling us.
You could try using this sudo module.
http://search.cpan.org/~landman/Sudo-0.21/lib/Sudo.pm
ganesh_mak
09-12-2006, 01:16 PM
Given the permission settings of your ls command, there is no reason for it to return "permission denied". There must be something else going on that your not telling us.
You could try using this sudo module.
http://search.cpan.org/~landman/Sudo-0.21/lib/Sudo.pm
thanks for the reply.
i installed new fc-2 and the same code is working perfectly fine.
but i could not resolve the problem on fc-3 . even i asked one my friend to test the same code on fc-4 it worked there also.
ls permission as above. sytem('ls') was not working and after adding apache user to suoers file also it did not work for me.
i dont understand why it was not working on fc-3.
any way thanks . if u could find somthing for this .......then let me know......
as i wasted two days for that n still did nt understand the problem ?
FishMonger
09-12-2006, 03:13 PM
The only other possible cause that I can think of is that you were running the script in 'taint' mode. However, I would have expected the following error message if that were the case.
Insecure $ENV{PATH} while running with -T switch
ganesh_mak
09-13-2006, 07:29 AM
The only other possible cause that I can think of is that you were running the script in 'taint' mode. However, I would have expected the following error message if that were the case.
Insecure $ENV{PATH} while running with -T switch
i used without taint mode .
ganesh_mak
09-18-2006, 10:59 AM
hi,
I tried doing this.
my @cmd1 = ("PATH=$ENV{PATH}:/bin/ls");
if (system(@cmd1)){
printf "<p>\n @cmd1->Reason: ($?) $! \n\n</p>";
}
if ($? == -1) {
print "failed to execute: $!\n";
}
elsif ($? & 127) {
printf "\nChild died with signal %d, %s coredump\n\n",
($? & 127), ($? & 128) ? 'with' : 'without';
}
else {
printf "child exited with value %d\n", $? >> 8;
}
its returning 0 . but as system returns code ,to get the o/p i used backticks .
my @bcmd = `PATH=$ENV{PATH}:/bin/ls`;
print $bcmd[0];
Now also i am unable to get the o/p on the browser
still its not working on Fedora Core -3 .
can u convert the above system() into backticks working code.so that through backticks i get the o/p on the browser.
ganesh_mak
09-19-2006, 01:31 PM
Finally i could solve the problem .
it was the fedora core 3 security issue with apache 2.0.52.
i had selinux enabled .so it was not giving permission to execute system('ls') and
system('c executable') through the server.
so i relabled the selinux policy .
chon -R system_u:object_r:bin_t <path to the c executable>
Now it works perfectly fine.
Its all about the seLinux Ploicies for fedora core.
Thanks a lot.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.