Class: WLValidate::Validate

Inherits:
Object
  • Object
show all
Defined in:
lib/wlvalidate.rb

Constant Summary collapse

@@dns_updated =
0
@@dns_server =
"8.8.8.8"
@@default_dns_server =
"8.8.4.4"
@@dns_type =
2

Instance Method Summary collapse

Constructor Details

#initialize(dns_server = "8.8.4.4", dns_type = 1) ⇒ Validate

Returns a new instance of Validate.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wlvalidate.rb', line 45

def initialize( dns_server="8.8.4.4", dns_type=1 )
   # PURPOSE: Executes code before anything else runs (hence Initialize)
   # RETURNS: Not a darn thing...
   # NOTES:   We want to soak in parameters for DNS servers (array), DNS type (integer) and DNS timeouts (integer):
   #          Resulting global variables: @dns_servers, @dns_type, @dns_timeout
   #          If these values are not set during instancing, we want to put in some default values
   #
   # VARS:    @dns_server:   single string, non-array of IP address
   #          @dns_type:     integer where 1 = local config, 2 = authorative server, 3 = Specific server
   
   @dns_updated = 0
   @dns_server = dns_server.to_s
   @default_dns_server = "8.8.4.4"
   @dns_type = dns_type.to_i

   if (dns_type.to_i == 3)
      if (dns_server)
         @dns_server = dns_server
      else
         @dns_server = @default_dns_server
      end
   end
end

Instance Method Details

#A(strWL, strDomain) ⇒ Object



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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/wlvalidate.rb', line 194

def A(strWL, strDomain)
   # PURPOSE: Loop through all oN.<wl>.<domain>.<tlds> to get IPs, then also do reverse DNS checks
   # RETURNS: Hash - see above for definition

   hashReturn = Hash.new
   arrayReturn = Array.new

   if ((strWL == "") || (strDomain == ""))
      hashReturn = { "type" => "A", "result" => "ERROR", "data" => "Missing parameters", "ext_data" => "One or more expected paramters are missing!" }
      arrayReturn.push(hashReturn)
   end

   set_auth_ns(strDomain)

   i = 1
   intDNSIPLoops = 11
   while i < intDNSIPLoops do
      begin
         tmpHost = "o" + i.to_s + "." + strWL + "." + strDomain
         tmpIP = Dnsruby::Resolv.getaddress(tmpHost)
         resolvedHost = Dnsruby::Resolv.getname(tmpIP)
         if tmpHost == resolvedHost
            hashReturn = { "type" => "A", "result" => "PASS", "data" => tmpHost, "ext_data" => tmpIP, "ip" => tmpIP }
            arrayReturn.push(hashReturn)
            hashReturn = {}
         end
      rescue Dnsruby::ResolvTimeout
            hashReturn = { "type" => "A", "result" => "ERROR", "data" => tmpHost, "ext_data" => "Timed out while attempting rDNS lookup", "ip" => tmpIP }
            arrayReturn.push(hashReturn)
            hashReturn = {}
      rescue      
            #hashReturn = { "type" => "A", "result" => "ERROR", "data" => tmpHost, "ext_data" => "Unable to resolve rDNS for host", "ip" => tmpIP }
            #arrayReturn.push(hashReturn)
            #hashReturn = {}
      end
      i += 1
   end
   if arrayReturn.empty?
      hashReturn = { "type" => "A", "result" => "FAIL", "data" => "No data", "ext_data" => "No SendGrid IPs", "ip" => "none" }
      arrayReturn.push(hashReturn)
      hashReturn = {}
   end

   return arrayReturn
end

#ALL(strWL, strDomain) ⇒ Object



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
# File 'lib/wlvalidate.rb', line 114

def ALL(strWL, strDomain)
   # PURPOSE: Execute all of the below methods as a single call
   # RETURNS: Returns the combined array of hashes as returned by each function
   # NOTES:   All functions have values on all return paths, except for MX (see TODO above)

   arrayReturns = Array.new

   hashSPF = self.SPF(strWL, strDomain)
   hashCNAME = self.CNAME(strWL, strDomain)
   hashA = self.A(strWL, strDomain)
   hashDKIM1 = self.DKIM1(strWL, strDomain)
   hashDKIM2 = self.DKIM2(strWL, strDomain)
   arrayMX = self.MX(strWL, strDomain)
   hashInfo = self.get_info()

   # array.push hashes, and array.concat arrays
   arrayReturns.push(hashSPF)
   arrayReturns.push(hashCNAME)
   arrayReturns.concat(hashA)
   arrayReturns.push(hashDKIM1)
   arrayReturns.push(hashDKIM2)
   arrayReturns.concat(arrayMX)
   arrayReturns.push(hashInfo)

   return arrayReturns
end

#CNAME(strWL, strDomain) ⇒ Object



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/wlvalidate.rb', line 240

def CNAME(strWL, strDomain)
   # PURPOSE: Get CNAME record and make sure it's mapped to 'sendgrid.net'
   # RETURNS: Hash - See above for definition

   hashReturn = Hash.new
   boolFoundMatch = false

   if ((strWL == "") || (strDomain == ""))
      hashReturn = { "type" => "CNAME", "result" => "ERROR", "data" => "Missing parameters", "ext_data" => "One or more expected paramters are missing!" }
   end

   set_auth_ns(strDomain)

   if ((@dns_type == 2) || (@dns_type == 3))
      dns_params = {:nameserver=>@dns_server}
   end

   Dnsruby::DNS.open(dns_params) {|dns|
      begin
         strHost = strWL + "." + strDomain
         arrayCNAME = dns.getresources(strHost, Dnsruby::Types.CNAME)
         arrayCNAME.each do |r|
            if boolFoundMatch == false
               tmpCNAME = r.domainname.to_s
               if tmpCNAME.match %r{sendgrid.net}
                  hashReturn = { "type" => "CNAME", "result" => "PASS", "data" => strHost, "ext_data" => tmpCNAME }
                  boolFoundMatch = true
               end
            end
            if hashReturn.empty?
               hashReturn = { "type" => "CNAME", "result" => "FAIL", "data" => strHost, "ext_data" => "No matching CNAME records found" }
            end
         end
         if boolFoundMatch == false
            hashReturn = { "type" => "CNAME", "result" => "FAIL", "data" => strHost, "ext_data" => "No matching CNAME records found" }
         end
      rescue Dnsruby::ResolvTimeout
         hashReturn = { "type" => "CNAME", "result" => "ERROR", "data" => strHost, "ext_data" => "Timed out while attempting rDNS lookup" }
      rescue
         hashReturn = { "type" => "CNAME", "result" => "ERROR", "data" => strHost, "ext_data" => "Unable to resolve host, or query timed out" }
      end
   }
   return hashReturn
end

#DKIM1(strWL, strDomain) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/wlvalidate.rb', line 285

def DKIM1(strWL, strDomain)
   # PURPOSE: Get TXT/CNAME record for smtpapi._domainkey.<domain>.<tlds>
   # RETURNS: Hash - See above for definition

   hashReturn = Hash.new
   boolFoundMatch = false
   strHost = "smtpapi._domainkey." + strDomain
   strDKIM_k = "k=rsa"
   strDKIM_t = "t=s"
   strDKIM_p = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPtW5iwpXVPiH5FzJ7Nrl8USzuY9zqqzjE0D1r04xDN6qwziDnmgcFNNfMewVKN2D1O+2J9N14hRprzByFwfQW76yojh54Xu3uSbQ3JP0A7k8o8GutRF8zbFUA8n0ZH2y0cIEjMliXY4W4LwPA7m4q0ObmvSjhd63O9d8z1XkUBwIDAQAB"

   if ((strWL == "") || (strDomain == ""))
      hashReturn = { "type" => "DKIM1", "result" => "ERROR", "data" => "Missing parameters", "ext_data" => "One or more expected paramters are missing!" }
   end

   set_auth_ns(strDomain)         

   if ((@dns_type == 2) || (@dns_type == 3))
      dns_params = {:nameserver=>@dns_server}
   end

   Dnsruby::DNS.open(dns_params) {|dns|
      begin
         arrayDKIM1 = dns.getresources(strHost, Dnsruby::Types.TXT)
         arrayDKIM1.each do |r|
            if r.name.to_s.match %r{dkim.sendgrid.net}
               hashReturn = { "type" => "DKIM1", "result" => "PASS", "data" => r.name.to_s, "ext_data" => "CNAME", "rdata" => "(cname)" }
               boolFoundMatch = true
            end
            if ((r.name.to_s == strHost) && (r.data.split(" ")[0].match %r{#{Regexp.escape(strDKIM_k)}}) && (r.data.split(" ")[1].match %r{#{Regexp.escape(strDKIM_t)}}) && (r.data.split(" ")[2].match %r{#{Regexp.escape(strDKIM_p)}}))
               hashReturn = { "type" => "DKIM1", "result" => "PASS", "data" => r.name.to_s, "ext_data" => "TXT", "rdata" => r.data }
               boolFoundMatch = true
            end
         end
         if ((boolFoundMatch == false) || (hashReturn.empty?))
            arrayDKIM1CNAME = dns.getresources(strHost, Dnsruby::Types.CNAME)
            arrayDKIM1CNAME.each do |c|
               if c.rdata_to_string.match %r{dkim.sendgrid.net}
                  hashReturn = { "type" => "DKIM1", "result" => "PASS", "data" => c.name.to_s, "ext_data" => "CNAME", "rdata" => "(cname)" }
                  boolFoundMatch = true
               end
            end
         end
         if ((boolFoundMatch == false) || (hashReturn.empty?))
            hashReturn = { "type" => "DKIM1", "result" => "FAIL", "data" => strHost, "ext_data" => "No matching DKIM/TXT records found" }
         end
      rescue
         hashReturn = { "type" => "DKIM1", "result" => "ERROR", "data" => strHost, "ext_data" => "Unable to resolve host, or query timed out" }
      end
   }
   return hashReturn
end

#DKIM2(strWL, strDomain) ⇒ Object



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/wlvalidate.rb', line 338

def DKIM2(strWL, strDomain)
   # PURPOSE: Get TXT/CNAME record for smtpapi._domainkey.<wl>.<domain>.<tlds>
   # RETURNS: Hash - See above for definition

   hashReturn = Hash.new
   boolFoundMatch = false
   strHost = "smtpapi._domainkey." + strWL + "." + strDomain
   strDKIM_k = "k=rsa"
   strDKIM_t = "t=s"
   strDKIM_p = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDPtW5iwpXVPiH5FzJ7Nrl8USzuY9zqqzjE0D1r04xDN6qwziDnmgcFNNfMewVKN2D1O+2J9N14hRprzByFwfQW76yojh54Xu3uSbQ3JP0A7k8o8GutRF8zbFUA8n0ZH2y0cIEjMliXY4W4LwPA7m4q0ObmvSjhd63O9d8z1XkUBwIDAQAB"

   if ((strWL == "") || (strDomain == ""))
      hashReturn = { "type" => "DKIM2", "result" => "ERROR", "data" => "Missing parameters", "ext_data" => "One or more expected paramters are missing!" }
   end

   set_auth_ns(strDomain)         

   if ((@dns_type == 2) || (@dns_type == 3))
      dns_params = {:nameserver=>@dns_server}
   end

   Dnsruby::DNS.open(dns_params) {|dns|
      begin
         arrayDKIM2 = dns.getresources(strHost, Dnsruby::Types.TXT)
         arrayDKIM2.each do |r|
            if r.name.to_s.match %r{dkim.sendgrid.net}
               hashReturn = { "type" => "DKIM2", "result" => "PASS", "data" => r.name.to_s, "ext_data" => "CNAME", "rdata" => "(cname)" }
               boolFoundMatch = true
            end
            if ((r.name.to_s == strHost) && (r.data.split(" ")[0].match %r{#{Regexp.escape(strDKIM_k)}}) && (r.data.split(" ")[1].match %r{#{Regexp.escape(strDKIM_t)}}) && (r.data.split(" ")[2].match %r{#{Regexp.escape(strDKIM_p)}}))
               hashReturn = { "type" => "DKIM2", "result" => "PASS", "data" => r.name.to_s, "ext_data" => "TXT", "rdata" => r.data }
               boolFoundMatch = true
            end
         end

         if ((boolFoundMatch == false) || (hashReturn.empty?))
            arrayDKIM2CNAME = dns.getresources(strHost, Dnsruby::Types.CNAME)
            arrayDKIM2CNAME.each do |c|
               if c.rdata_to_string.match %r{dkim.sendgrid.net}
                  hashReturn = { "type" => "DKIM2", "result" => "PASS", "data" => c.name.to_s, "ext_data" => "CNAME", "rdata" => "(cname)" }
                  boolFoundMatch = true
               end
            end
         end
         if ((boolFoundMatch == false) || (hashReturn.empty?))
            hashReturn = { "type" => "DKIM2", "result" => "FAIL", "data" => strHost, "ext_data" => "No matching DKIM/TXT records found" }
         end
      rescue
         hashReturn = { "type" => "DKIM2", "result" => "ERROR", "data" => strHost, "ext_data" => "Unable to resolve host, or query timed out" }
      end
   }
   return hashReturn
end

#get_infoObject



69
70
71
72
73
# File 'lib/wlvalidate.rb', line 69

def get_info()
   hashReturn = Hash.new
   hashReturn = { "type" => "INFO", "dns_server" => @dns_server, "dns_type" => @dns_type, "dns_updated" => @dns_updated }
   return hashReturn
end

#MX(strWL, strDomain) ⇒ Object



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
# File 'lib/wlvalidate.rb', line 141

def MX(strWL, strDomain)
   # PURPOSE: Loop through all <domain> MX records
   # RETURNS: Array of hashes 
   hashTemp = Hash.new
   arrayReturn = Array.new
   arrayMX = Array.new

   if ((strWL == "") || (strDomain == ""))
      hashReturn = { "type" => "MX", "result" => "ERROR", "data" => "Missing parameters", "ext_data" => "One or more parameters are missing!" }
      arrayReturn.push(hashReturn)
   end

   set_auth_ns(strDomain)

   if ((@dns_type == 2) || (@dns_type == 3))
      dns_params = {:nameserver=>@dns_server}
   end

   Dnsruby::DNS.open(dns_params) {|dns|
      begin
         arrayMX = dns.getresources(strDomain, Dnsruby::Types.MX)
         if (arrayMX.count > 0)
            arrayMX.sort!
            arrayMX.each do |r|
               myExchange = r.exchange.to_s
               myPreference = r.preference.to_s
               if myExchange.match %r{sendgrid.}
                  hashTemp = { "type" => "MX", "result" => "ERROR", "data" => myExchange, "ext_data" => "SendGrid is listed as handling incoming email on the root domain - this is generally a bad idea unless you're using ParseAPI..." }
               else
                  hashTemp = { "type" => "MX", "result" => "INFO", "data" => myExchange, "ext_data" => myPreference }
               end
               arrayReturn.push(hashTemp)
               hashTemp = {}
            end
         else
            hashTemp = { "type" => "MX", "result" => "FAIL", "data" => "No records", "ext_data" => "No MX records exist for this domain" }
            arrayReturn.push(hashTemp)
            hashTemp = {}
         end               
      rescue
         hashTemp = { "type" => "MX", "result" => "ERROR", "data" => "Unable to resolve", "ext_data" => "Unable to resolve MX records for domain " + strDomain }
         arrayReturn.push(hashTemp)
         hashTemp = {}
      end
   }
   if arrayReturn.empty?
      hashReturn = { "type" => "MX", "result" => "FAIL", "data" => "No data", "ext_data" => "No MX records in domain" }
      arrayReturn.push(hashReturn)
      hashReturn = {}
   end
   return arrayReturn
end

#set_auth_ns(strDomain) ⇒ Object



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
# File 'lib/wlvalidate.rb', line 75

def set_auth_ns(strDomain)
   # Since we can't get strDomain during __init__, we will check to see if @dns_type = 2 (auth) and if
   # @dns_server is empty. If so, then we want to get the authorative NS and set it globally.
   if ((@dns_updated == 0) && (@dns_type == 2))
      iplist = []
      begin
         res = Dnsruby::Resolver.new
     
         res.retry_times=(1)
         ns_req = nil
         ns_req = res.query(strDomain, 'NS')
     
         res.recurse=(0)
     
         (ns_req.answer.select {|r| r.type == 'NS'}).each do |nsrr|
            ns = nsrr.domainname

            local_res = Dnsruby::Resolver.new
            a_req=nil
            a_req = local_res.query(ns, 'A')
 
               (a_req.answer.select {|r| r.type == 'A'}).each do |r|
                  ip = r.address
                  res.nameserver=(ip.to_s)
                  soa_req=nil
                  soa_req = res.query(strDomain, 'SOA', 'IN')
                  iplist.push(ip.to_s)
               end
            end
         iplist.shuffle!
         @dns_server = iplist.first
         @dns_updated = 1
      rescue
         @dns_server = @default_dns_server
         @dns_updated = 1
      end
   end
end

#SPF(strWL, strDomain) ⇒ Object



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/wlvalidate.rb', line 392

def SPF(strWL, strDomain)
   # PURPOSE: Get TXT record and make sure it has include:sendgrid.net
   # RETURNS: Hash - See above for definition

   hashReturn = Hash.new
   boolFoundMatch = false

   if ((strWL == "") || (strDomain == ""))
      hashReturn = { "type" => "SPF", "result" => "ERROR", "data" => "Missing parameters", "ext_data" => "One or more expected paramters are missing!" }
   end

   set_auth_ns(strDomain)         

   if ((@dns_type == 2) || (@dns_type == 3))
      dns_params = {:nameserver=>@dns_server}
   end

   Dnsruby::DNS.open(dns_params) {|dns|
      begin
         arrayTXT = dns.getresources(strDomain, Dnsruby::Types.TXT)
         if arrayTXT.to_s != "[]"
            arrayTXT.each do |r|
               if boolFoundMatch == false
                  rTXT = r.strings.to_s
                  if ((rTXT.match('v=spf1')) && (rTXT.match('include:sendgrid.net')))
                     if rTXT.match %r{~al}
                        hashReturn = { "type" => "SPF", "result" => "PASS", "data" => rTXT, "ext_data" => "softfail" }
                     elsif rTXT.match %r{-al}
                        hashReturn = { "type" => "SPF", "result" => "PASS", "data" => rTXT, "ext_data" => "fail" }
                     elsif rTXT =~/^+al/
                        hashReturn = { "type" => "SPF", "result" => "PASS", "data" => rTXT, "ext_data" => "pass" }
                     elsif rTXT.match %r{\?al}
                        hashReturn = { "type" => "SPF", "result" => "PASS", "data" => rTXT, "ext_data" => "neutral" }
                     else
                        hashReturn = { "type" => "SPF", "result" => "PASS", "data" => rTXT, "ext_data" => "no qualifier" }
                     end
                     boolFoundMatch = true
                  else
                     hashReturn = { "type" => "SPF", "result" => "FAIL", "data" => "No record", "ext_data" => "The requested SPF record was not found" }
                  end
               end
            end
            if hashReturn.empty?
               hashReturn = { "type" => "SPF", "result" => "FAIL", "data" => "No record", "ext_data" => "No matching DKIM/TXT records found" }
            end
         else
            hashReturn = { "type" => "SPF", "result" => "FAIL", "data" => "No record", "ext_data" => "No TXT records found for domain" }
         end
      rescue
         hashReturn = { "type" => "SPF", "result" => "ERROR", "data" => "No record", "ext_data" => "The requested SPF record was not found" }
      end
   }
   return hashReturn
end