Help with API

9 posts / 0 new
Last post
#1 Fri, 07/17/2009 - 09:37
theashman

Help with API

Hi there, I plan on creating a management console for my website that will work in conjunction with Virtualmin's API. The plan is to have it so that a user can log in. If the user is a reseller they can create virtual servers. The reseller clicks "create domain" and it with present them with a form: Domain Password

they fill in the form and submit. the form then runs a part of my php virtualmin object to run the api "create-domain" command.

I have tested this on one of the virtual servers i have on my server by doing the following:

$domain = $_GET['domain'];
$pass = $_GET['pass'];

if($pass != '' && $domain != ''){
    echo "creating domain $domain<br />";
    $output = shell_exec("virtualmin create-domain --domain $domain --pass $pass --default-features"); // execute the linux command and put the output in output array

    // dump the output
    echo '<pre>';
    echo $output;
    echo '</pre>';
}

there was no output and the server did not get created. i tried to run the exact same command via CLI (logged in as root) and it worked.

how would i get it to work in a similar way to above? Thanks

Ashley

Fri, 07/17/2009 - 09:54
andreychek

Howdy,

First off, just a mention that the Pro version could save you some hassle there, since it already handles reseller accounts ;-)

However, if you're itching for a coding project, I can appreciate the fun of programming!

As to what the problem is in your case -- there's a lot of things that could be going awry there. So a few notes:

  1. We don't know what dir Apache is using as it's working directory, so you might want to run a different command -- maybe "ls /etc/" or something that you know will return some output.

  2. You may want to add an "else" line to your if statement above, so that if the condition you're using above doesn't work as expected, you still get some kind of output.

  3. Also be sure to look inn $HOME/logs/error_log if things don't work as expected, that's where PHP sends it's errors.

    -Eric

Fri, 07/17/2009 - 10:20
theashman

Hi again Eric! Thanks for replying. No offence to virtualmin, but i think if i offered my clients access to it, it could possibly give them an opportunity to be malicious. My CP will only be basic (create domains, disable, delete, create DBs, Email, FTP, users and such) nothing too intrusive. An i do also love a programming challenge.

Anyway, I have added the else, and tried to list out a dir. listing out a dir gave me the desired output, and if i omit the get variables i get the else section. i just used the command "ls", which listed out the contents of the public_html dir.

I checked my error log and there was nothing out of the ordinary relating to that command.

could it be a problem to do with which user is running the command?

Thanks Ashley

Fri, 07/17/2009 - 14:24
andreychek

Well, if Virtualmin were capable of being everything to everyone, it wouldn't need an API :-)

Can you show what code you have now, as well as what output you're seeing?

Thanks!

-Eric

Fri, 07/17/2009 - 14:52
theashman

well, the only code i have right now is this:

<?php

$domain = $_GET['domain'];
$pass = $_GET['pass'];

if($pass != '' && $domain != ''){
    echo "creating domain $domain<br />";
    #$output = shell_exec("ls"); // execute the linux command and put the output in output array
    $output = shell_exec("virtualmin create-domain --domain $domain --pass $pass --default-features");

    // dump the output
    echo '<pre>';
    echo $output;
    echo '</pre>';
}else{
    echo "nothing here";
}
?>

im testing it's capabilities first, before i get any deeper. the only output i see is "creating domain $x" $x being the get var in the url. so url being domain.tld?domain=hello.com&pass=hello

Thanks Ashley

Sat, 07/18/2009 - 09:50
andreychek

What I might recommend, is have your script print the exact command line that it's running -- and then run that command manually in the shell.

It's possible that you aren't seeing an error of some kind if that error is sent to STDERR, and if shell_exec doesn't capture STDERR output by default.

-Eric

Mon, 07/20/2009 - 03:45
theashman

Hi again,
hmm, i decided to test running the command as the user who is running the web processes.

i first did shell_exec("whoami"); to find the user in a php script, it came out as www-data.
then i tried running "sudo -u www-data virtualmin help" on the CLI and this is what i came out with:
root@doamain:/# sudo -u www-data virtualmin help
usage: /usr/sbin/virtualmin [args..]
or: /usr/sbin/virtualmin help

Available commands :

Error: Script was not run with full path (failed to find /usr/share/webmin/virtual-server/list-features.pl under )

-----
Script was not run with full path (failed to find /usr/share/webmin/virtual-server/list-features.pl under )
-----
root@domain:/#

i imagine this is what is happening when i try to run the command through the php script.

what does the error mean?

Thanks
Ashley

Mon, 07/20/2009 - 04:00
theashman

ok, so i added "2>&1" to the end of my command, which helps print out the errors.

i got this:
"sh: virtualmin: command not found"

so, something is amiss.

any ideas?

thanks
Ashley

Mon, 07/20/2009 - 08:58
andreychek

Yeah, okay, this brings up an excellent point that I had forgotten earlier -- the "virtualmin" command will need to be run as root rather than as www-data in order to work.

But the error you're seeing suggests that you might need to edit the path with your script, and set it to include the directory it's mentioning so that "list-features.pl" can be called.

-Eric

Topic locked