1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
3f08c3bdfSopenharmony_ci# Copyright (c) 2020 Petr Vorel <pvorel@suse.cz>
4f08c3bdfSopenharmony_ci# Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
5f08c3bdfSopenharmony_ci# Copyright (c) International Business Machines  Corp., 2001
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ciTST_SETUP="setup"
8f08c3bdfSopenharmony_ciTST_CLEANUP="cleanup"
9f08c3bdfSopenharmony_ciTST_NEEDS_CMDS="diff telnet in.telnetd xinetd"
10f08c3bdfSopenharmony_ciTST_NEEDS_TMPDIR=1
11f08c3bdfSopenharmony_ciTST_TESTFUNC="do_test"
12f08c3bdfSopenharmony_ciTST_CNT=2
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci. daemonlib.sh
15f08c3bdfSopenharmony_ci
16f08c3bdfSopenharmony_cisetup()
17f08c3bdfSopenharmony_ci{
18f08c3bdfSopenharmony_ci	[ -f "/usr/lib/systemd/system/telnet.socket" ] && \
19f08c3bdfSopenharmony_ci		tst_brk TCONF "xinetd doesn't manage telnet"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci	check_addr="127.0.0.1"
22f08c3bdfSopenharmony_ci	ip a | grep -q inet6 && check_addr="$check_addr ::1"
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_ci	cat > tst_xinetd.conf.1 <<-EOF
25f08c3bdfSopenharmony_cidefaults
26f08c3bdfSopenharmony_ci{
27f08c3bdfSopenharmony_ci    instances      = 25
28f08c3bdfSopenharmony_ci    log_type       = FILE /var/log/servicelog
29f08c3bdfSopenharmony_ci    log_on_success = HOST PID
30f08c3bdfSopenharmony_ci    log_on_failure = HOST
31f08c3bdfSopenharmony_ci    disabled       = telnet
32f08c3bdfSopenharmony_ci}
33f08c3bdfSopenharmony_ciEOF
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci	cat > tst_xinetd.conf.2 <<-EOF
36f08c3bdfSopenharmony_cidefaults
37f08c3bdfSopenharmony_ci{
38f08c3bdfSopenharmony_ci    instances      = 25
39f08c3bdfSopenharmony_ci    log_type       = FILE /var/log/servicelog
40f08c3bdfSopenharmony_ci    log_on_success = HOST PID
41f08c3bdfSopenharmony_ci    log_on_failure = HOST
42f08c3bdfSopenharmony_ci    # disabled       = telnet
43f08c3bdfSopenharmony_ci}
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ciservice telnet
46f08c3bdfSopenharmony_ci{
47f08c3bdfSopenharmony_ci    socket_type     = stream
48f08c3bdfSopenharmony_ci    protocol        = tcp
49f08c3bdfSopenharmony_ci    wait            = no
50f08c3bdfSopenharmony_ci    user            = root
51f08c3bdfSopenharmony_ci    server          = /usr/sbin/in.telnetd
52f08c3bdfSopenharmony_ci    server_args     = -n
53f08c3bdfSopenharmony_ci    no_access       =
54f08c3bdfSopenharmony_ci    flags           = IPv6
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ciEOF
57f08c3bdfSopenharmony_ci	ROD mv /etc/xinetd.conf xinetd.conf.orig
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cicleanup()
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	[ -f xinetd.conf.orig ] && \
63f08c3bdfSopenharmony_ci		mv xinetd.conf.orig /etc/xinetd.conf
64f08c3bdfSopenharmony_ci
65f08c3bdfSopenharmony_ci	restart_daemon xinetd
66f08c3bdfSopenharmony_ci}
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_cirestart_xinetd()
69f08c3bdfSopenharmony_ci{
70f08c3bdfSopenharmony_ci	tst_res TINFO "restart xinetd"
71f08c3bdfSopenharmony_ci	restart_daemon xinetd > tst_xinetd.out 2>&1
72f08c3bdfSopenharmony_ci	if [ $? -ne 0 ]; then
73f08c3bdfSopenharmony_ci		cat tst_xinetd.out
74f08c3bdfSopenharmony_ci		tst_brk TBROK "unable to restart service with telnet disabled"
75f08c3bdfSopenharmony_ci	fi
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci	grep -qi "fail" tst_xinetd.out && \
78f08c3bdfSopenharmony_ci		tst_brk TBROK "xinetd failed to restart"
79f08c3bdfSopenharmony_ci}
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_cixinetd_test()
82f08c3bdfSopenharmony_ci{
83f08c3bdfSopenharmony_ci	local cnt=$1
84f08c3bdfSopenharmony_ci	local desc="$2"
85f08c3bdfSopenharmony_ci	local pattern="$3"
86f08c3bdfSopenharmony_ci	local a p
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	tst_res TINFO "install the new config file with telnet $desc"
89f08c3bdfSopenharmony_ci	ROD mv tst_xinetd.conf.$cnt /etc/xinetd.conf
90f08c3bdfSopenharmony_ci	restart_xinetd
91f08c3bdfSopenharmony_ci
92f08c3bdfSopenharmony_ci	for a in $check_addr; do
93f08c3bdfSopenharmony_ci		p=$(echo $pattern | sed "s/ADDR/$a/")
94f08c3bdfSopenharmony_ci		echo '' | telnet $a 2>&1 | grep -qiE "$p"
95f08c3bdfSopenharmony_ci		[ $? -ne 0 ] && \
96f08c3bdfSopenharmony_ci			tst_brk TFAIL "not expected output for 'telnet $a'"
97f08c3bdfSopenharmony_ci	done
98f08c3bdfSopenharmony_ci	tst_res TPASS "expected output with telnet $desc"
99f08c3bdfSopenharmony_ci}
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_cido_test()
102f08c3bdfSopenharmony_ci{
103f08c3bdfSopenharmony_ci	case $1 in
104f08c3bdfSopenharmony_ci	1) xinetd_test $1 "disabled" \
105f08c3bdfSopenharmony_ci			"telnet: (connect to address ADDR|Unable to connect to remote host): Connection refused";;
106f08c3bdfSopenharmony_ci	2) xinetd_test $1 "enabled" \
107f08c3bdfSopenharmony_ci			"Connection closed by foreign host";;
108f08c3bdfSopenharmony_ci	esac
109f08c3bdfSopenharmony_ci}
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci. tst_net.sh
112f08c3bdfSopenharmony_citst_run
113