diff --git a/etc/cron.jobs.in b/etc/cron.jobs.in index e35b6c9..b35b472 100644 --- a/etc/cron.jobs.in +++ b/etc/cron.jobs.in @@ -16,8 +16,9 @@ ### mandatory -*/5 * * * * LC_ALL=C mpstat | tail -1 | awk '{print $11}' > /var/piler/stat/cpu.stat -*/15 * * * * /usr/local/bin/indexer --quiet delta1 --rotate && sleep 2 && /usr/local/bin/indexer --quiet --merge main1 delta1 --merge-dst-range deleted 0 0 --rotate +*/5 * * * * LC_ALL=C mpstat | tail -1 | awk '{print $11}' > /var/piler/stat/cpu.stat +5,35 2-23 * * * /usr/local/bin/indexer --quiet delta1 --rotate && sleep 2 && /usr/local/bin/indexer --quiet --merge dailydelta1 delta1 --merge-dst-range deleted 0 0 --rotate +15 0 * * * /usr/local/bin/indexer --quiet --merge main1 dailydelta1 --merge-dst-range deleted 0 0 --rotate && sleep 2 && /usr/local/bin/indexer --quiet dailydelta1 --rotate */15 * * * * /usr/local/bin/indexer --quiet tag1 --rotate */15 * * * * /usr/local/bin/indexer --quiet note1 --rotate diff --git a/etc/sphinx.conf.in b/etc/sphinx.conf.in index 0b2a112..81c156c 100644 --- a/etc/sphinx.conf.in +++ b/etc/sphinx.conf.in @@ -53,6 +53,12 @@ sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1; } +source dailydelta : base +{ + sql_query_pre = SET NAMES utf8 + sql_query = SELECT id, `from`, `to`, `fromdomain`, `todomain`, `subject`, `arrived`, `sent`, `body`, `size`, `direction`, `folder`, `attachments`, `attachment_types` FROM sph_index WHERE id=-1; +} + source tag : base { sql_query_pre = SET NAMES utf8 @@ -120,6 +126,17 @@ min_word_len = 1 } +index dailydelta1 +{ + source = dailydelta + path = /var/piler/sphinx/dailydelta1 + docinfo = extern + charset_type = utf-8 + enable_star = 1 + min_prefix_len = 6 + min_word_len = 1 +} + index delta1 { source = delta diff --git a/webui/config.php b/webui/config.php index 0b23379..0757000 100644 --- a/webui/config.php +++ b/webui/config.php @@ -24,6 +24,8 @@ $config['SITE_NAME'] = 'piler.yourdomain.com'; $config['SITE_URL'] = 'http://piler.yourdomain.com/'; +$config['ENABLE_SAAS'] = 0; + $config['TIMEZONE'] = 'Europe/Budapest'; $config['PROVIDED_BY'] = 'www.mailpiler.org'; @@ -147,7 +149,7 @@ $config['SPHINX_DRIVER'] = 'sphinx'; $config['SPHINX_DATABASE'] = 'sphinx'; $config['SPHINX_HOSTNAME'] = '127.0.0.1:9306'; -$config['SPHINX_MAIN_INDEX'] = 'main1,main2,main3,main4'; +$config['SPHINX_MAIN_INDEX'] = 'dailydelta1,main1,main2,main3,main4'; $config['SPHINX_TAG_INDEX'] = 'tag1'; $config['SPHINX_NOTE_INDEX'] = 'note1'; @@ -255,6 +257,7 @@ define('TABLE_ARCHIVING_RULE', 'archiving_rule'); define('TABLE_RETENTION_RULE', 'retention_rule'); define('TABLE_OPTION', 'option'); +define('TABLE_LDAP', 'ldap'); define('TABLE_GOOGLE', 'google'); define('TABLE_GOOGLE_IMAP', 'google_imap'); define('VIEW_MESSAGES', 'v_messages'); diff --git a/webui/controller/domain/domain.php b/webui/controller/domain/domain.php index 9080b05..1e3460b 100644 --- a/webui/controller/domain/domain.php +++ b/webui/controller/domain/domain.php @@ -14,14 +14,21 @@ $request = Registry::get('request'); $db = Registry::get('db'); + $ldap_id = 0; + + $this->load->model('domain/domain'); + if(ENABLE_SAAS == 1) { + $this->load->model('saas/ldap'); + $this->data['ldap'] = $this->model_saas_ldap->get(); + $ldap_id = $this->request->post['ldap_id']; + } $this->document->title = $this->data['text_domain']; $this->data['username'] = Registry::get('username'); - $this->data['page'] = 0; $this->data['page_len'] = get_page_length(); @@ -54,7 +61,7 @@ if($this->request->server['REQUEST_METHOD'] == 'POST') { if($this->validate() == true) { - if($this->model_domain_domain->addDomain($this->request->post['domain'], $this->request->post['mapped']) == 1) { + if($this->model_domain_domain->addDomain($this->request->post['domain'], $this->request->post['mapped'], $ldap_id) == 1) { $this->data['x'] = $this->data['text_successfully_added']; } else { $this->template = "common/error.tpl"; diff --git a/webui/controller/ldap/domains.php b/webui/controller/ldap/domains.php new file mode 100644 index 0000000..7ba1ed0 --- /dev/null +++ b/webui/controller/ldap/domains.php @@ -0,0 +1,82 @@ +id = "content"; + $this->template = "domain/domains.tpl"; + $this->layout = "common/layout-email"; + + + $request = Registry::get('request'); + $db = Registry::get('db'); + $language = Registry::get('language'); + + $this->load->model('domain/domain'); + + //$this->document->title = $language->get('text_group_management'); + + $this->data['page'] = 0; + $this->data['page_len'] = get_page_length(); + + $this->data['total'] = 0; + + $this->data['sort'] = 'domain'; + + $this->data['term'] = ''; + + if(!isset($this->request->get['term']) || strlen($this->request->get['term']) < 1) { die("no data"); } + + if(isset($this->request->get['page']) && is_numeric($this->request->get['page']) && $this->request->get['page'] > 0) { + $this->data['page'] = $this->request->get['page']; + } + + $this->data['search'] = $this->request->get['term']; + + /* check if we are admin */ + + if(Registry::get('admin_user') == 1) { + + // for autocomplete + + if(strlen($this->request->get['term']) >= 2) { + $domains = $this->model_domain_domain->get_domains_by_string($this->request->get['term']); + + $i = 0; + $s = '[ '; + + foreach($domains as $domain) { + $i++; + $s .= '{ "id": "' . $i . '", "value": "' . $domain['domain'] . '" },'; + } + + $s = preg_replace("/,$/", "", $s) . " ]"; + + print $s; + } + + // for domain list + + if(strlen($this->request->get['term']) == 1) { + $this->data['domains'] = $this->model_domain_domain->get_domains_by_string($this->request->get['term'], $this->data['page'], $this->data['page_len']); + + $this->data['total'] = $this->model_domain_domain->count_domains($this->request->get['term']); + + $this->data['prev_page'] = $this->data['page'] - 1; + $this->data['next_page'] = $this->data['page'] + 1; + + $this->data['total_pages'] = floor($this->data['total'] / $this->data['page_len']); + + $this->render(); + } + + } + } + + +} + +?> diff --git a/webui/controller/ldap/list.php b/webui/controller/ldap/list.php new file mode 100644 index 0000000..f7eb282 --- /dev/null +++ b/webui/controller/ldap/list.php @@ -0,0 +1,100 @@ +id = "content"; + $this->template = "ldap/list.tpl"; + $this->layout = "common/layout"; + + + $request = Registry::get('request'); + $db = Registry::get('db'); + + $this->load->model('saas/ldap'); + + $this->document->title = $this->data['text_ldap']; + + + $this->data['username'] = Registry::get('username'); + + + $this->data['page'] = 0; + $this->data['page_len'] = get_page_length(); + + $this->data['total'] = 0; + + $this->data['entries'] = array(); + + + /* check if we are admin */ + + if(Registry::get('admin_user') == 1) { + + if($this->request->server['REQUEST_METHOD'] == 'POST') { + if($this->validate() == true) { + + if($this->model_saas_ldap->add($this->request->post) == 1) { + $this->data['x'] = $this->data['text_successfully_added']; + } else { + $this->template = "common/error.tpl"; + $this->data['errorstring'] = $this->data['text_failed_to_add']; + } + } + else { + $this->template = "common/error.tpl"; + $this->data['errorstring'] = array_pop($this->error); + } + } + + $this->data['entries'] = $this->model_saas_ldap->get(); + + } + else { + $this->template = "common/error.tpl"; + $this->data['errorstring'] = $this->data['text_you_are_not_admin']; + } + + + $this->render(); + } + + + private function validate() { + + if(!isset($this->request->post['description']) || strlen($this->request->post['description']) < 1) { + $this->error['description'] = $this->data['text_invalid_data']; + } + + if(!isset($this->request->post['ldap_host']) || strlen($this->request->post['ldap_host']) < 1) { + $this->error['ldap_host'] = $this->data['text_invalid_data']; + } + + if(!isset($this->request->post['ldap_base_dn']) || strlen($this->request->post['ldap_base_dn']) < 1) { + $this->error['ldap_base_dn'] = $this->data['text_invalid_data']; + } + + if(!isset($this->request->post['ldap_bind_dn']) || strlen($this->request->post['ldap_bind_dn']) < 1) { + $this->error['ldap_bind_dn'] = $this->data['text_invalid_data']; + } + + if(!isset($this->request->post['ldap_bind_pw']) || strlen($this->request->post['ldap_bind_pw']) < 1) { + $this->error['ldap_bind_pw'] = $this->data['text_invalid_data']; + } + + if (!$this->error) { + return true; + } else { + return false; + } + + } + + + +} + +?> diff --git a/webui/controller/ldap/remove.php b/webui/controller/ldap/remove.php new file mode 100644 index 0000000..4f942a4 --- /dev/null +++ b/webui/controller/ldap/remove.php @@ -0,0 +1,79 @@ +id = "content"; + $this->template = "ldap/remove.tpl"; + $this->layout = "common/layout"; + + + $request = Registry::get('request'); + $db = Registry::get('db'); + + $this->load->model('saas/ldap'); + + $this->document->title = $this->data['text_ldap']; + + + $this->data['username'] = Registry::get('username'); + + $this->data['id'] = $this->request->get['id']; + $this->data['description'] = $this->request->get['description']; + $this->data['confirmed'] = (int)$this->request->get['confirmed']; + + + if($this->validate() == true) { + + if($this->data['confirmed'] == 1) { + $ret = $this->model_saas_ldap->delete($this->data['id'], $this->data['description']); + if($ret == 1){ + $this->data['x'] = $this->data['text_successfully_removed']; + } + else { + $this->data['x'] = $this->data['text_failed_to_remove']; + } + } + } + else { + $this->template = "common/error.tpl"; + $this->data['errorstring'] = array_pop($this->error); + } + + + + $this->render(); + } + + + private function validate() { + + if(Registry::get('admin_user') == 0) { + $this->error['admin'] = $this->data['text_you_are_not_admin']; + } + + if(!isset($this->request->get['description']) || strlen($this->request->get['description']) < 1) { + $this->error['description'] = $this->data['text_invalid_data']; + } + + if(!isset($this->request->get['id']) || !is_numeric($this->request->get['id'])) { + $this->error['id'] = $this->data['text_invalid_data']; + } + + if (!$this->error) { + return true; + } else { + return false; + } + + } + + +} + +?> diff --git a/webui/controller/ldap/test.php b/webui/controller/ldap/test.php new file mode 100644 index 0000000..bf40079 --- /dev/null +++ b/webui/controller/ldap/test.php @@ -0,0 +1,37 @@ +id = "content"; + $this->template = "ldap/list.tpl"; + $this->layout = "common/layout"; + + + $request = Registry::get('request'); + $db = Registry::get('db'); + $lang = Registry::get('language'); + + $ldap = new LDAP($this->request->post['ldap_host'], $this->request->post['ldap_bind_dn'], $this->request->post['ldap_bind_pw']); + + if($ldap->is_bind_ok()) { + print "" . $lang->data['text_connection_ok'] . " "; + + $query = $ldap->query($this->request->post['ldap_base_dn'], "(mail=*)", array()); + if($query->num_rows < 1) { + print "" . $lang->data['text_not_found_any_email_address'] . " "; + } + } + else { + print "" . $lang->data['text_connection_failed'] . " "; + } + + } + + +} + +?> diff --git a/webui/controller/login/login.php b/webui/controller/login/login.php index bd16dad..0757c5b 100644 --- a/webui/controller/login/login.php +++ b/webui/controller/login/login.php @@ -26,6 +26,10 @@ $this->load->model('user/prefs'); $this->load->model('folder/folder'); + if(ENABLE_SAAS == 1) { + $this->load->model('saas/ldap'); + } + $this->document->title = $this->data['text_login']; if($this->request->server['REQUEST_METHOD'] == 'POST' && $this->validate() == true) { diff --git a/webui/index.php b/webui/index.php index 1d2a0c5..74cfffa 100644 --- a/webui/index.php +++ b/webui/index.php @@ -1,13 +1,5 @@ db->query("SELECT domain, mapped FROM " . TABLE_DOMAIN . " ORDER BY domain ASC"); + $query = $this->db->query("SELECT domain, mapped, ldap_id FROM " . TABLE_DOMAIN . " ORDER BY domain ASC"); - if(isset($query->rows)) { return $query->rows; } + if(isset($query->rows)) { + foreach($query->rows as $q) { - return array(); + $ldap = ''; + + if($q['ldap_id'] > 0) { + $query2 = $this->db->query("SELECT description FROM " . TABLE_LDAP . " WHERE id=?", array($q['ldap_id'])); + if(isset($query2->row)) { $ldap = $query2->row['description']; } + } + + $data[] = array('domain' => $q['domain'], 'mapped' => $q['mapped'], 'ldap' => $ldap); + + } + } + + return $data; } @@ -38,14 +52,14 @@ } - public function addDomain($domain = '', $mapped = '') { + public function addDomain($domain = '', $mapped = '', $ldap_id = 0) { if($domain == "" || $mapped == "") { return 0; } $domains = explode("\n", $domain); foreach ($domains as $domain) { $domain = rtrim($domain); - $query = $this->db->query("INSERT INTO " . TABLE_DOMAIN . " (domain, mapped) VALUES (?,?)", array($domain, $mapped)); + $query = $this->db->query("INSERT INTO " . TABLE_DOMAIN . " (domain, mapped, ldap_id) VALUES (?,?,?)", array($domain, $mapped, $ldap_id)); $rc = $this->db->countAffected(); diff --git a/webui/model/saas/ldap.php b/webui/model/saas/ldap.php new file mode 100644 index 0000000..751624d --- /dev/null +++ b/webui/model/saas/ldap.php @@ -0,0 +1,60 @@ +db->query("SELECT id, description, ldap_host, ldap_base_dn, ldap_bind_dn FROM " . TABLE_LDAP . " ORDER BY description ASC"); + + if($query->num_rows > 0) { return $query->rows; } + + return array(); + } + + + public function delete($id = 0, $description = '') { + if($id == 0) { return 0; } + + $query = $this->db->query("DELETE FROM " . TABLE_LDAP . " WHERE id=?", array($id)); + + $rc = $this->db->countAffected(); + + LOGGER("remove ldap entry: #$id, $description (rc=$rc)"); + + return $rc; + } + + + public function add($arr = array()) { + if(!isset($arr['description']) || !isset($arr['ldap_host'])) { return 0; } + + $query = $this->db->query("INSERT INTO " . TABLE_LDAP . " (description, ldap_host, ldap_base_dn, ldap_bind_dn, ldap_bind_pw) VALUES (?,?,?,?,?)", array($arr['description'], $arr['ldap_host'], $arr['ldap_base_dn'], $arr['ldap_bind_dn'], $arr['ldap_bind_pw'])); + + $rc = $this->db->countAffected(); + + LOGGER("add ldap entry: " . $arr['description'] . " / " . $arr['ldap_host'] . " / " . $arr['ldap_base_dn'] . " (rc=$rc)"); + + if($rc == 1){ return 1; } + + return 0; + } + + + public function get_ldap_params_by_email($email = '') { + $domain = ''; + + if($email == '') { return array(); } + + list($l,$d) = explode("@", $email); + + $query = $this->db->query("SELECT ldap_host, ldap_base_dn, ldap_bind_dn, ldap_bind_pw from " . TABLE_DOMAIN . " as d, " . TABLE_LDAP . " as l where d.ldap_id=l.id and d.domain=?", array($d)); + + if($query->num_rows > 0) { return array($query->row['ldap_host'], $query->row['ldap_base_dn'], $query->row['ldap_bind_dn'], $query->row['ldap_bind_pw']); } + + return array(); + } + +} + +?> diff --git a/webui/model/user/auth.php b/webui/model/user/auth.php index 4f7b961..f9ffa3e 100644 --- a/webui/model/user/auth.php +++ b/webui/model/user/auth.php @@ -63,22 +63,36 @@ private function checkLoginAgainstLDAP($username = '', $password = '') { - $ldap = new LDAP(LDAP_HOST, LDAP_HELPER_DN, LDAP_HELPER_PASSWORD); + $ldap_host = LDAP_HOST; + $ldap_base_dn = LDAP_BASE_DN; + $ldap_helper_dn = LDAP_HELPER_DN; + $ldap_helper_password = LDAP_HELPER_PASSWORD; + + if(ENABLE_SAAS == 1) { + $a = $this->model_saas_ldap->get_ldap_params_by_email($username); + + $ldap_host = $a[0]; + $ldap_base_dn = $a[1]; + $ldap_helper_dn = $a[2]; + $ldap_helper_password = $a[3]; + } + + $ldap = new LDAP($ldap_host, $ldap_helper_dn, $ldap_helper_password); if($ldap->is_bind_ok()) { - $query = $ldap->query(LDAP_BASE_DN, "(&(objectClass=" . LDAP_ACCOUNT_OBJECTCLASS . ")(" . LDAP_MAIL_ATTR . "=$username))", array()); + $query = $ldap->query($ldap_base_dn, "(&(objectClass=" . LDAP_ACCOUNT_OBJECTCLASS . ")(" . LDAP_MAIL_ATTR . "=$username))", array()); if(isset($query->row['dn']) && $query->row['dn']) { $a = $query->row; - $ldap_auth = new LDAP(LDAP_HOST, $a['dn'], $password); + $ldap_auth = new LDAP($ldap_host, $a['dn'], $password); - if(ENABLE_SYSLOG == 1) { syslog(LOG_INFO, "ldap auth against '" . LDAP_HOST . "', dn: '" . $a['dn'] . "', result: " . $ldap_auth->is_bind_ok()); } + if(ENABLE_SYSLOG == 1) { syslog(LOG_INFO, "ldap auth against '" . $ldap_host . "', dn: '" . $a['dn'] . "', result: " . $ldap_auth->is_bind_ok()); } if($ldap_auth->is_bind_ok()) { - $query = $ldap->query(LDAP_BASE_DN, "(|(&(objectClass=" . LDAP_ACCOUNT_OBJECTCLASS . ")(" . LDAP_MAIL_ATTR . "=$username))(&(objectClass=" . LDAP_DISTRIBUTIONLIST_OBJECTCLASS . ")(" . LDAP_DISTRIBUTIONLIST_ATTR . "=$username)" . ")(&(objectClass=" . LDAP_DISTRIBUTIONLIST_OBJECTCLASS . ")(" . LDAP_DISTRIBUTIONLIST_ATTR . "=" . stripslashes($a['dn']) . ")))", array()); + $query = $ldap->query($ldap_base_dn, "(|(&(objectClass=" . LDAP_ACCOUNT_OBJECTCLASS . ")(" . LDAP_MAIL_ATTR . "=$username))(&(objectClass=" . LDAP_DISTRIBUTIONLIST_OBJECTCLASS . ")(" . LDAP_DISTRIBUTIONLIST_ATTR . "=$username)" . ")(&(objectClass=" . LDAP_DISTRIBUTIONLIST_OBJECTCLASS . ")(" . LDAP_DISTRIBUTIONLIST_ATTR . "=" . stripslashes($a['dn']) . ")))", array()); $is_auditor = $this->check_ldap_membership($query->rows); @@ -96,7 +110,7 @@ } } else if(ENABLE_SYSLOG == 1) { - syslog(LOG_INFO, "cannot bind to '" . LDAP_HOST . "' as '" . LDAP_HELPER_DN . "'"); + syslog(LOG_INFO, "cannot bind to '" . $ldap_host . "' as '" . $ldap_helper_dn . "'"); } return 0; diff --git a/webui/view/javascript/piler-in.js b/webui/view/javascript/piler-in.js index 80ce6af..88a8201 100644 --- a/webui/view/javascript/piler-in.js +++ b/webui/view/javascript/piler-in.js @@ -876,9 +876,37 @@ $('#fldr_' + id).hide(); $('#fldr_collapse_' + id).hide(); $('#fldr_open_' + id).show(); + }, + + + test_ldap_connection:function() + { + Piler.log("[test_ldap_connection]"); + + jQuery.ajax('index.php?route=ldap/test', { + data: { + description: $('#description').val(), + ldap_host: $('#ldap_host').val(), + ldap_base_dn: $('#ldap_base_dn').val(), + ldap_bind_dn: $('#ldap_bind_dn').val(), + ldap_bind_pw: $('#ldap_bind_pw').val() + }, + type: "POST" + }) + .done( function(a) { + $('#LDAPTEST').html(a); + }) + .fail(function(a, b) { alert("Problem retrieving XML data:" + b) }); + }, + + + clear_ldap_test: function() + { + $('#LDAPTEST').html(''); } + } diff --git a/webui/view/theme/default/templates/common/menu-admin.tpl b/webui/view/theme/default/templates/common/menu-admin.tpl deleted file mode 100644 index d1d557e..0000000 --- a/webui/view/theme/default/templates/common/menu-admin.tpl +++ /dev/null @@ -1,55 +0,0 @@ - - -
- diff --git a/webui/view/theme/default/templates/common/menu.tpl b/webui/view/theme/default/templates/common/menu.tpl index fb8c936..ffb3a89 100644 --- a/webui/view/theme/default/templates/common/menu.tpl +++ b/webui/view/theme/default/templates/common/menu.tpl @@ -35,6 +35,9 @@