forked from Cacti/cacti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_admin.php
3054 lines (2587 loc) · 98.4 KB
/
user_admin.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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2017 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program 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. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
include('./include/auth.php');
$user_actions = array(
1 => __('Delete'),
2 => __('Copy'),
3 => __('Enable'),
4 => __('Disable'),
5 => __('Batch Copy')
);
set_default_action();
if (isset_request_var('update_policy')) {
update_policies();
} else {
switch (get_request_var('action')) {
case 'actions':
form_actions();
break;
case 'save':
form_save();
break;
case 'perm_remove':
perm_remove();
break;
case 'user_edit':
top_header();
user_edit();
bottom_footer();
break;
case 'checkpass':
$error = secpass_check_pass(get_nfilter_request_var('password'));
if ($error == '') {
print $error;
} else {
print 'ok';
}
break;
default:
if (!api_plugin_hook_function('user_admin_action', get_request_var('action'))) {
top_header();
user();
bottom_footer();
}
break;
}
}
/* --------------------------
Actions Function
-------------------------- */
function update_policies() {
$set = '';
$set .= isset_request_var('policy_graphs') ? 'policy_graphs=' . get_nfilter_request_var('policy_graphs'):'';
$set .= isset_request_var('policy_trees') ? ($set != '' ? ',':'') . 'policy_trees=' . get_nfilter_request_var('policy_trees'):'';
$set .= isset_request_var('policy_hosts') ? ($set != '' ? ',':'') . 'policy_hosts=' . get_nfilter_request_var('policy_hosts'):'';
$set .= isset_request_var('policy_graph_templates') ? ($set != '' ? ',':'') . 'policy_graph_templates=' . get_nfilter_request_var('policy_graph_templates'):'';
if ($set != '') {
db_execute_prepared("UPDATE user_auth SET $set WHERE id = ?", array(get_nfilter_request_var('id')));
}
header('Location: user_admin.php?action=user_edit&header=false&tab=' . get_nfilter_request_var('tab') . '&id=' . get_nfilter_request_var('id'));
exit;
}
function form_actions() {
global $user_actions, $auth_realms;
/* if we are to save this form, instead of display it */
if (isset_request_var('associate_host')) {
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1]);
/* ==================================================== */
if (get_nfilter_request_var('drp_action') == '1') {
db_execute_prepared('REPLACE INTO user_auth_perms
(user_id, item_id, type)
VALUES (?, ?, 3)',
array(get_nfilter_request_var('id'), $matches[1]));
} else {
db_execute_prepared('DELETE FROM user_auth_perms
WHERE user_id = ?
AND item_id = ?
AND type = 3',
array(get_nfilter_request_var('id'), $matches[1]));
}
}
}
header('Location: user_admin.php?action=user_edit&header=false&tab=permsd&id=' . get_nfilter_request_var('id'));
exit;
} elseif (isset_request_var('associate_graph')) {
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1]);
/* ==================================================== */
if (get_nfilter_request_var('drp_action') == '1') {
db_execute_prepared('REPLACE INTO user_auth_perms
(user_id, item_id, type)
VALUES (?, ?, 1)',
array(get_nfilter_request_var('id'), $matches[1]));
} else {
db_execute_prepared('DELETE FROM user_auth_perms
WHERE user_id = ?
AND item_id = ?
AND type = 1',
array(get_nfilter_request_var('id'), $matches[1]));
}
}
}
header('Location: user_admin.php?action=user_edit&header=false&tab=permsg&id=' . get_nfilter_request_var('id'));
exit;
} elseif (isset_request_var('associate_template')) {
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1]);
/* ==================================================== */
if (get_nfilter_request_var('drp_action') == '1') {
db_execute_prepared('REPLACE INTO user_auth_perms
(user_id, item_id, type)
VALUES (?, ?, 4)',
array(get_nfilter_request_var('id'), $matches[1]));
} else {
db_execute_prepared('DELETE FROM user_auth_perms
WHERE user_id = ?
AND item_id = ?
AND type = 4',
array(get_nfilter_request_var('id'), $matches[1]));
}
}
}
header('Location: user_admin.php?action=user_edit&header=false&tab=permste&id=' . get_nfilter_request_var('id'));
exit;
} elseif (isset_request_var('associate_groups')) {
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1]);
/* ==================================================== */
if (get_nfilter_request_var('drp_action') == '1') {
db_execute_prepared('REPLACE INTO user_auth_group_members
(user_id, group_id)
VALUES (?, ?)',
array(get_nfilter_request_var('id'), $matches[1]));
} else {
db_execute_prepared('DELETE FROM user_auth_group_members
WHERE user_id = ?
AND group_id = ?',
array(get_nfilter_request_var('id'), $matches[1]));
}
}
}
header('Location: user_admin.php?action=user_edit&header=false&tab=permsgr&id=' . get_nfilter_request_var('id'));
exit;
} elseif (isset_request_var('associate_tree')) {
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1]);
/* ==================================================== */
if (get_nfilter_request_var('drp_action') == '1') {
db_execute_prepared('REPLACE INTO user_auth_perms
(user_id, item_id, type)
VALUES (?, ?, 2)',
array(get_nfilter_request_var('id'), $matches[1]));
} else {
db_execute_prepared('DELETE FROM user_auth_perms
WHERE user_id = ?
AND item_id = ?
AND type = 2',
array(get_nfilter_request_var('id'), $matches[1]));
}
}
}
header('Location: user_admin.php?action=user_edit&header=false&tab=permstr&id=' . get_nfilter_request_var('id'));
exit;
} elseif (isset_request_var('selected_items')) {
if (get_nfilter_request_var('drp_action') == '2') { /* copy */
/* ================= input validation ================= */
get_filter_request_var('selected_items');
get_filter_request_var('new_realm');
/* ==================================================== */
$new_username = get_nfilter_request_var('new_username');
$new_realm = get_nfilter_request_var('new_realm', 0);
$template_user = db_fetch_row_prepared('SELECT username, realm
FROM user_auth
WHERE id = ?',
array(get_nfilter_request_var('selected_items')));
$overwrite = array( 'full_name' => get_nfilter_request_var('new_fullname') );
if ($new_username != '') {
if (sizeof(db_fetch_assoc_prepared('SELECT username FROM user_auth WHERE username = ? AND realm = ?', array($new_username, $new_realm)))) {
raise_message(19);
} else {
if (user_copy($template_user['username'], $new_username, $template_user['realm'], $new_realm, false, $overwrite) === false) {
raise_message(2);
} else {
raise_message(1);
}
}
}
} else {
$selected_items = sanitize_unserialize_selected_items(get_nfilter_request_var('selected_items'));
if ($selected_items != false) {
if (get_nfilter_request_var('drp_action') == '1') { // delete
for ($i=0;($i<count($selected_items));$i++) {
user_remove($selected_items[$i]);
api_plugin_hook_function('user_remove', $selected_items[$i]);
}
} elseif (get_nfilter_request_var('drp_action') == '3') { // enable
for ($i=0;($i<count($selected_items));$i++) {
user_enable($selected_items[$i]);
}
} elseif (get_nfilter_request_var('drp_action') == '4') { // disable
for ($i=0;($i<count($selected_items));$i++) {
user_disable($selected_items[$i]);
}
} elseif (get_nfilter_request_var('drp_action') == '5') { // batch copy
/* ================= input validation ================= */
get_filter_request_var('template_user');
/* ==================================================== */
$copy_error = false;
$template = db_fetch_row_prepared('SELECT username, realm
FROM user_auth
WHERE id = ?',
array(get_nfilter_request_var('template_user')));
for ($i=0;($i<count($selected_items));$i++) {
$user = db_fetch_row_prepared('SELECT username, realm
FROM user_auth
WHERE id = ?',
array($selected_items[$i]));
if ((isset($user)) && (isset($template))) {
if (user_copy($template['username'], $user['username'], $template['realm'], $user['realm'], true) === false) {
$copy_error = true;
}
}
}
if ($copy_error) {
raise_message(2);
} else {
raise_message(1);
}
}
}
}
header('Location: user_admin.php?header=false');
exit;
}
/* loop through each of the users and process them */
$user_list = '';
$user_array = array();
$i = 0;
foreach ($_POST as $var => $val) {
if (preg_match('/^chk_([0-9]+)$/', $var, $matches)) {
/* ================= input validation ================= */
input_validate_input_number($matches[1]);
/* ==================================================== */
if (get_nfilter_request_var('drp_action') != '2') {
$user_list .= '<li>' . htmlspecialchars(db_fetch_cell_prepared('SELECT username FROM user_auth WHERE id = ?', array($matches[1]))) . '</li>';
}
$user_array[$i] = $matches[1];
$i++;
}
}
top_header();
form_start('user_admin.php');
html_start_box($user_actions[get_nfilter_request_var('drp_action')], '40%', '', '3', 'center', '');
if (isset($user_array) && sizeof($user_array)) {
if ((get_nfilter_request_var('drp_action') == '1') && (sizeof($user_array))) { // delete
print "<tr>
<td class='textArea'>
<p>" . __('Click \'Continue\' to delete the selected User(s).') . "</p>
<div class='itemlist'><ul>$user_list</ul></div>
</td>
</tr>\n";
$save_html = "<input type='button' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'><input type='submit' value='" . __esc('Continue') . "' title='" . __esc('Delete User(s)') . "'>";
}
$user_id = '';
if ((get_nfilter_request_var('drp_action') == '2') && (sizeof($user_array))) { // copy
$user_id = $user_array[0];
$user_realm = db_fetch_cell_prepared('SELECT realm FROM user_auth WHERE id = ?', array($user_id));
print "<tr>
<td class='textArea'>
<p>" . __('Click \'Continue\' to copy the selected User to a new User below.') . "</p>
</td>
</tr>
<tr>
<td class='textArea'>
<p>" . __('Template Username:') . " <i>" . htmlspecialchars(db_fetch_cell_prepared('SELECT username FROM user_auth WHERE id = ?', array($user_id))) . "</i></p>
</td>
</tr>
<tr>
<td class='textArea'>
<p>" . __('Username:') . " ";
print form_text_box('new_username', '', '', 25);
print "</p></td>
</tr>
<tr>
<td class='textArea'>
<p>" . __('Full Name:') . " ";
print form_text_box('new_fullname', '', '', 35);
print "</p></td>
</tr>
<tr>
<td class='textArea'>
<p>" . __('Realm:') ." ";
print form_dropdown('new_realm', $auth_realms, '', '', $user_realm, '', 0);
print "</p></td>
</tr>\n";
$save_html = "<input type='button' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' value='" . __esc('Continue') . "' title='" . __esc('Copy User') . "'>";
}
if ((get_nfilter_request_var('drp_action') == '3') && (sizeof($user_array))) { // enable
print "<tr>
<td class='textArea'>
<p>" . __('Click \'Continue\' to enable the selected User(s).'). "</p>
<div class='itemlist'><ul>$user_list</ul></div>
</td>
</tr>\n";
$save_html = "<input type='button' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' value='" . __esc('Continue') . "' title='" . __esc('Enable User(s)') . "'>";
}
if ((get_nfilter_request_var('drp_action') == '4') && (sizeof($user_array))) { // disable
print "<tr>
<td class='textArea'>
<p>" . __('Click \'Continue\' to disable the selected User(s).') . "</p>
<div class='itemlist'><ul>$user_list</ul></div>
</td>
</tr>\n";
$save_html = "<input type='button' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' value='" . __esc('Continue') . "' title='" . __esc('Disable User(s)') . "'>";
}
if ((get_nfilter_request_var('drp_action') == '5') && (sizeof($user_array))) { // batch copy
$usernames = db_fetch_assoc('SELECT id, username FROM user_auth WHERE realm = 0 ORDER BY username');
print "<tr>
<td class='textArea'>
<p>" . __('Click \'Continue\' to overwrite the User(s) settings with the selected template User settings and permissions. The original users Full Name, Password, Realm and Enable status will be retained, all other fields will be overwritten from Template User.') . "<br><br></td>
</tr><tr>
<td class='textArea'>
<p>" . __('Template User:') . " ";
print form_dropdown('template_user', $usernames, 'username', 'id', '', '', 0);
print "</p></td>
</tr><tr>
<td class='textArea'>
<p>" . __('User(s) to update:') . "</p>
<p><ul>$user_list</ul></p>
</td>
</tr>\n";
$save_html = "<input type='button' value='" . __esc('Cancel') . "' onClick='cactiReturnTo()'> <input type='submit' value='" . __esc('Continue') . "' title='" . __esc('Reset User(s) Settings') . "'>";
}
} else {
print "<tr><td class='even'><span class='textError'>" . __('You must select at least one user.') . "</span></td></tr>\n";
$save_html = "<input type='button' value='" . __esc('Return') . "' onClick='cactiReturnTo()'>";
}
print "<tr>
<td class='saveRow'>
<input type='hidden' name='action' value='actions'>";
if (get_nfilter_request_var('drp_action') == '2') { // copy
print "<input type='hidden' name='selected_items' value='" . $user_id . "'>\n";
} else {
print "<input type='hidden' name='selected_items' value='" . (isset($user_array) ? serialize($user_array) : '') . "'>\n";
}
print "<input type='hidden' name='drp_action' value='" . get_nfilter_request_var('drp_action') . "'>
$save_html
</td>
</tr>\n";
html_end_box();
form_end();
bottom_footer();
}
/* --------------------------
Save Function
-------------------------- */
function form_save() {
global $settings_user;
/* graph permissions */
if ((isset_request_var('save_component_graph_perms')) && (!is_error_message())) {
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('perm_graphs');
get_filter_request_var('perm_trees');
get_filter_request_var('perm_hosts');
get_filter_request_var('perm_graph_templates');
get_filter_request_var('policy_graphs');
get_filter_request_var('policy_trees');
get_filter_request_var('policy_hosts');
get_filter_request_var('policy_graph_templates');
/* ==================================================== */
$add_button_clicked = false;
if (isset_request_var('add_graph_x')) {
db_execute_prepared('REPLACE INTO user_auth_perms
(user_id,item_id,type)
VALUES (?, ?, 1)',
array(get_nfilter_request_var('id'), get_nfilter_request_var('perm_graphs')));
$add_button_clicked = true;
} elseif (isset_request_var('add_tree_x')) {
db_execute_prepared('REPLACE INTO user_auth_perms
(user_id,item_id,type)
VALUES (?, ?, 2)',
array(get_nfilter_request_var('id'), get_nfilter_request_var('perm_trees')));
$add_button_clicked = true;
} elseif (isset_request_var('add_host_x')) {
db_execute_prepared('REPLACE INTO user_auth_perms
(user_id,item_id,type)
VALUES (?, ?, 3)',
array(get_nfilter_request_var('id'), get_nfilter_request_var('perm_hosts')));
$add_button_clicked = true;
} elseif (isset_request_var('add_graph_template_x')) {
db_execute_prepared('REPLACE INTO user_auth_perms
(user_id,item_id,type)
VALUES (?, ?, 4)',
array(get_nfilter_request_var('id'), get_nfilter_request_var('perm_graph_templates')));
$add_button_clicked = true;
}
if ($add_button_clicked == true) {
header('Location: user_admin.php?action=user_edit&header=false&tab=graph_perms_edit&id=' . get_nfilter_request_var('id'));
exit;
}
} elseif (isset_request_var('save_component_user')) {
/* user management save */
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('realm');
get_filter_request_var('policy_hosts');
get_filter_request_var('policy_graphs');
get_filter_request_var('policy_trees');
get_filter_request_var('policy_graph_templates');
/* ==================================================== */
if ((get_nfilter_request_var('password') == '') && (get_nfilter_request_var('password_confirm') == '')) {
$password = db_fetch_cell_prepared('SELECT password
FROM user_auth
WHERE id = ?',
array(get_nfilter_request_var('id')));
} elseif (function_exists('password_hash')) {
$password = password_hash(get_nfilter_request_var('password'), PASSWORD_DEFAULT);
} else {
$password = md5(get_nfilter_request_var('password'));
}
/* check duplicate username */
if (sizeof(db_fetch_row_prepared('SELECT * FROM user_auth WHERE realm = ? AND username = ? AND id != ?', array(get_nfilter_request_var('realm'), get_nfilter_request_var('username'), get_nfilter_request_var('id'))))) {
raise_message(12);
}
/* check for guest or template user */
$username = db_fetch_cell_prepared('SELECT username FROM user_auth WHERE id = ?', array(get_nfilter_request_var('id')));
$history = db_fetch_cell_prepared('SELECT password_history FROM user_auth WHERE id = ?', array(get_nfilter_request_var('id')));
if ($username != '' && $username != get_nfilter_request_var('username')) {
$template_user = read_config_option('user_template');
$guest_user = read_config_option('guest_user');
if ($username == $template_user) {
raise_message(20);
}
if ($username == $guest_user) {
raise_message(20);
}
}
/* check to make sure the passwords match; if not error */
if (get_nfilter_request_var('password') != get_nfilter_request_var('password_confirm')) {
raise_message(4);
}
if (get_nfilter_request_var('must_change_password') == 'on' && get_nfilter_request_var('password_change') != 'on') {
raise_message('password_change');
}
$save['id'] = get_nfilter_request_var('id');
$save['username'] = form_input_validate(get_nfilter_request_var('username'), 'username', "^[A-Za-z0-9\._\\\@\ -]+$", false, 3);
$save['full_name'] = form_input_validate(get_nfilter_request_var('full_name'), 'full_name', '', true, 3);
$save['password'] = $password;
$save['must_change_password'] = form_input_validate(get_nfilter_request_var('must_change_password', ''), 'must_change_password', '', true, 3);
$save['password_change'] = form_input_validate(get_nfilter_request_var('password_change', ''), 'password_change', '', true, 3);
$save['show_tree'] = form_input_validate(get_nfilter_request_var('show_tree', ''), 'show_tree', '', true, 3);
$save['show_list'] = form_input_validate(get_nfilter_request_var('show_list', ''), 'show_list', '', true, 3);
$save['show_preview'] = form_input_validate(get_nfilter_request_var('show_preview', ''), 'show_preview', '', true, 3);
$save['graph_settings'] = form_input_validate(get_nfilter_request_var('graph_settings', ''), 'graph_settings', '', true, 3);
$save['login_opts'] = form_input_validate(get_nfilter_request_var('login_opts'), 'login_opts', '', true, 3);
$save['realm'] = get_nfilter_request_var('realm', 0);
$save['password_history'] = $history;
$save['enabled'] = form_input_validate(get_nfilter_request_var('enabled', ''), 'enabled', '', true, 3);
$save['email_address'] = form_input_validate(get_nfilter_request_var('email_address', ''), 'email_address', '', true, 3);
$save['locked'] = form_input_validate(get_nfilter_request_var('locked', ''), 'locked', '', true, 3);
$save['reset_perms'] = mt_rand();
if ($save['locked'] == '') {
$save['failed_attempts'] = 0;
}
$save = api_plugin_hook_function('user_admin_setup_sql_save', $save);
if (!is_error_message()) {
$user_id = sql_save($save, 'user_auth');
if ($user_id) {
raise_message(1);
} else {
raise_message(2);
}
}
} elseif (isset_request_var('save_component_realm_perms')) {
db_execute_prepared('DELETE FROM user_auth_realm WHERE user_id = ?', array(get_nfilter_request_var('id')));
foreach ($_POST as $var => $val) {
if (preg_match('/^[section]/i', $var)) {
if (substr($var, 0, 7) == 'section') {
db_execute_prepared('REPLACE INTO user_auth_realm
(user_id, realm_id)
VALUES (?, ?)',
array(get_nfilter_request_var('id'), substr($var, 7)));
}
}
}
reset_user_perms(get_nfilter_request_var('id'));
raise_message(1);
} elseif (isset_request_var('save_component_graph_settings')) {
foreach ($settings_user as $tab_short_name => $tab_fields) {
foreach ($tab_fields as $field_name => $field_array) {
if ((isset($field_array['items'])) && (is_array($field_array['items']))) {
foreach ($field_array['items'] as $sub_field_name => $sub_field_array) {
db_execute_prepared('REPLACE INTO settings_user
(user_id, name, value)
VALUES (?, ?, ?)',
array((!empty($user_id) ? $user_id : get_filter_request_var('id')), $sub_field_name, get_nfilter_request_var($sub_field_name, '')));
}
} else {
db_execute_prepared('REPLACE INTO settings_user
(user_id, name, value)
VALUES (?, ?, ?)',
array((!empty($user_id) ? $user_id : get_filter_request_var('id')), $field_name, get_nfilter_request_var($field_name)));
}
}
}
/* reset local settings cache so the user sees the new settings */
kill_session_var('sess_user_config_array');
reset_user_perms(get_request_var('id'));
raise_message(1);
} elseif (isset_request_var('save_component_graph_perms')) {
db_execute_prepared('UPDATE user_auth
SET policy_graphs = ?, policy_trees = ?, policy_hosts = ?, policy_graph_templates = ?
WHERE id = ?',
array(get_nfilter_request_var('policy_graphs'), get_nfilter_request_var('policy_trees'), get_nfilter_request_var('policy_hosts'), get_nfilter_request_var('policy_graph_templates'), get_nfilter_request_var('id')));
} else {
api_plugin_hook('user_admin_user_save');
reset_user_perms(get_filter_request_var('id'));
}
/* redirect to the appropriate page */
header('Location: user_admin.php?action=user_edit&header=false&id=' . (empty($user_id) ? get_filter_request_var('id') : $user_id));
}
/* --------------------------
Graph Permissions
-------------------------- */
function perm_remove() {
/* ================= input validation ================= */
get_filter_request_var('id');
get_filter_request_var('user_id');
/* ==================================================== */
if (get_request_var('type') == 'graph') {
db_execute_prepared('DELETE FROM user_auth_perms
WHERE type = 1
AND user_id = ?
AND item_id = ?',
array(get_request_var('user_id'), get_request_var('id')));
} elseif (get_request_var('type') == 'tree') {
db_execute_prepared('DELETE FROM user_auth_perms
WHERE type = 2
AND user_id = ?
AND item_id = ?',
array(get_request_var('user_id'), get_request_var('id')));
} elseif (get_request_var('type') == 'host') {
db_execute_prepared('DELETE FROM user_auth_perms
WHERE type = 3
AND user_id = ?
AND item_id = ?',
array(get_request_var('user_id'), get_request_var('id')));
} elseif (get_request_var('type') == 'graph_template') {
db_execute_prepared('DELETE FROM user_auth_perms
WHERE type = 4
AND user_id = ?
AND item_id = ?',
array(get_request_var('user_id'), get_request_var('id')));
}
header('Location: user_admin.php?action=user_edit&header=false&tab=graph_perms_edit&id=' . get_request_var('user_id'));
}
function get_permission_string(&$graph, &$policies) {
$grantStr = '';
$rejectStr = '';
if (read_config_option('graph_auth_method') == 1) {
$method = 'loose';
} else {
$method = 'strong';
}
$i = 1;
foreach($policies as $p) {
$allowed = 0;
$rejected = 0;
if ($p['policy_graphs'] == 1) {
if ($graph["user$i"] == '') {
$grantStr = $grantStr . ($grantStr != '' ? ', ':'') . 'Graph:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
} else {
$rejectStr = $rejectStr . ($rejectStr != '' ? ', ':'') . 'Graph:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
}
} elseif ($graph["user$i"] != '') {
$grantStr = $grantStr . ($grantStr != '' ? ', ':'') . 'Graph:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
} elseif ($method == 'loose') {
$rejected++;
} else {
$rejectStr = $rejectStr . ($rejectStr != '' ? ', ':'') . 'Graph:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
}
$i++;
if ($p['policy_hosts'] == 1) {
if ($graph["user$i"] == '') {
if ($method == 'loose') {
$grantStr = $grantStr . ($grantStr != '' ? ', ':'') . 'Device:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
} else {
$allowed++;
}
} else {
$rejectStr = $rejectStr . ($rejectStr != '' ? ', ':'') . 'Device:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
}
} elseif ($graph["user$i"] != '') {
if ($method == 'loose') {
$grantStr = $grantStr . ($grantStr != '' ? ', ':'') . 'Device:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
} else {
$allowed++;
}
} elseif ($method == 'loose') {
$rejected++;
}
$i++;
if ($p['policy_graph_templates'] == 1) {
if ($graph["user$i"] == '') {
if ($method == 'loose') {
$grantStr = $grantStr . ($grantStr != '' ? ', ':'') . 'Template:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
} else {
$allowed++;
}
} else {
$rejectStr = $rejectStr . ($rejectStr != '' ? ', ':'') . 'Template:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
}
} elseif ($graph["user$i"] != '') {
if ($method == 'loose') {
$grantStr = $grantStr . ($grantStr != '' ? ', ':'') . 'Template:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
} else {
$allowed++;
}
} elseif ($method == 'loose') {
$rejected++;
}
$i++;
if ($method != 'loose') {
if ($allowed == 2) {
$grantStr = $grantStr . ($grantStr != '' ? ', ':'') . 'Device+Template:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
} else {
$rejectStr = $rejectStr . ($rejectStr != '' ? ', ':'') . 'Device+Template:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
}
} elseif ($rejected == 3) {
$rejectStr = $rejectStr . ($rejectStr != '' ? ', ':'') . 'Graph+Device+Template:(' . ucfirst($p['type']) . ($p['type'] != 'user' ? '/' . $p['name'] . ')':')');
}
}
$permStr = '';
if ($grantStr != '') {
$permStr = "<span class='accessGranted'>Granted:</span> <span class='accessGrantedItem'>" . trim($grantStr,',') . '</span>';
}
if ($rejectStr != '') {
if ($grantStr == '') {
$permStr = "<span class='accessRestricted'>Restricted:</span> <span class='accessRestrictedItem'>" . trim($rejectStr,',') . '</span>';
} else {
$permStr .= ", <span class='accessRestrictedItem'>" . trim($rejectStr,',') . '</span>';
}
}
return $permStr;
}
function graph_perms_edit($tab, $header_label) {
/* ================= input validation ================= */
get_filter_request_var('id');
/* ==================================================== */
$sql_where = '';
$sql_join = '';
$limit = '';
$sql_having = '';
$policy_array = array(
1 => __('Allow'),
2 => __('Deny')
);
if (!isempty_request_var('id')) {
$policy = db_fetch_row_prepared('SELECT policy_graphs, policy_trees, policy_hosts, policy_graph_templates
FROM user_auth
WHERE id = ?', array(get_request_var('id')));
} else {
$policy = array(
'policy_graphs' => '1',
'policy_trees' => '1',
'policy_hosts' => '1',
'policy_graph_templates' => '1'
);
}
switch($tab) {
case 'permsg':
process_graph_request_vars();
graph_filter($header_label);
form_start('user_admin.php', 'policy');
if (read_config_option('graph_auth_method') == 1) {
$policy_note = __('<b>Note:</b> System Graph Policy is \'Permissive\' meaning the User must have access to at least one of Graph, Device, or Graph Template to gain access to the Graph');
} else {
$policy_note = __('<b>Note:</b> System Graph Policy is \'Restrictive\' meaning the User must have access to the Graph, Device, and Graph Template to gain access to the Graph');
}
/* box: device permissions */
html_start_box(__('Default Graph Policy'), '100%', '', '3', 'center', '');
?>
<tr class='even'>
<td><table><tr>
<td class='nowrap'><?php print __('Default Graph Policy for this User');?></td>
<td>
<?php form_dropdown('policy_graphs',$policy_array,'','',$policy['policy_graphs'],'',''); ?>
</td>
<td>
<input type='submit' name='update_policy' value='<?php print __esc('Update');?>'>
<input type='hidden' name='tab' value='<?php print $tab;?>'>
<input type='hidden' name='id' value='<?php print get_request_var('id');?>'>
<input type='hidden' name='update_policy' value='1'>
</td>
</tr></table></td>
</tr>
<tr class='even'>
<td><br><?php print $policy_note;?></td>
</tr>
<?php
html_end_box();
form_end();
/* if the number of rows is -1, set it to the default */
if (get_request_var('rows') == -1) {
$rows = read_config_option('num_rows_table');
} else {
$rows = get_request_var('rows');
}
$user_id = get_request_var('id');
if (read_config_option('graph_auth_method') == 1) {
$sql_operator = 'OR';
} else {
$sql_operator = 'AND';
}
$limit = 'LIMIT ' . ($rows*(get_request_var('page')-1)) . ',' . $rows;
/* get policies for all groups and user */
$policies = db_fetch_assoc_prepared("SELECT uag.id, 'group' AS type, uag.name,
policy_graphs, policy_hosts, policy_graph_templates
FROM user_auth_group AS uag
INNER JOIN user_auth_group_members AS uagm
ON uag.id = uagm.group_id
WHERE uag.enabled = 'on' AND uagm.user_id = ?",
array($user_id));
$policies[] = db_fetch_row_prepared("SELECT id, 'user' AS type, 'user' AS name,
policy_graphs, policy_hosts, policy_graph_templates
FROM user_auth WHERE id = ?",
array($user_id));
array_reverse($policies);
/* form the 'where' clause for our main sql query */
if (get_request_var('filter') != '') {
$sql_where = "WHERE (gtg.title_cache LIKE '%" . get_request_var('filter') . "%' AND gtg.local_graph_id > 0)";
} else {
$sql_where = 'WHERE (gtg.local_graph_id > 0)';
}
if (get_request_var('graph_template_id') == '-1') {
/* Show all items */
} elseif (get_request_var('graph_template_id') == '0') {
$sql_where .= ($sql_where != '' ? ' AND ':'WHERE ') . ' gtg.graph_template_id=0';
} elseif (!isempty_request_var('graph_template_id')) {
$sql_where .= ($sql_where != '' ? ' AND ':'WHERE ') . ' gtg.graph_template_id=' . get_request_var('graph_template_id');
}
$i = 1;
$user_perm = '';
$sql_select = '';
foreach($policies as $policy) {
if ($policy['type'] == 'user' && $user_perm == '') {
$user_perm = $i;
}
if (get_request_var('associated') == 'false') {
if ($policy['policy_graphs'] == 1) {
$sql_having .= ($sql_having != '' ? ' OR ':'') . " (user$i IS NULL";
} else {
$sql_having .= ($sql_having != '' ? ' OR ':'') . " (user$i IS NOT NULL";
}
}
$sql_join .= 'LEFT JOIN user_auth_' . ($policy['type'] == 'user' ? '':'group_') . "perms AS uap$i ON (gl.id=uap$i.item_id AND uap$i.type=1 AND uap$i." . $policy['type'] . '_id=' . get_request_var('id') . ') ';
$sql_select .= ($sql_select != '' ? ', ':'') . "uap$i." . $policy['type'] . "_id AS user$i";
$i++;
if (get_request_var('associated') == 'false') {
if ($policy['policy_hosts'] == 1) {
$sql_having .= " OR (user$i IS NULL";
} else {
$sql_having .= " OR (user$i IS NOT NULL";
}
}
$sql_join .= 'LEFT JOIN user_auth_' . ($policy['type'] == 'user' ? '':'group_') . "perms AS uap$i ON (gl.host_id=uap$i.item_id AND uap$i.type=3 AND uap$i." . $policy['type'] . '_id=' . get_request_var('id') . ') ';
$sql_select .= ($sql_select != '' ? ', ':'') . "uap$i." . $policy['type'] . "_id AS user$i";
$i++;
if (get_request_var('associated') == 'false') {
if ($policy['policy_graph_templates'] == 1) {
$sql_having .= " $sql_operator user$i IS NULL))";
} else {
$sql_having .= " $sql_operator user$i IS NOT NULL))";
}
}
$sql_join .= 'LEFT JOIN user_auth_' . ($policy['type'] == 'user' ? '':'group_') . "perms AS uap$i ON (gl.graph_template_id=uap$i.item_id AND uap$i.type=4 AND uap$i." . $policy['type'] . '_id=' . get_request_var('id') . ') ';
$sql_select .= ($sql_select != '' ? ', ':'') . "uap$i." . $policy['type'] . "_id AS user$i";
$i++;
}
if ($sql_having != '') {
$sql_having = 'HAVING ' . $sql_having;
}
$graphs = db_fetch_assoc("SELECT gtg.local_graph_id, h.description, gt.name AS template_name,
gtg.title_cache, gtg.width, gtg.height, gl.snmp_index, gl.snmp_query_id,
$sql_select
FROM graph_templates_graph AS gtg
INNER JOIN graph_local AS gl
ON gl.id = gtg.local_graph_id
LEFT JOIN graph_templates AS gt
ON gt.id = gl.graph_template_id
LEFT JOIN host AS h
ON h.id = gl.host_id
$sql_join
$sql_where
$sql_having
ORDER BY gtg.title_cache
$limit");
$total_rows = db_fetch_cell("SELECT COUNT(*)
FROM (
SELECT $sql_select
FROM graph_templates_graph AS gtg
INNER JOIN graph_local AS gl
ON gl.id = gtg.local_graph_id
LEFT JOIN graph_templates AS gt
ON gt.id = gl.graph_template_id
LEFT JOIN host AS h
ON h.id = gl.host_id
$sql_join
$sql_where
$sql_having
) AS rows");
$nav = html_nav_bar('user_admin.php?action=user_edit&tab=permsg&id=' . get_request_var('id'), MAX_DISPLAY_PAGES, get_request_var('page'), $rows, $total_rows, 11, __('Graphs'), 'page', 'main');
form_start(htmlspecialchars('user_admin.php?tab=permsg&id=' . get_request_var('id')), 'chk');
print $nav;
html_start_box('', '100%', '', '3', 'center', '');
$display_text = array(__('Graph Title'), __('ID'), __('Effective Policy'));
html_header_checkbox($display_text, false);
if (sizeof($graphs)) {