Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!

Permission denied when creating database via publicapi

Discussion in 'cPanel Developers' started by bimbo1989, May 7, 2018.

  1. bimbo1989

    bimbo1989 Member

    Joined:
    May 7, 2018
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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
     
  2. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,214
    Likes Received:
    1,936
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  3. bimbo1989

    bimbo1989 Member

    Joined:
    May 7, 2018
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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 );
    }
    }
    
     
    #3 bimbo1989, May 9, 2018
    Last edited by a moderator: May 9, 2018
  4. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,214
    Likes Received:
    1,936
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. bimbo1989

    bimbo1989 Member

    Joined:
    May 7, 2018
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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").
     
  6. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,214
    Likes Received:
    1,936
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    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.

    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  7. bimbo1989

    bimbo1989 Member

    Joined:
    May 7, 2018
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Rovigo
    cPanel Access Level:
    Website Owner
    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?
     
  8. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,214
    Likes Received:
    1,936
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  9. bimbo1989

    bimbo1989 Member

    Joined:
    May 7, 2018
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    Rovigo
    cPanel Access Level:
    Website Owner
    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.
     
  10. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,214
    Likes Received:
    1,936
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  11. bimbo1989

    bimbo1989 Member

    Joined:
    May 7, 2018
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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.
     
  12. bimbo1989

    bimbo1989 Member

    Joined:
    May 7, 2018
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    1
    Location:
    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.
     
  13. cPanelMichael

    cPanelMichael Technical Support Community Manager
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    45,214
    Likes Received:
    1,936
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Twitter:
    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.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
Loading...

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice