This repository has been archived by the owner on Apr 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcpanel-import.php
executable file
·454 lines (384 loc) · 13.2 KB
/
cpanel-import.php
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
#!/usr/bin/php
<?php
/*
* Cpanel account importer
*
* The purpose of this script is to import cpanel backups into a system that uses no cpanel
*
* usage: cpanel-import.php file [options]
*
* this script currently only supports:
* creation of users and assigning to a group
* creation of apache2 host file and symlinking it to active
* moving of all homedir files
* creationg of log directories
* creation of mysql dbs and users
*/
// look for some errors
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors',true);
set_time_limit(0);
date_default_timezone_set('America/Los_Angeles');
// set up the config
$config = new CpanelImport_Config;
$config->loadArgs($argv);
// import!
$import = new CpanelImport($config);
$import->import();
class CpanelImport {
public function __construct(CpanelImport_Config $config) {
$this->config = $config;
}
public function import() {
$this->files = new CpanelImport_Files($this);
$this->files->extract();
$this->group = new CpanelImport_Group($this);
$this->group->create();
$this->user = new CpanelImport_User($this);
$this->user->create();
$this->vhost = new CpanelImport_Vhost($this);
$this->vhost->write();
$this->mysql = new CpanelImport_Mysql($this);
$this->mysql->create();
$this->files->move();
$this->files->remove();
// restart apache
CpanelImport::exec('/etc/init.d/httpd restart');
}
public static function exec($cmd) {
if (defined('CPI_DEBUG')) {
echo "Running command:\n".$cmd."\n\n";
}
return exec($cmd);
}
public static function message($message) {
if (defined('CPI_VERBOSE')) {
echo $message."\n";
}
}
public function usage() {
echo "Cpanel account importer\n\n"
."Usage: cpanel-import.php file [options]\n"
."Ex: cpanel-import.php devin.tar.gz --ip=100.100.100.100 --forceuser\n\n"
." --username\t\tImport as a different username.\n"
." --password\t\tOverwrite the users password.\n"
." --ip\t\t\tThe IP to bind the vhost to.\n"
." --mysql-user\t\tThe root or super admin for mysql.\n"
." --mysql-pass\t\tThe root or super admin password for mysql.\n"
." --source\t\tWhere to find the file.\n"
." --dest\t\tWhere to find put the user.\n"
." --ignore\t\tComma separate list of files to ignore in homedir.\n"
." --httpd\t\tApache path.\n"
." --debug\t\tShow commands.\n"
." --verbose\t\tShow additional info.\n"
." --forceuser\t\tIf the user exists, keep going, and delete their home directory.\n\n";
}
}
class CpanelImport_Config {
// the source of where the backups are stored. backups need to be named like: USER.tar.gz
public $source = '/home/devin/backup/';
// the path to your apache files. usually httpd or apache2
public $http = '/etc/httpd/';
// where you want your users created
public $dest = '/home/';
// your mysql username that has grant and create db permissions. probably root
public $mysqlUser = 'root';
// your mysql password
public $mysqlPass = null;
// this template is created for apache in the sites-available path. edit this template if you want some suexec or something
public $hostTemplate = '
<VirtualHost _IP_>
ServerName _DOMAIN_
ServerAlias *._DOMAIN_ _DOMAINS_
DocumentRoot /home/_USER_/www
ServerAdmin webmaster@_DOMAIN_
UseCanonicalName Off
ErrorLog /home/_USER_/logs/error_log
CustomLog /home/_USER_/logs/access_log combined
</VirtualHost>
';
// the files to NOT copy over form the homedir
public $ignoreFiles = array(
'mail',
'public_ftp',
'cpbackup.*',
'backup.*',
'access-logs',
'tmp',
'logs',
'www',
'.lastlogin',
'.htpasswds',
'.cpanel',
'etc',
'.ftpquota',
'.autorespond',
'.filter',
'.spamkey',
'.lang',
'cpmove.psql',
'.cpaddons',
'account-locked.tpl',
'.bash_history',
'cpanel3-skel',
'.trash',
'.spamassassinenable',
'.spamassassin',
'.whmtheme'
);
//default values;
public $password;
public $username;
public $ip;
public $filename;
public $debug = false;
public $forceuser = false;
public $verbose = false;
public function loadArgs($args) {
$args = $this->parseArgs($args);
if (!$args) {
throw new CpanelImport_Exception;
}
$this->filename = array_shift($args);
foreach ($args as $key => $value) {
switch ($key) {
case 'password':
$this->password = $value;
break;
case 'httpd':
$this->httpd = $value;
break;
case 'source':
$this->source = $value;
break;
case 'dest':
$this->dest = $value;
break;
case 'username':
$this->username = $value;
break;
case 'mysql-pass':
$this->mysqlPass = $value;
break;
case 'mysql-user':
$this->mysqlUser = $value;
break;
case 'ignore':
$this->ignoreFiles = explode(',',$value);
break;
case 'ip':
$this->ip = $value;
break;
case 'debug':
define('CPI_DEBUG',true);
$this->debug = true;
break;
case 'verbose':
define('CPI_VERBOSE',true);
$this->verbose = true;
break;
case 'forceuser':
$this->forceuser = true;
break;
}
}
}
private function parseArgs($argv){
array_shift($argv); $o = array();
foreach ($argv as $a){
if (substr($a,0,2) == '--'){ $eq = strpos($a,'=');
if ($eq !== false){ $o[substr($a,2,$eq-2)] = substr($a,$eq+1); }
else { $k = substr($a,2); if (!isset($o[$k])){ $o[$k] = true; } } }
else if (substr($a,0,1) == '-'){
if (substr($a,2,1) == '='){ $o[substr($a,1,1)] = substr($a,3); }
else { foreach (str_split(substr($a,1)) as $k){ if (!isset($o[$k])){ $o[$k] = true; } } } }
else { $o[] = $a; } }
return $o;
}
}
class CpanelImport_Group {
public $groupname;
public function __construct($import) {
$this->groupname = $this->import->config->groupname ? $this->import->config->groupname : 'web';
}
public function create() {
CpanelImport::exec('groupadd '.$this->groupname);
}
}
class CpanelImport_User {
public $username;
public $password;
public $shadow;
public $cpanelUsername;
public function __construct($import) {
$this->import = $import;
$this->cpanelUsername = trim(CpanelImport::exec('ls '.$this->import->files->extracted.'cp'));
$this->password = $this->import->config->password;
$this->shadow = trim(file_get_contents($this->import->files->extracted.'shadow'));
$this->username = $this->import->config->username ? $this->import->config->username : $this->cpanelUsername;
if (!$this->import->config->forceuser && $this->check()) {
throw new CpanelImport_Exception('The user "'.$this->username.'" alerady exists!');
}
}
public function create() {
CpanelImport::exec('useradd '.$this->username.' -g'.$this->import->group->groupname);
if ($this->password) {
CpanelImport::exec('echo '.$argv[2].' | passwd '.$this->username.' --stdin');
} else {
CpanelImport::exec('awk -F: \'/'.$this->username.'/ { OFS=":"; $2="'.$this->shadow.'"; print}\' /etc/shadow');
}
}
public function check() {
return strpos(CpanelImport::exec('id '.$this->username),'No such user') ? false : true;
}
}
class CpanelImport_Files {
public $extracted;
private $basename;
public function __construct($import) {
$this->import = $import;
if (!file_exists($this->import->config->source.$this->import->config->filename)) {
throw new CpanelImport_Exception('The file "'.$this->import->config->filename.'" does not exist!');
}
$this->basename = 'restore-'.substr($this->import->config->filename,0,strpos($this->import->config->filename,'.')).'-'.date('YmdHis').'.tmp';
}
public function extract() {
// extract the files
mkdir($this->import->config->source.$this->basename);
CpanelImport::exec('tar xzf '.$this->import->config->source.$this->import->config->filename.' -C '.$this->import->config->source.$this->basename);
CpanelImport::exec('tar xf '.$this->import->config->source.$this->basename.'/*/homedir.tar -C '.$this->import->config->source.$this->basename.'/*/homedir');
$this->extracted = exec('cd '.$this->import->config->source.$this->basename.'/*/ && pwd').'/';
}
public function move() {
// create the user directory
if (!$this->import->user->username) {
throw new CpanelImport_Exception('There is no username!');
}
if ($this->import->config->forceuser) {
CpanelImport::exec('rm -Rf '.$this->import->config->dest.$this->import->user->username);
} else {
throw new CpanelImport_Exception('The directory "'.$this->import->config->dest.$this->import->user->username.'" alredy exists!');
}
CpanelImport::exec('mkdir '.$this->import->config->dest.$this->import->user->username);
CpanelImport::exec('mkdir '.$this->import->config->dest.$this->import->user->username.'/logs');
// move all web files
$iterator = new DirectoryIterator($this->extracted.'homedir');
foreach ($iterator as $fileinfo) {
if ($fileinfo->isDot()) {
continue;
}
$copy = true;
foreach ($this->import->config->ignoreFiles as $ignore) {
if (preg_match('/'.$ignore.'/',trim($fileinfo->getFilename()))) {
$copy = false;
}
}
if ($copy) {
CpanelImport::message('Copying: '.$this->extracted.'homedir/'.$fileinfo->getFilename());
CpanelImport::exec('mv '.$this->extracted.'homedir/'.$fileinfo->getFilename().' '.$this->import->config->dest.$this->import->user->username.'/');
}
}
// rename public_html to www because its shorter and i hate public_html
CpanelImport::exec('mv '.$this->import->config->dest.$this->import->user->username.'/public_html/ '.$this->import->config->dest.$this->import->user->username.'/www/');
// change permissions
CpanelImport::exec('chown -R '.$this->import->user->username.':'.$this->import->group->groupname.' '.$this->import->config->dest.$this->import->user->username);
}
public function remove() {
// remove extracted files
CpanelImport::message("Removing temp files ...");
CpanelImport::exec('rm -Rf '.$this->import->config->source.$this->basename);
}
}
class CpanelImport_Vhost {
public $domain;
public $domains;
public function __construct($import) {
$this->import = $import;
$this->read();
}
public function read() {
$domains = file($this->import->files->extracted.'cp/'.$this->import->user->username);
foreach ($domains as $line) {
if (preg_match('/^DNS([0-9]+)?=.*$/',$line)) {
$doms[] = trim(preg_replace('/^DNS([0-9]+)?=(.*)$/','\\2',$line));
}
}
$this->domain = $this->import->config->domain ? $this->import->config->domain : array_shift($doms);
$this->domains = implode($doms,' ');
}
public function write() {
CpanelImport::message("Writing vhosts ...");
$find = array(
'/_IP_/',
'/_USER_/',
'/_DOMAIN_/',
'/_DOMAINS_/'
);
$replace = array(
($this->import->config->ip ? $this->import->config->ip.':80' : '*'),
$this->import->user->username,
$this->domain,
$this->domains
);
$template = preg_replace($find, $replace, $this->import->config->hostTemplate);
file_put_contents($this->import->config->http.'sites-available/'.$this->import->user->username.'.conf', $template);
CpanelImport::exec('cd '.$this->import->config->http.'sites-enabled && ln -s ../sites-available/'.$this->import->user->username.'.conf');
}
}
class CpanelImport_Mysql {
private $auth;
public function __construct($import) {
$this->import = $import;
$this->auth = ' -u '.$this->import->config->mysqlUser.($this->import->config->mysqlPass ? ' -p'.$this->import->config->mysqlPass .' ' : '');
}
public function create() {
// create mysql databases and grant permissions
$iterator = new DirectoryIterator($this->import->files->extracted.'mysql/');
foreach ($iterator as $fileinfo) {
if ($fileinfo->isFile() && $fileinfo->getFilename() != 'horde.sql') {
$db = new stdClass;
$db->name = $fileinfo->getBasename('.sql');
$db->file = $fileinfo->getFilename();
$databases[] = $db;
}
}
//$this->sqlWrite("CREATE USER '".$this->import->user->username."'@'localhost' IDENTIFIED BY '".$argv[2]."';\n");
if ($databases) {
CpanelImport::message("Creating databases ...");
$sql = '';
foreach ($databases as $database) {
$sql .= "CREATE DATABASE `".$database->name."`;\n";
//$sql .= "GRANT ALL ON `".$database->name."`.* TO '".$this->import->user->username."'@'localhost';\n";
}
$this->sqlWrite($sql);
$sql = file_get_contents($this->import->files->extracted.'mysql.sql');
if ($this->import->user->cpanelUsername != $this->import->user->username) {
$sql = str_replace("'".$this->import->user->cpanelUsername.'_',"'".$this->import->user->username.'_');
$sql = str_replace('`'.$this->import->user->cpanelUsername.'\_','`'.$this->import->user->username.'\_');
}
$this->sqlWrite($sql);
foreach ($databases as $database) {
CpanelImport::exec('mysql'.$this->import->config->mysqlAuth.' "'.$database->name.'" < "'.$this->import->files->extracted.'mysql/'.$database->file.'"');
}
} else {
CpanelImport::message("No databases to create.");
}
}
private function sqlWrite($sql, $dbname = '') {
$tmpfile = tempnam('/tmp', 'temp-sql-import-');
file_put_contents($tmpfile,$sql);
if ($dbname) {
CpanelImport::exec('mysql'.$this->auth.' "'.$database->name.'" < "'.$tmpfile.'"');
} else {
CpanelImport::exec('mysql'.$this->auth.' < "'.$tmpfile.'"');
}
unlink($tmpfile);
}
}
class CpanelImport_Exception extends Exception {
public function __construct($e = null) {
if ($e) echo $e."\n";
CpanelImport::usage();
exit;
}
}