Ticket #5709 (new Patches)

Opened 1 year ago

Last modified 1 year ago

module dashboard can't detected value SSH port during installation

Reported by: 3ABXO3 Assigned to:
Priority: minor Milestone: 2.11
Component: FreePBX System Status Version: 2.10-branch
Keywords: Cc:
Confirmation: Unreviewed Distro:
Backend Engine: Asterisk 1.8 Distro Ver:
Backend Ver: SVN Revision (if applicable):

Description

This patch selects SSH port value during installation of the module and stores it in the database. patch https://sites.google.com/site/evrinoma/linux-dstgc-8/centosasteriskfreep...

Change History

03/23/12 08:37:27 changed by 3ABXO3

"https://sites.google.com/site/evrinoma/linux-dstgc-8/centosasteriskfreepbx/freepbx-2.10.0-dashboard.patch?revision=1" link 404 8(

Submitted by: 3ABXO3 (evrinoma@gmail.com)
Date: 2012-03-16
Initial Package Version:
Upstream Status:
Origin:
Description:
diff -Naur install.php amp_conf/htdocs/admin/modules/dashboard/install.php
--- /amp_conf/htdocs/admin/modules/dashboard/install.php	2011-12-18 01:02:34.000000000 +0400
+++ install.php	2012-03-16 11:00:54.448504564 +0400
@@ -30,7 +30,13 @@
 $stats_value = 6;
 $info_value = 30;
 
-if ($freepbx_conf->conf_setting_exists('DASHBOARD_STATS_UPDATE_TIME') || $freepbx_conf->conf_setting_exists('DASHBOARD_INFO_UPDATE_TIME')) {
+define(CONF_SSH_PATH,'/etc/ssh/sshd_config');
+define(CONF_SSH_PORT,'PORT');
+
+if ($freepbx_conf->conf_setting_exists('DASHBOARD_STATS_UPDATE_TIME') 
+|| $freepbx_conf->conf_setting_exists('DASHBOARD_INFO_UPDATE_TIME')
+|| $freepbx_conf->conf_setting_exists('SSHPORT')
+) {
   $full_settings =& $freepbx_conf->get_conf_settings();
 
   if ($full_settings['DASHBOARD_STATS_UPDATE_TIME']['type'] != CONF_TYPE_SELECT) {
@@ -41,6 +47,7 @@
       if ($old_val < $val) {
         break;
       }
+      out(sprintf(_("%s changed from %s to %s"),'DASHBOARD_STATS_UPDATE_TIME',$old_val,$stats_value));
       $stats_value = $val;
     }
     if ($stats_value != $old_val) {
@@ -61,6 +68,36 @@
       out(sprintf(_("%s changed from %s to %s"),'DASHBOARD_INFO_UPDATE_TIME',$old_val,$info_value));
     }
   }
+  if ($full_settings['SSHPORT']['type'] == CONF_TYPE_INT) {
+    $old_val = $full_settings['SSHPORT']['value'];
+    $freepbx_conf->remove_conf_setting('SSHPORT');
+	$content = file( CONF_SSH_PATH);
+	if ($content) {
+		foreach ($content as $line) 
+		if ( $line[0]!="\n" &&  $line[0]!="#" ){
+		    $line = strtoupper ($line);
+		    $pos = strpos($line,CONF_SSH_PORT);
+		    if ($pos !== false) {
+			$str = preg_replace("/[\s]/","",substr($line,$pos+strlen(CONF_SSH_PORT),strlen($line)));
+			$found=false;$stop=false;$i = 0;
+			while (!$stop) {
+			    if (is_numeric($str[$i]) && !($stop)){
+			        $digit .= $str[$i];
+			        $found=true;
+			    }else{
+				if ($found or $str[$i]=="#") $stop=true; else $stop=false;
+			    }
+			    $i++;
+			}
+			(strlen($digit) == 0) ? $ssh_value = 22 : $ssh_value = (int) $digit;
+		    }
+		}
+	}else
+	out(sprintf(_("File %s not found"),  CONF_SSH_PATH));
+    if ($info_value != $old_val) {
+      out(sprintf(_("%s changed from %s to %s"),'SSHPORT',$old_val,$ssh_value));
+    }
+  }
   unset($full_settings);
 }
 
@@ -98,7 +135,7 @@
 
 // SSHPORT
 //
-$set['value'] = '';
+$set['value'] = $ssh_value;
 $set['defaultval'] =& $set['value'];
 $set['options'] = array(1,65535);
 $set['readonly'] = 0;


03/23/12 17:19:10 changed by p_lindheimer

  • owner deleted.
  • component changed from - choose - to FreePBX System Status.

From quick glance, first observation, I question if this is not better checked in retrieve_conf (this module can provide code to be executed at that time) vs. module install time, that would keep it up-to-date if changes were made.

A quick observation/question … does this become an issue on other Linux distros based on the path of the conf file? I don't think it is that big of a deal other than this probably should be modified to check if the file_exists() before reading it so warnings are not kicked out and if it doesn't find the file then it can default and is no worse then it was.

03/26/12 00:50:14 changed by 3ABXO3

thank for help... well I'll rewrite the patch

04/02/12 05:23:30 changed by 3ABXO3

hello p_lindheimer... 8) what you say about that

Submitted by: 3ABXO3 (evrinoma@gmail.com)
Date: 2012-04-02
Initial Package Version:
Upstream Status:
Origin:
Description:
diff -Naur dashboard/class.procinfo.php dashboard/class.procinfo.php
--- dashboard/class.procinfo.php        2011-12-18 01:02:34.000000000 +0400
+++ dashboard-new/class.procinfo.php    2012-04-02 12:59:07.000000000 +0400
@@ -1,5 +1,5 @@
 <?php
-if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
+//if (!defined('FREEPBX_IS_AUTH')) { die('No direct script access allowed'); }
 
 class procinfo {
        var $distro;
@@ -17,6 +17,49 @@
                return false;
        }
 
+       function get_SSH_port($server = "localhost"){
+               $cont = file("/proc/net/tcp");
+               $search = "SSH";
+               $array_port="";
+               $max = count($cont);
+               for ($i=0;$i<$max;$i++){
+                       $str=explode(" ", $cont[$i]);
+                       if (preg_match('/[a-fA-F0-9:]$/', $str[4])) {
+                               $data=explode(":", $str[4]);
+                               $array_port.=hexdec($data[1]).":"; 
+                       }
+               }
+               $array_port=array_diff(array_unique(explode(":", $array_port)),array(''));;
+               $max = count($array_port);
+               for ($i=0;$i<$max;$i++){
+                       if ($sock = @fsockopen($server, $array_port[$i], $errno, $errstr, $timeout)) {
+                               stream_set_timeout($sock, 0, 100000);
+                               $tmp = strtoupper(fread($sock, 127));
+                               if (strpos($tmp,$search)!==false)
+                                       return $array_port[$i];
+                               fclose($sock);
+                       }
+               }
+       return -1;
+       }
+
+       function save_SSH_port($ssh_value){
+       global $db;
+       $sql="";
+       $freepbx_conf =& freepbx_conf::create();
+       if ($freepbx_conf->conf_setting_exists('SSHPORT')){
+               $old_val = $full_settings['SSHPORT']['value'];
+               if ($old_val != $ssh_value){
+                               $sql = "UPDATE asterisk.freepbx_settings SET value = ".sql_formattext($ssh_value)."WHERE freepbx_settings.keyword = 'SSHPORT'";
+                               $result = $db->query($sql);
+                               if(DB::IsError($result)) {
+                                       die_freepbx($result->getMessage().$sql);
+                               }
+                       }
+               unset($full_settings);
+               }
+       }
+
        /* FOP has been removed, currenlty unsupported optional module
        function check_fop_server() {
     global $amp_conf;
diff -Naur dashboard/page.index.php dashboard-new/page.index.php
--- dashboard/page.index.php    2012-02-15 00:28:55.000000000 +0400
+++ dashboard-new/page.index.php        2012-04-02 12:59:37.000000000 +0400
@@ -330,6 +330,7 @@
        global $astinfo;
        global $amp_conf;
        $out = '';
+       $port = '';
 
        $out .= "<h3 class=\"ui-widget-header  ui-state-default ui-corner-all\">"._("Server Status")."</h3>";
        // asterisk
@@ -384,7 +385,13 @@
        if ($procinfo->check_port($ssh_port)) {
                $out .= draw_status_box(_("SSH Server"), "ok", _('SSH Server is running'));
        } else {
+               $port=$procinfo->get_SSH_port();
+               if ($port!=-1){
+                       $procinfo->save_SSH_port($port);
+                       $out .= draw_status_box(_("SSH Server"), "ok", _('SSH Server is running'));
+               }else{
                $out .= draw_status_box(_("SSH Server"), "warn", _('SSH Server is not running, you will not be able to connect to the system console remotely'));
+               }
        }
        return $out;
 }


04/02/12 05:55:33 changed by 3ABXO3

this is not necessarily $port=$procinfo->get_SSH_port(); Now we can determine when the ssh server is not working and when you simply changed the port

04/02/12 05:55:57 changed by 3ABXO3

sorry $procinfo->save_SSH_port($port);