-
Notifications
You must be signed in to change notification settings - Fork 31
/
elevate-cpanel
executable file
·11517 lines (8305 loc) · 349 KB
/
elevate-cpanel
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
#!/usr/local/cpanel/3rdparty/bin/perl
## ----------------------------------------------------------------------------
##
## DO NOT EDIT THIS FILE
##
## This file is automatically generated from script/elevate-cpanel.PL
##
## view https://github.com/cpanel/elevate for more details
##
## ----------------------------------------------------------------------------
BEGIN { # Suppress load of all of these at earliest point.
$INC{'Elevate/Constants.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Base.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/AbsoluteSymlinks.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/AutoSSL.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/BootKernel.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/CCS.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/CloudLinux.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/cPanelPlugins.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/cPanelPrep.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/DatabaseUpgrade.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/DiskSpace.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Distros.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/DNS.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/EA4.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/ElevateScript.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/ELS.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Grub2ControlTest.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Grub2ChecksWorkarounds.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Imunify.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/InfluxDB.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/IsContainer.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/JetBackup.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/KernelCare.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Kernel.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Leapp.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Lists.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/LiteSpeed.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/MountPoints.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/MySQL.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/NICs.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/NixStats.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/OVH.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/PackageRestore.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/PackageDupes.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Panopta.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/PECL.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/PerlXS.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/PostgreSQL.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/R1Soft.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Repositories.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/RmMod.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/RpmDB.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/SSH.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Softaculous.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Ufw.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/UnconvertedModules.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/UpdateReleaseUpgrades.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/UpdateSystem.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/WHM.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/WPToolkit.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Components/Acronis.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/OS.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/OS/CentOS7.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/OS/CloudLinux7.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/OS/RHEL.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/OS/Ubuntu.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/OS/Ubuntu20.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/PkgMgr.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/PkgMgr/APT.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/PkgMgr/Base.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/PkgMgr/YUM.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Database.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/EA4.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Fetch.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Leapp.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Logger.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Marker.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Motd.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/NICs.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Notify.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Roles/Run.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Script.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Service.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/StageFile.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Stages.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/SystemctlService.pm'} = 'script/elevate-cpanel.PL.static';
$INC{'Elevate/Usage.pm'} = 'script/elevate-cpanel.PL.static';
}
{ # --- BEGIN lib/Elevate/Constants.pm
package Elevate::Constants;
use cPstrict;
use constant SERVICE_DIR => '/etc/systemd/system/';
use constant SERVICE_NAME => 'elevate-cpanel.service';
use constant LOG_FILE => q[/var/log/elevate-cpanel.log];
use constant PID_FILE => q[/var/run/elevate-cpanel.pid];
use constant DEFAULT_GRUB_FILE => '/etc/default/grub';
use constant YUM_REPOS_D => q[/etc/yum.repos.d];
use constant ELEVATE_BACKUP_DIR => "/root/.elevate.backup";
use constant RPMDB_DIR => q[/var/lib/rpm];
use constant RPMDB_BACKUP_DIR => q[/var/lib/rpm-elevate-backup];
use constant IMUNIFY_AGENT => '/usr/bin/imunify360-agent';
use constant CHKSRVD_SUSPEND_FILE => q[/var/run/chkservd.suspend];
use constant IGNORE_OUTDATED_SERVICES_FILE => q[/etc/cpanel/local/ignore_outdated_services];
use constant SBIN_IP => q[/sbin/ip];
use constant ETH_FILE_PREFIX => q[/etc/sysconfig/network-scripts/ifcfg-];
use constant R1SOFT_REPO => 'r1soft';
use constant R1SOFT_REPO_FILE => '/etc/yum.repos.d/r1soft.repo';
use constant R1SOFT_MAIN_AGENT_PACKAGE => 'serverbackup-agent';
use constant R1SOFT_AGENT_PACKAGES => qw{
r1soft-getmodule
serverbackup-agent
serverbackup-async-agent-2-6
serverbackup-enterprise-agent
serverbackup-setup
};
use constant ACRONIS_BACKUP_PACKAGE => 'acronis-backup-cpanel';
use constant ACRONIS_OTHER_PACKAGES => qw{
BackupAndRecoveryAgent
BackupAndRecoveryBootableComponents
dkms
file_protector
snapapi26_modules
};
use constant POSTGRESQL_SYSTEM_DATADIR => '/var/lib/pgsql/data';
use constant OVH_MONITORING_TOUCH_FILE => '/var/cpanel/acknowledge_ovh_monitoring_for_elevate';
1;
} # --- END lib/Elevate/Constants.pm
{ # --- BEGIN lib/Elevate/Components/Base.pm
package Elevate::Components::Base;
use cPstrict;
use Carp ();
use Cpanel::JSON ();
use Simple::Accessor qw(
components
);
# use Log::Log4perl qw(:easy);
INIT { Log::Log4perl->import(qw{:easy}); }
BEGIN {
my @_DELEGATE_TO_CPEV = qw{
getopt
upgrade_distro_manually
ssystem
ssystem_and_die
ssystem_capture_output
ssystem_hide_and_capture_output
};
foreach my $subname (@_DELEGATE_TO_CPEV) {
no strict 'refs';
*$subname = sub ( $self, @args ) {
my $cpev = $self->cpev or Carp::confess(qq[Cannot find cpev to call $subname]);
my $sub = $cpev->can($subname) or Carp::confess(qq[cpev does not support $subname]);
return $sub->( $cpev, @args );
}
}
}
sub _build_components {
if ( $0 =~ qr{\bt/} ) {
return Elevate::Components->new;
}
Carp::confess(q[Missing components]);
}
sub cpev ($self) {
return $self->components->cpev;
}
sub run_once ( $self, $subname ) {
my $cpev = $self->cpev;
my $run_once = $cpev->can('run_once') or Carp::confess(qq[cpev does not support 'run_once']);
my $label = ref($self) . "::$subname";
my $sub = $self->can($subname) or Carp::confess(qq[$self does not support '$subname']);
my $code = sub {
return $sub->($self);
};
return $run_once->( $cpev, $label, $code );
}
sub is_check_mode ( $self, @args ) {
return $self->components->is_check_mode(@args);
}
sub has_blocker ( $self, $msg, %others ) {
my $caller_id;
if ( $others{'blocker_id'} ) {
$caller_id = $others{'blocker_id'};
}
else {
( undef, undef, undef, $caller_id ) = caller(1);
$caller_id ||= ref $self;
}
my $analytics_data;
if ( $others{info} ) {
my $info = delete $others{info};
if ( ref $info eq 'HASH' ) {
$analytics_data = Cpanel::JSON::canonical_dump( [$info] );
}
else {
my ( $latest_version, $self_version ) = ( $self->cpev->script->latest_version(), cpev::VERSION() );
if ( $self_version > $latest_version ) {
die "Invalid data analytics given to blocker. 'info' must be a hash reference.\n";
}
}
}
my $blocker = cpev::Blocker->new( id => $caller_id, msg => $msg, %others, info => $analytics_data );
$self->components->add_blocker($blocker);
die $blocker if $self->components->abort_on_first_blocker();
if ( !$others{'quiet'} ) {
WARN( <<~"EOS");
*** Elevation Blocker detected: ***
$msg
EOS
}
return $blocker;
}
sub check ($self) {
return;
}
sub pre_distro_upgrade ($self) {
return;
}
sub post_distro_upgrade ($self) {
return;
}
{
package cpev::Blocker;
use Simple::Accessor qw{ id msg info };
sub TO_JSON ($self) {
my %hash = $self->%*;
return \%hash;
}
}
1;
} # --- END lib/Elevate/Components/Base.pm
{ # --- BEGIN lib/Elevate/Components.pm
package Elevate::Components;
use cPstrict;
use Elevate::Components::Base ();
use Elevate::Components::AbsoluteSymlinks ();
use Elevate::Components::AutoSSL ();
use Elevate::Components::BootKernel ();
use Elevate::Components::CCS ();
use Elevate::Components::CloudLinux ();
use Elevate::Components::cPanelPlugins ();
use Elevate::Components::cPanelPrep ();
use Elevate::Components::DatabaseUpgrade ();
use Elevate::Components::DiskSpace ();
use Elevate::Components::Distros ();
use Elevate::Components::DNS ();
use Elevate::Components::EA4 ();
use Elevate::Components::ElevateScript ();
use Elevate::Components::ELS ();
use Elevate::Components::Grub2ControlTest ();
use Elevate::Components::Grub2ChecksWorkarounds ();
use Elevate::Components::Imunify ();
use Elevate::Components::InfluxDB ();
use Elevate::Components::IsContainer ();
use Elevate::Components::JetBackup ();
use Elevate::Components::KernelCare ();
use Elevate::Components::Kernel ();
use Elevate::Components::Leapp ();
use Elevate::Components::Lists ();
use Elevate::Components::LiteSpeed ();
use Elevate::Components::MountPoints ();
use Elevate::Components::MySQL ();
use Elevate::Components::NICs ();
use Elevate::Components::NixStats ();
use Elevate::Components::OVH ();
use Elevate::Components::PackageRestore ();
use Elevate::Components::PackageDupes ();
use Elevate::Components::Panopta ();
use Elevate::Components::PECL ();
use Elevate::Components::PerlXS ();
use Elevate::Components::PostgreSQL ();
use Elevate::Components::R1Soft ();
use Elevate::Components::Repositories ();
use Elevate::Components::RmMod ();
use Elevate::Components::RpmDB ();
use Elevate::Components::SSH ();
use Elevate::Components::Softaculous ();
use Elevate::Components::Ufw ();
use Elevate::Components::UnconvertedModules ();
use Elevate::Components::UpdateReleaseUpgrades ();
use Elevate::Components::UpdateSystem ();
use Elevate::Components::WHM ();
use Elevate::Components::WPToolkit ();
use Elevate::Components::Acronis ();
use Simple::Accessor qw(
cpev
check_mode
blockers
abort_on_first_blocker
);
# use Log::Log4perl qw(:easy);
INIT { Log::Log4perl->import(qw{:easy}); }
use Cpanel::JSON ();
our @CHECKS = qw{
IsContainer
ElevateScript
MountPoints
SSH
DiskSpace
WHM
Distros
CloudLinux
Imunify
DNS
MySQL
Repositories
Lists
JetBackup
KernelCare
NICs
EA4
BootKernel
Grub2ChecksWorkarounds
OVH
AbsoluteSymlinks
AutoSSL
};
our @NOOP_CHECKS = qw{
CCS
DatabaseUpgrade
ELS
Grub2ControlTest
InfluxDB
Kernel
LiteSpeed
NixStats
PECL
PackageRestore
PackageDupes
Panopta
PerlXS
PostgreSQL
R1Soft
RmMod
RpmDB
Softaculous
Ufw
UnconvertedModules
UpdateReleaseUpgrades
UpdateSystem
WPToolkit
cPanelPlugins
cPanelPrep
Acronis
};
push @CHECKS, @NOOP_CHECKS;
push @CHECKS, 'Leapp'; # This blocker has to run last!
use constant ELEVATE_BLOCKER_FILE => '/var/cpanel/elevate-blockers';
our $_CHECK_MODE; # for now global so we can use the helper (move it later to the object)
sub _build_blockers { return []; }
sub check ($self) { # do_check - main entry point
if ( $self->cpev->service->is_active ) {
WARN("An elevation process is already in progress.");
return 1;
}
my $stage = Elevate::Stages::get_stage();
if ( $stage != 0 && $stage <= cpev::VALID_STAGES() ) {
die <<~"EOS";
An elevation process is currently in progress: running stage $stage
You can check the log by running:
/scripts/elevate-cpanel --log
or check the elevation status:
/scripts/elevate-cpanel --status
EOS
}
Elevate::Components::Distros::bail_out_on_inappropriate_distro();
my $blocker_file = $self->cpev->getopt('check') || ELEVATE_BLOCKER_FILE;
my $has_blockers = $self->_has_blockers( $self->cpev->getopt('start') ? 0 : 1 );
$self->save( $blocker_file, { 'blockers' => $self->{'blockers'} } );
if ($has_blockers) {
WARN( <<~'EOS' );
Please fix the detected issues before performing the elevation process.
Read More: https://cpanel.github.io/elevate/blockers/
EOS
}
else {
my $cmd = q[/scripts/elevate-cpanel --start];
INFO( <<~"EOS" );
There are no known blockers to start the elevation process.
You can consider running:
$cmd
EOS
}
return $has_blockers;
}
sub _has_blockers ( $self, $check_mode = 0 ) {
unless ( $< == 0 ) {
ERROR("This script can only be run by root");
return 666;
}
$_CHECK_MODE = !!$check_mode; # running with --check
$self->abort_on_first_blocker(0);
my $ok = eval { $self->_check_all_blockers; 1; };
if ( !$ok ) {
my $error = $@;
if ( ref $error eq 'cpev::Blocker' ) {
ERROR( $error->{msg} );
return 401;
}
WARN("Unknown error while checking blockers: $error");
return 127; # unknown error
}
return scalar $self->blockers->@*;
}
sub num_blockers_found ($self) {
return scalar $self->blockers->@*;
}
sub add_blocker ( $self, $blocker ) {
push $self->blockers->@*, $blocker;
return;
}
sub is_check_mode ($) {
return $_CHECK_MODE;
}
sub save ( $self, $path, $stash ) {
open( my $fh, '>', $path ) or LOGDIE( "Failed to open " . $path . ": $!" );
print {$fh} Cpanel::JSON::pretty_canonical_dump($stash);
close $fh;
return 1;
}
sub _check_all_blockers ($self) { # sub _blockers_check ($self) {
foreach my $blocker (@CHECKS) { # preserve order
$self->_check_single_blocker($blocker);
}
return 0;
}
sub _check_single_blocker ( $self, $name ) {
my $blocker = $self->_get_blocker_for($name);
my $check = $blocker->can('check')
or die qq[Missing check function from ] . ref($blocker);
return $check->($blocker);
}
sub _get_blocker_for ( $self, $name ) { # useful for tests
my $pkg = "Elevate::Components::$name"; # need to be loaded
return $pkg->new( components => $self );
}
1;
} # --- END lib/Elevate/Components.pm
{ # --- BEGIN lib/Elevate/Components/AbsoluteSymlinks.pm
package Elevate::Components::AbsoluteSymlinks;
use cPstrict;
use Cpanel::Chdir ();
use Cpanel::UUID ();
use File::Copy ();
# use Log::Log4perl qw(:easy);
INIT { Log::Log4perl->import(qw{:easy}); }
# use Elevate::Components::Base();
our @ISA;
BEGIN { push @ISA, qw(Elevate::Components::Base); }
sub get_abs_symlinks ($self) {
my %links;
foreach my $entry ( glob "/*" ) {
my $path = readlink($entry); # don't bother with stat, this is fast
next unless $path && substr( $path, 0, 1 ) eq '/';
$links{$entry} = $path;
}
return %links;
}
sub pre_distro_upgrade ($self) {
$self->ssystem(qw{/usr/bin/ln -snf usr/local/cpanel/scripts /scripts});
$self->_absolute_symlinks;
return;
}
sub _absolute_symlinks ($self) {
my %links = $self->get_abs_symlinks();
return unless %links;
my $chdir = Cpanel::Chdir->new("/");
foreach my $link ( keys(%links) ) {
my $updated = substr( $links{$link}, 1 );
my $rand_uid = Cpanel::UUID::random_uuid();
my $tries = 0;
while ( -e "$link-$rand_uid" && $tries++ < 10000 ) {
$rand_uid = Cpanel::UUID::random_uuid();
}
symlink( $updated, "$link-$rand_uid" ) or die "Can't create symlink $link-$rand_uid to $updated: $!";
File::Copy::mv( "$link-$rand_uid", $link ) or die "Can't overwite $link: $!";
}
return;
}
sub check ($self) {
my %links = $self->get_abs_symlinks();
my $ln_string = join ", ", sort keys %links;
WARN( <<~"EOS" );
Symlinks with absolute paths were found in /:
$ln_string
This can cause problems during the upgrade, and
the script will correct them to relative symlinks before elevation.
EOS
return;
}
1;
} # --- END lib/Elevate/Components/AbsoluteSymlinks.pm
{ # --- BEGIN lib/Elevate/Components/AutoSSL.pm
package Elevate::Components::AutoSSL;
use cPstrict;
use Cpanel::SSL::Auto ();
# use Elevate::Components::Base();
our @ISA;
BEGIN { push @ISA, qw(Elevate::Components::Base); }
# use Log::Log4perl qw(:easy);
INIT { Log::Log4perl->import(qw{:easy}); }
sub check ($self) {
return $self->_check_autossl_provider();
}
sub _check_autossl_provider ($self) {
if ( $self->is_using_sectigo() ) {
WARN( <<~"EOS" );
Elevating with Sectigo as the provider for AutoSSL is not supported.
If you proceed with this upgrade, we will switch your system
to use the Let's Encrypt™ provider.
EOS
}
return 0;
}
sub pre_distro_upgrade ($self) {
if ( $self->is_using_sectigo() ) {
$self->ssystem_and_die(qw{/usr/local/cpanel/scripts/autorepair set_autossl_to_lets_encrypt});
}
return;
}
sub is_using_sectigo ($self) {
my @providers = Cpanel::SSL::Auto::get_all_provider_info();
foreach my $provider (@providers) {
next unless ( ref $provider eq 'HASH' && $provider->{enabled} );
if ( defined $provider->{display_name}
&& $provider->{display_name} =~ /sectigo/i ) {
return 1;
}
}
return 0;
}
1;
} # --- END lib/Elevate/Components/AutoSSL.pm
{ # --- BEGIN lib/Elevate/Components/BootKernel.pm
package Elevate::Components::BootKernel;
use cPstrict;
# use Elevate::Components::Base();
our @ISA;
BEGIN { push @ISA, qw(Elevate::Components::Base); }
use Elevate::Constants ();
use Cpanel::Kernel::Status ();
use Cpanel::Exception ();
use Cpanel::YAML ();
use Cpanel::JSON ();
use Try::Tiny;
sub check ($self) {
return 1 if $self->upgrade_distro_manually; # skip when --upgrade-distro-manually is provided
my $ok = 0;
try {
my ( $running_version, $boot_version ) = Cpanel::Kernel::Status::reboot_status()->@{ 'running_version', 'boot_version' };
$ok = $running_version eq $boot_version;
$self->has_blocker( <<~EOS ) if !$ok;
The running kernel version ($running_version) does not match that of
the default boot entry ($boot_version). This could be due to the kernel
being changed by an update, meaning that a reboot should resolve this.
However, this also could indicate that the system does not have control
over which kernel and early boot environment (initrd) is used upon
reboot, which is required to upgrade the operating system with this
script.
If this message remains after a reboot, your server may have been
configured to boot into a particular kernel directly rather than to an
instance of the GRUB2 boot loader. This often happens to virtualized
servers, but physical servers also can have this problem under certain
configurations. Your provider may have a solution to allow booting into
GRUB2; contact them for further information.
EOS
}
catch {
my $ex = $_;
$self->has_blocker(
"Unable to determine running and boot kernels due to the following error:\n" #
. _to_str($ex)
);
};
return $ok ? 1 : 0;
}
sub _to_str ($e) {
$e //= '';
my $str = Cpanel::Exception::get_string($e);
if ( length $str ) {
my $hash = eval { Cpanel::YAML::Load($str) } # parse yaml
// eval { Cpanel::JSON::Load($str) } # or json output... we cannot predict
// {};
if ( ref $hash eq 'HASH' && $hash->{msg} ) {
$str = $hash->{msg};
}
}
return $str;
}
1;
} # --- END lib/Elevate/Components/BootKernel.pm
{ # --- BEGIN lib/Elevate/Components/CCS.pm
package Elevate::Components::CCS;
use cPstrict;
use Try::Tiny;
use File::Path ();
use File::Copy ();
use Cpanel::Autodie ();
use Cpanel::Config::Users ();
use Cpanel::JSON ();
use Cpanel::Pkgr ();
use Elevate::Notify ();
use Elevate::PkgMgr ();
use Elevate::StageFile ();
# use Log::Log4perl qw(:easy);
INIT { Log::Log4perl->import(qw{:easy}); }
# use Elevate::Components::Base();
our @ISA;
BEGIN { push @ISA, qw(Elevate::Components::Base); }
use constant CCS_PACKAGE => 'cpanel-ccs-calendarserver';
use constant ZPUSH_PACKAGE => 'cpanel-z-push';
use constant EXPORT_DIR => '/var/cpanel/elevate_ccs_export';
use constant CCS_RESTART_SCRIPT => '/usr/local/cpanel/scripts/restartsrv_cpanel_ccs';
use constant TASK_QUEUE_SCRIPT => '/usr/local/cpanel/bin/servers_queue';
use constant DUMP_TYPES => (
calendars => 'ics',
contacts => 'vcard',
);
sub pre_distro_upgrade ($self) {
my $ccs_installed = Cpanel::Pkgr::is_installed(CCS_PACKAGE);
Elevate::StageFile::update_stage_file( { ccs_installed => $ccs_installed } );
return unless $ccs_installed;
$self->_load_ccs_modules();
$self->run_once('export_ccs_data');
$self->remove_ccs_and_dependencies();
$self->clean_up_pkg_cruft();
return;
}
sub clean_up_pkg_cruft ($self) {
$self->remove_cpanel_ccs_home_directory();
return;
}
sub remove_cpanel_ccs_home_directory ($self) {
File::Path::remove_tree('/opt/cpanel-ccs') if -d '/opt/cpanel-ccs';
return;
}
sub remove_ccs_and_dependencies ($self) {
my $zpush_installed = Cpanel::Pkgr::is_installed(ZPUSH_PACKAGE);
Elevate::StageFile::update_stage_file( { zpush_installed => $zpush_installed } );
my @ccs_dependencies;
push @ccs_dependencies, ZPUSH_PACKAGE();
Elevate::PkgMgr::remove( CCS_PACKAGE(), @ccs_dependencies );
return;
}
sub _load_ccs_modules ($self) {
require Cpanel::LoadModule::Custom;
Cpanel::LoadModule::Custom::load_perl_module('Cpanel::CCS::Delegates');
Cpanel::LoadModule::Custom::load_perl_module('Cpanel::CCS::DBUtils');
Cpanel::LoadModule::Custom::load_perl_module('Cpanel::CCS::Userdata');
return;
}
sub export_ccs_data ($self) {
my $export_dir = EXPORT_DIR();
INFO("Exporting CCS data to '$export_dir'. A backup of this data will be left in place after elevate completes.");
$self->_ensure_export_directory();
my @users = Cpanel::Config::Users::getcpusers();
foreach my $user (@users) {
INFO(" Exporting data for $user");
$self->_export_data_for_single_user($user);
}
INFO('Completed exporting CCS data for all users');
return;
}
sub _export_data_for_single_user ( $self, $user ) {
my $users_ccs_info = $self->_get_ccs_info_for_user($user);
my @webmail_users = keys %{ $users_ccs_info->{users} };
next if ( !@webmail_users );
$self->_make_backup_paths_for_user($user);
$self->_dump_persistence_data_for_user($user);
$self->_dump_delegation_data_for_user($user);
foreach my $webmail_user (@webmail_users) {
$self->_process_calendar_and_contacts_for_webmail_user( $user, $webmail_user );
}
return;
}
sub _process_calendar_and_contacts_for_webmail_user ( $self, $user, $webmail_user ) {
my $path = $self->_get_export_path_for_user($user);
my $users_ccs_info = $self->_get_ccs_info_for_user($user);
my $uuid = $users_ccs_info->{users}{$webmail_user};
my %dump_types = DUMP_TYPES();
my $dbh = $self->_get_dbh();
foreach my $type ( keys %dump_types ) {
my ( $query_string, $query_args ) = $self->_get_query_for_type( $type, $uuid );
my $sth = $dbh->prepare($query_string);
$sth->execute(@$query_args);
my $num_rows = $sth->rows;
next if !$num_rows;
my $dump_file = "$path/$type/${uuid}_${type}.$dump_types{$type}";
Cpanel::Autodie::open( my $dh, ">", $dump_file );
binmode( $dh, ":encoding(UTF-8)" ) or die "Can't set binmode to UTF-8 on $dump_file: $!";
while ( my $text = $sth->fetch ) {
for (@$text) {
my $txt = $_;
$txt =~ tr/'//d;
print $dh $txt;
}
}
}
return;
}
sub _dump_delegation_data_for_user ( $self, $user ) {
my $path = $self->_get_export_path_for_user($user);
my $dbh = $self->_get_dbh();
my @webmail_users_info = Cpanel::CCS::Userdata::get_users($user);
my $delegates_ar = Cpanel::CCS::Delegates::get( @webmail_users_info, $dbh );
my $delegate_file = $path . '/' . 'delegates.json';
Cpanel::JSON::DumpFile( $delegate_file, $delegates_ar );
return;
}
sub _dump_persistence_data_for_user ( $self, $user ) {
my $path = $self->_get_export_path_for_user($user);
my $persistence_file = $path . '/' . 'persistence.json';
my $users_ccs_info = $self->_get_ccs_info_for_user($user);
Cpanel::JSON::DumpFile( $persistence_file, $users_ccs_info );
return;
}
sub _make_backup_paths_for_user ( $self, $user ) {
my $path = $self->_get_export_path_for_user($user);
File::Path::make_path($path);
my %dump_types = DUMP_TYPES();
for ( keys(%dump_types) ) { File::Path::make_path("$path/$_"); }
return;
}
sub _get_query_for_type ( $self, $type, $uuid ) {
my %querydata = (
'calendars' => {
'args' => [ $uuid, '1', 'f' ],
'query' => "SELECT icalendar_text
FROM
calendar_object
INNER JOIN calendar_bind ON calendar_bind.calendar_resource_id = calendar_object.calendar_resource_id
INNER JOIN calendar_metadata ON calendar_metadata.resource_id = calendar_bind.calendar_resource_id
INNER JOIN calendar_home ON calendar_home.resource_id = calendar_bind.calendar_home_resource_id
WHERE calendar_home.owner_uid = ?
AND calendar_bind.bind_status = ?
AND calendar_metadata.is_in_trash = ?;",
},
'contacts' => {
'args' => [ $uuid, 'f' ],
'query' => "SELECT vcard_text
FROM
addressbook_object
INNER JOIN addressbook_home ON addressbook_home.resource_id = addressbook_object.addressbook_home_resource_id
WHERE addressbook_home.owner_uid = ?
AND addressbook_object.is_in_trash = ?;",
},
);
return ( $querydata{$type}{'query'}, $querydata{$type}{'args'} );
}
sub _get_dbh ($self) {
$self->{dbh} ||= Cpanel::CCS::DBUtils::get_dbh();
return $self->{dbh};
}
sub _get_export_path_for_user ( $self, $user ) {
$self->{$user}{export_path} ||= EXPORT_DIR() . '/' . $user . '/calendar_and_contacts';
return $self->{$user}{export_path};
}
sub _get_ccs_info_for_user ( $self, $user ) {
$self->{$user}{info} ||= Cpanel::CCS::Userdata::get_cpanel_account_users_uuids($user);
return $self->{$user}{info};
}
sub _ensure_export_directory ($self) {
File::Path::make_path(EXPORT_DIR);
chmod 0700, EXPORT_DIR;
return;
}
sub post_distro_upgrade ($self) {
return unless Elevate::StageFile::read_stage_file('ccs_installed');
$self->run_once('move_pgsql_directory');
$self->_install_ccs_and_dependencies();
$self->_clear_task_queue();