From 581c05bce6288270afadc9c31bb93f616e20f8aa Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Fri, 16 Aug 2024 20:53:10 -0400 Subject: [PATCH] function checkmx implemented --- lib/gesspf.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++-- lib/subafn.c | 39 ++++++++++++++++++++++ lib/subafn.h | 6 ++++ lib/subrou.c | 2 +- lib/unidns.c | 20 +++++++++++ lib/unidns.h | 3 ++ 6 files changed, 161 insertions(+), 3 deletions(-) diff --git a/lib/gesspf.c b/lib/gesspf.c index 6a929cf..be082a3 100644 --- a/lib/gesspf.c +++ b/lib/gesspf.c @@ -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 } /* @@ -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 diff --git a/lib/subafn.c b/lib/subafn.c index e5b16c2..70f8396 100644 --- a/lib/subafn.c +++ b/lib/subafn.c @@ -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; +} +/* + +*/ +/********************************************************/ +/* */ /* Routine to convert clean string to be */ /* IP number in AFNTYP structure. */ /* */ diff --git a/lib/subafn.h b/lib/subafn.h index 119dff1..638713b 100644 --- a/lib/subafn.h +++ b/lib/subafn.h @@ -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); diff --git a/lib/subrou.c b/lib/subrou.c index 021702f..cf70252 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -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 diff --git a/lib/unidns.c b/lib/unidns.c index b5987fe..98dca29 100644 --- a/lib/unidns.c +++ b/lib/unidns.c @@ -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 */ diff --git a/lib/unidns.h b/lib/unidns.h index 83f63d8..ed13233 100644 --- a/lib/unidns.h +++ b/lib/unidns.h @@ -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); -- 2.47.3