]> SAFE projects GIT repository - jmp/mailleur/commitdiff
Better log display, scarmt.c work start to be good enough
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Mon, 14 Jul 2025 17:35:15 +0000 (13:35 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Mon, 14 Jul 2025 17:35:15 +0000 (13:35 -0400)
app/scarmt.c
lib/devlog.c
lib/devsql.c
lib/devsql.h
lib/gessql.c
lib/gessql.h
lib/subrou.c
lib/unimar.c
lib/unimar.h
lib/unipos.c
lib/unipos.h

index 9aea67f18ed9ee8671733db0aa2001dc187c9453..857ac23e4591ccbbe017cec0442028ba6cc2227f 100644 (file)
@@ -86,18 +86,22 @@ while (proceed==true) {
           listed=dns_is_blacklisted(*dnsbls,reversip);
           (void) snprintf(cst,sizeof(cst),"IP[%02d]: %s\t",num,rmtip);
           if (listed!=(char *)0) {
-            (void) rou_asprintf(&report,"%s listed->'%s'",cst,listed);
+            (void) rou_asprintf(&report,"%s%s",cst,listed);
             srv->credit-=10;
             if (srv->listing==(char *)0)
               srv->listing=strdup(listed);
             }
           else
-            (void) rou_asprintf(&report,"%s NOT listed by %s",cst,*dnsbls);
+            (void) rou_asprintf(&report,"%s%s -> NOT listed",cst,*dnsbls);
           (void) log_fprintlog(scanref->logptr,true,report); 
           report=rou_freestr(report);
           listed=rou_freestr(listed);
           dnsbls++;
           }
+        (void) log_fprintlog(scanref->logptr,true,"IP[%02d]: %s\tCurrent "
+                                                  "credit='%3d'",
+                                                  num,rmtip,srv->credit);
+        (void) log_fprintlog(scanref->logptr,false,"");
         }
       break;
     case 3      :       //updating record
@@ -210,7 +214,7 @@ return dnsbls;
 /*      remote ip credibility.                          */
 /*                                                     */
 /********************************************************/
-static void doscanning()
+static void doscanning(int frequency)
 
 {
 #define OPEP    "scarmt.c:doscanning,"
@@ -227,9 +231,9 @@ cycle=0;
 (void) memset(&scanref,'\000',sizeof(scanref));
 dnsbls=load_dnsbls();
 rmtip=(char **)0;
-delay=10;       //10 second sleeping time
+delay=60;       //60 second sleeping time
 if (debug>0)
-  delay=2;
+  delay/=10;
 phase=0;
 proceed=(dnsbls!=(char **)0);
 while (proceed==true) {
@@ -262,7 +266,7 @@ while (proceed==true) {
         }
       break;
     case 5      :       //check for new entry
-      if ((rmtip=sql_getnewrmtip(scanref.sqlptr))==(char **)0) {
+      if ((rmtip=sql_getnewrmtip(scanref.sqlptr,frequency))==(char **)0) {
         scanref.sqlptr=sql_closesql(scanref.sqlptr);
         phase=0;        //nothing to be done
         }
@@ -320,8 +324,8 @@ dnsbls=(char **)rou_freelist((void **)dnsbls,(genfree_t)rou_freestr);
 int main(int argc,char *argv[])
 
 {
-#define OPEP    "scarmt.c:main,"
-
+#define OPEP            "scarmt.c:main,"
+#define FREQUENCY       -(60*10)        //10 minutes
 int status;
 ARGTYP *params;
 int phase;
@@ -354,7 +358,7 @@ while (proceed==true) {
         }
       break;
     case 3      :       //do program main purpose
-      (void) doscanning();
+      (void) doscanning(FREQUENCY);
       break;
     default     :       //end of task
       params=par_freeparams(params);
index aacb8ebb93c93b4ffbee5596d03926dd220521ab..7eda56118b980c99f7352751d6e435888951e579 100644 (file)
@@ -347,7 +347,7 @@ while (proceed==true) {
         }
       break;
     case 2      :       //formating the line
-      if ((taille=rou_vasprintf(&line,format,args))<=0) {
+      if ((taille=rou_vasprintf(&line,format,args))<0) {
         (void) rou_alert(0,"%s Unable to format <%s> (Bug!?)",OPEP,format);
         phase=999;
         }
index 61d1f484384b8b1fdee7a6f167c25bc9469a097a..bf68dc4fbe01065eafa765f517f057b9e5cff2e1 100644 (file)
@@ -75,6 +75,32 @@ return value;
 */
 /********************************************************/
 /*                                                      */
+/*      Procedure to create an SQL pointer for proper   */
+/*      database type.                                  */
+/*                                                      */
+/********************************************************/
+static SQLTYP *gettyp(const char *dbtype)
+
+{
+const char *db[]={"POSTGRESQL","MYSQL",(const char *)0};
+
+SQLTYP *sql;
+
+sql=(SQLTYP *)0;
+for (int i=0;db[i]!=(const char *)0;i++) {
+  if (strcmp(db[i],dbtype)==0) {
+    sql=(SQLTYP *)calloc(1,sizeof(SQLTYP));
+    sql->sqldb=(SQLDBTYP)i;
+    break;      //data base type found
+    }
+  }
+return sql;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
 /*      Procedure to make sure a string to be use to    */
 /*      access database is clean.                       */
 /*      (no character to be misunderstood by database)  */
@@ -140,26 +166,39 @@ return gooddata;
 */
 /********************************************************/
 /*                                                      */
-/*      Procedure to create an SQL pointer for proper   */
-/*      database type.                                  */
+/*      Procedure to return a dynamicaly allocated      */
+/*      char * with an SQL date computation sequence    */
 /*                                                      */
 /********************************************************/
-static SQLTYP *gettyp(const char *dbtype)
+PUBLIC char *sql_caldate(SQLPTR *sqlptr,char *expression,int second)
 
 {
-const char *db[]={"POSTGRESQL","MYSQL",(const char *)0};
+#define OPEP    "devsql.c:sql_caldate,"
 
-SQLTYP *sql;
+char *seq;
 
-sql=(SQLTYP *)0;
-for (int i=0;db[i]!=(const char *)0;i++) {
-  if (strcmp(db[i],dbtype)==0) {
-    sql=(SQLTYP *)calloc(1,sizeof(SQLTYP));
-    sql->sqldb=(SQLDBTYP)i;
-    break;      //data base type found
+seq=(char *)0;
+if (sqlptr!=(SQLPTR *)0) {
+  SQLTYP *sql;
+
+  sql=(SQLTYP *)sqlptr;
+  switch(sql->sqldb) {
+    case db_postgres  :
+      seq=pos_caldate(expression,second);
+      break;
+    case db_maria      :
+      seq=mar_caldate(expression,second);
+      break;
+    default            :
+      (void) rou_alert(0,"%s Unexpected db type='%d' (BUG!?)",
+                          OPEP,(int)sql->sqldb);
+      seq=strdup("");
+      break;
     }
   }
-return sql;
+return seq;
+
+#undef  OPEP
 }
 /*
 \f
index ec8eb8d37634548214b6eae09ab40396cbc54c20..05ecb235b026b5d8154be0d72b75d14c6d76952f 100644 (file)
@@ -20,6 +20,10 @@ typedef void SQLRES;
 //as key search by database
 extern char *sql_gooddata(SQLPTR *sqlptr,char *key);
 
+//procedure to return a dynamicaly allocated string
+//to do SQL compute a date with a delta in second
+extern char *sql_caldate(SQLPTR *sqlptr,char *expression,int second);
+
 //to connect a remote SQL server
 extern SQLPTR *sql_opensql();
 
index 0c214dfde2078af01572236de9f691d7dec7ceca..293c5d058a6dced944b9c55ce1721c8ce3b1ff04 100644 (file)
@@ -745,30 +745,33 @@ return isok;
 /*      remote IP to be scanned to establish credibilty.*/
 /*                                                      */
 /********************************************************/
-PUBLIC char **sql_getnewrmtip(SQLPTR *sqlptr)
+PUBLIC char **sql_getnewrmtip(SQLPTR *sqlptr,int frequency)
 
 {
 #define OPEP    "gessql.c:sql_getnewrmtip,"
 #define DATLIM  "2020-01-01"
 
-static char *cmd="select remoteip from "RMTTBL" where lastscan<='"DATLIM"' order by lastupdate asc limit 5";
+static char *cmd="select remoteip from "RMTTBL" WHERE lastscan<%s "
+                 "order by lastscan asc limit 10";
 
 char **rmtip;
 SQLRES *rs;
 int num;
+char *delta;
 int phase;
 _Bool proceed;
 
 rmtip=(char **)0;
 rs=(SQLRES *)0;
 num=0;
+delta=sql_caldate(sqlptr,"lastupdate",frequency);
 phase=0;
 proceed=true;
 while (proceed==true) {
   switch (phase) {
     case 0      :       //do we have some remote IP
-      if ((rs=sql_gettupple(sqlptr,cmd))==(SQLRES *)0) {
-        (void) rou_alert(0,"%s unable to get remoteip list (Databse BUG?)",OPEP);
+      if ((rs=sql_gettupple(sqlptr,cmd,delta))==(SQLRES *)0) {
+        (void) rou_alert(0,"%s unable to get remoteip list (Database BUG?)",OPEP);
         phase=999;      //No need to go further
         }
       break;
@@ -778,7 +781,6 @@ while (proceed==true) {
         }
       break;
     case 2      :       //extracting list
-      (void) rou_alert(0,"JMPDG NUM='%d'",num);
       for (int i=0;i<num;i++) {
         const char *ip;
 
@@ -796,6 +798,7 @@ while (proceed==true) {
     }
   phase++;
   }
+delta=rou_freestr(delta);
 return rmtip;
 
 #undef  DATLIM
index 87937c18346f5acd461611732848b9c5d7f5a62c..9b09d28a41804501621244e708407f10824bdd12 100644 (file)
@@ -31,5 +31,5 @@ extern _Bool sql_newconnect(SQLPTR *sqlptr,char *rmtip,int delta);
 extern _Bool sql_mngremote(SQLPTR *sqlptr,SQLENUM action,char *key,SRVTYP **srv);
 
 //procedure to collect a list of "to be checked" remote IP
-extern char **sql_getnewrmtip(SQLPTR *sqlptr);
+extern char **sql_getnewrmtip(SQLPTR *sqlptr,int frequency);
 #endif
index 6b8421ba6f6da7e03139c473c198ab2bcf0db875..5e0015c13f6b3d3d2c1feac737acfed9cabf1655 100644 (file)
@@ -118,10 +118,15 @@ int taille;
 va_list sup;
 char loc[10];
 
+taille=0;
 (void) va_copy(sup,ap);
-if ((taille=vsnprintf(loc,4,fmt,ap))>0) {
-  *str=calloc(taille+2,sizeof(char));
-  taille=vsnprintf(*str,taille+1,fmt,sup);
+if (strlen(fmt)==0) 
+  *str=strdup("");
+else {
+  if ((taille=vsnprintf(loc,4,fmt,ap))>0) {
+    *str=calloc(taille+2,sizeof(char));
+    taille=vsnprintf(*str,taille+1,fmt,sup);
+    }
   }
 return taille;
 }
@@ -156,20 +161,26 @@ return realm;
 PUBLIC int rou_asprintf(char **str,const char *fmt,...)
 
 {
-va_list args;
 int taille;
-char loc[10];
 
-va_start(args,fmt);
-if ((taille=vsnprintf(loc,4,fmt,args))>0) {
-  va_list argssup;
+taille=0;
+if (strlen(fmt)==0)
+  *str=strdup("");
+else {
+  va_list args;
+  va_start(args,fmt);
+  char loc[10];
 
-  va_start(argssup,fmt);
-  *str=calloc(taille+2,sizeof(char));
-  taille=vsnprintf(*str,taille+1,fmt,argssup);
-  va_end(argssup);
+  if ((taille=vsnprintf(loc,4,fmt,args))>0) {
+    va_list argssup;
+  
+    va_start(argssup,fmt);
+    *str=calloc(taille+2,sizeof(char));
+    taille=vsnprintf(*str,taille+1,fmt,argssup);
+    va_end(argssup);
+    }
+  va_end(args);
   }
-va_end(args);
 return taille;
 }
 /*
index b9f2bd31f5aad9cfd3a36bc3229f651493b600f2..2c817c2805407e3a39ea0df9a8175a30c326a9fb 100644 (file)
   #define DB_MYSQL
 #endif
 
+/*
+\f
+*/
+/********************************************************/
+/*                                                     */
+/*     Procedure to detect and 'clean' any single quote*/
+/*     within a string, this is mandatory for string   */
+/*      variable in the database.                       */
+/*     Single quote and double quote are translated to */
+/*      '\'' and '\"'                                   */
+/*                                                     */
+/********************************************************/
+char *mar_cleanquote(char *dstr)
+
+{
+char *cleanstr;
+int taille;
+char *ptr;
+
+taille=3;
+cleanstr=calloc(3,sizeof(char));
+cleanstr[0]='\'';
+if (dstr!=(char *)0) {
+  char *nptr;
+
+  ptr=dstr;
+  while (((nptr=strchr(ptr,'\''))!=(char *)0)||((nptr=strchr(ptr,'\"'))!=(char *)0)) {
+    char lit[3];
+    char *newclean;
+
+    (void) strcpy(lit,"\\'");
+    if (*nptr=='"')
+      lit[1]='"';
+    *nptr='\000';      /*to segment string     */
+    taille+=strlen(ptr)+2;
+    if ((newclean=(char *)realloc(cleanstr,taille))!=(char *)0) {
+      cleanstr=newclean;
+      (void) strcat(cleanstr,ptr);
+      (void) strcat(cleanstr,lit);
+      *nptr=lit[1];    /*to glue back string   */
+      ptr=nptr+1;
+      }
+    }
+  if (*ptr!='\000') {
+    char *newclean;
+
+    taille+=strlen(ptr);
+    if ((newclean=(char *)realloc(cleanstr,taille))!=(char *)0) {
+      cleanstr=newclean;
+      (void) strcat(cleanstr,ptr);
+      }
+    }
+  }
+(void) strcat(cleanstr,"'");
+return cleanstr;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to return a dynamicaly allocated      */
+/*      char * with an MYSQL date computation sequenc   */
+/*                                                      */
+/********************************************************/
+PUBLIC char *mar_caldate(char *expression,int second)
+
+{
+char *seq;
+
+seq=(char *)0;
+(void) rou_asprintf(&seq,"(addtime(%s,%d)",expression,second);
+return seq;
+}
 /*
 \f
 */
@@ -97,62 +171,6 @@ return marptr;
 */
 /********************************************************/
 /*                                                     */
-/*     Procedure to detect and 'clean' any single quote*/
-/*     within a string, this is mandatory for string   */
-/*      variable in the database.                       */
-/*     Single quote and double quote are translated to */
-/*      '\'' and '\"'                                   */
-/*                                                     */
-/********************************************************/
-char *mar_cleanquote(char *dstr)
-
-{
-char *cleanstr;
-int taille;
-char *ptr;
-
-taille=3;
-cleanstr=calloc(3,sizeof(char));
-cleanstr[0]='\'';
-if (dstr!=(char *)0) {
-  char *nptr;
-
-  ptr=dstr;
-  while (((nptr=strchr(ptr,'\''))!=(char *)0)||((nptr=strchr(ptr,'\"'))!=(char *)0)) {
-    char lit[3];
-    char *newclean;
-
-    (void) strcpy(lit,"\\'");
-    if (*nptr=='"')
-      lit[1]='"';
-    *nptr='\000';      /*to segment string     */
-    taille+=strlen(ptr)+2;
-    if ((newclean=(char *)realloc(cleanstr,taille))!=(char *)0) {
-      cleanstr=newclean;
-      (void) strcat(cleanstr,ptr);
-      (void) strcat(cleanstr,lit);
-      *nptr=lit[1];    /*to glue back string   */
-      ptr=nptr+1;
-      }
-    }
-  if (*ptr!='\000') {
-    char *newclean;
-
-    taille+=strlen(ptr);
-    if ((newclean=(char *)realloc(cleanstr,taille))!=(char *)0) {
-      cleanstr=newclean;
-      (void) strcat(cleanstr,ptr);
-      }
-    }
-  }
-(void) strcat(cleanstr,"'");
-return cleanstr;
-}
-/*
-\f
-*/
-/********************************************************/
-/*                                                     */
 /*      Procedure to return a unix time in acsii        */
 /*      format (data-base compatible).                  */
 /*                                                     */
index 0dc555b4a8374c1ec5ae875d0eeee03657ebeeca..ff70733800ac6f14a377c91cb5e1c5fc8d68342e 100644 (file)
@@ -14,15 +14,19 @@ typedef void MARPTR;
 //reference to a MariaDB result
 typedef void MARRES;
 
+//Procedure to detect and 'clean' any single quote within a string
+extern char *mar_cleanquote(char *sequence);
+
+//procedure to return a dynamicaly allocated string
+//to do SQL compute a date with a delta in second
+extern char *mar_caldate(char *expression,int second);
+
 //Opening a postscript database
 extern MARPTR *mar_opensql(const char *host,const char *sqlport,const char *dbname);
 
 //closing a postscript database
 extern MARPTR *mar_closesql(MARPTR *marptr);
 
-//Procedure to detect and 'clean' any single quote within a string
-extern char *mar_cleanquote(char *sequence);
-
 //converting a time to a database representation
 extern const char *mar_fromunixtime(time_t timestamp);
 
index 16b27b3f358764f8f9802a29b8b528cd1c2deca5..bb73b21ae82b2dabf97d301446ade8d63c9d45fd 100644 (file)
@@ -73,6 +73,78 @@ return statut;
 \f
 */
 /********************************************************/
+/*                                                     */
+/*     Procedure to detect and 'clean' any single quote*/
+/*     within a string, this is mandatory for string   */
+/*      variable in the database.                       */
+/*     Single quote "'" are doubled becoming "''"      */
+/*                                                     */
+/********************************************************/
+PUBLIC char *pos_cleanquote(register char *dstr)
+
+{
+char *cleanstr;
+int taille;
+
+cleanstr=strdup("E'");
+taille=strlen(cleanstr)+2;
+if (dstr!=(char *)0) {
+  int max;
+  int i,p;
+  char *newclean;
+
+  /*to double char ' in the string                     */
+  max=strlen((const char *)dstr);
+  taille+=max;
+  if ((newclean=(char *)realloc(cleanstr,taille))!=(char *)0) {
+    cleanstr=newclean;
+    p=strlen(cleanstr);
+    for (i=0;i<max;i++,p++,dstr++) {
+      switch (*dstr) {
+        case '\\'              :
+        case '\''              :
+          taille++;
+          if ((newclean=(char *)realloc(cleanstr,taille))!=(char *)0) {
+           cleanstr=newclean;
+            cleanstr=(char *)realloc(cleanstr,taille);
+            cleanstr[p]=*dstr;
+           }
+          p++;
+          break;
+        default                :
+         break;
+        }
+      cleanstr[p]=*dstr;
+      }
+    cleanstr[p]='\000';
+    }
+  }
+(void) strcat(cleanstr,"'");
+return cleanstr;
+}
+/*
+\f
+*/
+/********************************************************/
+/*                                                      */
+/*      Procedure to return a dynamicaly allocated      */
+/*      char * with an POSTGRESQL date computation      */
+/*      sequence                                        */
+/*                                                      */
+/********************************************************/
+PUBLIC char *pos_caldate(char *expression,int second)
+
+{
+char *seq;
+
+seq=(char *)0;
+(void) rou_asprintf(&seq,"(%s + interval '%d sec')",expression,second);
+return seq;
+}
+/*
+\f
+*/
+/********************************************************/
 /*                                                      */
 /*      Procedure to establish a link with the          */
 /*      postgresq SQL server.                           */
@@ -148,59 +220,6 @@ return posptr;
 */
 /********************************************************/
 /*                                                     */
-/*     Procedure to detect and 'clean' any single quote*/
-/*     within a string, this is mandatory for string   */
-/*      variable in the database.                       */
-/*     Single quote "'" are doubled becoming "''"      */
-/*                                                     */
-/********************************************************/
-char *pos_cleanquote(register char *dstr)
-
-{
-char *cleanstr;
-int taille;
-
-cleanstr=strdup("E'");
-taille=strlen(cleanstr)+2;
-if (dstr!=(char *)0) {
-  int max;
-  int i,p;
-  char *newclean;
-
-  /*to double char ' in the string                     */
-  max=strlen((const char *)dstr);
-  taille+=max;
-  if ((newclean=(char *)realloc(cleanstr,taille))!=(char *)0) {
-    cleanstr=newclean;
-    p=strlen(cleanstr);
-    for (i=0;i<max;i++,p++,dstr++) {
-      switch (*dstr) {
-        case '\\'              :
-        case '\''              :
-          taille++;
-          if ((newclean=(char *)realloc(cleanstr,taille))!=(char *)0) {
-           cleanstr=newclean;
-            cleanstr=(char *)realloc(cleanstr,taille);
-            cleanstr[p]=*dstr;
-           }
-          p++;
-          break;
-        default                :
-         break;
-        }
-      cleanstr[p]=*dstr;
-      }
-    cleanstr[p]='\000';
-    }
-  }
-(void) strcat(cleanstr,"'");
-return cleanstr;
-}
-/*
-\f
-*/
-/********************************************************/
-/*                                                     */
 /*      Procedure to return a unix time in acsii        */
 /*      format (data-base compatible).                  */
 /*                                                     */
index dccfd87ec45f08623a579374eb277dcd7403282d..89dc2a9382c747d47a37b5aef01baf63c563b574 100644 (file)
@@ -14,15 +14,19 @@ typedef void POSPTR;
 //reference to a POSTGRESQL result
 typedef void POSRES;
 
+//Procedure to detect and 'clean' any single quote within a string
+extern char *pos_cleanquote(char *sequence);
+
+//procedure to return a dynamicaly allocated string
+//to do SQL compute a date with a delta in second
+extern char *pos_caldate(char *expression,int second);
+
 //Opening a postscript database
 extern POSPTR *pos_opensql(const char *host,const char *sqlport,const char *dbname);
 
 //closing a postscript database
 extern POSPTR *pos_closesql(POSPTR *posptr);
 
-//Procedure to detect and 'clean' any single quote within a string
-extern char *pos_cleanquote(char *sequence);
-
 //converting a time to a database representation
 extern const char *pos_fromunixtime(time_t timestamp);