Marius Burkard
2016-04-20 4569cae57f127afd093794310ccd290d2d9fdf36
commit | author | age
bd68aa 1 <?php
MC 2 /*
3 Copyright (c) 2012, Till Brehm, projektfarm Gmbh, ISPConfig UG
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without modification,
7 are permitted provided that the following conditions are met:
8
9     * Redistributions of source code must retain the above copyright notice,
10       this list of conditions and the following disclaimer.
11     * Redistributions in binary form must reproduce the above copyright notice,
12       this list of conditions and the following disclaimer in the documentation
13       and/or other materials provided with the distribution.
14     * Neither the name of ISPConfig nor the names of its contributors
15       may be used to endorse or promote products derived from this software without
16       specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
25 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 set_time_limit(0);
31
b1a6a5 32 require_once '../../lib/config.inc.php';
MC 33 require_once '../../lib/app.inc.php';
bd68aa 34
MC 35 //* Check permissions for module
36 $app->auth->check_module_permissions('admin');
37
38 //* This is only allowed for administrators
39 if(!$app->auth->is_admin()) die('only allowed for administrators.');
40
41 $app->uses('tpl,auth');
42
43 $app->tpl->newTemplate('form.tpl.htm');
44 $app->tpl->setInclude('content_tpl', 'templates/import_vpopmail.htm');
45 $msg = '';
46 $error = '';
47
48 //* load language file
49 $lng_file = 'lib/lang/'.$_SESSION['s']['language'].'_import_vpopmail.lng';
b1a6a5 50 include $lng_file;
bd68aa 51 $app->tpl->setVar($wb);
MC 52
53 if(isset($_POST['db_hostname']) && $_POST['db_hostname'] != '') {
b1a6a5 54
bd68aa 55     //* Set external Login details
MC 56     $conf['imp_db_host'] = $_POST['db_hostname'];
57     $conf['imp_db_database'] = $_POST['db_name'];
58     $conf['imp_db_user'] = $_POST['db_user'];
59     $conf['imp_db_password'] = $_POST['db_password'];
b1a6a5 60     $conf['imp_db_charset'] = 'utf8';
MC 61     $conf['imp_db_new_link'] = false;
62     $conf['imp_db_client_flags'] = 0;
63
bd68aa 64     //* create new db object
MC 65     $exdb = new db('imp');
b1a6a5 66
bd68aa 67     if($exdb !== false) {
MC 68         $msg .= 'Databse connection succeeded<br />';
b1a6a5 69
bd68aa 70         $local_server_id = intval($_POST['local_server_id']);
cc7a82 71         $tmp = $app->db->queryOneRecord("SELECT mail_server FROM server WHERE server_id = ?", $local_server_id);
b1a6a5 72
bd68aa 73         if($tmp['mail_server'] == 1) {
MC 74             start_import();
75         } else {
76             $msg .= 'The server with the ID $local_server_id is not a mail server.<br />';
77         }
b1a6a5 78
bd68aa 79     } else {
MC 80         $msg .= 'Database connection failed<br />';
81     }
b1a6a5 82
bd68aa 83 } else {
MC 84     $_POST['local_server_id'] = 1;
85 }
86
b1a6a5 87 $app->tpl->setVar('db_hostname', $_POST['db_hostname']);
MC 88 $app->tpl->setVar('db_user', $_POST['db_user']);
89 $app->tpl->setVar('db_password', $_POST['db_password']);
90 $app->tpl->setVar('db_name', $_POST['db_name']);
91 $app->tpl->setVar('local_server_id', $_POST['local_server_id']);
92 $app->tpl->setVar('msg', $msg);
93 $app->tpl->setVar('error', $error);
bd68aa 94
MC 95 $app->tpl_defaults();
96 $app->tpl->pparse();
97
b1a6a5 98 //##########################################################
bd68aa 99
MC 100 function start_import() {
101     global $app, $conf, $msg, $error, $exdb, $local_server_id;
b1a6a5 102
bd68aa 103     //* Import the clients
b1a6a5 104     $records = $exdb->queryAllRecords("SELECT * FROM vpopmail WHERE pw_name = 'postmaster'");
bd68aa 105     if(is_array($records)) {
MC 106         foreach($records as $rec) {
107             $pw_domain = $rec['pw_domain'];
108             //* Check if we have a client with that username already
cc7a82 109             $tmp = $app->db->queryOneRecord("SELECT count(client_id) as number FROM client WHERE username = ?", $pw_domain);
bd68aa 110             if($tmp['number'] == 0) {
MC 111                 $pw_crypt_password = $app->auth->crypt_password($rec['pw_clear_passwd']);
112                 $country = 'FI';
b1a6a5 113
bd68aa 114                 //* add client
b1a6a5 115                 $sql = "INSERT INTO `client` (`sys_userid`, `sys_groupid`, `sys_perm_user`, `sys_perm_group`, `sys_perm_other`, `company_name`, `company_id`, `contact_name`, `customer_no`, `vat_id`, `street`, `zip`, `city`, `state`, `country`, `telephone`, `mobile`, `fax`, `email`, `internet`, `icq`, `notes`, `bank_account_owner`, `bank_account_number`, `bank_code`, `bank_name`, `bank_account_iban`, `bank_account_swift`, `default_mailserver`, `limit_maildomain`, `limit_mailbox`, `limit_mailalias`, `limit_mailaliasdomain`, `limit_mailforward`, `limit_mailcatchall`, `limit_mailrouting`, `limit_mailfilter`, `limit_fetchmail`, `limit_mailquota`, `limit_spamfilter_wblist`, `limit_spamfilter_user`, `limit_spamfilter_policy`, `default_webserver`, `limit_web_ip`, `limit_web_domain`, `limit_web_quota`, `web_php_options`, `limit_cgi`, `limit_ssi`, `limit_perl`, `limit_ruby`, `limit_python`, `force_suexec`, `limit_hterror`, `limit_wildcard`, `limit_ssl`, `limit_web_subdomain`, `limit_web_aliasdomain`, `limit_ftp_user`, `limit_shell_user`, `ssh_chroot`, `limit_webdav_user`, `limit_aps`, `default_dnsserver`, `limit_dns_zone`, `limit_dns_slave_zone`, `limit_dns_record`, `default_dbserver`, `limit_database`, `limit_cron`, `limit_cron_type`, `limit_cron_frequency`, `limit_traffic_quota`, `limit_client`, `limit_mailmailinglist`, `limit_openvz_vm`, `limit_openvz_vm_template_id`, `parent_client_id`, `username`, `password`, `language`, `usertheme`, `template_master`, `template_additional`, `created_at`, `id_rsa`, `ssh_rsa`)
cc7a82 116                 VALUES(1, 1, 'riud', 'riud', '', '', '', ?, '', '', '', '', '', '', ?, '', '', '', '', 'http://', '', '', '', '', '', '', '', '', 1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0, 0, 0, 1, NULL, -1, -1, 'no,fast-cgi,cgi,mod,suphp', 'n', 'n', 'n', 'n', 'n', 'y', 'n', 'n', 'n', -1, -1, -1, 0, 'no,jailkit', 0, 0, 1, -1, -1, -1, 1, -1, 0, 'url', 5, -1, 0, -1, 0, 0, 0, ?, ?, ?, 'default', 0, '', NOW(), '', '')";
MC 117                 $app->db->query($sql, $pw_domain,$country, $pw_domain, $pw_crypt_password, $conf['language']);
bd68aa 118                 $client_id = $app->db->insertID();
b1a6a5 119
bd68aa 120                 //* add sys_group
3a11d2 121                 $groupid = $app->db->datalogInsert('sys_group', array("name" => $pw_domain, "description" => '', "client_id" => $client_id), 'groupid');
bd68aa 122                 $groups = $groupid;
b1a6a5 123
2af58c 124                 $username = $pw_domain;
bd68aa 125                 $password = $pw_crypt_password;
MC 126                 $modules = $conf['interface_modules_enabled'];
127                 $startmodule = 'dashboard';
2af58c 128                 $usertheme = 'default';
bd68aa 129                 $type = 'user';
MC 130                 $active = 1;
2af58c 131                 $language = $conf["language"];
bd68aa 132                 //$password = $app->auth->crypt_password($password);
b1a6a5 133
bd68aa 134                 // Create the controlpaneluser for the client
MC 135                 //Generate ssh-rsa-keys
136                 exec('ssh-keygen -t rsa -C '.$username.'-rsa-key-'.time().' -f /tmp/id_rsa -N ""');
cc7a82 137                 $app->db->query("UPDATE client SET created_at = UNIX_TIMESTAMP(), id_rsa = ?, ssh_rsa = ? WHERE client_id = ?", @file_get_contents('/tmp/id_rsa'), @file_get_contents('/tmp/id_rsa.pub'), $client_id);
bd68aa 138                 exec('rm -f /tmp/id_rsa /tmp/id_rsa.pub');
b1a6a5 139
bd68aa 140                 // Create the controlpaneluser for the client
MC 141                 $sql = "INSERT INTO sys_user (username,passwort,modules,startmodule,app_theme,typ,active,language,groups,default_group,client_id)
cc7a82 142                 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
MC 143                 $app->db->query($sql, $username,$password,$modules,$startmodule,$usertheme,$type,$active,$language,$groups,$groupid,$client_id);
b1a6a5 144
bd68aa 145                 //* Set the default servers
MC 146                 $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE mail_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
147                 $default_mailserver = $app->functions->intval($tmp['server_id']);
148                 $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE web_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
149                 $default_webserver = $app->functions->intval($tmp['server_id']);
150                 $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE dns_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
151                 $default_dnsserver = $app->functions->intval($tmp['server_id']);
152                 $tmp = $app->db->queryOneRecord('SELECT server_id FROM server WHERE db_server = 1 AND mirror_server_id = 0 LIMIT 0,1');
153                 $default_dbserver = $app->functions->intval($tmp['server_id']);
b1a6a5 154
cc7a82 155                 $sql = "UPDATE client SET default_mailserver = ?, default_webserver = ?, default_dnsserver = ?, default_dbserver = ? WHERE client_id = ?";
MC 156                 $app->db->query($sql, $default_mailserver, $default_webserver, $default_dnsserver, $default_dbserver, $client_id);
b1a6a5 157
bd68aa 158                 $msg .= "Added Client $username.<br />";
MC 159             } else {
160                 $msg .= "Client $username exists, skipped.<br />";
161             }
162         }
163     }
b1a6a5 164
bd68aa 165     //* Import the mail domains
b1a6a5 166     $records = $exdb->queryAllRecords("SELECT DISTINCT pw_domain FROM `vpopmail`");
bd68aa 167     if(is_array($records)) {
MC 168         foreach($records as $rec) {
169             $domain = $rec['pw_domain'];
b1a6a5 170
bd68aa 171             //* Check if domain exists already
cc7a82 172             $tmp = $app->db->queryOneRecord("SELECT count(domain_id) as number FROM mail_domain WHERE domain = ?", $domain);
bd68aa 173             if($tmp['number'] == 0) {
cc7a82 174                 $user_rec = $app->db->queryOneRecord("SELECT * FROM sys_user WHERE username = ?", $domain);
bd68aa 175                 $sys_userid = ($user_rec['userid'] > 0)?$user_rec['userid']:1;
MC 176                 $sys_groupid = ($user_rec['default_group'] > 0)?$user_rec['default_group']:1;
b1a6a5 177
3a11d2 178                 $sql = array(
MC 179                     "sys_userid" => $sys_userid,
180                     "sys_groupid" => $sys_groupid,
181                     "sys_perm_user" => 'riud',
182                     "sys_perm_group" => 'riud',
183                     "sys_perm_other" => '',
184                     "server_id" => $local_server_id,
185                     "domain" => $domain,
186                     "active" => 'y'
187                 );
bd68aa 188                 $app->db->datalogInsert('mail_domain', $sql, 'domain_id');
MC 189                 $msg .= "Imported domain $domain <br />";
190             } else {
191                 $msg .= "Skipped domain $domain <br />";
192             }
193         }
194     }
b1a6a5 195
bd68aa 196     //* Import mailboxes
b1a6a5 197     $records = $exdb->queryAllRecords("SELECT * FROM `vpopmail`");
bd68aa 198     if(is_array($records)) {
MC 199         foreach($records as $rec) {
200             $domain = $rec['pw_domain'];
201             $email = $rec['pw_name'].'@'.$rec['pw_domain'];
b1a6a5 202
bd68aa 203             //* Check for duplicate mailboxes
cc7a82 204             $tmp = $app->db->queryOneRecord("SELECT count(mailuser_id) as number FROM mail_user WHERE email = ?", $email);
b1a6a5 205
bd68aa 206             if($tmp['number'] == 0) {
b1a6a5 207
bd68aa 208                 //* get the mail domain for the mailbox
cc7a82 209                 $domain_rec = $app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = ?", $domain);
b1a6a5 210
bd68aa 211                 if(is_array($domain_rec)) {
MC 212                     $pw_crypt_password = $app->auth->crypt_password($rec['pw_clear_passwd']);
213                     $maildir_path = "/var/vmail/".$rec['pw_domain']."/".$rec['pw_name'];
b1a6a5 214
bd68aa 215                     //* Insert the mailbox
3a11d2 216                     $sql = array(
MC 217                         "sys_userid" => $domain_rec['sys_userid'],
218                         "sys_groupid" => $domain_rec['sys_groupid'],
219                         "sys_perm_user" => 'riud',
220                         "sys_perm_group" => 'riud',
221                         "sys_perm_other" => '',
222                         "server_id" => $local_server_id,
223                         "email" => $email,
224                         "login" => $email,
225                         "password" => $pw_crypt_password,
226                         "name" => $email,
227                         "uid" => 5000,
228                         "gid" => 5000,
229                         "maildir" => $maildir_path,
230                         "quota" => 0,
231                         "cc" => '',
232                         "homedir" => '/var/vmail',
233                         "autoresponder" => 'n',
651642 234                         "autoresponder_start_date" => null,
MB 235                         "autoresponder_end_date" => null,
3a11d2 236                         "autoresponder_subject" => 'Out of office reply',
MC 237                         "autoresponder_text" => '',
238                         "move_junk" => 'n',
239                         "custom_mailfilter" => '',
240                         "postfix" => 'y',
241                         "access" => 'n',
242                         "disableimap" => 'n',
243                         "disablepop3" => 'n',
244                         "disabledeliver" => 'n',
245                         "disablesmtp" => 'n',
246                         "disablesieve" => 'n',
247                         "disablelda" => 'n',
248                         "disabledoveadm" => 'n'
249                     );
bd68aa 250                     $app->db->datalogInsert('mail_user', $sql, 'mailuser_id');
MC 251                     $msg .= "Imported mailbox $email <br />";
252                 }
253             }else {
254                 $msg .= "Skipped mailbox $email <br />";
255             }
256         }
257     }
b1a6a5 258
bd68aa 259     //* Import Aliases
b1a6a5 260     $records = $exdb->queryAllRecords("SELECT * FROM `valias`");
bd68aa 261     if(is_array($records)) {
MC 262         foreach($records as $rec) {
b1a6a5 263
bd68aa 264             $email = $rec['alias'].'@'.$rec['domain'];
MC 265             $target = '';
b1a6a5 266
MC 267             if(stristr($rec['valias_line'], '|')) {
bd68aa 268                 //* Skipped
MC 269                 $msg .= "Skipped $email as target is a script pipe.<br />";
b1a6a5 270             } elseif (substr(trim($rec['valias_line']), -9) == '/Maildir/') {
MC 271                 $parts = explode('/', $rec['valias_line']);
bd68aa 272                 $target_user = $parts[count($parts)-3];
MC 273                 $target_domain = $parts[count($parts)-4];
274                 $target = $target_user.'@'.$target_domain;
b1a6a5 275             } elseif (substr(trim($rec['valias_line']), 0, 1) == '&') {
MC 276                 $target = substr(trim($rec['valias_line']), 1);
277             } elseif (stristr($rec['valias_line'], '@')) {
bd68aa 278                 $target = $rec['valias_line'];
MC 279             } else {
280                 //* Unknown
281                 $msg .= "Skipped $email as format of target ".$rec['valias_line']." is unknown.<br />";
282             }
b1a6a5 283
bd68aa 284             //* Check for duplicate forwards
cc7a82 285             $tmp = $app->db->queryOneRecord("SELECT count(forwarding_id) as number FROM mail_forwarding WHERE source = ? AND destination = ?", $email, $target);
b1a6a5 286
bd68aa 287             if($tmp['number'] == 0 && $target != '') {
b1a6a5 288
bd68aa 289                 //* get the mail domain
cc7a82 290                 $domain_rec = $app->db->queryOneRecord("SELECT * FROM mail_domain WHERE domain = ?", $rec['domain']);
b1a6a5 291
bd68aa 292                 if(is_array($domain_rec)) {
3a11d2 293                     $sql = array(
MC 294                         "sys_userid" => $domain_rec['sys_userid'],
295                         "sys_groupid" => $domain_rec['sys_groupid'],
296                         "sys_perm_user" => 'riud',
297                         "sys_perm_group" => 'riud',
298                         "sys_perm_other" => '',
299                         "server_id" => $local_server_id,
300                         "source" => $email,
301                         "destination" => $target,
302                         "type" => 'forward',
303                         "active" => 'y' 
304                     );
bd68aa 305                     $app->db->datalogInsert('mail_forwarding', $sql, 'forwarding_id');
MC 306                 }
307                 $msg .= "Imported alias $email.<br />";
308             } else {
309                 $msg .= "Skipped alias $email as it exists already.<br />";
310             }
311         }
312     }
b1a6a5 313
bd68aa 314 }
MC 315
316
317 ?>