morleypotter Posted August 11, 2010 Share Posted August 11, 2010 Hi Everyone, I REALLY need some help please before I go mad. As the subject of this post suggests i'm trying to launch Ubuntu applications from a php script, i have tried different ways of doing this but without much success. Firstly i would like to explain why and maybe somebody may have a better suggestion or an alternative route... I am making a 'personal' remix of Ubuntu which will be a live CD i can use for my university work (studying computer forensics), it will basically be a minimal install with every Linux based forensics application i can lay my hands on, instead of having a huge list of shortcut's in the menu's that ship with Ubuntu I though it would be fun/a challenge to make a fancy PHP based web site which contained a database of the programs so i could search through them and find what i need by category ect and then launch those applications from the same site. I can 'talk' to Ubuntu through some small scripts i've tried but i can't launch any applications. Here is what i've got so far.... <?php echo "test1"; $launcher = shell_exec('ls'); print "<pre>$launcher</pre>"; echo "test2"; $output = exec(ls); $doit2 = exec($output); print "<pre>$doit2</pre>"; echo "test3"; $cmd = 'ls'; echo "<pre>".shell_exec($cmd)."</pre>"; ?> Which outputs.... test1 index.php webalizer test2 Webalizer V2.01-10 (Linux 2.6.32-24-generic) locale: C test3 index.php webalizer Sucess i thought, so i then tried using the same formats to launch nautilus as a test, all of the below produced no results... <?php echo "test1"; $launcher = shell_exec('/usr/bin/nautilus'); print "<pre>$launcher</pre>"; echo "test2"; $output = exec(/usr/bin/nautilus); $doit2 = exec($output); print "<pre>$doit2</pre>"; echo "test3"; $cmd = '/usr/bin/nautilus'; echo "<pre>".shell_exec($cmd)."</pre>"; ?> Can anybody please help me out, i've searched all over these forums, php.net and googled endlessly. Any advice would be greatly appreciated Many thanks, MorleyPotter Quote Link to comment Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 Theoretically, if your Apache server user (the user Apache executes under) has the correct permissions this should be possible. You would however need to make a $HOME directory for this user because allot of applications store data within hidden dirs within $HOME. A better option might be to setup the suexec module for Apache, and have your users own there own versions of this 'site' interface. Then your server could execute commands as the user who owns the sites. The entire problem is that websites run under the server, while applications need to run under indeveidual users. I'd like to say this is simple to fix but its likely not unless you know a bit about Linux and Apache. Quote Link to comment Share on other sites More sharing options...
morleypotter Posted August 11, 2010 Author Share Posted August 11, 2010 Hi Thorpe, Firstly thank you for your help, i have read up on suEXEC which isn't (i think) the right tool for the job as there will only be one user on the live cd, [who i will probably make root as nothing can be written to a cd to break anything once i'm finished]. I have posted on ubuntuforums.org as well and what feedback i had from them suggests as you say it is theoretically possible, but nobody seems to actually know how to do it. I'm now changing my train of thought and thinking of writing shell scripts to open the applications and using php to 'activate' the script - is this possible do you think? Again thanks for your help. Quote Link to comment Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 I'm now changing my train of thought and thinking of writing shell scripts to open the applications and using php to 'activate' the script - is this possible do you think? It doesn't matter if you use shell scripts or not. PHP will do fine. Given that your only going to allow one user on these systems, I would make Apache execute as this user. This should (in theory) solve the issue as Apache will be running as the same user who owns the desktop environment. I wouldn't recommend them being root however as they can still quite easily damage filesystems on the machine. Quote Link to comment Share on other sites More sharing options...
morleypotter Posted August 11, 2010 Author Share Posted August 11, 2010 It doesn't matter if you use shell scripts or not. PHP will do fine. Given that your only going to allow one user on these systems, I would make Apache execute as this user. This should (in theory) solve the issue as Apache will be running as the same user who owns the desktop environment. I wouldn't recommend them being root however as they can still quite easily damage filesystems on the machine. Thanks again Thorpe, i have read somewhere that it is the normal practice to add the user to the www-data group, which i have done and re-ran the nautilus scripts above but nothing seems to be happening? I fear i may miss understand? Could you (or anyone willing) please advise on how I would make Apache execute as the current user? Thanks. Quote Link to comment Share on other sites More sharing options...
morleypotter Posted August 11, 2010 Author Share Posted August 11, 2010 Just a quick update on the code i'm using now (will this work once i've gotten Apache to run as the current user) <?php echo "test1"; // shell_exec can return results and print them $launcher = shell_exec('ls -lart'); echo "<pre>$launcher</pre>"; // now to try and launch an application echo "Firefox launchtest1"; echo "</br>"; echo "</br>"; $launcher = exec('firefox'); if ($launcher == "1") { echo "working...."; } else { echo "Firefox Launch Test 1 Failed!"; } echo "</br>"; echo "</br>"; echo "Firefox launchtest2"; echo "</br>"; echo "</br>"; $output = `firefox`; $doit2 = exec($output); if ($doit2 == "1") { echo "working...."; } else { echo "Firefox Launch Test 2 Failed!"; } ?> Which produces.... test1 total 16 drwxr-xr-x 2 root root 4096 May 25 16:54 webalizer drwxr-xr-x 16 root root 4096 May 25 16:54 .. -rw-r--r-- 1 root root 579 Aug 11 13:49 index.php drwxr-xr-x 3 root root 4096 Aug 11 13:49 . Firefox launchtest1 Firefox Launch Test 1 Failed! Firefox launchtest2 Firefox Launch Test 2 Failed! -- Thanks again. Quote Link to comment Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 No. As the manual says exec returns the last line of output resulting from the command. Its looks like your trying to check the commands status (which by the way will be 0 if successful). You'll want to look at the third arg to exec. Quote Link to comment Share on other sites More sharing options...
morleypotter Posted August 11, 2010 Author Share Posted August 11, 2010 No. As the manual says exec returns the last line of output resulting from the command. Its looks like your trying to check the commands status (which by the way will be 0 if successful). You'll want to look at the third arg to exec. Hi Thorpe, I changed the code and now it's reporting "working", so i assume that when i figure out how to give the sufficient rights to Apache it should launch the application? OR Because i added the 'if' statement to see if it was actually doing anything the code will now not launch the application? Apologies if this doesn't make sense, i think i've confused myself now! :-\ echo "Firefox launchtest2"; echo "</br>"; echo "</br>"; $output = `firefox`; $doit2 = exec($output); if ($doit2 == "0") { echo "working...."; } else { echo "Firefox Launch Test 2 Failed!"; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 Surrounding a command in backticks executes it. I'm not sure why you are then trying to pass the output of that commands execution to the exec function. Quote Link to comment Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 Oh and ps. The reason you are getting the working message is because exec must be returning false. 0 is the same as false in php. You will need to use the === operators to make sure you are actually getting the string "0". Quote Link to comment Share on other sites More sharing options...
morleypotter Posted August 11, 2010 Author Share Posted August 11, 2010 Oh and ps. The reason you are getting the working message is because exec must be returning false. 0 is the same as false in php. You will need to use the === operators to make sure you are actually getting the string "0". Thanks Thorpe, you are right it does now display "failed" as you suggest, as to why i thought it a good idea to pass it again to exec - i have no idea. The code i have below therefore isn't even attempting to launch firefox, i have also added the /usr/bin path to it to see if that helps but nothing, oh well, i'll try some more googling to see how to give those rights to Apache. I have just ran a echo exec('whoami'); the response being 'www-data' [which i understand is Apache] so i've done something wrong whilst trying to get Apache to run as the current user. <?php echo "Firefox Launch Test"; echo "</br>"; echo "</br>"; $output = `/usr/bin/firefox`; if ($output === "0") { echo "working...."; } else { echo "Firefox Launch Test Failed!"; } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 On Debian (Ubuntu should be the same) you need to edit the /etc/apache2/envvars file to change the user & group Apache runs as. You'll then need to restart Apache. sudo /etc/init.d/apache2 restart Quote Link to comment Share on other sites More sharing options...
morleypotter Posted August 11, 2010 Author Share Posted August 11, 2010 Thanks Thorpe - Couldn't find that info anywhere, i was looking at suPHP when i noticed your post. Here are the results of ps aux | grep apache which means i've changed it properly this time.... dave@dave-laptop:~$ ps aux | grep apache root 14744 0.0 0.3 21344 6364 ? Ss 15:18 0:00 /usr/sbin/apache2 -k start dave 14747 0.0 0.1 21344 3644 ? S 15:18 0:00 /usr/sbin/apache2 -k start dave 14748 0.0 0.2 21800 5352 ? S 15:18 0:00 /usr/sbin/apache2 -k start dave 14749 0.0 0.2 21800 5328 ? S 15:18 0:00 /usr/sbin/apache2 -k start dave 14750 0.0 0.2 21800 5336 ? S 15:18 0:00 /usr/sbin/apache2 -k start dave 14751 0.0 0.1 21344 3656 ? S 15:18 0:00 /usr/sbin/apache2 -k start dave 14856 0.0 0.1 21344 3644 ? S 15:18 0:00 /usr/sbin/apache2 -k start dave 15116 0.0 0.0 3324 804 pts/0 S+ 15:20 0:00 grep --color=auto apache And my code which is this still doesn't work? echo "Firefox Launch Test"; echo "</br>"; echo "</br>"; $output = `/usr/bin/firefox`; if ($output === "0") { echo "working...."; } else { echo "Firefox Launch Test Failed!"; } I'm so frustrated i thought i was onto a winner when you told me how to change the user Apache runs as. Quote Link to comment Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 Use shell_exec and take a look at what the command outputs. Quote Link to comment Share on other sites More sharing options...
morleypotter Posted August 11, 2010 Author Share Posted August 11, 2010 Hi Thorpe, I tried it but it only made a blank line, when i change the word 'firefox' with 'ls' for example it then produces a result so i'm sure i've done it right? <?php echo "Firefox Launch Test"; echo "</br>"; $output = shell_exec('firefox'); echo "<pre>$output</pre>"; if ($output === "0") { echo "working...."; } else { echo "Firefox Launch Test Failed!"; } echo "</br>"; echo "whoami query = "; echo (`whoami`); ?>; Quote Link to comment Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 Well, use exec then but take a look at the manual and the extra arguments you can provide it. Quote Link to comment Share on other sites More sharing options...
morleypotter Posted August 11, 2010 Author Share Posted August 11, 2010 Yeah, i tried that before, but it did the same thing, i have also read the manual but i'm not sure what other arguments to pass to it as i simply want it to open the application not do anything in particular? Quote Link to comment Share on other sites More sharing options...
trq Posted August 11, 2010 Share Posted August 11, 2010 i have also read the manual but i'm not sure what other arguments to pass to it as i simply want it to open the application not do anything in particular? You might want to read the manual entry again then. The optional arguments are explained. Both the second and third arguments are passed by reference meaning they can be accessed again from outside the function. The second arg is an array containing all output produced by the command. the third is the exit status. Quote Link to comment Share on other sites More sharing options...
morleypotter Posted August 12, 2010 Author Share Posted August 12, 2010 I've made a small amount of progress amending an example on the manual page but i was wondering if anyone could 'tweak' this code to get it to work. 1st the code then the output. <?php exec('TERM=xterm display localhost:0.0 /usr/bin/firefox n 1 b i', $xterm, $error ); echo nl2br(implode("\n",$xterm)); if ($error){ exec('TERM=xterm display localhost:0.0 /usr/bin/firefox n 1 b 2>&1', $error ); echo "Error: "; exit($error[0]); } ?> Error: display: unable to open X server `' @ display.c/DisplayImageCommand/422. I tried adding 'startx' in various places but it just makes the page hang? Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
trq Posted August 12, 2010 Share Posted August 12, 2010 Do you know what startx actually does? Sorry, but if you want to do this, your going to need to do more investigating yourself. Quote Link to comment Share on other sites More sharing options...
morleypotter Posted August 12, 2010 Author Share Posted August 12, 2010 well the error was: Error: display: unable to open X server `' @ display.c/DisplayImageCommand/422. So i thought that i may need to start another x session which is why i tried 'startx' Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.