Cpanel API Access denied - php

I'm using XML api 2
$xmlapi = new xmlapi('MY IP Address');
$xmlapi->password_auth("root",'My Cpanel password');
$xmlapi->set_output("json");
$xmlapi->set_debug(1);
print $xmlapi->api2_query($account, "Email", "listpopswithdisk" );
But it's giving this error.
{"cpanelresult":{"apiversion":"2","error":"Access denied","data":{"reason":"Access denied","result":"0"},"type":"text"}}
How to resolve this error.

Check your port settings -
$xmlapi->set_port(2083); //2082 if not using secure connection

Related

PHP GMail IMAP Connection Failure

I am using this repo for connecting to the gmail. But its getting error. I hope someone has experience handling this recently.
<?php
namespace program;
require_once "php-imap-client/vendor/autoload.php";
use SSilence\ImapClient\ImapClientException;
use SSilence\ImapClient\ImapConnect;
use SSilence\ImapClient\ImapClient as Imap;
$mailbox = 'imap.gmail.com';
$username = 'alice#gmail.com';
$password = "wonderland";
$encryption = Imap::ENCRYPT_SSL;
// Open connection
try{
$imap = new Imap($mailbox, $username, $password, $encryption);
// You can also check out example-connect.php for more connection options
}catch (ImapClientException $error){
echo $error->getMessage().PHP_EOL;
die(); // Oh no :( we failed
}
I've had this before too. From the link in the error message you're receiving.
Allow less secure apps: If you don't use 2-Step Verification, you
might need to allow less secure apps to access your account.
Here's a guide to do so

cpanel email account creation issue

I am trying to create an email account via script without logging into the cpanel.
Here is the script I am using
http://www.zubrag.com/scripts/cpanel-create-email-account.php
Hosting provider is bluehost
I am getting this error
Cannot create email account. Possible reasons: "fopen" function allowed on your server, PHP is running in SAFE mode
safe mode is off in my ini file.
Here is the credential area that the script is using
// cPanel info
$cpuser = 'example.com'; // cPanel username
$cppass = 'my_cpanel_pass'; // cPanel password
$cpdomain = 'example.com'; // cPanel domain or IP
$cpskin = 'x'; // I have tried x and also bluehost here
// Default email info for new email accounts
// These will only be used if not passed via URL
$epass = 'my_cpanel_pass'; // email password
$edomain = 'example.com'; // email domain (usually same as cPanel domain above)
$equota = 20; // amount of space in megabytes
The following code is sending me the errors/warnings
$f = fopen ("http://$cpuser:$cppass#$cpdomain:2082/frontend/$cpskin/mail/doaddpop.html?email=$euser&domain=$edomain&password=$epass&quota=$equota", "r");
if (!$f) {
$msg = 'Cannot create email account. Possible reasons: "fopen" function allowed on your server, PHP is running in SAFE mode';
break;
}
The warning I'm getting is
Warning: fopen(http://...#example.com:2082/frontend/x/mail/doaddpop.html?email=asif.k&domain=example.com&password=SmallAn123!&quota=20): failed to open stream: HTTP request failed! HTTP/1.1 401 Access Denied in E:\Web Softs\wamp\www\clnt5\cpemail.php on line 81
If I print all the stuff between fopen() I get this
http://cpanel_user_name:my_cpanel_pass#example.com:2082/frontend/x/mail/doaddpop.html?email=asif.k&domain=mydomain.com&password=SmallAn123!&quota=20
I have googled a lot to fix this. Any help or an alternate script that can help me achieve this will be much appreciated.
If you have cpanel username and password then you should use cpanel API to create email accounts and for lot more functionalities too rather than these get request.
require_once 'xmlapi.php';
$xmlapi = new \xmlapi($cpanel_domain);
$xmlapi->password_auth($cpanel_username,$cpanel_password);
$xmlapi->set_port('your domain port will be here');
$api2args = array();
$result = $xmlapi->api1_query($cpanel_password, 'CustInfo', 'getemail', $api2args);
// until here you will get confirmation of your connected cpanel with cpanel api
// then create cpanel as below
$api2args = array(
'domain' => $domain,
'email' => $email_address_to_create,
'password' => $email_password,
'quota' => '2048', // quota for email you want to set
);
$result = $xmlapi->api2_query($cpanel_username, 'Email', 'addpop', $api2args);
if(isset($result->error) && $result->error!=""){
// error can be recorded here
}
else{
// email account get created
}

WHM API: Find cPanel user by domain

I'm working on a WHM API and want to find the cPanel username by passing the domain name.
NOTE: I've the credentials of WHM.
There is no such documentation exists here https://documentation.cpanel.net/pages/viewpage.action?pageId=1507786
So, is there a way to achieve do so?
Update:
Now, I'm using xmlapi.php and here is what I tried so far.
require_once(base_path() . "/vendor/cpanel_api/xmlapi.php");
$ip = env('SERVER_IP', "127.0.0.1"); //your server's IP
$xmlapi = new \xmlapi($ip);
$xmlapi->password_auth(env('CPANEL_USER', "root"),env('CPANEL_PASSWORD', "")); //the server login info for the user you want to create the emails under
$xmlapi->set_output('json');
$xmlapi->set_debug(1);
$params = array('domain'=>$domain, 'searchtype'=>'domain'); //quota is in MB
$res = json_decode($xmlapi->api2_query('root', "listaccts", "", $params), true);
print_r($res);
in $xmlapi->api2_query method, there are 4 arguments
cPanel Username
WHM Module Name
Function under the Given Module in step 2
parameters, that'll be passed to the function in step 3
I have to find out the cPanel Username, so, I wrote the 'root' for now. but no success
Please give a try with cPanel API.
Here are the some useful docs of API.
https://documentation.cpanel.net/display/SDK/Guide+to+cPanel+API+2
https://documentation.cpanel.net/display/SDK/Guide+to+WHM+API+1

cPanel api2 for MysqlFE authorizehost

I am trying to use cPanel xml api to add ip addresses to the mysql remote access host list.
If I use this...
$newdomain = "12.345.45.678";
$myemails = $xmlapi->api2_query($account, "MysqlFE", "authorizehost", array('host'=>$newdomain) );
I can in fact add the ip to the remote host access list. I verified by logging into cPanel and checking.
When I try to access the database remotely, I am blocked. ???? like I have not whitelisted my ip ??
If I log into cPanel and remove the ip address and add it manually(the exact same ip)...then test my remote connection, and it works.
Am I missing something here?
The xmlapi adds the ip successfully, but I still can't get a remote connection.
If it helps at all, my whitelist already has some ip addresses that were added manually. So, it is not an empty record.
<?php
require_once("xmlapi.php");
$ip = 'website ip address here';
$account = 'cpanel username here';
$domain = 'mysite.com';
$passwd = 'cpanel password here';
$xmlapi = new xmlapi($ip);
$xmlapi->password_auth($account, $passwd);
$xmlapi->set_port(2083);
$xmlapi->set_output("simplexml");
$newAuthIP = "123.45.67.890"; <----- sanitized value from a form post
$myemails = $xmlapi->api2_query($account, "MysqlFE", "authorizehost", array('host'=>$newAuthIP) );
?>

Authenticating user using LDAP from PHP

My project is to make a module enrollment system for our university. So I contacted the IT people in my university for details to authenticate the students into the system. We are developing the system using the existing university login. They gave me some LDAP information, I don't know the usage of that.
I'm using PHP,Mysql on an Apacha server.
How can I authenticate a user logging into my system, given his userid and password with the LDAP information.
Given below is the LDAP information(i have changed the domain name etc.)
LDAP information for blueroom.ac.uk domain
LDAP Host : ad.blueroom.ac.uk
LDAP port no: 389
BASE DN : ou=bluebird, dc=bluebird, dc=ac, dc=my
LDAP account to bind : cn = kikdap, ou=servacc, dc=bluebird,dc=ac,dc=uk
LDAP account password : ********
Attribute : sAMAccountName
The general procedure would be (relevant ext/ldap php commands in brackets):
connect to LDAP server using the "LDAP Host" and "LDAP port no" (ldap_connect()) and set the correct connection options (ldap_set_option()), especially LDAP_OPT_PROTOCOL_VERSION and LDAP_OPT_REFERRALS
bind to LDAP server using the "LDAP account to bind" and "LDAP account password" (ldap_bind()) - if you're authenticating against an Active Directory server you can directly use the username and password from the login page and skip all the following steps.
search the tree for a matching user entry/object by specifing the "BASE DN" and the appropriate LDAP filter - most likely something like (&(objectClass=user)(sAMAccountName=%s)) where %s should be replaced by the username to be authenticated (ldap_search())
check if the number of returned entries is 1 (if <> 1 then something has gone wrong, e.g. no user found or multiple users found)
retrive the distinguished name (DN) of this single entry (ldap_get_dn())
use the DN found in the last step to try to bind to the LDAP server with the password given at the authentication page (ldap_bind())
if the bind succeeds then everything is OK, if not, most likely the password is wrong
It's really not as hard as it sounds at first. Generally I'd propose to use some sort of standard library for authenticating against a LDAP server such as the Net_LDAP2 PEAR package or Zend_Ldap out of the Zend Framework. I have no experience with actually using Net_LDAP2 (although I know the code quite well) but Zend_Ldap works very well against Active Directory servers or ADAMS servers (which is obviously what you're working with).
This will do the trick using Zend_Ldap:
$options = array(
'host' => 'ad.blueroom.ac.uk',
'useStartTls' => true,
'accountDomainName' => 'blueroom.ac.uk',
'accountCanonicalForm' => 4,
'baseDn' => 'ou=bluebird,dc=bluebird,dc=ac,dc=my',
);
$ldap = new Zend_Ldap($options);
try {
$ldap->bind('user', 'password');
} catch (Zend_Ldap_Exception $e) {
// something failed - inspect $e
}
// bind successful
$acctname = $ldap->getCanonicalAccountName('user', Zend_Ldap::ACCTNAME_FORM_DN);
You might try http://code.activestate.com/recipes/101525/ while referring to http://us3.php.net/ldap and other results from a Google search for [php ldap authentication].
#Stephen provided good points. Here is my plain PHP code to authenticate using AD:
first you need to know this parameters: server host, user domain (you need also base dn if you want query AD).
use the following code:
$ldap = ldap_connect($host); // e.g. 165.5.54.6 or an URL
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3); // Recommended for AD
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
$bind = ldap_bind($ldap, $username.'#'.$userDomain, $passwrod);
if($bind){
// successful authentication.
}
you could use http://pear.php.net/package/Net_LDAP2/docs
it's nice and works.
Example of connection taken by the doc:
// Inclusion of the Net_LDAP2 package:
require_once 'Net/LDAP.php';
// The configuration array:
$config = array (
'binddn' => 'cn=admin,ou=users,dc=example,dc=org',
'bindpw' => 'password',
'basedn' => 'dc=example,dc=org',
'host' => 'ldap.example.org'
);
// Connecting using the configuration:
$ldap = Net_LDAP2::connect($config);
// Testing for connection error
if (PEAR::isError($ldap)) {
die('Could not connect to LDAP-server: '.$ldap->getMessage());
}

Categories