1# Assumptions:
2# We're not running with uid 0 or 1.
3# We're not running with gid 0 or 2.
4# supervise is installed.
5# The DNS cache translates 127.0.0.1<->localhost.
6# There is no listener for TCP port 16.
7# There is no listener for TCP port 50016.
8# There is no use of TCP ports 50017, 50018, 50019, 50020.
9
10
11rm -rf rts-tmp
12mkdir rts-tmp
13cd rts-tmp
14
15
16echo '#!/bin/sh
17  trap "" 13
18  echo PROTO="$PROTO"
19  echo TCPLOCALHOST="${TCPLOCALHOST-unset}"
20  echo TCPLOCALIP="${TCPLOCALIP-unset}"
21  echo TCPLOCALPORT="${TCPLOCALPORT-unset}"
22  echo TCPREMOTEHOST="${TCPREMOTEHOST-unset}"
23  echo TCPREMOTEIP="${TCPREMOTEIP-unset}"
24  echo TCPREMOTEPORT="${TCPREMOTEPORT-unset}"
25  echo TCPREMOTEINFO="${TCPREMOTEINFO-unset}"
26' > print
27chmod 755 print
28
29mkdir 50016
30echo '#!/bin/sh
31exec tcpserver \
32-c 2 -Bbanner -vo -D -1 -Xx rules.cdb -Rt5 -hp -l Local -b 2 \
33127.0.0.1 50016 ../print
34' > 50016/run
35chmod 755 50016/run
36
37supervise 50016 >log 2>&1 &
38
39echo '--- tcpclient prints usage message without enough arguments'
40tcpclient 0 0; echo $?
41
42echo '--- tcpclient prints error message with unknown port name'
43tcpclient 0 nonexistentport echo wrong; echo $?
44
45echo '--- tcpclient prints error message when connection fails'
46tcpclient 0 016 echo wrong; echo $?
47
48echo '--- tcpclient -q does not print error message when connection fails'
49tcpclient -q 0 016 echo wrong; echo $?
50
51echo '--- tcpclient understands empty host name as synonym for 0'
52tcpclient '' 016 echo wrong; echo $?
53
54echo '--- tcpclient understands unbracketed IP address'
55tcpclient '127.000.000.001' 016 echo wrong; echo $?
56
57echo '--- tcpclient understands bracketed IP address'
58tcpclient '[127.000.000.001]' 016 echo wrong; echo $?
59
60echo '--- tcpclient prints error message with unknown host name'
61tcpclient nonexistent.local. 016 echo wrong; echo $?
62
63echo '--- tcpclient prints error message with unresolvable host name'
64tcpclient thislabelistoolongbecausednshasalimitof63charactersinasinglelabel. 016 echo wrong; echo $?
65
66echo '--- tcpserver prints usage message without enough arguments'
67tcpserver 0 0; echo $?
68
69echo '--- tcpserver -u 1 attempts to set uid to 1'
70tcpserver -u 1 0 0 echo wrong; echo $?
71
72echo '--- tcpserver -U reads $UID'
73env UID=1 tcpserver -U 0 0 echo wrong; echo $?
74
75echo '--- tcpserver -g 2 attempts to set gid to 2'
76tcpserver -g 2 0 0 echo wrong; echo $?
77
78echo '--- tcpserver -U reads $GID'
79env GID=2 tcpserver -U 0 0 echo wrong; echo $?
80
81echo '--- tcpserver prints error message with unknown port name'
82tcpserver 0 nonexistentport echo wrong; echo $?
83
84echo '--- tcpserver prints error message with unknown host name'
85tcpserver nonexistent.local. 016 echo wrong; echo $?
86
87echo '--- tcpserver prints error message with unresolvable host name'
88tcpserver thislabelistoolongbecausednshasalimitof63charactersinasinglelabel. 016 echo wrong; echo $?
89
90echo '--- tcpserver prints error message with non-local host name'
91tcpserver 1.2.3.4 016 echo wrong; echo $?
92
93echo '--- tcpserver sets basic environment variables'
94tcpclient -p 50017 -R -H -T 10 -l Local 0 50016 sh -c 'cat <&6'
95echo $?
96
97echo '--- tcpclient recognizes -D, -i, -r, -h, -t'
98tcpclient -Di 127.0.0.1 -p 50018 -hrt1 -l Local \
99127.0.0.1 50016 sh -c 'cat <&6'
100echo $?
101
102echo '--- tcpclient sets basic environment variables'
103tcpclient -p 50019 -R -H -l Local 0 50016 ./print
104echo $?
105
106echo '--- tcpclient looks up host names properly'
107tcpclient -p 50020 -R 0 50016 ./print
108echo $?
109
110echo '--- tcpclient -v works'
111tcpclient -v -R -H -l Local 0 50016 echo ok
112echo $?
113
114echo '--- tcpserver prints error message with used port'
115tcpserver -R -H -l Local 127.0.0.1 50016 echo wrong
116echo $?
117
118echo '--- tcpcat works'
119tcpcat 0 50016 | grep -v TCPREMOTEPORT
120echo $?
121
122echo '--- mconnect works'
123mconnect 0 50016 </dev/null | grep -v TCPREMOTEPORT
124echo $?
125
126echo '--- tcprules prints usage message without enough arguments'
127tcprules test.cdb; echo $?
128
129echo '--- tcprules prints error message if it cannot create tmp'
130echo 1.2.3.4:deny | tcprules test.cdb /dev/nonexistent/tmp; echo $?
131
132echo '--- tcprules prints error message if it cannot move tmp to cdb'
133echo 1.2.3.4:deny | tcprules /dev/nonexistent/cdb test.tmp; echo $?
134
135echo '--- tcprules creates a cdb file'
136echo 1.2.3.4:deny | tcprules test.cdb test.tmp; echo $?
137
138echo '--- tcprulescheck sees deny'
139env TCPREMOTEIP=1.2.3.4 tcprulescheck test.cdb; echo $?
140
141echo '--- tcprulescheck does not apply deny to another host'
142env TCPREMOTEIP=1.2.3.5 tcprulescheck test.cdb; echo $?
143
144echo '--- tcprules replaces a cdb file'
145echo 'joe@127.0.0.1:allow,which=/first/
14618.23.0.32:allow,which="second"
147127.:allow,which=+third+
148:allow,which==fourth=' | tcprules test.cdb test.tmp; echo $?
149
150echo '--- tcprulescheck finds rule with address and info'
151env TCPREMOTEIP=127.0.0.1 TCPREMOTEINFO=joe tcprulescheck test.cdb; echo $?
152
153echo '--- tcprulescheck finds rule with address'
154env TCPREMOTEIP=18.23.0.32 TCPREMOTEINFO=joe tcprulescheck test.cdb; echo $?
155
156echo '--- tcprulescheck finds one-dot wildcard'
157env TCPREMOTEIP=127.0.0.1 TCPREMOTEINFO=bill tcprulescheck test.cdb; echo $?
158
159echo '--- tcprulescheck finds zero-dot wildcard'
160env TCPREMOTEIP=10.119.75.39 TCPREMOTEINFO=bill tcprulescheck test.cdb; echo $?
161
162echo '--- tcprules handles comments, address ranges, multiple variables'
163echo '127.0-5.:allow,which=/first/,where=/whatever/
164# comment' | tcprules test.cdb test.tmp; echo $?
165env TCPREMOTEIP=127.0.0.1 tcprulescheck test.cdb
166env TCPREMOTEIP=127.1.0.1 tcprulescheck test.cdb
167env TCPREMOTEIP=127.2.0.1 tcprulescheck test.cdb
168env TCPREMOTEIP=127.3.0.1 tcprulescheck test.cdb
169env TCPREMOTEIP=127.4.0.1 tcprulescheck test.cdb
170env TCPREMOTEIP=127.5.0.1 tcprulescheck test.cdb
171env TCPREMOTEIP=127.6.0.1 tcprulescheck test.cdb
172env TCPREMOTEIP=127.7.0.1 tcprulescheck test.cdb
173env TCPREMOTEIP=127.8.0.1 tcprulescheck test.cdb
174env TCPREMOTEIP=127.9.0.1 tcprulescheck test.cdb
175env TCPREMOTEIP=127.10.0.1 tcprulescheck test.cdb
176
177echo '--- tcprules handles host names'
178echo '=known.edu:allow,which=/known/
179=.abuser.edu:deny
180:allow,which=/anybody/' | tcprules test.cdb test.tmp; echo $?
181env TCPREMOTEIP=1.2.3.4 TCPREMOTEHOST=known.edu tcprulescheck test.cdb
182env TCPREMOTEIP=1.2.3.4 TCPREMOTEHOST=random.edu tcprulescheck test.cdb
183env TCPREMOTEIP=1.2.3.4 TCPREMOTEHOST=abuser.edu tcprulescheck test.cdb
184env TCPREMOTEIP=1.2.3.4 TCPREMOTEHOST=x.abuser.edu tcprulescheck test.cdb
185env TCPREMOTEIP=1.2.3.4 TCPREMOTEHOST=x.y.abuser.edu tcprulescheck test.cdb
186
187echo '--- tcprulescheck searches for rules in the proper order'
188echo 'xyz@86.75.30.9:allow,which=/first/
189xyz@=one.two.three:allow,which=/second/
19086.75.30.9:allow,which=/third/
191=one.two.three:allow,which=/fourth/
19286.75.30.:allow,which=/fifth/
19386.75.:allow,which=/sixth/
19486.:allow,which=/seventh/
195=.two.three:allow,which=/eighth/
196=.three:allow,which=/ninth/
197=:allow,which=/tenth/
198:allow,which=/eleventh/
199' | tcprules test.cdb test.tmp; echo $?
200env TCPREMOTEIP=86.75.30.9 TCPREMOTEHOST=one.two.three TCPREMOTEINFO=xyz tcprulescheck test.cdb
201env TCPREMOTEIP=86.75.30.9 TCPREMOTEINFO=xyz tcprulescheck test.cdb
202env TCPREMOTEIP=86.75.30.10 TCPREMOTEHOST=one.two.three TCPREMOTEINFO=xyz tcprulescheck test.cdb
203env TCPREMOTEIP=86.75.30.9 TCPREMOTEHOST=one.two.three TCPREMOTEINFO=abc tcprulescheck test.cdb
204env TCPREMOTEIP=86.75.30.9 TCPREMOTEINFO=abc tcprulescheck test.cdb
205env TCPREMOTEIP=86.75.30.9 TCPREMOTEHOST=one.two.three tcprulescheck test.cdb
206env TCPREMOTEIP=86.75.30.9 tcprulescheck test.cdb
207env TCPREMOTEIP=86.75.30.10 TCPREMOTEHOST=one.two.three TCPREMOTEINFO=abc tcprulescheck test.cdb
208env TCPREMOTEIP=86.75.30.10 TCPREMOTEHOST=one.two.three tcprulescheck test.cdb
209env TCPREMOTEIP=86.75.30.10 TCPREMOTEHOST=four.two.three tcprulescheck test.cdb
210env TCPREMOTEIP=86.75.30.10 tcprulescheck test.cdb
211env TCPREMOTEIP=86.75.20.10 TCPREMOTEHOST=four.two.three TCPREMOTEINFO=abc tcprulescheck test.cdb
212env TCPREMOTEIP=86.75.20.10 TCPREMOTEINFO=abc tcprulescheck test.cdb
213env TCPREMOTEIP=86.75.20.10 TCPREMOTEHOST=four.two.three tcprulescheck test.cdb
214env TCPREMOTEIP=86.75.20.10 tcprulescheck test.cdb
215env TCPREMOTEIP=86.85.20.10 TCPREMOTEHOST=four.two.three TCPREMOTEINFO=abc tcprulescheck test.cdb
216env TCPREMOTEIP=86.85.20.10 TCPREMOTEINFO=abc tcprulescheck test.cdb
217env TCPREMOTEIP=86.85.20.10 TCPREMOTEHOST=four.two.three tcprulescheck test.cdb
218env TCPREMOTEIP=86.85.20.10 tcprulescheck test.cdb
219env TCPREMOTEIP=96.85.20.10 TCPREMOTEHOST=four.two.three TCPREMOTEINFO=abc tcprulescheck test.cdb
220env TCPREMOTEIP=96.85.20.10 TCPREMOTEHOST=four.two.three tcprulescheck test.cdb
221env TCPREMOTEIP=96.85.20.10 TCPREMOTEHOST=four.three TCPREMOTEINFO=abc tcprulescheck test.cdb
222env TCPREMOTEIP=96.85.20.10 TCPREMOTEHOST=four.three tcprulescheck test.cdb
223env TCPREMOTEIP=96.85.20.10 TCPREMOTEHOST=four TCPREMOTEINFO=abc tcprulescheck test.cdb
224env TCPREMOTEIP=96.85.20.10 TCPREMOTEHOST=four tcprulescheck test.cdb
225env TCPREMOTEIP=96.85.20.10 TCPREMOTEINFO=abc tcprulescheck test.cdb
226env TCPREMOTEIP=96.85.20.10 tcprulescheck test.cdb
227
228
229echo '--- addcr leaves an empty file alone'
230echo '' | tr -d '\012' | addcr; echo $?
231
232echo '--- addcr leaves a partial final line alone'
233echo test | tr -d '\012' | addcr; echo $?
234
235echo '--- addcr adds CR after the first line'
236echo test | addcr; echo $?
237
238echo '--- addcr adds CR after the second line'
239( echo test; echo test2 ) | addcr; echo $?
240
241echo '--- addcr handles nulls'
242echo test | tr e '\0' | addcr; echo $?
243
244echo '--- delcr leaves an empty file alone'
245echo '' | tr -d '\012' | delcr; echo $?
246
247echo '--- delcr leaves a non-CR line alone'
248echo test | delcr; echo $?
249
250echo '--- delcr removes CR if a line has it'
251echo testx | tr x '\015' | delcr; echo $?
252
253echo '--- delcr converts CR CR LF to CR LF'
254echo testxx | tr x '\015' | delcr; echo $?
255
256echo '--- delcr does not remove CR from a partial final line'
257echo testx | tr -d '\012' | tr x '\015' | delcr; echo $?
258
259echo '--- delcr handles a non-CR partial final line'
260echo test | tr -d '\012' | delcr; echo $?
261
262echo '--- delcr handles nulls'
263echo test | tr e '\0' | delcr; echo $?
264
265echo '--- fixcrio works'
266( echo ''; echo hi; echo therex ) | tr x '\015' \
267| fixcrio sh -c 'cat; echo bye' | cat
268
269echo '--- recordio works'
270( echo test; sleep 1 ) | recordio cat 2>&1 >/dev/null \
271| sed 's/^[0-9]*/.../' | sed 's/$/$/'
272
273echo '--- recordio handles multiple-line packets'
274( echo 'test
275test2'; sleep 1 ) | recordio cat 2>&1 >/dev/null \
276| sed 's/^[0-9]*/.../' | sed 's/$/$/'
277
278echo '--- recordio handles partial final lines'
279( echo test | tr -d '\012'; sleep 1 ) | recordio cat 2>&1 >/dev/null \
280| sed 's/^[0-9]*/.../' | sed 's/$/$/'
281
282echo '--- argv0 works'
283argv0 sh zero -c 'echo $0'; echo $?
284
285echo '--- argv0 requires arguments'
286argv0 sh; echo $?
287
288
289echo '--- rblsmtpd does not find 127.0.0.1 on the RBL'
290( echo help; echo quit ) \
291| ( TCPREMOTEIP=127.0.0.1 rblsmtpd echo ok 2>&1; echo $? ) \
292| sed 's/pid [0-9]*/pid x/'
293
294echo '--- rblsmtpd finds 127.0.0.2 on the RBL'
295( echo help; echo quit ) \
296| ( TCPREMOTEIP=127.0.0.2 rblsmtpd echo whoops 2>&1; echo $? ) \
297| sed 's/pid [0-9]*/pid x/'
298
299echo '--- rblsmtpd -b uses a permanent error code'
300( echo help; echo quit ) \
301| ( TCPREMOTEIP=127.0.0.2 rblsmtpd -b echo whoops 2>&1; echo $? ) \
302| sed 's/pid [0-9]*/pid x/'
303
304echo '--- rblsmtpd quits after a timeout'
305sleep 2 \
306| ( TCPREMOTEIP=127.0.0.2 rblsmtpd -cBt1 echo whoops 2>&1; echo $? ) \
307| sed 's/pid [0-9]*/pid x/'
308
309echo '--- rblsmtpd prints an immediate error message with -t0'
310sleep 2 \
311| ( TCPREMOTEIP=127.0.0.2 rblsmtpd -Ct0 echo whoops 2>&1; echo $? ) \
312| sed 's/pid [0-9]*/pid x/'
313
314echo '--- rblsmtpd understands an empty $RBLSMTPD'
315( echo help; echo quit ) \
316| ( TCPREMOTEIP=127.0.0.2 RBLSMTPD= rblsmtpd echo ok 2>&1; echo $? ) \
317| sed 's/pid [0-9]*/pid x/'
318
319echo '--- rblsmtpd understands a nonempty $RBLSMTPD'
320( echo help; echo quit ) \
321| ( TCPREMOTEIP=127.0.0.2 RBLSMTPD=Error rblsmtpd echo whoops 2>&1; echo $? ) \
322| sed 's/pid [0-9]*/pid x/'
323
324echo '--- rblsmtpd understands a permanent $RBLSMTPD'
325( echo help; echo quit ) \
326| ( TCPREMOTEIP=127.0.0.2 RBLSMTPD=-Error rblsmtpd echo whoops 2>&1; echo $? ) \
327| sed 's/pid [0-9]*/pid x/'
328
329echo '--- rblsmtpd understands -r'
330( echo help; echo quit ) \
331| ( TCPREMOTEIP=127.0.0.2 rblsmtpd -r nonexistent.local echo ok 2>&1; echo $? ) \
332| sed 's/pid [0-9]*/pid x/'
333
334echo '--- rblsmtpd understands -a'
335( echo help; echo quit ) \
336| ( TCPREMOTEIP=127.0.0.2 rblsmtpd -a rbl.maps.vix.com echo ok 2>&1; echo $? ) \
337| sed 's/pid [0-9]*/pid x/'
338
339
340svc -dx 50016
341wait
342
343echo '--- tcpserver -1v prints proper messages'
344sed -e 's/::.*/::x/' -e 's/ [0-9]* / x /' < log
345
346
347exit 0
348