The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Set database user privileges from bash or API1 or API2

Discussion in 'cPanel Developers' started by ITGabs, Feb 14, 2016.

  1. ITGabs

    ITGabs Well-Known Member

    Joined:
    Jul 30, 2013
    Messages:
    81
    Likes Received:
    0
    Trophy Points:
    6
    cPanel Access Level:
    Root Administrator
    Hi,

    I need for years just a reliable way to set up database user privileges from bash. By cpanel recommendation I started to use API1 and API2 and just to do this, but still is quite unstable

    Bash
    If I directly create users databases and privileges in mysql, when an account is deleted the privileges configuration is lost, the /script/dbmaptool not help

    API1
    I created my own dbmaptool.php first using the API 1 but from a month ago
    hxxps://127.0.0.1:2087/json-api/cpanel?user=$cpuser&cpanel_jsonapi_module=Mysql&cpanel_jsonapi_func=adduserdb&cpanel_jsonapi_apiversion=1&arg-0=$cpdbname&arg-1=$cpdbuser&arg-2=all

    Produce random errors when an account is deleted, many databases created by command show the same problems

    API2
    Was working good but again started to show some problems

    hxxps://127.0.0.1:2087/json-api/cpanel?cpanel_jsonapi_user=$cpuser&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_func=setdbuserprivileges&db=$cpdbname&dbuser=$cpdbuser&privileges=ALL%20PRIVILEGES
    hxxps://127.0.0.1:2087/json-api/cpanel?cpanel_jsonapi_user=$cpuser&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_func=setdbuserprivileges&db=$cpdbname&dbuser=$cpdbuser&privileges=ALL PRIVILEGES

    The json result show this

    {"cpanelresult":{"apiversion":2,"error":"1","data":[],"func":"setdbuserprivileges","event":{"result":1},"module":"MysqlFE"}}

    why json is giving back a error and later a result ok?

    while the other commans shows
    {"cpanelresult":{"apiversion":2,"data":[1],"func":"createdbuser","event":{"result":1},"module":"MysqlFE"}}
    {"cpanelresult":{"apiversion":2,"data":[1],"func":"createdb","event":{"result":1},"module":"MysqlFE"}}
     
    #1 ITGabs, Feb 14, 2016
    Last edited by a moderator: Feb 15, 2016
  2. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    651
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  3. ITGabs

    ITGabs Well-Known Member

    Joined:
    Jul 30, 2013
    Messages:
    81
    Likes Received:
    0
    Trophy Points:
    6
    cPanel Access Level:
    Root Administrator
    how can I log as root in the UAPI from bash? I tried before opening this thread, this is a server level application never accessible to users, I am using curl to log as root in the API1 and API2.

    Simple is better this is already bloated for something that need 1 query in mysql
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    651
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
  5. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    651
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello :)

    Also, to note, internal case CPANEL-4218 is open to address an issue where the MysqlFE setdbuserprivileges function for cPanel API 2 fails in cPanel versions 54 and newer. Please monitor our change log to see when this case has been addressed:

    Change Logs - Documentation - cPanel Documentation

    Thank you.
     
  6. ITGabs

    ITGabs Well-Known Member

    Joined:
    Jul 30, 2013
    Messages:
    81
    Likes Received:
    0
    Trophy Points:
    6
    cPanel Access Level:
    Root Administrator
    Ok cool, I was right about the version 54
    I will update the API tomorrow, just 1 more question, in the API1 and API2 i am not using the root password to login, instead a hash, this is a fragment of the code that I am using to login using curl

    PHP:
    $whmusername='root';
    $hash='
    56435ed0b3eb38a428270492f6ffb6ad55f04753370f0fa9fec716cf3748f28fa79fed752040eb22c... ...16166c8f0bfcf1c650f37b32cf30e0fbd6220dd60deed349a1a69bc3614c20066587e3f7b0ab64
    '
    $query "https://127.0.0.1:2087/json-api/cpanel?cpanel_jsonapi_user=$cpuser&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_func=setdbuserprivileges&db=$cpdbname&dbuser=$cpdbuser&privileges=ALL%20PRIVILEGES";

    $curl curl_init();
    curl_setopt($curlCURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($curlCURLOPT_SSL_VERIFYPEER,0);
    curl_setopt($curlCURLOPT_RETURNTRANSFER,1);

    $header[0] = "Authorization: WHM $whmusername:" preg_replace("'(\r|\n)'","",$hash);
    curl_setopt($curl,CURLOPT_HTTPHEADER,$header);

    curl_setopt($curlCURLOPT_URL$query);
    $result curl_exec($curl);
    if (
    $result == false) {
        
    error_log("curl_exec threw error \"" curl_error($curl) . "\" for $query");
    }
    echo 
    $result;


    [
    code]
    I really like to use hashs instead of raw passwords, is there a option similar? can you check for me?

    Thanks!
     
  7. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,678
    Likes Received:
    651
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
Loading...

Share This Page