-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathvirtual-server-lib-funcs.pl
executable file
·21412 lines (20084 loc) · 599 KB
/
virtual-server-lib-funcs.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
use Time::Local;
use POSIX;
use feature 'state';
## Work out where our extra -lib.pl files are, and load them
$virtual_server_root = $module_root_directory;
if (!$virtual_server_root) {
foreach my $i (keys %INC) {
if ($i =~ /^(.*)\/virtual-server-lib-funcs.pl$/) {
$virtual_server_root = $1;
}
}
}
if (!$virtual_server_root) {
$0 =~ /^(.*)\//;
$virtual_server_root = "$1/virtual-server";
}
foreach my $lib ("scripts", "resellers", "admins", "users", "simple", "s3",
"php", "ruby", "vui", "dynip", "collect", "maillog",
"balancer", "newfeatures", "resources", "backups",
"domainname", "commands", "connectivity", "plans",
"postgrey", "wizard", "security", "json", "redirects", "ftp",
"dkim", "provision", "stats", "bkeys", "rs", "cron",
"ratelimit", "cloud", "google", "gcs", "dropbox", "copycert",
"jailkit", "ports", "bb", "dnscloud", "dnscloudpro",
"smtpcloud", "pro-tip", "azure", "remotedns", "drive",
"acme", "api-create-domain") {
my $libfile = "$virtual_server_root/pro/$lib-lib.pl";
if (!-r $libfile) {
$libfile = "$virtual_server_root/$lib-lib.pl";
}
do $libfile;
if ($@ && -r $libfile) {
print STDERR "failed to load $lib-lib.pl : $@\n";
}
}
# Wrapper function to get webprefix safely
sub get_webprefix_safe
{
if (defined(&get_webprefix)) {
return &get_webprefix();
}
return $gconfig{'webprefix'};
}
# require_useradmin([no-quotas])
sub require_useradmin
{
if (!$require_useradmin++) {
&foreign_require("useradmin");
%uconfig = &foreign_config("useradmin");
$home_base = &resolve_links($config{'home_base'} || $uconfig{'home_base'});
$cannot_rehash_password = 0;
if ($config{'ldap'}) {
&foreign_require("ldap-useradmin");
%luconfig = &foreign_config("ldap-useradmin");
$usermodule = "ldap-useradmin";
if ($ldap_useradmin::config{'md5'} == 3 ||
$ldap_useradmin::config{'md5'} == 4) {
$cannot_rehash_password = 1;
}
}
else {
$usermodule = "useradmin";
}
}
if (!&has_quota_commands() && !$_[0] && !$require_useradmin_quota++) {
&foreign_require("quota");
}
}
# Bring in libraries used for migrating from other servers
sub require_migration
{
foreach my $m (@migration_types) {
do "$module_root_directory/migration-$m.pl";
}
}
# list_domains()
# Returns a list of structures containing information about hosted domains
sub list_domains
{
local (@rv, $d);
local @files;
local @st = stat($domains_dir);
if (scalar(@main::list_domains_cache) &&
$st[9] == $main::list_domains_cache_time) {
# Use cache of domain IDs in RAM
@files = @main::list_domains_cache;
}
else {
# Re-scan the directory, if un-changed
opendir(DIR, $domains_dir);
@files = readdir(DIR);
closedir(DIR);
@main::list_domains_cache = @files;
$main::list_domains_cache_time = $st[9];
}
foreach $d (@files) {
if ($d !~ /^\./ && $d !~ /\.(lock|bak|back|backup|rpmsave|sav|swp|~)$/i && $d !~ /\.webmintmp\.\d+/i) {
push(@rv, &get_domain($d));
}
}
return @rv;
}
# list_visible_domains([opts])
# Returns a list of domain structures the current user can see, for use in
# domain menus. Excludes those that he doesn't have access to, and perhaps
# alias domains.
sub list_visible_domains
{
my ($opts) = @_;
my @rv = grep { &can_edit_domain($_) } &list_domains();
# Always exclude default host domain
my $exclude_defaulthostdomain =
{ 'exclude' => 'defaulthostdomain' };
$opts = {%$opts, %$exclude_defaulthostdomain}
if ($config{'default_domain_ssl'} != 2);
# Exclude domains based on given field
@rv = grep { ! $_->{$opts->{'exclude'}} } @rv
if ($opts->{'exclude'});
return @rv;
}
# sort_indent_domains(&domains)
# Returns a list of all domains sorted according to the module config setting.
# Those that should be indented have the 'indent' field set to some number.
sub sort_indent_domains
{
local @doms = @{$_[0]};
local $sortfield = $config{'domains_sort'} || "user";
local %sortkey;
if ($sortfield eq 'dom' || $sortfield eq 'sub') {
%sortkey = map { $_->{'id'}, &show_domain_name($_) } @doms;
}
else {
%sortkey = map { $_->{'id'}, $_->{$sortfield} } @doms;
}
@doms = sort { $sortkey{$a->{'id'}} cmp $sortkey{$b->{'id'}} ||
$a->{'created'} <=> $b->{'created'} } @doms;
foreach my $d (@doms) {
$d->{'indent'} = 0;
}
if ($sortfield eq 'user' || $sortfield eq 'sub') {
# Re-categorize by owner, with sub-servers indented one and alias
# servers indented two under their targets
local @catdoms;
foreach my $d (grep { !$_->{'parent'} } @doms) {
push(@catdoms, $d);
foreach my $sd (grep { $_->{'parent'} eq $d->{'id'} &&
!$_->{'alias'} } @doms) {
$sd->{'indent'} = 1;
push(@catdoms, $sd);
foreach my $ad (grep { $_->{'alias'} eq $sd->{'id'} }
@doms) {
$ad->{'indent'} = 3;
push(@catdoms, $ad);
}
}
foreach my $ad (grep { $_->{'alias'} eq $d->{'id'} } @doms) {
$ad->{'indent'} = 2;
push(@catdoms, $ad);
}
}
# Any domains that we missed due to their parent not being included
# should appear at the top level
my %incatdoms = map { $_->{'id'}, $_ } @catdoms;
foreach my $d (@doms) {
if (!$incatdoms{$d->{'id'}}) {
push(@catdoms, $d);
}
}
@doms = @catdoms;
}
return @doms;
}
# get_domain(id, [file], [force-reread])
# Looks up a domain object by ID
sub get_domain
{
local ($id, $file, $force) = @_;
return undef if (!$id && !$file);
if ($id && defined($main::get_domain_cache{$id}) && !$force) {
return $main::get_domain_cache{$id};
}
local %dom;
$file ||= "$domains_dir/$id";
&read_file($file, \%dom) || return undef;
$dom{'file'} = "$domains_dir/$id";
$dom{'id'} ||= $id;
$dom{'lastread_time'} = time();
&complete_domain(\%dom);
if (!defined($dom->{'created'})) {
# compat - creation date can be inferred from ID
$dom->{'id'} =~ /^(\d{10})/;
$dom->{'created'} = $1;
}
delete($dom->{'missing'}); # never set in a saved domain
if ($id) {
if ($main::get_domain_cache{$id} && $force) {
# In forced re-read mode, update existing object in cache
local $cache = $main::get_domain_cache{$id};
%$cache = %dom;
}
else {
# Add to cache
$main::get_domain_cache{$id} = \%dom;
}
}
return \%dom;
}
# complete_domain(&domain)
# Fills in any missing fields in a domain object
sub complete_domain
{
local ($dom) = @_;
$dom->{'mail'} = 1 if (!defined($dom->{'mail'})); # compat - assume mail is on
if (!defined($dom->{'ugid'})) {
# compat - assume user's group is domain's group
$dom->{'ugid'} = $dom->{'gid'}
}
if (!defined($dom->{'ugroup'}) && defined($dom->{'ugid'})) {
$dom->{'ugroup'} = getgrgid($dom->{'ugid'});
}
if ($dom->{'disabled'} eq '1') {
# compat - assume everything was disabled
$dom->{'disabled'} = "unix,web,dns,mail,mysql,postgres";
}
elsif ($dom->{'disabled'}) {
# compat - user disabled has changed to unix
$dom->{'disabled'} =~ s/user/unix/g;
}
if ($dom->{'disabled'}) {
# Manually disabled
$dom->{'disabled_reason'} ||= 'manual';
}
if (!defined($dom->{'gid'}) && defined($dom->{'group'})) {
# compat - get GID from group name
$dom->{'gid'} = getgrnam($dom->{'group'});
}
if (!defined($dom->{'unix'}) && !$dom->{'parent'}) {
# compat - unix is always on for parent domains
$dom->{'unix'} = 1;
}
if (!defined($dom->{'dir'})) {
# if unix is on, so is home
$dom->{'dir'} = $dom->{'unix'};
if ($dom->{'parent'}) {
# if server has a parent, it never has a Unix user
$dom->{'unix'} = 0;
}
}
if (!defined($dom->{'limit_unix'})) {
# compat - unix is always available for subdomains
$dom->{'limit_unix'} = 1;
}
if (!defined($dom->{'limit_dir'})) {
# compat - home is always available for subdomains
$dom->{'limit_dir'} = 1;
}
if (!defined($dom->{'virt'})) {
# compat - assume virtual IP if interface assigned
$dom->{'virt'} = $dom->{'iface'} ? 1 : 0;
}
if (!defined($dom->{'template'})) {
# assume default parent or sub-server template
$dom->{'template'} = $dom->{'parent'} ? 1 : 0;
}
if (!$dom->{'web_port'} && $dom->{'web'}) {
# compat - assume web port is current setting
my $tmpl = &get_template($dom->{'template'});
$dom->{'web_port'} = $tmpl->{'web_port'} || $default_web_port;
}
if (!$dom->{'web_sslport'} && $dom->{'ssl'}) {
# compat - assume SSL port is current setting
my $tmpl = &get_template($dom->{'template'});
$dom->{'web_sslport'} = $tmpl->{'web_sslport'} || $web_sslport;
}
if (!defined($dom->{'prefix'})) {
# compat - assume that prefix is same as group
$dom->{'prefix'} = $dom->{'group'};
}
if (!defined($dom->{'home'})) {
local @u = getpwnam($dom->{'user'});
if (@u && $u[7] ne '/' && $u[7] ne '/root') {
$dom->{'home'} = $u[7];
}
}
if (!defined($dom->{'proxy_pass_mode'}) && $dom->{'proxy_pass'}) {
# assume that proxy pass mode is proxy-based if not set
$dom->{'proxy_pass_mode'} = 1;
}
if (!defined($dom->{'plan'}) && !$main::no_auto_plan) {
# assume first plan
local @plans = sort { $a->{'id'} <=> $b->{'id'} } &list_plans();
$dom->{'plan'} = $plans[0]->{'id'};
}
if ($dom->{'db'} eq '' && ($dom->{'mysql'} || $dom->{'postgres'})) {
$dom->{'db'} = &database_name($dom);
}
if (!defined($dom->{'db_mysql'}) && $dom->{'mysql'}) {
# Assume just one MySQL DB
$dom->{'db_mysql'} = $dom->{'db'};
}
$dom->{'db_mysql'} = join(" ", &unique(split(/\s+/, $dom->{'db_mysql'})));
if (!defined($dom->{'db_postgres'}) && $dom->{'postgres'}) {
# Assume just one PostgreSQL DB
$dom->{'db_postgres'} = $dom->{'db'};
}
$dom->{'db_postgres'} = join(" ", &unique(split(/\s+/, $dom->{'db_postgres'})));
if ($dom->{'dns'} && $dom->{'dns_submode'} && !$dom->{'dns_subof'}) {
# Assume parent DNS domain is parent virtual server
$dom->{'dns_subof'} = $dom->{'subdom'} || $dom->{'parent'};
}
if (!$dom->{'letsencrypt_id'} && $dom->{'letsencrypt_last'}) {
# Default ACME provider to Let's Encrypt
$dom->{'letsencrypt_id'} = 1;
}
# Emailto is a computed field
&compute_emailto($dom);
# Also compute first actual email address
if (!$dom->{'emailto_addr'} ||
$dom->{'emailto_src'} ne $dom->{'emailto'}) {
if ($dom->{'emailto'} =~ /^[a-z0-9\.\_\-\+]+\@[a-z0-9\.\_\-]+$/i) {
$dom->{'emailto_addr'} = $dom->{'emailto'};
}
else {
($dom->{'emailto_addr'}) =
&extract_address_parts($dom->{'emailto'});
}
$dom->{'emailto_src'} = $dom->{'emailto'};
}
# Set edit limits based on ability to edit domains
local %acaps = map { $_, 1 } &list_automatic_capabilities($dom->{'domslimit'});
foreach my $ed (@edit_limits) {
if (!defined($dom->{'edit_'.$ed})) {
$dom->{'edit_'.$ed} = $acaps{$ed} || 0;
}
}
delete($dom->{'pass_set'}); # Only set by callers for modify_* functions
}
# compute_emailto(&domain)
# Populate the emailto field based on the email field
sub compute_emailto
{
local ($dom) = @_;
local $parent;
if ($dom->{'parent'} && ($parent = &get_domain($dom->{'parent'}))) {
$dom->{'emailto'} = $parent->{'emailto'};
}
elsif ($dom->{'email'}) {
$dom->{'emailto'} = $dom->{'email'};
}
elsif ($dom->{'mail'}) {
$dom->{'emailto'} = $dom->{'user'}.'@'.$dom->{'dom'};
}
else {
$dom->{'emailto'} = $dom->{'user'}.'@'.&get_system_hostname();
}
}
# list_automatic_capabilities(can-create-domains)
# Returns a list of default capabilities for a domain owner or plan
sub list_automatic_capabilities
{
local ($cancreate) = @_;
if ($cancreate) {
return @edit_limits;
}
else {
return ( 'users', 'aliases', 'html', 'passwd' );
}
}
# get_domain_by(field, value, [field, value, ...])
# Looks up a domain by some field(s). For each field, we either use the quick
# map to find relevant domains, or check though all that we have left.
# The special value _ANY_ matches any domains where the field is non-empty
sub get_domain_by
{
local @rv;
for(my $i=0; $i<@_; $i+=2) {
local $mf = $get_domain_by_maps{$_[$i]};
local @possible;
local %map;
if ($mf && &read_file_cached($mf, \%map)) {
# The map knows relevant domains
if ($_[$i+1] eq "_ANY_") {
# Find domains where the field is non-empty
foreach my $k (keys %map) {
next if ($k eq '');
foreach my $did (split(" ", $map{$k})) {
local $d = &get_domain($did);
push(@possible, $d) if ($d);
}
}
}
else {
# Check for a match
foreach my $did (split(" ", $map{$_[$i+1]})) {
local $d = &get_domain($did);
push(@possible, $d) if ($d);
}
}
}
else {
# Need to check manually
@possible = grep { $_->{$_[$i]} eq $_[$i+1] ||
$_->{$_[$i]} ne "" && $_[$i+1] eq "_ANY_" }
&list_domains();
}
if ($i == 0) {
# First field, so matches are the result
@rv = @possible;
}
else {
# Later field, so winnow down prevent results with new set
local %possible = map { $_->{'id'}, $_ } @possible;
@rv = grep { $possible{$_->{'id'}} } @rv;
}
}
return wantarray ? @rv : $rv[0];
}
# get_domains_by_names_users(&dnames, &usernames, &errorfunc, &plans,
# [subservers-too])
# Given a list of domain names, usernames and plans, returns all matching
# domains (unioned). May callback to the error function if one cannot be found.
sub get_domains_by_names_users
{
local ($dnames, $users, $efunc, $plans, $subs) = @_;
foreach my $domain (&unique(@$dnames)) {
local $d = &get_domain_by("dom", $domain);
$d || &$efunc("Virtual server $domain does not exist");
push(@doms, $d);
}
foreach my $uname (&unique(@$users)) {
local $dinfo = &get_domain_by("user", $uname, "parent", "");
if ($dinfo) {
push(@doms, $dinfo);
push(@doms, &get_domain_by("parent", $dinfo->{'id'}));
}
else {
&$efunc("No top-level domain owned by $uname exists");
}
}
foreach my $plan (&unique(@$plans)) {
foreach my $dinfo (&get_domain_by("plan", $plan->{'id'})) {
push(@doms, $dinfo);
push(@doms, &get_domain_by("parent", $dinfo->{'id'}));
}
}
if ($subs) {
my @add;
foreach my $d (@doms) {
push(@add, &get_domain_by("parent", $d->{'id'}))
if (!$d->{'parent'});
push(@add, &get_domain_by("alias", $d->{'id'}))
if (!$d->{'alias'});
push(@add, &get_domain_by("subdom", $d->{'id'}))
if (!$d->{'subdom'});
}
push(@doms, @add);
}
local %donedomain;
@doms = grep { !$donedomain{$_->{'id'}}++ } @doms;
return @doms;
}
# get_domain_by_user(username)
# Given a domain owner's Webmin login, return his top-level domain
sub get_domain_by_user
{
local ($user) = @_;
if ($access{'admin'}) {
# Extra admin
local $d = &get_domain($access{'admin'});
if ($d && $d->{'parent'}) {
$d = &get_domain($d->{'parent'});
}
return $d;
}
else {
# Domain owner
local $d = &get_domain_by("user", $user, "parent", "");
return $d;
}
}
# get_domains_by_names(name, ...)
# Given a list of domain names, returns the domain objects (where they exist)
sub get_domains_by_names
{
local @rv;
foreach my $dname (@_) {
my $d = &get_domain_by("dom", $dname);
push(@rv, $d) if ($d);
}
return @rv;
}
# expand_dns_subdomains(&domains)
# Given a list of domains, find all their sub-domains as well and return the
# complete list
sub expand_dns_subdomains
{
my ($doms) = @_;
my @rv;
foreach my $d (@doms) {
push(@rv, $d);
if ($d->{'dns'}) {
push(@rv, grep { $_->{'dns'} }
&get_domain_by("dns_subof", $d->{'id'}));
}
}
my %done;
return grep { !$done{$_->{'id'}}++ } @rv;
}
# domain_id()
# Returns a new unique domain ID
sub domain_id
{
local $rv = time().$$.$main::domain_id_count;
$main::domain_id_count++;
return $rv;
}
# lock_domain(&domain|id)
# Lock the config file for some domain
sub lock_domain
{
my ($id) = @_;
$id = $id->{'id'} if (ref($id));
&lock_file("$domains_dir/$id");
}
# unlock_domain(&domain|id)
# Unlock the config file for some domain
sub unlock_domain
{
my ($id) = @_;
$id = $id->{'id'} if (ref($id));
&unlock_file("$domains_dir/$id");
}
# save_domain(&domain, [creating])
# Write domain information to disk
sub save_domain
{
local ($d, $creating) = @_;
local $file = "$domains_dir/$d->{'id'}";
if (!$creating && $d->{'id'} && !-r $file) {
# Deleted from under us! Don't save
print STDERR "Domain $file was deleted before saving!\n";
&print_call_stack() if ($gconfig{'error_stack'});
return 0;
}
if ($d->{'dom'} eq '') {
# Has no domain name!
print STDERR "Domain has no name!\n";
return 0;
}
&make_dir($domains_dir, 0700);
&lock_file($file);
local $oldd = { };
if (&read_file($file, $oldd)) {
my @st = stat($file);
if ($d->{'lastread_time'} && $st[9] > $d->{'lastread_time'}) {
print STDERR "Domain $file was modified since last read!\n";
&print_call_stack() if ($gconfig{'error_stack'});
}
}
if (!$d->{'created'}) {
$d->{'created'} = time();
$d->{'creator'} ||= $remote_user;
$d->{'creator'} ||= getpwuid($<);
}
$d->{'id'} ||= &domain_id();
$d->{'lastsave'} = time();
$d->{'lastsave_script'} = $ENV{'SCRIPT_NAME'} || $0;
$d->{'lastsave_user'} = $remote_user;
$d->{'lastsave_type'} = $main::webmin_script_type;
$d->{'lastsave_webmincron'} = $main::webmin_script_webmincron;
$d->{'lastsave_pid'} = $main::initial_process_id;
delete($d->{'lastread_time'});
&write_file($file, $d);
&unlock_file($file);
$d->{'lastread_time'} = time();
$main::get_domain_cache{$d->{'id'}} = $d;
if (scalar(@main::list_domains_cache)) {
@main::list_domains_cache =
&unique(@main::list_domains_cache, $d->{'id'});
}
# Only rebuild maps if something relevant changed
local $mchanged = 0;
foreach my $m (keys %get_domain_by_maps) {
$mchanged++ if ($d->{$m} ne $oldd->{$m});
}
if ($mchanged) {
&build_domain_maps();
}
&set_ownership_permissions(undef, undef, 0700, $file);
return 1;
}
# delete_domain(&domain)
# Delete all of Virtualmin's internal information about a domain
sub delete_domain
{
my ($d) = @_;
my $id = $d->{'id'};
&unlink_logged("$domains_dir/$id");
# And the bandwidth and plain-text password files
&unlink_file("$bandwidth_dir/$id");
&unlink_file("$plainpass_dir/$id");
&unlink_file("$hashpass_dir/$id");
&unlink_file("$nospam_dir/$id");
&unlink_file("$quota_cache_dir/$id");
if (defined(&get_autoreply_file_dir)) {
# Delete any autoreply file links
local $dir = &get_autoreply_file_dir();
opendir(AUTODIR, $dir);
foreach my $f (readdir(AUTODIR)) {
next if ($f eq "." || $f eq "..");
if ($f =~ /^\Q$id-\E/) {
unlink("$dir/$f");
}
}
closedir(AUTODIR);
}
# Delete scheduled backups owned by the domain owner, or that backup only
# this domain
foreach my $sched (&list_scheduled_backups()) {
my @dids = split(/\s+/, $sched->{'doms'});
if ($sched->{'owner'} eq $id) {
&delete_scheduled_backup($sched);
}
elsif (!$sched->{'all'} && !$sched->{'virtualmin'} &&
&indexof($id, @dids) >= 0) {
# If the backup is for specified domains, check if any of
# them exist and aren't this domain
my $candelete = 1;
foreach my $did (@dids) {
my $bd = &get_domain($did);
if ($bd && $id ne $bd->{'id'}) {
$candelete = 0;
last;
}
}
if ($candelete) {
&delete_scheduled_backup($sched);
}
}
}
# Delete any script notifications logs
if (-r $script_warnings_file) {
&read_file($script_warnings_file, \%warnsent);
foreach my $key (keys %warnsent) {
my ($keyid) = split(/\//, $key);
delete($warnsent{$key}) if ($keyid eq $id);
}
&write_file($script_warnings_file, \%warnsent);
}
# Delete script install logs
&unlink_file("$script_log_directory/$id");
# Delete differential backup file
&unlink_file("$incremental_backups_dir/$id");
# Delete cached links for the domain
&clear_links_cache($d);
# Delete any saved aliases
&unlink_file("$saved_aliases_dir/$id");
# Release any lock on the name
&unlock_file("$domainnames_dir/$d->{'dom'}");
&unlink_file("$domainnames_dir/$d->{'dom'}.lock");
# Remove from caches
delete($main::get_domain_cache{$d->{'id'}});
if (scalar(@main::list_domains_cache)) {
@main::list_domains_cache = grep { $_ ne $d->{'id'} }
@main::list_domains_cache;
}
&build_domain_maps();
}
# build_domain_maps()
# Create the files used by get_domain_by to quickly lookup domains by user
# or parent
sub build_domain_maps
{
local @doms = &list_domains();
foreach my $m (keys %get_domain_by_maps) {
local %map;
foreach my $d (@doms) {
local $v = $d->{$m};
#next if ($v eq '');
if (!defined($map{$v})) {
$map{$v} = $d->{'id'};
}
else {
$map{$v} .= " ".$d->{'id'};
}
}
&write_file($get_domain_by_maps{$m}, \%map);
}
}
# supports_firstname()
# Returns 1 if users can have a first name and surname
sub supports_firstname
{
&require_useradmin();
if ($usermodule eq "ldap-useradmin") {
my %luconfig = &foreign_config("ldap-useradmin");
return $luconfig{'given'} ? 1 : 0;
}
return 0;
}
# list_domain_users([&domain], [skipunix], [no-virts], [no-quotas], [no-dbs], [include-extra])
# List all Unix users who are in the domain's primary group.
# If domain is omitted, returns local users.
sub list_domain_users
{
local ($d, $skipunix, $novirts, $noquotas, $nodbs, $includeextra) = @_;
# Get all aliases (and maybe generics) to look for those that match users
local (%aliases, %generics);
if ($config{'mail'} && !$novirts) {
&require_mail();
if ($mail_system == 1) {
# Find Sendmail aliases for users
%aliases = map { $_->{'name'}, $_ } grep { $_->{'enabled'} }
&sendmail::list_aliases($sendmail_afiles);
}
elsif ($mail_system == 0) {
# Find Postfix aliases for users
%aliases = map { $_->{'name'}, $_ }
&$postfix_list_aliases($postfix_afiles);
}
if ($config{'generics'}) {
%generics = &get_generics_hash();
}
}
# Get all virtusers to look for those for users
local @virts;
if (!$novirts) {
@virts = &list_virtusers();
}
# Are we setting quotas individually?
local $ind_quota = 0;
if (&has_quota_commands() && $config{'quota_get_user_command'} && $d) {
$ind_quota = 1;
}
local @users = &list_all_users_quotas($noquotas || $ind_quota);
if ($d) {
# Limit to domain users.
@users = grep { $d->{'gid'} ne '' &&
$_->{'gid'} == $d->{'gid'} ||
$_->{'user'} eq $d->{'user'} } @users;
foreach my $u (@users) {
if ($u->{'user'} eq $d->{'user'}) {
# Virtual server owner
$u->{'domainowner'} = 1;
if ($d->{'hashpass'}) {
$u->{'pass_crypt'} = $d->{'crypt_enc_pass'};
$u->{'pass_md5'} = $d->{'md5_enc_pass'};
$u->{'pass_mysql'} = $d->{'mysql_enc_pass'};
$u->{'pass_digest'} = $d->{'digest_enc_pass'};
}
}
elsif ($u->{'uid'} == $d->{'uid'}) {
# Web management user
$u->{'webowner'} = 1;
$u->{'noquota'} = 1;
$u->{'noprimary'} = 1;
$u->{'noextra'} = 1;
$u->{'noalias'} = 1;
$u->{'nocreatehome'} = 1;
$u->{'nomailfile'} = 1;
delete($u->{'email'});
}
if ($ind_quota && !$noquotas) {
# Call quota getting command for each user
local $out = &run_quota_command(
"get_user", $u->{'user'});
local ($used, $soft, $hard) = split(/\s+/, $out);
$u->{'softquota'} = $soft;
$u->{'hardquota'} = $hard;
$u->{'uquota'} = $used;
}
}
local @subdoms;
if ($d->{'parent'}) {
# This is a subdomain - exclude parent domain users
@users = grep { $_->{'home'} =~ /^$d->{'home'}(\/|$)/ } @users;
}
elsif (@subdoms = &get_domain_by("parent", $d->{'id'})) {
# This domain has subdomains - exclude their users
@users = grep { $_->{'home'} !~ /^$d->{'home'}\/domains(\/|$)/ } @users;
}
@users = grep { !$_->{'domainowner'} } @users
if ($skipunix || $d->{'parent'});
# Remove users with @ in their names for whom a user with the @ replace
# already exists (for Postfix)
if ($mail_system == 0) {
local %umap = map { &replace_atsign($_->{'user'}), $_ }
grep { $_->{'user'} =~ /\@/ } @users;
@users = grep { !$umap{$_->{'user'}} } @users;
}
# Find users with broken home dir (not under homes, or
# domain's home, or public_html (for web ftp users))
local $phd = &public_html_dir($d);
foreach my $u (@users) {
if (!$u->{'webowner'} && $u->{'home'} &&
$u->{'home'} !~ /^$d->{'home'}\/$config{'homes_dir'}\// &&
!&is_under_directory($d->{'home'}, $u->{'home'})) {
# Home dir is outside domain's home base somehow
$u->{'brokenhome'} = 1;
}
elsif ($u->{'webowner'} && $u->{'home'} &&
!&is_under_directory($phd, $u->{'home'}) &&
$u->{'home'} ne $d->{'home'}) {
# Website FTP user's home dir must be under public_html
# or domain's home
$u->{'brokenhome'} = 1;
}
elsif (!$u->{'webowner'} && $u->{'home'} eq $d->{'home'}) {
# Home dir is equal to domain's dir, which is invalid,
# unless it is the web owner user (FTP user)
$u->{'brokenhome'} = 1;
}
}
if ($d->{'hashpass'}) {
# Merge in encrypted passwords
&read_file_cached("$hashpass_dir/$d->{'id'}", \%hash);
foreach my $u (@users) {
foreach my $s (@hashpass_types) {
$u->{'pass_'.$s} = $hash{$u->{'user'}.' '.$s};
}
}
}
else {
# Merge in plain text passwords
local (%plain, $need_plainpass_save);
&read_file_cached("$plainpass_dir/$d->{'id'}", \%plain);
foreach my $u (@users) {
if ($u->{'domainowner'}) {
# The domain owner's password is always known
$u->{'plainpass'} = $d->{'pass'};
}
elsif (!defined($u->{'plainpass'}) &&
defined($plain{$u->{'user'}})) {
# Check if the plain password is valid, in case
# the crypted password was changed behind
# our back
if ($plain{$u->{'user'}." encrypted"} eq
$u->{'pass'} ||
&encrypt_user_password(
$u, $plain{$u->{'user'}}) eq
$u->{'pass'} ||
&safe_unix_crypt($plain{$u->{'user'}},
$u->{'pass'})
eq $u->{'pass'}) {
# Valid - we can use it
$u->{'plainpass'} =$plain{$u->{'user'}};
if (!defined($plain{$u->{'user'}.
" encrypted"})) {
# Save the correct crypted
# version now
$plain{$u->{'user'}.
" encrypted"} =
$u->{'pass'};
$need_plainpass_save = 1;
}
}
else {
# We know it is wrong, so remove from
# the plain password cache file
delete($plain{$u->{'user'}});
delete($plain{$u->{'user'}." encrypted"});
$need_plainpass_save = 1;
}
}
}
if ($need_plainpass_save) {
&write_file("$plainpass_dir/$d->{'id'}", \%plain);
}
}
}
else {
# Limit to local users
local @lg = getgrnam($config{'localgroup'});
@users = grep { $_->{'gid'} == $lg[2] } @users;
}
# Set appropriate quota field
local $tmpl = &get_template($d ? $d->{'template'} : 0);
local $qtype = $tmpl->{'quotatype'};
local $u;
foreach $u (@users) {
$u->{'quota'} = $u->{$qtype.'quota'} if (!defined($u->{'quota'}));
$u->{'mquota'} = $u->{$qtype.'mquota'} if (!defined($u->{'mquota'}));
}
# Merge in cached quotas
if ($d) {
my $qfile = $quota_cache_dir."/".$d->{'id'};
my %qcache;
&read_file_cached($qfile, \%qcache);
foreach $u (@users) {
$u->{'quota_cache'} = $qcache{$u->{'user'}."_quota"};
$u->{'mquota_cache'} = $qcache{$u->{'user'}."_mquota"};
}
}
# Check if spamc is being used
local $spamc;
if ($d && $d->{'spam'}) {
local $spamclient = &get_domain_spam_client($d);
$spamc = 1 if ($spamclient =~ /spamc/);
}
# Detect user who are close to their quota
if (&has_home_quotas()) {
local $bsize = "a_bsize("home");
foreach $u (@users) {
local $diff = $u->{'quota'}*$bsize - $u->{'uquota'}*$bsize;
if ($u->{'quota'} && $diff < $quota_spam_margin &&
$d->{'spam'} && !$spamc) {
# Close to quota, which will block spamassassin ..
$u->{'spam_quota'} = 1;
$u->{'spam_quota_diff'} = $diff < 0 ? 0 : $diff;
}
if ($u->{'quota'} && $u->{'uquota'} >= $u->{'quota'}) {
# At or over quota
$u->{'over_quota'} = 1;
}
elsif ($u->{'quota'} && $u->{'uquota'} >= $u->{'quota'}*0.95) {
# Over 95% of quota
$u->{'warn_quota'} = 1;
}
}
}
# Add user-configured recovery addresses
if (!$novirts) {
foreach my $u (@users) {
if (!defined($u->{'recovery'})) {
my $recovery = &write_as_mailbox_user($u,
sub { &read_file_contents(
"$u->{'home'}/.usermin/changepass/recovery") });
$recovery =~ s/\r|\n//g;
$u->{'recovery'} = $recovery;
}
}
}
if (!$novirts) {
# Add email addresses and forwarding addresses to user structures
foreach my $u (@users) {
$u->{'email'} = $u->{'virt'} = undef;
$u->{'alias'} = $u->{'to'} = $u->{'generic'} = undef;
$u->{'extraemail'} = $u->{'extravirt'} = undef;
local ($al, $va);
if ($al = $aliases{&escape_alias($u->{'user'})}) {
$u->{'alias'} = $al;
$u->{'to'} = $al->{'values'};
}
elsif ($va = $valiases{"$u->{'user'}\@$d->{'dom'}"}) {
$u->{'valias'} = $va;
$u->{'to'} = $va->{'to'};