gesspf.o gestcp.o geseml.o \
devlog.o devsoc.o devsql.o \
unidns.o unieml.o unipar.o \
- uniprc.o unisig.o unitls.o \
+ uniprc.o unisig.o unisql.o unitls.o \
subafn.o subrou.o
LIBS= \
devsoc.h: \
unitls.h
+devsql.h: \
+ unisql.h
+
uniprc.h: \
subrou.h
/* to handle SQL request */
/* */
/********************************************************/
+#include <ctype.h>
#include <stdlib.h>
#include <string.h>
}
break;
case 3 : //checking if the database is properly open
- if (sql->db.psql==(POSPTR *)0) {
+ _Bool isok;
+
+ isok=false;
+ switch (sql->sqldb) {
+ case db_postgres:
+ isok=(sql->db.psql!=(POSPTR *)0);
+ break;
+ case db_maria :
+ isok=(sql->db.msql!=(MARPTR *)0);
+ break;
+ default :
+ break;
+ }
+ if (isok==false) {
(void) rou_alert(0,"%s data-base not properly connected (config?)",OPEP);
(void) free(sql);
sql=(SQLTYP *)0;
sql->db.psql=pos_closesql(sql->db.psql);
break;
case db_maria :
- sql->db.msql=mar_closesql(sql->db.psql);
+ sql->db.msql=mar_closesql(sql->db.msql);
break;
default :
(void) rou_alert(0,"%s Unexpected type='%d' (bug?)",
return (SQLPTR *)sql;
#undef OPEP
}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to make sure a string to be use to */
+/* access database is clean. */
+/* (no character to be misunderstood by database) */
+/* */
+/********************************************************/
+PUBLIC char *sql_cleanstr(SQLPTR *sqlptr,char *str)
+
+{
+#define OPEP "devsql.c:sql_cleanstr,"
+char *cleanstr;
+SQLTYP *sql;
+
+cleanstr=(char *)0;
+sql=(SQLTYP *)sqlptr;
+if (str!=(char *)0) {
+ (void) sql_checkencoding(str);
+ switch(sql->sqldb) {
+ case (db_postgres) :
+ cleanstr=pos_cleanquote(str);
+ break;
+ case (db_maria) :
+ cleanstr=mar_cleanquote(str);
+ break;
+ case (db_unknown) :
+ default :
+ (void) rou_alert(0,"%s Unknown SQL daemon (type='%d')",OPEP,sql->sqldb);
+ break;
+ }
+ }
+else
+ cleanstr=strdup("NULL");
+return cleanstr;
+
+#undef OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to retreive information about user */
+/* known within database. */
+/* */
+/********************************************************/
+PUBLIC USRTYP *sql_getusr(SQLPTR *sqlptr,char *email)
+
+{
+#define OPEP "devsql.c:sql_getuser,"
+
+USRTYP *usr;
+SQLTYP *sql;
+int phase;
+_Bool proceed;
+
+usr=(USRTYP *)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;
+ }
+ break;
+ case 1 : //is email defined?
+ if ((email==(char *)0)||(strlen(email)==0)) {
+ (void) rou_alert(0,"%s Email <%s> is NUll or empty (Bug?)",OPEP,email);
+ phase=999;
+ }
+ break;
+ case 2 : //check email structure
+ for (int i=0,l=strlen(email);i<l;i++) {
+ char car;
+
+ car=email[i];
+ if (isalnum((int)car)!=0)
+ continue;
+ if (car=='@')
+ continue;
+ (void) rou_alert(0,"%s Email <%s> not accepted, '%c' not an email char",
+ OPEP,email,car);
+ phase=999;
+ }
+ break;
+ case 3 : //opening specific database
+ switch (sql->sqldb) {
+ case db_postgres :
+ break;
+ case db_maria :
+ break;
+ default :
+ (void) rou_alert(0,"%s Unexpected type='%d' (bug?)",OPEP,sql->sqldb);
+ break;
+ }
+ break;
+ default :
+ proceed=false;
+ break;
+ }
+ phase++;
+ }
+return usr;
+#undef OPEP
+}
#ifndef DEVSQL
#define DEVSQL
+#include "unisql.h"
+
//reference to a SQL pointer reference
typedef void SQLPTR;
//procedure to close an previously opened SQL channel
extern SQLPTR *sql_closesql(SQLPTR *sqlptr);
+//procedure to make sure a string with database
+//is clean
+extern char *sql_cleanstr(SQLPTR *sqlptr,char *str);
+
+//procedure to get information on exiting user
+extern USRTYP *sql_getusr(SQLPTR *sqlptr,char *email);
+
#endif
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <unistd.h>
#include <mysql/mysql.h>
#include <sys/types.h>
#undef OPEP
}
+/*
+\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;
+}
#ifndef UNIMAR
#define UNIMAR
-
//reference to a SQL pointer reference
typedef void MARPTR;
//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);
+
#endif
/********************************************************/
#include <libpq-fe.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
#include "subrou.h"
#include "unipos.h"
#undef OPEP
}
+/*
+\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 "''" */
+/* */
+/********************************************************/
+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;
+}
//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);
+
#endif
--- /dev/null
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/********************************************************/
+/* */
+/* Module for low level SQL subroutine */
+/* */
+/********************************************************/
+#include <ctype.h>
+#include <errno.h>
+#include <iconv.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "subrou.h"
+#include "unisql.h"
+
+#define DLANG "UNICODE"
+
+/*
+\f
+*/
+/************************************************/
+/* */
+/* Procedure to convert an char sequence */
+/* from on char set to another. */
+/* if successful return a new pointer */
+/* */
+/************************************************/
+static char *cnvconvert(char *lfrom,char *lto,char *encoded)
+
+{
+#define OPEP "rousql.c:cnvconvert,"
+
+char *converted;
+char *marker;
+iconv_t hconv;
+size_t inbuflft;
+char *inbuf;
+size_t outbuflft;
+char *outbuf;
+int phase;
+int proceed;
+
+converted=(char *)0;
+marker=(char *)0;
+hconv=(iconv_t)0;
+inbuflft=(size_t)0;
+inbuf=(char *)0;
+outbuflft=(size_t)0;
+outbuf=(char *)0;
+phase=0;
+proceed=1;
+while (proceed==true) {
+ switch (phase) {
+ case 0 : /*opening conv channel */
+ if ((hconv=iconv_open(lto,lfrom))==(iconv_t)-1) {
+ (void) rou_alert(0,"%s Unexpected lang conversion request "
+ "from=<%s> to=<%s> (errno=<%s>)",
+ OPEP,lfrom,lto,strerror(errno));
+ phase=999; /*trouble trouble */
+ }
+ break;
+ case 1 : /*doing convertion */
+ inbuflft=strlen(encoded);
+ outbuflft=(inbuflft*4)+1; /*spare room */
+ inbuf=encoded;
+ marker=(char *)calloc(outbuflft,sizeof(char));
+ outbuf=marker;
+ if (iconv(hconv,&inbuf,&inbuflft,&outbuf,&outbuflft)==(size_t)(-1)) {
+ (void) rou_alert(2,"%s Unable to convert <%s> "
+ "from=<%s> to=<%s> (errno=<%s>)",
+ OPEP,encoded,lfrom,lto,strerror(errno));
+ (void) free(marker);
+ marker=(char *)0;
+ }
+ break;
+ case 2 : /*doing convertion */
+ if (iconv_close(hconv)<0)
+ (void) rou_alert(0,"%s Unexpected close conversion error "
+ "(errno=<%s>) (bug?)",OPEP,strerror(errno));
+ break;
+ default : /*SAFE Guard */
+ converted=marker;
+ proceed=false;
+ break;
+ }
+ phase++;
+ }
+return converted;
+
+#undef OPEP
+}
+/*
+\f
+*/
+/********************************************************/
+/* */
+/* Procedure to free memory used by a user */
+/* definition. */
+/* */
+/********************************************************/
+PUBLIC USRTYP *sql_freeusr(USRTYP *usr)
+
+{
+if (usr!=(USRTYP *)0) {
+ usr->email=rou_freestr(usr->email);
+ (void) free(usr);
+ usr=(USRTYP *)0;
+ }
+return usr;
+}
+/*
+\f
+*/
+/************************************************/
+/* */
+/* Routine to check if string is properly */
+/* coded, if it is not the case replace all*/
+/* dubious charactere with plain ASCII */
+/* */
+/************************************************/
+PUBLIC void sql_checkencoding(char *strencoded)
+
+{
+char *ptr;
+int phase;
+int proceed;
+
+ptr=(char *)0;
+phase=0;
+proceed=true;
+while (proceed==true) {
+ switch (phase) {
+ case 0 : /*autoconvert string */
+ if (((ptr=getenv("DB_LANG"))==(char *)0)||(strlen(ptr)==0))
+ ptr=DLANG;
+ if ((ptr=cnvconvert(ptr,ptr,strencoded))!=(char *)0) {
+ (void) free(ptr);
+ phase=999; /*everything fine */
+ }
+ break;
+ case 1 : /*setting to plain ASCII*/
+ if (strencoded!=(char *)0) { /*alway */
+ while (*strencoded!='\000') {
+ if (isascii(*strencoded)==0)
+ *strencoded='_';
+ strencoded++;
+ }
+ }
+ break;
+ default : /*SAFE Guard */
+ proceed=false;
+ break;
+ }
+ phase++;
+ }
+}
--- /dev/null
+// vim: smarttab tabstop=8 shiftwidth=2 expandtab
+/************************************************/
+/* */
+/* Low level SQL subroutine declaration */
+/* */
+/************************************************/
+#ifndef UNISQL
+#define UNISQL
+
+#include <sys/types.h>
+
+//structure about user within the database
+typedef struct {
+ char *email; //user emails
+ u_long space; //user space used;
+ u_long mxspace; //user maximun space
+ }USRTYP;
+
+//procedure to free space used by an USRTYP
+extern USRTYP *sql_freeusr(USRTYP *usr);
+
+//PRocedure to check if string is properly coded according DB_LANG
+extern void sql_checkencoding(char *strencoded);
+
+#endif