From 165c7665b854e929e3520289cd3d0b80f26c3aad Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Sun, 18 Aug 2024 14:53:38 -0400 Subject: [PATCH] starting test using spf.zoo domain --- app/chkdns.c | 5 +++- lib/gesspf.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++--- lib/subafn.c | 59 ++++++++++++++++++++++++++++++++++++++++++-- lib/subafn.h | 4 +++ lib/subrou.c | 2 +- 5 files changed, 131 insertions(+), 8 deletions(-) diff --git a/app/chkdns.c b/app/chkdns.c index 189d804..cfa8553 100644 --- a/app/chkdns.c +++ b/app/chkdns.c @@ -71,8 +71,11 @@ status=true; try=0; afnnum=afn_getipnum(peerip); spf=spf_getstatus(&try,domain,afnnum); -if (strcmp(spfstr,spf_spfASCII(spf))!=0) +if (strcmp(spfstr,spf_spfASCII(spf))!=0) { + (void) rou_alert(2,"expected SPF status=<%s>, got<%s>", + spfstr,spf_spfASCII(spf)); status=false; + } afnnum=afn_freeipnum(afnnum); return status; } diff --git a/lib/gesspf.c b/lib/gesspf.c index 8c777a4..be272da 100644 --- a/lib/gesspf.c +++ b/lib/gesspf.c @@ -22,7 +22,7 @@ typedef enum { //mechanism definition mch_all, //"all" - mch_a, //'a' + mch_addr, //'a' mch_exists, //"existe" mch_include, //"include" mch_ip4, //"ip4" @@ -45,7 +45,7 @@ static _Bool modopen; //module open/close status static void dbgspf(int dbg,char *proc,char *domain,AFNTYP *afnnum,char *seq,SPFENU status) { -if (debug>-dbg) { +if (debug>=dbg) { (void) rou_alert(dbg,"Origin=\"%s\"\n" "\tCurrent SPF Value <%s>\n" "\tPeer IP=[%s]\n" @@ -156,6 +156,61 @@ return seq; */ /********************************************************/ /* */ +/* Procedure to check the SPF A value againt the */ +/* current afnnum. */ +/* return true if successful. */ +/* */ +/********************************************************/ +static _Bool checkaddr(char *domain,AFNTYP *afnnum,char *addr) + +{ +#define OPEP "gesspf.c:checkaddr" + +_Bool found; +int cidr; +char *addrdom; +int phase; +_Bool proceed; + +found=false; +cidr=128; +addrdom=domain; +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //getting the list of MX + if (addrdom!=(char *)0) { //always + char *ptr; + + if ((ptr=strchr(addrdom,'/'))!=(char *)0) { + *ptr='\000'; + cidr=atoi(ptr+1); + } + if ((ptr=strchr(addrdom,':'))!=(char *)0) { + *ptr='\000'; + addrdom=ptr+1; + } + } + break; + case 1 : //getting the list of MX for mxdomain + (void) printf("JMPDBG, checkaddr got <%s> cidr='%d'",addrdom,cidr); + break; + default : //SAFE guard + proceed=false; + break; + } + phase++; + } +return found; + +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ /* Procedure to check the SPF MX values againt the */ /* current afnnum. */ /* return true if successful. */ @@ -228,6 +283,8 @@ while (proceed==true) { OPEP,mxlist[i]->mxname); continue; } + (void) printf("JMPDBG addr=<%s> peer=<%s> cidr='%d'\n", + addrnum->strnumip,afnnum->strnumip,cidr); switch (afn_cmpipnum(addrnum,afnnum,cidr)) { case false : //not found break; @@ -386,6 +443,10 @@ while (proceed==true) { case mch_all : //usually last in sequence spf=locspf; break; + case mch_addr : //check IP addr + if (checkaddr(domain,afnnum,seq)==true) + spf=locspf; + break; case mch_mx : //This is a MX refrence if (checkmx(domain,afnnum,seq)==true) spf=locspf; @@ -394,7 +455,7 @@ while (proceed==true) { case mch_ip6 : //This is IPV6 number if (checkip(domain,afnnum,seq+1)==true) spf=locspf; - (void) dbgspf(2,"checkip",domain,afnnum,seq+1,spf); + (void) dbgspf(5,"checkip",domain,afnnum,seq+1,spf); break; case mch_include : //include sub domain if (seq[0]==':') { @@ -468,7 +529,7 @@ while (proceed==true) { if (spf!=spf_neutral) break; //Found a pass status, breaking loop } - (void) dbgspf(2,"is_peerip_ok",domain,afnnum,ref,spf); + (void) dbgspf(5,"is_peerip_ok",domain,afnnum,ref,spf); } break; default : //SAFE Guard diff --git a/lib/subafn.c b/lib/subafn.c index 70f8396..8da9840 100644 --- a/lib/subafn.c +++ b/lib/subafn.c @@ -175,6 +175,7 @@ if (rp!=(struct addrinfo *)0) { } if (ptr!=(u_char *)0) (void) memmove(afnnum->ip,ptr,afnnum->afnmask/8); + afnnum->strnumip=afn_getstrip(afnnum); } return afnnum; } @@ -210,7 +211,6 @@ while (proceed==true) { break; case 1 : /*lets say ip format ok */ afnnum=(AFNTYP *)calloc(1,sizeof(AFNTYP)); - afnnum->strnumip=strdup(cleanipstr); afnnum->afntype=AF_INET; afnnum->afnmask=32; if (strchr(cleanipstr,':')==(char *)0) @@ -227,8 +227,13 @@ while (proceed==true) { cleanipstr=ZIPV4; break; case 4 : //lets convert it - if (inet_pton(afnnum->afntype,cleanipstr,afnnum->ip)<=0) + if (inet_pton(afnnum->afntype,cleanipstr,afnnum->ip)<=0) { afnnum=afn_freeipnum(afnnum); + phase=999; //no need to go further + } + break; + case 5 : //lets convert it + afnnum->strnumip=afn_getstrip(afnnum); break; default : /*SAFE guard */ proceed=false; @@ -239,6 +244,56 @@ while (proceed==true) { return afnnum; #undef ZIPV4 +#undef OPEP +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to normalize IP number format */ +/* Memory allocated need to be freed later */ +/* on. */ +/* */ +/* */ +/********************************************************/ +char *afn_getstrip(AFNTYP *afnnum) + +{ +#define OPEP "subafn.c:afn_getstrip" + +char *ipnorm; +char buffer[300]; + +ipnorm=(char *)0; +(void) strcpy(buffer,""); +if (afnnum!=(AFNTYP *)0) { + switch (afnnum->afntype) { + case AF_INET : + (void) snprintf(buffer,sizeof(buffer),"%03d.%03d.%03d.%03d", + afnnum->ip[0],afnnum->ip[1], + afnnum->ip[2],afnnum->ip[3]); + break; + case AF_INET6 : + (void) snprintf(buffer,sizeof(buffer),"%02x%02x:%02x%02x:%02x%02x:%02x%02x:" + "%02x%02x:%02x%02x:%02x%02x:%02x%02x", + afnnum->ip[0],afnnum->ip[1], + afnnum->ip[2],afnnum->ip[3], + afnnum->ip[4],afnnum->ip[5], + afnnum->ip[6],afnnum->ip[7], + afnnum->ip[8],afnnum->ip[9], + afnnum->ip[10],afnnum->ip[11], + afnnum->ip[12],afnnum->ip[13], + afnnum->ip[14],afnnum->ip[15]); + break; + default : + (void) fprintf(stderr,"%s, unable to fin IP type (bug?)",OPEP); + } + } +if (strlen(buffer)>0) + ipnorm=strdup(buffer); +return ipnorm; + #undef OPEP } /* diff --git a/lib/subafn.h b/lib/subafn.h index 638713b..9363104 100644 --- a/lib/subafn.h +++ b/lib/subafn.h @@ -32,6 +32,10 @@ extern AFNTYP *afn_freeipnum(AFNTYP *afnnum); //IP number extern char *afn_stripnum(AFNTYP *afnnum); +//procedure to extract a dynamic string with the +//clean IP number from a AFNTYP struct. +extern char *afn_getstrip(AFNTYP *afnnum); + //procedure to convert a addrinfo struct to an //IP number in AFNTYP structure. extern AFNTYP *afn_getaddrinfo(struct addrinfo *rp); diff --git a/lib/subrou.c b/lib/subrou.c index 2f43ae0..a88f64c 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -21,7 +21,7 @@ //version definition #define VERSION "0.4.2" -#define RELEASE "22" +#define RELEASE "23" //Public variables PUBLIC int debug=0; //debug level -- 2.47.3