/
functions.php
326 lines (286 loc) · 10.6 KB
/
functions.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<?php
/**
*
* @file configapi.php
* @author Nils Laumaillé
* @version 2.1.20
* @copyright (c) 2009-2014 Nils Laumaillé
* @licensing GNU AFFERO GPL 3.0
* @link http://www.teampass.net
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
$teampass_config_file = "../includes/settings.php";
function teampass_api_enabled() {
$bdd = teampass_connect();
$response = $bdd->query("select valeur from ".$GLOBALS['pre']."misc WHERE type = 'admin' AND intitule = 'api'");
return $response->fetch(PDO::FETCH_ASSOC);
}
function teampass_whitelist() {
$bdd = teampass_connect();
$apiip_pool = teampass_get_ips();
if (count($apiip_pool) > 0 && array_search($_SERVER['REMOTE_ADDR'], $apiip_pool) === false) {
rest_error('IPWHITELIST');
}
}
function teampass_connect() {
require_once($GLOBALS['teampass_config_file']);
try
{
$bdd = new PDO("mysql:host=".$GLOBALS['server'].";dbname=".$GLOBALS['database'], $GLOBALS['user'], $GLOBALS['pass']);
return ($bdd);
}
catch (Exception $e)
{
rest_error('MYSQLERR', 'Error : ' . $e->getMessage());
}
}
function teampass_get_ips() {
$array_of_results = array();
$bdd = teampass_connect();
$response = $bdd->query("select value from ".$GLOBALS['pre']."api WHERE type = 'ip'");
while ($data = $response->fetch())
{
array_push($array_of_results, $data['value']);
}
return $array_of_results;
}
function teampass_get_keys() {
$bdd = teampass_connect();
$response = $bdd->query("select value from ".$GLOBALS['pre']."api WHERE type = 'key'");
return $response->fetch(PDO::FETCH_ASSOC);
}
function teampass_get_randkey() {
$bdd = teampass_connect();
$response = $bdd->query("select rand_key from ".$GLOBALS['pre']."keys limit 0,1");
$array = $response->fetch(PDO::FETCH_OBJ);
return $array->rand_key;
}
function rest_head () {
header('HTTP/1.1 402 Payment Required');
}
function rest_delete () {
if(apikey_checker($GLOBALS['apikey'])) {
$bdd = teampass_connect();
$rand_key = teampass_get_randkey();
if ($GLOBALS['request'][0] == "write") {
if($GLOBALS['request'][1] == "category") {
$array_category = explode(';',$GLOBALS['request'][2]);
foreach($array_category as $category) {
if(!preg_match_all("/^([\w\:\'\-\sàáâãäåçèéêëìíîïðòóôõöùúûüýÿ]+)$/i", $category,$result)) {
rest_error('CATEGORY_MALFORMED');
}
}
if(count($array_category) > 1 && count($array_category) < 5) {
for ($i = count($array_category); $i > 0; $i--) {
$slot = $i - 1;
if (!$slot) {
$category_query .= "select id from ".$GLOBALS['pre']."nested_tree where title LIKE '".$array_category[$slot]."' AND parent_id = 0";
} else {
$category_query .= "select id from ".$GLOBALS['pre']."nested_tree where title LIKE '".$array_category[$slot]."' AND parent_id = (";
}
}
for ($i = 1; $i < count($array_category); $i++) { $category_query .= ")"; }
} elseif (count($array_category) == 1) {
$category_query = "select id from ".$GLOBALS['pre']."nested_tree where title LIKE '".$array_category[0]."' AND parent_id = 0";
} else {
rest_error ('NO_CATEGORY');
}
// Delete items which in category
$response = $bdd->query("delete from ".$GLOBALS['pre']."items where id_tree = (".$category_query.")");
// Delete sub-categories which in category
$response = $bdd->query("delete from ".$GLOBALS['pre']."nested_tree where parent_id = (".$category_query.")");
// Delete category
$response = $bdd->query("delete from ".$GLOBALS['pre']."nested_tree where id = (".$category_query.")");
$json['type'] = 'category';
$json['category'] = $GLOBALS['request'][2];
if($response) {
$json['status'] = 'OK';
} else {
$json['status'] = 'KO';
}
} elseif($GLOBALS['request'][1] == "item") {
$array_category = explode(';',$GLOBALS['request'][2]);
$item = $GLOBALS['request'][3];
foreach($array_category as $category) {
if(!preg_match_all("/^([\w\:\'\-\sàáâãäåçèéêëìíîïðòóôõöùúûüýÿ]+)$/i", $category,$result)) {
rest_error('CATEGORY_MALFORMED');
}
}
if(!preg_match_all("/^([\w\:\'\-\sàáâãäåçèéêëìíîïðòóôõöùúûüýÿ]+)$/i", $item,$result)) {
rest_error('ITEM_MALFORMED');
} elseif (empty($item) || count($array_category) == 0) {
rest_error('MALFORMED');
}
if(count($array_category) > 1 && count($array_category) < 5) {
for ($i = count($array_category); $i > 0; $i--) {
$slot = $i - 1;
if (!$slot) {
$category_query .= "select id from ".$GLOBALS['pre']."nested_tree where title LIKE '".$array_category[$slot]."' AND parent_id = 0";
} else {
$category_query .= "select id from ".$GLOBALS['pre']."nested_tree where title LIKE '".$array_category[$slot]."' AND parent_id = (";
}
}
for ($i = 1; $i < count($array_category); $i++) { $category_query .= ")"; }
} elseif (count($array_category) == 1) {
$category_query = "select id from ".$GLOBALS['pre']."nested_tree where title LIKE '".$array_category[0]."' AND parent_id = 0";
} else {
rest_error ('NO_CATEGORY');
}
// Delete item
$response = $bdd->query("delete from ".$GLOBALS['pre']."items where id_tree = (".$category_query.") and label LIKE '".$item."'");
$json['type'] = 'item';
$json['item'] = $item;
$json['category'] = $GLOBALS['request'][2];
if($response) {
$json['status'] = 'OK';
} else {
$json['status'] = 'KO';
}
}
if ($json) {
echo json_encode($json);
} else {
rest_error ('EMPTY');
}
} else {
rest_error ('METHOD');
}
}
}
function rest_get () {
if(apikey_checker($GLOBALS['apikey'])) {
$bdd = teampass_connect();
$rand_key = teampass_get_randkey();
if ($GLOBALS['request'][0] == "read") {
if($GLOBALS['request'][1] == "category") {
// get ids
if (strpos($GLOBALS['request'][2],",") > 0) {
$condition = "id_tree IN (".$GLOBALS['request'][2].")";
} else {
$condition = "id_tree = '".$GLOBALS['request'][2]."'";
}
$response = $bdd->query("select id,label,login,pw from ".$GLOBALS['pre']."items where ".$condition);
while ($data = $response->fetch())
{
$id = $data['id'];
$json[$id]['label'] = utf8_encode($data['label']);
$json[$id]['login'] = utf8_encode($data['login']);
$json[$id]['pw'] = teampass_decrypt_pw($data['pw'],SALT,$rand_key);
}
} elseif($GLOBALS['request'][1] == "item") {
$array_category = explode(';',$GLOBALS['request'][2]);
$item = $GLOBALS['request'][3];
foreach($array_category as $category) {
if(!preg_match_all("/^([\w\:\'\-\sàáâãäåçèéêëìíîïðòóôõöùúûüýÿ]+)$/i", $category,$result)) {
rest_error('CATEGORY_MALFORMED');
}
}
if(!preg_match_all("/^([\w\:\'\-\sàáâãäåçèéêëìíîïðòóôõöùúûüýÿ]+)$/i", $item,$result)) {
rest_error('ITEM_MALFORMED');
} elseif (empty($item) || count($array_category) == 0) {
rest_error('MALFORMED');
}
if(count($array_category) > 1 && count($array_category) < 5) {
for ($i = count($array_category); $i > 0; $i--) {
$slot = $i - 1;
if (!$slot) {
$category_query .= "select id from ".$GLOBALS['pre']."nested_tree where title LIKE '".$array_category[$slot]."' AND parent_id = 0";
} else {
$category_query .= "select id from ".$GLOBALS['pre']."nested_tree where title LIKE '".$array_category[$slot]."' AND parent_id = (";
}
}
for ($i = 1; $i < count($array_category); $i++) { $category_query .= ")"; }
} elseif (count($array_category) == 1) {
$category_query = "select id from ".$GLOBALS['pre']."nested_tree where title LIKE '".$array_category[0]."' AND parent_id = 0";
} else {
rest_error ('NO_CATEGORY');
}
$response = $bdd->query("select id,label,login,pw,id_tree from ".$GLOBALS['pre']."items where id_tree = (".$category_query.") and label LIKE '".$item."'");
while ($data = $response->fetch())
{
$id = $data['id'];
$json[$id]['label'] = utf8_encode($data['label']);
$json[$id]['login'] = utf8_encode($data['login']);
$json[$id]['pw'] = teampass_decrypt_pw($data['pw'],SALT,$rand_key);
}
}
if (isset($json) && $json) {
echo json_encode($json);
} else {
rest_error ('EMPTY');
}
} else {
rest_error ('METHOD');
}
}
}
function rest_error ($type,$detail = 'N/A') {
switch ($type) {
case 'APIKEY':
$message = Array('err' => 'This api_key '.$GLOBALS['apikey'].' doesn\'t exist');
header('HTTP/1.1 405 Method Not Allowed');
break;
case 'NO_CATEGORY':
$message = Array('err' => 'No category specified');
break;
case 'EMPTY':
$message = Array('err' => 'No results');
break;
case 'IPWHITELIST':
$message = Array('err' => 'Ip address '.$_SERVER['REMOTE_ADDR'].' not allowed.');
header('HTTP/1.1 405 Method Not Allowed');
break;
case 'MYSQLERR':
$message = Array('err' => $detail);
header('HTTP/1.1 500 Internal Server Error');
break;
case 'METHOD':
$message = Array('err' => 'Method not authorized');
header('HTTP/1.1 405 Method Not Allowed');
break;
default:
$message = Array('err' => 'Something happen ... but what ?');
header('HTTP/1.1 500 Internal Server Error');
break;
}
echo json_encode($message);
exit(0);
}
function apikey_checker ($apikey_used) {
$bdd = teampass_connect();
$apikey_pool = teampass_get_keys();
if (in_array($apikey_used, $apikey_pool)) {
return(1);
} else {
rest_error('APIKEY',$apikey_used);
}
}
function teampass_pbkdf2_hash($p, $s, $c, $kl, $st = 0, $a = 'sha256')
{
$kb = $st + $kl;
$dk = '';
for ($block = 1; $block <= $kb; $block++) {
$ib = $h = hash_hmac($a, $s . pack('N', $block), $p, true);
for ($i = 1; $i < $c; $i++) {
$ib ^= ($h = hash_hmac($a, $h, $p, true));
}
$dk .= $ib;
}
return substr($dk, $st, $kl);
}
function teampass_decrypt_pw($encrypted, $salt, $rand_key, $itcount = 2072)
{
$encrypted = base64_decode($encrypted);
$pass_salt = substr($encrypted, -64);
$encrypted = substr($encrypted, 0, -64);
$key = teampass_pbkdf2_hash($salt, $pass_salt, $itcount, 16, 32);
$iv = base64_decode(substr($encrypted, 0, 43) . '==');
$encrypted = substr($encrypted, 43);
$mac = substr($encrypted, -64);
$encrypted = substr($encrypted, 0, -64);
if ($mac !== hash_hmac('sha256', $encrypted, $salt)) return null;
return substr(rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $key, $encrypted, 'ctr', $iv), "\0\4"), strlen($rand_key));
}