domenica 4 gennaio 2015

[bash CPanel] hostmonster / bluehost email accounts and forwarders bulk management

Hostmonster and Bluehost give you the possibility to bulk add email accounts and forwarders loading a preformatted CSV. Ok, but I have hundreds and I need to bulk add, delete, migrate, etc etc.

I came to several websites stating that something like this would have worked:

http://username:password@domain.com:2082/frontend/hostmonster/mail/dodelfwd.html?bla bla bla

But you can safely replace the frontend theme "hostmonster" with "x3" (default CPanel theme) or "bluehost" or your custom theme. Perhaps it worked a long time ago, or it works in a non chrooted environment. I own a shared hosting, so I don't have access to sudo, nor to /etc/valiases, neither to other special folders, so the classic sysadmin ways are not applicable for me.

Bluehost / hostmonster have a landing page on which you need to authenticate, then it generates a CPanel session (you can see it in the url "cpsess" as soon as you authenticate) and you just can't perform this from outside without spending days on curl / phantomjs etc. Another time wasting method would be the CPanel XML API authentication, which wouldn't work in this case due to the special Bluehost double authentication mentioned above.

Life is easier... It's not possible to handle this method from outside, but it's perfectly working from localhost bash. Thus you simply need to login via SSH in order to perform manual commands or upload a script in order to handle this automatically.

SSH must be enabled on CPanel. Login via SSH and try yourself:

curl http://youruser:yourpass@127.0.0.1:2082
\/json-api/cpanel?cpanel_jsonapi_user=repeatyouruser
\&cpanel_jsonapi_version=2\&cpanel_jsonapi_module=Email
\&cpanel_jsonapi_func=delforward\&email=yourforwardercompleteemail
\&emaildest=yourrecipientcompleteemail

Please note that I needed to escape the & in order for curl to work on bash, or it will interprete the & as a process fork and puts curl in background. The new API calls can be found here:

https://documentation.cpanel.net/display/SDK/cPanel+API+2+Functions+-+Email%3A%3Aaddforward

Read it carefully, you can now perform almost any kind of operations e.g. giving your cuatomers a "subpanel" in which perform almost any task with your (hidden) credentials. Please note that if you miss something, you will always get return 1 (should be = ok), so please carefully refer to the documentation. In my example, addforward returns the submitted email address unless you mispell some parameter or miss something. It only gives error if you completely omit all the parameters.

Do you like to use their PHP APIs instead? No problem, download it from:
https://github.com/CpanelInc/xmlapi-php

Here's the above example translated into PHP API call:

$xmlapi = new xmlapi("127.0.0.1");
$xmlapi->password_auth("youruser","yourpass");

$xmlapi->set_output("json"); //it could be xml
$xmlapi->set_port(2082);
$xmlapi->set_debug(1);

$acct = array(
email => "aaa@yourdomain.com",
domain => "yourdomain.com",
fwdopt => "fwd",
fwdemail => "you@ yourdomain.com"
 );

print $xmlapi->api2_query( "repeatyouruser", "Email", "addforward", $acct );