From: Jean-Marc Pigeon (Delson) Date: Fri, 11 Jul 2025 10:28:10 +0000 (-0400) Subject: gesql.c include sql_mngusr function X-Git-Tag: tag-0.14~139 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=40795421260fce53f288212c2f5cc92701c1b6de;p=jmp%2Fmailleur gesql.c include sql_mngusr function --- diff --git a/lib/devsql.c b/lib/devsql.c index ba8b497..1b54805 100644 --- a/lib/devsql.c +++ b/lib/devsql.c @@ -242,114 +242,6 @@ return sql; */ /********************************************************/ /* */ -/* Procedure to select a user from emails tables */ -/* and return contents; */ -/* */ -/********************************************************/ -static USRTYP *select_user(SQLTYP *sql,char *email) - -{ -#define OPEP "devsql.c:select_user," -#define SELUSR "SELECT * FROM "EMAILS" WHERE email=%s" - -//field available in table "emails" -static const FLDTYP usrfield[]={ - {1,"email"}, - {2,"password"}, - {3,"hash"}, - {4,"space"}, - {5,"mxspace"}, - {6,"locked"}, - {0,(char *)0} - }; - -USRTYP *usr; -SQLRES *rs; -char cmd[300]; -register int phase; -register _Bool proceed; - -usr=(USRTYP *)0; -rs=(SQLRES *)0; -(void) snprintf(cmd,sizeof(cmd),SELUSR,email); -phase=0; -proceed=true; -while (proceed==true) { - switch (phase) { - case 0 : //Selecting user - if ((rs=gettupple(sql,cmd))==(SQLRES *)0) { - (void) rou_alert(0,"%s Unable to get data for user <%s> (Database?)", - OPEP,email); - phase=999; //user not found within database - } - break; - case 1 : //do we have ONE user - int nbr; - - nbr=nbrtupple(sql,rs); - switch (nbr) { - case 0 : //No user email found - phase=999; //this could append - break; - case 1 : //we have only one record, found user - break; - default : //such case should never ever happen - (void) rou_alert(0,"<%s> return '%d' record %s (Database corrupted?)", - cmd,nbr,"while only 1 is expected"); - phase=999; - break; - } - break; - case 2 : //user data extraction - usr=(USRTYP *)calloc(1,sizeof(USRTYP)); - for (int i=0;usrfield[i].num!=0;i++) { - char *locval; - - if ((locval=getvalue(sql,rs,0,usrfield[i].name))==(char *)0) - continue; - switch (usrfield[i].num) { - case 1 : //user email - usr->email=strdup(locval); - break; - case 2 : //User crypted password - usr->password=strdup(locval); - break; - case 3 : //'email:realm:password' MD5 - usr->hash=strdup(locval); - break; - case 4 : //user used space - usr->space=atoi(locval); - break; - case 5 : //user max space available - usr->mxspace=atoi(locval); - break; - case 6 : //lock status - usr->lock=atoi(locval); - break; - default : - (void) rou_alert(0,"%s field <%d:%s> not implemented (Bug?)", - OPEP,usrfield[i].num,usrfield[i].name); - break; - } - } - break; - default : //SAFE Guard - rs=dropresult(sql,rs); - proceed=false; - break; - } - phase++; - } -return usr; - -#undef SELUSR -#undef OPEP -} -/* - -*/ -/********************************************************/ -/* */ /* Procedure to establish a link with the */ /* designated SQL server. */ /* */ @@ -594,65 +486,6 @@ if ((rou_vasprintf(&cmd,fmt,args))>0) { va_end(args); return number; -#undef OPEP -} -/* - -*/ -/********************************************************/ -/* */ -/* Procedure to retreive information about user */ -/* known within database. */ -/* */ -/********************************************************/ -PUBLIC _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *email,USRTYP **usr) - -{ -#define OPEP "devsql.c:sql_mngusr," - -_Bool isok; -char *gooddata; -SQLTYP *sql; -int phase; -_Bool proceed; - -isok=false; -gooddata=(char *)0; -sql=(SQLTYP *)sqlptr; -phase=0; -proceed=true; -while (proceed==true) { - switch (phase) { - case 0 : //checking SQL - if (sql==(SQLTYP *)0) { - (void) rou_alert(0,"%s SQL pointer is NUll (Bug?)",OPEP); - phase=999; - } - if ((gooddata=sql_gooddata(sql,email))==(char *)0) { - (void) rou_alert(0,"%s %s table, key <%s> is empty (Bug?!)", - OPEP,EMAILS,email); - phase=999; - } - break; - case 1 : //getting user information - switch (action) { - case sql_select : - if ((*usr=select_user(sql,gooddata))!=(USRTYP *)0) - isok=true; - break; - default : - (void) rou_alert(0,"%s action='%d' not yet implemented!",OPEP,action); - } - gooddata=rou_freestr(gooddata); - break; - default : - proceed=false; - break; - } - phase++; - } -return isok; - #undef OPEP } /* diff --git a/lib/devsql.h b/lib/devsql.h index e62bde0..afa5e32 100644 --- a/lib/devsql.h +++ b/lib/devsql.h @@ -10,9 +10,12 @@ #include "unisql.h" -//reference to a SQL pointer reference +//An SQL database pointer reference typedef void SQLPTR; +//An SQL database result reference +typedef void SQLRES; + //to connect a remote SQL server extern SQLPTR *sql_opensql(); @@ -23,15 +26,19 @@ extern SQLPTR *sql_closesql(SQLPTR *sqlptr); //as key search by database extern char *sql_gooddata(SQLPTR *sqlptr,char *key); -//procedure to manage information on exiting user -extern _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *email,USRTYP **usr); +//procedure to transmit a simple data-base action +extern int sql_request(SQLPTR *sqlptr,const char *fmt,...); + +//to extract data from database +extern char *sql_getvalue(SQLPTR *sqlptr,SQLRES *rs,int row,char *name); -//procedure to manage information on email exchange session -extern _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,SESTYP **ses); +//to extract data from database +extern SQLRES *sql_gettupple(SQLPTR *sqlptr,const char *fmt,...); -//procedure to manage action stats on email exhange status -extern _Bool sql_mngact(SQLPTR *sqlptr,SQLENUM action,ACTTYP *act); +//to retreive the number of tupple available within result +extern int sql_getnbrtupple(SQLPTR *sqlptr,SQLRES *); -extern int sql_request(SQLPTR *sqlptr,const char *fmt,...); +//to free memory used by a database tupple +extern SQLRES *sql_droptupple(SQLPTR *sqlptr,SQLRES *res); #endif diff --git a/lib/gessql.c b/lib/gessql.c index 03f22c0..0df04f5 100644 --- a/lib/gessql.c +++ b/lib/gessql.c @@ -4,6 +4,8 @@ /* Module to manage complexe SQL request */ /* */ /********************************************************/ +#include +#include #include #include "subrou.h" @@ -11,10 +13,129 @@ #include "gessql.h" //DATABASE Table names +#define EMLTBL "emails" //Table about user list #define RMTTBL "remotes" //Table about remotes site #define ACTTBL "actions" //action tables #define SESTBL "sessions" //session tables +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to select a user from emails tables */ +/* and return contents; */ +/* */ +/********************************************************/ +static _Bool select_user(SQLPTR *sqlptr,char *email,USRTYP **usr) + +{ +#define OPEP "gessql.c:select_user," + +static const char *sel="SELECT * FROM "EMLTBL" WHERE email=%s"; + +//field available in table "emails" +static const FLDTYP usrfield[]={ + {1,"email"}, + {2,"password"}, + {3,"hash"}, + {4,"space"}, + {5,"mxspace"}, + {6,"locked"}, + {0,(char *)0} + }; + +_Bool isok; +USRTYP *locusr; +SQLRES *rs; +register int phase; +register _Bool proceed; + +isok=false; +locusr=(USRTYP *)0; +rs=(SQLRES *)0; +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //checking parameters + if ((usr==(USRTYP **)0)||(email=(char *)0)||(strlen(email)==0)) { + (void) rou_alert(0,"%s usr=<%p> or email=<%s> sing (Bug?)",OPEP,usr,email); + phase=999; + } + break; + case 1 : //Selecting user + if ((rs=sql_gettupple(sqlptr,sel,email))==(SQLRES *)0) { + (void) rou_alert(0,"%s Unable to get data for user <%s> (Database?)", + OPEP,email); + phase=999; //user not found within database + } + break; + case 2 : //do we have ONE user + int nbr; + + nbr=sql_getnbrtupple(sqlptr,rs); + switch (nbr) { + case 0 : //No user email found + phase=999; //this could append + break; + case 1 : //we have only one record, found user + break; + default : //such case should never ever happen + (void) rou_alert(0,"%s got '%d' users record %s (Database corrupted?)", + OPEP,nbr,"while only 1 is expected"); + phase=999; + break; + } + break; + case 3 : //user data extraction + isok=true; + locusr=(USRTYP *)calloc(1,sizeof(USRTYP)); + for (int i=0;i<(sizeof(usrfield)/sizeof(FLDTYP))&&(isok==true);i++) { + char *locval; + + if ((locval=sql_getvalue(sqlptr,rs,0,usrfield[i].name))==(char *)0) + continue; + switch (usrfield[i].num) { + case 1 : //user email + locusr->email=strdup(locval); + break; + case 2 : //User crypted password + locusr->password=strdup(locval); + break; + case 3 : //'email:realm:password' MD5 + locusr->hash=strdup(locval); + break; + case 4 : //user used space + locusr->space=atoi(locval); + break; + case 5 : //user max space available + locusr->mxspace=atoi(locval); + break; + case 6 : //lock status + locusr->lock=atoi(locval); + break; + default : + (void) rou_alert(0,"%s field <%d:%s> not implemented (Bug?)", + OPEP,usrfield[i].num,usrfield[i].name); + locusr=sql_freeusr(locusr); + isok=false; + break; + } + } + break; + default : //SAFE Guard + rs=sql_droptupple(sqlptr,rs); + proceed=false; + break; + } + phase++; + } +*usr=locusr; +return isok; + +#undef OPEP +} /* */ @@ -94,6 +215,68 @@ if (isok==true) cmtset=rou_freestr(cmtset); return isok; +#undef OPEP +} +/* + +*/ +/********************************************************/ +/* */ +/* Procedure to retreive information about user */ +/* known within database. */ +/* */ +/********************************************************/ +PUBLIC _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *key,USRTYP **usr) + +{ +#define OPEP "gessql.c:sql_mngusr," + +_Bool isok; +char *gooddata; +int phase; +_Bool proceed; + +isok=false; +gooddata=(char *)0; +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //checking SQL and user record + if (sqlptr==(SQLPTR *)0) { + (void) rou_alert(0,"%s SQL pointer is NULL (Bug?)",OPEP); + phase=999; + } + if (usr==(USRTYP **)0) { + (void) rou_alert(0,"%s USER pointer is NULL (Bug?)",OPEP); + phase=999; + } + break; + case 1 : //do we have a good key + if ((gooddata=sql_gooddata(sqlptr,key))==(char *)0) { + (void) rou_alert(0,"%s %s table, key <%s> is empty (Bug?!)", + OPEP,EMLTBL,key); + phase=999; + } + break; + case 2 : //getting user information + switch (action) { + case sql_select : + isok=select_user(sqlptr,gooddata,usr); + break; + default : + (void) rou_alert(0,"%s action='%d' not yet implemented!",OPEP,action); + } + gooddata=rou_freestr(gooddata); + break; + default : + proceed=false; + break; + } + phase++; + } +return isok; + #undef OPEP } /* @@ -108,7 +291,7 @@ return isok; PUBLIC _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,SESTYP **ses) { -#define OPEP "devsql.c:sql_mngses," +#define OPEP "gessql.c:sql_mngses," static const char *ins="INSERT INTO "SESTBL" (sessid) VALUES(%s)"; @@ -170,7 +353,7 @@ return isok; PUBLIC _Bool sql_mngact(SQLPTR *sqlptr,SQLENUM action,ACTTYP *act) { -#define OPEP "devsql.c:sql_mngact," +#define OPEP "gessql.c:sql_mngact," static const char *del="DELETE FROM "ACTTBL" WHERE sessid=%s AND rcptto=%s"; static const char *ins="INSERT INTO "ACTTBL" (%s) VALUES(%s,%s,%s,%d,%s)"; diff --git a/lib/gessql.h b/lib/gessql.h index 4da3505..64b44ee 100644 --- a/lib/gessql.h +++ b/lib/gessql.h @@ -11,9 +11,15 @@ #include "devsql.h" +//procedure to manage information on exiting user +extern _Bool sql_mngusr(SQLPTR *sqlptr,SQLENUM action,char *key,USRTYP **usr); + //procedure to manage action stats on email exhange status extern _Bool sql_mngact(SQLPTR *sqlptr,SQLENUM action,ACTTYP *act); +//procedure to manage information on email exchange session +extern _Bool sql_mngses(SQLPTR *sqlptr,SQLENUM action,SESTYP **ses); + //Procedure to increment (or decrement) the remoteip //connection number extern _Bool sql_newconnect(SQLPTR *sqlptr,char *rmtip,int delta); diff --git a/lib/unisql.h b/lib/unisql.h index e37fd7d..ae99323 100644 --- a/lib/unisql.h +++ b/lib/unisql.h @@ -17,21 +17,10 @@ typedef enum { sql_uknown //No action }SQLENUM; -typedef enum { - row_integer, //row data is an integer - row_char, //row data is a char - row_unknown //row data unsoecifier - }FLDENUM; - //defining a database column definition retrieval typedef struct { int num; //field number char *name; //field name; - FLDENUM specie; //field type - union { - u_long entier;//data is a number - char *string; //data is a string - }data; }FLDTYP; //structure about user within the database