Properly handle zero length in chunk_alloc[a]/chunk_clone[a]
[strongswan.git] / src / manager / storage.c
index 87e0374..f7635ea 100644 (file)
@@ -11,8 +11,6 @@
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  * for more details.
- *
- * $Id$
  */
 
 #include "storage.h"
@@ -32,7 +30,7 @@ struct private_storage_t {
         * public functions
         */
        storage_t public;
-       
+
        /**
         * database connection
         */
@@ -45,12 +43,11 @@ struct private_storage_t {
 static int login(private_storage_t *this, char *username, char *password)
 {
        hasher_t *hasher;
-       chunk_t hash, data;
+       chunk_t hash, data, hex_str;
        size_t username_len, password_len;
        int uid = 0;
-       char *str;
        enumerator_t *enumerator;
-       
+
        /* hash = SHA1( username | password ) */
        hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
        if (hasher == NULL)
@@ -65,18 +62,18 @@ static int login(private_storage_t *this, char *username, char *password)
        memcpy(data.ptr + username_len, password, password_len);
        hasher->get_hash(hasher, data, hash.ptr);
        hasher->destroy(hasher);
-       str = chunk_to_hex(hash, FALSE);
-       
-       enumerator = this->db->query(this->db, 
+       hex_str = chunk_to_hex(hash, NULL, FALSE);
+
+       enumerator = this->db->query(this->db,
                        "SELECT oid FROM users WHERE username = ? AND password = ?;",
-                       DB_TEXT, username, DB_TEXT, str,
+                       DB_TEXT, username, DB_TEXT, hex_str.ptr,
                        DB_INT);
        if (enumerator)
        {
                enumerator->enumerate(enumerator, &uid);
                enumerator->destroy(enumerator);
        }
-       free(str);
+       free(hex_str.ptr);
        return uid;
 }
 
@@ -86,8 +83,8 @@ static int login(private_storage_t *this, char *username, char *password)
 static enumerator_t* create_gateway_enumerator(private_storage_t *this, int user)
 {
        enumerator_t *enumerator;
-       
-       enumerator = this->db->query(this->db, 
+
+       enumerator = this->db->query(this->db,
                        "SELECT gateways.oid AS gid, name, port, address FROM "
                        "gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
                        DB_INT, user,
@@ -114,11 +111,11 @@ static void destroy(private_storage_t *this)
 storage_t *storage_create(char *uri)
 {
        private_storage_t *this = malloc_thing(private_storage_t);
-       
+
        this->public.login = (int(*)(storage_t*, char *username, char *password))login;
        this->public.create_gateway_enumerator = (enumerator_t*(*)(storage_t*,int))create_gateway_enumerator;
        this->public.destroy = (void(*)(storage_t*))destroy;
-       
+
        this->db = lib->db->create(lib->db, uri);
        if (this->db == NULL)
        {