[Opendnssec-commits] [svn.opendnssec.org/svn/dnssec] r5396 - in trunk/OpenDNSSEC/enforcer: ksm/include/ksm utils

Sion Lloyd sion at nominet.org.uk
Fri Aug 12 10:07:59 CEST 2011


Author: sion
Date: 2011-08-12 10:07:58 +0200 (Fri, 12 Aug 2011)
New Revision: 5396

Modified:
   trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksmutil.h
   trunk/OpenDNSSEC/enforcer/utils/ksmutil.c
Log:
Quote single quotes in mysql username and password, trac ticket #259.


Modified: trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksmutil.h
===================================================================
--- trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksmutil.h	2011-08-12 06:40:24 UTC (rev 5395)
+++ trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksmutil.h	2011-08-12 08:07:58 UTC (rev 5396)
@@ -98,6 +98,7 @@
 int keyRoll(int zone_id, int policy_id, int key_type);
 int get_policy_name_from_id(KSM_ZONE *zone);
 int append_zone(xmlDocPtr doc, KSM_ZONE *zone);
+int ShellQuoteString(const char* string, char* buffer, size_t buflen);
 
 #ifdef __cplusplus
 }

Modified: trunk/OpenDNSSEC/enforcer/utils/ksmutil.c
===================================================================
--- trunk/OpenDNSSEC/enforcer/utils/ksmutil.c	2011-08-12 06:40:24 UTC (rev 5395)
+++ trunk/OpenDNSSEC/enforcer/utils/ksmutil.c	2011-08-12 08:07:58 UTC (rev 5396)
@@ -478,6 +478,9 @@
     char *user = NULL;
     char *password = NULL;
 
+	char quoted_user[KSM_NAME_LENGTH];
+	char quoted_password[KSM_NAME_LENGTH];
+
     char* setup_command = NULL;
     char* lock_filename = NULL;
 
@@ -562,9 +565,35 @@
     else {
         /* MySQL setup */
         /* will look like: <SQL_BIN> -u <USER> -h <HOST> -P <PORT> -p<PASSWORD> <DBSCHEMA> < <SQL_SETUP> */
+		/* Get a quoted version of the username */
+		status = ShellQuoteString(user, quoted_user, KSM_NAME_LENGTH);
+		if (status != 0) {
+			printf("Failed to connect to database, username too long.\n");
+			db_disconnect(lock_fd);
+			StrFree(host);
+			StrFree(port);
+			StrFree(dbschema);
+			StrFree(user);
+			StrFree(password);
+			return(1);
+		}
+
+		/* Get a quoted version of the password */
+		status = ShellQuoteString(password, quoted_password, KSM_NAME_LENGTH);
+		if (status != 0) {
+			printf("Failed to connect to database, password too long.\n");
+			db_disconnect(lock_fd);
+			StrFree(host);
+			StrFree(port);
+			StrFree(dbschema);
+			StrFree(user);
+			StrFree(password);
+			return(1);
+		}
+		
         StrAppend(&setup_command, SQL_BIN);
         StrAppend(&setup_command, " -u '");
-        StrAppend(&setup_command, user);
+        StrAppend(&setup_command, quoted_user);
         StrAppend(&setup_command, "'");
         if (host != NULL) {
             StrAppend(&setup_command, " -h ");
@@ -576,7 +605,7 @@
         }
         if (password != NULL) {
             StrAppend(&setup_command, " -p'");
-            StrAppend(&setup_command, password);
+            StrAppend(&setup_command, quoted_password);
 			StrAppend(&setup_command, "'");
         }
         StrAppend(&setup_command, " ");
@@ -8284,3 +8313,25 @@
 
     return(0);
 }
+
+int ShellQuoteString(const char* string, char* buffer, size_t buflen)
+{
+    size_t i;           /* Loop counter */
+    size_t j = 0;       /* Counter for new string */
+	
+	size_t len = strlen(string);
+
+    if (string) {
+        for (i = 0; i < len; ++i) {
+            if (string[i] == '\'') {
+                buffer[j++] = '\'';
+                buffer[j++] = '\\';
+                buffer[j++] = '\'';
+            }
+			buffer[j++] = string[i];
+        }
+    }
+	buffer[j] = '\0';
+    return ( (j <= buflen) ? 0 : 1);
+}
+




More information about the Opendnssec-commits mailing list