*/
/********************************************************/
/* */
-/* 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
-}
-/*
-\f
-*/
-/********************************************************/
-/* */
/* Procedure to establish a link with the */
/* designated SQL server. */
/* */
va_end(args);
return number;
-#undef OPEP
-}
-/*
-\f
-*/
-/********************************************************/
-/* */
-/* 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
}
/*
#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();
//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
/* Module to manage complexe SQL request */
/* */
/********************************************************/
+#include <malloc.h>
+#include <stdlib.h>
#include <string.h>
#include "subrou.h"
#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
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* 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
+}
/*
\f
*/
cmtset=rou_freestr(cmtset);
return isok;
+#undef OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* 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
}
/*
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)";
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)";
#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);
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