1<?php
2/*
3 * Kewlio Looking Glass, klg.php
4 *
5 * Copyright (c) 2012, Daniel Austin MBCS <daniel@kewlio.net>
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, this
12 *    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 the Daniel Austin MBCS 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, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 *
31 */
32
33	/* bring in configuration */
34	require("klg_config.php");
35	require("klg_router_functions.php");
36	if ($config["default_form_method"] == "")
37		$config["default_form_method"] = "post";
38
39	/* define variables for configurations without register_globals */
40	$server = $action = $args = "";
41	if (isset($_REQUEST["server"]))
42		$server = htmlentities($_REQUEST["server"]);
43	if (isset($_REQUEST["action"]))
44		$action = htmlentities($_REQUEST["action"]);
45	if (isset($_REQUEST["args"]))
46		$args = htmlentities($_REQUEST["args"]);
47	/* if register_globals is enabled, ensure that we overwrite $error below anyway */
48	$error = "";
49
50	/* check for defaults here */
51	if ($server=="")
52	{
53		$args = $_SERVER["REMOTE_ADDR"];
54		if (strpos($args, ":")>0)
55		{
56			/* ipv6 client */
57			$server = $config["default_ipv6_router"];
58			$action = $config["default_ipv6_command"];
59		} else {
60			/* ipv4 client */
61			$server = $config["default_ipv4_router"];
62			$action = $config["default_ipv4_command"];
63		}
64	}
65
66	/* define our functions here */
67	Function DisplayCriticalError($errortext)
68	{
69		/* display error text and exit application */
70		Global $config;
71		echo "<HTML>\n";
72		echo "<HEAD><TITLE>" . $config["page_title"] . "</TITLE></HEAD>\n";
73		echo "<BODY>\n";
74		echo "An error occured!<br /><br />\n";
75		echo "$errortext\n";
76		echo "</BODY>\n";
77		echo "</HTML>\n";
78		die;
79	}
80
81	Function CleanOutput($text)
82	{
83		/* clean the output of router commands to make them work in our environment */
84		Global $config;
85		$tmp = "";
86		while (strlen($text) > $config["output_max_width"])
87		{
88			/* add linebreaks in to format output */
89			$tmp .= substr($text, 0, $config["output_max_width"]);
90			$tmp .= "\n";
91			$text = substr($text, $config["output_max_width"], strlen($text));
92		}
93		$tmp .= $text;
94		/* get rid of any double line breaks (just in case) */
95		$tmp = str_replace("\n\n","\n",$tmp);
96		/* clean output a little more */
97		$tmp = str_replace("> quit","&gt; quit", $tmp);
98		return $tmp;
99	}
100
101	Function ValidateIP($ip)
102	{
103		/* validate an IP address based on the required action/address family */
104		Global $action;
105		$tmp = "";
106		for ($i=0; $i<strlen($ip); $i++)
107		{
108			if ($action=="show ip bgp" || $action=="show ip route")
109			{
110				switch (substr($ip, $i, 1)) {
111					case "0": case "1": case "2": case "3":
112					case "4": case "5": case "6": case "7":
113					case "8": case "9": case ".": case "/":	$tmp .= substr($ip, $i, 1); break;
114					default:				break;
115				}
116			}
117			if ($action=="show bgp ipv6 unicast" || $action=="show ipv6 route")
118			{
119				switch (strtolower(substr($ip, $i, 1))) {
120					case "0": case "1": case "2": case "3":
121					case "4": case "5": case "6": case "7":
122					case "8": case "9": case "a": case "b":
123					case "c": case "d": case "e": case "f":
124					case ":": case "/":			$tmp .= substr($ip, $i, 1); break;
125					default:				break;
126				}
127			}
128			if ($action=="ping" || $action=="traceroute" || $action=="show route")
129			{
130				switch (strtolower(substr($ip, $i, 1))) {
131					case "0": case "1": case "2": case "3":
132					case "4": case "5": case "6": case "7":
133					case "8": case "9": case "a": case "b":
134					case "c": case "d": case "e": case "f":
135					case ":": case "/": case ".":		$tmp .= substr($ip, $i, 1); break;
136					default:				break;
137				}
138			}
139			if ($action=="show ip bgp regexp" || $action=="show bgp ipv6 unicast regexp")
140			{
141				switch (substr($ip, $i, 1)) {
142					case "0": case "1": case "2": case "3":
143					case "4": case "5": case "6": case "7":
144					case "8": case "9": case "^": case "$":
145					case "_": case " ": case ".":		$tmp .= substr($ip, $i, 1); break;
146					default:				break;
147				}
148			}
149		}
150		return $tmp;
151	}
152
153	Function LookupCommunity($asn, $community)
154	{
155		/* lookup a Community from our database */
156		Global $sql_id, $cache_community, $config;
157		if ($config["lookup_communities"] == "0")
158			return "";
159		if (!isset($cache_community["$asn"]))
160		{
161			$query = "SELECT `asn`,`community`,`name` FROM `BGPcommunities` WHERE `asn`='$asn' AND `community`='$community'";
162			$result = mysqli_query($sql_id, $query);
163			if (mysqli_num_rows($result)>0)
164			{
165				$row = mysqli_fetch_object($result);
166				$cname = $row->name;
167			} else {
168				$cname = "";
169			}
170		} else {
171			if (isset($cache_community["$asn"]))
172				$cname = $cache_community["$asn"]["$community"];
173			else
174				$cname = "";
175		}
176		return $cname;
177	}
178
179	Function LookupRIPEbased($host, $asn)
180	{
181		/* lookup an AS name via a RIPE-based whois server */
182		Global $sql_id, $cache_asnums;
183		$fd = @fsockopen($host,43,$errno,$errmsg);
184		if ($fd)
185		{
186			@fwrite($fd, "-T aut-num -r AS$asn\n");
187			while ($buf = @fgets($fd, 8192))
188			{
189				if (substr($buf, 0, 8)=="as-name:")
190				{
191					$buf = str_replace("\r","",$buf);
192					$buf = str_replace("\n","",$buf);
193					while (strpos($buf, "\t")>0)
194						$buf = str_replace("\t", " ", $buf);
195					while (strpos($buf, "  ")>0)
196						$buf = str_replace("  ", " ", $buf);
197					$asname = substr($buf, strpos($buf, " ")+1, strlen($buf));
198					$query = "REPLACE INTO `asnumbers` (`asnum`,`name`,`ts`)VALUES(";
199					$query .= "\"$asn\",\"" . AddSlashes($asname) ."\"," . time() . ")";
200					$result = mysqli_query($sql_id, $query);
201					$cache_asnums["$asn"] = $asname;
202				}
203			}
204			@fclose($fd);
205		}
206		return $asname;
207	}
208
209	Function LookupAS($asn)
210	{
211		/* lookup an AS and return the result - check local cache first */
212		Global $sql_id, $config, $cache_asnums;
213		if ($config["lookup_asnumbers"] == "0")
214			return $asn;
215		if (!isset($cache_asnums["$asn"]))
216		{
217			$ts = time() - $config["cache_time_asnumbers"];
218			$query = "SELECT `asnum`,`name`,`ts` FROM `asnumbers` WHERE `asnum`='$asn' AND (`ts`>=$ts OR `ts`=0)";
219			$result = mysqli_query($sql_id, $query);
220			if (mysqli_num_rows($result)>0)
221			{
222				$row = mysqli_fetch_object($result);
223				$cache_asnums["$asn"] = $row->name;
224				$asname = $row->name;
225			} else {
226				/* unknown locally - try RIPE */
227				$asname = LookupRIPEbased("whois.ripe.net",$asn);
228				if ($asname=="")
229				{
230					/* unknown at RIPE - try ARIN instead */
231					$fd2 = @fsockopen("whois.arin.net",43,$errno,$errmsg);
232					if ($fd2)
233					{
234						@fwrite($fd2, "AS$asn\n");
235						while ($buf = @fgets($fd2, 8192))
236						{
237							if (substr($buf, 0, 8)=="ASName: ")
238							{
239								$buf = str_replace("\r","",$buf);
240								$buf = str_replace("\n","",$buf);
241								while (strpos($buf, "  ")>0)
242									$buf = str_replace("  ", " ", $buf);
243								$asname = substr($buf, strpos($buf, " ")+1, strlen($buf));
244								if (substr($asname, 0, 6)=="APNIC-")
245								{
246									/* force an APNIC lookup */
247									$asname = "";
248								} else {
249									$query = "REPLACE INTO `asnumbers` (`asnum`,`name`,`ts`) ";
250									$query .= "VALUES(\"$asn\",\"" . AddSlashes($asname) . "\"," . time() . ")";
251									$result = mysqli_query($sql_id, $query);
252									$cache_asnums["$asn"] = $asname;
253								}
254							}
255						}
256						@fclose($fd2);
257					}
258				}
259				if ($asname=="")
260				{
261					/* unknown at RIPE or ARIN - try APNIC instead */
262					$asname = LookupRIPEbased("whois.apnic.net",$asn);
263				}
264			}
265		} else {
266			/* cached entry - report local cache name */
267			if (isset($cache_asnums["$asn"]))
268				$asname = $cache_asnums["$asn"];
269			else
270				$asname = "";
271		}
272		return $asname;
273	}
274
275	/* Load the template */
276	$fd = @fopen($config["page_template"], "r");
277	if (!$fd)
278	{
279		/* can not read the template */
280		DisplayCriticalError("Unable to open output template.");
281	}
282	$klg_template = @fread($fd, 128000);
283	@fclose($fd);
284	/* do some sanity checks on template */
285	if (!strstr($klg_template, "##KLG_TITLE##"))
286		DisplayCriticalError("Output template does not contain '##KLG_TITLE##' variable!");
287	if (!strstr($klg_template, "##KLG_FORM_START##"))
288		DisplayCriticalError("Output template does not contain '##KLG_FORM_START##' variable!");
289	if (!strstr($klg_template, "##KLG_ROUTER_LIST##"))
290		DisplayCritialError("Output template does not contain '##KLG_ROUTER_LIST##' variable!");
291	if (!strstr($klg_template, "##KLG_ACTION_LIST##"))
292		DisplayCritialError("Output template does not contain '##KLG_ACTION_LIST##' variable!");
293	if (!strstr($klg_template, "##KLG_ARGS##"))
294		DisplayCriticalError("Output template does not contain '##KLG_ARGS##' variable!");
295	if (!strstr($klg_template, "##KLG_SUBMIT_BUTTON##"))
296		DisplayCriticalError("Output template does not contain '##KLG_SUBMIT_BUTTON##' variable!");
297	if (!strstr($klg_template, "##KLG_FORM_END##"))
298		DisplayCriticalError("Output template does not contain '##KLG_FORM_END##' variable!");
299	if (!strstr($klg_template, "##KLG_OUTPUT##"))
300		DisplayCriticalError("Output template does not contain '##KLG_OUTPUT##' variable!");
301	/* do some extra sanity checks on template (order of variables) */
302	if (strpos($klg_template, "##KLG_FORM_END##") < strpos($klg_template, "##KLG_FORM_START##"))
303		DisplayCriticalError("Output template can not have '##KLG_FORM_END##' before '##KLG_FORM_START##'!");
304
305	if ($config["mysql_enable"] == "1")
306	{
307		/* connect to database */
308		$sql_id = @mysqli_connect($config["mysql_host"], $config["mysql_user"], $config["mysql_pass"], $config["mysql_db"]);
309		if (!$sql_id)
310		{
311			/* could not connect */
312			$error .= "Unable to connect to SQL (" . mysqli_error($sql_id) . ")<br />\n";
313		}
314	} else {
315		/* override some other config variables to disable lookups */
316		$config["lookup_asnumbers"] = "0";
317		$config["lookup_communities"] = "0";
318	}
319
320	/* final validation checks */
321	$i = 0;
322	$found_router = 0;
323	$enable_juniper_commands = 0;
324	while ((isset($routers[$i])) && ($routers[$i]["id"] != ""))
325	{
326		if ($routers[$i]["type"] == "juniper")
327			$enable_juniper_commands = 1;
328		if ($routers[$i]["id"] == $server)
329		{
330			/* this is the router we're interested in! */
331			$found_router = 1;
332			$lg_server = $routers[$i]["host"];
333			$lg_port = $routers[$i]["bgpd_port"];
334			if ($routers[$i]["type"] == "cisco" || $routers[$i]["type"] == "juniper")
335				$lg_port2 = $lg_port;
336			else
337				$lg_port2 = $routers[$i]["zebra_port"];
338			$lg_login = $routers[$i]["login"];
339			$lg_type = $routers[$i]["type"];
340			$lg_ssh = $routers[$i]["bgpd_ssh"];
341			$lg_ssh_user = $routers[$i]["ssh_user"];
342			$lg_ssh_pass = $routers[$i]["ssh_pass"];
343			$flags = $routers[$i]["flags"];
344			/* check the the action is valid for this router */
345			/* check global commands first */
346			if (($config["allow_ipv4_commands"] != "1") && (substr($action,0,8)=="show ip "))
347			{
348				$error .= "IPv4 commands are not available on this looking glass<br />\n";
349			}
350			if (($config["allow_ipv6_commands"] != "1") && ((substr($action,0,9)=="show bgp ") || (substr($action,0,10)=="show ipv6 ")))
351			{
352				$error .= "IPv6 commands are not available on this looking glass<br />\n";
353			}
354			if ($config["allow_ping"] != "1" && $action=="ping")
355			{
356				$error .= "The PING command is not available on this looking glass<br />\n";
357			}
358			if ($config["allow_traceroute"] != "1" && $action=="traceroute")
359			{
360				$error .= "The TRACEROUTE command is not available on this looking glass<br />\n";
361			}
362			if ($config["allow_show_interface"] != "1" && $action=="show interface")
363			{
364				$error .= "The 'SHOW INTERFACE' command is not available on this looking glass<br />\n";
365			}
366			if ($config["allow_environmental"] != "1" && $action=="show environment all")
367			{
368				$error .= "The 'SHOW ENVIRONMENT ALL' command is not available on this looking glass<br />\n";
369			}
370			/* ok, global checks done - do local router checks - these are not really errors, but report them anyway */
371			if (($flags & $flag_allow_ipv4) == 0)
372			{
373				/* this router does not allow ipv4 commands */
374				if (substr($action,0,8)=="show ip ")
375					$error .= "The selected router ($server) does not support IPv4 commands<br />\n";
376			}
377			if (($flags & $flag_allow_ipv6) == 0)
378			{
379				/* this router does not allow ipv6 commands */
380				if ((substr($action,0,9)=="show bgp ") || (substr($action,0,10)=="show ipv6 "))
381					$error .= "The selected router ($server) does not support IPv6 commands<br />\n";
382			}
383			if ((($flags & $flag_allow_ping) == 0) && ($action=="ping"))
384				$error .= "The selected router ($server) does not support the PING command<br />\n";
385			if ((($flags & $flag_allow_traceroute) == 0) && ($action=="traceroute"))
386				$error .= "The selected router ($server) does not support the TRACEROUTE command<br />\n";
387			if ((($flags & $flag_allow_show_interface) == 0) && ($action=="show interface"))
388				$error .= "The selected router ($server) does not support the 'SHOW INTERFACE' command<br />\n";
389			if ((($flags & $flag_allow_environmental) == 0) && ($action=="show environment all"))
390				$error .= "The selected router ($server) does not support the 'SHOW ENVIRONMENT ALL' command<br />\n";
391			if ((($flags & $flag_allow_dampening) == 0) && (strpos($action,"dampening")>0))
392				$error .= "The selected router ($server) does not support the '" . strtoupper($action) . " command<br />\n";
393			/* specific commands now */
394			switch ($action) {
395				case "show route":		if ((($flags & $flag_allow_junos_show_route) == 0) || ($lg_type != "juniper"))
396									$error .= "The selected router ($server) does not support the 'show route' command<br />\n";
397								break;
398				case "show ip bgp":		if (($flags & $flag_allow_show_ip_bgp) == 0)
399									$error .= "The selected router ($server) does not support the 'show ip bgp' command<br />\n";
400								break;
401				case "show ip bgp summary":	if (($flags & $flag_allow_show_ip_bgp_summary) == 0)
402									$error .= "The selected router ($server) does not support the 'show ip bgp summary' command<br />\n";
403								break;
404				case "show ip route":		if (($flags & $flag_allow_show_ip_route) == 0)
405									$error .= "The selected router ($server) does not support the 'show ip route' command<br />\n";
406								break;
407				case "show ip bgp regexp":	if (($flags & $flag_allow_show_ip_bgp_regexp) == 0)
408									$error .= "The selected router ($server) does not support the 'show ip bgp regexp' command<br />\n";
409								break;
410				case "show bgp ipv6 unicast":	if (($flags & $flag_allow_show_bgp_ipv6) == 0)
411									$error .= "The selected router ($server) does not support the 'show bgp ipv6 unicast' command<br />\n";
412								break;
413				case "show bgp ipv6 unicast summary":	if (($flags & $flag_allow_show_bgp_summary) == 0)
414									$error .= "The selected router ($server) does not support the 'show bgp ipv6 unicast summary' command<br />\n";
415								break;
416				case "show ipv6 route":		if (($flags & $flag_allow_show_ipv6_route) == 0)
417									$error .= "The selected router ($server) does not support the 'show ipv6 route' command<br />\n";
418								break;
419				case "show bgp ipv6 unicast regexp":	if (($flags & $flag_allow_show_bgp_ipv6_regexp) == 0)
420									$error .= "The selected router ($server) does not support the 'show bgp ipv6 unicast regexp' command<br />\n";
421								break;
422				default:			break;
423			}
424		}
425		$i++;
426	}
427
428	if ($found_router==0)
429		$error .= "The router requested ($server) was not found in my configuration file!<br />\n";
430
431	/* main program below */
432	$args = ValidateIP($args);
433	$cache_asnums = Array();
434	$cache_community = Array();
435
436	/* output routers here */
437	$klg_router_list = "<select name=\"server\">\n";
438	$i = 0;
439	while (isset($routers[$i]) && ($routers[$i]["id"] != ""))
440	{
441		$klg_router_list .= "<option value=\"" . $routers[$i]["id"] . "\"";
442		if ($server==$routers[$i]["id"])
443			$klg_router_list .= " selected=\"selected\"";
444		$klg_router_list .= ">" . $routers[$i]["name"] . "</option>\n";
445		$i++;
446	}
447	$klg_router_list .= "</select>";
448
449	$klg_action_list = "<select name=\"action\">\n";
450
451	/* check if each command is allowed */
452	if ($config["allow_ipv4_commands"] == "1")
453	{
454		$klg_action_list .= "<option class=\"box1\" value=\"\"";
455		if ($action == "")
456			$klg_action_list .= " selected=\"selected\"";
457		$klg_action_list .= ">-- IPv4 --</option>\n";
458
459		if ($config["allow_show_ip_bgp"] == "1")
460		{
461			$klg_action_list .= "<option class=\"box1\" value=\"show ip bgp\"";
462			if ($action == "show ip bgp")
463				$klg_action_list .= " selected=\"selected\"";
464			$klg_action_list .= ">show ip bgp &lt;ip&gt;</option>\n";
465		}
466
467		if ($config["allow_show_ip_bgp_summary"] == "1")
468		{
469			$klg_action_list .= "<option class=\"box1\" value=\"show ip bgp summary\"";
470			if ($action == "show ip bgp summary")
471				$klg_action_list .= " selected=\"selected\"";
472			$klg_action_list .= ">show ip bgp summary</option>\n";
473		}
474
475		if ($config["allow_show_ip_route"] == "1")
476		{
477			$klg_action_list .= "<option class=\"box1\" value=\"show ip route\"";
478			if ($action == "show ip route")
479				$klg_action_list .= " selected=\"selected\"";
480			$klg_action_list .= ">show ip route &lt;ip&gt;</option>\n";
481		}
482
483		if ($config["allow_show_ip_bgp_regexp"] == "1")
484		{
485			$klg_action_list .= "<option class=\"box1\" value=\"show ip bgp regexp\"";
486			if ($action == "show ip bgp regexp")
487				$klg_action_list .= " selected=\"selected\"";
488			$klg_action_list .= ">show ip bgp regexp &lt;expression&gt;</option>\n";
489		}
490
491		if ($config["allow_dampening"] == "1")
492		{
493			$klg_action_list .= "<option class=\"box1\" value=\"show ip bgp dampening dampened-paths\"";
494			if ($action == "show ip bgp dampening dampened-paths")
495				$klg_action_list .= " selected=\"selected\"";
496			$klg_action_list .= ">show ip bgp dampening dampened-paths</option>\n";
497
498			$klg_action_list .= "<option class=\"box1\" value=\"show ip bgp dampening flap-statistics\"";
499			if ($action == "show ip bgp dampening flap-statistics")
500				$klg_action_list .= " selected=\"selected\"";
501			$klg_action_list .= ">show ip bgp dampening flap-statistics</option>\n";
502		}
503	}
504
505	if ($config["allow_ipv6_commands"] == "1")
506	{
507		$klg_action_list .= "<option class=\"box2\" value=\"\"";
508		if ($action == "")
509			$klg_action_list .= " selected=\"selected\"";
510		$klg_action_list .= ">-- IPv6 --</option>\n";
511
512		if ($config["allow_show_bgp_ipv6"] == "1")
513		{
514			$klg_action_list .= "<option class=\"box2\" value=\"show bgp ipv6 unicast\"";
515			if ($action == "show bgp ipv6 unicast")
516				$klg_action_list .= " selected=\"selected\"";
517			$klg_action_list .= ">show bgp ipv6 unicast &lt;ip/prefixlen&gt;</option>\n";
518		}
519
520		if ($config["allow_show_bgp_summary"] == "1")
521		{
522			$klg_action_list .= "<option class=\"box2\" value=\"show bgp ipv6 unicast summary\"";
523			if ($action == "show bgp ipv6 unicast summary")
524				$klg_action_list .= " selected=\"selected\"";
525			$klg_action_list .= ">show bgp ipv6 unicast summary</option>\n";
526		}
527
528		if ($config["allow_show_ipv6_route"] == "1")
529		{
530			$klg_action_list .= "<option class=\"box2\" value=\"show ipv6 route\"";
531			if ($action=="show ipv6 route")
532				$klg_action_list .= " selected=\"selected\"";
533			$klg_action_list .= ">show ipv6 route &lt;ip/prefixlen&gt;</option>\n";
534		}
535
536		if ($config["allow_show_bgp_ipv6_regexp"] == "1")
537		{
538			$klg_action_list .= "<option class=\"box2\" value=\"show bgp ipv6 unicast regexp\"";
539			if ($action == "show bgp ipv6 unicast regexp")
540				$klg_action_list .= " selected=\"selected\"";
541			$klg_action_list .= ">show bgp ipv6 unicast regexp &lt;expression&gt;</option>\n";
542		}
543	}
544
545	if ($enable_juniper_commands==1)
546	{
547		$klg_action_list .= "<option class=\"box3\" value=\"\"";
548		if ($action == "")
549			$klg_action_list .= " selected=\"selected\"";
550		$klg_action_list .= ">-- Juniper commands --</option>\n";
551
552		$klg_action_list .= "<option class=\"box3\" value=\"show route\"";
553		if ($action == "show route")
554			$klg_action_list .= " selected=\"selected\"";
555		$klg_action_list .= ">show route &lt;ip/prefix&gt; detail</option>\n";
556	}
557
558	if (($config["allow_ping"] == "1") || ($config["allow_show_interface"] == "1") ||
559		($config["allow_traceroute"] == "1"))
560	{
561		$klg_action_list .= "<option class=\"box3\" value=\"\"";
562		if ($action == "")
563			$klg_action_list .= " selected=\"selected\"";
564		$klg_action_list .= ">-- Non-specific --</option>\n";
565	}
566
567	if ($config["allow_ping"] == "1")
568	{
569		$klg_action_list .= "<option class=\"box3\" value=\"ping\"";
570		if ($action == "ping")
571			$klg_action_list .= " selected=\"selected\"";
572		$klg_action_list .= ">ping &lt;ip&gt;</option>\n";
573	}
574
575	if ($config["allow_traceroute"] == "1")
576	{
577		$klg_action_list .= "<option class=\"box3\" value=\"traceroute\"";
578		if ($action == "traceroute")
579			$klg_action_list .= " selected=\"selected\"";
580		$klg_action_list .= ">traceroute &lt;ip&gt;</option>\n";
581	}
582
583	if ($config["allow_show_interface"] == "1")
584	{
585		$klg_action_list .= "<option class=\"box3\" value=\"show interface\"";
586		if ($action == "show interface")
587			$klg_action_list .= " selected=\"selected\"";
588		$klg_action_list .= ">show interface</option>\n";
589	}
590
591	if ($config["allow_environmental"] == "1")
592	{
593		$klg_action_list .= "<option class=\"box3\" value=\"show environment all\"";
594		if ($action == "show environment all")
595			$klg_action_list .= " selected=\"selected\"";
596		$klg_action_list .= ">show environment all</option>\n";
597	}
598	$klg_action_list .= "</select>";
599	$klg_args = "<input type=\"text\" name=\"args\" value=\"$args\" />";
600	$klg_submit_button = "<input type=\"submit\" value=\"Query\" />";
601	$klg_form_start = "<form action=\"" . $_SERVER["PHP_SELF"] . "\" method=\"" . $config["default_form_method"] . "\">\n";
602	$klg_form_end = "</form>";
603	$klg_output_footer = "<br /><span style=\"font-size: 1;\">";
604        $klg_output_footer .= "// \$Id: klg.php,v 1.23 2015/10/25 18:57:51 danielaustin Exp $ //<br />\n";
605	$klg_output_footer .= "Source code available at: <a href=\"http://sourceforge.net/projects/klg/\">";
606	$klg_output_footer .= "http://sourceforge.net/projects/klg/</a> - Author: ";
607	$klg_output_footer .= "<a href=\"https://www.dan.me.uk/\">Daniel Austin MBCS</a></span><br /><br />\n";
608
609	/* check if we need to lookup */
610	if ($config["log_queries"] == "1")
611	{
612	        $logfd = @fopen($config["log_file"],"a");
613		if (!$logfd)
614		{
615			/* can't open log file for writing */
616			$error .= "Unable to open logfile for writing (check permissions!)<br />\n";
617		}
618	        $datestamp = date("d-m-Y H:i:s");
619	        @fwrite($logfd, "$datestamp | ");
620	        @fwrite($logfd, $_SERVER["REMOTE_ADDR"]);
621	        @fwrite($logfd, " | $server | $action $args\n");
622	        @fclose($logfd);
623	}
624
625	/* ok, do variable replacements */
626	$output = $klg_template;
627	$output = str_replace("##KLG_TITLE##",$config["page_title"],$output);
628	$output = str_replace("##KLG_FORM_START##",$klg_form_start,$output);
629	$output = str_replace("##KLG_FORM_END##",$klg_form_end,$output);
630	$output = str_replace("##KLG_ROUTER_LIST##",$klg_router_list,$output);
631	$output = str_replace("##KLG_ACTION_LIST##",$klg_action_list,$output);
632	$output = str_replace("##KLG_ARGS##",$klg_args,$output);
633	$output = str_replace("##KLG_SUBMIT_BUTTON##",$klg_submit_button,$output);
634
635	$klg_output = "";
636
637	/* if we have any errors, display them NOW before processing any requests */
638	if ($error != "")
639	{
640		$klg_output .= "Sorry! An error has occured (see below)<br /><br />\n";
641		$klg_output .= "<span style=\"color: red;\">\n";
642		$klg_output .= $error;
643		$klg_output .= "</span>\n";
644		$klg_output .= $klg_output_footer;
645		$output = str_replace("##KLG_OUTPUT##",$klg_output,$output);
646		echo $output;
647		die;
648	}
649
650	switch ($lg_type) {
651		case "juniper":		/* juniper routers */
652					if ($action=="show bgp ipv6 unicast summary" || $action=="traceroute")
653						KLG_Router_Generic_Command("$action $args");
654					if ($action=="show route")
655						KLG_Router_Juniper_BGP_Formatted_Command("$action $args detail");
656					break;
657		case "cisco":
658		case "zebra":
659		case "quagga":		/* cisco/zebra/quagga routers */
660					if ($action=="show ip bgp summary" || $action=="show bgp ipv6 unicast summary" ||
661						(($action=="show ip bgp regexp" || $action=="show bgp ipv6 unicast regexp") && ($args!="")))
662					{
663						$cmd = $action;
664						if ($action=="show ip bgp regexp" || $action=="show bgp ipv6 unicast regexp")
665							$cmd .= " $args";
666
667						KLG_Router_Generic_BGP_Command($cmd);
668					}
669
670					if ($action=="show interface" || $action=="show environment all" ||
671						$action=="show ip bgp dampening dampened-paths" ||
672						$action=="show ip bgp dampening flap-statistics" ||
673						(($action=="show ip route" || $action=="show ipv6 route" ||
674							$action=="ping" || $action=="traceroute") && $args!="") )
675					{
676						$cmd = $action;
677						if ($action=="show ip route" || $action=="show ipv6 route" ||
678							$action=="ping" || $action=="traceroute")
679							$cmd .= " $args";
680
681						KLG_Router_Generic_Command($cmd);
682					}
683
684					if (($action=="show ip bgp" || $action=="show bgp ipv6 unicast") && ($args!=""))
685					{
686						KLG_Router_BGP_Formatted_Command("$action $args");
687					}
688					break;
689		default:		/* unknown router type */
690					break;
691	}
692
693	/* add footer to output and display */
694	$klg_output .= $klg_output_footer;
695	$output = str_replace("##KLG_OUTPUT##",$klg_output,$output);
696	echo $output;
697?>
698