Marius Cramer
2015-08-06 37b29231e47a0c4458dc1c15d98588f16f07e1e2
commit | author | age
8ec1d8 1 <?php
M 2
3 /*
4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
5 Copyright (c) 2012, Marius Cramer, pixcept KG
6 All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without modification,
9 are permitted provided that the following conditions are met:
10
11     * Redistributions of source code must retain the above copyright notice,
12       this list of conditions and the following disclaimer.
13     * Redistributions in binary form must reproduce the above copyright notice,
14       this list of conditions and the following disclaimer in the documentation
15       and/or other materials provided with the distribution.
16     * Neither the name of ISPConfig nor the names of its contributors
17       may be used to endorse or promote products derived from this software without
18       specific prior written permission.
19
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
27 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 class validate_domain {
7fe908 33
MC 34     function get_error($errmsg) {
35         global $app;
36
37         if(isset($app->tform->wordbook[$errmsg])) {
38             return $app->tform->wordbook[$errmsg]."<br>\r\n";
39         } else {
40             return $errmsg."<br>\r\n";
41         }
42     }
43
44     /* Validator function for domain (website) */
45     function web_domain($field_name, $field_value, $validator) {
46         if(empty($field_value)) return $this->get_error('domain_error_empty');
47
48         // do not allow wildcards on website domains
49         $result = $this->_regex_validate($field_value);
50         if(!$result) return $this->get_error('domain_error_regex');
51
52         $result = $this->_check_unique($field_value);
53         if(!$result) return $this->get_error('domain_error_unique');
54     }
55
56     /* Validator function for sub domain */
57     function sub_domain($field_name, $field_value, $validator) {
58         if(empty($field_value)) return $this->get_error('domain_error_empty');
59
60         $allow_wildcard = $this->_wildcard_limit();
61         if($allow_wildcard == false && substr($field_value, 0, 2) === '*.') return $this->get_error('domain_error_wildcard');
62
63         $result = $this->_regex_validate($field_value, $allow_wildcard);
64         if(!$result) return $this->get_error('domain_error_regex');
65
66         $result = $this->_check_unique($field_value);
67         if(!$result) return $this->get_error('domain_error_unique');
68     }
69
70     /* Validator function for alias domain */
71     function alias_domain($field_name, $field_value, $validator) {
72         if(empty($field_value)) return $this->get_error('domain_error_empty');
73
74         // do not allow wildcards on alias domains
75         $result = $this->_regex_validate($field_value);
76         if(!$result) return $this->get_error('domain_error_regex');
77
78         $result = $this->_check_unique($field_value);
79         if(!$result) return $this->get_error('domain_error_unique');
80     }
81
82     /* Validator function for checking the auto subdomain of a web/aliasdomain */
83     function web_domain_autosub($field_name, $field_value, $validator) {
84         global $app;
85         if(empty($field_value) || $field_name != 'subdomain') return; // none set
86
87         $check_domain = $_POST['domain'];
88         $app->uses('ini_parser,getconf');
89         $settings = $app->getconf->get_global_config('domains');
90         if ($settings['use_domain_module'] == 'y') {
cc7a82 91             $sql = "SELECT domain_id, domain FROM domain WHERE domain_id = ?";
MC 92             $domain_check = $app->db->queryOneRecord($sql, $check_domain);
7fe908 93             if(!$domain_check) return;
MC 94             $check_domain = $domain_check['domain'];
95         }
96
97         $result = $this->_check_unique($field_value . '.' . $check_domain, true);
98         if(!$result) return $this->get_error('domain_error_autosub');
99     }
614b23 100     
TB 101     /* Check apache directives */
102     function web_apache_directives($field_name, $field_value, $validator) {
103         global $app;
104         
105         if(trim($field_value) != '') {
106             $security_config = $app->getconf->get_security_config('ids');
107         
108             if($security_config['apache_directives_scan_enabled'] == 'yes') {
109                 
110                 // Get blacklist
111                 $blacklist_path = '/usr/local/ispconfig/security/apache_directives.blacklist';
112                 if(is_file('/usr/local/ispconfig/security/apache_directives.blacklist.custom')) $blacklist_path = '/usr/local/ispconfig/security/apache_directives.blacklist.custom';
113                 if(!is_file($blacklist_path)) $blacklist_path = realpath(ISPC_ROOT_PATH.'/../security/apache_directives.blacklist');
114                 
115                 $directives = explode("\n",$field_value);
116                 $regex = explode("\n",file_get_contents($blacklist_path));
117                 $blocked = false;
118                 $blocked_line = '';
119                 
120                 if(is_array($directives) && is_array($regex)) {
121                     foreach($directives as $directive) {
122                         $directive = trim($directive);
123                         foreach($regex as $r) {
124                             if(preg_match(trim($r),$directive)) {
125                                 $blocked = true;
1f4d64 126                                 $blocked_line .= $directive.'<br />';
614b23 127                             };
TB 128                         }
129                     }
130                 }
131             }
132         }
133         
134         if($blocked === true) {
135             return $this->get_error('apache_directive_blocked_error').' '.$blocked_line;
136         }
137     }
138     
7fe908 139
MC 140     /* internal validator function to match regexp */
141     function _regex_validate($domain_name, $allow_wildcard = false) {
142         $pattern = '/^' . ($allow_wildcard == true ? '(\*\.)?' : '') . '[\w\.\-]{2,255}\.[a-zA-Z0-9\-]{2,30}$/';
143         return preg_match($pattern, $domain_name);
144     }
145
146     /* check if the domain hostname is unique (keep in mind the auto subdomains!) */
147     function _check_unique($domain_name, $only_domain = false) {
148         global $app, $page;
149
150         if(isset($app->remoting_lib->primary_id)) {
151             $primary_id = $app->remoting_lib->primary_id;
10b4c8 152             $domain = $app->remoting_lib->dataRecord;
7fe908 153         } else {
MC 154             $primary_id = $app->tform->primary_id;
10b4c8 155             $domain = $page->dataRecord;
7fe908 156         }
10b4c8 157
T 158         if($domain['ip_address'] == '' || $domain['ipv6_address'] == ''){
159             if($domain['parent_domain_id'] > 0){
cc7a82 160                 $parent_domain = $app->db->queryOneRecord("SELECT * FROM web_domain WHERE domain_id = ?", $domain['parent_domain_id']);
a52337 161                 if(is_array($parent_domain) && !empty($parent_domain)){
MC 162                     $domain['ip_address'] = $parent_domain['ip_address'];
163                     $domain['ipv6_address'] = $parent_domain['ipv6_address'];
164                 }
10b4c8 165             }
T 166         }
7fe908 167
10b4c8 168         // check if domain has alias/subdomains - if we move a web to another IP, make sure alias/subdomains are checked as well
cc7a82 169         $aliassubdomains = $app->db->queryAllRecords("SELECT * FROM web_domain WHERE parent_domain_id = ? AND (type = 'alias' OR type = 'subdomain' OR type = 'vhostsubdomain')", $primary_id);
10b4c8 170         $additional_sql1 = '';
T 171         $additional_sql2 = '';
cc7a82 172         $domain_params = array();
10b4c8 173         if(is_array($aliassubdomains) && !empty($aliassubdomains)){
T 174             foreach($aliassubdomains as $aliassubdomain){
cc7a82 175                 $additional_sql1 .= " OR d.domain = ?";
MC 176                 $additional_sql2 .= " OR CONCAT(d.subdomain, '.', d.domain) = ?";
177                 $domain_params[] = $aliassubdomain['domain'];
10b4c8 178             }
T 179         }
f43ef9 180         
0b6b84 181         
cc7a82 182         $qrystr = "SELECT d.domain_id, IF(d.parent_domain_id != 0 AND p.domain_id IS NOT NULL, p.ip_address, d.ip_address) as `ip_address`, IF(d.parent_domain_id != 0 AND p.domain_id IS NOT NULL, p.ipv6_address, d.ipv6_address) as `ipv6_address` FROM `web_domain` as d LEFT JOIN `web_domain` as p ON (p.domain_id = d.parent_domain_id) WHERE (d.domain = ?" . $additional_sql1 . ") AND d.server_id = ? AND d.domain_id != ?" . ($primary_id ? " AND d.parent_domain_id != ?" : "");
8ae138 183         $params = array_merge(array($domain_name), $domain_params, array($domain['server_id'], $primary_id, $primary_id));
cc7a82 184         $checks = $app->db->queryAllRecords($qrystr, true, $params);
10b4c8 185         if(is_array($checks) && !empty($checks)){
T 186             foreach($checks as $check){
187                 if($domain['ip_address'] == '*') return false;
188                 if($check['ip_address'] == '*') return false;
189                 if($domain['ip_address'] != '' && $check['ip_address'] == $domain['ip_address']) return false;
190                 if($domain['ipv6_address'] != '' && $check['ipv6_address'] == $domain['ipv6_address']) return false;
191             }
192         }
0b6b84 193         
7fe908 194         if($only_domain == false) {
8ae138 195             $qrystr = "SELECT d.domain_id, IF(d.parent_domain_id != 0 AND p.domain_id IS NOT NULL, p.ip_address, d.ip_address) as `ip_address`, IF(d.parent_domain_id != 0 AND p.domain_id IS NOT NULL, p.ipv6_address, d.ipv6_address) as `ipv6_address` FROM `web_domain` as d LEFT JOIN `web_domain` as p ON (p.domain_id = d.parent_domain_id) WHERE (CONCAT(d.subdomain, '.', d.domain) = ?" . $additional_sql2 . ") AND d.server_id = ? AND d.domain_id != ?" . ($primary_id ? " AND d.parent_domain_id != ?" : "");
MC 196             $params = array_merge(array($domain_name), $domain_params, array($domain['server_id'], $primary_id, $primary_id));
a6e3ae 197             $checks = $app->db->queryAllRecords($qrystr, true, $params);
10b4c8 198             if(is_array($checks) && !empty($checks)){
T 199                 foreach($checks as $check){
200                     if($domain['ip_address'] == '*') return false;
201                     if($check['ip_address'] == '*') return false;
202                     if($domain['ip_address'] != '' && $check['ip_address'] == $domain['ip_address']) return false;
203                     if($domain['ipv6_address'] != '' && $check['ipv6_address'] == $domain['ipv6_address']) return false;
204                 }
205             }
7fe908 206         }
0b6b84 207         
7fe908 208         return true;
MC 209     }
210
211     /* check if the client may add wildcard domains */
212     function _wildcard_limit() {
213         global $app;
214
215         if($_SESSION["s"]["user"]["typ"] != 'admin') {
216             // Get the limits of the client
35509d 217             $client_group_id = $app->functions->intval($_SESSION["s"]["user"]["default_group"]);
cc7a82 218             $client = $app->db->queryOneRecord("SELECT limit_wildcard FROM sys_group, client WHERE sys_group.client_id = client.client_id and sys_group.groupid = ?", $client_group_id);
7fe908 219
MC 220             if($client["limit_wildcard"] == 'y') return true;
221             else return false;
222         }
223         return true; // admin may always add wildcard domain
224     }
614b23 225     
7fe908 226
MC 227 }