Set database user privileges from bash or API1 or API2
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"}}
-
Hello :) We recommend you use our newer UAPI functions for this action: UAPI Functions - Mysql::set_privileges_on_database - Software Development Kit - cPanel Documentation Please let us know if any issues when using UAPI. Thank you. 0 -
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 0 -
I am using curl to log as root in the API1 and API2.
Hello :) You may find this thread helpful if you are using cURL: Accessing CPanel UAPI from command line on Linux / Mac OS Thank you.0 -
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 [code=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($curl, CURLOPT_SSL_VERIFYHOST,0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0); curl_setopt($curl, CURLOPT_RETURNTRANSFER,1); $header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'","",$hash); curl_setopt($curl,CURLOPT_HTTPHEADER,$header); curl_setopt($curl, CURLOPT_URL, $query); $result = curl_exec($curl); if ($result == false) { error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query"); } echo $result;
I really like to use hashs instead of raw passwords, is there a option similar? can you check for me? Thanks!0 -
I really like to use hashs instead of raw passwords, is there a option similar? can you check for me?
Have you considered using the "Single Sign On" feature? It's documented at: Guide to API Authentication - Software Development Kit - cPanel Documentation Thank you.0
Please sign in to leave a comment.
Comments
6 comments