]> SAFE projects GIT repository - jmp/mailleur/commitdiff
function checkmx implemented
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 17 Aug 2024 00:53:10 +0000 (20:53 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Sat, 17 Aug 2024 00:53:10 +0000 (20:53 -0400)
lib/gesspf.c
lib/subafn.c
lib/subafn.h
lib/subrou.c
lib/unidns.c
lib/unidns.h

index 6a929cfcad457235df935dfdf2cf7e7a10f7ac81..be082a3e152bc7b97d743d7f5a72d03f66929ab0 100644 (file)
@@ -161,13 +161,103 @@ return seq;
 /*      return true if successful.                      */
 /*                                                      */
 /********************************************************/
-static _Bool checkmx(char *domain,AFNTYP *afnnum,char *seq)
+static _Bool checkmx(char *domain,AFNTYP *afnnum,char *mx)
 
 {
+#define OPEP    "gesspf.c:checkmx"
+
 _Bool found;
+int cidr;
+char *mxdom;
+MXTYP **mxlist;
+struct addrinfo hints;
+int phase;
+_Bool proceed;
 
 found=false;
+cidr=128;
+mxdom=domain;
+mxlist=(MXTYP **)0;
+(void) memset(&hints,'\000',sizeof(hints));
+hints.ai_family=PF_UNSPEC;
+hints.ai_socktype=SOCK_STREAM;
+hints.ai_flags=HINTFLG;
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //getting the list of MX
+      if (mx!=(char *)0) {      //always
+        char *ptr;
+
+        (void) printf("JMPDBG mx value=<%s>\n",mx);
+        if ((ptr=strchr(mx,'/'))!=(char *)0) {
+          *ptr='\000';
+          cidr=atoi(ptr+1);
+          }
+        if ((ptr=strchr(mx,':'))!=(char *)0) {
+          *ptr='\000';
+          mxdom=ptr+1;
+          }
+        }
+      break;
+    case 1      :       //getting the list of MX for mxdomain
+      if ((mxlist=dns_getmx(mxdom))==(MXTYP **)0) {
+        (void) rou_alert(0,"%s empty MX list for domain <%s> (remote DNS config?)",
+                            OPEP,mxdom);
+        phase=999;      //trouble no need to go further
+        }
+      break;
+    case 2      :       //getting the list of MX for mxdomain
+      for (int i=0;(found==false)&&(mxlist[i]!=(MXTYP *)0);i++) {
+        int status;
+        struct addrinfo *res;
+        struct addrinfo *rp;
+
+        status=getaddrinfo(mxlist[i]->mxname,"",&hints,&res);
+        if (status!=0) {
+          (void) rou_alert(0,"%s Unable to find addrinfo for <%s> (error=<%s>)",
+                             OPEP,mxlist[i]->mxname,gai_strerror(status));
+          continue;
+          }
+        rp=res;
+        for (int j=0;rp!=(struct addrinfo *)0;rp=rp->ai_next,j++) {
+          AFNTYP *addrnum;
+
+          if ((addrnum=afn_getaddrinfo(rp))==(AFNTYP *)0) {
+            (void) rou_alert(0,"%s Found unknown inet family for <%s> IP number",
+                             OPEP,mxlist[i]->mxname);
+            continue;
+            }
+          switch (afn_cmpipnum(addrnum,afnnum,cidr)) {
+            case false  :       //not found
+              break;
+            case true   :       //found
+              found=true; 
+              break;
+            case -1     :       //trouble?
+              (void) rou_alert(0,"%s Unable to compare MX IP for "
+                                 "host <%s> (error=<%s>)",
+                                 OPEP,mxlist[i]->mxname,strerror(errno));
+              break;
+            }
+          addrnum=afn_freeipnum(addrnum);
+          if (found==true)
+            break;              //no need to loop further
+          }
+        (void) freeaddrinfo(res);
+        }
+      mxlist=dns_freemxlist(mxlist);
+      break;
+    default     :       //SAFE guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
 return found;
+
+#undef  OPEP
 }
 /*
 \f
@@ -298,7 +388,7 @@ while (proceed==true) {
           spf=locspf;
           break;
         case mch_mx             :       //This is a MX refrence
-          if (checkmx(domain,afnnum,seq+1)==true)
+          if (checkmx(domain,afnnum,seq)==true)
             spf=locspf;
           break;
         case mch_ip4            :       //This is IPV4 number
index e5b16c2b8d14b123e4b8c700a627347549926060..70f83961e2aa844a049e9bf5b36678b4a978e5da 100644 (file)
@@ -144,6 +144,45 @@ return strip;
 */
 /********************************************************/
 /*                                                      */
+/*     Routine to convert a addrinfo struct to an      */
+/*     IP number in AFNTYP structure.                  */
+/*                                                      */
+/********************************************************/
+PUBLIC AFNTYP *afn_getaddrinfo(struct addrinfo *rp)
+
+{
+AFNTYP *afnnum;
+
+afnnum=(AFNTYP *)0;
+if (rp!=(struct addrinfo *)0) {
+  u_char *ptr;
+
+  ptr=(u_char *)0;
+  afnnum=calloc(1,sizeof(AFNTYP));
+  afnnum->afntype=rp->ai_family;
+  switch (rp->ai_family) {
+    case AF_INET       :
+      afnnum->afnmask=32;
+      ptr=(u_char *)&(((struct sockaddr_in *)rp->ai_addr)->sin_addr.s_addr);
+      break;
+    case AF_INET6      :
+      afnnum->afnmask=128;
+      ptr=(u_char *)&(((struct sockaddr_in6 *)rp->ai_addr)->sin6_addr.s6_addr);
+      break;
+    default            :       /*trouble trouble       */
+      afnnum=afn_freeipnum(afnnum);
+      break;
+    }
+  if (ptr!=(u_char *)0)
+    (void) memmove(afnnum->ip,ptr,afnnum->afnmask/8);
+  }
+return afnnum;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
 /*     Routine to convert clean string to be           */
 /*     IP number in AFNTYP structure.                  */
 /*                                                      */
index 119dff194983852e94293604bb811af56046ba0b..638713b3fc194cf7cf95206c6e237b85203b8dff 100644 (file)
@@ -16,6 +16,8 @@
 //space to store (at least) IPV6 number
 #define        AFT sizeof(struct in6_addr)
 
+#define HINTFLG AI_ALL|AI_CANONNAME|AI_CANONIDN
+
 typedef        struct  {
         char *strnumip; //IP in string format   
        int afntype;    //IP type
@@ -30,6 +32,10 @@ extern AFNTYP *afn_freeipnum(AFNTYP *afnnum);
 //IP number
 extern char *afn_stripnum(AFNTYP *afnnum);
 
+//procedure to convert a addrinfo struct to an 
+//IP number in AFNTYP structure.
+extern AFNTYP *afn_getaddrinfo(struct addrinfo *rp);
+
 //procedure to convert a IP as string to an AFNTYP structure
 extern AFNTYP *afn_getipnum(char *cleanipstr);
 
index 021702f007456c47000e33178164423d63878a7f..cf702521a84ccdfa4e2756cad10bd9072cfa46d7 100644 (file)
@@ -21,7 +21,7 @@
 
 //version definition 
 #define VERSION "0.4.2"
-#define RELEASE "17"
+#define RELEASE "18"
 
 //Public variables
 PUBLIC  int debug=0;            //debug level
index b5987fe4f63ba2bdaa4af97b911e9d39eef4c6e4..98dca2991670119bf922dead2c8f4ebe8cf447f7 100644 (file)
@@ -287,6 +287,26 @@ return spfrec;
 */
 /********************************************************/
 /*                                                      */
+/*     Procedure to free memory used by a list of      */
+/*      MXTYP.                                          */
+/*                                                      */
+/********************************************************/
+PUBLIC MXTYP **dns_freemxlist(MXTYP **mxlist)
+
+{
+if (mxlist!=(MXTYP **)0) {
+  for (int i=0;mxlist[i]!=(MXTYP *)0;i++)
+    (void) free(mxlist[i]);
+  (void) free(mxlist);
+  mxlist=(MXTYP **)0;
+  }
+return mxlist;
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
 /*     Procedure to extract a domain MX.               */
 /*     If the domain doesn't exist or domain           */
 /*     do not have a MX, a void pointer is             */
index 83f63d8442c336255d55ec1faa17377554f108f6..ed132334df6b0a993dc01c8058e97e678acdcef5 100644 (file)
@@ -17,6 +17,9 @@ typedef       struct  {
 //Procedure to get ONE SPF entry within domain dns
 extern char *dns_getspf(char *domain);
 
+//procedure to free un list de MX related to domain
+extern MXTYP **dns_freemxlist(MXTYP **mxlist);
+
 //procedure to get a list of MX IP releated to a specific
 //domain.
 extern MXTYP **dns_getmx(char *domain);