Force the final dot, even if not provided in the configuration for RBLs.
[apps/pfixtools.git] / common / rbl.c
index 11ed464..1f2f442 100644 (file)
 
 static inline rbl_result_t rbl_dns_check(const char *hostname)
 {
+    debug("looking up for %s", hostname);
     struct hostent *host = gethostbyname(hostname);
     if (host != NULL) {
+        debug("host found");
         return RBL_FOUND;
     } else {
         if (h_errno == HOST_NOT_FOUND) {
+            debug("host not found: %s", hostname);
             return RBL_NOTFOUND;
         }
+        debug("dns error: %m");
         return RBL_ERROR;
     }
 }
@@ -52,15 +56,27 @@ static inline rbl_result_t rbl_dns_check(const char *hostname)
 rbl_result_t rbl_check(const char *rbl, uint32_t ip)
 {
     char host[257];
-    snprintf(host, 257, "%d.%d.%d.%d.%s",
-             ip & 0xff, (ip >> 8) & 0xff, (ip >> 16) & 0xff, (ip >> 24) & 0xff,
-             rbl);
+    int len;
+
+    len = snprintf(host, 257, "%d.%d.%d.%d.%s.",
+                   ip & 0xff, (ip >> 8) & 0xff, (ip >> 16) & 0xff, (ip >> 24) & 0xff,
+                   rbl);
+    if (len >= (int)sizeof(host))
+        return RBL_ERROR;
+    if (host[len - 2] == '.')
+        host[len - 1] = '\0';
     return rbl_dns_check(host);
 }
 
 rbl_result_t rhbl_check(const char *rhbl, const char *hostname)
 {
     char host[257];
-    snprintf(host, 257, "%s.%s", hostname, rhbl);
+    int len;
+
+    len = snprintf(host, 257, "%s.%s.", hostname, rhbl);
+    if (len >= (int)sizeof(host))
+        return RBL_ERROR;
+    if (host[len - 2] == '.')
+        host[len - 1] = '\0';
     return rbl_dns_check(host);
 }