ipf_include.sh revision 6f7d61cdf37e55c737a1ecb01bf5b8453f55c7d1
#
# 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
#
#
IPFILTER_FMRI="svc:/network/ipfilter:default"
if [ $? -eq 1 ]; then
fi
if [ $? -eq 1 ]; then
fi
if [ $? -eq 1 ]; then
fi
CONF_FILES=""
NAT_FILES=""
IPF_SUFFIX=".ipf"
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"
EXCEPTIONS_PROP="exceptions"
FW_CONFIG_DEF_PG="firewall_config_default"
FW_CONFIG_OVR_PG="firewall_config_override"
CUSTOM_FILE_PROP="custom_policy_file"
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 store relevant configuration into GLOBAL_CONFIG and
# and their corresponding values (i.e. svcprop -p pg fmri output). To get values
#
{
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
}
#
# Given a service, gets its firewall policy
#
{
config_pg=`get_config_pg $1`
svcprop -p $config_pg/${EXCEPTIONS_PROP} $1 2>/dev/null
}
#
# Given a service, gets its firewall policy
#
{
config_pg=`get_config_pg $1`
svcprop -p $config_pg/${APPLY2_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
#
get_IP()
{
value_is_interface $1 && return 1
-e 's,^host:\(.*\),\1,p' \
-e 's,^network:\(.*\),\1,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 || return 1
lports=`file_get_ports $1`
}
#
# 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
# variable 'server_port_list'.
#
{
check_ipf_syntax $1 || return 1
lports=`file_get_ports $1`
return 0
}
{
}
{
check_ipf_syntax $1 && ipf -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
}
#
# 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
ip="any"
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
#
tports=`$SERVINFO -R -p -t -s $iana_name 2>/dev/null`
done
fi
uports=`$SERVINFO -R -p -u -s $iana_name 2>/dev/null`
done
fi
return 0
fi
#
# Get the IANA port and supported protocols(tcp and udp)
# No support for IPv6 at this point.
#
tport=`$SERVINFO -p -t -s $iana_name 2>&1`
fi
uport=`$SERVINFO -p -u -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
ip=$4
port=$5
out=$6
#
# Default mode is to inherit from global's policy
#
tcp_opts=""
#
# Allow all if policy is 'none'
#
echo "pass in log quick proto ${proto} from any to ${ip}" \
return 0
fi
#
# For now, let's concern only with incoming traffic.
#
echo "${ecmd} in log quick on ${ifc} from any to" \
continue
fi
echo "${ecmd} in log quick proto ${proto} from ${addr}" \
fi
done
echo "${acmd} in log quick on ${ifc} from any to" \
continue
fi
echo "${acmd} in log quick proto ${proto} from ${addr}" \
fi
done
echo "${ecmd} in log quick proto ${proto} from any to ${ip}" \
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}
return $?
;;
'deny')
ecmd="pass"
acmd="block"
;;
'allow')
ecmd="block"
acmd="pass"
;;
*)
return 1;
;;
esac
continue
fi
fi
done
continue
fi
fi
done
#
# Allow DHCP 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}
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
replace_file ${IPFILOVRCONF} ${TEMP}
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
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 [ $? -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
#