From: afresh1 Date: Fri, 16 Dec 2022 04:04:25 +0000 (+0000) Subject: Prioritize lladdr over name/unit in hostname.if processing X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=51e3b6acd682ac91941d711aa2e4790b9d9cf2f2;p=openbsd Prioritize lladdr over name/unit in hostname.if processing When needed, lladdr is more precise and enduring. Suggested by deraadt@ Many improvments and OK kn@ --- diff --git a/distrib/miniroot/install.sub b/distrib/miniroot/install.sub index c39b14ed54c..b6bcccdcbd5 100644 --- a/distrib/miniroot/install.sub +++ b/distrib/miniroot/install.sub @@ -1,5 +1,5 @@ #!/bin/ksh -# $OpenBSD: install.sub,v 1.1215 2022/12/05 20:12:00 afresh1 Exp $ +# $OpenBSD: install.sub,v 1.1216 2022/12/16 04:04:25 afresh1 Exp $ # # Copyright (c) 1997-2015 Todd Miller, Theo de Raadt, Ken Westerback # Copyright (c) 2015, Robert Peichaer @@ -356,6 +356,15 @@ get_ifs() { done } +# Map an interface to its MAC address if it is unique +if_name_to_lladdr() { + local _lladdr + + _lladdr=$(ifconfig $1 2>/dev/null | + sed -n 's/^[[:space:]]*lladdr[[:space:]]//p') + [[ -n $_lladdr && -n $(ifconfig -M "$_lladdr") ]] && echo $_lladdr +} + # Return the device name of the disk device $1, which may be a disklabel UID. get_dkdev_name() { local _dev=${1#/dev/} _d @@ -2428,13 +2437,18 @@ parse_hn_line() { # Start interface using the on-disk hostname.if file passed as argument $1. # Much of this is gratuitously stolen from /etc/netstart. ifstart() { - local _if=$1 _hn=/mnt/etc/hostname.$1 _cmds _i=0 _line + local _if=$1 _lladdr _hn=/mnt/etc/hostname.$1 _cmds _i=0 _line set -A _cmds - if [[ $_if == ??:??:??:??:??:?? ]]; then - _if=$(ifconfig -M $_if) - [[ -z $_if ]] && return # invalid interface - [[ -e /mnt/etc/hostname.$_if ]] && return # duplicate config + if [[ $_if == +([[:alpha:]])+([[:digit:]]) ]]; then + _lladdr=$(if_name_to_lladdr $_if) + [[ -n $_lladdr && -f /mnt/etc/hostname.$_lladdr ]] && return + elif [[ $_if == ??:??:??:??:??:?? ]]; then + _lladdr=$_if + _if=$(ifconfig -M $_lladdr) + [[ -z $_if ]] && return + else + return fi # Create interface if it does not yet exist. diff --git a/etc/netstart b/etc/netstart index 7c4ab2b606d..8377f85ac7c 100644 --- a/etc/netstart +++ b/etc/netstart @@ -1,6 +1,6 @@ #!/bin/sh - # -# $OpenBSD: netstart,v 1.230 2022/12/05 20:12:00 afresh1 Exp $ +# $OpenBSD: netstart,v 1.231 2022/12/16 04:04:25 afresh1 Exp $ # Turn off Strict Bourne shell mode. set +o sh @@ -132,28 +132,33 @@ vifscreate() { # Start a single interface. # Usage: ifstart if1 ifstart() { - local _if=$1 _hn=/etc/hostname.$1 _cmds _i=0 _line _stat + local _if=$1 _lladdr _hn=/etc/hostname.$1 _cmds _i=0 _line _stat set -A _cmds - if [[ $_if == ??:??:??:??:??:?? ]]; then - if ! _line=$( ifconfig -M $_if ); then - print -u2 "${0##*/}: $_if is not unique." + # Interface names must be alphanumeric only. We check to avoid + # configuring backup or temp files, and to catch the "*" case. + if [[ $_if == +([[:alpha:]])+([[:digit:]]) ]]; then + _lladdr=$(ifconfig $_if 2>/dev/null | + sed -n 's/^[[:space:]]*lladdr[[:space:]]//p') + if [[ -n $_lladdr && -f /etc/hostname.$_lladdr && \ + -n $(ifconfig -M "$_lladdr") ]]; then + print -u2 "${0##*/}: $_hn: /etc/hostname.$_lladdr overrides" return fi - [[ -z $_line ]] && return - _if=$_line - _line= - - if [[ -e /etc/hostname.$_if ]]; then - print -u2 "${0##*/}: $_hn: /etc/hostname.$_if overrides" + # We also support hostname.lladdr, but it must resolve to be valid + elif [[ $_if == ??:??:??:??:??:?? ]]; then + _lladdr=$_if + _if=$(ifconfig -M $_lladdr) + if (($? != 0)); then + print -u2 "${0##*/}: $_lladdr is not unique." return fi - fi - # Interface names must be alphanumeric only. We check to avoid - # configuring backup or temp files, and to catch the "*" case. - [[ $_if != +([[:alpha:]])+([[:digit:]]) ]] && return + [[ -z $_if ]] && return + else + return + fi if [[ ! -f $_hn ]]; then print -u2 "${0##*/}: $_hn: No such file or directory." diff --git a/share/man/man5/hostname.if.5 b/share/man/man5/hostname.if.5 index a999ff49e33..9505d6be29e 100644 --- a/share/man/man5/hostname.if.5 +++ b/share/man/man5/hostname.if.5 @@ -1,4 +1,4 @@ -.\" $OpenBSD: hostname.if.5,v 1.81 2022/12/15 06:39:05 jmc Exp $ +.\" $OpenBSD: hostname.if.5,v 1.82 2022/12/16 04:04:25 afresh1 Exp $ .\" $NetBSD: hosts.5,v 1.4 1994/11/30 19:31:20 jtc Exp $ .\" .\" Copyright (c) 1983, 1991, 1993 @@ -30,7 +30,7 @@ .\" .\" @(#)hosts.5 8.2 (Berkeley) 12/11/93 .\" -.Dd $Mdocdate: December 15 2022 $ +.Dd $Mdocdate: December 16 2022 $ .Dt HOSTNAME.IF 5 .Os .Sh NAME @@ -49,7 +49,7 @@ so interfaces can alternatively be referenced by their link layer address (lladdr), such as .Dq hostname.00:00:5e:00:53:af . -Priority is given to configuration by interface name/unit over lladdr. +Priority is given to configuration by interface lladdr over name/unit. A configuration file is not needed for lo0. .Pp The configuration information is expressed in a line-by-line packed format