Skip to content

Commit 74e5bc3

Browse files
committed
Add a new exlcude option to exclude a whole file on match
Has a similar syntax to replace. While here add a new -L option to list the resolv.conf files we have and apply post-processing so we can easily test. Also while here implement a poor mans cat. This means our needs and avoids a fork. This is fine as cat may exist in /usr on some platforms which may not be available in early boot for us and the files we do read shouldn't be many lines long.
1 parent 5379489 commit 74e5bc3

File tree

4 files changed

+130
-21
lines changed

4 files changed

+130
-21
lines changed

Diff for: libc.in

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
2-
# Copyright (c) 2007-2023 Roy Marples
2+
# Copyright (c) 2007-2025 Roy Marples
33
# All rights reserved
44

55
# libc subscriber for resolvconf
@@ -96,7 +96,7 @@ fi
9696
: ${resolv_conf:=/etc/resolv.conf}
9797
: ${resolv_conf_tmp:="$resolv_conf.$$.openresolv"}
9898
: ${libc_service:=nscd}
99-
: ${list_resolv:=@SBINDIR@/resolvconf -l}
99+
: ${list_resolv:=@SBINDIR@/resolvconf -L}
100100
if [ "${resolv_conf_head-x}" = x ] && [ -f "$SYSCONFDIR"/resolv.conf.head ]
101101
then
102102
resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.head)"

Diff for: resolvconf.8.in

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" Copyright (c) 2007-2023 Roy Marples
1+
.\" Copyright (c) 2007-2025 Roy Marples
22
.\" All rights reserved
33
.\"
44
.\" Redistribution and use in source and binary forms, with or without
@@ -22,7 +22,7 @@
2222
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2323
.\" SUCH DAMAGE.
2424
.\"
25-
.Dd December 23, 2016
25+
.Dd March 24, 2025
2626
.Dt RESOLVCONF 8
2727
.Os
2828
.Sh NAME
@@ -46,7 +46,7 @@
4646
.Fl d Ar interface Ns Op Ar .protocol
4747
.Nm
4848
.Op Fl x
49-
.Fl il Ar pattern
49+
.Fl iLl Ar pattern
5050
.Nm
5151
.Fl u
5252
.Nm
@@ -168,6 +168,17 @@ List the interfaces and protocols, optionally matching
168168
we have
169169
.Pa resolv.conf
170170
files for.
171+
.It Fl L Ar pattern
172+
List the
173+
.Pa resolv.conf
174+
files we have,
175+
post-processed by the
176+
.Xr resolvconf.conf 5
177+
configuration.
178+
If
179+
.Ar pattern
180+
is specified then we list the files for the interfaces and protocols
181+
that match it.
171182
.It Fl l Ar pattern
172183
List the
173184
.Pa resolv.conf

Diff for: resolvconf.conf.5.in

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" Copyright (c) 2009-2023 Roy Marples
1+
.\" Copyright (c) 2009-2025 Roy Marples
22
.\" All rights reserved
33
.\"
44
.\" Redistribution and use in source and binary forms, with or without
@@ -22,7 +22,7 @@
2222
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2323
.\" SUCH DAMAGE.
2424
.\"
25-
.Dd Aug 02, 2024
25+
.Dd March 24, 2025
2626
.Dt RESOLVCONF.CONF 5
2727
.Os
2828
.Sh NAME
@@ -65,6 +65,19 @@ Defaults to YES.
6565
If set, only these interfaces will be processed.
6666
.It Sy deny_interfaces
6767
If set, these interfaces will not be processed.
68+
.It Sy exclude
69+
Is a space separated list of key/value pairs to match.
70+
If all key/value pairs in one element can be found in the file,
71+
then the whole file will be excluded from processing.
72+
The syntax is this:
73+
.Va $keyword Ns / Ns Va $match Ns Op / Ns Va $keyword Ns / Ns Va $match
74+
.Pp
75+
For example given this configuration:
76+
.Bd -compact -literal -offset indent
77+
exclude="search/foo*/nameserver/1.2.3.4 search/bar.org"
78+
.Ed
79+
Then any resolv.conf with both a search option starting with foo with a nameserver of 1.2.3.4
80+
OR a search option of bar.org would be excluded.
6881
.It Sy interface_order
6982
These interfaces will always be processed first.
7083
If unset, defaults to the following:-

Diff for: resolvconf.in

+99-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh
2-
# Copyright (c) 2007-2023 Roy Marples
2+
# Copyright (c) 2007-2025 Roy Marples
33
# All rights reserved
44

55
# Redistribution and use in source and binary forms, with or without
@@ -25,7 +25,7 @@
2525
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2626

2727
RESOLVCONF="$0"
28-
OPENRESOLV_VERSION="3.13.2"
28+
OPENRESOLV_VERSION="3.14.0"
2929
SYSCONFDIR=@SYSCONFDIR@
3030
LIBEXECDIR=@LIBEXECDIR@
3131
VARDIR=@VARDIR@
@@ -34,7 +34,7 @@ RESTARTCMD=@RESTARTCMD@
3434

3535
if [ "$1" = "--version" ]; then
3636
echo "openresolv $OPENRESOLV_VERSION"
37-
echo "Copyright (c) 2007-2020 Roy Marples"
37+
echo "Copyright (c) 2007-2025 Roy Marples"
3838
exit 0
3939
fi
4040

@@ -48,6 +48,27 @@ dynamic_order="tap[0-9]* tun[0-9]* vpn vpn[0-9]* wg[0-9]* ppp[0-9]* ippp[0-9]*"
4848
interface_order="lo lo[0-9]*"
4949
name_server_blacklist="0.0.0.0"
5050

51+
# Poor mans cat
52+
# /usr might not be available
53+
cat()
54+
{
55+
OIFS="$IFS"
56+
IFS=''
57+
if [ -n "$1" ]; then
58+
while read -r line; do
59+
printf "%s\n" "$line"
60+
done < "$1"
61+
else
62+
while read -r line; do
63+
printf "%s\n" "$line"
64+
done
65+
fi
66+
retval=$?
67+
IFS="$OIFS"
68+
return $retval
69+
}
70+
71+
5172
# Support original resolvconf configuration layout
5273
# as well as the openresolv config file
5374
if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
@@ -98,6 +119,7 @@ usage()
98119
pattern
99120
-l [\$PATTERN] Show DNS information, optionally from interfaces
100121
that match the specified pattern
122+
-L [\$PATTERN] Same as -l, but adjusted by our config
101123
102124
-u Run updates from our current DNS information
103125
--version Echo the ${RESOLVCONF##*/} version
@@ -434,12 +456,68 @@ deprecated_interface()
434456
return 1
435457
}
436458

459+
match()
460+
{
461+
match="$1"
462+
file="$2"
463+
retval=1
464+
count=0
465+
466+
while read -r keyword value; do
467+
new_match=
468+
for om in $match; do
469+
m="$om"
470+
keep=
471+
while [ -n "$m" ]; do
472+
k="${m%%/*}"
473+
r="${m#*/}"
474+
f="${r%%/*}"
475+
r="${r#*/}"
476+
# If the length of m is the same as k/f then
477+
# we know that we are done
478+
if [ ${#m} = $((${#k} + 1 + ${#f})) ]; then
479+
r=
480+
fi
481+
m="$r"
482+
matched=false
483+
case "$keyword" in
484+
$k)
485+
case "$value" in
486+
$f)
487+
matched=true
488+
;;
489+
esac
490+
;;
491+
esac
492+
if ! $matched; then
493+
keep="$keep${keep:+/}$k/$f"
494+
fi
495+
done
496+
if [ -n "$om" ] && [ -z "$keep" ]; then
497+
retval=0
498+
break 2
499+
fi
500+
new_match="${new_match}${new_match:+ }${keep}"
501+
done
502+
match="${new_match}"
503+
done < "$file"
504+
return $retval
505+
}
506+
437507
list_resolv()
438508
{
439509
[ -d "$IFACEDIR" ] || return 0
440510

441-
cmd="$1"
442-
shift
511+
OPTIND=
512+
list_cmd=
513+
while getopts iLl OPT; do
514+
case "$OPT" in
515+
'?') exit 1;;
516+
*) list_cmd="$OPT";;
517+
esac
518+
done
519+
shift $(($OPTIND - 1))
520+
443521
pattern_specified="$1"
444522

445523
excl=false
@@ -540,7 +618,7 @@ list_resolv()
540618
continue
541619
fi
542620

543-
if ! $ALLIFACES; then
621+
if [ "$list_cmd" = L ]; then
544622
if [ -n "$allow_interfaces" ]; then
545623
x=false
546624
for j in $allow_interfaces; do
@@ -555,16 +633,20 @@ list_resolv()
555633
continue 2
556634
fi
557635
done
636+
637+
if [ -n "$exclude" ] && match "$exclude" "$i"; then
638+
continue
639+
fi
558640
fi
559641

560-
if [ "$cmd" = i ] || [ "$cmd" = "-i" ]; then
642+
if [ "$list_cmd" = i ]; then
561643
printf %s "$i "
562644
else
563645
echo_resolv "$i" && echo
564646
fi
565647
[ $? = 0 ] && [ "$retval" = 1 ] && retval=0
566648
done
567-
[ "$cmd" = i ] || [ "$cmd" = "-i" ] && echo
649+
[ "$list_cmd" = i ] && echo
568650
return $retval
569651
}
570652

@@ -674,7 +756,7 @@ make_vars()
674756
if [ -z "$VFLAG" ]; then
675757
IF_EXCLUSIVE=1
676758
list_resolv -i "$@" >/dev/null || IF_EXCLUSIVE=0
677-
eval "$(list_resolv -l "$@" | replace | parse_resolv)"
759+
eval "$(list_resolv -L "$@" | replace | parse_resolv)"
678760
fi
679761
if [ -n "${name_servers_append}${search_domains_append}" ]; then
680762
eval "$(echo_append | parse_resolv)"
@@ -724,7 +806,7 @@ make_vars()
724806

725807
force=false
726808
VFLAG=
727-
while getopts a:C:c:Dd:fhIilm:pRruvVx OPT; do
809+
while getopts a:C:c:Dd:fhIiLlm:pRruvVx OPT; do
728810
case "$OPT" in
729811
f) force=true;;
730812
h) usage;;
@@ -762,11 +844,14 @@ fi
762844

763845
# -l lists our resolv files, optionally for a specific interface
764846
if [ "$cmd" = l ] || [ "$cmd" = i ]; then
765-
ALLIFACES=true
766-
list_resolv "$cmd" "$args"
847+
list_resolv "-$cmd" "$args"
767848
exit $?
768849
fi
769-
ALLIFACES=false
850+
# -L is the same as -l, but post-processed from our config
851+
if [ "$cmd" = L ]; then
852+
list_resolv "-$cmd" "$args" | replace
853+
exit $?2
854+
fi
770855

771856
# Restart a service or echo the command to restart a service
772857
if [ "$cmd" = r ] || [ "$cmd" = R ]; then
@@ -1077,7 +1162,7 @@ export RESTARTCMD RCDIR _NOINIT_WARNED
10771162

10781163
eval "$(make_vars)"
10791164
export RESOLVCONF DOMAINS SEARCH NAMESERVERS LOCALNAMESERVERS
1080-
: ${list_resolv:=list_resolv -l}
1165+
: ${list_resolv:=list_resolv -L}
10811166
retval=0
10821167

10831168
# Run scripts in the same directory resolvconf is run from

0 commit comments

Comments
 (0)