Location: PHPKode > projects > Pothos > pothos-0.1.1/includes/functions.inc
<?
///////////////////////////////////////////////////////////////////////////////////////
// Pothos - IPTables Firewall Tool
// Copyright (c) 2004, Ken Stanley
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//  - Redistributions of source code must retain the above copyright notice, this list
//    of conditions and the following disclaimer.
//  - Redistributions in binary form must reproduce the above copyright notice, this
//    list of conditions and the following disclaimer in the documentation and/or other
//    materials provided with the distribution.
//  - Neither the name of the Author nor the names of its contributors may be
//    used to endorse or promote products derived from this software without specific
//    prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// Contact author : hide@address.com
///////////////////////////////////////////////////////////////////////////////////////
function getport($protocol, $port) {
	switch ($protocol) {
		case "tcp":
			return $proto['tcp'] = "$port";
			break;
		case "udp":
			return $proto['udp'] = "$port";
			break;
		default:
			return NULL;
	}
}

function insert_module($chandle) {
	echo "<table border=0 cellpadding=0 cellspacing=0 width=100%>\n";
	echo " <tr>\n";
	if ($dh = opendir("modules/")) {
		while (($module = readdir($dh)) !== false) {
			if (substr($module, -4) == ".mod") {
				echo "   <td>\n";
				include('modules/' . $module);
				echo "   </td>\n";
			}
		}
	}
	echo " </tr>\n";
	echo "</table>\n";
}

function create_table($width, $path, $header, $body) {
	echo "<table border=0 cellpadding=0 cellspacing=1 width=" . $width . ">\n";
	echo " <tr>\n";
	echo "  <td background=\"" . $path . "/images/spacer.gif\">\n";
	echo "   <table border=0 cellpadding=0 cellspacing=0 width=100%>\n";
	echo "    <tr>\n";
	echo "     <td width=12 height=25 background=\"" . $path . "/images/spacer.gif\">";
	echo "<img src=\"" . $path . "/images/012004021700.gif\" width=12 height=25></td>\n";
	echo "     <td width=25 height=25 background=\"" . $path . "/images/spacer.gif\">";
	echo "<img src=\"" . $path . "/images/022004021700.gif\" width=25 height=25></td>\n";
	echo "     <td background=\"" . $path . "/images/032004021700.gif\">\n";
	echo "      <table border=0 cellpadding=0 cellspacing=0 width=100%>\n";
	echo "       <tr>\n";
	echo "        <td class=\"banner_text\" background=\"" . $path . "/images/spacer.gif\">";
	echo $header . "</td>\n";
	echo "        <td width=9 valign=\"bottom\" background=\"" . $path;
	echo "/images/spacer.gif\">";
	echo "<img src=\"" . $path . "/images/042004021700.gif\"></td>\n";
	echo "       </tr>\n";
	echo "      </table>\n";
	echo "     </td>\n";
	echo "     <td width=7 height=25 background=\"" . $path . "/images/spacer.gif\">";
	echo "<img src=\"" . $path . "/images/052004021700.gif\" width=7 height=25></td>\n";
	echo "    </tr>\n";
	echo "   </table>\n";
	echo "  </td>\n";
	echo " </tr>\n";
	echo " <tr>\n";
	echo "  <td>\n";
	echo "   <table border=0 cellpadding=0 cellspacing=0 width=100%>\n";
	echo "    <tr>\n";
	echo "     <td height=39 background=\"" . $path . "/images/spacer.gif\">";
	echo "<img src=\"" . $path . "/images/062004021700.gif\" width=12 height=39></td>\n";
	echo "     <td background=\"" . $path . "/images/072004021700.gif\" width=25 height=39>";
	echo "<img src=\"" . $path . "/images/082004021700.gif\" width=25 height=39></td>\n";
	echo "     <td background=\"" . $path . "/images/072004021700.gif\" height=39>\n";
	echo "      <table border=0 cellpadding=0 cellspacing=0 width=100%>\n";
	echo "       <tr>\n";
	echo "        <td class=\"banner_text\" background=\"" . $path . "/images/spacer.gif\">";
	echo "<img src=\"" . $path . "/images/spacer.gif\" width=1 height=1></td>\n";
	echo "        <td width=9 valign=\"top\" background=\"" . $path . "/images/spacer.gif\">";
	echo "<img src=\"" . $path . "/images/spacer.gif\" width=1 height=1></td>\n";
	echo "       </tr>\n";
	echo "      </table>\n";
	echo "     </td>\n";
	echo "     <td align=\"right\" height=39 background=\"" . $path . "/images/072004021700.gif\"";
	//echo " valign=\"top\" class=\"rivet\"><img src=\"" . $path . "/images/042004021700.gif\">";
	echo " valign=\"top\" class=\"rivet\"><img src=\"" . $path . "/images/spacer.gif\">";
	echo "</td>\n";
	echo "     <td align=\"right\" height=39 background=\"" . $path . "/images/spacer.gif\">";
	echo "<img src=\"" . $path . "/images/092004021700.gif\">";
	echo "<img src=\"" . $path . "/images/102004021700.gif\" width=7 height=39></td>\n";
	echo "    </tr>\n";
	echo "    <tr>\n";
	echo "     <td width=12 background=\"" . $path . "/images/112004021700.gif\">";
	echo "<img src=\"" . $path . "/images/spacer.gif\" width=1 height=1></td>\n";
	echo "     <td bgcolor=\"#CCCCCC\" colspan=3 background=\"" . $path;
	echo "/images/spacer.gif\">\n";
	echo "      <!-- // Begin the main section -->\n";
	echo "$body\n";
	echo "      <!-- // End the main section -->\n";
	echo "     </td>\n";
	echo "     <td background=\"" . $path . "/images/122004021700.gif\" width=16>";
	echo "<img src=\"" . $path . "/images/spacer.gif\" width=1 height=1></td>\n";
	echo "    </tr>\n";
	echo "    <tr>\n";
	echo "     <td height=12 background=\"" . $path . "/images/spacer.gif\">";
	echo "<img src=\"" . $path . "/images/132004021700.gif\" width=12 height=12></td>\n";
	echo "     <td background=\"" . $path . "/images/142004021700.gif\" colspan=3>";
	echo "<img src=\"" . $path . "/images/spacer.gif\" width=1 height=1></td>\n";
	echo "     <td align=\"right\" height=12 background=\"" . $path;
	echo "/images/spacer.gif\">";
	echo "<img src=\"" . $path . "/images/152004021700.gif\" width=16 height=12></td>\n";
	echo "    </tr>\n";
	echo "   </table>\n";
	echo "  </td>\n";
	echo " </tr>\n";
	echo "</table>\n";
}

function create_labels($fields, $protocol) {
		// $type = tcp|udp - This is for the ports.
		// We always have the ID field.
                $labels = "  <td class=\"header\" valign=\"bottom\"><a href=\"?sort_by=id\"";
                $labels .= " title=\"Sort by ID\">ID</a></td>\n";

		foreach ($fields as $field) {
			// This is ugly, but a necessary evil.
			// TODO: Find a better way of doing this with less code.
			switch ($field) {
				case "raw_mac":
					$label = "Mac";
					break;
				case "oob_time_sec":
				case "oob_time_usec":
					$label = "Date";
					break;
				case "oob_prefix":
					$label = "Chain";
					break;
				case "oob_mark":
					$label = "Mark";
					break;
				case "oob_in":
					$label = "In";
					break;
				case "oob_out":
					$label = "Out";
					break;
				case "ip_saddr":
					$label = "Source Address";
					break;
				case "ip_daddr":
					$label = "Destination Address";
					break;
				case "ip_protocol":
					$label = "Proto";
					break;
				case "ip_tos":
					$label = "Type of Service";
					break;
				case "ip_ttl":
					$label = "Time To Live";
					break;
				case "ip_totlen":
					$label = "Total Length";
					break;
				case "ip_csum":
					$label = "IP Checksum";
					break;
				case "udp_sport":
					if ($protocol == "udp")
						$label = "S. Port";
					else
						unset($label);
					break;
				case "tcp_sport":
					if ($protocol == "tcp")
						$label = "S. Port";
					else
						unset($label);
					break;
				case "udp_dport":
					if ($protocol == "udp")
						$label = "S. Port";
					else
						unset($label);
					break;
				case "tcp_dport":
					if ($protocol == "tcp")
						$label = "D. Port";
					else
						unset($label);
					break;
				case "udp_len":
					$label = "UDP Length";
					break;
				case "icmp_type":
					$label = "ICMP Type";
					break;
				// FIXME: Find out what each of the following are,
				// FIXME: and what labels they should have.
				case "ip_ihl":
				case "ip_fragoff":
				case "ip_id":
				case "tcp_seq":
				case "tcp_ackseq":
				case "tcp_window":
				case "tcp_urg":
				case "tcp_urgp":
				case "tcp_ack":
				case "tcp_psh":
				case "tcp_rst":
				case "tcp_syn":
				case "tcp_fin":
				case "icmp_code":
				case "icmp_echoid":
				case "icmp_echoseq":
				case "icmp_gateway":
				case "icmp_fragmtu":
				case "pwsniff_user":
				case "pwsniff_pass":
				case "ahesp_spi":
				default:
					// Give Unknown label for fields we aren't
					// aware of, or mispellings by the user.
					$label = "Unknown";
					break;
			}
			if ($protocol == "tcp" && ($key <> "udp_sport" || $key <> "udp_dport")) {
				$labels .= "  <td class=\"header\" valign=\"bottom\"><a href=\"?sort_by=$field\"";
				$labels .= " title=\"Sort by $label\">$label</a></td>\n";
			}
			if ($protocol == "udp" && ($key <> "tcp_sport" || $key <> "tcp_dport")) {
				$labels .= "  <td class=\"header\" valign=\"bottom\"><a href=\"?sort_by=$field\"";
				$labels .= " title=\"Sort by $label\">$label</a></td>\n";
			}

		}

		return $labels;
}
function dberr($path) {
	create_table("100%",$path,"Database Error",mysql_error());
}

function compile_data($record,$preferences) {
	$results = "";
	foreach ($record as $key => $var) {
		switch ($key) {
			case "id":
				// Record ID
				$title_label = "ID";
				$data = $var;
				break;
			case "oob_prefix":
				// Chain (ex. Drop or Accept)
				$title_label = "Chain";
				$data = $var;
				break;
			case "oob_time_sec":
				// Date/Timestamp
				$result_date = date("Y-m-d H:i:s", $var);
				$title_label = "Date";
				$data = $result_date;
				break;
			case "ip_protocol":
				// Protocol
				$protocol = getprotobynumber($var);
				$title_label = "Protocol";
				$data = $protocol;
				break;
			case "ip_daddr":
				$title_label = "Destination Address";
			case "ip_saddr":
				if (empty($title_label))
					$title_label = "Source Address";
				// Source Address
				if ($preferences['resolve'] == "true")
					$ip_addr = gethostbyaddr(long2ip($var));
				else
					$ip_addr = long2ip($var);
	
				if ((strlen($ip_addr) > $preferences['trunicate'])
					&& ($preferences['trunicate']) > 0)
						$ip_addr = substr(
							$ip_addr,
							0,
							$preferences['trunicate']) . "...";
	
					$data = $ip_addr;
				break;
			case "tcp_sport":
				// Source Port (TCP)
				if ($var <> NULL) {
					$title_label = "Source Port";
					$data = getport($protocol, $var);
				}
				break;
			case "tcp_dport":
				// Destination Port (TCP)
				if ($var <> NULL) {
					$title_label = "Destination Port";
					$data = getport($protocol, $var);
				}
				break;
			case "udp_sport":
				// Source Port (UDP)
				if ($var <> NULL) {
					$title_label = "Source Port";
					$data = getport($protocol, $var);
				}
				break;
			case "udp_dport":
				// Destination Port (UDP)
				if ($var <> NULL) {
					$title_label = "Destination Port";
					$data = getport($protocol, $var);
				}
				break;
			case "oob_in":
				$title_label = "In Interface";
				$data = $var;
				break;
			case "oob_out":
				$title_label = "Out Interface";
				$data = $var;
				break;
			default:
				// Everything else.
				$title_label = "Unknown";
				$data = $var;
		}
		if ($data <> NULL) {
			if (empty($_GET['start']) || $start < 0)
				$start = 0;
			else
				$start = $_GET['start'];

			if (!empty($_GET['limit']))
				$limit = $_GET['limit'];

			$results .= "<td class=\"value\" valign=\"bottom\">";

			if ($key == "id") {
				$page = "fullrecord.php";
				$title = "Retrieve Full Record";
			}
			else {
				$page = "filter.php";
				$title = "Filter by $title_label";
			}

			$results .= "<a href=\"$page?start=$start&limit=$limit&field=$key&val=$var\"";
			$results .= " title=\"$title\">";
			$results .= "$data</td>\n";
		}
	
		unset($title_label);
		unset($data);
	}

	return $results;
}
?>
Return current item: Pothos