From 1e9bab7e3aab4e215ecccee273a727a4429e4159 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Mon, 14 Jul 2025 13:35:15 -0400 Subject: [PATCH] Better log display, scarmt.c work start to be good enough --- app/scarmt.c | 22 +++++---- lib/devlog.c | 2 +- lib/devsql.c | 63 ++++++++++++++++++++----- lib/devsql.h | 4 ++ lib/gessql.c | 13 ++++-- lib/gessql.h | 2 +- lib/subrou.c | 37 +++++++++------ lib/unimar.c | 130 +++++++++++++++++++++++++++++---------------------- lib/unimar.h | 10 ++-- lib/unipos.c | 125 ++++++++++++++++++++++++++++--------------------- lib/unipos.h | 10 ++-- 11 files changed, 262 insertions(+), 156 deletions(-) diff --git a/app/scarmt.c b/app/scarmt.c index 9aea67f..857ac23 100644 --- a/app/scarmt.c +++ b/app/scarmt.c @@ -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); diff --git a/lib/devlog.c b/lib/devlog.c index aacb8eb..7eda561 100644 --- a/lib/devlog.c +++ b/lib/devlog.c @@ -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; } diff --git a/lib/devsql.c b/lib/devsql.c index 61d1f48..bf68dc4 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -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; +} +/* + +*/ +/********************************************************/ +/* */ /* 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 } /* diff --git a/lib/devsql.h b/lib/devsql.h index ec8eb8d..05ecb23 100644 --- a/lib/devsql.h +++ b/lib/devsql.h @@ -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(); diff --git a/lib/gessql.c b/lib/gessql.c index 0c214df..293c5d0 100644 --- a/lib/gessql.c +++ b/lib/gessql.c @@ -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;i0) { - *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; } /* diff --git a/lib/unimar.c b/lib/unimar.c index b9f2bd3..2c817c2 100644 --- a/lib/unimar.c +++ b/lib/unimar.c @@ -21,6 +21,80 @@ #define DB_MYSQL #endif +/* + +*/ +/********************************************************/ +/* */ +/* 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; +} +/* + +*/ +/********************************************************/ +/* */ +/* 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; +} /* */ @@ -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; -} -/* - -*/ -/********************************************************/ -/* */ /* Procedure to return a unix time in acsii */ /* format (data-base compatible). */ /* */ diff --git a/lib/unimar.h b/lib/unimar.h index 0dc555b..ff70733 100644 --- a/lib/unimar.h +++ b/lib/unimar.h @@ -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); diff --git a/lib/unipos.c b/lib/unipos.c index 16b27b3..bb73b21 100644 --- a/lib/unipos.c +++ b/lib/unipos.c @@ -73,6 +73,78 @@ return statut; */ /********************************************************/ +/* */ +/* 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