1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
|
# ZCTEST 1.0
# $Id: mail.rb,v 1.36 2004/02/19 14:20:42 sdalu Exp $
#
# CONTACT : zonecheck@nic.fr
# AUTHOR : Stephane D'Alu <sdalu@nic.fr>
#
# CREATED : 2002/09/25 19:14:21
# REVISION : $Revision: 1.36 $
# DATE : $Date: 2004/02/19 14:20:42 $
#
# CONTRIBUTORS: (see also CREDITS file)
#
#
# LICENSE : GPL v2 (or MIT/X11-like after agreement)
# COPYRIGHT : AFNIC (c) 2003
#
# This file is part of ZoneCheck.
#
# ZoneCheck is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# ZoneCheck is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with ZoneCheck; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#
# TODO: remove temporary hack where we are looking at the local resolver
# due to bestresolverip not correctly implemented
#
require 'framework'
require 'mail'
module CheckExtra
##
## Check domain NS records
##
class Mail < Test
with_msgcat 'test/mail.%s'
CONNECTION_TIMEOUT = 20
#-- Initialisation ------------------------------------------
def initialize(*args)
super(*args)
@fake_dest = const('fake_mail_dest')
@fake_from = const('fake_mail_from')
@fake_user = const('fake_mail_user')
@fake_host = const('fake_mail_host')
end
#-- Shortcuts -----------------------------------------------
def bestmx(name)
pref, exch = 65536, nil
mxlist = mx(bestresolverip(name), name)
mxlist = mx(nil, name) if mxlist.empty?
mxlist.each { |m|
if m.preference < pref
pref, exch = m.preference, m.exchange
end
}
exch
end
def mhosttest(mdom, mhost, dbgio=nil)
# Mailhost and IP
mip = addresses(mhost, bestresolverip(mhost))[0]
mip = addresses(mhost, nil)[0] if mip.nil?
raise "No host servicing mail for domain #{mdom}" if mip.nil?
# Execute test on mailhost
mrelay = ZCMail::new(mdom, mip.to_s, dbgio)
mrelay.open(CONNECTION_TIMEOUT)
begin
mrelay.banner
mrelay.helo(@fake_host)
mrelay.fake_info(@fake_user, @fake_dest, @fake_from)
yield mrelay
ensure
mrelay.quit
mrelay.close
end
end
def openrelay(mdom, mhost)
status = nil
begin
mhosttest(mdom, mhost) { |mrelay|
return status = mrelay.test_openrelay }
ensure
dbgmsg {
[ "not an openrelay : #{DBG.status2str(status, false)}",
" on domain #{mdom} using relay #{mhost}" ]
}
end
end
def testuser(user, mdom, mhost)
status = nil
dbgio = []
begin
mhosttest(mdom, mhost, dbgio) { |mrelay|
return status = mrelay.test_userexists(user) }
ensure
dbgmsg {
[ "mail for user #{user} : #{DBG.status2str(status)}",
" on domain #{mdom} using relay #{mhost}" ] + dbgio
}
end
end
#-- Checks --------------------------------------------------
# DESC: Check that the best MX for hostmaster is not an openrelay
def chk_mail_openrelay_hostmaster
rname = soa(bestresolverip).rname
mdom = rname.domain
mhost = bestmx(mdom) || mdom
return true unless openrelay(mdom, mhost)
{ 'mailhost' => mhost,
'hostmaster' => "#{rname[0].data}@#{mdom}",
'from_host' => @fake_from,
'to_host' => @fake_dest }
end
# DESC: Check that the best MX for the domain is not an openrelay
def chk_mail_openrelay_domain
mdom = @domain.name
mhost = bestmx(mdom) || mdom
return true unless openrelay(mdom, mhost)
{ 'mailhost' => mhost,
'from_host' => @fake_from,
'to_host' => @fake_dest }
end
# DESC: Check that hostmaster address is valid
def chk_mail_delivery_hostmaster
rname = soa(bestresolverip).rname
mdom = rname.domain
user = "#{rname[0].data}@#{mdom}"
mxlist = mx(bestresolverip(mdom), mdom)
mxlist = mx(nil, mdom) if mxlist.empty?
mxlist.sort! { |a,b|
a.preference <=> b.preference }
if mxlist.empty?
return true if testuser(user, mdom, mdom)
else
mxlist.each { |m|
begin
return true if testuser(user, mdom, m.exchange)
rescue TimeoutError, Errno::ECONNREFUSED
end
}
end
{ 'hostmaster' => user,
'mxlist' => mxlist.collect { |mx| mx.exchange }.join(', ') }
end
# DESC: check for MX or A
def chk_mail_mx_or_addr
ip = bestresolverip
!mx(ip).empty? || !addresses(@domain.name, ip).empty?
end
# DESC: Check that postmaster address is valid
def chk_mail_delivery_postmaster
mdom = @domain.name
user = "postmaster@#{mdom}"
mxlist = mx(bestresolverip(mdom), mdom)
mxlist = mx(nil, mdom) if mxlist.empty?
mxlist.sort! { |a,b|
a.preference <=> b.preference }
if mxlist.empty?
return true if testuser(user, mdom, mdom)
else
mxlist.each { |m|
begin
return true if testuser(user, mdom, m.exchange)
rescue TimeoutError, Errno::ECONNREFUSED
end
}
end
{ 'postmaster' => user,
'mxlist' => mxlist.collect { |mx| mx.exchange }.join(', ') }
end
# DESC:
def chk_mail_hostmaster_mx_cname
rname = soa(bestresolverip).rname
mdom = rname.domain
mhost = bestmx(mdom)
return true if mhost.nil? # No MX
! is_cname?(mhost)
end
#-- Tests ---------------------------------------------------
#
def tst_mail_delivery
ip = bestresolverip
if !mx(ip).empty? then 'MX'
elsif !addresses(@domain.name, ip).empty? then 'A'
else 'nodelivery'
end
end
end
end
|