Public Domain
tcpserver -v -RHl0 127.0.0.1 2023 sh -c "echo 'Go away......'"
telnet 127.0.0.1 2023
groupadd nofiles
useradd -g nofiles -d /nonexistent -s /nonexistent multilog
useradd -g nofiles -d /nonexistent -s /nonexistent daytime
mkdir -p /var/svc.d/daytime/log
mkdir -p /var/multilog/daytime
mkdir -p /etc/tcprules/
chown multilog /var/multilog/daytime
#!/bin/sh
USRID=$(id -u daytime)
GRPID=$(id -g nofiles)
CONLIMIT=100
PORT=13
LOCALNAME=0
MEMORY=4000000
ACL="/etc/tcprules/daytime.cdb"
exec 2>&1
exec softlimit -m${MEMORY} \
tcpserver -v -RH -l0 \
-u "$USRID" -g "$GRPID" \
-c ${CONLIMIT} -x ${ACL} \
${LOCALNAME} ${PORT} date
#!/bin/sh
# howdyd/log/run
# logging service for howdyd daemon
exec setuidgid multilog multilog t /var/multilog/daytime
127.0.0.1:allow
:deny
cd /etc/tcprules
tcprules daytime.cdb daytime.tmp < daytime.rules
chmod 444 daytime.cdb
ln -s /var/svc.d/daytime /service/daytime
tail -f /var/multilog/daytime/current | tai64nlocal
tai64nlocal < /var/multilog/daytime/current
ps -axww | grep super
date@
or
/usr/local/bin/tcpclient -RHl0 -- "${1-0}" 13 sh -c 'exec /usr/local/bin/delcr <&6' | cat -v
groupadd nofiles
useradd -g nofiles -d /nonexistent -s /nonexistent multilog
useradd -g nofiles -d /nonexistent -s /nonexistent howdyd
nofiles:x:1001:
multilog:x:1002:1001::/nonexistent:/nonexistent
howdyd:x:1003:1001::/nonexistent:/nonexistent
mkdir -p /var/svc.d/howdyd/log
#!/bin/sh
# howdyd.sh
# a howdy daemon
echo "Hi there!"
exit 0
### that's all, folks!
#!/bin/sh
# howdyd/run
# daemontools run script for howdyd service
CONLIMIT=13
PORT=1789
LOCALNAME=0
MEMORY=4000000
exec 2>&1
exec setuidgid howdyd softlimit -m${MEMORY} \
tcpserver -v -rh -l${LOCALNAME} \
-c ${CONLIMIT} \
-x /etc/tcprules/howdy.cdb \
${LOCALNAME} ${PORT} \
./howdyd.sh
#!/bin/sh
# howdyd/log/run
# logging service for howdyd daemon
exec setuidgid multilog multilog t /var/multilog/howdyd
cd /var/svc.d/howdyd/
chmod +x run
chmod +x log/run
chmod +x howdyd.sh
mkdir -p /var/multilog/howdyd
chown multilog /var/multilog/howdyd
mkdir -p /etc/tcprules/
# howdy.rules
127.:allow
10.0.:allow,SOMEVAR="somevalue"
:deny
cd /etc/tcprules
tcprules howdy.cdb howdy.tmp < howdy.rules
chmod 444 howdy.cdb
ln -s /var/svc.d/howdyd /service/howdyd
tail -f /var/multilog/howdyd/current | tai64nlocal
ps -axww | grep super
telnet 127.0.0.1 1789
tcpcat 127.0.0.1 1789
#!/bin/sh
# howdyd.sh
# a howdy daemon
# ===
echo "*** A visitor from ${TCPREMOTEIP}!" >&2
echo "Hi there! Welcome to `hostname`!"
echo "The time here: `date`"
echo "Our uptime is: `uptime`"
echo
echo "The howdyd environment:"
printenv | sort
echo
echo "The howdyd user:"
who -Hm
echo
echo "Our users:"
w -h
echo
/usr/games/fortune
echo "Bye!"
echo "*** The visitor from ${TCPREMOTEIP} departs!" >&2
exit 0
### that's all, folks!
svc -t /service/howdyd
tail -f /var/multilog/howdyd/current | tai64nlocal
svc -d /service/howdyd
rm /service/howdyd
Change setuidgid to envuidgid in the run script. What happens differently?
Set up the server to run on a port number less than 1024, say 981. Leaving the setuidgid invocation in the run script, what happens? Then change setuidgid to envuidgid. Now what happens? Why does the port number matter?
How can you arrange to run this service as a non-priveleged user even when using a priveleged port?
Decrease the softlimit parameters until the run script croaks. What messages appear in the log?
Remove the execute bits on the run script, with chmod 644 run. What happens in the log? What happens when you fix the problem, with chmod 755 run?
Experiment with the data gathering options to tcpserver. How do these affect the $TCP* environmental variables and the log output? How do these affect the latency (that is, the time required) to service remote connections?
Play with different access rules in /etc/tcprules/howdy.rules. Are there funner things you can make the script do, setting up different environmental variables depending on the client?
Of course, howdyd.sh doesn't have to be a shell script, either. Use your favorite language, Perl, Python, Ruby. Why, you could write your server using C!
http://www.bytereef.org/howto/djb/daytime-server.html
http://thedjbway.b0llix.net/ucspi-tcp/howdyd_1.html
http://thedjbway.b0llix.net/ucspi-tcp/howdyd_2.html
BY: Pejman Moghadam
TAG: tcpserver, ucspi-tcp, djb
DATE: 2013-05-22 15:11:27