]> code.citadel.org Git - citadel.git/blobdiff - citadel/locate_host.c
* Use syslog-compatible logging levels in lprintf(); the loglevel chosen
[citadel.git] / citadel / locate_host.c
index 18a5dc907711da123908d9f8effdf08c7174ea52..09aef65451542cf7f90d5a860f164b191fef74e7 100644 (file)
 #include "tools.h"
 #include "domain.h"
 
-void locate_host(char *tbuf, size_t n, const struct in_addr *addr)
+void locate_host(char *tbuf, size_t n,
+               char *abuf, size_t na,
+               const struct in_addr *addr)
 {
        struct hostent *ch;
        const char *i;
        char *j;
        int a1, a2, a3, a4;
+       char address_string[SIZ];
 
-       lprintf(9, "locate_host() called\n");
+       lprintf(CTDL_DEBUG, "locate_host() called\n");
 
 #ifdef HAVE_NONREENTRANT_NETDB
        begin_critical_section(S_NETDB);
 #endif
 
-       if ((ch = gethostbyaddr((const char *) addr, sizeof(*addr), AF_INET)) ==
-           NULL) {
-             bad_dns:
-               i = (const char *) addr;
-               a1 = ((*i++) & 0xff);
-               a2 = ((*i++) & 0xff);
-               a3 = ((*i++) & 0xff);
-               a4 = ((*i++) & 0xff);
-               snprintf(tbuf, n, "%d.%d.%d.%d", a1, a2, a3, a4);
+       i = (const char *) addr;
+       a1 = ((*i++) & 0xff);
+       a2 = ((*i++) & 0xff);
+       a3 = ((*i++) & 0xff);
+       a4 = ((*i++) & 0xff);
+       sprintf(address_string, "%d.%d.%d.%d", a1, a2, a3, a4);
+
+       if (abuf != NULL) {
+               safestrncpy(abuf, address_string, na);
+       }
+
+       if ((ch = gethostbyaddr((const char *) addr,
+          sizeof(*addr), AF_INET)) == NULL) {
+bad_dns:
+               safestrncpy(tbuf, address_string, n);
                goto end;       /* because we might need to end the critical
                                   section */
        }
@@ -74,7 +83,7 @@ void locate_host(char *tbuf, size_t n, const struct in_addr *addr)
 #endif
 
        tbuf[63] = 0;
-       lprintf(9, "locate_host() exiting\n");
+       lprintf(CTDL_DEBUG, "locate_host() exiting\n");
 }
 
 
@@ -103,7 +112,7 @@ int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
        num_rbl = get_hosts(rbl_domains, "rbl");
        if (num_rbl < 1) return(0);
 
-       /* Try all configured RBL */
+       /* Try all configured RBL's */
         for (rbl=0; rbl<num_rbl; ++rbl) {
                snprintf(tbuf, sizeof tbuf,
                        "%d.%d.%d.%d.",
@@ -112,7 +121,7 @@ int rbl_check_addr(struct in_addr *addr, char *message_to_spammer)
 
                if (gethostbyname(tbuf) != NULL) {
                        strcpy(message_to_spammer,
-                               "Message rejected due to "
+                               "5.7.1 Message rejected due to "
                                "known spammer source IP address"
                        );
                        return(1);
@@ -136,3 +145,27 @@ int rbl_check(char *message_to_spammer) {
        }
        return(0);
 }
+
+/*
+ * Convert a host name to a dotted quad address. 
+ * Returns zero on success or nonzero on failure.
+ */
+int hostname_to_dotted_quad(char *addr, char *host) {
+       struct hostent *ch;
+       const char *i;
+       int a1, a2, a3, a4;
+
+       ch = gethostbyname(host);
+       if (ch == NULL) {
+               strcpy(addr, "0.0.0.0");
+               return(1);
+       }
+
+       i = (const char *) ch->h_addr_list[0];
+       a1 = ((*i++) & 0xff);
+       a2 = ((*i++) & 0xff);
+       a3 = ((*i++) & 0xff);
+       a4 = ((*i++) & 0xff);
+       sprintf(addr, "%d.%d.%d.%d", a1, a2, a3, a4);
+       return(0);
+}