taskrambler  0.1.9
Web server and task management solution.
auth/storage/storage.c
Go to the documentation of this file.
1 /**
2  * \file
3  *
4  * \author Georg Hopp
5  *
6  * \copyright
7  * Copyright © 2012 Georg Hopp
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include "class.h"
24 #include "storage/storage.h"
25 #include "auth.h"
26 #include "uuid.h"
27 #include "user.h"
28 #include "commons.h"
29 #include "utils/memory.h"
30 
31 static
32 int
33 authStorageCtor(void * _this, va_list * params)
34 {
35  AuthStorage this = _this;
36 
37  this->store = va_arg(*params, Storage);
38 
39  return 0;
40 }
41 
42 static
43 void
44 authStorageDtor(void * _this)
45 {
46 }
47 
48 static
49 int
50 authStorageAuthenticate(void * _this, Credential cred, Uuid user_index)
51 {
52  AuthStorage this = _this;
53 
54  unsigned char current_hash[HASH_SIZE];
55  unsigned char * found_hash = NULL;
56  size_t nfound_hash = 0;
57 
58  if (CRED_PASSWORD != cred->type) {
59  return FALSE;
60  }
61 
62  storageGet(
63  this->store,
64  (char *)(user_index->uuid).value,
65  sizeof((user_index->uuid).value),
66  (char **)&found_hash,
67  &nfound_hash);
68 
69  if (NULL == found_hash || (SALT_SIZE + HASH_SIZE) != nfound_hash) {
70  /* user not found or found hash is invalid */
71  return FALSE;
72  }
73 
74  /* found_hash <=> salt+hash */
75  if (FALSE == hash_pw(
76  CRED_PWD(cred).pass,
77  CRED_PWD(cred).npass,
78  current_hash,
79  &found_hash)) {
80  MEM_FREE(found_hash);
81  return FALSE;
82  }
83 
84  if (0 != memcmp(current_hash, found_hash+SALT_SIZE, HASH_SIZE)) {
85  MEM_FREE(found_hash);
86  return FALSE;
87  }
88 
89  MEM_FREE(found_hash);
90  return TRUE;
91 }
92 
95 CREATE_CLASS(AuthStorage, NULL, IFACE(Class), IFACE(Auth));
96 
97 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
static void authStorageDtor(void *_this)
static int authStorageAuthenticate(void *_this, Credential cred, Uuid user_index)
#define SALT_SIZE
Definition: auth/storage.h:34
INIT_IFACE(Class, authStorageCtor, authStorageDtor, NULL)
#define IFACE(name)
Definition: interface.h:34
void storageGet(Storage, char *, size_t, char **, size_t *)
Definition: storage/get.c:34
#define FALSE
Definition: commons.h:28
CREATE_CLASS(AuthStorage, NULL, IFACE(Class), IFACE(Auth))
#define TRUE
Definition: commons.h:27
static int authStorageCtor(void *_this, va_list *params)
int hash_pw(const char *, const size_t, unsigned char *, unsigned char **)
Definition: hash_pw.c:74
#define HASH_SIZE
Definition: auth/storage.h:35
#define CRED_PWD(c)
Definition: credential.h:30