lots of changed, though it'll probably be all backed-out because
[configmanager.git] / lib / screenos.plugin.php
1 <?php 
2 $HOST_TYPE["screenos"]["name"] = "Netscreen Screen OS";
3 $HOST_TYPE["screenos"]["configform"] = "nsos_formFunction";
4 $HOST_TYPE["screenos"]["postfunction"] = "nsos_postFunction";
5 $HOST_TYPE["screenos"]["updateconfig"] = "nsos_updateConfig";
6 $HOST_TYPE["screenos"]["detail"] = "nsos_detail";
7 $HOST_TYPE["screenos"]["getconfig"] = "nsos_getConfigFromDB";
8
9 function nsos_formFunction()
10 {
11         ?>
12 Note: If your router/firewall/device uses a one-time password, leave these blank and manually update from the
13 home page<br>
14 Name <input type="text" name="username"><br>
15 Password <input type="password" name="password"><br>
16 Method <select name="methodtype"><option value="ssh">SSH</option><option value="telnet">Telnet</option></select>
17         <?php
18 }
19
20 function nsos_postFunction()
21 {
22         if($_REQUEST["password"] != "" && $_REQUEST["username"] != "") {
23                 
24                 $hip = $_REQUEST["hostip"];
25                 $user = $_REQUEST["username"];
26                 $pass = $_REQUEST["password"];
27                 $config = nsos_getConfig($hip, $user, $pass);
28                 $hconfig["username"] = $user;
29                 $hconfig["password"] = $pass;
30                 $lastupdate = time();
31                 $upsched = $_REQUEST["upsched"];
32                 db_updateData("hosts", "hostconfig", base64_encode(serialize($hconfig)), "hostip", "$hip");
33                 db_updateData("hosts", "lastupdate", "$lastupdate", "hostip", "$hip");
34                 db_updateData("hosts", "updateschedule", "$upsched", "hostip", "$hip");
35                 db_insertData("configs", "$hip", "$lastupdate", base64_encode($config));
36                 //db_createTable("configs", "hostip", "configtime", "configdata");
37                 //db_createTable("hosts", "hostname", "hostip", "hosttype", "hostconfig", "lastupdate", "updateschedule");
38                 //function db_updateData($tablename, $column, $newdata, $wherecol, $wheredata, $exact=true)
39                 
40                 echo "if the below is accurate, this has worked<br>";
41                 
42                 echo "Attempting first config grab, i see<br>";
43                 echo "<pre>";
44                 echo $config;
45                 echo "</pre>";
46         } else {
47                 echo "No username and password set, you can update manually from the home page\n";
48                 $hconfig["username"] = "";
49                 $hconfig["password"] = "";
50                 db_updateData("hosts", "hostconfig", base64_encode(serialize($hconfig)), "hostip", "$hip");
51         }
52         
53         
54         echo "<pre>";
55         print_r($_REQUEST);
56         echo "</pre>";
57 }
58
59
60 function nsos_getConfigFromDB($hip, $cid)
61 {
62         $data = db_selectData("configs", "configs_id", "$cid");
63         
64         return base64_decode($data[0]["configdata"]);
65 }
66
67 function nsos_detail($hip)
68 {
69         $data = db_selectData("configs", "hostip", "$hip", "configtime desc");
70         
71         echo "<table border=\"1\">";
72         echo "<tr><th>Config Data</th><th>Compare</th></tr>";
73         foreach($data as $dstruct) {
74                 //echo "<pre>";
75                 //print_r($dstruct);
76                 //echo "</pre>";
77                 $dold = round((time()-$dstruct["configtime"])/86400);
78                                 
79                 $lupdate = strftime("%T %e %b %G", $dstruct["configtime"])." ($dold days old)";
80                 $cid = $dstruct["configs_id"];
81                 echo "<tr><td>$lupdate <a href=\"?action=getconfig&cid=$cid&hip=$hip\">Get</a> <a href=\"?action=downloadconfig&cid=$cid&hip=$hip\">Download</a></td><td>Tick</td></tr>";
82         }
83         echo "</table>";
84         
85 }
86
87 function nsos_updateConfig($hip, $hostconfig)
88 {
89         $hconf = unserialize(base64_decode($hostconfig));
90         if($hconf["username"] == "" && !isset($_REQUEST["username"])) {
91                 echo "No username/password data<br>";
92 ?>
93 <form method="post" action="?action=update&hip=<?php echo $hip ?>">
94 Name <input type="text" name="username"><br>
95 Password <input type="password" name="password"><br>
96 <input type="submit" name="Go" value="Go"><br>
97 </form>
98 <?php
99                 return 0;
100         }
101         
102         if(isset($_REQUEST["username"])) {
103                 $hconf["username"] = $_REQUEST["username"];
104                 $hconf["password"] = $_REQUEST["password"];
105         }
106         
107         $lastconfig_n_d = db_getMaxValue("configs", "configs_id", "hostip", $hip);
108         $lastconfig_n = $lastconfig_n_d[0][0];
109         $lastconfig_d = db_selectData("configs", "configs_id", "$lastconfig_n");
110         $lastconfig = base64_decode($lastconfig_d[0]["configdata"]);
111         
112         $config = nsos_getConfig($hip, $hconf["username"], $hconf["password"]);
113         
114         if(md5($config) == md5($lastconfig)) {
115                 echo "Retrieved config is same as previous one, ignoring<br>";
116                 return 0;
117         }
118         
119         //error_log("getting config with ".$hconf["username"]." and ".$conf["password"]);
120         echo "Got config, saving it to db<br>";
121         $lastupdate = time();
122         db_updateData("hosts", "lastupdate", "$lastupdate", "hostip", "$hip");
123         db_insertData("configs", "$hip", "$lastupdate", base64_encode($config));
124
125         echo "Config is:<br><pre>";
126         echo $config;
127         echo "</pre>";
128 }
129
130 function nsos_getConfig($host, $username, $password)
131 {
132         return genericssh_ssh($host, $username, $password, "cli show configuration");
133 }
134 ?>