function getOurHostID() { $db = gs_db_slave_connect(); $rs = $db->execute('SELECT `id`, `host` FROM `hosts`'); $hosts = array(); while ($r = $rs->fetchRow()) { $hosts[] = $r; } $ips = array(); foreach ($hosts as $h) { $h['host'] = trim(normalizeIPs($h['host'])); if (preg_match('/^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$/', $h['host'])) { $ips[$h['host']] = $h['id']; } else { $tmp = getHostByNameL($h['host']); if (is_array($tmp)) { foreach ($tmp as $ip) { $ips[normalizeIPs($ip)] = $h['id']; } } } } unset($hosts); $ifconfig = normalizeIPs(trim(@shell_exec('ifconfig 2>>/dev/null'))); foreach ($ips as $ip => $hostid) { if (strPos($ifconfig, $ip) !== false) { return $hostid; } } return false; }
function ip_addr_in_network_list($ip, $network_list) { $networks = explode(',', $network_list); foreach ($networks as $net) { if (ip_addr_in_network($ip, trim(normalizeIPs($net)))) { return true; } } return false; }
function gs_prov_check_trust_requester() { global $_SERVER; $ret = array('allowed' => false, 'proxy_ip' => null, 'phone_ip' => '0.0.0.0'); # find the phone's IP address # $remote_ip = @$_SERVER['REMOTE_ADDR']; $xff_header = gs_get_conf('GS_PROV_PROXIES_XFF_HEADER'); $xff_env_key = 'HTTP_' . str_replace('-', '_', strToUpper($xff_header)); if (array_key_exists('HTTP_VIA', $_SERVER) || array_key_exists($xff_env_key, $_SERVER)) { # is a request through a proxy $ret['proxy_ip'] = $remote_ip; # do we trust the proxy's XFF header? $proxy_allowed = ip_addr_in_network_list($remote_ip, gs_get_conf('GS_PROV_PROXIES_TRUST')); if (!$proxy_allowed) { gs_log(GS_LOG_NOTICE, 'Proxy ' . $remote_ip . ' is not trusted'); return $ret; } if (!array_key_exists($xff_env_key, $_SERVER)) { gs_log(GS_LOG_NOTICE, 'No "' . $xff_header . '" header from proxy ' . $remote_ip); return $ret; } $xff_chain = explode(',', $_SERVER[$xff_env_key]); if (count($xff_chain) < 1) { gs_log(GS_LOG_NOTICE, 'Empty "' . $xff_header . '" header from proxy ' . $remote_ip); return $ret; } $phone_ip = trim($xff_chain[0]); if (!preg_match('/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/', $phone_ip)) { gs_log(GS_LOG_NOTICE, 'IP address "' . $phone_ip . '" not recognized'); return $ret; } $ret['phone_ip'] = $phone_ip; } else { # direct request without proxy $ret['phone_ip'] = $remote_ip; } $ret['phone_ip'] = trim(normalizeIPs($ret['phone_ip'])); # phone allowed to request settings? # $ret['allowed'] = ip_addr_in_network_list($ret['phone_ip'], gs_get_conf('GS_PROV_ALLOW_NET')); if (!$ret['allowed']) { gs_log(GS_LOG_NOTICE, 'Phone ' . $ret['phone_ip'] . ' is not allowed to request settings'); return $ret; } return $ret; }
function gs_get_listen_to_ips($primary_only = false) { /* if (gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) { # return special address //return ($primary_only ? array('255.255.255.255') : array('255.255.255.255')); return array('255.255.255.255'); } */ //if (gs_get_conf('GS_INSTALLATION_TYPE_SINGLE')) { if (gs_get_conf('GS_INSTALLATION_TYPE') === 'gpbx') { return array(trim(gs_keyval_get('vlan_0_ipaddr'))); } $file = GS_DIR . 'etc/listen-to-ip'; if (!@file_exists($file)) { # kann entweder passieren wenn wir ein Gemeinschaft-Node sind # (dann ist es extrem schlecht wenn die Datei fehlt) oder wenn # wir ein Web-Server ohne Asterisk sind (dann ist es ok) gs_log(GS_LOG_DEBUG, "File \"{$file}\" not found"); return false; } if (!is_array($lines = @file($file))) { gs_log(GS_LOG_DEBUG, "Failed to read \"{$file}\""); return false; } $ips = array(); foreach ($lines as $line) { $line = trim($line); if ($line == '' || @$line[0] == '#') { continue; } if (!preg_match('/^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}/', $line, $m)) { continue; } $ips[] = normalizeIPs($m[0]); if ($primary_only) { # only return the first IP address (our main one) return $ips; } } // remove duplicates: $ips = array_flip(array_flip($ips)); sort($ips); return $ips; }
function gs_host_add($host_ip_or_name, $comment, $foreign = false, $group_id = null, $boi_prefix = '', $sip_proxy_from_wan = null, $sip_sbc_from_wan = null) { if (!$host_ip_or_name) { return new GsError('Invalid host.'); } $host = normalizeIPs($host_ip_or_name); if (!preg_match('/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/', $host)) { # not an IP address. => resolve hostname $addresses = @gethostbynamel($host); if (count($addresses) < 1) { return new GsError('Could not resolve hostname: ' . $host); } elseif (count($addresses) > 1) { return new GsError('Hostname ' . $host . ' cannot be used because it\'s resolved to more than one IP addr.'); } elseif (count($addresses) == 1) { if (strlen($addresses[0]) == 0) { return new GsError('Could not resolve hostname: ' . $host); } $host = $addresses[0]; } } else { $host = $host_ip_or_name; } $group_id = (int) $group_id; if ($group_id < 1) { $group_id = null; } if (!$foreign) { if ($boi_prefix != '') { return new GsError('Route prefix not allowed for non-foreign hosts.'); } } else { if ($boi_prefix != '' && !preg_match('/^[1-9][0-9]*$/', $boi_prefix)) { return new GsError('Route prefix must be numeric.'); } } if ($sip_proxy_from_wan != '' && !preg_match('/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/', $sip_proxy_from_wan)) { return new GsError('Invalid IP address of SIP proxy from WAN.'); } if ($sip_sbc_from_wan != '' && !preg_match('/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/', $sip_sbc_from_wan)) { return new GsError('Invalid IP address of SIP SBC from WAN.'); } if ($foreign) { $api = gs_get_conf('GS_BOI_API_DEFAULT'); } else { $api = ''; } # connect to db # $db = gs_db_master_connect(); if (!$db) { return new GsError('Could not connect to database.'); } $db->execute('OPTIMIZE TABLE `hosts`'); # recalculate next auto-increment value $db->execute('ANALYZE TABLE `hosts`'); $sql_query = 'INSERT INTO `hosts` ( `id`, `host`, `comment`, `is_foreign`, `group_id` ) VALUES ( NULL, \'' . $db->escape($host) . '\', \'' . $db->escape($comment) . '\', ' . ($foreign ? 1 : 0) . ', ' . (int) $group_id . ' )'; $ok = $db->execute($sql_query); if (!$ok) { return new GsError('Failed to add host ' . $host); } $host_id = (int) $db->getLastInsertId(); if ($host_id < 1) { return new GsError('Failed to add host ' . $host); } $db->execute('REPLACE INTO `host_params` (`host_id`, `param`, `value`) VALUES (' . $host_id . ', \'api\', \'' . $db->escape($api) . '\')'); if ($sip_proxy_from_wan != '') { $db->execute('REPLACE INTO `host_params` (`host_id`, `param`, `value`) VALUES (' . $host_id . ', \'sip_proxy_from_wan\', \'' . $db->escape($sip_proxy_from_wan) . '\')'); } if ($sip_sbc_from_wan != '') { $db->execute('REPLACE INTO `host_params` (`host_id`, `param`, `value`) VALUES (' . $host_id . ', \'sip_server_from_wan\', \'' . $db->escape($sip_sbc_from_wan) . '\')'); } if ($foreign) { $db->execute('REPLACE INTO `host_params` (`host_id`, `param`, `value`) VALUES (' . $host_id . ', \'route_prefix\', \'' . $db->escape($boi_prefix) . '\')'); } return true; }
function ip_addr_dec($ip) { $ip = trim(normalizeIPs($ip)); if (!preg_match('/(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})/', $ip, $m)) { return 0; } $ipDec = 0; for ($i = 4; $i > 0; --$i) { $ipDec += ($m[$i] < 256 ? $m[$i] : 255) * pow(256, 4 - $i); } return $ipDec; //$ipBin = str_pad(decBin($ipDec), 32, '0', STR_PAD_LEFT); //return $ipBin; }
} return null; } $edit_host = (int) trim(@$_REQUEST['edit']); $save_host = (int) trim(@$_REQUEST['save']); $per_page = (int) GS_GUI_NUM_RESULTS; $page = (int) @$_REQUEST['page']; $host = trim(@$_REQUEST['host']); $hostid = (int) trim(@$_REQUEST['hostid']); $comment = trim(@$_REQUEST['comment']); $group_id = (int) trim(@$_REQUEST['grp_id']); $delete_host = (int) trim(@$_REQUEST['delete']); $search_number = trim(@$_REQUEST['number']); $search_ip = trim(@$_REQUEST['ipaddr']); if ($host) { $host = normalizeIPs($host); $bInvalHostName = false; if (!preg_match('/^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$/', $host)) { # not an IP address. => resolve hostname $addresses = @gethostbynamel($host); if (count($addresses) < 1) { echo '<div class="errorbox">'; echo sPrintF(__('Hostname "%s" konnte nicht aufgelöst werden.'), htmlEnt($host)); echo '</div>', "\n"; $bInvalHostName = true; } elseif (count($addresses) > 1) { echo '<div class="errorbox">'; echo sPrintF(__('Hostname "%s" kann nicht verwendet werden, da er zu mehr als einer IP-Adresse aufgelöst wird.'), htmlEnt($host)); echo '</div>', "\n"; $bInvalHostName = true; } elseif (count($addresses) == 1) {
echo '<img alt="', __('weiter'), '" src="', GS_URL_PATH, 'crystal-svg/16/act/next_notavail.png" />', "\n"; } ?> </th> </tr> </thead> <tbody> <?php @ob_flush(); @flush(); if (@$rs) { $i = 0; while ($r = $rs->fetchRow()) { echo '<tr class="', ++$i % 2 ? 'odd' : 'even', '">', "\n"; $ip = normalizeIPs($r['host']); if (!@is_array($nodes[$ip])) { $nodes[$ip] = array(); } $nodes[$ip]['host_id'] = $r['id']; $nodes[$ip]['comment'] = $r['comment']; $nodes[$ip]['active'] = true; $nodes[$ip]['watchdog'] = false; if ($edit_host == $r['id']) { echo '<form method="post" action="', GS_URL_PATH, '">', "\n"; echo gs_form_hidden($SECTION, $MODULE), "\n"; echo '<input type="hidden" name="page" value="', htmlEnt($page), '" />', "\n"; echo '<input type="hidden" name="save" value="', $r['id'], '" />', "\n"; echo '<td class="r">', htmlEnt($r['id']), '</td>', "\n"; echo '<td>', htmlEnt(@$nodes[$ip]['static_ip']), '</td>', "\n"; echo '<td>';
$nodes[$ip] = $node; $nodes[$ip]['active'] = false; $nodes[$ip]['watchdog'] = true; } # get hosts from DB and mix with nodes # $hosts = gs_hosts_get(); if (isGsError($hosts)) { $warnings[] = $hosts->getMsg(); $hosts = array(); } elseif (!is_array($hosts)) { $warnings[] = __('Fehler beim Abfragen der Hosts aus der Datenbank!'); $hosts = array(); } foreach ($hosts as $host) { $ip = normalizeIPs($host['host']); if (!@is_array($nodes[$ip])) { $nodes[$ip] = array(); } $nodes[$ip]['host_id'] = $host['id']; $nodes[$ip]['comment'] = $host['comment']; $nodes[$ip]['active'] = true; $nodes[$ip]['watchdog'] = false; } unset($hosts); if (is_array($warnings) && count($warnings) > 0) { foreach ($warnings as $warning) { ?> <div style="max-width:600px;"> <img alt=" " src="<?php echo GS_URL_PATH;