-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
feature-mysql.pl
executable file
·2842 lines (2672 loc) · 79.7 KB
/
feature-mysql.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
sub require_mysql
{
return if ($require_mysql++);
$mysql::use_global_login = 1;
&foreign_require("mysql");
if (!$mysql::config{'login'}) {
$mysql::config{'login'} = 'root';
$mysql::mysql_login ||= 'root';
$mysql::authstr = &mysql::make_authstr();
}
%mconfig = &foreign_config("mysql");
$password_func = $mysql::password_func || "password";
$mysql_user_size = $config{'mysql_user_size'} || 16;
# Try to get the mysql server version, but fall back to the local version
eval {
local $main::error_must_die = 1;
if (defined(&mysql::get_remote_mysql_version)) {
$remote_mysql_version = &mysql::get_remote_mysql_version();
}
};
if ($@ || !$remote_mysql_version) {
$remote_mysql_version = $mysql::mysql_version;
}
}
# check_depends_mysql(&dom)
# Ensure that a sub-server has a parent server with MySQL enabled
sub check_depends_mysql
{
return undef if (!$_[0]->{'parent'});
local $parent = &get_domain($_[0]->{'parent'});
return $text{'setup_edepmysql'} if (!$parent->{'mysql'});
return undef;
}
# check_anti_depends_mysql(&dom)
# Ensure that a parent server without MySQL does not have any children with it
sub check_anti_depends_mysql
{
if (!$_[0]->{'mysql'}) {
local @subs = &get_domain_by("parent", $_[0]->{'id'});
foreach my $s (@subs) {
return $text{'setup_edepmysqlsub'} if ($s->{'mysql'});
}
}
return undef;
}
# check_mysql_clash(&domain, [field])
# Returns 1 if some MySQL user or database is used by another domain
sub check_mysql_clash
{
local ($d, $field) = @_;
local @doms = grep { $_->{'mysql'} && $_->{'id'} ne $d->{'id'} }
&list_domains();
# Check for DB clash
if (!$field || $field eq 'db') {
foreach my $od (@doms) {
foreach my $db (split(/\s+/, $od->{'db_mysql'})) {
if ($db eq $d->{'db'}) {
return &text('setup_emysqldbdom', $d->{'db'},
&show_domain_name($od));
}
}
}
}
# Check for user clash
if (!$d->{'parent'} && (!$field || $field eq 'user')) {
foreach my $od (@doms) {
if (&mysql_user($d) eq &mysql_user($od)) {
return &text('setup_emysqluserdom', &mysql_user($d),
&show_domain_name($od));
}
}
}
return undef;
}
# setup_mysql(&domain, [no-db])
# Create a new MySQL database, user and permissions
sub setup_mysql
{
local ($d, $nodb) = @_;
local $tmpl = &get_template($d->{'template'});
&require_mysql();
$d->{'mysql_user'} = &mysql_user($d);
local $user = $d->{'mysql_user'};
if ($d->{'provision_mysql'}) {
# Create the user on provisioning server
if (!$d->{'parent'}) {
&$first_print($text{'setup_mysqluser_provision'});
my $info = { 'user' => $user,
'domain-owner' => '' };
if ($d->{'mysql_enc_pass'}) {
$info->{'encpass'} = $d->{'mysql_enc_pass'};
}
else {
$info->{'pass'} = &mysql_pass($d);
}
local @hosts = &unique(map { &to_ipaddress($_) }
&get_mysql_hosts($d, 2));
$info->{'remote'} = \@hosts;
my $conns = &get_mysql_user_connections($d, 0);
$info->{'conns'} = $conns if ($conns);
my ($ok, $msg) = &provision_api_call(
"provision-mysql-login", $info, 0);
if (!$ok) {
&$second_print(
&text('setup_emysqluser_provision', $msg));
return 0;
}
# Configure MySQL module to use that system from now on
my $mysql_host = $msg =~ /host=(\S+)/ ? $1 : undef;
my $mysql_user = $msg =~ /owner_user=(\S+)/ ? $1 : undef;
my $mysql_pass = $msg =~ /owner_pass=(\S+)/ ? $1 : undef;
my @mdoms = grep { $_->{'mysql'} && $_->{'id'} ne $d->{'id'} }
&list_domains();
if (!$mysql::config{'host'} || !@mdoms) {
# Configure MySQL module
$mysql::config{'host'} = $mysql_host;
$mysql::config{'login'} = $mysql_user;
$mysql::config{'pass'} = $mysql_pass;
$mysql::mysql_login = $mysql_user;
$mysql::mysql_pass = $mysql_pass;
$mysql::authstr = &mysql::make_authstr();
&mysql::save_module_config(\%mysql::config, 'mysql');
}
elsif ($mysql::config{'host'} ne $mysql_host) {
# Mis-match with current setting!?
&$second_print(&text('setup_emysqluser_provclash',
$mysql::config{'host'},
$mysql_host));
return 0;
}
&$second_print(&text('setup_mysqluser_provisioned',
$mysql_host));
}
}
else {
# Create the user
local @hosts = &get_mysql_hosts($d, 1);
if (&indexof("%", @hosts) >= 0 &&
&indexof("localhost", @hosts) < 0 &&
&indexof("127.0.0.1", @hosts) < 0) {
# Always add localhost if % was allowed
push(@hosts, "localhost");
}
local $wild = &substitute_domain_template($tmpl->{'mysql_wild'}, $d);
if (!$d->{'parent'}) {
&$first_print($text{'setup_mysqluser'});
local $cfunc = sub {
local $encpass = &encrypted_mysql_pass($d);
foreach my $h (@hosts) {
&mysql::execute_sql_logged($mysql::master_db,
"delete from user where host = '$h' ".
"and user = '$user'");
&execute_user_creation_sql($h, $user,$encpass);
if ($wild && $wild ne $d->{'db'}) {
&add_db_table($h, $wild, $user);
}
&set_mysql_user_connections($d, $h, $user, 0);
}
&mysql::execute_sql_logged($mysql::master_db,
'flush privileges');
};
&execute_for_all_mysql_servers($cfunc);
&$second_print($text{'setup_done'});
}
}
# Create the initial DB (if requested)
local $ok;
if (!$nodb && $tmpl->{'mysql_mkdb'} && !$d->{'no_mysql_db'}) {
local $opts = &default_mysql_creation_opts($d);
$ok = &create_mysql_database($d, $d->{'db'}, $opts);
if (!$ok) {
# Failed, but instread of marking this whole feature as failed,
# just record that there was no DB
$d->{'db_mysql'} = "";
$ok = 1;
}
}
else {
# No DBs can exist
$ok = 1;
$d->{'db_mysql'} = "";
}
# Save the initial password
if ($tmpl->{'mysql_nopass'}) {
&set_mysql_pass($d, &mysql_pass($d, 1));
}
return $ok;
}
# add_db_table(host, db, user)
# Adds an entry to the db table, with all permission columns set to Y
sub add_db_table
{
local ($host, $db, $user) = @_;
local @str = &mysql::table_structure($mysql::master_db, 'db');
local ($s, @fields, @yeses);
foreach $s (@str) {
if ($s->{'field'} =~ /_priv$/i) {
push(@fields, $s->{'field'});
push(@yeses, "'Y'");
}
}
local $qdb = "e_mysql_database($db);
&mysql::execute_sql_logged($mysql::master_db, "delete from db where host = '$host' and db = '$qdb' and user = '$user'");
&mysql::execute_sql_logged($mysql::master_db, "insert into db (host, db, user, ".join(", ", @fields).") values ('$host', '$qdb', '$user', ".join(", ", @yeses).")");
}
# delete_mysql(&domain, [preserve-remote])
# Delete mysql databases, the domain's mysql user and all permissions for both
sub delete_mysql
{
local ($d, $preserve) = @_;
&require_mysql();
my @dblist = &unique(split(/\s+/, $d->{'db_mysql'}));
# If MySQL is hosted remotely, don't delete the DB on the assumption that
# other servers sharing the DB will still be using it
if ($preserve && &remote_mysql($d)) {
&$first_print(&text('delete_mysqldb', join(" ", @dblist)));
&$second_print(&text('delete_mysqlpreserve', $mysql::config{'host'}));
return 1;
}
# Get the domain's users, so we can remove their MySQL logins
local @users = &list_domain_users($d, 1, 1, 1, 0);
# First remove the databases
if ($d->{'db_mysql'}) {
&delete_mysql_database($d, @dblist);
}
if ($d->{'provision_mysql'}) {
# Remove the main user on the provisioning server
if (!$d->{'parent'}) {
&$first_print($text{'delete_mysqluser_provision'});
my $info = { 'user' => &mysql_user($d),
'host' => $mysql::config{'host'} };
my ($ok, $msg) = &provision_api_call(
"unprovision-mysql-login", $info, 0);
if ($ok) {
&$second_print($text{'setup_done'});
}
else {
&$second_print(&text('delete_emysqluser_provision',
$msg));
return 0;
}
}
# Take away access from mailbox users
foreach my $u (@users) {
my @mydbs = grep { $_->{'type'} eq 'mysql' } @{$u->{'dbs'}};
if (@mydbs) {
&delete_mysql_database_user($d, $u->{'user'});
}
}
# If this was the last domain with MySQL enabled on the system,
# turn off use of the remote host that if it gets enabled again, a
# new host and login are used
my @mdoms = grep { $_->{'mysql'} && $_->{'id'} ne $d->{'id'} }
&list_domains();
if (!@mdoms && $mysql::config{'host'}) {
delete($mysql::config{'host'});
$mysql::authstr = &mysql::make_authstr();
&mysql::save_module_config(\%mysql::config, 'mysql');
}
}
else {
# Remove the main user locally
&$first_print($text{'delete_mysqluser'}) if (!$d->{'parent'});
local $dfunc = sub {
local $user = &mysql_user($d);
local $tmpl = &get_template($d->{'template'});
local $wild = &substitute_domain_template(
$tmpl->{'mysql_wild'}, $d);
if (!$d->{'parent'}) {
# Delete the user and any database permissions
&mysql::execute_sql_logged($mysql::master_db,
"delete from user where user = '$user'");
&mysql::execute_sql_logged($mysql::master_db,
"delete from db where user = '$user'");
}
if ($wild && $wild ne $d->{'db'}) {
# Remove any wildcard entry for the user
&mysql::execute_sql_logged($mysql::master_db,
"delete from db where db = '$wild'");
}
# Remove any other users. This has to be done here, as when
# users in the domain are deleted they won't be able to find
# their database privileges anymore.
foreach my $u (@users) {
foreach my $udb (@{$u->{'dbs'}}) {
if ($udb->{'type'} eq 'mysql') {
local $myuser =
&mysql_username($u->{'user'});
&mysql::execute_sql_logged(
$mysql::master_db,
"delete from user where user = ?",
$myuser);
&mysql::execute_sql_logged(
$mysql::master_db,
"delete from db where user = ?",
$myuser);
}
}
}
&mysql::execute_sql_logged(
$mysql::master_db, 'flush privileges');
};
&execute_for_all_mysql_servers($dfunc);
&$second_print($text{'setup_done'}) if (!$d->{'parent'});
}
return 1;
}
# modify_mysql(&domain, &olddomain)
# Changes the mysql user's password if needed
sub modify_mysql
{
local ($d, $oldd) = @_;
local $tmpl = &get_template($d->{'template'});
&require_mysql();
local $rv = 0;
local $changeduser = $d->{'user'} ne $oldd->{'user'} &&
!$tmpl->{'mysql_nouser'} ? 1 : 0;
local $olduser = &mysql_user($oldd);
local $user = &mysql_user($d, $changeduser);
local $oldencpass = &encrypted_mysql_pass($oldd);
local $encpass = &encrypted_mysql_pass($d);
local @dbnames = map { $_->{'name'} } &domain_databases($d, [ "mysql" ]);
if ($encpass ne $oldencpass && !$d->{'parent'} && !$oldd->{'parent'} &&
(!$tmpl->{'mysql_nopass'} || $d->{'mysql_pass'})) {
# Change MySQL password, for a top-level server that isn't being
# converted from a sub-server
if ($d->{'provision_mysql'}) {
# Change on provisioning server
&$first_print($text{'save_mysqlpass_provision'});
my $info = { 'user' => &mysql_user($d),
'host' => $mysql::config{'host'} };
if ($d->{'mysql_enc_pass'}) {
$info->{'encpass'} = $d->{'mysql_enc_pass'};
}
else {
$info->{'pass'} = &mysql_pass($d);
}
my ($ok, $msg) = &provision_api_call("modify-mysql-login",
$info, 0);
if (!$ok) {
&$second_print(&text('save_emysqlpass_provision',$msg));
}
else {
&$second_print($text{'setup_done'});
}
$rv++;
}
else {
# Change locally
&$first_print($text{'save_mysqlpass'});
if (&mysql_user_exists($d)) {
local $pfunc = sub {
&execute_password_change_sql(
$olduser, $encpass);
};
&execute_for_all_mysql_servers($pfunc);
&$second_print($text{'setup_done'});
$rv++;
}
else {
&$second_print($text{'save_nomysql'});
}
}
}
if (!$d->{'parent'} && $oldd->{'parent'}) {
# Server has been converted to a parent .. need to create user, and
# change access to old DBs
$d->{'mysql_user'} = &mysql_user($d, 1);
local $user = $d->{'mysql_user'};
local @hosts = &get_mysql_hosts($d);
# If hashed passwords are in use, generate a random MySQL password
# for the new MySQL user
if ($tmpl->{'hashpass'}) {
$d->{'mysql_pass'} = &random_password(8);
delete($d->{'mysql_enc_pass'});
}
if ($d->{'provision_mysql'}) {
# Change on provisioning server .. first create new user
&$first_print($text{'setup_mysqluser_provision'});
my $info = { 'user' => $user,
'domain-owner' => '' };
if ($d->{'mysql_enc_pass'}) {
$info->{'encpass'} = $d->{'mysql_enc_pass'};
}
else {
$info->{'pass'} = &mysql_pass($d);
}
local @hosts = map { &to_ipaddress($_) } @hosts;
$info->{'remote'} = \@hosts;
my $conns = &get_mysql_user_connections($d, 0);
$info->{'conns'} = $conns if ($conns);
my ($ok, $msg) = &provision_api_call(
"provision-mysql-login", $info, 0);
if (!$ok) {
&$second_print(
&text('setup_emysqluser_provision', $msg));
}
# Then take away DBs from old user
if ($ok && @dbnames) {
my $info = { 'user' => $olduser,
'host' => $mysql::config{'host'},
'remove-database' => \@dbnames };
($ok, $msg) = &provision_api_call(
"modify-mysql-login", $info, 0);
if (!$ok) {
&$second_print(
&text('save_emysqluser2_provision', $msg));
}
}
# Grant to new user
if ($ok && @dbnames) {
my $info = { 'user' => $user,
'host' => $mysql::config{'host'},
'add-database' => \@dbnames };
($ok, $msg) = &provision_api_call(
"modify-mysql-login", $info, 0);
if (!$ok) {
&$second_print(
&text('save_emysqluser2_provision2', $msg));
}
}
if ($ok) {
&$second_print($text{'setup_done'});
}
}
else {
# Change locally
&$first_print($text{'setup_mysqluser'});
local $wild = &substitute_domain_template(
$tmpl->{'mysql_wild'}, $d);
local $encpass = &encrypted_mysql_pass($d);
local $pfunc = sub {
local $h;
foreach $h (@hosts) {
&execute_user_creation_sql($h, $user,$encpass);
if ($wild && $wild ne $d->{'db'}) {
&add_db_table($h, $wild, $user);
}
&set_mysql_user_connections($d, $h, $user, 0);
}
foreach my $db (@dbnames) {
local $qdb = "e_mysql_database($db);
&mysql::execute_sql_logged($mysql::master_db,
"update db set user = ? where user = ? and ".
"(db = ? or db = ?)",
$user, $olduser, $db, $qdb);
}
&mysql::execute_sql_logged($mysql::master_db,
'flush privileges');
};
&execute_for_all_mysql_servers($pfunc);
&$second_print($text{'setup_done'});
}
$rv++;
}
elsif ($d->{'parent'} && !$oldd->{'parent'}) {
# Server has changed from parent to sub-server .. need to remove the
# old user and update all DB permissions
if ($d->{'provision_mysql'}) {
# Update on provisioning server .. first remove ownership
# of all DBs
&$first_print($text{'save_mysqluser_provision'});
my ($ok, $msg) = (1, undef);
if (@dbnames) {
my $info = { 'user' => $olduser,
'host' => $mysql::config{'host'},
'remove-database' => \@dbnames };
($ok, $msg) = &provision_api_call(
"modify-mysql-login", $info, 0);
if (!$ok) {
&$second_print(
&text('save_emysqluser2_provision', $msg));
}
}
# Then remove the user
if ($ok && $mysql::config{'host'}) {
my $info = { 'user' => $olduser,
'host' => $mysql::config{'host'} };
($ok, $msg) = &provision_api_call(
"unprovision-mysql-login", $info, 0);
if (!$ok) {
&$second_print(
&text('save_emysqluser_provision',$msg));
}
}
# Then grant DBs to new user
if ($ok && @dbnames) {
my $info = { 'user' => $user,
'host' => $mysql::config{'host'},
'add-database' => \@dbnames };
($ok, $msg) = &provision_api_call(
"modify-mysql-login", $info, 0);
if (!$ok) {
&$second_print(
&text('save_emysqluser2_provision2', $msg));
}
}
if ($ok) {
&$second_print($text{'setup_done'});
}
}
else {
# Update locally
&$first_print($text{'save_mysqluser'});
local $pfunc = sub {
&mysql::execute_sql_logged($mysql::master_db,
"delete from user where user = ?", $olduser);
&mysql::execute_sql_logged($mysql::master_db,
"update db set user = ? where user = ?",
$user, $olduser);
&mysql::execute_sql_logged($master_db,
'flush privileges');
};
&execute_for_all_mysql_servers($pfunc);
&$second_print($text{'setup_done'});
$rv++;
}
}
elsif ($user ne $olduser && !$d->{'parent'}) {
# MySQL user in a parent domain has changed, perhaps due to username
# change. Need to update user in DB and all db entries
if ($d->{'provision_mysql'}) {
# Rename on provisioning server
&$first_print($text{'save_mysqluser_provision'});
my $info = { 'user' => $olduser,
'host' => $mysql::config{'host'},
'new-user' => $user };
my ($ok, $msg) = &provision_api_call(
"modify-mysql-login", $info, 0);
if (!$ok) {
&$second_print(&text('save_emysqluser_provision',$msg));
}
else {
&$second_print($text{'setup_done'});
}
$rv++;
}
else {
# Rename locally
&$first_print($text{'save_mysqluser'});
if (&mysql_user_exists($oldd)) {
$d->{'mysql_user'} = $user;
local $pfunc = sub {
&mysql::execute_sql_logged($mysql::master_db,
"update user set user = ? where user = ?",
$user, $olduser);
&mysql::execute_sql_logged($mysql::master_db,
"update db set user = ? where user = ?",
$user, $olduser);
&mysql::execute_sql_logged($master_db,
"flush privileges");
};
&execute_for_all_mysql_servers($pfunc);
&$second_print($text{'setup_done'});
$rv++;
}
else {
&$second_print($text{'save_nomysql'});
}
}
}
elsif ($user ne $olduser && $d->{'parent'} && @dbnames) {
# Sub-server has moved to a new user .. change ownership of DBs
if ($d->{'provision_mysql'}) {
# Change on provisioning server, by removing DBs from the old
# owner's list, and added to new owner's list
&$first_print($text{'save_mysqluser2_provision'});
my $info = { 'user' => $olduser,
'host' => $mysql::config{'host'},
'remove-database' => \@dbnames };
my ($ok, $msg) = &provision_api_call(
"modify-mysql-login", $info, 0);
if (!$ok) {
&$second_print(
&text('save_emysqluser2_provision', $msg));
}
else {
# Add databases back to the new owner
my $info = { 'user' => $user,
'host' => $mysql::config{'host'},
'add-database' => \@dbnames };
my ($ok, $msg) = &provision_api_call(
"modify-mysql-login", $info, 0);
if (!$ok) {
&$second_print(
&text('save_emysqluser2_provision2', $msg));
}
else {
&$second_print($text{'setup_done'});
}
}
$rv++;
}
else {
# Change locally
&$first_print($text{'save_mysqluser2'});
local $pfunc = sub {
foreach my $db (@dbnames) {
local $qdb = "e_mysql_database($db);
&mysql::execute_sql_logged($mysql::master_db,
"update db set user = ? where user = ? ".
"and (db = ? or db = ?)",
$user, $olduser, $db, $qdb);
}
&mysql::execute_sql_logged($master_db,
'flush privileges');
};
&execute_for_all_mysql_servers($pfunc);
$rv++;
&$second_print($text{'setup_done'});
}
}
if ($d->{'group'} ne $oldd->{'group'} && $tmpl->{'mysql_chgrp'}) {
# Unix group has changed - fix permissions on all DB files
&$first_print($text{'save_mysqlgroup'});
foreach my $db (&domain_databases($d, [ "mysql" ])) {
local $dd = &get_mysql_database_dir($db->{'name'});
if ($dd) {
&system_logged("chgrp -R $d->{'group'} ".
quotemeta($dd));
}
}
&$second_print($text{'setup_done'});
}
return $rv;
}
# clone_mysql(&domain, &old-domain)
# Copy all databases and their contents to a new domain
sub clone_mysql
{
local ($d, $oldd) = @_;
&$first_print($text{'clone_mysql'});
# Re-create each DB with a new name
local %dbmap;
my @dbs = &domain_databases($oldd, [ 'mysql' ]);
foreach my $db (@dbs) {
local $newname = $db->{'name'};
local $newprefix = &fix_database_name($d->{'prefix'}, 'mysql');
local $oldprefix = &fix_database_name($oldd->{'prefix'}, 'mysql');
if ($newname eq $oldd->{'db'} &&
$oldd->{'db'} eq &database_name($oldd)) {
# If the DB name was the primary database for the old domain,
# set the new DB name to be the primary database for the new
# domain
$newname = $d->{'db'};
}
elsif ($newname !~ s/\Q$oldprefix\E/$newprefix/) {
# Otherwise, just replace the DB name prefix. If that isn't
# possible, prepend the new prefix as a last resort or just
# use the new prefix if this is the only database in the domain
&$second_print(&text('clone_mysqlprefix', $newname,
$oldprefix, $newprefix));
if (@dbs == 1 && !&check_mysql_database_clash($d, $newprefix)) {
# This domain has only one database, so we can just use
# the new prefix directly (as long as it doesn't clash)
$newname = $newprefix;
}
else {
# Prepend new prefix
$newname = $newprefix.$newname;
}
&$second_print(&text('clone_mysqlprefix2', $newname));
}
if (&check_mysql_database_clash($d, $newname)) {
&$second_print(&text('clone_mysqlclash', $newname));
next;
}
&push_all_print();
&set_all_null_print();
local $opts = &get_mysql_creation_opts($oldd, $db->{'name'});
local $ok = &create_mysql_database($d, $newname, $opts);
&pop_all_print();
if (!$ok) {
&$second_print(&text('clone_mysqlcreate', $newname));
}
else {
$dbmap{$newname} = $db->{'name'};
}
}
&$second_print(&text('clone_mysqldone', scalar(keys %dbmap)));
# Copy across contents
if (%dbmap) {
&require_mysql();
&$first_print($text{'clone_mysqlcopy'});
foreach my $db (&domain_databases($d, [ 'mysql' ])) {
local $oldname = $dbmap{$db->{'name'}};
local $temp = &transname();
local $err = &mysql::backup_database($oldname, $temp, 0, 1, 0,
undef, undef, undef, undef, 1);
if ($err) {
&$second_print(&text('clone_mysqlbackup',
$oldname, $err));
next;
}
local ($ex, $out) = &mysql::execute_sql_file($db->{'name'},
$temp);
&unlink_file($temp);
if ($ex) {
&$second_print(&text('clone_mysqlrestore',
$db->{'name'}, $out));
next;
}
}
&$second_print($text{'setup_done'});
}
if (!$d->{'parent'}) {
# Duplicate allowed hosts
local @allowed = &get_mysql_allowed_hosts($oldd);
&save_mysql_allowed_hosts($d, \@allowed);
}
}
# validate_mysql(&domain)
# Make sure all MySQL databases exist, and that the admin user exists
sub validate_mysql
{
local ($d) = @_;
&require_mysql();
if ($d->{'provision_mysql'}) {
# Check login on provisioning server
my ($ok, $msg) = &provision_api_call(
"check-mysql-login", { 'user' => &mysql_user($d) });
if (!$ok) {
return &text('validate_emysqlcheck', $msg);
}
elsif ($msg !~ /host=(\S+)/) {
return &text('validate_emysqluser', &mysql_user($d));
}
elsif ($1 ne $mysql::config{'host'}) {
return &text('validate_emysqluserhost',
$1, $mysql::config{'host'});
}
# Check DBs on provisioning server
foreach my $db (&domain_databases($d, [ "mysql" ])) {
my ($ok, $msg) = &provision_api_call(
"check-mysql-database", { 'database' => $db->{'name'} });
if (!$ok) {
return &text('validate_emysqlcheck',
$db->{'name'}, $msg);
}
elsif ($msg !~ /host=(\S+)/) {
return &text('validate_emysql', $db->{'name'});
}
}
}
else {
# Check locally
local %got = map { $_, 1 } &mysql::list_databases();
foreach my $db (&domain_databases($d, [ "mysql" ])) {
$got{$db->{'name'}} ||
return &text('validate_emysql', $db->{'name'});
}
if (!&mysql_user_exists($d)) {
return &text('validate_emysqluser', &mysql_user($d));
}
}
return undef;
}
# disable_mysql(&domain)
# Modifies the mysql user for this domain so that he cannot login
sub disable_mysql
{
local ($d) = @_;
&require_mysql();
if ($d->{'parent'}) {
&$second_print($text{'save_nomysqlpar'});
}
elsif ($d->{'provision_mysql'}) {
# Lock on provisioning server
&$first_print($text{'disable_mysqluser_provision'});
my $info = { 'user' => &mysql_user($d),
'host' => $mysql::config{'host'},
'lock' => '' };
my ($ok, $msg) = &provision_api_call("modify-mysql-login", $info, 0);
if (!$ok) {
&$second_print(&text('disable_emysqluser_provision', $msg));
return 0;
}
else {
&$second_print($text{'setup_done'});
return 1;
}
}
else {
# Lock locally
&$first_print($text{'disable_mysqluser'});
local $user = &mysql_user($d);
if ($oldpass = &mysql_user_exists($d)) {
local $dfunc = sub {
&execute_password_change_sql($user, "'".("0" x 41)."'");
};
&execute_for_all_mysql_servers($dfunc);
$d->{'disabled_oldmysql'} = $oldpass;
&$second_print($text{'setup_done'});
return 1;
}
else {
&$second_print($text{'save_nomysql'});
return 0;
}
}
}
# enable_mysql(&domain)
# Puts back the original password for the mysql user so that he can login again
sub enable_mysql
{
local ($d) = @_;
&require_mysql();
if ($d->{'parent'}) {
&$second_print($text{'save_nomysqlpar'});
return 0;
}
elsif ($d->{'provision_mysql'}) {
# Unlock on provisioning server
&$first_print($text{'enable_mysql_provision'});
my $info = { 'user' => &mysql_user($d),
'host' => $mysql::config{'host'},
'unlock' => '' };
my ($ok, $msg) = &provision_api_call(
"modify-mysql-login", $info, 0);
if (!$ok) {
&$second_print(&text('enable_emysql_provision', $msg));
return 0;
}
else {
&$second_print($text{'setup_done'});
return 1;
}
}
else {
# Un-lock locally
&$first_print($text{'enable_mysql'});
local $user = &mysql_user($d);
if (&mysql_user_exists($d)) {
local $efunc = sub {
if ($d->{'disabled_oldmysql'}) {
local $qpass = &mysql_escape(
$d->{'disabled_oldmysql'});
&execute_password_change_sql($user, "'$qpass'");
}
else {
local $pass = &mysql_pass($d);
local $qpass = &mysql_escape($pass);
&execute_password_change_sql($user, "$password_func('$qpass')");
}
&mysql::execute_sql($master_db, 'flush privileges');
};
&execute_for_all_mysql_servers($efunc);
delete($d->{'disabled_oldmysql'});
&$second_print($text{'setup_done'});
return 1;
}
else {
&$second_print($text{'save_nomysql'});
return 0;
}
}
}
# mysql_user_exists(&domain)
# Returns his password if a mysql user exists for the domain's user, or undef
sub mysql_user_exists
{
&require_mysql();
local $user = &mysql_user($_[0]);
local $u;
eval {
# Try old password column first
local $main::error_must_die = 1;
$u = &mysql::execute_sql($mysql::master_db,
"select password from user where user = ?", $user);
};
if ($@ || @{$u->{'data'}} && $u->{'data'}->[0]->[0] eq '') {
# Try new mysql user table format if the password query failed, or
# if there was no password
eval {
local $main::error_must_die = 1;
$u = &mysql::execute_sql($mysql::master_db,
"select authentication_string from user where user = ?", $user);
};
}
foreach my $r (@{$u->{'data'}}) {
return $r->[0] if ($r->[0]);
}
return undef;
}
# check_warnings_mysql(&dom, &old-domain)
# Return warning if a MySQL database or user with a clashing name exists.
# This can be overridden to allow a takeover of the DB.
sub check_warnings_mysql
{
local ($d, $oldd) = @_;
$d->{'mysql'} && (!$oldd || !$oldd->{'mysql'}) || return undef;
if ($d->{'provision_mysql'}) {
# DB clash on provisioning server
my ($ok, $msg) = &provision_api_call(
"check-mysql-database", { 'database' => $d->{'db'} });
return &text('provision_emysqldbcheck', $msg) if (!$ok);
if ($msg =~ /host=/) {
return &text('provision_emysqldb', $d->{'db'});
}
# User clash on provisioning server
if (!$d->{'parent'}) {
my ($ok, $msg) = &provision_api_call(
"check-mysql-login", { 'user' => &mysql_user($d) });
return &text('provision_emysqlcheck', $msg) if (!$ok);
if ($msg =~ /host=/) {
return &text('provision_emysql', &mysql_user($d));
}
}
}
else {
# DB clash on local
&require_mysql();
local @dblist = &mysql::list_databases();
return &text('setup_emysqldb', $d->{'db'})
if (&indexof($d->{'db'}, @dblist) >= 0);
# User clash on local
if (!$d->{'parent'}) {
return &text('setup_emysqluser', &mysql_user($d))
if (&mysql_user_exists($d));
}
}
return undef;
}
# backup_mysql(&domain, file)
# Dumps this domain's mysql database to a backup file
sub backup_mysql
{
local ($d, $file) = @_;
&require_mysql();
# Find all domain's databases
local $tmpl = &get_template($d->{'template'});
local $wild = &substitute_domain_template($tmpl->{'mysql_wild'}, $d);
local @alldbs = &list_all_mysql_databases($d);
local @dbs;
if ($wild) {
$wild =~ s/\%/\.\*/g;
$wild =~ s/_/\./g;
@dbs = grep { /^$wild$/i } @alldbs;
}
push(@dbs, split(/\s+/, $d->{'db_mysql'}));
@dbs = &unique(@dbs);
# Filter out any excluded DBs
my @exclude = &get_backup_db_excludes($d);
my %exclude = map { $_, 1 } @exclude;
@dbs = grep { !$exclude{$_} } @dbs;
# Create base backup file with meta-information
local @hosts = &get_mysql_allowed_hosts($d);
local %info = ( 'hosts' => join(' ', @hosts),
'remote' => $mysql::config{'host'} );
&write_as_domain_user($d, sub { &write_file($file, \%info) });
# Back them all up