@@ -203,6 +203,8 @@ if (cb->rc == DNS_SUCCEED)
203203 {
204204 dns_address * da = NULL ;
205205 uschar * addlist = cb -> rhs -> address ;
206+ uschar * orig_dnslist_domain = NULL ;
207+ int filter_rc = FAIL ;
206208
207209 /* For A and AAAA records, there may be multiple addresses from multiple
208210 records. For A6 records (currently not expected to be used) there may be
@@ -214,6 +216,76 @@ if (cb->rc == DNS_SUCCEED)
214216 HDEBUG (D_dnsbl ) debug_printf ("DNS lookup for %s succeeded (yielding %s)\n" ,
215217 query , addlist );
216218
219+ /* Make dnslist_domain available to dnslist_valid_addresses expansion. */
220+ orig_dnslist_domain = dnslist_domain ;
221+ dnslist_domain = domain_txt ;
222+
223+ for (da = cb -> rhs ; da ; da = da -> next )
224+ {
225+ switch (verify_check_this_host (& dnslist_valid_addresses , NULL , US "" , da -> address , NULL ))
226+ {
227+ case OK :
228+ da -> dnsbl_invalid = FALSE;
229+
230+ if (filter_rc != DEFER )
231+ filter_rc = OK ;
232+ break ;
233+
234+ case FAIL :
235+ da -> dnsbl_invalid = TRUE;
236+ addlist = NULL ;
237+
238+ log_write (0 , LOG_MAIN ,
239+ "DNS list lookup for %s at %s returned %s;"
240+ " invalid address discarded" ,
241+ keydomain , domain , da -> address );
242+ break ;
243+
244+ case DEFER :
245+ log_write (0 , LOG_MAIN ,
246+ "DNS list lookup for %s at %s returned %s;"
247+ " unable to verify, returned DEFER" ,
248+ keydomain , domain , da -> address );
249+
250+ filter_rc = DEFER ;
251+ break ;
252+ }
253+ }
254+
255+ dnslist_domain = orig_dnslist_domain ;
256+
257+ if (filter_rc == FAIL )
258+ {
259+ HDEBUG (D_dnsbl )
260+ {
261+ debug_printf ("=> all addresses are invalid\n" );
262+ debug_printf ("=> that means %s is not listed at %s\n" ,
263+ keydomain , domain );
264+ }
265+ }
266+
267+ if (filter_rc != OK ) return filter_rc ;
268+
269+ /* Need to recreate addlist without filtered addresses. */
270+ if (addlist == NULL )
271+ {
272+ for (da = cb -> rhs ; da ; da = da -> next )
273+ {
274+ if (da -> dnsbl_invalid )
275+ continue ;
276+
277+ if (addlist == NULL )
278+ addlist = da -> address ;
279+ else
280+ addlist = string_sprintf ("%s, %s" , addlist , da -> address );
281+ }
282+
283+ HDEBUG (D_dnsbl )
284+ {
285+ debug_printf ("=> updated address list: %s\n" , addlist );
286+ }
287+ }
288+
217289 /* Address list check; this can be either for equality, or via a bitmask.
218290 In the latter case, all the bits must match. */
219291
@@ -225,6 +297,9 @@ if (cb->rc == DNS_SUCCEED)
225297 const uschar * ptr = iplist ;
226298 uschar * res ;
227299
300+ if (da -> dnsbl_invalid )
301+ continue ;
302+
228303 /* Handle exact matching */
229304
230305 if (!bitmask )
@@ -249,14 +324,7 @@ if (cb->rc == DNS_SUCCEED)
249324 We change this only for IPv4 addresses in the list. */
250325
251326 if (host_aton (da -> address , address ) == 1 )
252- if ((address [0 ] & 0xff000000 ) != 0x7f000000 ) /* 127.0.0.0/8 */
253- log_write (0 , LOG_MAIN ,
254- "DNS list lookup for %s at %s returned %s;"
255- " not in 127.0/8 and discarded" ,
256- keydomain , domain , da -> address );
257-
258- else
259- mask = address [0 ];
327+ mask = address [0 ];
260328
261329 /* Scan the returned addresses, skipping any that are IPv6 */
262330
@@ -311,33 +379,6 @@ if (cb->rc == DNS_SUCCEED)
311379 }
312380 }
313381
314- /* No address list check; discard any illegal returns and give up if
315- none remain. */
316-
317- else
318- {
319- BOOL ok = FALSE;
320- for (da = cb -> rhs ; da ; da = da -> next )
321- {
322- int address [4 ];
323-
324- if ( host_aton (da -> address , address ) == 1 /* ipv4 */
325- && (address [0 ] & 0xff000000 ) == 0x7f000000 /* 127.0.0.0/8 */
326- )
327- ok = TRUE;
328- else
329- log_write (0 , LOG_MAIN ,
330- "DNS list lookup for %s at %s returned %s;"
331- " not in 127.0/8 and discarded" ,
332- keydomain , domain , da -> address );
333- }
334- if (!ok )
335- {
336- yield = FAIL ;
337- goto out ;
338- }
339- }
340-
341382 /* Either there was no IP list, or the record matched, implying that the
342383 domain is on the list. We now want to find a corresponding TXT record. If an
343384 alternate domain is specified for the TXT record, call this function
0 commit comments