[PATCH] MySQL Escape String (another one)

Donald Jackson <[email protected]>
Newsgroups gmane.comp.mobile.kannel.devel
Message-ID <[email protected]>
Hi everyone,
Just posting this patch in case anyone wants to use it. It escapes the
source address string to make it MySQL safe on the connection.

In hindsight possibly not perfect implementation but it does the job.

Thanks,
-- 
Donald Jackson
http://www.ddj.co.za/
donaldjster(a)gmail.com
dlr_escape.patch (application/octet-stream, 5.1 KB)
Index: gw/dlr.c
===================================================================
RCS file: /home/cvs/gateway/gw/dlr.c,v
retrieving revision 1.52
diff -u -w -r1.52 dlr.c
--- gw/dlr.c	11 Feb 2005 15:35:48 -0000	1.52
+++ gw/dlr.c	7 Aug 2009 09:44:44 -0000
@@ -308,6 +308,15 @@
     return "unknown";
 }
  
+Octstr *dlr_escape(Octstr *data) {
+
+	if(handles->dlr_escape == NULL) {
+		return octstr_duplicate(data);
+	}
+	
+	return handles->dlr_escape(data);
+}
+ 
 /*
  * Add new dlr entry into dlr storage
  */
@@ -335,7 +344,7 @@
     /* now copy all values, we are interested in */
     dlr->smsc = (smsc ? octstr_duplicate(smsc) : octstr_create(""));
     dlr->timestamp = (ts ? octstr_duplicate(ts) : octstr_create(""));
-    dlr->source = (msg->sms.sender ? octstr_duplicate(msg->sms.sender) : octstr_create(""));
+    dlr->source = (msg->sms.sender ? dlr_escape(msg->sms.sender) : octstr_create(""));  /* We can use the escape directly as it is duplicating the data */
     dlr->destination = (msg->sms.receiver ? octstr_duplicate(msg->sms.receiver) : octstr_create(""));
     dlr->service = (msg->sms.service ? octstr_duplicate(msg->sms.service) : octstr_create(""));
     dlr->url = (msg->sms.dlr_url ? octstr_duplicate(msg->sms.dlr_url) : octstr_create(""));
Index: gw/dlr_mem.c
===================================================================
RCS file: /home/cvs/gateway/gw/dlr_mem.c,v
retrieving revision 1.9
diff -u -w -r1.9 dlr_mem.c
--- gw/dlr_mem.c	11 Feb 2005 15:35:48 -0000	1.9
+++ gw/dlr_mem.c	7 Aug 2009 09:44:44 -0000
@@ -186,7 +186,8 @@
     .dlr_remove = dlr_mem_remove,
     .dlr_shutdown = dlr_mem_shutdown,
     .dlr_messages = dlr_mem_messages,
-    .dlr_flush = dlr_mem_flush
+    .dlr_flush = dlr_mem_flush,
+    .dlr_escape = NULL
 };
 
 /*
Index: gw/dlr_mysql.c
===================================================================
RCS file: /home/cvs/gateway/gw/dlr_mysql.c,v
retrieving revision 1.12
diff -u -w -r1.12 dlr_mysql.c
--- gw/dlr_mysql.c	10 Jan 2006 12:53:29 -0000	1.12
+++ gw/dlr_mysql.c	7 Aug 2009 09:44:44 -0000
@@ -134,6 +134,27 @@
     return result;
 }
 
+static Octstr *dlr_mysql_escape_string(const Octstr *instr) {
+        DBPoolConn *pc;
+        char *outstr;
+        Octstr *result;
+        
+        pc = dbpool_conn_consume(pool);
+        if (pc == NULL) {
+            error(0, "MYSQL: Database pool got no connection! DB escape failed!");
+            return NULL;
+        }
+
+        outstr = gw_malloc((octstr_len(instr) * 2) + 1);
+        mysql_real_escape_string(pc->conn, outstr, octstr_get_cstr(instr), octstr_len(instr));
+        result = octstr_create(outstr);
+        gw_free(outstr);
+        
+        dbpool_conn_produce(pc);
+        
+        return result;
+}
+
 static void dlr_mysql_shutdown()
 {
     dbpool_destroy(pool);
@@ -296,7 +317,8 @@
     .dlr_remove = dlr_mysql_remove,
     .dlr_shutdown = dlr_mysql_shutdown,
     .dlr_messages = dlr_mysql_messages,
-    .dlr_flush = dlr_mysql_flush
+    .dlr_flush = dlr_mysql_flush,
+    .dlr_escape = dlr_mysql_escape_string
 };
 
 struct dlr_storage *dlr_init_mysql(Cfg *cfg)
Index: gw/dlr_oracle.c
===================================================================
RCS file: /home/cvs/gateway/gw/dlr_oracle.c,v
retrieving revision 1.9
diff -u -w -r1.9 dlr_oracle.c
--- gw/dlr_oracle.c	21 Sep 2005 02:01:22 -0000	1.9
+++ gw/dlr_oracle.c	7 Aug 2009 09:44:45 -0000
@@ -321,7 +321,8 @@
     .dlr_get = dlr_get_oracle,
     .dlr_remove = dlr_remove_oracle,
     .dlr_update = dlr_update_oracle,
-    .dlr_flush = dlr_flush_oracle
+    .dlr_flush = dlr_flush_oracle,
+    .dlr_escape = NULL
 };
 
 struct dlr_storage *dlr_init_oracle(Cfg *cfg)
Index: gw/dlr_p.h
===================================================================
RCS file: /home/cvs/gateway/gw/dlr_p.h,v
retrieving revision 1.7
diff -u -w -r1.7 dlr_p.h
--- gw/dlr_p.h	11 Feb 2005 15:35:48 -0000	1.7
+++ gw/dlr_p.h	7 Aug 2009 09:44:45 -0000
@@ -137,6 +137,10 @@
      * Shutdown storage
      */
     void (*dlr_shutdown) (void);
+    /* 
+    * Escape strings
+    */
+    Octstr* (*dlr_escape) (const Octstr *instr);
 };
 
 /*
Index: gw/dlr_pgsql.c
===================================================================
RCS file: /home/cvs/gateway/gw/dlr_pgsql.c,v
retrieving revision 1.9
diff -u -w -r1.9 dlr_pgsql.c
--- gw/dlr_pgsql.c	10 Jan 2006 12:53:29 -0000	1.9
+++ gw/dlr_pgsql.c	7 Aug 2009 09:44:45 -0000
@@ -291,7 +291,8 @@
     .dlr_remove = dlr_pgsql_remove,
     .dlr_shutdown = dlr_pgsql_shutdown,
     .dlr_messages = dlr_pgsql_messages,
-    .dlr_flush = dlr_pgsql_flush
+    .dlr_flush = dlr_pgsql_flush,
+    .dlr_escape = NULL
 };
 
 
Index: gw/dlr_sdb.c
===================================================================
RCS file: /home/cvs/gateway/gw/dlr_sdb.c,v
retrieving revision 1.12
diff -u -w -r1.12 dlr_sdb.c
--- gw/dlr_sdb.c	21 Sep 2005 02:01:22 -0000	1.12
+++ gw/dlr_sdb.c	7 Aug 2009 09:44:45 -0000
@@ -357,7 +357,8 @@
     .dlr_remove = dlr_sdb_remove,
     .dlr_shutdown = dlr_sdb_shutdown,
     .dlr_messages = dlr_sdb_messages,
-    .dlr_flush = dlr_sdb_flush
+    .dlr_flush = dlr_sdb_flush,
+    .dlr_escape = NULL
 };
 
 struct dlr_storage *dlr_init_sdb(Cfg* cfg)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.