interfaces: add proper argument parsing, simplify invokes #5565

This commit is contained in:
Franco Fichtner 2022-02-25 09:50:29 +01:00
parent bf484cbcf2
commit b8bdb8deb7
5 changed files with 112 additions and 104 deletions

View File

@ -2758,25 +2758,25 @@ fi
case \$REASON in
REQUEST)
/usr/bin/logger -t dhcp6c "dhcp6c \$REASON on {$wanif} - running newipv6"
rm -f /tmp/{$wanif}_pdinfo /tmp/${wanif}_searchdomainv6
if [ -n "\${PDINFO}" ]; then
echo \${PDINFO} > /tmp/{$wanif}_pdinfo
else
rm -f /tmp/{$wanif}_pdinfo
fi
if [ -n "\${new_domain_name}" ]; then
echo \${new_domain_name} > /tmp/${wanif}_searchdomainv6
fi
/usr/local/opnsense/scripts/interfaces/nameserver.sh -d ${wanif} 6
if [ -n "\${new_domain_name_servers}" ]; then
for NAMESERVER in \${new_domain_name_servers}; do
/usr/local/opnsense/scripts/interfaces/nameserver.sh -a \${NAMESERVER} ${wanif} 6
done
fi
ARGS="-i ${wanif} -6nd"
for NAMESERVER in \${new_domain_name_servers}; do
ARGS="\${ARGS} -a \${NAMESERVER}"
done
/usr/local/opnsense/scripts/interfaces/nameserver.sh \${ARGS}
ARGS="-i ${wanif} -6sd \${new_domain_name:+"-a \${new_domain_name}"}"
/usr/local/opnsense/scripts/interfaces/nameserver.sh \${ARGS}
/usr/local/sbin/configctl -d interface newipv6 {$wanif}
;;
EXIT|RELEASE)
/usr/bin/logger -t dhcp6c "dhcp6c \$REASON on {$wanif} - running newipv6"
/usr/local/opnsense/scripts/interfaces/nameserver.sh -d ${wanif} 6
rm -f /tmp/{$wanif}_pdinfo /tmp/${wanif}_searchdomainv6
/usr/local/opnsense/scripts/interfaces/nameserver.sh -i ${wanif} -6nd
/usr/local/opnsense/scripts/interfaces/nameserver.sh -i ${wanif} -6sd
rm -f /tmp/{$wanif}_pdinfo
/usr/local/sbin/configctl -d interface newipv6 {$wanif}
;;
*)

View File

@ -215,21 +215,16 @@ add_new_routes() {
add_new_resolv_conf() {
$LOGGER "Creating resolv.conf"
/usr/local/opnsense/scripts/interfaces/nameserver.sh -d ${interface}
ARGS="-i ${interface} -4nd"
for nameserver in ${new_domain_name_servers}; do
ARGS="${ARGS} -a ${nameserver}"
done
if [ -n "$new_domain_name_servers" ]; then
for nameserver in $new_domain_name_servers; do
# only add the nameservers to persistent storage and
# let the system decide if routes need to be installed
/usr/local/opnsense/scripts/interfaces/nameserver.sh -a ${nameserver} ${interface}
done
fi
/usr/local/opnsense/scripts/interfaces/nameserver.sh ${ARGS}
rm -f /tmp/${interface}_searchdomain
ARGS="-i ${interface} -4sd ${new_domain_name:+"-a ${new_domain_name}"}"
if [ -n "$new_domain_name" ]; then
echo $new_domain_name > /tmp/${interface}_searchdomain
fi
/usr/local/opnsense/scripts/interfaces/nameserver.sh ${ARGS}
return 0
}

View File

@ -23,65 +23,86 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# -a attach contents
# -d undo contents
# -v print contents
DO="-v"
IP=
# requirements:
# -4 IPv4 (default)
# -6 IPv6
# -i <interface>
case "${1}" in
-a)
DO="-a"
shift
IP=${1}
shift
;;
-d)
DO="-d"
shift
;;
-v)
DO="-v"
shift
;;
-*)
echo "unknwon option '${1}'"
exit 1
;;
*)
;;
esac
# commands:
# -a <content> attach (multiple)
# -d undo contents
# -n nameserver mode (default)
# -s searchdomain mode
# (none of the above: print contents)
IF=${1}
AF=inet
DO_CONTENTS=
DO_DELETE=
DO_MODE=
AF=
MD=
EX=
IF=
if [ "${2:-4}" = 6 ]; then
AF=inet6
EX=v6
# default to IPv4 with nameserver mode
set -- -4 -n ${@}
while getopts 46a:di:ns OPT; do
case ${OPT} in
4)
AF=inet
EX=
;;
6)
AF=inet6
EX=v6
;;
a)
DO_CONTENTS="${DO_CONTENTS} ${OPTARG}"
;;
d)
DO_DELETE="-d"
;;
i)
IF=${OPTARG}
;;
n)
DO_MODE="-n"
MD="nameserver"
;;
s)
DO_MODE="-s"
MD="searchdomain"
;;
*)
echo "Unknown option: ${OPT}" >&2
exit 1
;;
esac
done
if [ -z "${IF}" ]; then
echo "Missing option: -i" >&2
exit 1
fi
FILE="/tmp/${IF:?}_nameserver${EX}"
FILE="/tmp/${IF}_${MD}${EX}"
if [ ! -f ${FILE} -a ${DO} != "-a" ]; then
return
fi
case "${DO}" in
-a)
echo "${IP}" >> ${FILE}
;;
-d)
for IP in $(cat ${FILE}); do
# flush routes here to make sure they are recycled properly
route delete -"${AF}" "${IP}"
done
if [ -n "${DO_DELETE}" ]; then
if [ "${DO_MODE}" = "-n" -a -f ${FILE} ]; then
for CONTENT in $(cat ${FILE}); do
# flush routes here to make sure they are recycled properly
route delete -${AF} "${CONTENT}"
done
fi
rm -f ${FILE}
;;
-v)
fi
for CONTENT in ${DO_CONTENTS}; do
echo "${CONTENT}" >> ${FILE}
done
if [ -z "${DO_CONTENTS}${DO_DELETE}" -a -f ${FILE} ]; then
cat ${FILE}
;;
*)
;;
esac
fi

View File

@ -20,7 +20,7 @@ if [ "${AF}" = "inet" ]; then
route delete -${AF} default "${GW}"
fi
/usr/local/opnsense/scripts/interfaces/nameserver.sh -d ${IF} 4
/usr/local/opnsense/scripts/interfaces/nameserver.sh -i ${IF} -4 -d
rm -f /tmp/${IF}_router
elif [ "${AF}" = "inet6" ]; then
@ -33,7 +33,7 @@ elif [ "${AF}" = "inet6" ]; then
route delete -${AF} default "${GW}"
fi
/usr/local/opnsense/scripts/interfaces/nameserver.sh -d ${IF} 6
/usr/local/opnsense/scripts/interfaces/nameserver.sh -i ${IF} -6 -d
rm -f /tmp/${IF}_routerv6

View File

@ -2,44 +2,36 @@
export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin
if [ "${2}" = "inet" ]; then
rm -f /tmp/${1}_router
DNS1=
if echo "${6}" | grep -q dns1; then
DNS1="-a $(echo "${6}" | awk '{print $2}')"
fi
DNS2=
if echo "${7}" | grep -q dns2; then
DNS2="-a $(echo "${7}" | awk '{print $2}')"
fi
if [ "${2}" = "inet" ]; then
if [ -n "${4}" ]; then
echo ${4} > /tmp/${1}_router
else
rm -f /tmp/${1}_router
fi
/usr/local/opnsense/scripts/interfaces/nameserver.sh -d {1} 4
if echo "${6}" | grep -q dns1; then
DNS1=$(echo "${6}" | awk '{print $2}')
/usr/local/opnsense/scripts/interfaces/nameserver.sh -a ${DNS1} {1} 4
fi
if echo "${7}" | grep -q dns2; then
DNS2=$(echo "${7}" | awk '{print $2}')
/usr/local/opnsense/scripts/interfaces/nameserver.sh -a ${DNS2} {1} 4
fi
/usr/local/opnsense/scripts/interfaces/nameserver.sh -i ${1} -4 -d ${DNS1} ${DNS2}
/usr/local/sbin/configctl -d interface newip ${1}
elif [ "${2}" = "inet6" ]; then
rm -f /tmp/${1}_routerv6
if [ -n "${4}" ]; then
# XXX if this is link local why bother stripping the required scope?
echo ${4} | cut -d% -f1 > /tmp/${1}_routerv6
else
rm -f /tmp/${1}_routerv6
fi
/usr/local/opnsense/scripts/interfaces/nameserver.sh -d {1} 6
if echo "${6}" | grep -q dns1; then
DNS1=$(echo "${6}" | awk '{print $2}')
/usr/local/opnsense/scripts/interfaces/nameserver.sh -a ${DNS1} {1} 6
fi
if echo "${7}" | grep -q dns2; then
DNS2=$(echo "${7}" | awk '{print $2}')
/usr/local/opnsense/scripts/interfaces/nameserver.sh -a ${DNS2} {1} 6
fi
/usr/local/opnsense/scripts/interfaces/nameserver.sh -i ${1} -6 -d ${DNS1} ${DNS2}
/usr/local/sbin/configctl -d interface newipv6 ${1}
fi