taskrambler  0.1.9
Web server and task management solution.
login.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 #define _GNU_SOURCE
24 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 
29 #include "class.h"
30 #include "auth.h"
31 #include "uuid.h"
32 #include "storage/storage.h"
33 
34 #include "interface/serializable.h"
35 #include "interface/indexable.h"
36 
37 #include "utils/memory.h"
39 
40 
41 int
43  Application this,
44  Credential credential,
45  Session session)
46 {
47  Uuid search;
48  AuthModule auth_module;
49 
50  User user = new(User, NULL);
51 
52  user->username = CRED_PWD(credential).user;
53  user->nusername = &CRED_PWD(credential).nuser;
54  search = indexUuid(user, this->user_namespace);
55 
56  auth_module = authenticate(this->auth, credential, search);
57 
58  if (0 != auth_module) {
59  char * user_serialized;
60  size_t nuser_serialized;
61 
62  session->user = user;
63 
64  switch (credential->type) {
65  case CRED_PASSWORD:
66  storageGet(
67  this->users,
68  (char *)(search->uuid).value,
69  sizeof((search->uuid).value),
70  &user_serialized,
71  &nuser_serialized);
72 
73  if (NULL != user_serialized) {
75  session->user,
76  (unsigned char *)user_serialized,
77  nuser_serialized);
78  MEM_FREE(user_serialized);
79  } else {
80  /**
81  * this is a user authenticated via another method
82  * than the password database and has not yet
83  * logged in.
84  * NOTE: first we have to remove the search user and
85  * as username is initialized with something that we
86  * will free later here we must set it to NULL so that
87  * the delete will not free it.
88  */
89  session->user->username = NULL;
90  delete(session->user);
91  session->user = new(User,
92  CRED_PWD(credential).user,
93  CRED_PWD(credential).nuser,
94  CSTRA(""),
95  CSTRA(""),
96  CSTRA(""));
97 
98  serialize(
99  session->user,
100  (unsigned char **)&user_serialized,
101  &nuser_serialized);
102  /**
103  * \todo
104  * Handle error...if this fails we have most likely
105  * a collision.
106  */
107  storagePut(
108  this->users,
109  (char *)(search->uuid).value,
110  sizeof((search->uuid).value),
111  user_serialized,
112  nuser_serialized);
113  MEM_FREE(user_serialized);
114  }
115 
116  session->user->auth_type = auth_module;
117  break;
118 
119  default:
120  break;
121  }
122 
123  delete(search);
124  return TRUE;
125  }
126 
127  delete(search);
128 
129  return FALSE;
130 }
131 
132 // vim: set ts=4 sw=4:
#define MEM_FREE(seg)
Definition: memory.h:28
StoragePutResult storagePut(Storage, char *, size_t, char *, size_t)
Definition: storage/put.c:34
#define CSTRA(val)
Const STRing Argument.
Definition: memory.h:26
int authenticate(void *, Credential, Uuid)
void storageGet(Storage, char *, size_t, char **, size_t *)
Definition: storage/get.c:34
#define FALSE
Definition: commons.h:28
#define TRUE
Definition: commons.h:27
void serialize(void *, unsigned char **, size_t *)
Definition: serializable.c:32
int applicationLogin(Application this, Credential credential, Session session)
Definition: login.c:42
Uuid indexUuid(void *, Uuid)
Definition: indexable.c:33
void unserialize(void *, const unsigned char *, size_t)
Definition: serializable.c:41
AuthModule
Definition: auth/auth.h:51
#define CRED_PWD(c)
Definition: credential.h:30