LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-21-2012, 08:24 AM   #1
evgenyz
Member
 
Registered: Sep 2012
Posts: 48

Rep: Reputation: Disabled
failed to execute system command from CGI program


I have CGI program (written in C) that runs from apache server (2.22).
As a part of program flow, there are some commands that the program suppose to execute (grep, mutt). When I run this program mnually it works, but when it runs as a URL (https://myhost.mydomain/cgi-bin/myprogram) any system command is not executing without generating error message.
I tried to create some log file where the output and error messages should be written (such as system("/bin/grep XXX file > /tmp/grep.log 2>&1")) but no log file was created.
Any ideas?
 
Old 11-21-2012, 09:21 AM   #2
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 802
Blog Entries: 2

Rep: Reputation: 203Reputation: 203Reputation: 203
1. Your program may have a different PATH or other environment when run as CGI which prevents it from working.

2. Output from it may be visible in the apache error log.

3. Building commands to run in a shell ( system(...) ) using remote input from web users is dangerous and you'd be safer using Perl in taint mode and with system() in Perl's list format. If you must use C use execv() rather than system() so that input isn't given to a shell. More detail at http://cwe.mitre.org/top25/index.html#CWE-78

4. Is apache using a chroot so that /tmp/grep.log is in a place you didn't look and it might lack permissions for?
 
Old 11-21-2012, 09:27 AM   #3
evgenyz
Member
 
Registered: Sep 2012
Posts: 48

Original Poster
Rep: Reputation: Disabled
Thanks for answering!
My program is not executing commands based on remote input from users, it just executes few system commands based on program logic.
The "commands" (grep and mutt) are in the PATH of the server, hovewer probably I need to check the chroot in apache configuration for the user...
I probably will try execv or popen/pclose.
 
Old 11-22-2012, 09:43 AM   #4
evgenyz
Member
 
Registered: Sep 2012
Posts: 48

Original Poster
Rep: Reputation: Disabled
fork + execv solved the problem,
thanks!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Some Way to Execute a CGI Command Automatically des_a Programming 3 10-27-2007 06:18 PM
Execute a windows command from inside a c-cgi-script leamassiot General 51 02-09-2006 10:06 AM
Failed to execute cgi script in Apache!! baby_linu Linux - Newbie 4 06-30-2004 04:23 PM
Bash command $? failed to execute. Linh Programming 7 05-14-2004 11:11 AM


All times are GMT -5. The time now is 10:16 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration