forked from webmin/webmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acl-lib.pl
executable file
·2188 lines (1955 loc) · 59.2 KB
/
acl-lib.pl
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
=head1 acl-lib.pl
Library for editing webmin users, passwords and access rights.
foreign_require("acl", "acl-lib.pl");
@users = acl::list_users();
$newguy = { 'name' => 'newguy',
'pass' => acl::encrypt_password('smeg'),
'modules' => [ 'useradmin' ] };
acl::create_user($newguy);
=cut
BEGIN { push(@INC, ".."); };
use strict;
use warnings;
use WebminCore;
&init_config();
do 'md5-lib.pl';
our ($module_root_directory, %text, %sessiondb, %config, %gconfig,
$base_remote_user, %hash_session_id_cache);
our %access = &get_module_acl();
$access{'switch'} = 0 if (&is_readonly_mode());
=head2 list_users([&only-users])
Returns a list of hashes containing Webmin user details. Useful keys include :
=item name - Login name
=item pass - Encrypted password
=item modules - Array references of modules
=item theme - Custom theme, if any
If the only-users parameter is given, limit the list to just users with
those usernames.
=cut
sub list_users
{
my ($only) = @_;
my (%miniserv, @rv, %acl, %logout);
&read_acl(undef, \%acl);
&get_miniserv_config(\%miniserv);
if ($miniserv{'logouttimes'}) {
foreach my $a (split(/\s+/, $miniserv{'logouttimes'})) {
if ($a =~ /^([^=]+)=(\S+)$/) {
$logout{$1} = $2;
}
}
}
my $fh = "PWFILE";
&open_readfile($fh, $miniserv{'userfile'});
while(my $l = <$fh>) {
$l =~ s/\r|\n//g;
my @user = split(/:/, $l);
if (@user) {
my %user;
next if ($only && &indexof($user[0], @$only) < 0);
while(@user < 11) { push(@user, ""); } # Prevent warnings
$user{'name'} = $user[0];
$user{'pass'} = $user[1];
$user{'sync'} = $user[2];
$user{'cert'} = $user[3];
if ($user[4] =~ /^(allow|deny)\s+(.*)/) {
$user{$1} = $2;
$user{$1} =~ s/;/:/g;
}
if ($user[5] =~ /days\s+(\S+)/) {
$user{'days'} = $1;
}
if ($user[5] =~ /hours\s+(\d+\.\d+)-(\d+\.\d+)/) {
$user{'hoursfrom'} = $1;
$user{'hoursto'} = $2;
}
$user{'lastchange'} = $user[6];
$user{'olds'} = $user[7] ? [ split(/\s+/, $user[7]) ] : [ ];
$user{'minsize'} = $user[8];
$user{'nochange'} = int($user[9] || 0);
$user{'temppass'} = int($user[10] || 0);
$user{'twofactor_provider'} = $user[11];
$user{'twofactor_id'} = $user[12];
$user{'twofactor_apikey'} = $user[13];
$user{'modules'} = $acl{$user[0]};
$user{'lang'} = $gconfig{"lang_$user[0]"};
$user{'langauto'} = $gconfig{"langauto_$user[0]"};
$user{'notabs'} = $gconfig{"notabs_$user[0]"};
$user{'rbacdeny'} = $gconfig{"rbacdeny_$user[0]"};
if ($gconfig{"theme_$user[0]"}) {
($user{'theme'}, $user{'overlay'}) =
split(/\s+/, $gconfig{"theme_$user[0]"});
}
elsif (defined($gconfig{"theme_$user[0]"})) {
$user{'theme'} = "";
}
$user{'readonly'} = $gconfig{"readonly_$user[0]"};
$user{'ownmods'} =
[ split(/\s+/, $gconfig{"ownmods_$user[0]"} || "") ];
$user{'logouttime'} = $logout{$user[0]};
$user{'real'} = $gconfig{"realname_$user[0]"};
push(@rv, \%user);
}
}
close($fh);
# If a user DB is enabled, get users from it too
if ($miniserv{'userdb'}) {
my ($dbh, $proto, $prefix, $args) =&connect_userdb($miniserv{'userdb'});
&error("Failed to connect to user database : $dbh") if (!ref($dbh));
if ($proto eq "mysql" || $proto eq "postgresql") {
# Fetch users with SQL
my %userid;
my $cmd = $dbh->prepare(
"select id,name,pass from webmin_user".
($only ? " where name in (".
join(",", map { "'$_'" } @$only).")" : ""));
$cmd && $cmd->execute() ||
&error("Failed to query users : ".$dbh->errstr);
while(my ($id, $name, $pass) = $cmd->fetchrow()) {
my $u = { 'name' => $name,
'pass' => $pass,
'proto' => $proto,
'id' => $id };
push(@rv, $u);
$userid{$id} = $u;
}
$cmd->finish();
# Add user attributes
$cmd = $dbh->prepare(
"select id,attr,value from webmin_user_attr ".
($only && %userid ?
" where id in (".join(",", keys %userid).")" : ""));
$cmd && $cmd->execute() ||
&error("Failed to query user attrs : ".$dbh->errstr);
while(my ($id, $attr, $value) = $cmd->fetchrow()) {
if ($attr eq "olds" || $attr eq "modules" ||
$attr eq "ownmods") {
$value = [ split(/\s+/, $value) ];
}
$userid{$id}->{$attr} = $value;
}
$cmd->finish();
}
elsif ($proto eq "ldap") {
# Find users with LDAP query
my $filter = '(objectClass='.$args->{'userclass'}.')';
if ($only) {
my $ufilter =
"(|".join("", map { "(cn=$_)" } @$only).")";
$filter = "(&".$filter.$ufilter.")";
}
my $rv = $dbh->search(
base => $prefix,
filter => $filter,
scope => 'sub');
if (!$rv || $rv->code) {
&error("Failed to search users : ".
($rv ? $rv->error : "Unknown error"));
}
foreach my $l ($rv->all_entries) {
my $u = { 'name' => $l->get_value('cn'),
'pass' => $l->get_value('webminPass'),
'proto' => $proto,
'id' => $l->dn() };
foreach my $la ($l->get_value('webminAttr')) {
my ($attr, $value) = split(/=/, $la, 2);
if ($attr eq "olds" || $attr eq "ownmods") {
$value = [ split(/\s+/, $value) ];
}
$u->{$attr} = $value;
}
$u->{'modules'} = [ $l->get_value('webminModule') ];
push(@rv, $u);
}
}
&disconnect_userdb($miniserv{'userdb'}, $dbh);
}
return @rv;
}
=head2 get_user(username)
Returns a hash ref of details of the user with some name, in the same format
as list_users. Returns undef if not found
=cut
sub get_user
{
my ($username) = @_;
my @rv = &list_users([ $username ]);
my ($user) = grep { $_->{'name'} eq $username } @rv;
return $user;
}
=head2 list_groups([&only-groups])
Returns a list of hashes, one per Webmin group. Group membership is stored in
/etc/webmin/webmin.groups, and other attributes in the config file. Useful
keys include :
=item name - Group name
=item members - Array reference of member users
=item modules - Modules to grant to members
If the only-groups parameter is given, limit the list to just groups with
those group names.
=cut
sub list_groups
{
my ($only) = @_;
my @rv;
my %miniserv;
&get_miniserv_config(\%miniserv);
# Add groups from local files
if ( -r "$config_directory/webmin.groups" ) {
my $lref = &read_file_lines("$config_directory/webmin.groups", 1);
foreach my $l (@$lref) {
$l =~ s/\r|\n//g;
my @g = split(/:/, $l);
if (@g) {
next if ($only && &indexof($g[0], @$only) < 0);
my $group = { 'name' => $g[0],
'members' => [ split(/\s+/, $g[1] || "") ],
'modules' => [ split(/\s+/, $g[2] || "") ],
'desc' => $g[3],
'ownmods' => [ split(/\s+/, $g[4] || "") ] };
push(@rv, $group);
}
}
}
# If a user DB is enabled, get groups from it too
if ($miniserv{'userdb'}) {
my ($dbh, $proto, $prefix, $args) =&connect_userdb($miniserv{'userdb'});
&error("Failed to connect to group database : $dbh") if (!ref($dbh));
if ($proto eq "mysql" || $proto eq "postgresql") {
# Fetch groups with SQL
my %groupid;
my $cmd = $dbh->prepare(
"select id,name,description from webmin_group ".
($only ? " where name in (".
join(",", map { "'$_'" } @$only).")" : ""));
$cmd && $cmd->execute() ||
&error("Failed to query groups : ".$dbh->errstr);
while(my ($id, $name, $desc) = $cmd->fetchrow()) {
my $g = { 'name' => $name,
'desc' => $desc,
'proto' => $proto,
'id' => $id };
push(@rv, $g);
$groupid{$id} = $g;
}
$cmd->finish();
# Add group attributes
$cmd = $dbh->prepare(
"select id,attr,value from webmin_group_attr ".
($only && %groupid ?
" where id in (".join(",", keys %groupid).")" : ""));
$cmd && $cmd->execute() ||
&error("Failed to query group attrs : ".$dbh->errstr);
while(my ($id, $attr, $value) = $cmd->fetchrow()) {
if ($attr eq "members" || $attr eq "modules" ||
$attr eq "ownmods") {
$value = [ split(/\s+/, $value) ];
}
$groupid{$id}->{$attr} = $value;
}
$cmd->finish();
}
elsif ($proto eq "ldap") {
# Find groups with LDAP query
my $filter = '(objectClass='.$args->{'groupclass'}.')';
if ($only) {
my $gfilter =
"(|".join("", map { "(cn=$_)" } @$only).")";
$filter = "(&".$filter.$gfilter.")";
}
my $rv = $dbh->search(
base => $prefix,
filter => $filter,
scope => 'sub');
if (!$rv || $rv->code) {
&error("Failed to search groups : ".
($rv ? $rv->error : "Unknown error"));
}
foreach my $l ($rv->all_entries) {
my $g = { 'name' => $l->get_value('cn'),
'desc' => $l->get_value('webminDesc'),
'proto' => $proto,
'id' => $l->dn() };
foreach my $la ($l->get_value('webminAttr')) {
my ($attr, $value) = split(/=/, $la, 2);
if ($attr eq "members" || $attr eq "ownmods") {
$value = [ split(/\s+/, $value) ];
}
$g->{$attr} = $value;
}
$g->{'modules'} = [ $l->get_value('webminModule') ];
push(@rv, $g);
}
}
&disconnect_userdb($miniserv{'userdb'}, $dbh);
}
return @rv;
}
=head2 get_group(groupname)
Returns a hash ref of details of the group with some name, in the same format
as list_groups. Returns undef if not found
=cut
sub get_group
{
my ($groupname) = @_;
my @rv = &list_groups([ $groupname ]);
my ($group) = grep { $_->{'name'} eq $groupname } @rv;
return $group;
}
=head2 list_modules
Returns a list of the dirs of all modules available on this system.
=cut
sub list_modules
{
return map { $_->{'dir'} } &list_module_infos();
}
=head2 list_module_infos
Returns a list of the details of all modules that can be used on this system,
each of which is a hash reference in the same format as their module.info files.
=cut
sub list_module_infos
{
my @mods = grep { &check_os_support($_) } &get_all_module_infos();
return sort { $a->{'desc'} cmp $b->{'desc'} } @mods;
}
=head2 create_user(&details, [clone])
Creates a new Webmin user, based on the hash reference in the details parameter.
This must be in the same format as those returned by list_users. If the clone
parameter is given, it must be a username to copy detailed access control
settings from for this new user.
=cut
sub create_user
{
my ($user, $clone) = @_;
my %miniserv;
my @mods = &list_modules();
$user->{'name'} eq "webmin" && &error("Invalid username webmin for new user");
&get_miniserv_config(\%miniserv);
if ($miniserv{'userdb'} && !$miniserv{'userdb_addto'}) {
# Adding to user database
my ($dbh, $proto, $prefix, $args) =&connect_userdb($miniserv{'userdb'});
&error("Failed to connect to user database : $dbh") if (!ref($dbh));
if ($proto eq "mysql" || $proto eq "postgresql") {
# Add user with SQL
my $cmd = $dbh->prepare("insert into webmin_user (name,pass) values (?, ?)");
$cmd && $cmd->execute($user->{'name'}, $user->{'pass'}) ||
&error("Failed to add user : ".$dbh->errstr);
$cmd->finish();
$cmd = $dbh->prepare("select max(id) from webmin_user");
$cmd->execute();
my ($id) = $cmd->fetchrow();
$cmd->finish();
# Add other attributes
$cmd = $dbh->prepare("insert into webmin_user_attr (id,attr,value) values (?, ?, ?)");
foreach my $attr (keys %$user) {
next if ($attr eq "name" || $attr eq "pass");
my $value = $user->{$attr};
if ($attr eq "olds" || $attr eq "modules" ||
$attr eq "ownmods") {
$value = $value ? join(" ", @$value) : "";
}
$cmd->execute($id, $attr, $value) ||
&error("Failed to add user attribute : ".
$dbh->errstr);
$cmd->finish();
}
$user->{'id'} = $id;
$user->{'proto'} = $proto;
}
elsif ($proto eq "ldap") {
# Add user to LDAP
my $dn = "cn=".$user->{'name'}.",".$prefix;
my @attrs = ( "objectClass", $args->{'userclass'},
"cn", $user->{'name'},
"webminPass", $user->{'pass'} );
my @webminattrs;
foreach my $attr (keys %$user) {
next if ($attr eq "name" || $attr eq "pass" ||
$attr eq "modules");
my $value = $user->{$attr};
if ($attr eq "olds" || $attr eq "ownmods") {
$value = $value ? join(" ", @$value) : "";
}
push(@webminattrs,
defined($value) ? $attr."=".$value : $attr);
}
if (@webminattrs) {
push(@attrs, "webminAttr", \@webminattrs);
}
if ($user->{'modules'} && @{$user->{'modules'}}) {
push(@attrs, "webminModule", $user->{'modules'});
}
my $rv = $dbh->add($dn, attr => \@attrs);
if (!$rv || $rv->code) {
&error("Failed to add user to LDAP : ".
($rv ? $rv->error : "Unknown error"));
}
$user->{'id'} = $dn;
$user->{'proto'} = 'ldap';
}
&disconnect_userdb($miniserv{'userdb'}, $dbh);
$user->{'proto'} = $proto;
}
else {
# Adding to local files
&lock_file($ENV{'MINISERV_CONFIG'});
if ($user->{'theme'}) {
$miniserv{"preroot_".$user->{'name'}} =
$user->{'theme'}.($user->{'overlay'} ? " ".$user->{'overlay'} : "");
}
elsif (defined($user->{'theme'})) {
$miniserv{"preroot_".$user->{'name'}} = "";
}
if (defined($user->{'logouttime'})) {
my @logout = split(/\s+/, $miniserv{'logouttimes'});
push(@logout, "$user->{'name'}=$user->{'logouttime'}");
$miniserv{'logouttimes'} = join(" ", @logout);
}
&put_miniserv_config(\%miniserv);
&unlock_file($ENV{'MINISERV_CONFIG'});
my @times;
push(@times, "days", $user->{'days'}) if (defined($user->{'days'}) &&
$user->{'days'} ne '');
push(@times, "hours", $user->{'hoursfrom'}."-".$user->{'hoursto'})
if ($user->{'hoursfrom'});
&lock_file($miniserv{'userfile'});
my $fh = "PWFILE";
&open_tempfile($fh, ">>$miniserv{'userfile'}");
my $allow = $user->{'allow'} || "";
$allow =~ s/:/;/g;
my $deny = $user->{'deny'} || "";
$deny =~ s/:/;/g;
&print_tempfile($fh,
$user->{'name'},":",
$user->{'pass'},":",
($user->{'sync'} || ""),":",
($user->{'cert'} || ""),":",
($allow ? "allow $allow" :
$deny ? "deny $deny" : ""),":",
join(" ", @times),":",
($user->{'lastchange'} || ""),":",
join(" ", @{$user->{'olds'} || []}),":",
($user->{'minsize'} || ""),":",
($user->{'nochange'} || ""),":",
($user->{'temppass'} || ""),":",
($user->{'twofactor_provider'} || ""),":",
($user->{'twofactor_id'} || ""),":",
($user->{'twofactor_apikey'} || ""),
"\n");
&close_tempfile($fh);
&unlock_file($miniserv{'userfile'});
&lock_file(&acl_filename());
$fh = "ACL";
&open_tempfile($fh, ">>".&acl_filename());
&print_tempfile($fh, &acl_line($user, \@mods));
&close_tempfile($fh);
&unlock_file(&acl_filename());
delete($gconfig{"lang_".$user->{'name'}});
$gconfig{"lang_".$user->{'name'}} = $user->{'lang'} if ($user->{'lang'});
delete($gconfig{"notabs_".$user->{'name'}});
$gconfig{"notabs_".$user->{'name'}} = $user->{'notabs'} if ($user->{'notabs'});
delete($gconfig{"rbacdeny_".$user->{'name'}});
$gconfig{"rbacdeny_".$user->{'name'}} = $user->{'rbacdeny'} if ($user->{'rbacdeny'});
delete($gconfig{"ownmods_".$user->{'name'}});
$gconfig{"ownmods_".$user->{'name'}} = join(" ", @{$user->{'ownmods'}})
if ($user->{'ownmods'} && @{$user->{'ownmods'}});
delete($gconfig{"theme_".$user->{'name'}});
if ($user->{'theme'}) {
$gconfig{"theme_".$user->{'name'}} =
$user->{'theme'}.($user->{'overlay'} ? " ".$user->{'overlay'} : "");
}
elsif (defined($user->{'theme'})) {
$gconfig{"theme_".$user->{'name'}} = '';
}
$gconfig{"readonly_".$user->{'name'}} = $user->{'readonly'}
if (defined($user->{'readonly'}));
$gconfig{"realname_".$user->{'name'}} = $user->{'real'}
if (defined($user->{'real'}));
&write_file("$config_directory/config", \%gconfig);
}
# Copy ACLs from user being cloned
if ($clone) {
©_acl_files($clone, $user->{'name'}, [ "", @mods ]);
}
}
=head2 modify_user(old-name, &details)
Updates an existing Webmin user, identified by the old-name parameter. The
details hash must be in the same format as returned by list_users or passed
to create_user.
=cut
sub modify_user
{
my ($username, $user) = @_;
my (%miniserv, @pwfile, @acl, @mods, $m);
&get_miniserv_config(\%miniserv);
$user->{'name'} eq "webmin" &&
&error("Invalid username webmin for modified user");
if ($user->{'proto'}) {
# In users and groups DB
my ($dbh, $proto) = &connect_userdb($miniserv{'userdb'});
&error("Failed to connect to user database : $dbh") if (!ref($dbh));
if ($proto eq "mysql" || $proto eq "postgresql") {
# Get old password, for change detection
my $cmd = $dbh->prepare(
"select pass from webmin_user where id = ?");
$cmd && $cmd->execute($user->{'id'}) ||
&error("Failed to get old password : ".$dbh->errstr);
my ($oldpass) = $cmd->fetchrow();
$cmd->finish();
&add_old_password($user, $oldpass, \%miniserv);
# Update primary details
$cmd = $dbh->prepare("update webmin_user set name = ?, ".
"pass = ? where id = ?");
$cmd && $cmd->execute($user->{'name'}, $user->{'pass'},
$user->{'id'}) ||
&error("Failed to update user : ".$dbh->errstr);
$cmd->finish();
# Re-save attributes
$cmd = $dbh->prepare("delete from webmin_user_attr ".
"where id = ?");
$cmd && $cmd->execute($user->{'id'}) ||
&error("Failed to delete attrs : ".$dbh->errstr);
$cmd = $dbh->prepare("insert into webmin_user_attr ".
"(id,attr,value) values (?, ?, ?)");
foreach my $attr (keys %$user) {
next if ($attr eq "name" || $attr eq "pass");
my $value = $user->{$attr};
if ($attr eq "olds" || $attr eq "modules" ||
$attr eq "ownmods") {
$value = $value ? join(" ", @$value) : "";
}
$cmd->execute($user->{'id'}, $attr, $value) ||
&error("Failed to add user attribute : ".
$dbh->errstr);
$cmd->finish();
}
}
elsif ($proto eq "ldap") {
# Rename in LDAP if needed
if ($user->{'name'} ne $username) {
my $newdn = $user->{'id'};
$newdn =~ s/^cn=\Q$username\E,/cn=$user->{'name'},/;
my $rv = $dbh->moddn($user->{'id'},
newrdn => "cn=$user->{'name'}");
if (!$rv || $rv->code) {
&error("Failed to rename user : ".
($rv ? $rv->error : "Unknown error"));
}
$user->{'id'} = $newdn;
}
# Re-save all the attributes
my @attrs = ( "cn", $user->{'name'},
"webminPass", $user->{'pass'} );
my @webminattrs;
foreach my $attr (keys %$user) {
next if ($attr eq "name" || $attr eq "desc" ||
$attr eq "modules");
my $value = $user->{$attr};
if ($attr eq "olds" || $attr eq "ownmods") {
$value = $value ? join(" ", @$value) : "";
}
push(@webminattrs,
defined($value) ? $attr."=".$value : $attr);
}
push(@attrs, "webminAttr", \@webminattrs);
push(@attrs, "webminModule", $user->{'modules'});
my $rv = $dbh->modify($user->{'id'}, replace => { @attrs });
if (!$rv || $rv->code) {
&error("Failed to modify user : ".
($rv ? $rv->error : "Unknown error"));
}
}
&disconnect_userdb($miniserv{'userdb'}, $dbh);
}
else {
# In local files
&lock_file($ENV{'MINISERV_CONFIG'});
delete($miniserv{"preroot_".$username});
if ($user->{'theme'}) {
$miniserv{"preroot_".$user->{'name'}} =
$user->{'theme'}.($user->{'overlay'} ? " ".$user->{'overlay'} : "");
}
elsif (defined($user->{'theme'})) {
$miniserv{"preroot_".$user->{'name'}} = "";
}
my @logout = split(/\s+/, $miniserv{'logouttimes'} || "");
@logout = grep { !/^$username=/ } @logout;
if (defined($user->{'logouttime'})) {
push(@logout, "$user->{'name'}=$user->{'logouttime'}");
}
$miniserv{'logouttimes'} = join(" ", @logout);
&put_miniserv_config(\%miniserv);
&unlock_file($ENV{'MINISERV_CONFIG'});
my @times;
push(@times, "days", $user->{'days'}) if ($user->{'days'} &&
$user->{'days'} ne '');
push(@times, "hours", $user->{'hoursfrom'}."-".$user->{'hoursto'})
if ($user->{'hoursfrom'});
&lock_file($miniserv{'userfile'});
my $fh = "PWFILE";
&open_readfile($fh, $miniserv{'userfile'});
@pwfile = <$fh>;
close($fh);
&open_tempfile($fh, ">$miniserv{'userfile'}");
my $allow = $user->{'allow'};
$allow =~ s/:/;/g if ($allow);
my $deny = $user->{'deny'};
$deny =~ s/:/;/g if ($deny);
foreach my $l (@pwfile) {
if ($l =~ /^([^:]+):([^:]*)/ && $1 eq $username) {
&add_old_password($user, "$2", \%miniserv);
&print_tempfile($fh,
$user->{'name'},":",
$user->{'pass'},":",
($user->{'sync'} || ""),":",
($user->{'cert'} || ""),":",
($allow ? "allow $allow" :
$deny ? "deny $deny" : ""),":",
join(" ", @times),":",
$user->{'lastchange'},":",
join(" ", @{$user->{'olds'} || []}),":",
$user->{'minsize'},":",
$user->{'nochange'},":",
$user->{'temppass'},":",
$user->{'twofactor_provider'},":",
$user->{'twofactor_id'},":",
$user->{'twofactor_apikey'},
"\n");
}
else {
&print_tempfile($fh, $l);
}
}
&close_tempfile($fh);
&unlock_file($miniserv{'userfile'});
&lock_file(&acl_filename());
@mods = &list_modules();
$fh = "ACL";
&open_readfile($fh, &acl_filename());
@acl = <$fh>;
close($fh);
&open_tempfile($fh, ">".&acl_filename());
foreach my $l (@acl) {
if ($l =~ /^(\S+):/ && $1 eq $username) {
&print_tempfile($fh, &acl_line($user, \@mods));
}
else {
&print_tempfile($fh, $l);
}
}
&close_tempfile($fh);
&unlock_file(&acl_filename());
delete($gconfig{"lang_".$username});
$gconfig{"lang_".$user->{'name'}} = $user->{'lang'} if ($user->{'lang'});
delete($gconfig{"langauto_".$username});
$gconfig{"langauto_".$user->{'name'}} = $user->{'langauto'} if (defined($user->{'langauto'}));
delete($gconfig{"notabs_".$username});
$gconfig{"notabs_".$user->{'name'}} = $user->{'notabs'}
if ($user->{'notabs'});
delete($gconfig{"rbacdeny_".$username});
$gconfig{"rbacdeny_".$user->{'name'}} = $user->{'rbacdeny'}
if ($user->{'rbacdeny'});
delete($gconfig{"ownmods_".$username});
$gconfig{"ownmods_".$user->{'name'}} = join(" ", @{$user->{'ownmods'}})
if ($user->{'ownmods'} && @{$user->{'ownmods'}});
delete($gconfig{"theme_".$username});
if ($user->{'theme'}) {
$gconfig{"theme_".$user->{'name'}} =
$user->{'theme'}.($user->{'overlay'} ? " ".$user->{'overlay'} : "");
}
elsif (defined($user->{'theme'})) {
$gconfig{"theme_".$user->{'name'}} = '';
}
delete($gconfig{"readonly_".$username});
$gconfig{"readonly_".$user->{'name'}} = $user->{'readonly'}
if (defined($user->{'readonly'}));
delete($gconfig{"realname_".$username});
$gconfig{"realname_".$user->{'name'}} = $user->{'real'}
if (defined($user->{'real'}));
&write_file("$config_directory/config", \%gconfig);
}
if ($username ne $user->{'name'} && !$user->{'proto'}) {
# Rename all .acl files if user renamed
foreach my $m (@mods, "") {
my $file = "$config_directory/$m/$username.acl";
if (-r $file) {
&rename_file($file,
"$config_directory/$m/$user->{'name'}.acl");
}
}
my $file = "$config_directory/$username.acl";
if (-r $file) {
&rename_file($file, "$config_directory/$user->{'name'}.acl");
}
}
if ($miniserv{'session'} && $username ne $user->{'name'}) {
# Modify all sessions for the renamed user
&rename_session_user(\%miniserv, $username, $user->{'name'});
}
}
=head2 add_old_password(&user, oldpass, &miniserv)
Internal function to update the olds list of old passwords for a user
=cut
sub add_old_password
{
my ($user, $oldpass, $miniserv) = @_;
if ($oldpass ne $user->{'pass'} &&
"!".$oldpass ne $user->{'pass'} &&
$oldpass ne "!".$user->{'pass'} &&
$oldpass ne 'x' &&
$oldpass ne 'e' &&
$oldpass ne '*LK*') {
# Password change detected .. update change time
# and save the old one
my $nolock = $oldpass;
$nolock =~ s/^\!//;
$user->{'olds'} ||= [];
unshift(@{$user->{'olds'}}, $nolock);
if ($miniserv->{'pass_oldblock'}) {
while(scalar(@{$user->{'olds'}}) >
$miniserv->{'pass_oldblock'}) {
pop(@{$user->{'olds'}});
}
}
$user->{'lastchange'} = time();
}
}
=head2 delete_user(name)
Deletes the named user, including all .acl files for detailed module access
control settings.
=cut
sub delete_user
{
my ($username) = @_;
my (@pwfile, @acl, %miniserv);
&lock_file($ENV{'MINISERV_CONFIG'});
&get_miniserv_config(\%miniserv);
delete($miniserv{"preroot_".$username});
my @logout = split(/\s+/, $miniserv{'logouttimes'});
@logout = grep { !/^$username=/ } @logout;
$miniserv{'logouttimes'} = join(" ", @logout);
&put_miniserv_config(\%miniserv);
&unlock_file($ENV{'MINISERV_CONFIG'});
&lock_file($miniserv{'userfile'});
my $fh = "PWFILE";
&open_readfile($fh, $miniserv{'userfile'});
@pwfile = <$fh>;
close($fh);
&open_tempfile($fh, ">$miniserv{'userfile'}");
foreach my $l (@pwfile) {
if ($l !~ /^([^:]+):/ || $1 ne $username) {
&print_tempfile($fh, $l);
}
}
&close_tempfile($fh);
&unlock_file($miniserv{'userfile'});
&lock_file(&acl_filename());
$fh = "ACL";
&open_readfile($fh, &acl_filename());
@acl = <$fh>;
close($fh);
&open_tempfile($fh, ">".&acl_filename());
foreach my $l (@acl) {
if ($l !~ /^([^:]+):/ || $1 ne $username) {
&print_tempfile($fh, $l);
}
}
&close_tempfile($fh);
&unlock_file(&acl_filename());
delete($gconfig{"lang_".$username});
delete($gconfig{"notabs_".$username});
delete($gconfig{"ownmods_".$username});
delete($gconfig{"rbacdeny_".$username});
delete($gconfig{"theme_".$username});
delete($gconfig{"overlay_".$username});
delete($gconfig{"readonly_".$username});
delete($gconfig{"realname_".$username});
&write_file("$config_directory/config", \%gconfig);
# Delete all module .acl files
&unlink_file(map { "$config_directory/$_/$username.acl" } &list_modules());
&unlink_file("$config_directory/$username.acl");
if ($miniserv{'session'}) {
# Delete all sessions for the deleted user
&delete_session_user(\%miniserv, $username);
}
if ($miniserv{'userdb'}) {
# Also delete from user database
my ($dbh, $proto, $prefix, $args) =&connect_userdb($miniserv{'userdb'});
&error("Failed to connect to user database : $dbh") if (!ref($dbh));
if ($proto eq "mysql" || $proto eq "postgresql") {
# Find the user with SQL query
my $cmd = $dbh->prepare(
"select id from webmin_user where name = ?");
$cmd && $cmd->execute($username) ||
&error("Failed to find user : ".$dbh->errstr);
my ($id) = $cmd->fetchrow();
$cmd->finish();
if (defined($id)) {
# Delete the user
my $cmd = $dbh->prepare(
"delete from webmin_user where id = ?");
$cmd && $cmd->execute($id) ||
&error("Failed to delete user : ".$dbh->errstr);
$cmd->finish();
# Delete attributes
$cmd = $dbh->prepare(
"delete from webmin_user_attr where id = ?");
$cmd && $cmd->execute($id) ||
&error("Failed to delete user attrs : ".
$dbh->errstr);
$cmd->finish();
# Delete ACLs
$cmd = $dbh->prepare(
"delete from webmin_user_acl where id = ?");
$cmd && $cmd->execute($id) ||
&error("Failed to delete user acls : ".
$dbh->errstr);
$cmd->finish();
}
}
elsif ($proto eq "ldap") {
# Find user with LDAP query
my $rv = $dbh->search(
base => $prefix,
filter => '(&(cn='.$username.')(objectClass='.
$args->{'userclass'}.'))',
scope => 'sub');
if (!$rv || $rv->code) {
&error("Failed to find user : ".
($rv ? $rv->error : "Unknown error"));
}
my ($user) = $rv->all_entries;
if ($user) {
# Delete sub-objects
my $rv = $dbh->search(
base => $user->dn(),
filter => '(objectClass=*)',
scope => 'sub');
if (!$rv || $rv->code) {
&error("Failed to delete LDAP user : ".
($rv ? $rv->error : "Unknown error"));
}
foreach my $so ($rv->all_entries) {
next if ($so->dn() eq $user->dn());
my $drv = $dbh->delete($so->dn());
if ($drv->code) {
&error("Failed to delete LDAP ".
"sub-object : ".$drv->error);
}
}
# Delete the user from LDAP
$rv = $dbh->delete($user->dn());
if (!$rv || $rv->code) {
&error("Failed to delete LDAP user : ".
($rv ? $rv->error : "Unknown error"));
}
}
}
&disconnect_userdb($miniserv{'userdb'}, $dbh);
}
}
=head2 create_group(&group, [clone])
Add a new webmin group, based on the details in the group hash. The required
keys are :
=item name - Unique name of the group
=item modules - An array reference of module names
=item members - An array reference of group member names. Sub-groups must have their names prefixed with an @.
=cut
sub create_group
{
my ($group, $clone) = @_;
my %miniserv;
&get_miniserv_config(\%miniserv);
if ($miniserv{'userdb'} && !$miniserv{'userdb_addto'}) {
# Adding to group database
my ($dbh, $proto, $prefix, $args) =&connect_userdb($miniserv{'userdb'});
&error("Failed to connect to group database : $dbh") if (!ref($dbh));
if ($proto eq "mysql" || $proto eq "postgresql") {
# Add group with SQL
my $cmd = $dbh->prepare("insert into webmin_group (name,description) values (?, ?)");
$cmd && $cmd->execute($group->{'name'}, $group->{'desc'}) ||
&error("Failed to add group : ".$dbh->errstr);
$cmd->finish();
$cmd = $dbh->prepare("select max(id) from webmin_group");
$cmd->execute();
my ($id) = $cmd->fetchrow();
$cmd->finish();
# Add other attributes
$cmd = $dbh->prepare("insert into webmin_group_attr (id,attr,value) values (?, ?, ?)");
foreach my $attr (keys %$group) {
next if ($attr eq "name" || $attr eq "desc");
my $value = $group->{$attr};
if ($attr eq "members" || $attr eq "modules" ||
$attr eq "ownmods") {
$value = $value ? join(" ", @$value) : "";
}
$cmd->execute($id, $attr, $value) ||
&error("Failed to add group attribute : ".
$dbh->errstr);
$cmd->finish();
}
$_[0]->{'id'} = $id;
$_[0]->{'proto'} = $proto;
}
elsif ($proto eq "ldap") {
# Add group to LDAP
my $dn = "cn=".$group->{'name'}.",".$prefix;
my @attrs = ( "objectClass", $args->{'groupclass'},
"cn", $group->{'name'},
"webminDesc", $group->{'desc'} );
my @webminattrs;
foreach my $attr (keys %$group) {
next if ($attr eq "name" || $attr eq "desc" ||
$attr eq "modules");
my $value = $group->{$attr};
if ($attr eq "members" || $attr eq "ownmods") {
$value = $value ? join(" ", @$value) : "";
}
push(@webminattrs, $attr."=".$value);
}
if (@webminattrs) {
push(@attrs, "webminAttr", \@webminattrs);
}
if ($group->{'modules'} && @{$group->{'modules'}}) {
push(@attrs, "webminModule", $group->{'modules'});
}