#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2016 Hans Rosenfeld <rosenfeld@grumpf.hope-2000.org>
#
IPFILTER_FMRI="svc:/network/ipfilter:default"
if [ $? -eq 1 ]; then
fi
if [ $? -eq 1 ]; then
fi
CONF_FILES=""
CONF6_FILES=""
NAT_FILES=""
IPF_SUFFIX=".ipf"
IPF6_SUFFIX=".ipf6"
NAT_SUFFIX=".nat"
# version for configuration upgrades
RPCBINDFMRI="svc:/network/rpc/bind:default"
SMF_ONLINE="online"
SMF_MAINT="maintenance"
SMF_NONE="none"
FW_CONTEXT_PG="firewall_context"
METHOD_PROP="ipf_method"
FW_CONFIG_PG="firewall_config"
POLICY_PROP="policy"
APPLY2_PROP="apply_to"
APPLY2_6_PROP="apply_to_6"
EXCEPTIONS_PROP="exceptions"
EXCEPTIONS_6_PROP="exceptions_6"
TARGET_PROP="target"
TARGET_6_PROP="target_6"
BLOCKPOL_PROP="block_policy"
FW_CONFIG_DEF_PG="firewall_config_default"
FW_CONFIG_OVR_PG="firewall_config_override"
CUSTOM_FILE_PROP="custom_policy_file"
CUSTOM_FILE_6_PROP="custom_policy_file_6"
OPEN_PORTS_PROP="open_ports"
PREFIX_HOST="host:"
PREFIX_NET="network:"
PREFIX_POOL="pool:"
PREFIX_IF="if:"
#
# Get value(s) for given property from either firewall_config_default or
# firewall_config_override property groups.
#
# global_get_prop_value pg_name propname
# pg_name - FW_CONFIG_DEF_PG or FW_CONFIG_OVR_PG
# propname - property name
#
{
target_pg=$1
prop=$2
found=0
for (i=1; i<=NF; i++) {
if (found == 1) {
if (index($i, target_pg) == 1 || index($i, extra_pg) == 1)
break;
print $i;
}
if (split($i, values, "/") < 2)
continue;
if (values[1] == target_pg && values[2] == prop)
found=1;
}
# Return
echo "$value"
}
#
#
# Since an SMF service configuration may get updated during the execution of the
# service method, it's best to read all relevant configuration via one svcprop
# invocation and cache it for later use.
#
# This function reads and stores relevant configuration into GLOBAL_CONFIG and
# initializes the GLOBAL_POLICY and GLOBAL_BLOCK_POLICY variables. GLOBAL_CONFIG
# global_get_prop_value().
#
{
GLOBAL_CONFIG=`svcprop -p ${FW_CONFIG_OVR_PG} -p ${FW_CONFIG_DEF_PG} \
}
#
# Given a service, gets its config pg name
#
{
echo "$FW_CONFIG_DEF_PG"
else
echo "$FW_CONFIG_PG"
fi
return 0
}
#
# Given a service, gets its firewall policy
#
{
config_pg=`get_config_pg $1`
svcprop -p $config_pg/${POLICY_PROP} $1 2>/dev/null
}
#
# block policy can be set to "return", which will expand into
# separate block rules for tcp (block return-rst ...) and all other
# protocols (block return-icmp-as-dest ...)
#
{
config_pg=`get_config_pg $1`
svcprop -p $config_pg/${BLOCKPOL_PROP} $1 2>/dev/null
}
#
# Given a service, gets its source address exceptions for IPv4
#
{
config_pg=`get_config_pg $1`
exceptions=`svcprop -p $config_pg/${EXCEPTIONS_PROP} $1 2>/dev/null`
echo $exceptions | sed -e 's/\\//g'
}
#
# Given a service, gets its source address exceptions for IPv6
#
{
config_pg=`get_config_pg $1`
exceptions6=`svcprop -p $config_pg/${EXCEPTIONS_6_PROP} $1 2>/dev/null`
echo $exceptions6 | sed -e 's/\\//g'
}
#
# Given a service, gets its firewalled source addresses for IPv4
#
{
config_pg=`get_config_pg $1`
apply2=`svcprop -p $config_pg/${APPLY2_PROP} $1 2>/dev/null`
}
#
# Given a service, gets its firewalled source addresses for IPv6
#
{
config_pg=`get_config_pg $1`
apply2_6=`svcprop -p $config_pg/${APPLY2_6_PROP} $1 2>/dev/null`
}
#
# Given a service, gets its firewalled target addresses for IPv4
#
{
config_pg=`get_config_pg $1`
target=`svcprop -p $config_pg/${TARGET_PROP} $1 2>/dev/null`
}
#
# Given a service, gets its firewalled target addresses for IPv6
#
{
config_pg=`get_config_pg $1`
target6=`svcprop -p $config_pg/${TARGET_6_PROP} $1 2>/dev/null`
}
{
[ -d $VAR_IPF_DIR ] && return 0
mkdir $VAR_IPF_DIR >/dev/null 2>&1 || return 1
}
#
# fmri_to_file fmri suffix
#
{
check_ipf_dir || return 1
echo "${fprefix}${2}"
}
#
# Return service's enabled property
#
{
#
# Temporary enabled state overrides the persistent state
# so check it first.
#
enabled_ovr=`svcprop -c -p general_ovr/enabled $1 2>/dev/null`
fi
}
#
# Return whether service is desired state
#
# Args: fmri state
# Return:
# 0 - desired state is service's current state
# 1 - desired state is not service's current state
#
{
#
# Make sure we're done with ongoing state transition
#
sleep 1
done
}
#
# "pool:number", and "if:interface". This function returns the
#
{
value_is_interface $1 && return 1
-e "s,^${PREFIX_HOST}\(.*\),\1,p" \
-e "s,^${PREFIX_NET}\(.*\),\1,p" \
-e "s,^any,any,p"
}
{
value_is_interface $1 || return 1
}
#
#
#
{
[ -z "$1" ] && return 1
}
#
# Remove rules in given file from active list without restarting ipfilter
#
{
}
{
}
{
}
{
}
{
}
{
awk '{if (length($0) > 1) {printf("%s ", $1)}}'
}
{
awk '{if (length($0) > 1) {printf("%s ",$1)}}'
}
#
# Given two list of ports, return failure if there's a duplicate.
#
{
#
# If either list is empty, there isn't any conflict.
#
[ -z "$1" -o -z "$2" ] && return 0
for p in $1; do
done
done
return 0
}
#
# Given a file containing ipf rules, check the syntax and verify
# the rules don't conflict, use same port number, with active
# rules (ipfstat -io output).
#
{
check_ipf_syntax $1 $2 || return 1
lports=`file_get_ports $1 $2`
}
#
# Given a file containing ipf rules, check the syntax and verify
# the rules don't conflict with already processed services.
#
# The list of processed services' ports are maintained in the global
# variables 'server_port_list' and 'server_port_list_6'.
#
{
check_ipf_syntax $1 $2 || return 1
lports=`file_get_ports $1 $2`
if [ "$2" = "-6" ]; then
else
fi
return 0
}
{
}
{
check_ipf_syntax $1 $2 && ipf $2 -f $1 >/dev/null 2>&1
}
{
check_nat_syntax $1 && ipnat -f $1 >/dev/null 2>&1
}
#
# get port information from string of the form "proto:{port | port-port}"
#
{
if [ $? -eq 0 ]; then
return 1
[ $a \-le $b ] && echo $a $b || echo $b $a )`
[ $p -gt 65535 ] && return 1
done
echo "$ports"
else
#
# port_str is a single port, verify and return it.
#
echo "$port_str"
fi
}
#
# get proto info from string of the form "{tcp | udp}:port"
#
{
return 0
}
{
newpid=$$
#
# Clear lock if the owning process is no longer around.
#
fi
#
# Grab the lock
#
while :; do
sleep 1
done
}
#
# Remove lock if it's ours
#
{
fi
return 0
}
#
#
{
#
# Nothing to do if the input file doesn't exist.
#
[ ! -f "$1" ] && return 0
check_ipf_dir || return 1
rm $IPFILCONF >/dev/null 2>&1
ln -s $1 $IPFILCONF >/dev/null 2>&1
}
#
#
{
#
# Nothing to do if the input file doesn't exist.
#
[ ! -f "$1" ] && return 0
check_ipf_dir || return 1
rm $IP6FILCONF >/dev/null 2>&1
ln -s $1 $IP6FILCONF >/dev/null 2>&1
}
#
# New file replaces original file if they have different content
#
{
orig=$1
new=$2
#
# IPFILCONF may be a symlink, remove it if that's the case
#
fi
check_ipf_dir || return 1
}
#
# Given a service, gets the following details for ipf rule:
# - policy
# - protocol
# - port(IANA port obtained by running servinfo)
#
{
service=$1
policy=`get_policy ${service}`
#
# Empties service's rules file so callers won't use existing rule if
# we fail here.
#
#
# Nothing to do if policy is "use_global"
#
else
fi
#
# Bail if iana_name isn't defined. Services with static rules
# iana name and protocol information.
#
#
# RPC services
#
# The ports used for IPv6 are usually also reachable
# through IPv4, so generate IPv4 rules for them, too.
tports=`$SERVINFO -R -p -t -s $iana_name 2>/dev/null`
done
fi
done
fi
uports=`$SERVINFO -R -p -u -s $iana_name 2>/dev/null`
done
fi
done
fi
return 0
fi
#
# Get the IANA port and supported protocols(tcp and udp)
#
tport=`$SERVINFO -p -t -s $iana_name 2>&1`
fi
tport6=`$SERVINFO -p -t6 -s $iana_name 2>&1`
fi
uport=`$SERVINFO -p -u -s $iana_name 2>&1`
fi
uport6=`$SERVINFO -p -u6 -s $iana_name 2>&1`
fi
return 0
}
#
# Given a service's name, policy, protocol and port, generate ipf rules
#
# A 'use_global' policy inherits the system-wided Global Default policy
# ordered as:
#
# - make exceptions to policy for those in "exceptions" list
# - apply policy to those specified in "apply_to" list
# - policy rule
#
{
service=$1
mypolicy=$2
proto=$3
port=$4
out=$5
_6=$6
#
# Default mode is to inherit from global's policy
#
tcp_opts=""
fi
else
block_policy=""
fi
#
# Allow all if policy is 'none'
#
echo "pass in log quick proto ${proto} from any to ${daddr}" \
done
return 0
fi
#
# For now, let's concern ourselves only with incoming traffic.
#
echo "${ecmd} in log quick on ${ifc} from any to" \
done
continue
fi
echo "${ecmd} in log quick proto ${proto} from ${saddr}" \
done
fi
done
echo "${acmd} in log quick on ${ifc} from any to" \
done
continue
fi
echo "${acmd} in log quick proto ${proto} from ${saddr}" \
done
fi
done
echo "${ecmd} in log quick proto ${proto} from any to ${daddr}" \
done
return 0
}
#
# Service has either IANA ports and proto or its own firewall method to
# generate the rules.
#
# - if service has a custom method, use it to populate its rules
# - if service has a firewall_config pg, use process_server_svc
#
# Argument - fmri
#
{
#
#
service_check_state $1 $SMF_MAINT && return 1
method=`svcprop -p $FW_CONTEXT_PG/$METHOD_PROP $1 2>/dev/null | \
else
svcprop -p $FW_CONFIG_PG $1 >/dev/null 2>&1 || return 1
process_server_svc $1 || return 1
fi
return 0
}
#
# Generate rules for protocol/port defined in firewall_config_default/open_ports
# property. These are non-service programs whose network resource info are
# defined as "{tcp | upd}:{PORT | PORT-PORT}". Essentially, these programs need
# some specific local ports to be opened. For example, BitTorrent clients need to
# have 6881-6889 opened.
#
{
out=$1
[ $? -eq 1 ] && continue
set -- $port
if [ $# -gt 1 ]; then
echo "pass in log quick from any to any" \
else
echo "pass in log quick proto ${proto} from any" \
fi
else
echo "pass in log quick from any to any" \
else
echo "pass in log quick proto ${proto} from any" \
fi
fi
done
return 0
}
#
# ipf.conf is empty .
#
{
return 0
fi
fi
'none')
# No rules
replace_file ${IPFILCONF} ${TEMP}
replace_file ${IP6FILCONF} ${TEMP6}
return $?
;;
'deny')
ecmd="pass"
acmd="block"
;;
'allow')
ecmd="block"
acmd="pass"
;;
*)
return 1;
;;
esac
continue
fi
fi
done
continue
fi
fi
done
continue
fi
fi
done
continue
fi
fi
done
#
# Allow DHCP(v6) traffic if running as a DHCP client
#
if [ $? -eq 0 ]; then
echo "pass out log quick from any port = 68" \
echo "pass out log quick from any port = 546" \
fi
fi
replace_file ${IPFILCONF} ${TEMP}
replace_file ${IP6FILCONF} ${TEMP6}
return $?
}
#
# Generate a new /etc/ipf/ipf_ovr.conf, the override system-wide policy. It's
# a simplified policy that doesn't support 'exceptions' entities.
#
# If firewall policy is "none", no rules are generated.
#
# Note that "pass" rules don't have "quick" as we don't want
# them to override services' block rules.
#
{
#
# Simply empty override file if global policy is 'custom'
#
return 0
fi
#
# Get and process override policy
#
return 0
fi
for name in $apply2_list; do
continue
fi
fi
done
for name in $apply2_6_list; do
continue
fi
fi
done
replace_file ${IPFILOVRCONF} ${TEMP}
replace_file ${IP6FILOVRCONF} ${TEMP6}
return $?
}
#
# Service is put into maintenance state due to its invalid firewall
#
{
svcadm mark maintenance $1 >/dev/null 2>&1
echo "[ $date ${0}: $1 has invalid ipf configuration. ]"
echo "[ $date ${0}: placing $1 in maintenance. ]"
#
# Move service's rule files to another location since
# they're most likely invalid.
#
return 0
}
{
svcprop -p $FW_CONFIG_PG $1 >/dev/null 2>&1
}
#
# Create rules for enabled firewalling and client services.
# - obtain the list of enabled services and process them
# - save the list of rules file for later use
#
{
#
# Do nothing if global policy is 'custom'
#
#
# Get all enabled services
#
#
# Process enabled services
#
service_is_enabled $s || continue
process_service $s || continue
if [ $? -ne 0 ]; then
continue
fi
if [ $? -eq 0 ]; then
if [ $? -ne 0 ]; then
continue
fi
fi
fi
if [ $? -ne 0 ]; then
continue
fi
if [ $? -eq 0 ]; then
if [ $? -ne 0 ]; then
continue
fi
fi
fi
if [ $? -ne 0 ]; then
continue
fi
fi
done
return 0
}
#
# We update a services ipf ruleset in the following manners:
# - service is disabled, tear down its rules.
# - service is disable or refreshed(online), setup or update its rules.
#
{
svc=$1
#
# Don't go further if service is disabled or in maintenance.
#
service_is_enabled $svc || return 0
service_check_state $1 $SMF_MAINT && return 0
process_service $svc || return 1
if [ $? -ne 0 ]; then
return 1
fi
fi
if [ $? -ne 0 ]; then
return 1
fi
fi
if [ $? -ne 0 ]; then
return 1
fi
fi
if [ $? -eq 0 ]; then
if [ $? -ne 0 ]; then
return 1
fi
fi
#
# reload Global Override rules to
# maintain correct ordering.
#
fi
if [ $? -eq 0 ]; then
if [ $? -ne 0 ]; then
return 1
fi
fi
#
# reload Global Override rules to
# maintain correct ordering.
#
fi
return 0
}
#
# Call the service_update_rules with appropriate svc fmri.
#
#
{
svc=$1
ret=0
#
# If ipfilter isn't online or global policy is 'custom',
# nothing should be done.
#
service_check_state $SMF_FMRI $SMF_ONLINE || return 0
service_update_rules $svc || ret=1
return $ret
}
#
# Initialize global configuration
#