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
/* remote ip credibility. */
/* */
/********************************************************/
-static void doscanning()
+static void doscanning(int frequency)
{
#define OPEP "scarmt.c:doscanning,"
(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) {
}
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
}
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;
}
break;
case 3 : //do program main purpose
- (void) doscanning();
+ (void) doscanning(FREQUENCY);
break;
default : //end of task
params=par_freeparams(params);
}
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;
}
*/
/********************************************************/
/* */
+/* 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) */
*/
/********************************************************/
/* */
-/* 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
//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();
/* 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;
}
break;
case 2 : //extracting list
- (void) rou_alert(0,"JMPDG NUM='%d'",num);
for (int i=0;i<num;i++) {
const char *ip;
}
phase++;
}
+delta=rou_freestr(delta);
return rmtip;
#undef DATLIM
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
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;
}
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;
}
/*
#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
*/
*/
/********************************************************/
/* */
-/* 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). */
/* */
//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);
\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. */
*/
/********************************************************/
/* */
-/* 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). */
/* */
//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);