]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Adding module subafn
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Thu, 15 Aug 2024 10:54:40 +0000 (06:54 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Thu, 15 Aug 2024 10:54:40 +0000 (06:54 -0400)
app/chkdns.c
lib/Makefile
lib/subafn.c [new file with mode: 0644]
lib/subafn.h [new file with mode: 0644]
lib/subrou.c
lib/subrou.h
lib/unidns.c
lib/unidns.h

index 375963d8d9303fb5799c5742edecc841ca161eda..19b7d01b40d6007542fc12dee84fc8e5f28220e8 100644 (file)
@@ -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
index e333e5a3d702bd49f96a27fdf73adfae8c7c7ff2..e0109c3036ed4f7cbcd2894e7779d0a33d074dcd 100644 (file)
@@ -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 (file)
index 0000000..67f75c7
--- /dev/null
@@ -0,0 +1,49 @@
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/*                                                     */
+/*     Sub kevel procedure to manage IP number.        */
+/*                                                     */
+/********************************************************/
+#include        <stdbool.h>
+#include        <stdio.h>
+
+#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 (file)
index 0000000..d2e3460
--- /dev/null
@@ -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       <netdb.h>
+
+//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
index 61d85ecc412e912dfe035bfffb2cea8361bd221d..3335e56ea64d308ae8259b5a09cf707ecbedbca7 100644 (file)
@@ -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
index 2e583bc77e065562d8871e65ab0e80099a7bd951..fd4400004989fcb1dab30c1d119c5a17c4865e75 100644 (file)
@@ -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
index 649c154cdee7f4da1038dcb8e36650d857a270d1..7489fa56e66f4bac1539c371d30243fec1841905 100644 (file)
@@ -13,6 +13,7 @@
 #include        <string.h>
 #include        <unistd.h>
 
+#include       "subafn.h"
 #include       "subrou.h"
 #include       "unidns.h"
 
@@ -27,11 +28,11 @@ static  _Bool modopen;          //module open/close status
 /*
 \f
 */
-/************************************************/
-/*                                             */
-/*     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;
 /*
 \f
 */
-/************************************************/
-/*                                             */
-/*     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;
index 9b37cba234e4fcb1a680268561990601547d6dd0..53fafa6cdd28ffd73da53dec790c433266f9bb46 100644 (file)
@@ -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);