xref: /openbsd/regress/usr.bin/ssh/cfginclude.sh (revision 8a4d411d)
1*8a4d411dSdjm#	$OpenBSD: cfginclude.sh,v 1.5 2024/09/27 01:05:54 djm Exp $
23ecffd51Sdjm#	Placed in the Public Domain.
33ecffd51Sdjm
43ecffd51Sdjmtid="config include"
53ecffd51Sdjm
6ec83c022Sdtucker# to appease StrictModes
7ec83c022Sdtuckerumask 022
8ec83c022Sdtucker
93ecffd51Sdjmcat > $OBJ/ssh_config.i << _EOF
103ecffd51SdjmMatch host a
113ecffd51Sdjm	Hostname aa
123ecffd51Sdjm
13*8a4d411dSdjmMatch host=b # comment
143ecffd51Sdjm	Hostname bb
153ecffd51Sdjm	Include $OBJ/ssh_config.i.*
163ecffd51Sdjm
173ecffd51SdjmMatch host c
183ecffd51Sdjm	Include $OBJ/ssh_config.i.*
193ecffd51Sdjm	Hostname cc
203ecffd51Sdjm
21*8a4d411dSdjmMatch host=m !user xxxyfake
22f93cb3deSdjm	Include $OBJ/ssh_config.i.* # comment
233ecffd51Sdjm
243ecffd51SdjmHost d
25f93cb3deSdjm	Hostname dd # comment
263ecffd51Sdjm
273ecffd51SdjmHost e
283ecffd51Sdjm	Hostname ee
293ecffd51Sdjm	Include $OBJ/ssh_config.i.*
303ecffd51Sdjm
313ecffd51SdjmHost f
323ecffd51Sdjm	Include $OBJ/ssh_config.i.*
333ecffd51Sdjm	Hostname ff
343ecffd51Sdjm
353ecffd51SdjmHost n
363ecffd51Sdjm	Include $OBJ/ssh_config.i.*
373ecffd51Sdjm_EOF
383ecffd51Sdjm
393ecffd51Sdjmcat > $OBJ/ssh_config.i.0 << _EOF
403ecffd51SdjmMatch host xxxxxx
413ecffd51Sdjm_EOF
423ecffd51Sdjm
433ecffd51Sdjmcat > $OBJ/ssh_config.i.1 << _EOF
44*8a4d411dSdjmMatch host=a
453ecffd51Sdjm	Hostname aaa
463ecffd51Sdjm
473ecffd51SdjmMatch host b
483ecffd51Sdjm	Hostname bbb
493ecffd51Sdjm
50f93cb3deSdjmMatch host c # comment
513ecffd51Sdjm	Hostname ccc
523ecffd51Sdjm
53f93cb3deSdjmHost d # comment
543ecffd51Sdjm	Hostname ddd
553ecffd51Sdjm
563ecffd51SdjmHost e
573ecffd51Sdjm	Hostname eee
583ecffd51Sdjm
593ecffd51SdjmHost f
60f93cb3deSdjm	Hostname fff # comment
613ecffd51Sdjm_EOF
623ecffd51Sdjm
633ecffd51Sdjmcat > $OBJ/ssh_config.i.2 << _EOF
643ecffd51SdjmMatch host a
653ecffd51Sdjm	Hostname aaaa
663ecffd51Sdjm
67*8a4d411dSdjmMatch host=b !user blahblahfake
683ecffd51Sdjm	Hostname bbbb
693ecffd51Sdjm
70*8a4d411dSdjmMatch host=c
713ecffd51Sdjm	Hostname cccc
723ecffd51Sdjm
733ecffd51SdjmHost d
743ecffd51Sdjm	Hostname dddd
753ecffd51Sdjm
763ecffd51SdjmHost e
773ecffd51Sdjm	Hostname eeee
783ecffd51Sdjm
793ecffd51SdjmHost f
803ecffd51Sdjm	Hostname ffff
813ecffd51Sdjm
823ecffd51SdjmMatch all
833ecffd51Sdjm	Hostname xxxx
843ecffd51Sdjm_EOF
853ecffd51Sdjm
863ecffd51Sdjmtrial() {
873ecffd51Sdjm	_host="$1"
883ecffd51Sdjm	_exp="$2"
893ecffd51Sdjm	${REAL_SSH} -F $OBJ/ssh_config.i -G "$_host" > $OBJ/ssh_config.out ||
903ecffd51Sdjm		fatal "ssh config parse failed"
913ecffd51Sdjm	_got=`grep -i '^hostname ' $OBJ/ssh_config.out | awk '{print $2}'`
923ecffd51Sdjm	if test "x$_exp" != "x$_got" ; then
933ecffd51Sdjm		fail "host $_host include fail: expected $_exp got $_got"
943ecffd51Sdjm	fi
953ecffd51Sdjm}
963ecffd51Sdjm
973ecffd51Sdjmtrial a aa
983ecffd51Sdjmtrial b bb
993ecffd51Sdjmtrial c ccc
1003ecffd51Sdjmtrial d dd
1013ecffd51Sdjmtrial e ee
1023ecffd51Sdjmtrial f fff
1033ecffd51Sdjmtrial m xxxx
1043ecffd51Sdjmtrial n xxxx
1053ecffd51Sdjmtrial x x
1063ecffd51Sdjm
1073ecffd51Sdjm# Prepare an included config with an error.
1083ecffd51Sdjm
1093ecffd51Sdjmcat > $OBJ/ssh_config.i.3 << _EOF
1103ecffd51SdjmHostname xxxx
1113ecffd51Sdjm	Junk
1123ecffd51Sdjm_EOF
1133ecffd51Sdjm
1143ecffd51Sdjm${REAL_SSH} -F $OBJ/ssh_config.i -G a 2>/dev/null && \
1153ecffd51Sdjm	fail "ssh include allowed invalid config"
1163ecffd51Sdjm
1173ecffd51Sdjm${REAL_SSH} -F $OBJ/ssh_config.i -G x 2>/dev/null && \
1183ecffd51Sdjm	fail "ssh include allowed invalid config"
1193ecffd51Sdjm
1203ecffd51Sdjmrm -f $OBJ/ssh_config.i.*
1213ecffd51Sdjm
1223ecffd51Sdjm# Ensure that a missing include is not fatal.
1233ecffd51Sdjmcat > $OBJ/ssh_config.i << _EOF
1243ecffd51SdjmInclude $OBJ/ssh_config.i.*
1253ecffd51SdjmHostname aa
1263ecffd51Sdjm_EOF
1273ecffd51Sdjm
1283ecffd51Sdjmtrial a aa
1293ecffd51Sdjm
1303ecffd51Sdjm# Ensure that Match/Host in an included config does not affect parent.
1313ecffd51Sdjmcat > $OBJ/ssh_config.i.x << _EOF
1323ecffd51SdjmMatch host x
1333ecffd51Sdjm_EOF
1343ecffd51Sdjm
1353ecffd51Sdjmtrial a aa
1363ecffd51Sdjm
1373ecffd51Sdjmcat > $OBJ/ssh_config.i.x << _EOF
1383ecffd51SdjmHost x
1393ecffd51Sdjm_EOF
1403ecffd51Sdjm
1413ecffd51Sdjmtrial a aa
1423ecffd51Sdjm
1433ecffd51Sdjm# cleanup
1443ecffd51Sdjmrm -f $OBJ/ssh_config.i $OBJ/ssh_config.i.* $OBJ/ssh_config.out
145*8a4d411dSdjm#	$OpenBSD: cfginclude.sh,v 1.5 2024/09/27 01:05:54 djm Exp $
1463ecffd51Sdjm#	Placed in the Public Domain.
1473ecffd51Sdjm
1483ecffd51Sdjmtid="config include"
1493ecffd51Sdjm
1503ecffd51Sdjmcat > $OBJ/ssh_config.i << _EOF
1513ecffd51SdjmMatch host a
1523ecffd51Sdjm	Hostname aa
1533ecffd51Sdjm
1543ecffd51SdjmMatch host b
1553ecffd51Sdjm	Hostname bb
1563ecffd51Sdjm	Include $OBJ/ssh_config.i.*
1573ecffd51Sdjm
1583ecffd51SdjmMatch host c
1593ecffd51Sdjm	Include $OBJ/ssh_config.i.*
1603ecffd51Sdjm	Hostname cc
1613ecffd51Sdjm
1623ecffd51SdjmMatch host m
1633ecffd51Sdjm	Include $OBJ/ssh_config.i.*
1643ecffd51Sdjm
1653ecffd51SdjmHost d
1663ecffd51Sdjm	Hostname dd
1673ecffd51Sdjm
1683ecffd51SdjmHost e
1693ecffd51Sdjm	Hostname ee
1703ecffd51Sdjm	Include $OBJ/ssh_config.i.*
1713ecffd51Sdjm
1723ecffd51SdjmHost f
1733ecffd51Sdjm	Include $OBJ/ssh_config.i.*
1743ecffd51Sdjm	Hostname ff
1753ecffd51Sdjm
1763ecffd51SdjmHost n
1773ecffd51Sdjm	Include $OBJ/ssh_config.i.*
1783ecffd51Sdjm_EOF
1793ecffd51Sdjm
1803ecffd51Sdjmcat > $OBJ/ssh_config.i.0 << _EOF
1813ecffd51SdjmMatch host xxxxxx
1823ecffd51Sdjm_EOF
1833ecffd51Sdjm
1843ecffd51Sdjmcat > $OBJ/ssh_config.i.1 << _EOF
1853ecffd51SdjmMatch host a
1863ecffd51Sdjm	Hostname aaa
1873ecffd51Sdjm
188f93cb3deSdjmMatch host b # comment
1893ecffd51Sdjm	Hostname bbb
1903ecffd51Sdjm
1913ecffd51SdjmMatch host c
192f93cb3deSdjm	Hostname ccc # comment
1933ecffd51Sdjm
1943ecffd51SdjmHost d
1953ecffd51Sdjm	Hostname ddd
1963ecffd51Sdjm
1973ecffd51SdjmHost e
1983ecffd51Sdjm	Hostname eee
1993ecffd51Sdjm
2003ecffd51SdjmHost f
2013ecffd51Sdjm	Hostname fff
2023ecffd51Sdjm_EOF
2033ecffd51Sdjm
2043ecffd51Sdjmcat > $OBJ/ssh_config.i.2 << _EOF
2053ecffd51SdjmMatch host a
2063ecffd51Sdjm	Hostname aaaa
2073ecffd51Sdjm
2083ecffd51SdjmMatch host b
2093ecffd51Sdjm	Hostname bbbb
2103ecffd51Sdjm
2113ecffd51SdjmMatch host c
2123ecffd51Sdjm	Hostname cccc
2133ecffd51Sdjm
2143ecffd51SdjmHost d
2153ecffd51Sdjm	Hostname dddd
2163ecffd51Sdjm
2173ecffd51SdjmHost e
2183ecffd51Sdjm	Hostname eeee
2193ecffd51Sdjm
2203ecffd51SdjmHost f
2213ecffd51Sdjm	Hostname ffff
2223ecffd51Sdjm
223f93cb3deSdjmMatch all # comment
224f93cb3deSdjm	Hostname xxxx # comment
2253ecffd51Sdjm_EOF
2263ecffd51Sdjm
2273ecffd51Sdjmtrial() {
2283ecffd51Sdjm	_host="$1"
2293ecffd51Sdjm	_exp="$2"
2303ecffd51Sdjm	${REAL_SSH} -F $OBJ/ssh_config.i -G "$_host" > $OBJ/ssh_config.out ||
2313ecffd51Sdjm		fatal "ssh config parse failed"
2323ecffd51Sdjm	_got=`grep -i '^hostname ' $OBJ/ssh_config.out | awk '{print $2}'`
2333ecffd51Sdjm	if test "x$_exp" != "x$_got" ; then
2343ecffd51Sdjm		fail "host $_host include fail: expected $_exp got $_got"
2353ecffd51Sdjm	fi
2363ecffd51Sdjm}
2373ecffd51Sdjm
2383ecffd51Sdjmtrial a aa
2393ecffd51Sdjmtrial b bb
2403ecffd51Sdjmtrial c ccc
2413ecffd51Sdjmtrial d dd
2423ecffd51Sdjmtrial e ee
2433ecffd51Sdjmtrial f fff
2443ecffd51Sdjmtrial m xxxx
2453ecffd51Sdjmtrial n xxxx
2463ecffd51Sdjmtrial x x
2473ecffd51Sdjm
2483ecffd51Sdjm# Prepare an included config with an error.
2493ecffd51Sdjm
2503ecffd51Sdjmcat > $OBJ/ssh_config.i.3 << _EOF
2513ecffd51SdjmHostname xxxx
2523ecffd51Sdjm	Junk
2533ecffd51Sdjm_EOF
2543ecffd51Sdjm
2553ecffd51Sdjm${REAL_SSH} -F $OBJ/ssh_config.i -G a 2>/dev/null && \
2563ecffd51Sdjm	fail "ssh include allowed invalid config"
2573ecffd51Sdjm
2583ecffd51Sdjm${REAL_SSH} -F $OBJ/ssh_config.i -G x 2>/dev/null && \
2593ecffd51Sdjm	fail "ssh include allowed invalid config"
2603ecffd51Sdjm
2613ecffd51Sdjmrm -f $OBJ/ssh_config.i.*
2623ecffd51Sdjm
2633ecffd51Sdjm# Ensure that a missing include is not fatal.
2643ecffd51Sdjmcat > $OBJ/ssh_config.i << _EOF
2653ecffd51SdjmInclude $OBJ/ssh_config.i.*
2663ecffd51SdjmHostname aa
2673ecffd51Sdjm_EOF
2683ecffd51Sdjm
2693ecffd51Sdjmtrial a aa
2703ecffd51Sdjm
2713ecffd51Sdjm# Ensure that Match/Host in an included config does not affect parent.
2723ecffd51Sdjmcat > $OBJ/ssh_config.i.x << _EOF
2733ecffd51SdjmMatch host x
2743ecffd51Sdjm_EOF
2753ecffd51Sdjm
2763ecffd51Sdjmtrial a aa
2773ecffd51Sdjm
2783ecffd51Sdjmcat > $OBJ/ssh_config.i.x << _EOF
2793ecffd51SdjmHost x
2803ecffd51Sdjm_EOF
2813ecffd51Sdjm
2823ecffd51Sdjmtrial a aa
2833ecffd51Sdjm
2843ecffd51Sdjm# Ensure that recursive includes are bounded.
2853ecffd51Sdjmcat > $OBJ/ssh_config.i << _EOF
2863ecffd51SdjmInclude $OBJ/ssh_config.i
2873ecffd51Sdjm_EOF
2883ecffd51Sdjm
2893ecffd51Sdjm${REAL_SSH} -F $OBJ/ssh_config.i -G a 2>/dev/null && \
2903ecffd51Sdjm	fail "ssh include allowed infinite recursion?" # or hang...
2913ecffd51Sdjm
29267ce4081Sdjm# Environment variable expansion
29367ce4081Sdjmcat > $OBJ/ssh_config.i << _EOF
29467ce4081SdjmInclude $OBJ/ssh_config.\${REAL_FILE}
29567ce4081Sdjm_EOF
29667ce4081Sdjmcat > $OBJ/ssh_config.i.x << _EOF
29767ce4081SdjmHostname xyzzy
29867ce4081Sdjm_EOF
29967ce4081SdjmREAL_FILE=i.x
30067ce4081Sdjmexport REAL_FILE
30167ce4081Sdjmtrial a xyzzy
30267ce4081Sdjm
30367ce4081Sdjm# Environment variable expansion
30467ce4081Sdjmcat > $OBJ/ssh_config.i << _EOF
30567ce4081SdjmInclude $OBJ/ssh_config.i.%h%h
30667ce4081Sdjm_EOF
30767ce4081Sdjmcat > $OBJ/ssh_config.i.blahblah << _EOF
30867ce4081SdjmHostname mekmitastdigoat
30967ce4081Sdjm_EOF
31067ce4081SdjmREAL_FILE=i.x
31167ce4081Sdjmexport REAL_FILE
31267ce4081Sdjmtrial blah mekmitastdigoat
31367ce4081Sdjm
3143ecffd51Sdjm# cleanup
3153ecffd51Sdjmrm -f $OBJ/ssh_config.i $OBJ/ssh_config.i.* $OBJ/ssh_config.out
316