| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
#include "storage.h" |
|---|
| 19 |
|
|---|
| 20 |
#include <library.h> |
|---|
| 21 |
#include <crypto/hashers/hasher.h> |
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
typedef struct private_storage_t private_storage_t; |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
struct private_storage_t { |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
storage_t public; |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
database_t *db; |
|---|
| 40 |
}; |
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
static int login(private_storage_t *this, char *username, char *password) |
|---|
| 46 |
{ |
|---|
| 47 |
hasher_t *hasher; |
|---|
| 48 |
chunk_t hash, data, hex_str; |
|---|
| 49 |
size_t username_len, password_len; |
|---|
| 50 |
int uid = 0; |
|---|
| 51 |
enumerator_t *enumerator; |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1); |
|---|
| 55 |
if (hasher == NULL) |
|---|
| 56 |
{ |
|---|
| 57 |
return 0; |
|---|
| 58 |
} |
|---|
| 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); |
|---|
| 68 |
|
|---|
| 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, |
|---|
| 72 |
DB_INT); |
|---|
| 73 |
if (enumerator) |
|---|
| 74 |
{ |
|---|
| 75 |
enumerator->enumerate(enumerator, &uid); |
|---|
| 76 |
enumerator->destroy(enumerator); |
|---|
| 77 |
} |
|---|
| 78 |
free(hex_str.ptr); |
|---|
| 79 |
return uid; |
|---|
| 80 |
} |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
static enumerator_t* create_gateway_enumerator(private_storage_t *this, int user) |
|---|
| 86 |
{ |
|---|
| 87 |
enumerator_t *enumerator; |
|---|
| 88 |
|
|---|
| 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 = ?;", |
|---|
| 92 |
DB_INT, user, |
|---|
| 93 |
DB_INT, DB_TEXT, DB_INT, DB_TEXT); |
|---|
| 94 |
if (!enumerator) |
|---|
| 95 |
{ |
|---|
| 96 |
enumerator = enumerator_create_empty(); |
|---|
| 97 |
} |
|---|
| 98 |
return enumerator; |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
static void destroy(private_storage_t *this) |
|---|
| 105 |
{ |
|---|
| 106 |
this->db->destroy(this->db); |
|---|
| 107 |
free(this); |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
storage_t *storage_create(char *uri) |
|---|
| 114 |
{ |
|---|
| 115 |
private_storage_t *this = malloc_thing(private_storage_t); |
|---|
| 116 |
|
|---|
| 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; |
|---|
| 120 |
|
|---|
| 121 |
this->db = lib->db->create(lib->db, uri); |
|---|
| 122 |
if (this->db == NULL) |
|---|
| 123 |
{ |
|---|
| 124 |
free(this); |
|---|
| 125 |
return NULL; |
|---|
| 126 |
} |
|---|
| 127 |
return &this->public; |
|---|
| 128 |
} |
|---|
| 129 |
|
|---|