forked from kselkowitz/rekeyandsync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrekey.php
193 lines (150 loc) · 4.93 KB
/
rekey.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
<?php
define("SERVER", "serverfqdn");
define("SUPERUSER", "username");
define("PASSWORD", "password");
define("CLIENTID", "clientid");
define("CLIENTSECRET", "clientsecret");
define("KEYLENGTH", "10");
define("CONFHOST", "@conference-bridge");
/* First Step is to get a new Access token to given server.*/
$query = array(
'grant_type' => 'password',
'username' => SUPERUSER,
'password' => PASSWORD,
'client_id' => CLIENTID,
'client_secret' => CLIENTSECRET,
);
$postFields = http_build_query($query);
$http_response = "";
$curl_result = __doCurl("https://".SERVER."/ns-api/oauth2/token", CURLOPT_POST, NULL, NULL, $postFields, $http_response);
if (!$curl_result){
header( 'Location: rekey.php?error=server' ) ;
exit;
}
$token = json_decode($curl_result, /*assoc*/true);
if (!isset($token['access_token'])) {
header( 'Location: rekey.php?error=server' ) ;
exit;
}
$token = $token['access_token'];
/* Get domain List */
$query = array(
'object' => 'domain',
'action' => "read",
'format' => 'json',
);
$domainList = __doCurl("https://".SERVER."/ns-api/", CURLOPT_POST, "Authorization: Bearer " . $token, $query, null, $http_response);
$domainList = json_decode($domainList,true);
// print domain selection menu
echo "<form><select name=\"domain\">";
foreach ($domainList as $array) {
echo "<option value=\"$array[domain]\">". $array[domain] . "</option>";
}
echo "</select><input type=\"submit\" value=\"Submit\"></form>";
// error on oauth
if (isset($_REQUEST['error'])){
if ($_REQUEST['error'] == "server")
echo 'check the define statements in the file for correct data!';
}
// form was submitted with domain
if (isset($_REQUEST['domain'])){
$resetDomain = $_REQUEST['domain'];
echo 'Processing key reset and resync for domain ' . $resetDomain;
/* get devices for domain */
$query = array(
'object' => 'device',
'action' => 'read',
'format' => 'json',
'noNDP' => 'true',
'domain' => "$resetDomain",
);
$deviceList = __doCurl("https://".SERVER."/ns-api/", CURLOPT_POST, "Authorization: Bearer " . $token, $query, null, $http_response);
$deviceList = json_decode($deviceList,true);
foreach ($deviceList as $array) {
//echo $array[aor].'<br>';
// reset device password and resync
$aor = $array[aor];
$newPass = substr(md5(uniqid(rand())),0, KEYLENGTH);
// check if conf bridge
if(stristr($aor, CONFHOST) === FALSE) {
// not a bridge
// resync device
if(stristr($aor, "Yealink") === FALSE)
{
$query = array(
'object' => 'device',
'action' => 'update',
'device' => "$aor",
'check-sync' => "yes",
);
}
else
{
$query = array(
'object' => 'device',
'action' => 'update',
'device' => "$aor",
'check-sync' => "yes",
'evtCheckSync' => "check-sync;reboot=true",
);
}
__doCurl("https://".SERVER."/ns-api/", CURLOPT_POST, "Authorization: Bearer " . $token, $query, null, $http_response);
sleep(2);
// change password
$query = array(
'object' => 'device',
'action' => 'update',
'device' => "$aor",
'authentication_key' => "$newPass",
'domain' => "$resetDomain",
);
__doCurl("https://".SERVER."/ns-api/", CURLOPT_POST, "Authorization: Bearer " . $token, $query, null, $http_response);
echo "<br>reset and resync $aor";
}
else
{
echo "<br>skipping bridge $aor";
}
}
}
function __doCurl($url, $method, $authorization, $query, $postFields, &$http_response)
{
$start= microtime(true);
$curl_options = array(
CURLOPT_URL => $url . ($query ? '?' . http_build_query($query) : ''),
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FORBID_REUSE => true,
CURLOPT_TIMEOUT => 60
);
$headers = array();
if ($authorization != NULL)
{
if ("bus:bus" == $authorization)
$curl_options[CURLOPT_USERPWD]=$authorization;
else
$headers[$authorization]=$authorization;
}
$curl_options[$method] = true;
if ($postFields != NULL )
{
$curl_options[CURLOPT_POSTFIELDS] = $postFields;
}
if (sizeof($headers)>0)
$curl_options[CURLOPT_HTTPHEADER] = $headers;
$curl_handle = curl_init();
curl_setopt_array($curl_handle, $curl_options);
$curl_result = curl_exec($curl_handle);
$http_response = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
//print_r($http_response);
curl_close($curl_handle);
$end = microtime(true);
if (!$curl_result)
return NULL;
else if ($http_response >= 400)
return NULL;
else
return $curl_result;
}
?>