replaced the example manager database by a sql script
[strongswan.git] / src / manager / storage.c
1 /*
2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
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>.
9 *
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
13 * for more details.
14 *
15 * $Id$
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 * private data of storage
28 */
29 struct private_storage_t {
30
31 /**
32 * public functions
33 */
34 storage_t public;
35
36 /**
37 * database connection
38 */
39 database_t *db;
40 };
41
42 /**
43 * Implementation of storage_t.login.
44 */
45 static int login(private_storage_t *this, char *username, char *password)
46 {
47 hasher_t *hasher;
48 chunk_t hash, data;
49 size_t username_len, password_len;
50 int uid = 0;
51 char *str;
52 enumerator_t *enumerator;
53
54 /* hash = SHA1( username | password ) */
55 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
56 if (hasher == NULL)
57 {
58 return 0;
59 }
60 hash = chunk_alloca(hasher->get_hash_size(hasher));
61 username_len = strlen(username);
62 password_len = strlen(password);
63 data = chunk_alloca(username_len + password_len);
64 memcpy(data.ptr, username, username_len);
65 memcpy(data.ptr + username_len, password, password_len);
66 hasher->get_hash(hasher, data, hash.ptr);
67 hasher->destroy(hasher);
68 str = chunk_to_hex(hash, FALSE);
69
70 enumerator = this->db->query(this->db,
71 "SELECT oid FROM users WHERE username = ? AND password = ?;",
72 DB_TEXT, username, DB_TEXT, str,
73 DB_INT);
74 if (enumerator)
75 {
76 enumerator->enumerate(enumerator, &uid);
77 enumerator->destroy(enumerator);
78 }
79 free(str);
80 return uid;
81 }
82
83 /**
84 * Implementation of storage_t.create_gateway_enumerator.
85 */
86 static enumerator_t* create_gateway_enumerator(private_storage_t *this, int user)
87 {
88 enumerator_t *enumerator;
89
90 enumerator = this->db->query(this->db,
91 "SELECT gateways.oid AS gid, name, port, address FROM "
92 "gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
93 DB_INT, user,
94 DB_INT, DB_TEXT, DB_INT, DB_TEXT);
95 if (!enumerator)
96 {
97 enumerator = enumerator_create_empty();
98 }
99 return enumerator;
100 }
101
102 /**
103 * Implementation of storage_t.destroy
104 */
105 static void destroy(private_storage_t *this)
106 {
107 this->db->destroy(this->db);
108 free(this);
109 }
110
111 /*
112 * see header file
113 */
114 storage_t *storage_create(char *uri)
115 {
116 private_storage_t *this = malloc_thing(private_storage_t);
117
118 this->public.login = (int(*)(storage_t*, char *username, char *password))login;
119 this->public.create_gateway_enumerator = (enumerator_t*(*)(storage_t*,int))create_gateway_enumerator;
120 this->public.destroy = (void(*)(storage_t*))destroy;
121
122 this->db = lib->db->create(lib->db, uri);
123 if (this->db == NULL)
124 {
125 free(this);
126 return NULL;
127 }
128 return &this->public;
129 }
130