]> git.ipfire.org Git - thirdparty/strongswan.git/blame - src/manager/storage.c
pki: Added option rsa-padding=pss-max-salt
[thirdparty/strongswan.git] / src / manager / storage.c
CommitLineData
552cc11b
MW
1/*
2 * Copyright (C) 2007 Martin Willi
1b671669 3 * HSR Hochschule fuer Technik Rapperswil
552cc11b
MW
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.
552cc11b
MW
14 */
15
16#include "storage.h"
17
18#include <library.h>
19#include <crypto/hashers/hasher.h>
20
21
22typedef struct private_storage_t private_storage_t;
23
24/**
25 * private data of storage
26 */
27struct private_storage_t {
28
29 /**
30 * public functions
31 */
32 storage_t public;
7daf5226 33
552cc11b
MW
34 /**
35 * database connection
36 */
37 database_t *db;
38};
39
62d4707b
TB
40METHOD(storage_t, login, int,
41 private_storage_t *this, char *username, char *password)
552cc11b
MW
42{
43 hasher_t *hasher;
36fecdb8 44 chunk_t hash, data, hex_str;
552cc11b
MW
45 size_t username_len, password_len;
46 int uid = 0;
552cc11b 47 enumerator_t *enumerator;
7daf5226 48
552cc11b
MW
49 /* hash = SHA1( username | password ) */
50 hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
51 if (hasher == NULL)
52 {
53 return 0;
54 }
55 hash = chunk_alloca(hasher->get_hash_size(hasher));
56 username_len = strlen(username);
57 password_len = strlen(password);
58 data = chunk_alloca(username_len + password_len);
59 memcpy(data.ptr, username, username_len);
60 memcpy(data.ptr + username_len, password, password_len);
8bd6a30a
MW
61 if (!hasher->get_hash(hasher, data, hash.ptr))
62 {
63 hasher->destroy(hasher);
64 return 0;
65 }
552cc11b 66 hasher->destroy(hasher);
36fecdb8 67 hex_str = chunk_to_hex(hash, NULL, FALSE);
7daf5226
MW
68
69 enumerator = this->db->query(this->db,
552cc11b 70 "SELECT oid FROM users WHERE username = ? AND password = ?;",
36fecdb8 71 DB_TEXT, username, DB_TEXT, hex_str.ptr,
552cc11b
MW
72 DB_INT);
73 if (enumerator)
74 {
75 enumerator->enumerate(enumerator, &uid);
76 enumerator->destroy(enumerator);
77 }
36fecdb8 78 free(hex_str.ptr);
552cc11b
MW
79 return uid;
80}
81
62d4707b
TB
82METHOD(storage_t, create_gateway_enumerator, enumerator_t*,
83 private_storage_t *this, int user)
552cc11b
MW
84{
85 enumerator_t *enumerator;
7daf5226
MW
86
87 enumerator = this->db->query(this->db,
552cc11b
MW
88 "SELECT gateways.oid AS gid, name, port, address FROM "
89 "gateways, user_gateway AS ug ON gid = ug.gateway WHERE ug.user = ?;",
90 DB_INT, user,
91 DB_INT, DB_TEXT, DB_INT, DB_TEXT);
92 if (!enumerator)
93 {
94 enumerator = enumerator_create_empty();
95 }
96 return enumerator;
97}
98
62d4707b
TB
99METHOD(storage_t, destroy, void,
100 private_storage_t *this)
552cc11b
MW
101{
102 this->db->destroy(this->db);
103 free(this);
104}
105
106/*
107 * see header file
108 */
109storage_t *storage_create(char *uri)
110{
62d4707b
TB
111 private_storage_t *this;
112
113 INIT(this,
114 .public = {
115 .login = _login,
116 .create_gateway_enumerator = _create_gateway_enumerator,
117 .destroy = _destroy,
118 },
119 .db = lib->db->create(lib->db, uri),
120 );
552cc11b
MW
121 if (this->db == NULL)
122 {
123 free(this);
124 return NULL;
125 }
126 return &this->public;
127}
128