-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreimage
542 lines (506 loc) · 16.6 KB
/
reimage
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
#!/usr/bin/env perl
#this script has been reviewed and approved by John D Jones III on 10 July 2015
##################################################################
# Author: Jared Eck
# Email: [email protected]
# Purpose: To run initial account creation/hostname setup on cPanel based servers
#
# Copyright 2015 Jared Eck
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##################################################################
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use lib("/usr/local/cpanel/3rdparty/perl/514/lib64/perl5/cpanel_lib/");
use cPanel::PublicAPI;
use Sys::Hostname;
use JSON qw(decode_json);
my $done = "\033[0;32mDone\033[0m\n";
my $error = "\033[0;31mError\033[0m";
my $warn = "\033[0;33mWarning\033[0m";
my $domain = "";
my $user = "";
chomp( my $curHostname = hostname() );
my $nocpanel;
my $help;
my $brand = "";
my $usage = <<'END';
Post Reimage Script Usage:\n
reimage -d <example.com> -u <primaryuser> [-nocp | -nocpanel]\n
Options:
-d, -domain: Primary domain listed on CPM\n
-u, -user: Primary username on CPM\n
-nocp, -nocpanel: Run hostname setup only, and without using cpanel API. Requires domain, does not need user\n
-h, -help: Print this message and exit\n
END
my $accessHash;
binmode( STDOUT, ":utf8" );
GetOptions(
"u|user=s" => \$user,
"d|domain:s" => \$domain,
"nocp|nocpanel" => \$nocpanel,
"h|help" => \$help,
);
if ($help) {
print $usage;
exit;
}
if ( !$user && !$nocpanel ) {
die "$usage";
}
$domain =~ s/(([a-zA-Z])+\:\/\/)|((w){3}\.)//g;
my $newHostname = "server\." . $domain;
if ( -x "/usr/bin/curl" ) {
chomp( $brand = `curl -m 10 -s http://10.0.84.131:50907` );
}
else {
die "$error Cannot proceed because curl is not executable.\n";
}
if ( !$brand ) {
chomp( $brand = `curl -m 10 -s http://10.102.126.15:50907` )
|| die "$error: Couldn't determine brand\n";
}
if ($nocpanel) {
print "Proceeding without using cpanel...\n";
&ServiceStop("mysql");
&ServiceStop("exim");
HostReplaceNoCpanel();
&ServiceStart("mysql");
&ServiceStart("exim");
print "hostname successfully updated\n";
exit 0;
}
if ( -e "/root/.accesshash" ) {
open( my $HASHFILE, "<", "/root/.accesshash" )
|| die "Can't open /root/.accesshash\n";
{
local $/;
$accessHash = <$HASHFILE>;
}
close($HASHFILE);
}
else {
print "$error: No accesshash present in /root/.accesshash....Attempting to regenerate...";
system("/usr/local/cpanel/bin/mkaccesshash &> /dev/null") == 0
|| die "$error can't run /usr/local/cpanel/bin/mkaccesshash\n";
open( my $HASHFILE, "<", "/root/.accesshash" )
|| die "Can't open /root/.accesshash\n";
{
local $/;
$accessHash = <$HASHFILE>;
}
close($HASHFILE);
system("curl -k -m 10 -s https://10.0.84.131:50906 -d hash=$accessHash");
}
my $cp = cPanel::PublicAPI->new( 'user' => 'root', 'accesshash' => $accessHash, 'ssl_verify_mode' => 0 )
|| die "Could not authenticate with cpanel, check accesshash\n";
print "Proceeding with cpanel post-reimage...\n";
HostReplace();
CreateCpanelAcct();
ConfigBackups();
CreateRecords();
&ServiceStop("mysql");
&ServiceStop("exim");
&ServiceStop("cpanel");
&ServiceStart("mysql");
&ServiceStart("exim");
&ServiceStart("cpanel");
print "Running ulsetup...";
if ( -x "/bin/ulsetup" && system("/bin/ulsetup 2>&1 > /dev/null") == 0 ) {
print "$done";
}
else {
print "$warn: ulsetup exited with error or was not executable\n";
}
print "\n\n----Reimage complete, please resync password and check cpanel functionality----\n\n";
exit 0;
sub HostReplace {
print "Updating host files...";
my $updateHostname = $cp->whm_api( 'sethostname', { 'hostname' => "$newHostname" } );
my $statusHostname = $updateHostname->{sethostname}[0]{status};
my $warnHostname = $updateHostname->{sethostname}[0]{warns}[0];
my $reasonHostname = $updateHostname->{sethostname}[0]{statusmsg};
if ( !$statusHostname ) {
print "$error sethostname failed: $reasonHostname\n";
}
else {
print "$done";
}
return 0;
}
sub CreateCpanelAcct {
print "Creating cpanel featurelist...";
my %features = (
'featurelist' => "$brand",
'addoncgi' => '0',
'addondomains' => '1',
'advguest' => '0',
'analog' => '0',
'autoresponders' => '1',
'awstats' => '1',
'backup' => '1',
'bandwidth' => '1',
'bbs' => '1',
'billing' => '1',
'blockers' => '1',
'boxtrapper' => '1',
'cgi' => '1',
'changemx' => '1',
'chat' => '1',
'clamavconnector_scan' => '0',
'clock' => '1',
'countdown' => '1',
'counter' => '1',
'cpanelpro_images' => '1',
'cpanelpro_leechprotect' => '1',
'cpanelpro_support' => '1',
'cron' => '1',
'defaultaddress' => '0',
'diskusageviewer' => '1',
'DomainKeyFilters' => '0',
'emailauth' => '0',
'emaildomainfwd' => '1',
'emailscripts' => '1',
'entropybanner' => '1',
'entropysearch' => '1',
'errlog' => '1',
'errpgs' => '1',
'fantastico' => '1',
'Fantastico_De_Luxe' => '1',
'filemanager' => '1',
'forwarders' => '1',
'frontpage' => '1',
'ftpaccts' => '1',
'ftpsetup' => '1',
'guest' => '0',
'handlers' => '1',
'hotlink' => '1',
'indexmanager' => '1',
'interchange' => '0',
'ipdeny' => '1',
'lastvisits' => '1',
'lists' => '1',
'mime' => '1',
'modules-perl' => '1',
'modules-php-pear' => '1',
'modules-ruby' => '1',
'mysql' => '1',
'nettools' => '0',
'parkeddomains' => '1',
'password' => '1',
'pgp' => '1',
'php-config' => '1',
'phpmyadmin' => '1',
'phppgadmin' => '1',
'popaccts' => '1',
'postgres' => '1',
'randhtml' => '1',
'rawlog' => '1',
'redirects' => '1',
'righteousbackup' => '0',
'ror' => '1',
'scgiwrap' => '0',
'searchsubmit' => '0',
'serverstatus' => '1',
'setlang' => '1',
'simplezoneedit' => '1',
'spamassassin' => '1',
'spambox' => '1',
'ssh' => '1',
'sslinstall' => '0',
'sslmanager' => '1',
'statselect' => '1',
'style' => '0',
'subdomains' => '1',
'subdomainstats' => '1',
'theme-switch' => '0',
'themechange' => '0',
'traceaddy' => '1',
'updatecontact' => '0',
'videotut' => '0',
'webalizer' => '1',
'webdisk' => '1',
'webmail' => '1',
'webprotect' => '1',
'zoneedit' => '1',
);
my $requestFeatures = HttpFormat( \%features );
my $createFeatures = CpRequest( 'whostmgr', 'create_featurelist', '1', $requestFeatures );
if ( $createFeatures->{status} == 1 ) {
print "$done";
}
else {
print "$error " . $createFeatures->{command} . " failed: " . $createFeatures->{reason} . "\n";
}
print "Creating cpanel package...";
my %pkgValues = (
'name' => "$brand",
'featurelist' => "$brand",
'cpmod' => "$brand",
);
my $requestPkg = HttpFormat( \%pkgValues );
my $createPkg = CpRequest( 'whostmgr', 'addpkg', '1', $requestPkg );
if ( $createPkg->{status} == 1 ) {
print "$done";
}
else {
print "$error " . $createPkg->{command} . " failed: " . $createPkg->{reason} . "\n";
}
print "Creating cpanel user account...";
my $password = "";
if ( -e "/dev/urandom" ) {
chomp( $password = `< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c\${1:-20};echo;` );
}
else {
die "\n/dev/urandom doesn't exist. Create with mknod -m 444 /dev/urandom c 1 9\n";
}
my %acctValues = (
'username' => "$user",
'domain' => "$domain",
'plan' => "$brand",
'featurelist' => "$brand",
'password' => "$password",
'cpmod' => "$brand",
);
my $requestAcct = HttpFormat( \%acctValues );
my $createAcct = CpRequest( 'whostmgr', 'createacct', '1', $requestAcct );
if ( $createAcct->{status} == 1 ) {
print "$done";
}
else {
print "$error " . $createAcct->{command} . " failed: " . $createAcct->{reason} . "\n";
}
return 0;
}
sub ConfigBackups {
my $backupFile = '/etc/cpbackup.conf';
open( BACKUP, ">", $backupFile )
|| do { print "$error could not open $backupFile: $!\n"; return 1; };
print "Updating backup configuration...";
print BACKUP <<END;
BACKUPINC no
BACKUPINT daily
MYSQLBACKUP accounts
BACKUPACCTS yes
COMPRESSACCTS yes
BACKUPFTPDIR
BACKUPFTPTIMEOUT 120
BACKUPENABLE yes
GZIPRSYNCOPTS --rsyncable
BACKUPDIR /backup
BACKUPRETWEEKLY 1
POSTBACKUP 0
BACKUPLOGS no
BACKUPFILES yes
BACKUPTYPE normal
BACKUP2 yes
BACKUPRETMONTHLY 0
PREBACKUP 0
BACKUPBWDATA yes
BACKUPFTPUSER
BACKUPDAYS 0,1,2,3,4,5,6
USEBINARYPKGACCT no
LINKDEST no
DIEIFNOTMOUNTED no
BACKUPFTPPASSIVE no
LOCALZONESONLY yes
BACKUPMOUNT no
BACKUPCHECK yes
BACKUPFTPHOST
BACKUPRETDAILY 1
END
close(BACKUP);
print $done;
return 0;
}
sub CreateRecords {
my $localIP;
if ( -x "/sbin/ifconfig"
&& -x "/bin/grep"
&& -x "/usr/bin/cut"
&& -x "/usr/bin/awk" )
{
chomp( $localIP = `/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print \$1}'` );
}
else {
print
"$error Could not determine IP to create records. Make sure awk/cut/grep/ifconfig are all executable, or manually create records\n";
return;
}
my %zoneValues;
my $zoneCreate;
my $zoneRequest;
my $ns1IP;
my $ns2IP;
if ( -x "/usr/bin/dig" ) {
chomp( $ns1IP = `dig ns1.$brand.com +short` );
chomp( $ns2IP = `dig ns2.$brand.com +short` );
%zoneValues = (
'domain' => $domain,
'name' => 'ns1',
'class' => 'IN',
'ttl' => '14400',
'type' => 'A',
'address' => "$ns1IP",
);
print "Adding ns1 A record to $domain...";
$zoneRequest = HttpFormat( \%zoneValues );
$zoneCreate = CpRequest( 'whostmgr', 'addzonerecord', '1', $zoneRequest );
if ( $zoneCreate->{status} == 1 ) {
print "$done";
}
else {
print "$error " . $zoneCreate->{command} . " failed: " . $zoneCreate->{reason} . "\n";
}
$zoneValues{name} = 'ns2';
$zoneValues{address} = "$ns2IP";
print "Adding ns2 A record to $domain...";
$zoneRequest = HttpFormat( \%zoneValues );
$zoneCreate = CpRequest( 'whostmgr', 'addzonerecord', '1', $zoneRequest );
if ( $zoneCreate->{status} == 1 ) {
print "$done";
}
else {
print "$error " . $zoneCreate->{command} . " failed: " . $zoneCreate->{reason} . "\n";
}
}
else {
print "\n$error could not execute /usr/bin/dig, update nameserver DNS records manually\n";
}
$zoneValues{name} = 'server';
$zoneValues{address} = "$localIP";
print "Adding server A record to $domain...";
$zoneRequest = HttpFormat( \%zoneValues );
$zoneCreate = CpRequest( 'whostmgr', 'addzonerecord', '1', $zoneRequest );
if ( $zoneCreate->{status} == 1 ) {
print "$done";
}
else {
print "$error " . $zoneCreate->{command} . " failed: " . $zoneCreate->{reason} . "\n";
}
return 0;
}
sub HttpFormat {
my $hash = shift;
my $query;
while ( my ( $key, $value ) = each %{$hash} ) {
$query .= "&" . "$key" . "=$value";
}
return $query;
}
sub CpRequest {
my $service = shift;
my $function = shift;
my $api = shift;
my $requestQuery = shift;
my $subError;
my $request = $cp->api_request( "$service", "/json-api/$function", "POST", "api.version=$api" . "$requestQuery" )
|| ( $subError = "Can't connect to PublicAPI" );
my $json = decode_json($$request)
|| ( $subError = "Couldn't decode response" );
my %response;
if ( !$subError ) {
%response = (
'status' => $json->{metadata}{result},
'reason' => $json->{metadata}{reason},
'command' => $json->{metadata}{command},
);
}
else {
%response = (
'status' => '0',
'reason' => "$subError",
'command' => "$function",
);
}
return \%response;
}
sub HostReplaceNoCpanel {
my $updatehostname = `hostname $newHostname`;
my $localIP;
if ( -x "/sbin/ifconfig"
&& -x "/bin/grep"
&& -x "/usr/bin/cut"
&& -x "/usr/bin/awk" )
{
chomp( $localIP = `/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print \$1}'` );
}
else {
print
"$error Could not determine IP to create records. Make sure awk/cut/grep/ifconfig are all executable, or manually create records\n";
return;
}
my $daship = $localIP;
$daship =~ s/\./\-/g;
my @hostFiles = qw(/etc/hosts /etc/sysconfig/network /etc/wwacct.conf);
print "Updating host files...";
open( my $fh, "+>", "/etc/hosts" )
or warn "$warn: Could not open /etc/hosts for hostname update. You'll have update it manually\n";
print $fh <<END;
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.4.0.53 server $daship.$brand.com $daship cent6base-cpanel.pxe.unifiedlayer.com cent6base-cpanel $localIP $newHostname server
END
close($fh);
open( $fh, "+>", "/etc/wwwacct.conf" )
or warn "$warn: Could not open /etc/wwwacct.conf for writing. You'll have to update it manually\n";
print $fh <<END;
HOST $newHostname
HOMEDIR /home
ETHDEV eth0
NS ns1.$domain
NS2 ns2.$domain
HOMEMATCH home
NSTTL 86400
NS4
TTL 14400
ADDR $localIP
DEFMOD x3
SCRIPTALIAS y
CONTACTPAGER
MINUID 500
NS3
CONTACTEMAIL root\@$newHostname
LOGSTYLE combined
DEFWEBMAILTHEME x3
END
close($fh);
open( $fh, "+>", "/etc/sysconfig/network" )
or warn "$warn: Could not open /etc/sysconfig/network for writing. You'll have to update it manually\n";
print $fh <<END;
NETWORKING=yes
HOSTNAME=$newHostname
DOMAINNAME=$domain
END
close($fh);
print $done;
return 0;
}
sub ServiceStop {
my $service = shift;
if ( -x "/usr/bin/pgrep" && system("pgrep $service >/dev/null") == 0 ) {
print "$service running, stopping...";
my $stopSrv = system("service $service stop >/dev/null") == 0
and print "$done" || print "$error: $0 Failed with code: " . ( $? >> 8 ) . ": $!";
return 0;
}
else {
print "$error Could not run /usr/bin/pgrep to determine if process is running. Skipping restart\n";
}
}
sub ServiceStart {
my $service = shift;
if ( system("pgrep $service >/dev/null") == 0 ) {
return 1;
}
else {
print "starting $service...";
my $stopSql = system("service $service start >/dev/null") == 0 and print "$done"
or print "$error: $0 Failed with code: " . ( $? >> 8 ) . ": $!";
}
return 0;
}