Project

General

Profile

« Previous | Next » 

Revision 60120e37

Added by Ermal Luçi over 16 years ago

  • Convert schedules to pf(4).
    This allows to schedule the whole feature of the rules like queues/limiters/gateways/blocks/allows/etc...
  • Whitespace cleaning on filter.inc
  • Move schedule backend logic from pfsense-utils.inc to filter.inc and prefix with filter_.
  • Small bugfixes here and there.

View differences:

etc/inc/filter.inc
265 265
		update_filter_reload_status("Plugins completed.");
266 266
	}
267 267
	/* if time based rules are enabled then swap in the set */
268
	if($time_based_rules == true) {
269
		tdr_install_cron(true);
270
		tdr_install_set();
271
	} else {
272
		tdr_install_cron(false);
273
	}
268
	if ($time_based_rules == true)
269
		filter_tdr_install_cron(true);
270
	else
271
		filter_tdr_install_cron(false);
272

  
274 273
	/*
275 274
	 *	we need a way to let a user run a shell cmd after each
276 275
	 *	filter_configure() call.  run this xml command after
......
278 277
	 */
279 278
	if($config['system']['afterfilterchangeshellcmd'] <> "")
280 279
		mwexec($config['system']['afterfilterchangeshellcmd']);
280

  
281 281
	/* sync carp entries to other firewalls */
282 282
	update_filter_reload_status("Syncing CARP data");
283 283
	carp_sync_client();
284

  
284 285
	if ($g['booting'] == true)
285
				echo ".";
286
		echo ".";
286 287
	system_routing_configure();
288

  
287 289
	find_dns_aliases();
290

  
288 291
	update_filter_reload_status("Done");
289 292
	if ($g['booting'] == true)
290 293
		echo "done.\n";
294

  
291 295
	return 0;
292 296
}
293 297

  
......
1224 1228
		/* we're not using load balancing, just setup gateway */
1225 1229
		else if($foundlb == 0) {
1226 1230
			$gateway = $rule['gateway'];
1227
			if(!is_ipaddr($gateway)) {
1231
			if (!is_ipaddr($gateway)) {
1228 1232
				$gwip = $GatewaysList[$gateway]['gateway'];
1229 1233
				if ($GatewaysList[$gateway]['interface'])
1230 1234
					$int = $GatewaysList[$gateway]['interface'];
......
1242 1246
	}
1243 1247

  
1244 1248
	if (isset($rule['protocol'])) {
1245
		if($rule['protocol'] == "tcp/udp")
1249
		if ($rule['protocol'] == "tcp/udp")
1246 1250
			$aline['prot'] = " proto { tcp udp } ";
1247
		elseif($rule['protocol'] == "icmp")
1251
		elseif ($rule['protocol'] == "icmp")
1248 1252
			$aline['prot'] = " inet proto icmp ";
1249 1253
		else
1250 1254
			$aline['prot'] = " proto {$rule['protocol']} ";
1251 1255
	} else {
1252
		if($rule['source']['port'] <> "" || $rule['destination']['port'] <> "")
1256
		if ($rule['source']['port'] <> "" || $rule['destination']['port'] <> "")
1253 1257
			$aline['prot'] = " proto tcp ";
1254 1258
	}
1255 1259
	update_filter_reload_status("Creating rule {$rule['descr']}");
......
1420 1424
		else
1421 1425
			$dst = "{ {$not} {$expdst} }";
1422 1426
	}
1423
	if (!$dst || ($dst == "/")) {
1427
	if (!$dst || ($dst == "/"))
1424 1428
		return "# returning at dst $dst == \"/\"";
1425
	}
1429

  
1426 1430
	$aline['dst'] = "to $dst ";
1427 1431
	if (in_array($rule['protocol'], array("tcp","udp","tcp/udp"))) {
1428 1432
		if ($rule['destination']['port']) {
......
1458 1462
		$l7_structures = $l7rule->get_unique_structures();
1459 1463
		$aline['divert'] = "divert " . $l7rule->GetRPort() . " ";
1460 1464
	}
1461
	if (($rule['protocol'] == "icmp") && $rule['icmptype']) {
1465
	if (($rule['protocol'] == "icmp") && $rule['icmptype'])
1462 1466
		$aline['icmp-type'] = "icmp-type {$rule['icmptype']} ";
1463
	}
1464 1467
	if (!empty($rule['tag']))
1465 1468
		$aline['tag'] = " tag " .$rule['tag']. " ";
1466 1469
	if (!empty($rule['tagged']))
......
1474 1477
			if($rule['protocol'] == "tcp")
1475 1478
				$aline['flags'] = "flags S/SA ";
1476 1479
		/*
1477
			# keep state
1478
				works with TCP, UDP, and ICMP.
1479
			# modulate state
1480
				works only with TCP. pfSense will generate strong Initial Sequence Numbers (ISNs)
1481
				for packets matching this rule.
1482
			# synproxy state
1483
				proxies incoming TCP connections to help protect servers from spoofed TCP SYN floods.
1484
				This option includes the functionality of keep state and modulate state combined.
1485
			# none
1486
				do not use state mechanisms to keep track. this is only useful if your doing advanced
1487
				queueing in certain situations. please check the faq.
1488
		*/
1480
		 *	# keep state
1481
		 *		works with TCP, UDP, and ICMP.
1482
		 *	# modulate state
1483
		 *		works only with TCP. pfSense will generate strong Initial Sequence Numbers (ISNs)
1484
		 *		for packets matching this rule.
1485
		 *	# synproxy state
1486
		 *		proxies incoming TCP connections to help protect servers from spoofed TCP SYN floods.
1487
		 *		This option includes the functionality of keep state and modulate state combined.
1488
		 *	# none
1489
		 *		do not use state mechanisms to keep track. this is only useful if your doing advanced
1490
		 *		queueing in certain situations. please check the faq.
1491
		 */
1489 1492
		$noadvoptions = false;
1490 1493
		if (isset($rule['statetype']) && $rule['statetype'] <> "") {
1491 1494
			switch($rule['statetype']) {
......
1501 1504
				default:
1502 1505
					$aline['flags'] = "{$rule['statetype']} ";
1503 1506
			}
1504
		} else {
1507
		} else
1505 1508
			$aline['flags'] = "keep state ";
1506
		}
1507
		if($noadvoptions == false || $l7_present) 
1509

  
1510
		if ($noadvoptions == false || $l7_present)
1508 1511
			if( isset($rule['source-track']) and $rule['source-track'] <> "" or
1509
			  isset($rule['max-src-nodes']) and $rule['max-src-nodes'] <> "" or
1510
			   isset($rule['max-src-conn-rate']) and $rule['max-src-conn-rate'] <> "" or
1512
				isset($rule['max-src-nodes']) and $rule['max-src-nodes'] <> "" or
1513
				isset($rule['max-src-conn-rate']) and $rule['max-src-conn-rate'] <> "" or
1511 1514
				isset($rule['max-src-conn-rates']) and $rule['max-src-conn-rates'] <> "" or
1512
				 isset($rule['max-src-states']) and $rule['max-src-states'] <> "" or
1513
				  isset($rule['statetimeout']) and $rule['statetimeout'] <> "" or
1514
				   isset($rule['l7container']) and $rule['l7container'] != "none") {
1515
				isset($rule['max-src-states']) and $rule['max-src-states'] <> "" or
1516
				isset($rule['statetimeout']) and $rule['statetimeout'] <> "" or
1517
				isset($rule['l7container']) and $rule['l7container'] != "none") {
1515 1518
					$aline['flags'] .= "( ";
1516 1519
					if(isset($rule['source-track']) and $rule['source-track'] <> "")
1517 1520
						$aline['flags'] .= "source-track rule ";
......
1532 1535
					$aline['flags'] .= " ) ";
1533 1536
				}
1534 1537
	}
1535
		if ($type == "reject" && $rule['protocol'] == "tcp") {
1536
			/* special reject packet */
1537
			$aline['flags'] .= "flags S/SA ";
1538
		}
1539
		if ($type == "pass") {
1540
			if ($rule['defaultqueue'] <> "") {
1541
				$aline['queue'] = " queue (".$rule['defaultqueue'];
1542
				if ($rule['ackqueue'] <> "")
1543
					$aline['queue'] .= ",".$rule['ackqueue'];
1544
				$aline['queue'] .= ") "; 
1545
			}
1546
			if ($rule['dnpipe'] <> "") {
1547
				if ($rule['dnpipe'][0] == "?") {
1548
					$aline['dnpipe'] = " dnqueue( ";
1549
					$aline['dnpipe'] .= substr($rule['dnpipe'],1);
1550
					if ($rule['pdnpipe'] <> "")
1551
						$aline['dnpipe'] .= ",".substr($rule['pdnpipe'], 1);
1552
				} else {
1553
					$aline['dnpipe'] = " dnpipe ( " . $rule['dnpipe'];
1554
					if ($rule['pdnpipe'] <> "")
1555
						$aline['dnpipe'] .= ", " . $rule['pdnpipe'];
1556
				}
1557
				$aline['dnpipe'] .= ") ";
1558
			}
1538
	if ($type == "reject" && $rule['protocol'] == "tcp") {
1539
		/* special reject packet */
1540
		$aline['flags'] .= "flags S/SA ";
1541
	}
1542
	if ($type == "pass") {
1543
		if ($rule['defaultqueue'] <> "") {
1544
			$aline['queue'] = " queue (".$rule['defaultqueue'];
1545
			if ($rule['ackqueue'] <> "")
1546
				$aline['queue'] .= ",".$rule['ackqueue'];
1547
			$aline['queue'] .= ") ";
1559 1548
		}
1560
		/* cache entries */
1561
		if (isset($src_table))
1562
			if (isset($table_cache[$src_table])) {
1563
				if ($g['debug'])
1564
					echo "{$src_table} found in cache\n";
1565
			} else {
1566
				if ($g['debug'])
1567
					echo "{$src_table} NOT found in cache...adding\n";
1568
				$table_cache[$src_table] = $src_table_line;
1569
			}
1570
		if (isset($dst_table))
1571
			if (isset($table_cache[$dst_table])) {
1572
				if ($g['debug'])
1573
					echo "{$dst_table} found in cache\n";
1549
		if ($rule['dnpipe'] <> "") {
1550
			if ($rule['dnpipe'][0] == "?") {
1551
				$aline['dnpipe'] = " dnqueue( ";
1552
				$aline['dnpipe'] .= substr($rule['dnpipe'],1);
1553
				if ($rule['pdnpipe'] <> "")
1554
					$aline['dnpipe'] .= ",".substr($rule['pdnpipe'], 1);
1574 1555
			} else {
1575
				if ($g['debug'])
1576
					echo "{$dst_table} NOT found in cache...adding\n";
1577
				$table_cache[$dst_table] = $dst_table_line;
1556
				$aline['dnpipe'] = " dnpipe ( " . $rule['dnpipe'];
1557
				if ($rule['pdnpipe'] <> "")
1558
					$aline['dnpipe'] .= ", " . $rule['pdnpipe'];
1578 1559
			}
1579

  
1580
		/* exception(s) to a user rules can go here. */
1581
		/* rules with a gateway or pool should create another rule for routing to local networks or vpns */
1582
		/* we only trigger this for a rule with the destination of any and without a gateway */
1583
		if (($aline['route'] <> "") && (trim($aline['type']) == "pass") && (trim($dst) == "any")) {
1584
			/* negate VPN/PPTP/PPPoE networks for load balancer/gateway rules */
1585
			$vpns = " to <vpns> ";
1586
			$line .= $aline['type'] . $aline['direction'] . $aline['log'] . $aline['quick'] . $aline['interface'] . $aline['prot'] .
1587
				$aline['src'] . $aline['srcport'] . $aline['os'] . $vpns . $aline['dstport'].
1588
				$aline['icmp-type'] . $aline['tag'] . $aline['tagged'] . $aline['dscp'] . $aline['allowopts'] . $aline['flags'].
1589
				$aline['queue'] . $aline['dnpipe'] . 
1590
				" label \"NEGATE_ROUTE: Negate policy route for local network(s)\"\n";
1591
			/* negate directly connected networks for load balancer/gateway rules */
1592
			$direct_networks = " to <direct_networks> ";
1593
			$line .= $aline['type'] . $aline['direction'] . $aline['log'] . $aline['quick'] . $aline['interface'] . $aline['prot'] .
1594
				$aline['src'] . $aline['srcport'] . $aline['os'] . $direct_networks . $aline['dstport'].
1595
				$aline['icmp-type'] . $aline['tag'] . $aline['tagged'] . $aline['dscp'] . $aline['allowopts'] .
1596
				$aline['flags'] . $aline['queue'] . $aline['dnpipe'] .
1597
				" label \"NEGATE_ROUTE: Negate policy route for local network(s)\"\n";
1560
			$aline['dnpipe'] .= ") ";
1598 1561
		}
1599
		/* piece together the actual user rule */
1600
		$line .= $aline['type'] . $aline['direction'] . $aline['log'] . $aline['quick'] . $aline['interface'] . $aline['reply'] .
1601
			$aline['route'] . $aline['prot'] . $aline['src'] . $aline['srcport'] . $aline['os'] . $aline['dst'] .
1602
			$aline['dstport'] . $aline['divert'] . $aline['icmp-type'] . $aline['tag'] . $aline['tagged'] . $aline['dscp'] . 
1603
			$aline['allowopts'] . $aline['flags'] . $aline['queue'] . $aline['dnpipe'];
1604

  
1605
		/* is a time based rule schedule attached? */
1606
		if(!empty($rule['sched'])) {
1607
			if($config['schedules']) {
1608
				$foundsched = false;
1609
				foreach($config['schedules']['schedule'] as $sched) {
1610
					if($sched['name'] == $rule['sched']) {
1611
						$status = get_time_based_rule_status($schedule_xml_block);
1612
						$foundsched = true;
1613
						break;
1614
					}				
1615
				}
1616
				if ($foundsched == false)
1617
					return $line;
1618
			} else
1619
				return $line;
1620

  
1621
			if($status) {
1622
				if($g['debug'])
1623
					log_error("[TDR DEBUG] status true -- rule type '$type'");
1624
				if($type == "block") {
1625
					// active deny rules should deny
1626
					$ipfw_rule = tdr_create_ipfw_rule($rule, "deny");
1627
					tdr_install_rule($ipfw_rule);
1628
				} else {
1629
					// active allow rules should allow
1630
					$ipfw_rule = tdr_create_ipfw_rule($rule, "allow");
1631
					tdr_install_rule($ipfw_rule);
1632
				}
1633
				return "$line";
1634
			} else {
1635
				/*	 rule is turned off, if type == pass, deny traffic until 
1636
				 *	 active else allow traffic until active 
1637
				 */
1638
				if($type == "pass") {
1639
					// inactive pass rules should deny
1640
					$ipfw_rule = tdr_create_ipfw_rule($rule, "deny");
1641
					tdr_install_rule($ipfw_rule);
1642
				} else {
1643
					// inactive block rules should skipto
1644
					$ipfw_rule = tdr_create_ipfw_rule($rule, "skipto");
1645
					tdr_install_rule($ipfw_rule);			
1646
				}
1647
				return "# $line";
1648
			}
1649
		} 
1562
	}
1563
	/* cache entries */
1564
	if (isset($src_table))
1565
		if (isset($table_cache[$src_table])) {
1566
			if ($g['debug'])
1567
				echo "{$src_table} found in cache\n";
1568
		} else {
1569
			if ($g['debug'])
1570
				echo "{$src_table} NOT found in cache...adding\n";
1571
			$table_cache[$src_table] = $src_table_line;
1572
		}
1573
	if (isset($dst_table))
1574
		if (isset($table_cache[$dst_table])) {
1575
			if ($g['debug'])
1576
				echo "{$dst_table} found in cache\n";
1577
		} else {
1578
			if ($g['debug'])
1579
				echo "{$dst_table} NOT found in cache...adding\n";
1580
			$table_cache[$dst_table] = $dst_table_line;
1581
		}
1582

  
1583
	/* is a time based rule schedule attached? */
1584
        if(!empty($rule['sched']) && !empty($config['schedules'])) {
1585
		$aline['schedlabel'] = "";
1586
                foreach ($config['schedules']['schedule'] as $sched) {
1587
                        if ($sched['name'] == $rule['sched']) {
1588
                                if (!filter_get_time_based_rule_status($sched)) {
1589
                                        mwexec("/sbin/pfctl -y \"{$sched['schedlabel']}\"");
1590
                                        $line = "# schedule finished - {$rule}";
1591
                                } else if ($g['debug'])
1592
                                        log_error("[TDR DEBUG] status true -- rule type '$type'");
1593

  
1594
				$aline['schedlabel'] = " schedule \"{$sched['schedlabel']}\" ";
1595
                                break;
1596
                        }
1597
                }
1598
        }
1650 1599

  
1651
		return $line;			
1600
	/* exception(s) to a user rules can go here. */
1601
	/* rules with a gateway or pool should create another rule for routing to vpns */
1602
	if (($aline['route'] <> "") && (trim($aline['type']) == "pass") && (trim($dst) == "any")) {
1603
		/* negate VPN/PPTP/PPPoE networks for load balancer/gateway rules */
1604
		$vpns = " to <vpns> ";
1605
		$line .= $aline['type'] . $aline['direction'] . $aline['log'] . $aline['quick'] .
1606
			$aline['interface'] . $aline['prot'] . $aline['src'] . $aline['srcport'] .
1607
			$aline['os'] . $vpns . $aline['dstport'] . $aline['icmp-type'] . $aline['tag'] .
1608
			$aline['tagged'] . $aline['dscp'] . $aline['allowopts'] . $aline['flags'] .
1609
			$aline['queue'] . $aline['dnpipe'] . $aline['schedlabel'] .
1610
			" label \"NEGATE_ROUTE: Negate policy route for vpn(s)\"\n";
1611

  
1612
	}
1613
	/* piece together the actual user rule */
1614
	$line .= $aline['type'] . $aline['direction'] . $aline['log'] . $aline['quick'] . $aline['interface'] .
1615
		$aline['reply'] . $aline['route'] . $aline['prot'] . $aline['src'] . $aline['srcport'] .
1616
		$aline['os'] . $aline['dst'] . $aline['dstport'] . $aline['divert'] . $aline['icmp-type'] .
1617
		$aline['tag'] . $aline['tagged'] . $aline['dscp'] .  $aline['allowopts'] . $aline['flags'] .
1618
		$aline['queue'] . $aline['dnpipe'] . $aline['schedlabel'];
1619

  
1620
	return $line;
1652 1621
}
1653 1622

  
1654 1623
function filter_rules_generate() 
......
1699 1668

  
1700 1669
EOD;
1701 1670

  
1702
        if(!isset($config['system']['ipv6allow'])) {
1671
        if (!isset($config['system']['ipv6allow'])) {
1703 1672
                $ipfrules .= "# Block all IPv6\n";
1704 1673
                $ipfrules .= "block in quick inet6 all\n";
1705 1674
                $ipfrules .= "block out quick inet6 all\n";
......
1940 1909
EOD;
1941 1910
	
1942 1911
	if (isset($config['filter']['rule'])) {
1943
		$load_ipfw_module = false;
1944 1912
		/* Pre-cache all our rules so we only have to generate them once */
1945 1913
		$rule_arr1 = array();
1946 1914
		$rule_arr2 = array();
......
1956 1924
					$rule_arr2[] = generate_user_filter_rule_arr($rule);
1957 1925
				}
1958 1926
				if ($rule['sched']) 
1959
					$load_ipfw_module = true;
1927
					$time_based_rules = true;
1960 1928
			}
1961 1929
		}
1962 1930
		$rule_arr = array_merge($rule_arr1,$rule_arr2);
1963 1931
		/* 
1964
		 * check to see if any rules reference a schedule
1965
		 * or if CP is enabled
1932
		 * check to see if CP is enabled
1966 1933
		 * and if so load ipfw for later usage.
1967 1934
		 */
1968 1935
		if (isset($config['captiveportal']['enable']) && isset($config['interfaces'][$config['captiveportal']['interface']]['enable'])) {
1969
			$load_ipfw_module = true;
1970
		}
1971

  
1972
		if ($load_ipfw_module == true) {
1973 1936
			filter_load_ipfw();
1974
			$time_based_rules = true;
1975
			exec("/sbin/ipfw delete set 9");
1976
			exec("/sbin/ipfw delete 2");
1977
			exec("/sbin/ipfw delete 3");
1937
			mwexec("/sbin/ipfw delete set 3");
1938
			mwexec("/sbin/ipfw delete set 2");
1978 1939
		}
1979 1940
		$ipfrules .= "\n# User-defined aliases follow\n";
1980 1941
		/* tables for aliases */
......
2037 1998
	return $ipfrules;
2038 1999
}
2039 2000

  
2001
/****f* filter/filter_tdr_install_cron
2002
 * NAME
2003
 *   filter_tdr_install_cron
2004
 * INPUTS
2005
 *   $should_install true if the cron entry should be installed, false
2006
 *   if the entry should be removed if it is present
2007
 * RESULT
2008
 *   none
2009
 ******/
2010
function filter_tdr_install_cron($should_install) {
2011
        global $config, $g;
2012

  
2013
        if ($g['booting']==true)
2014
                return;
2015

  
2016
        $is_installed = false;
2017

  
2018
        if(!$config['cron']['item'])
2019
                return;
2020

  
2021
        $x=0;
2022
        foreach($config['cron']['item'] as $item) {
2023
                if (strstr($item['command'], "filter_configure_sync")) {
2024
                        $is_installed = true;
2025
                        break;
2026
                }
2027
                $x++;
2028
        }
2029
        switch($should_install) {
2030
                case true:
2031
                        if(!$is_installed) {
2032
                                $cron_item = array();
2033
                                $cron_item['minute'] = "0,15,30,45";
2034
                                $cron_item['hour'] = "*";
2035
                                $cron_item['mday'] = "*";
2036
                                $cron_item['month'] = "*";
2037
                                $cron_item['wday'] = "*";
2038
                                $cron_item['who'] = "root";
2039
                                $cron_item['command'] = "/etc/rc.filter_configure_sync";
2040
                                $config['cron']['item'][] = $cron_item;
2041
                                write_config("Installed 15 minute filter reload for Time Based Rules");
2042
                                configure_cron();
2043
                        }
2044
                break;
2045
                case false:
2046
                        if($is_installed == true) {
2047
                                if($x > 0) {
2048
                                        unset($config['cron']['item'][$x]);
2049
                                        write_config();
2050
                                }
2051
                                configure_cron();
2052
                        }
2053
                break;
2054
        }
2055
}
2056

  
2057
/****f* filter/filter_get_time_based_rule_status
2058
 * NAME
2059
 *   filter_get_time_based_rule_status
2060
 * INPUTS
2061
 *   xml schedule block
2062
 * RESULT
2063
 *   true/false - true if the rule should be installed
2064
 ******/
2065
/*
2066
 <schedules>
2067
   <schedule>
2068
     <name>ScheduleMultipleTime</name>
2069
     <descr>main descr</descr>
2070
     <time>
2071
       <position>0,1,2</position>
2072
       <hour>0:0-24:0</hour>
2073
       <desc>time range 2</desc>
2074
     </time>
2075
     <time>
2076
       <position>4,5,6</position>
2077
       <hour>0:0-24:0</hour>
2078
       <desc>time range 1</desc>
2079
     </time>
2080
   </schedule>
2081
 </schedules>
2082
*/
2083
function filter_get_time_based_rule_status($schedule) {
2084
        $should_add_rule = false;
2085
        /* no schedule? rule should be installed */
2086
        if (empty($schedule))
2087
                return true;
2088
        /*
2089
         * iterate through time blocks and deterimine
2090
         * if the rule should be installed or not.
2091
         */
2092
        foreach($schedule['timerange'] as $timeday) {
2093
                if($timeday['month'])
2094
                        $month = $timeday['month'];
2095
                else
2096
                        $week = "";
2097
                if($timeday['day'])
2098
                        $day = $timeday['day'];
2099
                else
2100
                        $day = "";
2101
                if($timeday['hour'])
2102
                        $hour = $timeday['hour'];
2103
                else
2104
                        $hour = "";
2105
                if($timeday['position'])
2106
                        $position = $timeday['position'];
2107
                else
2108
                        $position = "";
2109
                if($timeday['desc'])
2110
                        $desc = $timeday['desc'];
2111
                else
2112
                        $desc = "";
2113
                if($month) {
2114
                        $monthstatus = filter_tdr_month($month);
2115
                } else {
2116
                        $monthstatus = true;
2117
                }
2118
                if($day) {
2119
                        $daystatus = filter_tdr_day($day);
2120
                } else {
2121
                        $daystatus = true;
2122
                }
2123
                if($hour) {
2124
                        $hourstatus = filter_tdr_hour($hour);
2125
                } else {
2126
                        $hourstatus = true;
2127
                }
2128
                if($position) {
2129
                        $positionstatus = filter_tdr_position($position);
2130
                } else {
2131
                        $positionstatus = true;
2132
                }
2133

  
2134
                if ($monthstatus == true && $daystatus == true && $positionstatus == true && $hourstatus == true)
2135
			$should_add_rule = true;
2136
        }
2137

  
2138
        return $should_add_rule;
2139
}
2140

  
2141
function filter_tdr_day($schedule) {
2142
        /*
2143
         * Calculate day of month.
2144
         * IE: 29th of may
2145
         */
2146
        $weekday        = date("w");
2147
        if ($weekday == 0)
2148
                $weekday = 7;
2149
        $date           = date("d");
2150
        $defined_days = split(",", $schedule);
2151
        log_error("[TDR DEBUG] filter_tdr_day($schedule)");
2152
        foreach($defined_days as $dd) {
2153
                if($date == $dd)
2154
                        return true;
2155
        }
2156
        return false;
2157
}
2158
function filter_tdr_hour($schedule) {
2159
        /* $schedule should be a string such as 16:00-19:00 */
2160
        $tmp = split("-", $schedule);
2161
        $starting_time = strtotime($tmp[0]);
2162
        $ending_time = strtotime($tmp[1]);
2163
        $now = strtotime("now");
2164
        log_error("[TDR DEBUG] S: $starting_time E: $ending_time N: $now");
2165
        if ($now >= $starting_time and $now <= $ending_time)
2166
                return true;
2167
        return false;
2168
}
2169

  
2170
function filter_tdr_position($schedule) {
2171
        /*
2172
         * Calculate possition, ie: day of week.
2173
         * Sunday = 7, Monday = 1, Tuesday = 2
2174
         * Weds = 3, Thursday = 4, Friday = 5,
2175
         * Saturday = 6
2176
         * ...
2177
         */
2178
        $weekday = date("w");
2179
        log_error("[TDR DEBUG] filter_tdr_position($schedule) $weekday");
2180
        if ($weekday == 0)
2181
                $weekday = 7;
2182
        $schedule_days = split(",", $schedule);
2183
        foreach($schedule_days as $day) {
2184
                if ($day == $weekday)
2185
                        return true;
2186
        }
2187
        return false;
2188
}
2189

  
2190
function filter_tdr_month($schedule) {
2191
        /*
2192
         * Calculate month
2193
         */
2194
        $todays_month = date("n");
2195
        $months = split(",", $schedule);
2196
        log_error("[TDR DEBUG] filter_tdr_month($schedule)");
2197
        foreach($months as $month) {
2198
                if ($month == $todays_month)
2199
                        return true;
2200
        }
2201
        return false;
2202
}
2203

  
2040 2204
function setup_logging_interfaces() 
2041 2205
{
2042 2206
	global $config;
......
2051 2215
	foreach ($ifdescrs as $ifdescr => $ifname) {
2052 2216
		/* do not work with tun interfaces */
2053 2217
		$int = get_real_interface($ifname);
2054
		if(stristr($int, "tun") == true)
2055
			continue;
2056 2218
		$rules .= "set loginterface {$int}\n";
2057 2219
	}
2058 2220
	return $rules;
etc/inc/globals.inc
59 59
	"product_email" => "coreteam@pfsense.org",
60 60
	"hideplatform" => false,
61 61
	"debug" => false,
62
	"latest_config" => "5.8",
62
	"latest_config" => "5.9",
63 63
	"nopkg_platforms" => array("cdrom"),
64 64
	"minimum_ram_warning" => "115",
65 65
	"minimum_ram_warning_text" => "128 megabytes",
etc/inc/pfsense-utils.inc
131 131
	return "/tmp/tmp-" . time();
132 132
}
133 133

  
134
/****f* pfsense-utils/tdr_install_cron
135
 * NAME
136
 *   tdr_install_cron
137
 * INPUTS
138
 *   $should_install true if the cron entry should be installed, false
139
 *   if the entry should be removed if it is present
140
 * RESULT
141
 *   none
142
 ******/
143
function tdr_install_cron($should_install) {
144
	global $config, $g;
145
	if($g['booting']==true) 
146
		return;
147
	$is_installed = false;
148
	if(!$config['cron']['item'])
149
		return;
150
	$x=0;
151
	foreach($config['cron']['item'] as $item) {
152
		if(strstr($item['command'], "filter_configure_sync")) {
153
			$is_installed = true;
154
			break;
155
		}
156
		$x++;
157
	}
158
	switch($should_install) {
159
		case true:
160
			if(!$is_installed) {
161
				$cron_item = array();
162
				$cron_item['minute'] = "0,15,30,45";
163
				$cron_item['hour'] = "*";
164
				$cron_item['mday'] = "*";
165
				$cron_item['month'] = "*";
166
				$cron_item['wday'] = "*";
167
				$cron_item['who'] = "root";
168
				$cron_item['command'] = "/etc/rc.filter_configure_sync";		
169
				$config['cron']['item'][] = $cron_item;
170
				write_config("Installed 15 minute filter reload for Time Based Rules");
171
				configure_cron();
172
			}
173
		break;
174
		case false:
175
			if($is_installed == true) {
176
				if($x > 0) {
177
					unset($config['cron']['item'][$x]);
178
					write_config();
179
				}
180
				configure_cron();
181
			}
182
		break;
183
	}
184
}
185

  
186
/****f* pfsense-utils/tdr_create_ipfw_rule
187
 * NAME
188
 *   tdr_create_ipfw_rule
189
 * INPUTS
190
 *   $rule xml firewall rule array, $type allow or deny
191
 * RESULT
192
 *   text string with ipfw rule already formatted
193
 ******/
194
function tdr_create_ipfw_rule($rule, $type) {
195
	global $config, $g, $tdr_get_next_ipfw_rule, $FilterIflist;
196

  
197
	if (isset($rule['disabled']))
198
		return "";
199

  
200
	$int = "";
201
	/* Check to see if the interface is in our list */
202
	if (isset($rule['floating'])) {
203
		if (isset($rule['interface']) && $rule['interface'] <> "") 
204
			$aline['interface'] = "multiple"; /* XXX */
205
		else
206
			$aline['interface'] = "";
207
	} else if (!array_key_exists($rule['interface'], $FilterIflist)) 
208
			return "# {$rule['interface']} does not exist or is disabled for " . $rule['descr'];
209
	else {
210
		if ($rule['interface'] == "pptp" || $rule['interface'] == "pppoe" || $rule['interface'] == "l2tp")
211
			$aline['interface'] = "ng*";
212
		else
213
			$aline['interface'] = " " . $FilterIflist[$rule['interface']]['if'] . " ";
214
	}
215

  
216
	$ifcfg = $FilterIflist[$rule['interface']];
217
	if ($pptpdcfg['mode'] != "server") {
218
		if (($rule['source']['network'] == "pptp") ||
219
			($rule['destination']['network'] == "pptp")) 
220
				return "# source network or destination network == pptp on " . $rule['descr'];
221
	}
222
	if ($rule['source']['network'] && strstr($rule['source']['network'], "opt")) {
223
		if (!array_key_exists($rule['source']['network'], $FilterIflist)) {
224
			$optmatch = "";
225
			if (preg_match("/opt([0-999])/", $rule['source']['network'], $optmatch)) {
226
				$opt_ip = $FilterIflist["opt{$optmatch[1]}"]['ip'];
227
				if(!is_ipaddr($opt_ip))
228
					return "# unresolvable optarray $optmatch[0] - $opt_ip";
229
			} else {
230
				return "# tdr {$rule['source']['network']} !array_key_exists source network " . $rule['descr'];
231
			}
232
		}
233
	}
234
	if ($rule['destination']['network'] && strstr($rule['destination']['network'], "opt")) {
235
		if (!array_key_exists($rule['destination']['network'], $FilterIflist)) {
236
			if(preg_match("/opt([0-999])/", $rule['destination']['network'], $optmatch)) {
237
				$opt_ip = $FilterIflist["opt{$optmatch[1]}"]['ip'];
238
				if(!is_ipaddr($opt_ip))
239
					return "# unresolvable oparray $optmatch[0] - $opt_ip";
240
			} else {
241
				return "# tdr {$item} {$rule['destination']['network']} !array_key_exists dest network " . $rule['descr'];
242
			}
243
		}
244
	}
245
	/* check for unresolvable aliases */
246
	if ($rule['source']['address'] && !alias_expand($rule['source']['address'])) {
247
		file_notice("Filter_Reload", "# unresolvable source aliases {$rule['descr']}");
248
		return "# tdr unresolvable source aliases {$rule['descr']}";
249
	}
250
	if ($rule['destination']['address'] && !alias_expand($rule['destination']['address'])) {
251
		file_notice("Filter_Reload", "# unresolvable dest aliases {$rule['descr']}");
252
		return "# tdr unresolvable dest aliases {$rule['descr']}";
253
	}
254

  
255
	if (isset($rule['protocol'])) {
256
		if($rule['protocol'] == "tcp/udp")
257
			$aline['prot'] = "ip ";
258
		else if($rule['protocol'] == "icmp")
259
			$aline['prot'] = "icmp ";
260
		else
261
			$aline['prot'] = "{$rule['protocol']} ";
262
	} else {
263
		if($rule['source']['port'] <> "" || $rule['destination']['port'] <> "")
264
			$aline['prot'] = "tcp ";
265
	}
266

  
267
	/* source address */
268
	if (isset($rule['source']['any']))
269
		$src = "any";
270
	else if ($rule['source']['network']) {
271
		if (strstr($rule['source']['network'], "opt")) {
272
			$src = $FilterIflist[$rule['source']['network']]['sa'] . "/" .
273
				$FilterIflist[$rule['source']['network']]['sn'];
274
			if (isset($rule['source']['not'])) 
275
				$src = " not {$src}";
276
			/* check for opt$NUMip here */
277
			$matches = "";
278
			if (preg_match("/opt([0-9999])ip/", $rule['source']['network'], $matches)) {
279
				$optnum = $matches[1];
280
				$src = $FilterIflist["opt{$optnum}"]['ip'];
281
			}
282
		} else {
283
			switch ($rule['source']['network']) {
284
				case 'wanip':
285
					$src = $FilterIflist["wan"]['ip'];
286
					break;
287
				case 'lanip':
288
					$src = $FilterIflist["lan"]['ip'];
289
					break;
290
				case 'lan':
291
					$lansa = $FilterIflist['lan']['sa'];
292
					$lansn = $FilterIflist['lan']['sn'];
293
					$src = "{$lansa}/{$lansn}";
294
					break;
295
				case 'pptp':
296
					$pptpsa = gen_subnet($FilterIflist['pptp']['ip'], $FilterIflist['pptp']['sn']);
297
					$pptpsn = $FilterIflist['pptp']['sn'];
298
					$src = "{$pptpsa}/{$pptpsn}";
299
					break;
300
				case 'pppoe':
301
					$pppoesa = gen_subnet($FilterIflist['pppoe']['ip'], $FilterIflist['pppoe']['sn']);
302
					$pppoesn = $FilterIflist['pppoe']['sn'];
303
					$src = "{$pppoesa}/{$pppoesn}";
304
					break;
305
			}
306
			if (isset($rule['source']['not'])) 
307
				$src = " not {$src}";
308
		}
309
	} else if ($rule['source']['address']) {
310
		$expsrc = alias_expand_value($rule['source']['address']);
311
		if(!$expsrc) 
312
			$expsrc = $rule['source']['address'];
313
				
314
		if (isset($rule['source']['not']))
315
			$not = " not";
316
		else
317
			$not = "";
318

  
319
		if (alias_expand_value($rule['source']['address'])) {
320
			$src = "{";
321
			$first_item = true;
322
			foreach(preg_split("/[\s]+/", alias_expand_value($rule['source']['address'])) as $item) {
323
				if($item != "") {
324
					if(!$first_item) 
325
						$src .= " or";
326
						$src .= " {$not}{$item}";
327
						$first_item = false;
328
					}
329
				}
330
					$src .= " }";
331
		} else
332
			$src = "{$not}" . $expsrc;
333
	}
334
	if (!$src || ($src == "/")) 
335
		return "# tdr at the break!";
336
	
337
	$aline['src'] = "from $src ";
338

  
339
	$srcporta = "";
340
	if (in_array($rule['protocol'], array("tcp","udp","tcp/udp"))) {
341
		if ($rule['source']['port']) {
342
			$srcport = explode("-", $rule['source']['port']);
343
			if(alias_expand($srcport[0])) {
344
				$first_time = true;
345
				foreach(preg_split("/[\s]+/", alias_expand_value($srcport[0])) as $item) {
346
					if(!$first_time) 
347
						$srcporta .= ",";				
348
					$srcporta .= $item;
349
					$first_time = false;
350
				}
351
			} else 
352
				$srcporta = $srcport[0];
353
			
354
			if ((!$srcport[1]) || ($srcport[0] == $srcport[1])) {
355
				if(alias_expand($srcport[0]))
356
					$aline['srcport'] = "{$srcporta} ";
357
				else
358
					$aline['srcport'] = "{$srcporta} ";
359
			} else if (($srcport[0] == 1) && ($srcport[1] == 65535)) {
360
				/* no need for a port statement here */
361
			} else if ($srcport[1] == 65535) 
362
				$aline['srcport'] = ">={$srcport[0]} ";
363
			else if ($srcport[0] == 1) 
364
				$aline['srcport']= "<={$srcport[1]} ";
365
			else 
366
				$aline['srcport'] = "{$srcport[0]}-{$srcport[1]} ";
367
		}
368
	}
369

  
370
	/* destination address */
371
	if (isset($rule['destination']['any']))
372
		$dst = "any";
373
	else if ($rule['destination']['network']) {
374
		if (strstr($rule['destination']['network'], "opt")) {
375
			$dst = $FilterIflist[$rule['destination']['network']]['sa'] . "/" .
376
				$FilterIflist[$rule['destination']['network']]['sn'];
377
			if (isset($rule['destination']['not'])) 
378
				$dst = " not {$dst}";
379
			/* check for opt$NUMip here */
380
			$matches = "";
381
			if (preg_match("/opt([0-9999])ip/", $rule['destination']['network'], $matches)) {
382
				$optnum = $matches[1];
383
				$dst = $FilterIflist["opt{$optnum}"]['ip'];
384
			}
385
		} else {
386
			switch ($rule['source']['network']) {
387
				case 'wanip':
388
					$dst = $FilterIflist["wan"]['ip'];
389
					break;
390
				case 'lanip':
391
					$dst = $FilterIflist["lan"]['ip'];
392
					break;
393
				case 'lan':
394
					$lansa = $FilterIflist['lan']['sa'];
395
					$lansn = $FilterIflist['lan']['sn'];
396
					$dst = "{$lansa}/{$lansn}";
397
					break;
398
				case 'pptp':
399
					$pptpsa = gen_subnet($FilterIflist['pptp']['ip'], $FilterIflist['pptp']['sn']);
400
					$pptpsn = $FilterIflist['pptp']['sn'];
401
					$dst = "{$pptpsa}/{$pptpsn}";
402
					break;
403
				case 'pppoe':
404
					$pppoesa = gen_subnet($FilterIflist['pppoe']['ip'], $FilterIflist['pppoe']['sn']);
405
					$pppoesn = $FilterIflist['pppoe']['sn'];
406
					$dst = "{$pppoesa}/{$pppoesn}";
407
					break;
408
			}
409
			if (isset($rule['destination']['not'])) 
410
				$dst = " not {$dst}";
411
		}
412
	} else if ($rule['destination']['address']) {
413
		$expdst = alias_expand_value($rule['destination']['address']);
414
		if(!$expdst) 
415
			$expdst = $rule['destination']['address'];
416
				
417
		if (isset($rule['destination']['not']))
418
			$not = " not";
419
		else
420
			$not = "";
421

  
422
		if (alias_expand_value($rule['destination']['address'])) {
423
			$dst = "{";
424
			$first_item = true;
425
			foreach(preg_split("/[\s]+/", alias_expand_value($rule['destination']['address'])) as $item) {
426
				if($item != "") {
427
					if(!$first_item) 
428
						$dst .= " or";
429
						$dst .= " {$not}{$item}";
430
						$first_item = false;
431
					}
432
				}
433
					$dst .= " }";
434
		} else
435
			$dst = "{$not}" . $expdst;
436
	}
437

  
438
	if (!$dst || ($dst == "/")) 
439
		return "# returning at dst $dst == \"/\"";
440

  
441
	$aline['dst'] = "to $dst ";
442
	$dstporta = "";
443
	if (in_array($rule['protocol'], array("tcp","udp","tcp/udp"))) {
444
		if ($rule['destination']['port']) {
445
			$dstport = explode("-", $rule['destination']['port']);
446
			if(alias_expand($dstport[0])) {
447
				$first_time = true;
448
				foreach(preg_split("/[\s]+/", alias_expand_value($dstport[0])) as $item) {
449
					if(!$first_time)
450
				 		$dstporta .= ",";
451
					$dstporta .= $item;			
452
					$first_time = false;
453
				}
454
			} else 
455
				$dstporta = $dstport[0];
456
		
457
			if ((!$dstport[1]) || ($dstport[0] == $dstport[1])) {
458
				if(alias_expand($dstport[0]))
459
					$aline['dstport'] = "{$dstporta} ";
460
				else
461
					$aline['dstport'] = "{$dstporta} ";
462
			} else if (($dstport[0] == 1) && ($dstport[1] == 65535)) {
463
				/* no need for a port statement here */
464
			} else if ($dstport[1] == 65535) 
465
				$aline['dstport'] = ">= {$dstport[0]} ";
466
			else if ($dstport[0] == 1)
467
				$aline['dstport'] = "<= {$dstport[1]} ";
468
			else
469
				$aline['dstport'] = "{$dstport[0]}-{$dstport[1]} ";
470
		}
471
	}
472
	
473
	if($aline['prot'] == "")
474
		$aline['prot'] = "ip ";
475

  
476
	tdr_get_next_ipfw_rule();
477

  
478
 	/* piece together the actual user rule */
479
	if($type == "skipto") {
480
		$next_rule = tdr_get_next_ipfw_rule();
481
		$next_rule = $next_rule+1;
482
		$type = "skipto $next_rule";
483
	}
484

  
485
	/* piece together the actual user rule */
486
	if ($aline['interface'] == "multiple") {
487
		$tmpline = $type . " " . $aline['prot'] . $aline['src'] . 
488
			$aline['srcport'] . $aline['dst'] . $aline['dstport'] . " in recv ";
489
		$interfaces = explode(",", $rule['interface']);
490
		$ifliste = "";
491
		foreach ($interfaces as $iface) {
492
			if (array_key_exists($iface, $FilterIflist)) 
493
				$line .= "{$tmpline} " . $FilterIflist[$iface]['if'] . "; ";/* XXX */
494
		}
495
	} else if ($aline['interface'] == "")
496
		$line .= $type . " " . $aline['prot'] . $aline['src'] . 
497
			$aline['srcport'] . $aline['dst'] . $aline['dstport'] . " in ";
498
	else
499
		$line .= $type . " " . $aline['prot'] . $aline['src'] . 
500
			$aline['srcport'] . $aline['dst'] . $aline['dstport'] . " in recv " .
501
			$aline['interface'];
502

  
503
	return $line;
504
}
505

  
506
/****f* pfsense-utils/tdr_install_rule
507
 * NAME
508
 *   tdr_install_rule
509
 * INPUTS
510
 *   $rule - ascii string containing the ifpw rule to add
511
 * RESULT
512
 *   none
513
 ******/
514
function tdr_install_rule($rule) {
515
	global $tdr_next_ipfw_rule, $g;
516

  
517
	log_error("installing {$rule}");
518
	$lines = explode(";", $rule);
519
	if (count($lines) > 1) {
520
		foreach ($lines as $line) {
521
			if ($g['debug'])
522
				log_error("Executing /sbin/ipfw -f add {$tdr_next_ipfw_rule} set 9 $line");
523
			mwexec("/sbin/ipfw -f add {$tdr_next_ipfw_rule} set 9 $line");
524
			$tdr_next_ipfw_rule++;
525
		}
526
	} else {
527
		if ($g['debug'])
528
			log_error("Executing /sbin/ipfw -f add {$tdr_next_ipfw_rule} set 9 $rules");
529
		mwexec("/sbin/ipfw -f add $tdr_next_ipfw_rule set 9 $rule");
530
	}
531
	$tdr_next_ipfw_rule++;
532
}
533

  
534
/****f* pfsense-utils/tdr_get_next_ipfw_rule
535
 * NAME
536
 *   tdr_get_next_ipfw_rule
537
 * INPUTS
538
 *  none
539
 * RESULT
540
 *   returns the next available ipfw rule number
541
 ******/
542
function tdr_get_next_ipfw_rule() {
543
	global $tdr_next_ipfw_rule;
544
	if(intval($tdr_next_ipfw_rule) < 2) 
545
		$tdr_next_ipfw_rule = 2;
546
	return $tdr_next_ipfw_rule;
547
 }
548

  
549
/****f* pfsense-utils/tdr_install_set
550
 * NAME
551
 *   tdr_install_set
552
 * INPUTS
553
 *  none
554
 * RESULT
555
 *   swaps in the temporary ipfw time based rule set
556
 ******/
557
function tdr_install_set() {
558
	global $config;
559
	
560
	mwexec("/sbin/ipfw delete 1");
561
	mwexec("/sbin/ipfw add 1 check-state");
562
	mwexec("/sbin/ipfw delete 65534");
563
	mwexec("/sbin/ipfw add 1 allow all from me to any keep-state");
564
	if (!isset ($config['system']['webgui']['noantilockout']) && count($config['interfaces']) > 1) {
565
		/* lan ip lockout */
566
		$lanip = get_interface_ip("lan");
567
		$lansn = get_interface_subnet("lan");
568
		$lansa = gen_subnet($lanip, $lansn);
569
		mwexec("/sbin/ipfw add 1 allow all from {$lansa}/{$lansn} to $lanip keep-state");
570
	}
571
	mwexec("/sbin/ipfw add 65534 check-state");
572
	/* set 8 contains time based rules */
573
	mwexec("/sbin/ipfw -f delete set 8");
574
	mwexec("/sbin/ipfw -f set swap 9 8");
575
}
576

  
577
/****f* pfsense-utils/get_time_based_rule_status
578
 * NAME
579
 *   get_time_based_rule_status
580
 * INPUTS
581
 *   xml schedule block
582
 * RESULT
583
 *   true/false - true if the rule should be installed
584
 ******/
585
/*
586
 <schedules>
587
   <schedule>
588
     <name>ScheduleMultipleTime</name>
589
     <descr>main descr</descr>
590
     <time>
591
       <position>0,1,2</position>
592
       <hour>0:0-24:0</hour>
593
       <desc>time range 2</desc>
594
     </time>
595
     <time>
596
       <position>4,5,6</position>
597
       <hour>0:0-24:0</hour>
598
       <desc>time range 1</desc>
599
     </time>
600
   </schedule>
601
 </schedules>
602
*/
603
function get_time_based_rule_status($schedule) {
604
	$should_add_rule = false;
605
	/* no schedule? rule should be installed */
606
	if($schedule == "") 
607
		return true;
608
	/*
609
	 * iterate through time blocks and deterimine
610
	 * if the rule should be installed or not.
611
	 */
612
	foreach($schedule['timerange'] as $timeday) {
613
		if($timeday['month']) 
614
			$month = $timeday['month'];
615
		else 
616
			$week = "";	
617
		if($timeday['day']) 
618
			$day = $timeday['day'];
619
		else 
620
			$day = "";
621
		if($timeday['hour']) 
622
			$hour = $timeday['hour'];
623
		else 
624
			$hour = "";
625
		if($timeday['position']) 
626
			$position = $timeday['position'];
627
		else 
628
			$position = "";
629
		if($timeday['desc']) 
630
			$desc = $timeday['desc'];
631
		else 
632
			$desc = "";
633
		if($month) {
634
			$monthstatus = tdr_month($month);
635
		} else {
636
			$monthstatus = true;
637
		}
638
		if($day) {
639
			$daystatus = tdr_day($day);
640
		} else {
641
			$daystatus = true;
642
		}
643
		if($hour) {
644
			$hourstatus = tdr_hour($hour);
645
		} else {
646
			$hourstatus = true;
647
		}
648
		if($position) {
649
			$positionstatus = tdr_position($position);
650
		} else {
651
			$positionstatus = true;
652
		}
653

  
654
		if($monthstatus == true) 
655
			if($daystatus == true) 
656
				if($positionstatus == true) 
657
					if($hourstatus == true) {
658
						$should_add_rule = true;
659
					}
660
	}
661
	
662
	return $should_add_rule;
663
}
664

  
665
function tdr_day($schedule) {
666
	/*
667
	 * Calculate day of month. 
668
	 * IE: 29th of may
669
	 */
670
	$weekday	= date("w");
671
	if ($weekday == 0)
672
		$weekday = 7;
673
	$date	 	= date("d");
674
	$defined_days = split(",", $schedule);
675
	log_error("[TDR DEBUG] tdr_day($schedule)");
676
	foreach($defined_days as $dd) {
677
		if($date == $dd) {
678
			return true;
679
		}
680
	}
681
	return false;
682
}
683

  
684
function tdr_hour($schedule) {
685
	/* $schedule should be a string such as 16:00-19:00 */
686
	$tmp = split("-", $schedule);
687
	$starting_time = strtotime($tmp[0]);
688
	$ending_time = strtotime($tmp[1]);
689
	$now = strtotime("now");
690
	log_error("[TDR DEBUG] S: $starting_time E: $ending_time N: $now");
691
	if($now >= $starting_time and $now <= $ending_time) {
692
		return true;
693
	}
694
	return false;
695
}
696

  
697
function tdr_position($schedule) {
698
	/*
699
	 * Calculate possition, ie: day of week.
700
	 * Sunday = 7, Monday = 1, Tuesday = 2
701
	 * Weds = 3, Thursday = 4, Friday = 5,
702
	 * Saturday = 6
703
	 * ...
704
	 */
705
	$weekday	= date("w");
706
	log_error("[TDR DEBUG] tdr_position($schedule) $weekday");
707
	if ($weekday == 0)
708
		$weekday = 7;
709
	$schedule_days = split(",", $schedule);
710
	foreach($schedule_days as $day) {
711
		if($day == $weekday) {
712
			return true;
713
		}
714
	}
715
	return false;
716
}
717

  
718
function tdr_month($schedule) {
719
	/*
720
	 * Calculate month
721
	 */
722
	$todays_month = date("n");
723
	$months = split(",", $schedule);
724
	log_error("[TDR DEBUG] tdr_month($schedule)");
725
	foreach($months as $month) {
726
		if($month == $todays_month) {
727
			return true;
728
		}
729
	}
730
	return false;
731
}
732

  
733 134
/****f* pfsense-utils/find_number_of_needed_carp_interfaces
734 135
 * NAME
735 136
 *   find_number_of_needed_carp_interfaces
etc/inc/upgrade_config.inc
1733 1733
		foreach($config['ipsec']['phase2'] as & $ph2ent)
1734 1734
			$ph2ent['mode'] = 'tunnel';
1735 1735
}
1736

  
1737
function upgrade_058_to_059() {
1738
	global $config;
1739

  
1740
	if (is_array($config['schedules']['schedule'])) {
1741
		foreach ($config['schedules']['schedule'] as & $schedl)
1742
			$schedl['schedlabel'] = uniqid();
1743
	}
1744
}
1736 1745
?>
usr/local/www/firewall_rules.php
389 389
					foreach ($a_schedules as $schedule)
390 390
					{
391 391
						if ($schedule['name'] == $filterent['sched'] ){
392
							$schedstatus = get_time_based_rule_status($schedule);
392
							$schedstatus = filter_get_time_based_rule_status($schedule);
393 393
							
394 394
							foreach($schedule['timerange'] as $timerange) {
395 395
								$tempFriendlyTime = "";
usr/local/www/firewall_rules_edit.php
979 979
				}?>
980 980
				</select>
981 981
				<p>Leave as 'none' to leave the rule enabled all the time.</p>
982
				<strong>NOTE:  schedule logic can be a bit different.  Click <a target="_new" href='firewall_rules_schedule_logic.php'>here</a> for more information.</strong>
983 982
			</td>
984 983
		</tr>
985 984
		
usr/local/www/firewall_rules_schedule_logic.php
1
<pre>
2
When working with {$g['product_name']} based schedules, the logic is a bit different from the normal {$g['product_name']} rules.
3

  
4
For example, the rules are evaluated from top to bottom.   
5

  
6
If you have a pass rule and the rule is outside of the schedule, the traffic will be BLOCKED regardless 
7
of pass rules that occur after this rule.
8

  
9
In these cases you will want to change the pass rule to a block style rule to get the needed functionality.
10
</pre>
usr/local/www/firewall_schedule.php
107 107
	<tr>
108 108
	   <td class="listlr" ondblclick="document.location='firewall_schedule_edit.php?id=<?=$i;?>';">
109 109
			<?=htmlspecialchars($schedule['name']); 
110
			  	 $schedstatus = get_time_based_rule_status($schedule);
110
					$schedstatus = filter_get_time_based_rule_status($schedule);
111 111
					 if ($schedstatus) { ?>
112 112
					 	&nbsp;<img src="./themes/<?= $g['theme']; ?>/images/icons/icon_frmfld_time.png" title="Schedule is currently active" width="17" height="17" border="0">
113 113
					 <?php } ?>
usr/local/www/firewall_schedule_edit.php
65 65
	$pconfig['name'] = $a_schedules[$id]['name'];
66 66
	$pconfig['descr'] = html_entity_decode($a_schedules[$id]['descr']);
67 67
	$pconfig['timerange'] = $a_schedules[$id]['timerange'];
68
	$pconfig['schedlabel'] = $a_schedules[$id]['schedlabel'];
68 69
	$getSchedule = true;
69 70
}
70 71

  
......
152 153
		
153 154
	if (!$input_errors) {		
154 155
		
156
		if (!empty($pconfig['schedlabel']))
157
			$schedule['schedlabel'] = $pconfig['schedlabel'];
158
		else
159
			$schedule['schedlabel'] = uniqid();
160

  
155 161
		if (isset($id) && $a_schedules[$id]){
156 162
			$a_schedules[$id] = $schedule;
157 163
		}

Also available in: Unified diff