From: Jean-Marc Pigeon (Delson) Date: Thu, 15 Aug 2024 10:54:40 +0000 (-0400) Subject: Adding module subafn X-Git-Tag: tag-0.4.2-end~31 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=62bbd43260f0510628b159ae94f2d4f9235e6275;p=jmp%2Fmailleur Adding module subafn --- diff --git a/app/chkdns.c b/app/chkdns.c index 375963d..19b7d01 100644 --- a/app/chkdns.c +++ b/app/chkdns.c @@ -59,7 +59,7 @@ while (proceed==true) { SPFENU spf; spf=dns_get_spf_status(argv[1],argv[2]); - (void) rou_alert(0,"%s %s ==> %s",argv[1],argv[2],dns_spfascii(spf)); + (void) rou_alert(0,"%s %s ==> %s",argv[1],argv[2],dns_spfASCII(spf)); } break; case 3 : //doing main tash diff --git a/lib/Makefile b/lib/Makefile index e333e5a..e0109c3 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -22,7 +22,7 @@ OBJS= \ devlog.o devsoc.o \ unidns.o unieml.o \ unipar.o uniprc.o unisig.o unitls.o \ - subrou.o + subafn.o subrou.o objs : $(OBJS) @ ar -cr libAI.a $(OBJS) @@ -62,7 +62,7 @@ devsoc.o: \ devsoc.h devsoc.c unidns.o: \ - subrou.h \ + subafn.h subrou.h \ unidns.h unidns.c unieml.o: \ @@ -88,6 +88,9 @@ unitls.o: \ subrou.o: \ subrou.h subrou.c +subafn.o: \ + subafn.h subafn.c + lvleml.h: \ gestcp.h diff --git a/lib/subafn.c b/lib/subafn.c new file mode 100644 index 0000000..67f75c7 --- /dev/null +++ b/lib/subafn.c @@ -0,0 +1,49 @@ +// vim: smarttab tabstop=8 shiftwidth=2 expandtab +/********************************************************/ +/* */ +/* Sub kevel procedure to manage IP number. */ +/* */ +/********************************************************/ +#include +#include + +#include "subafn.h" + +#define PUBLIC //to specify public variable + +static _Bool modopen; //module open/close status +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to "open/close" module and do */ +/* homework purpose */ +/* return zero if everything right */ +/* */ +/********************************************************/ +PUBLIC int afn_modesubafn(_Bool mode) + +{ +#define OPEP "subafn.c:afn_modesubafn" + +int status; + +status=0; +if (mode!=modopen) { + switch ((int)mode) { + case true : + break; + case false : + break; + default : + (void) fprintf(stderr,"Calling %s with wrong mode='%d' (Bug?!):", + OPEP,(int)mode); + status=-1; + break; + } + modopen=mode; + } +return status; +#undef OPEP +} diff --git a/lib/subafn.h b/lib/subafn.h new file mode 100644 index 0000000..d2e3460 --- /dev/null +++ b/lib/subafn.h @@ -0,0 +1,28 @@ +// vim: smarttab tabstop=8 shiftwidth=2 expandtab +/************************************************/ +/* */ +/* Define all routine to manage all */ +/* procedure to manage IP number conversion*/ +/* */ +/************************************************/ +#ifndef SUBAFN +#define SUBAFN + +#include + +//space to store (at least) IPV6 number +#define AFT sizeof(struct in6_addr) + +typedef struct { + int afntype; /*IP type */ + int afnmask; /*IP Mask */ + u_char ip[AFT]; /*IP number */ + }AFNTYP; + +//procedure to convert a IP as string to an AFNTYP structure +extern AFNTYP *afn_getipnum(char *cleanipstr); + +//homework to be done before starting/stoping module. +extern int afn_modesubafn(_Bool mode); + +#endif diff --git a/lib/subrou.c b/lib/subrou.c index 61d85ec..3335e56 100644 --- a/lib/subrou.c +++ b/lib/subrou.c @@ -21,7 +21,7 @@ //version definition #define VERSION "0.4.2" -#define RELEASE "4" +#define RELEASE "5" //Public variables PUBLIC int debug=0; //debug level diff --git a/lib/subrou.h b/lib/subrou.h index 2e583bc..fd44000 100644 --- a/lib/subrou.h +++ b/lib/subrou.h @@ -15,6 +15,8 @@ #define APPNAME "maild" //application name +#define PUBLIC //to specify public variable + typedef void (*freehandler_t)(void *); typedef struct timespec TIMESPEC; @@ -93,5 +95,4 @@ extern void rou_core_dump(const char *fmt,...); //homework to be done before starting/stoping module. extern int rou_modesubrou(_Bool mode); -#define PUBLIC //to specify public variable #endif diff --git a/lib/unidns.c b/lib/unidns.c index 649c154..7489fa5 100644 --- a/lib/unidns.c +++ b/lib/unidns.c @@ -13,6 +13,7 @@ #include #include +#include "subafn.h" #include "subrou.h" #include "unidns.h" @@ -27,11 +28,11 @@ static _Bool modopen; //module open/close status /* */ -/************************************************/ -/* */ -/* Procedure to query DNS information */ -/* */ -/************************************************/ +/********************************************************/ +/* */ +/* Procedure to query DNS information */ +/* */ +/********************************************************/ static int myquery(const char *dname,int class,int type, unsigned char *answer, int anslen) @@ -68,13 +69,12 @@ return got; /* */ -/************************************************/ -/* */ -/* Procedure to extract text information */ -/* from DNS record. */ -/* SMTP client status. */ -/* */ -/************************************************/ +/********************************************************/ +/* */ +/* Procedure to extract text information */ +/* from DNS record. */ +/* */ +/********************************************************/ static char **extracting(RSPTYP *rsp,int rsplen,char *request,char *field) { @@ -195,15 +195,14 @@ return list; /* ^L */ -/************************************************/ -/* */ -/* Procedure to extract a domain TXT */ -/* If the domain doesn't exist or domain */ -/* do not have a TXT, a void pointer is */ -/* returned. */ -/* */ -/* */ -/************************************************/ +/********************************************************/ +/* */ +/* Procedure to extract a domain TXT */ +/* If the domain doesn't exist or domain */ +/* do not have a TXT, a void pointer is */ +/* returned. */ +/* */ +/********************************************************/ static char **gettxt(char *domain,int ttype,char *field) { @@ -227,17 +226,16 @@ return inftxt; /* ^L */ -/************************************************/ -/* */ -/* Procedure to extract a hostname SPF */ -/* Search first for an SPF record, if not */ -/* implemented search for a TXT record */ -/* If the domain doesn't exist or domain */ -/* do not have a TXT or SPF, a void pointer*/ -/* is returned. */ -/* */ -/* */ -/************************************************/ +/********************************************************/ +/* */ +/* Procedure to extract a hostname SPF */ +/* Search first for an SPF record, if not */ +/* implemented search for a TXT record */ +/* If the domain doesn't exist or domain */ +/* do not have a TXT or SPF, a void pointer */ +/* is returned. */ +/* */ +/********************************************************/ static char **getspf(char *domain) { @@ -268,10 +266,36 @@ return infspf; /* ^L */ -/************************************************/ -/* */ -/* Procedure to extract a hostname TXT */ -/* If the domain doesn't exist or domain */ +/********************************************************/ +/* */ +/* Procedure to return check if an IP is part of */ +/* spf allowed IP */ +/* */ +/********************************************************/ +static SPFENU is_peerip_ok(char *peerip,char *spfrec) + +{ +SPFENU spf; +AFNTYP *afnnum; +int phase; +_Bool proceed; + +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //Converting the IP number + afnnum=afn_getipnum(peerip); + break; + default : //SAFE Guard + proceed=false; + break; + } + phase++; + } +spf=spf_neutral; +return spf; +} /* ^L */ @@ -280,9 +304,10 @@ return infspf; /* Procedure to return the SPF status string value */ /* */ /********************************************************/ -PUBLIC const char *dns_spfascii(SPFENU spf) +PUBLIC const char *dns_spfASCII(SPFENU spf) { +#define OPEP "unidns.c:dns_spfASCII" static char *spfascii[]= { "spf_pass", @@ -291,6 +316,7 @@ static char *spfascii[]= "spf_neutral", "spf_timeout", "spf_missing", + "spf_permerr", "spf_unknown" }; @@ -304,13 +330,17 @@ switch (spf) { case spf_neutral : case spf_timeout : case spf_missing : + case spf_permerr : case spf_unknown : ascii=spfascii[spf]; break; default : + (void) rou_alert(0,"%s, Unexpected '%d' SPF status (Bug!)",OPEP,spf); break; } return ascii; + +#undef OPEP } /* ^L @@ -331,7 +361,7 @@ char **list; int phase; _Bool proceed; -spf=spf_unknown; +spf=spf_permerr; list=(char **)0; phase=0; proceed=true; @@ -344,22 +374,20 @@ while (proceed==true) { } break; case 1 : //get the spf LIST related to SPF - if ((list=getspf(domain))==(char **)0) { - phase=999; - } + if ((list=getspf(domain))==(char **)0) + phase=999; //trouble trouble break; - case 2 : //gett the spf LIST related to SPF - if (list!=(char **)0) { - char **ptr; - - ptr=list; - while (*ptr!=(char *)0) { - (void) printf("JMPDBG <%s>\n",*ptr); - ptr++; - } - list=(char **)rou_freelist((void **)list,(freehandler_t)rou_freestr); + case 2 : //check if we have onlye ONE SPF entry + if (rou_nbrlist((void **)list)>1) { + (void) rou_alert(0,"%s more than 1 SPF record for domain <%s>", + OPEP,domain); + phase=999; //trouble trouble } break; + case 3 : //gett the spf LIST related to SPF + spf=is_peerip_ok(peerip,list[0]); + list=(char **)rou_freelist((void **)list,(freehandler_t)rou_freestr); + break; default : //SAFE guard proceed=false; break; @@ -390,6 +418,7 @@ int status; status=0; if (mode!=modopen) { (void) rou_modesubrou(mode); + (void) afn_modesubafn(mode); switch ((int)mode) { case true : break; diff --git a/lib/unidns.h b/lib/unidns.h index 9b37cba..53fafa6 100644 --- a/lib/unidns.h +++ b/lib/unidns.h @@ -15,11 +15,12 @@ typedef enum { spf_neutral, //same status as no SPF spf_timeout, //no answer within time spf_missing, //SPF not found + spf_permerr, //SPF permanent error spf_unknown //Unknown SPF directive }SPFENU; //procedure to return spf status as an ASCII string -extern const char *dns_spfascii(SPFENU spf); +extern const char *dns_spfASCII(SPFENU spf); //get the SPF status for a specific domain and a remote peer IP extern SPFENU dns_get_spf_status(char *domain,char *peerip);