Greenbone Security Assistant  8.0.0
gsad_session.c
Go to the documentation of this file.
1 /* Copyright (C) 2018 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: GPL-2.0-or-later
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
25 #include "gsad_session.h"
26 
27 #include "gsad_user.h"
28 #include "utils.h" /* for str_equal */
29 
30 #undef G_LOG_DOMAIN
31 
34 #define G_LOG_DOMAIN "gsad session"
35 
39 GPtrArray *users = NULL;
40 
44 static GMutex *mutex = NULL;
45 
46 user_t *
48 {
49  int index;
50  for (index = 0; index < users->len; index++)
51  {
52  user_t *item = (user_t *) g_ptr_array_index (users, index);
53  const gchar *token = user_get_token (item);
54 
55  if (str_equal (id, token))
56  {
57  return item;
58  }
59  }
60 
61  return NULL;
62 }
63 
64 void
66 {
68 
69  if (user)
70  {
71  g_ptr_array_remove (users, (gpointer) user);
72 
73  user_free (user);
74  }
75 }
76 
77 void
79 {
80  g_ptr_array_add (users, (gpointer) user_copy (user));
81 }
82 
83 void
85 {
86  mutex = g_malloc (sizeof (GMutex));
87  g_mutex_init (mutex);
88  users = g_ptr_array_new ();
89 }
90 
96 user_t *
97 session_get_user_by_id (const gchar *id)
98 {
99  user_t *user;
100 
101  g_mutex_lock (mutex);
102 
104 
105  g_mutex_unlock (mutex);
106 
107  return user;
108 }
109 
115 user_t *
116 session_get_user_by_username (const gchar *username)
117 {
118  int index;
119  user_t *user = NULL;
120 
121  g_mutex_lock (mutex);
122 
123  for (index = 0; index < users->len; index++)
124  {
125  user_t *item = (user_t *) g_ptr_array_index (users, index);
126  const gchar *name = user_get_username (item);
127 
128  if (str_equal (name, username))
129  {
130  user = user_copy (item);
131  break;
132  }
133  }
134 
135  g_mutex_unlock (mutex);
136 
137  return user;
138 }
139 
146 void
147 session_add_user (const gchar *id, user_t *user)
148 {
149  g_mutex_lock (mutex);
150 
152 
154 
155  g_mutex_unlock (mutex);
156 }
157 
163 void
164 session_remove_user (const gchar *id)
165 {
166  g_mutex_lock (mutex);
167 
169 
170  g_mutex_unlock (mutex);
171 }
172 
180 void
182 {
183  int index;
184 
185  g_mutex_lock (mutex);
186 
187  for (index = 0; index < users->len; index++)
188  {
189  user_t *item = (user_t *) g_ptr_array_index (users, index);
190 
191  const gchar *itemtoken = user_get_token (item);
192  const gchar *itemname = user_get_username (item);
193  const gchar *username = user_get_username (user);
194 
195  if (str_equal (itemname, username) && !str_equal (id, itemtoken))
196  {
197  g_debug ("%s: logging out user '%s', token '%s'", __FUNCTION__,
198  itemname, itemtoken);
199  g_ptr_array_remove (users, (gpointer) item);
200 
201  user_free (item);
202 
203  index--;
204  }
205  }
206 
207  g_mutex_unlock (mutex);
208 }
void session_remove_user(const gchar *id)
Remove a user from the session "database".
Definition: gsad_session.c:164
void session_init()
Definition: gsad_session.c:84
gboolean str_equal(const gchar *str1, const gchar *str2)
Check if two strings are equal.
Definition: utils.c:35
GSAD user session handling.
user_t * session_get_user_by_username(const gchar *username)
Definition: gsad_session.c:116
void user_free(user_t *user)
Definition: gsad_user.c:102
const gchar * user_get_username(user_t *user)
Definition: gsad_user.c:157
user_t * user_copy(user_t *user)
Definition: gsad_user.c:124
user_t * session_get_user_by_id(const gchar *id)
Definition: gsad_session.c:97
Headers/structs for utility functions in GSAD.
static GMutex * mutex
Mutex to prevent concurrent access to the session.
Definition: gsad_session.c:44
const gchar * user_get_token(user_t *user)
Definition: gsad_user.c:175
user_t * session_get_user_by_id_internal(const gchar *id)
Definition: gsad_session.c:47
void session_add_user_internal(user_t *user)
Definition: gsad_session.c:78
void session_add_user(const gchar *id, user_t *user)
Add user to the session "database".
Definition: gsad_session.c:147
void session_remove_user_internal(const gchar *id)
Definition: gsad_session.c:65
GPtrArray * users
User session data.
Definition: gsad_session.c:39
void session_remove_other_sessions(const gchar *id, user_t *user)
Removes all session of the user, except the one with the passed id.
Definition: gsad_session.c:181
User information structure, for sessions.
Definition: gsad_user.c:42
GSAD user handling.