一、xinetd简介

1、什么是xinetd

xinetd:eXtended InterNET Daemon  扩展的互联网守护程序
   xinetd是新一代的网络守护进程服务程序,又叫超级守护进程,常用来管理多种轻量级Internet服务。xinetd提供类似于inetd + tcp_wrapper的功能,但是更加强大和安全。


2、Xinetd的缺点

   当前最大的缺点是对RPC支持的不稳定,但是可以启动protmap,使它与xinetd共存来解决这个问题。


3、使用xinetd启动守护进程
   原则上任何系统服务都可以使用xinetd,然而最适合的应该是那些常用的网络服务,同时,这个服务的请求数目和频繁程度不会太高。像DNS和Apache就不适合采用这种方式,而像FTP、Telnet、SSH等就适合使用xinetd模式。
 

二、xinetd的使用

  xinetd为接收用户请求较少的服务专门提供监听功能,有请求时,xinetd临时地启动相应服务并响应请求,结束后又关掉相应的服务。

  这种被xinetd管理的服务叫非独立守护进程又被称为瞬时守护进程他们无需定义在运行级别下,只需要一次性地定义xinetd的运行级别

  能自我管理,无需xinetd提供监听服务的进程叫独立(standalone)守护进程

[root@Node4 xinetd.d]# chkconfig --list xinetd        #xinetd默认开机运行
xinetd         	0:off	1:off	2:off	3:on	4:on	5:on	6:off
[root@Node4 ~]# chkconfig --list
.
.
.                                   #省略了独立守护进程
xinetd based services:              #基于xinetd的服务
	chargen-dgram: 	off
	chargen-stream:	off
	daytime-dgram: 	off
	daytime-stream:	off
	discard-dgram: 	off
	discard-stream:	off
	echo-dgram:    	off
	echo-stream:   	off
	rsync:         	off
	tcpmux-server: 	off
	telnet:        	on
	time-dgram:    	off
	time-stream:   	off

1、那如何启动基于xinetd的服务?

例如,我想启动rsync服务

在/etc/inetd.d/目录下每个被xinetd管理的服务都有一个配置文件:

[root@Node4 ~]# cd /etc/xinetd.d
[root@Node4 xinetd.d]# ls
chargen-dgram   daytime-stream  echo-dgram   tcpmux-server  time-stream
chargen-stream  discard-dgram   echo-stream  telnet
daytime-dgram   discard-stream  rsync        time-dgram
[root@Node4 xinetd.d]# cat rsync
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#	allows crc checksumming etc.
service rsync
{
	disable	= yes               #是否禁用,改成no就启用了,无需重启xinetd
	flags		= IPv6
	socket_type     = stream
	wait            = no
	user            = root
	server          = /usr/bin/rsync
	server_args     = --daemon
	log_on_failure  += USERID
}

#也可以直接使用chkconfig设置
[root@Node4 xinetd.d]# chkconfig --list|grep rsync
	rsync:         	on
[root@Node4 xinetd.d]# chkconfig rsync off
[root@Node4 xinetd.d]# chkconfig --list|grep rsync
	rsync:         	off
[root@Node4 xinetd.d]# cat rsync 
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
#	allows crc checksumming etc.
service rsync
{
	disable	= yes                    #chkconfig命令也是修改这个值
	flags		= IPv6
	socket_type     = stream
	wait            = no
	user            = root
	server          = /usr/bin/rsync
	server_args     = --daemon
	log_on_failure  += USERID
}

我们可以总结出:

   非独立守护进程和独立守护进程不一样,非独立守护进程使用chkconfig既然设置开机是否运行,也设置服务当前开启和关闭,而独立守护进程chkconfig是仅设置开机在哪些运行级别下是否运行

   瞬时守护进程依赖于xinetd监听端口,当xinetd服务没启动时,非独立守护进程启动着也没用

[root@Node4 ~]# service xinetd status
xinetd (pid  1637) is running...
[root@Node4 ~]# ss -tunlp|grep :23
[root@Node4 ~]# chkconfig telnet on
[root@Node4 ~]# ss -tunlp|grep :23
tcp    LISTEN     0      64                    :::23                   :::*      users:(("xinetd",1637,5))
[root@Node4 ~]# service xinetd stop
Stopping xinetd:                                           [  OK  ]
[root@Node4 ~]# service xinetd status
xinetd is stopped
[root@Node4 ~]# chkconfig --list|grep telnet
	telnet:        	on
[root@Node4 ~]# ss -tunlp|grep :23
[root@Node4 ~]#


二、tcp_wrapper

1、tcp_wrapper简介

  tcp_wrapper  tcp包装器,

  是一种访问控制工具,类似于iptables,可以作访问控制。

  tcp_wrapper只能对基于tcp协议的服务作访问控制,但并不是所有基于tcp协议的服务都能实现用tcp wraper作访问控制。


2、tcp_wrapper工作机制

守护进程:tcpd,也可以说tcp_wrapper是一个库,程序依赖于tcp_wrapper就表明接受tcp_wrapper控制

配置文件:/etc/hosts.allow,/etc/hosts.deny

 注意:

    并非所有服务均能由tcp_wrapper控制

    判断某服务程序是否能由tcp_wrap控制

     动态编译:

        ldd命令检测其是否链接至libwrap库上即可

    libwarp.so.0 =>/lib64/libwarp.so.0

[root@Node1 ~]# ldd `which sshd`|grep libwrap
	libwrap.so.0 => /lib64/libwrap.so.0 (0x00007fd7dbe27000)

     静态编译:

       string /path/to/program

       如果出现以下某项说明也接受tcp_wrapper控制   

         hosts.allow

         hosts.deny


   tcp_wrapper实现访问控制主要依靠两个文件,一个是/etc.hosts.allow文件,另一个是/etc/hosts.deny文件从文件的名字上可以理解:一个是定义允许的,一个是定义拒绝的。那这两个文件生效的次序是怎样的呢?

说明:如图所示,N表示没有匹配的规则,Y表示有匹配的规则,OK表示允许访问,DENY表示拒绝访问。


匹配机制:

  1.先检查/etc/hosts.allow,如果被允许,则直接放心

  2.如果/etc/hosts.allow没有匹配项,则检查/etc/hosts.deny,如果被拒绝,则禁止访问;

  3.如果二者均无匹配,则放行


3、tcp_wrapper配置

配置文件语法格式:

   daemon_list:client_list [:options]

   进程列表:客户端列表


daemon_list的格式:

   应用程序名称(不是进程名),如果有多个,用逗号分隔即可

   ALL:匹配所有的列表


client_list:

    ip地址:172.16.100.100

    主机名:www.magedu.com

    网络地址/子网掩码:掩码仅允许使用长格式,不允许使用CIDR格式

                 172.16. 表示172.16.0.0/255.255.0.0


[:options]

   在hosts.allow文件中使用deny选项:表示在hosts.allow文件中定义拒绝规则

   在hosts.deny文件中使用allow选项:表示在hosts.deny文件中定义放行规则


tcp_wrapper有几个内置的宏:

   用于client_list的有:ALLKNOWN(主机名能正常解析的),UNKNOWN(主机名不能正常解析),PARANOID(主机名正反项解析不匹配的)

   用于daemon_list的有:ALL

            EXCEPT:可以用户client和daemon之中,起到排除功能

例子:

in.telnetd:  172.16.  EXCEPT  172.16.251.105
in.telnetd: 172.16. :deny       # 表示在deny这个172.16.0.0网段中的主机


spawn

  发起执行一条命令的意思,可以用来记录日志


    %c:user@host

    %s:server@host

    %h:客户端主机名

    %p:服务器上的进程PID

              

man 5 hosts_access:获取其完整帮助信息


注意:

   spaw要定义在客户端被哪个文件拒绝的那个文件里才生效

   echo的信息无需加引号,否则,命令替换可能不会进行

[root@Node1 ~]# cat /etc/hosts.deny 
#
# hosts.deny	This file contains access rules which are used to
#		deny connections to network services that either use
#		the tcp_wrappers library or that have been
#		started through a tcp_wrappers-enabled xinetd.
#
#		The rules in this file can also be set up in
#		/etc/hosts.allow with a 'deny' option instead.
#
#		See 'man 5 hosts_options' and 'man 5 hosts_access'
#		for information on rule syntax.
#		See 'man tcpd' for information on tcp_wrappers
#
sshd: 192.168.10.5 : spawn echo `date` login attempt from %c to %s >> /var/log/tcp_wrapper.log
[root@Node1 ~]# cat /var/log/tcp_wrapper.log 
Sun Feb 12 12:45:59 CST 2017 login attempt from 192.168.10.5 to sshd@192.168.10.1


练习:

   使用tcp_wrapper控制vsftp服务仅允许172.16.0.0网络中的主机访问,但要拒绝172.16.200.中的所有主机,对所有的拒绝访问尝试使用记录日志