-
Notifications
You must be signed in to change notification settings - Fork 1
/
rkUsers.php
266 lines (237 loc) · 7.63 KB
/
rkUsers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/php
<?php
//////////////////////////////////////////////////////////////////////////////
// Rubrik Php User Management version 1.0 //
// (c) 2018, 2019 - F. Lhoest //
//////////////////////////////////////////////////////////////////////////////
// Created on OS X with BBEdit //
//////////////////////////////////////////////////////////////////////////////
/* __________ ___. .__ __
\______ \ __ __ \_ |__ _______ |__|| | __
| _/| | \ | __ \ \_ __ \| || |/ /
| | \| | / | \_\ \ | | \/| || <
|____|_ /|____/ |___ / |__| |__||__|_ \
\/ \/ \/
*/
// ----------------------------------------------
// Includes
// ----------------------------------------------
include_once "rkCredentials.php";
include_once "rkFramework.php";
// ==========================
// Local functions
// ==========================
// ----------------------------------------------
function getObscuredText($strMaskChar='*')
{
if(!is_string($strMaskChar) || $strMaskChar=='')
{
$strMaskChar='*';
}
$strMaskChar=substr($strMaskChar,0,1);
readline_callback_handler_install('', function(){});
$strObscured='';
while(true)
{
$strChar = stream_get_contents(STDIN, 1);
$intCount=0;
// Protect against copy and paste passwords
// Comment \/\/\/ to remove password injection protection
$arrRead = array(STDIN);
$arrWrite = NULL;
$arrExcept = NULL;
while (stream_select($arrRead, $arrWrite, $arrExcept, 0,0) && in_array(STDIN, $arrRead))
{
stream_get_contents(STDIN, 1);
$intCount++;
}
// /\/\/\
// End of protection against copy and paste passwords
if($strChar===chr(10))
{
break;
}
if ($intCount===0)
{
if(ord($strChar)===127)
{
if(strlen($strObscured)>0)
{
$strObscured=substr($strObscured,0,strlen($strObscured)-1);
echo(chr(27).chr(91)."D"." ".chr(27).chr(91)."D");
}
}
elseif ($strChar>=' ')
{
$strObscured.=$strChar;
echo($strMaskChar);
//echo(ord($strChar));
}
}
}
readline_callback_handler_remove();
return($strObscured);
}
// ----------------------------------------------
function syntaxError()
{
global $argv;
print("\n\n========================================\n");
print(rkColorRed("Syntax error")." : ".rkColorOutput("min 2 keywords are required!"));
print("\n========================================\n\n");
print($argv[0]." [ACTION] [ClusteName] [UserName]\n");
// print($argv[0]." [cluster_name]\n\n");
print("\tAction can be : add, del, list\n");
// print("\tCluster name can be : BE, UK, US, ...\n\n");
print(rkColorOutput("Example: \t".$argv[0]." add US flhoest")."\n");
print(rkColorOutput("Example: \t".$argv[0]." list BE ")."\n\n");
print("Note : keywords are case insensitive.\n\n");
print("Ended.\n\n");
exit();
}
// ----------------------------------------------
function isStrongPassword($password)
{
// Validate password strength
$uppercase = preg_match('@[A-Z]@', $password);
$lowercase = preg_match('@[a-z]@', $password);
$number = preg_match('@[0-9]@', $password);
$specialChars = preg_match('@[^\w]@', $password);
if(!$uppercase || !$lowercase || !$number || !$specialChars || strlen($password) < 8)
{
return "no";
}
else
{
return "yes";
}
}
// ==============================================
// Main entry point
// ==============================================
if( (count($argv)==3) || (count($argv)==4) )
{
$action=$argv[1];
$cluster=$argv[2];
if(isset($argv[3])) $user=$argv[3];
switch(strtoupper($action))
{
case "ADD":
print("Creating user ".rkColorOutput($user)." in ".rkColorOutput($cluster)."\n");
print("Password : ");
$password=getObscuredText();
// Check if password is strong
if(isStrongPassword($password))
{
// Add user into the $user cluster
// Parse $clusterConnect until "cluster" = $cluster
for($i=0;$i<count($clusterConnect);$i++)
{
if($clusterConnect[$i]["cluster"]==strtoupper($cluster))
{
print("\n");
$connect["ip"]=$clusterConnect[$i]["ip"];
$connect["username"]=$clusterConnect[$i]["username"];
$connect["password"]=$clusterConnect[$i]["password"];
}
}
if(isset($connect))
{
// Cluster found in credential file, adding user now
$res=rkCreateUser($connect,$user,$password);
if(isset($res->id))
{
$newID=$res->id;
print("User has been created (id=".rkColorOutput($newID).")\n");
// Set admin right on user
$res=rkMakeAdminUser($connect,$newID);
if(isset($res->total))
{
print("User has been granted with admin rights.\n");
}
else
{
print("Cannot make user admin at this time.\n");
}
}
else print("User has not been created!\n");
}
else
{
print("\nCluster ".rkColorRed(strtoupper($cluster))." not found!\n");
}
}
break;
case "DEL":
print("deleting user ".rkColorOutput($user)." in ".rkColorOutput(strtoupper($cluster))."\n");
// Get cluster connection info based on requested cluster in command line
for($i=0;$i<count($clusterConnect);$i++)
{
if($clusterConnect[$i]["cluster"]==strtoupper($cluster))
{
$connect["ip"]=$clusterConnect[$i]["ip"];
$connect["username"]=$clusterConnect[$i]["username"];
$connect["password"]=$clusterConnect[$i]["password"];
}
}
if(isset($connect))
{
// Cluster found in credential file, deleting user now
$userID=rkGetUserID($connect,$user);
if($userID==NULL)
{
print("User not found. Exiting.\n");
exit();
}
$res=rkDeleteUser($connect,$userID);
if($res=="OK")
{
print("User has been deleted\n");
}
else print("User has not been deleted!\n");
}
else
{
print("\nCluster ".rkColorRed(strtoupper($cluster))." not found!\n");
}
break;
case "LIST":
print("Listing users in ".rkColorOutput(strtoupper($cluster))."\n");
// Get cluster connection info based on requested cluster in command line
for($i=0;$i<count($clusterConnect);$i++)
{
if($clusterConnect[$i]["cluster"]==strtoupper($cluster))
{
$connect["ip"]=$clusterConnect[$i]["ip"];
$connect["username"]=$clusterConnect[$i]["username"];
$connect["password"]=$clusterConnect[$i]["password"];
}
}
if(isset($connect))
{
// Cluster found in credential file, listing users now
$res=rkGetUsers($connect);
print("\n");
for($i=0;$i<count($res);$i++)
{
print("Username : \t".rkColorOutput($res[$i]->username)."\n");
print("ID : \t\t".rkColorOutput($res[$i]->id)."\n");
print("\n");
}
print("Listing completed.\n");
}
else
{
print("\nCluster ".rkColorRed(strtoupper($cluster))." not found!\n");
}
break;
default:
print("Error, wrong action keyword\n");
syntaxError();
}
}
else
{
syntaxError();
}
?>