2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 #include <crypto/hashers/hasher.h>
24 typedef struct private_storage_t private_storage_t
;
27 * private data of storage
29 struct private_storage_t
{
43 * Implementation of storage_t.login.
45 static int login(private_storage_t
*this, char *username
, char *password
)
48 chunk_t hash
, data
, hex_str
;
49 size_t username_len
, password_len
;
51 enumerator_t
*enumerator
;
53 /* hash = SHA1( username | password ) */
54 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
59 hash
= chunk_alloca(hasher
->get_hash_size(hasher
));
60 username_len
= strlen(username
);
61 password_len
= strlen(password
);
62 data
= chunk_alloca(username_len
+ password_len
);
63 memcpy(data
.ptr
, username
, username_len
);
64 memcpy(data
.ptr
+ username_len
, password
, password_len
);
65 hasher
->get_hash(hasher
, data
, hash
.ptr
);
66 hasher
->destroy(hasher
);
67 hex_str
= chunk_to_hex(hash
, NULL
, FALSE
);
69 enumerator
= this->db
->query(this->db
,
70 "SELECT oid FROM users WHERE username = ? AND password = ?;",
71 DB_TEXT
, username
, DB_TEXT
, hex_str
.ptr
,
75 enumerator
->enumerate(enumerator
, &uid
);
76 enumerator
->destroy(enumerator
);
83 * Implementation of storage_t.create_gateway_enumerator.
85 static enumerator_t
* create_gateway_enumerator(private_storage_t
*this, int user
)
87 enumerator_t
*enumerator
;
89 enumerator
= this->db
->query(this->db
,
90 "SELECT gateways.oid AS gid, name, port, address FROM "
91 "gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
93 DB_INT
, DB_TEXT
, DB_INT
, DB_TEXT
);
96 enumerator
= enumerator_create_empty();
102 * Implementation of storage_t.destroy
104 static void destroy(private_storage_t
*this)
106 this->db
->destroy(this->db
);
113 storage_t
*storage_create(char *uri
)
115 private_storage_t
*this = malloc_thing(private_storage_t
);
117 this->public.login
= (int(*)(storage_t
*, char *username
, char *password
))login
;
118 this->public.create_gateway_enumerator
= (enumerator_t
*(*)(storage_t
*,int))create_gateway_enumerator
;
119 this->public.destroy
= (void(*)(storage_t
*))destroy
;
121 this->db
= lib
->db
->create(lib
->db
, uri
);
122 if (this->db
== NULL
)
127 return &this->public;