/
upgrade_run_3.0.0_users.php
executable file
·755 lines (655 loc) · 31.5 KB
/
upgrade_run_3.0.0_users.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
<?php
/**
* Teampass - a collaborative passwords manager.
* ---
* 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.
* ---
* @project Teampass
* @file upgrade_run_3.0.0_users.php
* ---
* @author Nils Laumaillé (nils@teampass.net)
* @copyright 2009-2023 Teampass.net
* @license https://spdx.org/licenses/GPL-3.0-only.html#licenseText GPL-3.0
* ---
* @see https://www.teampass.net
*/
set_time_limit(600);
require_once '../sources/SecureHandler.php';
session_name('teampass_session');
session_start();
error_reporting(E_ERROR | E_PARSE);
$_SESSION['db_encoding'] = 'utf8';
$_SESSION['CPM'] = 1;
require_once '../includes/language/english.php';
require_once '../includes/config/include.php';
require_once '../includes/config/settings.php';
require_once '../sources/main.functions.php';
require_once '../includes/config/tp.config.php';
require_once 'tp.functions.php';
require_once 'libs/aesctr.php';
// Prepare POST variables
$post_nb = filter_input(INPUT_POST, 'nb', FILTER_SANITIZE_NUMBER_INT);
$post_start = filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT);
// Load libraries
require_once '../includes/libraries/protect/SuperGlobal/SuperGlobal.php';
$superGlobal = new protect\SuperGlobal\SuperGlobal();
// Some init
$_SESSION['settings']['loaded'] = '';
$finish = false;
$next = ($post_nb + $post_start);
// Test DB connexion
$pass = defuse_return_decrypted(DB_PASSWD);
$server = DB_HOST;
$pre = DB_PREFIX;
$database = DB_NAME;
$port = DB_PORT;
$user = DB_USER;
if (mysqli_connect(
$server,
$user,
$pass,
$database,
$port
)
) {
$db_link = mysqli_connect(
$server,
$user,
$pass,
$database,
$port
);
$db_link->set_charset(DB_ENCODING);
} else {
$res = 'Impossible to get connected to server. Error is: ' . addslashes(mysqli_connect_error());
echo '[{"finish":"1", "error":"Impossible to get connected to server. Error is: ' . addslashes(mysqli_connect_error()) . '!"}]';
mysqli_close($db_link);
exit();
}
$post_step = filter_input(INPUT_POST, 'step', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$post_data = filter_input(INPUT_POST, 'data', FILTER_SANITIZE_FULL_SPECIAL_CHARS, FILTER_FLAG_NO_ENCODE_QUOTES);
$post_number = filter_input(INPUT_POST, 'number', FILTER_SANITIZE_NUMBER_INT);
$post_extra = filter_input(INPUT_POST, 'extra', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$post_tp_user = filter_input(INPUT_POST, 'tp_user_done', FILTER_SANITIZE_NUMBER_INT);
if (null !== $post_step) {
switch ($post_step) {
/*
* CASE
* creating a new user's public/private keys
*/
case 'step1':
// Manage initial case that permit to catch the list of users to treat.
$listOfUsers = array();
if (empty($post_number) === true && $post_step === 'step1' && $post_extra !== 'all_users_created') {
// Count users
$users = mysqli_query(
$db_link,
'SELECT id
FROM ' . $pre . 'users
WHERE (public_key = "none" OR public_key = "" OR public_key IS NULL)
AND id NOT IN (' . OTV_USER_ID . ',' . SSH_USER_ID . ',' . API_USER_ID . ')'
);
while ($user = mysqli_fetch_array($users)) {
array_push($listOfUsers, $user['id']);
}
// User id to treat now
$post_number = $listOfUsers[0];
// Remove this ID from the array
array_shift($listOfUsers);
}
// 3.0.0.23 - special case of TP user just created.
// We need to create all its keys
if ((int) $post_tp_user === 0) {
$TPUserKeysExist = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT count(*)
FROM ' . $pre . 'sharekeys_items
WHERE user_id = ' . (int) TP_USER_ID
)
);
if ((int) $TPUserKeysExist[0] === 0) {
// this user is new and needs to be populated
$userQuery = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT pw, public_key, private_key, name, lastname, login
FROM ' . $pre . 'users
WHERE id = ' . (int) TP_USER_ID
)
);
$usersArray = array(
'id' => TP_USER_ID,
'otp' => $random_string,
'public_key' => $userQuery['public_key'],
'private_key' => $userQuery['private_key'],
'login' => $userQuery['login'],
'name' => $userQuery['name'],
'lastname' => $userQuery['lastname'],
);
// Return
echo '[{"finish":"0" , "next":"step1", "error":"" , "data" : "' . base64_encode(json_encode($usersArray)) . '" , "number":"1" , "loop_finished" : "' . (count($listOfUsers) === 0 ? "true" : "false") . '" , "rest" : "' . base64_encode(json_encode($listOfUsers)) . '"}]';
exit();
}
}
// Treat the 1st user in the list
if (count($listOfUsers) > 0 || (count($listOfUsers) === 0 && empty($post_number) === false && $post_extra !== 'all_users_created')) {
// Get info about user
$userQuery = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT pw, public_key, private_key, name, lastname, login
FROM ' . $pre . 'users
WHERE id = ' . (int) $post_number
)
);
// CHeck if user has already migrated to AES
if (empty($userQuery['public_key']) === true || $userQuery['public_key'] === 'none') {
// Now create his keys pair
// Generate new user password
$small_letters = range('a', 'z');
$big_letters = range('A', 'Z');
$digits = range(0, 9);
$symbols = array('#', '_', '-', '@', '$', '+', '&');
$res = array_merge($small_letters, $big_letters, $digits, $symbols);
$c = count($res);
// first variant
$random_string_lenght = 20;
$random_string = '';
for ($i = 0; $i < $random_string_lenght; ++$i) {
$random_string .= $res[random_int(0, $c - 1)];
}
// Generate keys
$userKeys = generateUserKeys($random_string);
// load passwordLib library
include_once '../sources/SplClassLoader.php';
$pwdlib = new SplClassLoader('PasswordLib', '../includes/libraries');
$pwdlib->register();
$pwdlib = new PasswordLib\PasswordLib();
// Store
mysqli_query(
$db_link,
'UPDATE ' . $pre . "users
SET public_key = '" . $userKeys['public_key'] . "',
private_key = '" . $userKeys['private_key'] . "',
upgrade_needed = 1,
special = 'otc_is_required_on_next_login'
WHERE id = " . $post_number
);
// Remove all sharekeys if exists
mysqli_query(
$db_link,
'DELETE
FROM ' . $pre . 'sharekeys_items
WHERE user_id = ' . (int) $userInfo['id']
);
mysqli_query(
$db_link,
'DELETE
FROM ' . $pre . 'sharekeys_logs
WHERE user_id = ' . (int) $userInfo['id']
);
mysqli_query(
$db_link,
'DELETE
FROM ' . $pre . 'sharekeys_fields
WHERE user_id = ' . (int) $userInfo['id']
);
mysqli_query(
$db_link,
'DELETE
FROM ' . $pre . 'sharekeys_suggestions
WHERE user_id = ' . (int) $userInfo['id']
);
mysqli_query(
$db_link,
'DELETE
FROM ' . $pre . 'sharekeys_files
WHERE user_id = ' . (int) $userInfo['id']
);
$usersArray = array(
'id' => $post_number,
'otp' => $random_string,
'public_key' => $userKeys['public_key'],
'private_key' => $userKeys['private_key'],
'login' => $userQuery['login'],
'name' => $userQuery['name'],
'lastname' => $userQuery['lastname'],
);
}
// Return
echo '[{"finish":"0" , "next":"step1", "error":"" , "data" : "' . base64_encode(json_encode($usersArray)) . '" , "number":"' . ((int) $post_number + 1) . '" , "loop_finished" : "' . (count($listOfUsers) === 0 ? "true" : "false") . '" , "rest" : "' . base64_encode(json_encode($listOfUsers)) . '"}]';
} else {
// No more user to treat
echo '[{"finish":"0" , "next":"step2", "error":"" , "data" : "" , "number":"' . (empty($post_number) === true ? 0 : $post_number) . '" , "loop_finished" : "true" , "rest" : ""}]';
}
exit();
break;
/*
* CASE
* creating user's items keys
*/
case 'step2':
// Prepare post variables
$post_user_info = base64_decode(filter_input(INPUT_POST, 'userInfo', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$post_start = filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT);
$post_count_in_loop = filter_input(INPUT_POST, 'count_in_loop', FILTER_SANITIZE_NUMBER_INT);
// Get POST with user info
$userInfo = json_decode($post_user_info, true);
if ($userInfo['public_key'] === null) {
echo '[{"finish":"1" , "next":"step3", "error":"Public key is null; provided key is '.$post_user_info.'" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "true"}]';
exit();
break;
}
// Get POST with admin info
$post_admin_info = json_decode(base64_decode(filter_input(INPUT_POST, 'info', FILTER_SANITIZE_FULL_SPECIAL_CHARS)));
$adminId = $post_admin_info[2];
$adminPwd = Encryption\Crypt\aesctr::decrypt(base64_decode($post_admin_info[1]), 'cpm', 128);
$adminQuery = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT private_key
FROM ' . $pre . 'users
WHERE id = ' . (int) $adminId
)
);
$adminPrivateKey = decryptPrivateKey($adminPwd, $adminQuery['private_key']);
if ($adminPrivateKey === false) {
echo '[{"finish":"1" , "next":"step3", "error":"Admin PWD is null; provided key is '.$post_admin_info[1].'" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "true"}]';
exit();
break;
}
// Count Items
$db_count = mysqli_fetch_row(
mysqli_query(
$db_link,
'SELECT count(*)
FROM ' . $pre . 'items
WHERE perso = 0'
)
);
//echo $post_start.' - '.$db_count[0];
if ($post_start < $db_count[0]) {
// Get items
$rows = mysqli_query(
$db_link,
'SELECT id, pw, encryption_type
FROM ' . $pre . 'items
WHERE perso = 0
LIMIT ' . $post_start . ', ' . $post_count_in_loop
);
while ($item = mysqli_fetch_array($rows)) {
// Get itemKey for admin
$adminItem = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT share_key
FROM ' . $pre . 'sharekeys_items
WHERE object_id = ' . (int) $item['id'] . ' AND user_id = ' . (int) $adminId
)
);
if ($adminItem !== null) {
// Decrypt itemkey with admin key
$itemKey = decryptUserObjectKey($adminItem['share_key'], $adminPrivateKey);
// Encrypt Item key
if ($userInfo['public_key'] === null) {
$dataTmp = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT public_key
FROM ' . $pre . 'users
WHERE id = ' . (int) $userInfo['id']
)
);
$userInfo['public_key'] = $dataTmp['public_key'];
}
$share_key_for_item = $userInfo['public_key'] !== null ? encryptUserObjectKey($itemKey, $userInfo['public_key']) : '';
// Save the key in DB
mysqli_query(
$db_link,
'INSERT INTO `' . $pre . 'sharekeys_items`(`increment_id`, `object_id`, `user_id`, `share_key`)
VALUES (NULL,' . (int) $item['id'] . ',' . (int) $userInfo['id'] . ",'" . $share_key_for_item . "')"
);
}
}
echo '[{"finish":"0" , "next":"step2", "error":"" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "false"}]';
} else {
echo '[{"finish":"0" , "next":"step3", "error":"" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "true"}]';
}
exit();
break;
/*
* CASE
* creating user's logs keys
*/
case 'step3':
// Prepare post variables
$post_user_info = base64_decode(filter_input(INPUT_POST, 'userInfo', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$post_start = filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT);
$post_count_in_loop = filter_input(INPUT_POST, 'count_in_loop', FILTER_SANITIZE_NUMBER_INT);
// Get POST with user info
$userInfo = json_decode($post_user_info, true);
// Get POST with admin info
$post_admin_info = json_decode(base64_decode(filter_input(INPUT_POST, 'info', FILTER_SANITIZE_FULL_SPECIAL_CHARS)));
$adminId = $post_admin_info[2];
$adminPwd = Encryption\Crypt\aesctr::decrypt(base64_decode($post_admin_info[1]), 'cpm', 128);
$adminQuery = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT private_key
FROM ' . $pre . 'users
WHERE id = ' . (int) $adminId
)
);
$adminPrivateKey = decryptPrivateKey($adminPwd, $adminQuery['private_key']);
// Count Items
$db_count = mysqli_fetch_row(
mysqli_query(
$db_link,
'SELECT increment_id
FROM ' . $pre . "log_items
WHERE raison LIKE 'at_pw :%' AND encryption_type = 'teampass_aes'"
)
);
//echo $post_start.' - '.$db_count[0];
if ($post_start < $db_count[0]) {
// Get items
$rows = mysqli_query(
$db_link,
'SELECT increment_id
FROM ' . $pre . "log_items
WHERE raison LIKE 'at_pw :%' AND encryption_type = 'teampass_aes'
LIMIT " . $post_start . ', ' . $post_count_in_loop
);
while ($item = mysqli_fetch_array($rows)) {
// Get itemKey for admin
$adminItem = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT share_key
FROM ' . $pre . 'sharekeys_logs
WHERE object_id = ' . (int) $item['id'] . ' AND user_id = ' . (int) $adminId
)
);
if ($adminItem !== null) {
// Decrypt itemkey with admin key
$itemKey = decryptUserObjectKey($adminItem['share_key'], $adminPrivateKey);
// Encrypt Item key
$share_key_for_item = $userInfo['public_key'] !== null ? encryptUserObjectKey($itemKey, $userInfo['public_key']) : '';
// Save the key in DB
mysqli_query(
$db_link,
'INSERT INTO `' . $pre . 'sharekeys_logs`(`increment_id`, `object_id`, `user_id`, `share_key`)
VALUES (NULL,' . (int) $item['id'] . ',' . (int) $userInfo['id'] . ",'" . $share_key_for_item . "')"
);
}
}
echo '[{"finish":"0" , "next":"step3", "error":"" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "false"}]';
} else {
echo '[{"finish":"0" , "next":"step4", "error":"" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "true"}]';
}
exit();
break;
/*
* CASE
* creating user's fields keys
*/
case 'step4':
// Prepare post variables
$post_user_info = base64_decode(filter_input(INPUT_POST, 'userInfo', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$post_start = filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT);
$post_count_in_loop = filter_input(INPUT_POST, 'count_in_loop', FILTER_SANITIZE_NUMBER_INT);
// Get POST with user info
$userInfo = json_decode($post_user_info, true);
// Get POST with admin info
$post_admin_info = json_decode(base64_decode(filter_input(INPUT_POST, 'info', FILTER_SANITIZE_FULL_SPECIAL_CHARS)));
$adminId = $post_admin_info[2];
$adminPwd = Encryption\Crypt\aesctr::decrypt(base64_decode($post_admin_info[1]), 'cpm', 128);
$adminQuery = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT private_key
FROM ' . $pre . 'users
WHERE id = ' . (int) $adminId
)
);
$adminPrivateKey = decryptPrivateKey($adminPwd, $adminQuery['private_key']);
// Count Items
$db_count = mysqli_fetch_row(
mysqli_query(
$db_link,
'SELECT id
FROM ' . $pre . 'categories_items'
)
);
if ($post_start < $db_count[0]) {
// Get items
$rows = mysqli_query(
$db_link,
'SELECT id, data, encryption_type
FROM ' . $pre . 'categories_items
WHERE encryption_type = "' . TP_ENCRYPTION_NAME . '"
LIMIT ' . $post_start . ', ' . $post_count_in_loop
);
while ($item = mysqli_fetch_array($rows)) {
// Get itemKey for admin
$adminItem = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT share_key
FROM ' . $pre . 'sharekeys_fields
WHERE object_id = ' . (int) $item['id'] . ' AND user_id = ' . (int) $adminId
)
);
if ($adminItem !== null) {
// Decrypt itemkey with admin key
$itemKey = decryptUserObjectKey($adminItem['share_key'], $adminPrivateKey);
// Encrypt Item key
$share_key_for_item = $userInfo['public_key'] !== null ? encryptUserObjectKey($itemKey, $userInfo['public_key']) : '';
// Save the key in DB
mysqli_query(
$db_link,
'INSERT INTO `' . $pre . 'sharekeys_fields`(`increment_id`, `object_id`, `user_id`, `share_key`)
VALUES (NULL,' . (int) $item['id'] . ',' . (int) $userInfo['id'] . ",'" . $share_key_for_item . "')"
);
}
}
echo '[{"finish":"0" , "next":"step4", "error":"" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "false"}]';
} else {
echo '[{"finish":"0" , "next":"step5", "error":"" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "true"}]';
}
exit();
break;
/*
* CASE
* creating user's Suggestion keys
*/
case 'step5':
// Prepare post variables
$post_user_info = base64_decode(filter_input(INPUT_POST, 'userInfo', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$post_start = filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT);
$post_count_in_loop = filter_input(INPUT_POST, 'count_in_loop', FILTER_SANITIZE_NUMBER_INT);
// Get POST with user info
$userInfo = json_decode($post_user_info, true);
// Get POST with admin info
$post_admin_info = json_decode(base64_decode(filter_input(INPUT_POST, 'info', FILTER_SANITIZE_FULL_SPECIAL_CHARS)));
$adminId = $post_admin_info[2];
$adminPwd = Encryption\Crypt\aesctr::decrypt(base64_decode($post_admin_info[1]), 'cpm', 128);
$adminQuery = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT private_key
FROM ' . $pre . 'users
WHERE id = ' . (int) $adminId
)
);
$adminPrivateKey = decryptPrivateKey($adminPwd, $adminQuery['private_key']);
// Count Items
$db_count = mysqli_fetch_row(
mysqli_query(
$db_link,
'SELECT id
FROM ' . $pre . 'suggestion'
)
);
//echo $post_start.' - '.$db_count[0];
if ($post_start < $db_count[0]) {
// Get items
$rows = mysqli_query(
$db_link,
'SELECT id
FROM ' . $pre . 'suggestion
LIMIT ' . $post_start . ', ' . $post_count_in_loop
);
while ($item = mysqli_fetch_array($rows)) {
// Get itemKey for admin
$adminItem = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT share_key
FROM ' . $pre . 'sharekeys_suggestions
WHERE object_id = ' . (int) $item['id'] . ' AND user_id = ' . (int) $adminId
)
);
if ($adminItem !== null) {
// Decrypt itemkey with admin key
$itemKey = decryptUserObjectKey($adminItem['share_key'], $adminPrivateKey);
// Decrypt password
//$itemPwd = doDataDecryption($item['pw'], $itemKey);
// Encrypt with Password
//$cryptedStuff = doDataEncryption($itemPwd);
// Encrypt Item key
$share_key_for_item = $userInfo['public_key'] !== null ? encryptUserObjectKey($itemKey, $userInfo['public_key']) : '';
// Save the key in DB
mysqli_query(
$db_link,
'INSERT INTO `' . $pre . 'sharekeys_suggestions`(`increment_id`, `object_id`, `user_id`, `share_key`)
VALUES (NULL,' . (int) $item['id'] . ',' . (int) $userInfo['id'] . ",'" . $share_key_for_item . "')"
);
}
}
echo '[{"finish":"0" , "next":"step5", "error":"" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "false"}]';
} else {
echo '[{"finish":"0" , "next":"step6", "error":"" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "true"}]';
}
exit();
break;
/*
* CASE
* creating user's FILES keys
*/
case 'step6':
// Prepare post variables
$post_user_info = base64_decode(filter_input(INPUT_POST, 'userInfo', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
$post_start = filter_input(INPUT_POST, 'start', FILTER_SANITIZE_NUMBER_INT);
$post_count_in_loop = filter_input(INPUT_POST, 'count_in_loop', FILTER_SANITIZE_NUMBER_INT);
// Get POST with user info
$userInfo = json_decode($post_user_info, true);
// Get POST with admin info
$post_admin_info = json_decode(base64_decode(filter_input(INPUT_POST, 'info', FILTER_SANITIZE_FULL_SPECIAL_CHARS)));
$adminId = $post_admin_info[2];
$adminPwd = Encryption\Crypt\aesctr::decrypt(base64_decode($post_admin_info[1]), 'cpm', 128);
$adminQuery = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT private_key
FROM ' . $pre . 'users
WHERE id = ' . (int) $adminId
)
);
$adminPrivateKey = decryptPrivateKey($adminPwd, $adminQuery['private_key']);
// Count Items
$db_count = mysqli_fetch_row(
mysqli_query(
$db_link,
'SELECT id
FROM ' . $pre . 'files'
)
);
if ($post_start < $db_count[0]) {
// Get items
$rows = mysqli_query(
$db_link,
'SELECT id
FROM ' . $pre . 'files
WHERE status = "' . TP_ENCRYPTION_NAME . '"
LIMIT ' . $post_start . ', ' . $post_count_in_loop
);
while ($item = mysqli_fetch_array($rows)) {
// Get itemKey for admin
$adminItem = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT share_key
FROM ' . $pre . 'sharekeys_files
WHERE object_id = ' . (int) $item['id'] . ' AND user_id = ' . (int) $adminId
)
);
if ($adminItem !== null) {
// Decrypt itemkey with admin key
$itemKey = decryptUserObjectKey($adminItem['share_key'], $adminPrivateKey);
// Decrypt password
//$itemPwd = doDataDecryption($item['pw'], $itemKey);
// Encrypt with Password
//$cryptedStuff = doDataEncryption($itemPwd);
// Encrypt Item key
$share_key_for_item = $userInfo['public_key'] !== null ? encryptUserObjectKey($itemKey, $userInfo['public_key']) : '';
// Save the key in DB
mysqli_query(
$db_link,
'INSERT INTO `' . $pre . 'sharekeys_files`(`increment_id`, `object_id`, `user_id`, `share_key`)
VALUES (NULL,' . (int) $item['id'] . ',' . (int) $userInfo['id'] . ",'" . $share_key_for_item . "')"
);
}
}
echo '[{"finish":"0" , "next":"step6", "error":"" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "false"}]';
} else {
echo '[{"finish":"0" , "next":"nextUser", "error":"" , "data" : "" , "number":"' . $post_number . '" , "loop_finished" : "true"}]';
}
exit();
break;
/*
* CASE
* Sending email to user
*/
case 'send_pwd_by_email':
// Prepare post variables
$post_user_info = base64_decode(filter_input(INPUT_POST, 'userInfo', FILTER_SANITIZE_FULL_SPECIAL_CHARS));
// Get POST with user info
$userInfo = json_decode($post_user_info, true);
// Get language
$_SESSION['teampass']['lang'] = include_once '../includes/language/english.php';
// Find user's email
$userEmail = mysqli_fetch_array(
mysqli_query(
$db_link,
'SELECT email, name as userName
FROM ' . $pre . 'users
WHERE id = ' . (int) $userInfo['id']
)
);
if (empty($userEmail['email']) === false) {
// Send email
try {
sendEmail(
'[Teampass] Your One Time Code',
str_replace(
array('#tp_password#'),
array($userInfo['otp']),
'Hello '.$userEmail['userName'].',<br><br>This is a generated email from Teampass passwords manager.<br><br>Teampass administrator has performed an update which includes a change of encryption protocol. During your next login in Teampass, a One Time Code will be asked in order to re-encrypt your data.<br><br>Please use on demand (it is unique for your account):<br><br><b>#tp_password#</b><br><br>This process might take a couple of minutes depending of the number of items existing in the database.<br><br>Cheers'
),
$userEmail['email'],
$SETTINGS,
'',
true
);
} catch (Exception $e) {
console . log(e);
}
}
break;
}
}
echo '[{"finish":"1" , "next":"' . $next . '", "error":""}]';