ipf_include.sh revision 0a5f928c505bb3f860fa2383364d7b1dff588d59
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#!/sbin/sh
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# CDDL HEADER START
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# The contents of this file are subject to the terms of the
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Common Development and Distribution License (the "License").
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# You may not use this file except in compliance with the License.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# or http://www.opensolaris.org/os/licensing.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# See the License for the specific language governing permissions
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# and limitations under the License.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# When distributing Covered Code, include this CDDL HEADER in each
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# If applicable, add the following below this CDDL HEADER, with the
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# fields enclosed by brackets "[]" replaced with your own identifying
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# information: Portions Copyright [yyyy] [name of copyright owner]
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# CDDL HEADER END
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Use is subject to license terms.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenETC_IPF_DIR=/etc/ipf
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenIP6FILCONF=$ETC_IPF_DIR/ipf6.conf
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenIPNATCONF=$ETC_IPF_DIR/ipnat.conf
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenIPPOOLCONF=$ETC_IPF_DIR/ippool.conf
ada83f08084885463e1f362944377a12294a1feeTruong NguyenVAR_IPF_DIR=/var/run/ipf
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenIPFILCONF=$VAR_IPF_DIR/ipf.conf
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenIPFILOVRCONF=$VAR_IPF_DIR/ipf_ovr.conf
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenIPF_LOCK=/var/run/ipflock
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenCONF_FILES=""
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenNAT_FILES=""
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenIPF_SUFFIX=".ipf"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenNAT_SUFFIX=".nat"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# version for configuration upgrades
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenCURRENT_VERSION=1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenIPF_FMRI="svc:/network/ipfilter:default"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenINETDFMRI="svc:/network/inetd:default"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenRPCBINDFMRI="svc:/network/rpc/bind:default"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenSMF_ONLINE="online"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenSMF_MAINT="maintenance"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenSMF_NONE="none"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenFW_CONTEXT_PG="firewall_context"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenMETHOD_PROP="ipf_method"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenFW_CONFIG_PG="firewall_config"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenPOLICY_PROP="policy"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenAPPLY2_PROP="apply_to"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenEXCEPTIONS_PROP="exceptions"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenFW_CONFIG_DEF_PG="firewall_config_default"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenFW_CONFIG_OVR_PG="firewall_config_override"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenCUSTOM_FILE_PROP="custom_policy_file"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenOPEN_PORTS_PROP="open_ports"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenPREFIX_HOST="host:"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenPREFIX_NET="network:"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenPREFIX_POOL="pool:"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenPREFIX_IF="if:"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong NguyenSERVINFO=/usr/lib/servinfo
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Given a service, gets its config pg name
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenget_config_pg()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ "$1" = "$IPF_FMRI" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "$FW_CONFIG_DEF_PG"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen else
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "$FW_CONFIG_PG"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Given a service, gets its firewall policy
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenget_policy()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen config_pg=`get_config_pg $1`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svcprop -p $config_pg/${POLICY_PROP} $1 2>/dev/null
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenget_global_def_policy()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svcprop -p ${FW_CONFIG_DEF_PG}/${POLICY_PROP} $IPF_FMRI 2>/dev/null
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Given a service, gets its firewall policy
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenget_exceptions()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen config_pg=`get_config_pg $1`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svcprop -p $config_pg/${EXCEPTIONS_PROP} $1 2>/dev/null
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Given a service, gets its firewall policy
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenget_apply2_list()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen config_pg=`get_config_pg $1`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svcprop -p $config_pg/${APPLY2_PROP} $1 2>/dev/null
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyencheck_ipf_dir()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -d $VAR_IPF_DIR ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen mkdir $VAR_IPF_DIR >/dev/null 2>&1 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# fmri_to_file fmri suffix
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenfmri_to_file()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_ipf_dir || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fprefix="${VAR_IPF_DIR}/`echo $1 | tr -s '/:' '__'`"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${fprefix}${2}"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Return service's enabled property
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenservice_is_enabled()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Temporary enabled state overrides the persistent state
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # so check it first.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen enabled_ovr=`svcprop -c -p general_ovr/enabled $1 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -n "$enabled_ovr" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$enabled_ovr" = "true" ] && return 0 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen enabled=`svcprop -c -p general/enabled $1 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -n "$enabled" -a "$enabled" = "true" ] && return 0 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Return whether service is desired state
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Args: fmri state
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Return:
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# 0 - desired state is service's current state
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# 1 - desired state is not service's current state
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenservice_check_state()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Make sure we're done with ongoing state transition
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen while [ "`svcprop -p restarter/next_state $1`" != "$SMF_NONE" ]; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen sleep 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "`svcprop -p restarter/state $1`" = "$2" ] && return 0 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Deny/Allow list stores values in the form "host:addr", "network:addr/netmask",
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# "pool:number", and "if:interface". This function returns the
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# IP(addr or addr/netmask) value or a pool number.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenget_IP()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen value_is_interface $1 && return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "$1" | sed -n -e 's,^pool:\(.*\),pool/\1,p' \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen -e 's,^host:\(.*\),\1,p' \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen -e 's,^network:\(.*\),\1,p'
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenget_interface()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen value_is_interface $1 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen scratch=`echo "$1" | sed -e 's/^if://'`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ifconfig $scratch >/dev/null 2>&1 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo $scratch | sed -e 's/:.*//'
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenvalue_is_interface()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$1" ] && return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo $1 | grep "^if:" >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Remove rules in given file from active list without restarting ipfilter
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenremove_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -f "$1" ] && ipf -r -f $1 >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenremove_nat_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -f "$1" ] && ipnat -r -f $1 >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyencheck_ipf_syntax()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipf -n -f $1 >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyencheck_nat_syntax()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipnat -n -f $1 >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenfile_get_ports()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipf -n -v -f $1 2>/dev/null | sed -n -e \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen 's/.*to.* port = \([a-z0-9]*\).*/\1/p' | uniq | \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen awk '{if (length($0) > 1) {printf("%s ", $1)}}'
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenget_active_ports()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipfstat -io 2>/dev/null | sed -n -e \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen 's/.*to.* port = \([a-z0-9]*\).*/\1/p' | uniq | \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen awk '{if (length($0) > 1) {printf("%s ",$1)}}'
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Given two list of ports, return failure if there's a duplicate.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyensets_check_duplicate()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # If either list is empty, there isn't any conflict.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$1" -o -z "$2" ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for p in $1; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for ap in $2; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$p" = "$ap" ] && return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Given a file containing ipf rules, check the syntax and verify
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# the rules don't conflict, use same port number, with active
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# rules (ipfstat -io output).
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenupdate_check_ipf_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_ipf_syntax $1 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen lports=`file_get_ports $1`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen lactive_ports=`get_active_ports`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen sets_check_duplicate "$lports" "$lactive_ports" || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenserver_port_list=""
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Given a file containing ipf rules, check the syntax and verify
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# the rules don't conflict with already processed services.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# The list of processed services' ports are maintained in the global
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# variable 'server_port_list'.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyencheck_ipf_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_ipf_syntax $1 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen lports=`file_get_ports $1`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen sets_check_duplicate "$lports" "$server_port_list" || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen server_port_list="$server_port_list $lports"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenprepend_new_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_ipf_syntax $1 && tail -r $1 | sed -e 's/^[a-z]/@0 &/' | \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipf -f - >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenappend_new_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_ipf_syntax $1 && ipf -f $1 >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenappend_new_nat_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_nat_syntax $1 && ipnat -f $1 >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# get port information from string of the form "proto:{port | port-port}"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyentuple_get_port()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen port_str=`echo "$1" | sed -e 's/ //g; s/.*://' 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$port_str" ] && return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo $port_str | grep "-" >/dev/null
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo $port_str | grep '^[0-9]\{1,5\}-[0-9]\{1,5\}$' >/dev/null || \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ports=`echo $port_str | ( IFS=- read a b ; \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ $a \-le $b ] && echo $a $b || echo $b $a )`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for p in $ports; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ $p -gt 65535 ] && return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "$ports"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen else
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # port_str is a single port, verify and return it.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "$port_str" | grep '^[0-9]\{1,5\}$' >/dev/null || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ $port_str -gt 65535 ] && return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "$port_str"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# get proto info from string of the form "{tcp | udp}:port"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyentuple_get_proto()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen proto=`echo "$1" | sed -e 's/ //g; s/:.*//' 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$proto" ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$proto" = "tcp" -o "$proto" = "udp" ] && echo $proto || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenipf_get_lock()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen newpid=$$
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -f "$IPF_LOCK/pid" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen curpid=`cat $IPF_LOCK/pid 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$curpid" = "$newpid" ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Clear lock if the owning process is no longer around.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ps -p $curpid >/dev/null 2>&1 || rm -r $IPF_LOCK >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Grab the lock
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen while :; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen mkdir $IPF_LOCK 2>/dev/null && break;
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen sleep 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo $newpid > $IPF_LOCK/pid
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Remove lock if it's ours
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenipf_remove_lock()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -f "$IPF_LOCK/pid" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "`cat $IPF_LOCK/pid`" = "$$" ] && rm -r $IPF_LOCK
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Make IPFILCONF, /var/tmp/ipf/ipf.conf, a symlink to the input file argument.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyencustom_set_symlink()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
caa64d545b688cd708983338e8c85b054ecfc8beTruong Nguyen # Nothing to do if the input file doesn't exist.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ ! -f "$1" ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
caa64d545b688cd708983338e8c85b054ecfc8beTruong Nguyen check_ipf_dir || return 1
caa64d545b688cd708983338e8c85b054ecfc8beTruong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen rm $IPFILCONF >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ln -s $1 $IPFILCONF >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# New file replaces original file if they have different content
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenreplace_file()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen orig=$1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen new=$2
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # IPFILCONF may be a symlink, remove it if that's the case
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -L "$orig" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen rm $orig
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen touch $orig
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
caa64d545b688cd708983338e8c85b054ecfc8beTruong Nguyen check_ipf_dir || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen mv $new $orig && return 0 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Given a service, gets the following details for ipf rule:
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - policy
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - protocol
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - port(IANA port obtained by running servinfo)
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenprocess_server_svc()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen service=$1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ip="any"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen policy=`get_policy ${service}`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Empties service's rules file so callers won't use existing rule if
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # we fail here.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen file=`fmri_to_file $service $IPF_SUFFIX`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$file" ] && return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "# $service" >${file}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Nothing to do if policy is "use_global"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$policy" = "use_global" ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen restarter=`svcprop -p general/restarter $service 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ "$restarter" = "$INETDFMRI" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen iana_name=`svcprop -p inetd/name $service 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen isrpc=`svcprop -p inetd/isrpc $service 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen else
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen iana_name=`svcprop -p $FW_CONTEXT_PG/name $service 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen isrpc=`svcprop -p $FW_CONTEXT_PG/isrpc $service 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Bail if iana_name isn't defined. Services with static rules
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # like nis/client don't need to generate rules using
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # iana name and protocol information.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$iana_name" ] && return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # RPC services
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ "$isrpc" = "true" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen tports=`$SERVINFO -R -p -t -s $iana_name 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -n "$tports" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for tport in $tports; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen generate_rules $service $policy "tcp" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen $ip $tport $file
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen uports=`$SERVINFO -R -p -u -s $iana_name 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -n "$uports" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for uport in $uports; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen generate_rules $service $policy "udp" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen $ip $uport $file
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Get the IANA port and supported protocols(tcp and udp)
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # No support for IPv6 at this point.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen tport=`$SERVINFO -p -t -s $iana_name 2>&1`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$tport" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen generate_rules $service $policy "tcp" $ip $tport $file
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen uport=`$SERVINFO -p -u -s $iana_name 2>&1`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$uport" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen generate_rules $service $policy "udp" $ip $uport $file
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Given a service's name, policy, protocol and port, generate ipf rules
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - list of host/network/interface to apply policy
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# A 'use_global' policy inherits the system-wided Global Default policy
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# from network/ipfilter. For {deny | allow} policies, the rules are
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# ordered as:
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - make exceptions to policy for those in "exceptions" list
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - apply policy to those specified in "apply_to" list
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - policy rule
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyengenerate_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen service=$1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen mypolicy=$2
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen proto=$3
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ip=$4
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen port=$5
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen out=$6
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Default mode is to inherit from global's policy
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$mypolicy" = "use_global" ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen tcp_opts=""
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$proto" = "tcp" ] && tcp_opts="flags S keep state keep frags"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Allow all if policy is 'none'
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ "$mypolicy" = "none" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "pass in log quick proto ${proto} from any to ${ip}" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "port = ${port} ${tcp_opts}" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # For now, let's concern only with incoming traffic.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$mypolicy" = "deny" ] && { ecmd="pass"; acmd="block"; }
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$mypolicy" = "allow" ] && { ecmd="block"; acmd="pass"; }
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for name in `get_exceptions $service`; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$name" -o "$name" = '""' ] && continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ifc=`get_interface $name`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$ifc" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${ecmd} in log quick on ${ifc} from any to" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "${ip} port = ${port}" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen addr=`get_IP ${name}`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$addr" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${ecmd} in log quick proto ${proto} from ${addr}" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "to ${ip} port = ${port} ${tcp_opts}" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for name in `get_apply2_list $service`; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$name" -o "$name" = '""' ] && continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ifc=`get_interface $name`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$ifc" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${acmd} in log quick on ${ifc} from any to" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "${ip} port = ${port}" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen addr=`get_IP ${name}`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$addr" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${acmd} in log quick proto ${proto} from ${addr}" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "to ${ip} port = ${port} ${tcp_opts}" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${ecmd} in log quick proto ${proto} from any to ${ip}" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "port = ${port} ${tcp_opts}" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Service has either IANA ports and proto or its own firewall method to
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# generate the rules.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - if service has a custom method, use it to populate its rules
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - if service has a firewall_config pg, use process_server_svc
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Argument - fmri
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenprocess_service()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Don't process network/ipfilter
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$1" = "$IPF_FMRI" ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen service_check_state $1 $SMF_MAINT && return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen method=`svcprop -p $FW_CONTEXT_PG/$METHOD_PROP $1 2>/dev/null | \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen sed 's/\\\//g'`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -n "$method" -a "$method" != '""' ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ( exec $method $1 >/dev/null )
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen else
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svcprop -p $FW_CONFIG_PG $1 >/dev/null 2>&1 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen process_server_svc $1 || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Generate rules for protocol/port defined in firewall_config_default/open_ports
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# property. These are non-service programs whose network resource info are
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# defined as "{tcp | upd}:{PORT | PORT-PORT}". Essentially, these programs need
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# some specific local ports to be opened. For example, BitTorrent clients need to
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# have 6881-6889 opened.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenprocess_nonsvc_progs()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen out=$1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "# Non-service programs rules" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen progs=`svcprop -p ${FW_CONFIG_DEF_PG}/${OPEN_PORTS_PROP} \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen $SMF_FMRI 2>/dev/null`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for prog in $progs; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$prog" -o "$prog" = '""' ] && continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen port=`tuple_get_port $prog`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ $? -eq 1 -o -z "$port" ] && continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen proto=`tuple_get_proto $prog`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ $? -eq 1 ] && continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen set -- $port
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $# -gt 1 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -z "$proto" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "pass in log quick from any to any" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "port ${1} >< ${2}" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen else
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "pass in log quick proto ${proto} from any" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "to any port ${1} >< ${2}" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen else
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -z "$proto" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "pass in log quick from any to any" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "port = ${1}" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen else
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "pass in log quick proto ${proto} from any" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "to any port = ${1}" >>${out}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Generate a new /etc/ipf/ipf.conf. If firewall policy is 'none',
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# ipf.conf is empty .
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyencreate_global_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen policy=`get_global_def_policy`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ "$policy" = "custom" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen file=`svcprop -p ${FW_CONFIG_DEF_PG}/${CUSTOM_FILE_PROP} $SMF_FMRI`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -n "$file" ] && custom_set_symlink $file
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen TEMP=`mktemp /var/run/ipf.conf.pid$$.XXXXXX`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen process_nonsvc_progs $TEMP
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "# Global Default rules" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ "$policy" != "none" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "pass out log quick all keep state" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen case "$policy" in
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen 'none')
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # No rules
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen replace_file ${IPFILCONF} ${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return $?
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ;;
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen 'deny')
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ecmd="pass"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen acmd="block"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ;;
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen 'allow')
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ecmd="block"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen acmd="pass"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ;;
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen *)
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 1;
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ;;
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen esac
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for name in `get_exceptions $SMF_FMRI`; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$name" -o "$name" = '""' ] && continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ifc=`get_interface $name`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$ifc" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${ecmd} in log quick on ${ifc} all" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen addr=`get_IP ${name}`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$addr" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${ecmd} in log quick from ${addr} to any" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for name in `get_apply2_list $SMF_FMRI`; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$name" -o "$name" = '""' ] && continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ifc=`get_interface $name`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$ifc" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${acmd} in log quick on ${ifc} all" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen addr=`get_IP ${name}`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$addr" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${acmd} in log quick from ${addr} to any" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ "$policy" = "allow" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Allow DHCP traffic if running as a DHCP client
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen /sbin/netstrategy | grep dhcp >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "pass out log quick from any port = 68" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "keep state" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "pass out log quick from any port = 546" \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen "keep state" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "pass in log quick from any to any port = 68" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "pass in log quick from any to any port = 546" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "block in log all" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen replace_file ${IPFILCONF} ${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return $?
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Generate a new /etc/ipf/ipf_ovr.conf, the override system-wide policy. It's
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# a simplified policy that doesn't support 'exceptions' entities.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# If firewall policy is "none", no rules are generated.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Note that "pass" rules don't have "quick" as we don't want
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# them to override services' block rules.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyencreate_global_ovr_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Simply empty override file if global policy is 'custom'
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ "`get_global_def_policy`" = "custom" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "# 'custom' global policy" >$IPFILOVRCONF
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Get and process override policy
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ovr_policy=`svcprop -p ${FW_CONFIG_OVR_PG}/${POLICY_PROP} $IPF_FMRI`
0a5f928c505bb3f860fa2383364d7b1dff588d59Truong Nguyen if [ "$ovr_policy" = "none" ]; then
0a5f928c505bb3f860fa2383364d7b1dff588d59Truong Nguyen echo "# global override policy is 'none'" >$IPFILOVRCONF
0a5f928c505bb3f860fa2383364d7b1dff588d59Truong Nguyen return 0
0a5f928c505bb3f860fa2383364d7b1dff588d59Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
0a5f928c505bb3f860fa2383364d7b1dff588d59Truong Nguyen TEMP=`mktemp /var/run/ipf_ovr.conf.pid$$.XXXXXX`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$ovr_policy" = "deny" ] && acmd="block in log quick"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$ovr_policy" = "allow" ] && acmd="pass in log"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen apply2_list=`svcprop -p $FW_CONFIG_OVR_PG/$APPLY2_PROP $IPF_FMRI`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for name in $apply2_list; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$name" -o "$name" = '""' ] && continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ifc=`get_interface $name`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$ifc" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${acmd} on ${ifc} all" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen addr=`get_IP ${name}`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 -a -n "$addr" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "${acmd} from ${addr} to any" >>${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen replace_file ${IPFILOVRCONF} ${TEMP}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return $?
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Service is put into maintenance state due to its invalid firewall
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# definition and/or policy.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyensvc_mark_maintenance()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svcadm mark maintenance $1 >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen date=`date`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "[ $date ${0}: $1 has invalid ipf configuration. ]"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen echo "[ $date ${0}: placing $1 in maintenance. ]"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Move service's rule files to another location since
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # they're most likely invalid.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipfile=`fmri_to_file $1 $IPF_SUFFIX`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -f "$ipfile" ] && mv $ipfile "$ipfile.bak"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen natfile=`fmri_to_file $1 $NAT_SUFFIX`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -f "$natfile" ] && mv $natfile "$natfile.bak"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyensvc_is_server()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svcprop -p $FW_CONFIG_PG $1 >/dev/null 2>&1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Create rules for enabled firewalling and client services.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - obtain the list of enabled services and process them
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - save the list of rules file for later use
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyencreate_services_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Do nothing if global policy is 'custom'
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen global_policy=`get_global_def_policy`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "$global_policy" = "custom" ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipf_get_lock
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Get all enabled services
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen allsvcs=`svcprop -cf -p general/enabled -p general_ovr/enabled '*' \
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen 2>/dev/null | sed -n 's,^\(svc:.*\)/:properties/.* true$,\1,p' | sort -u`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Process enabled services
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen for s in $allsvcs; do
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen service_is_enabled $s || continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen process_service $s || continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipfile=`fmri_to_file $s $IPF_SUFFIX`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -n "$ipfile" -a -r "$ipfile" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_ipf_syntax $ipfile
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -ne 0 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svc_mark_maintenance $s
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svc_is_server $s
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_ipf_rules $ipfile
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -ne 0 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svc_mark_maintenance $s
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen CONF_FILES="$CONF_FILES $ipfile"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen natfile=`fmri_to_file $s $NAT_SUFFIX`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -n "$natfile" -a -r "$natfile" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_nat_syntax $natfile
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -ne 0 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svc_mark_maintenance $s
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen continue
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen NAT_FILES="$NAT_FILES $natfile"
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen done
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipf_remove_lock
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# We update a services ipf ruleset in the following manners:
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - service is disabled, tear down its rules.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# - service is disable or refreshed(online), setup or update its rules.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenservice_update_rules()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # If ipfilter isn't online or global policy is 'custom',
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # nothing should be done.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen service_check_state $SMF_FMRI $SMF_ONLINE || return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ "`get_global_def_policy`" = "custom" ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svc=$1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipfile=`fmri_to_file $svc $IPF_SUFFIX`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -z "$ipfile" ] && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen remove_rules $ipfile
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen natfile=`fmri_to_file $svc $NAT_SUFFIX`
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -n "$natfile" ] && remove_nat_rules $natfile
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # Don't go further if service is disabled or in maintenance.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen service_is_enabled $svc || return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen service_check_state $1 $SMF_MAINT && return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen process_service $svc || return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -f "$ipfile" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_ipf_syntax $ipfile
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -ne 0 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svc_mark_maintenance $svc
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -f "$natfile" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen check_nat_syntax $natfile
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -ne 0 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svc_mark_maintenance $svc
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ -f "$ipfile" ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svc_is_server $svc
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -eq 0 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen update_check_ipf_rules $ipfile
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen if [ $? -ne 0 ]; then
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svc_mark_maintenance $svc
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen prepend_new_rules $ipfile
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # reload Global Override rules to
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen # maintain correct ordering.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen #
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen remove_rules $IPFILOVRCONF
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen prepend_new_rules $IPFILOVRCONF
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen fi
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen [ -f "$natfile" ] && append_new_nat_rules $natfile
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return 0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# Call the service_update_rules with appropriate svc fmri.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# This is called from '/lib/svc/method/ipfilter fw_update' whenever
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen# a service is disabled/enabled/refreshed.
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen#
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyenservice_update()
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen{
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen svc=$1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ret=0
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipf_get_lock
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen service_update_rules $svc || ret=1
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen ipf_remove_lock
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen return $ret
eb1a34638eba7c5add1421327f3eb225a8ea7518Truong Nguyen}