Permission denied when creating database via publicapi

bimbo1989

Member
May 7, 2018
7
0
1
Rovigo
cPanel Access Level
Website Owner
I have a problem with the cpanel api
This is the one I'm using
CpanelInc/publicapi-php
I could successfully create a database using the default user that our hosting provider (FastComet) gave us, but I need to use a dedicated user for this task.
So we created a new user on our server, with appearently the same permissions, but we can't get it to work.
We already opened a ticket by them, but they told us we need to contact the cpanel support directly.
What could be the issue?
Thank you in advance
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,229
463
Hello @bimbo1989,

Can you let us know the contents of your test script? Is this for a script you are running as a plugin within the cPanel or WHM interface, or are you running it via a standalone PHP script uploaded to your server?

Thank you.
 

bimbo1989

Member
May 7, 2018
7
0
1
Rovigo
cPanel Access Level
Website Owner
This is the code I am using in my project (using this package CpanelInc/publicapi-php ) .
Please note that if I change che user from "software_api" to "software" it works but it resets my password, that's why I wanted to use a different user to avoid any problem.
Code:
<?php

require_once(__DIR__.'/Util/Autoload.php');

class CPanel{

public function __construct(){
cpanel_autoload();
}

public function createDatabase( $databaseName ){
$cp = $this->getInstance();

$url = '/json-api/cpanel';
$formdata = array(
'cpanel_jsonapi_module' => 'MysqlFE',
'cpanel_jsonapi_apiversion' => '2',
'cpanel_jsonapi_user' => 'software_api',
'cpanel_jsonapi_func' => 'createdb',
'db' => 'software_' . $databaseName
);

$response = $cp->api_request(
'WHM',
$url,
'GET',
$formdata
);

return json_decode($response->getRawResponse());
}

private function getInstance(){
$config = array(
'service' => array(
'whm' => array(
'config' => array(
'host' => '172.104.147.32',
'user' => 'software_api',
'password' => 'MY_PASSWORD'
),
),
),
);
return Cpanel_PublicAPI::getInstance( $config );
}
}
 
Last edited by a moderator:

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,229
463
I could successfully create a database using the default user that our hosting provider (FastComet) gave us, but I need to use a dedicated user for this task.
So we created a new user on our server, with appearently the same permissions, but we can't get it to work.
Hello @bimbo1989,

Could you provide some more background information on this issue? I'd like to reproduce it on a test environment but I need more information about what the overall goal or task you need to complete. Also, can you clarify what you mean by "dedicated user"?

Thank you.
 

bimbo1989

Member
May 7, 2018
7
0
1
Rovigo
cPanel Access Level
Website Owner
Hello,
the goal is to create a database everytime someone registers to my portal.
With "dedicated user" I mean using credentials for this task only, without using the "root" user (in my case named "software").
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,229
463
With "dedicated user" I mean using credentials for this task only, without using the "root" user (in my case named "software").
The authentication process accesses the server in order to run code as a specific cPanel or WHM user with a specific set of permissions. There's no support for authenticating as a non-root user that's not associated with an existing cPanel account.

the goal is to create a database everytime someone registers to my portal.
Do you have root access to this server, or just access to the cPanel account? If you have root access, you may want to consider setting up a hook that makes use of the Mysql::create_database UAPI function:

Guide to Standardized Hooks - Developer Documentation - cPanel Documentation
UAPI Functions - Mysql::create_database - Developer Documentation - cPanel Documentation

Thank you.
 

bimbo1989

Member
May 7, 2018
7
0
1
Rovigo
cPanel Access Level
Website Owner
The authentication process accesses the server in order to run code as a specific cPanel or WHM user with a specific set of permissions. There's no support for authenticating as a non-root user that's not associated with an existing cPanel account.
Ok, this closes this problem but opens another.
If I use my root user, named "software", I can successfully create the database via the whm BUT... somehow, I don't know what I'm doing wrong, after I create the database I can't access the control panel anymore and I need to reset the password of the "software" user to be able to log in again in the control panel of the server.
Am I missing something?
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,229
463
If I use my root user, named "software", I can successfully create the database via the whm BUT... somehow, I don't know what I'm doing wrong, after I create the database I can't access the control panel anymore and I need to reset the password of the "software" user to be able to log in again in the control panel of the server.
Am I missing something?
Hello,

Generally, "root" is the only username that has full root access to the system. Is "software" a reseller user? What method are you using to create a database from Web Host Manager?

Thank you.
 

bimbo1989

Member
May 7, 2018
7
0
1
Rovigo
cPanel Access Level
Website Owner
Hello,
Generally, "root" is the only username that has full root access to the system. Is "software" a reseller user? What method are you using to create a database from Web Host Manager?
Thank you.
It probably is the name that FastComet (the reseller) gave to the our user on our server.
I am using this WHM API cPanel API 2 Functions - MysqlFE::createdb - Developer Documentation - cPanel Documentation .
The fact is that it WORKS and the database gets created successfully, but after the creation of the db I can't access to cpanel anymore and I need to reset the password of my user.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,229
463
It probably is the name that FastComet (the reseller) gave to the our user on our server.
I am using this WHM API cPanel API 2 Functions - MysqlFE::createdb - Developer Documentation - cPanel Documentation .
The fact is that it WORKS and the database gets created successfully, but after the creation of the db I can't access to cpanel anymore and I need to reset the password of my user.
Hello,

If I understand correctly, you are using WHM API (JSON) example in your web browser while logged in as the reseller user. If so, that's acceptable, and should not alter the account's password upon execution. If you do not have root access via SSH to this server, please report this to your web hosting provider so they can take a closer look at /usr/local/cpanel/logs/login_log to see exactly why authentication fails when you attempt to access cPanel. If they are unable to determine the cause of the issue, feel free to have them open a support ticket using the link in my signature so we can take a closer look.

Thank you.
 

bimbo1989

Member
May 7, 2018
7
0
1
Rovigo
cPanel Access Level
Website Owner
No, I explained myself badly.
At the moment of the execution of the script, I never logged on cpanel before (not on the PC i was using).
I was executing the script in localhost, calling of course the live server URL.
The database got created just fine, only lost my user credentials.
I'll try and contact them for the logs.
 

bimbo1989

Member
May 7, 2018
7
0
1
Rovigo
cPanel Access Level
Website Owner
This is the relevant log we found
[2018-05-05 12:59:24 +0200] info [whostmgrd] 149.13.148.129 - software_api "GET /json-api/cpanel?cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_user=software_api&cpanel_jsonapi_func=createdb&db=software_prova HTTP/1.1" FAILED LOGIN whostmgrd: login attempt to whm by a non-reseller/root

That is coherent with what you told me: "software_api" was a user created by ourselves, so it's not capable of creating databases.

But a trouble remains: if I use cpanel_jsonapi_user=software that IS our main account, the database gets created but the password of the user gets somehow reset.
 

cPanelMichael

Administrator
Staff member
Apr 11, 2011
47,909
2,229
463
But a trouble remains: if I use cpanel_jsonapi_user=software that IS our main account, the database gets created but the password of the user gets somehow reset.
Hello @bimbo1989,

I've been unable to reproduce this issue on a test environment. Could you ask your provider to open a support ticket with the link in my signature so we can take a closer look at the affected system to see what's happening?

Thank you.