Greenbone Security Assistant  9.0.1
gsad_user.c
Go to the documentation of this file.
1 /* Copyright (C) 2016-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_user.h"
26 
27 #include "gsad_base.h" /* for set_language_code */
28 #include "gsad_gmp_auth.h"
29 #include "gsad_session.h"
30 #include "gsad_settings.h"
31 #include "utils.h"
32 
33 #include <assert.h> /* for asset */
34 #include <gvm/util/uuidutils.h> /* for gvm_uuid_make */
35 #include <string.h> /* for strcmp */
36 
37 #define BROWSER_LANGUAGE "Browser Language"
38 
42 struct user
43 {
44  gchar *cookie;
45  gchar *token;
46  gchar *username;
47  gchar *password;
48  gchar *role;
49  gchar *timezone;
50  gchar *severity;
51  gchar *capabilities;
52  gchar *language;
53  gchar *pw_warning;
54  gchar *address;
55  time_t time;
56 };
57 
58 void
60 {
61  user->time = time (NULL);
62 }
63 
64 user_t *
66 {
67  user_t *user = g_malloc0 (sizeof (user_t));
68  return user;
69 }
70 
71 user_t *
72 user_new_with_data (const gchar *username, const gchar *password,
73  const gchar *timezone, const gchar *severity,
74  const gchar *role, const gchar *capabilities,
75  const gchar *language, const gchar *pw_warning,
76  const gchar *address)
77 {
78  user_t *user = user_new ();
79 
80  user->cookie = gvm_uuid_make ();
81  user->token = gvm_uuid_make ();
82 
83  user->username = g_strdup (username);
84  user->password = g_strdup (password);
85  user->timezone = g_strdup (timezone);
86  user->severity = g_strdup (severity);
87  user->role = g_strdup (role);
88  user->capabilities = g_strdup (capabilities);
89  user->language = g_strdup (language);
90  user->pw_warning = g_strdup (pw_warning);
91  user->address = g_strdup (address);
92 
93  user_set_language (user, language);
95 
96  return user;
97 }
98 
99 void
101 {
102  if (!user)
103  {
104  return;
105  }
106 
107  g_free (user->cookie);
108  g_free (user->token);
109  g_free (user->username);
110  g_free (user->password);
111  g_free (user->role);
112  g_free (user->timezone);
113  g_free (user->severity);
114  g_free (user->capabilities);
115  g_free (user->language);
116  g_free (user->pw_warning);
117  g_free (user->address);
118  g_free (user);
119 }
120 
121 user_t *
123 {
124  if (!user)
125  {
126  return NULL;
127  }
128 
129  user_t *copy = user_new ();
130 
131  copy->cookie = g_strdup (user->cookie);
132  copy->token = g_strdup (user->token);
133  copy->username = g_strdup (user->username);
134  copy->password = g_strdup (user->password);
135  copy->role = g_strdup (user->role);
136  copy->timezone = g_strdup (user->timezone);
137  copy->severity = g_strdup (user->severity);
138  copy->capabilities = g_strdup (user->capabilities);
139  copy->language = g_strdup (user->language);
140  copy->pw_warning = g_strdup (user->pw_warning);
141  copy->address = g_strdup (user->address);
142  copy->time = user->time;
143 
144  return copy;
145 }
146 
147 gboolean
149 {
150  return (time (NULL) - user->time) > (get_session_timeout () * 60);
151 }
152 
153 const gchar *
155 {
156  return user->username;
157 }
158 
159 const gchar *
161 {
162  return user->language;
163 }
164 
165 const gchar *
167 {
168  return user->cookie;
169 }
170 
171 const gchar *
173 {
174  return user->token;
175 }
176 
177 const gchar *
179 {
180  return user->capabilities;
181 }
182 
183 const gchar *
185 {
186  return user->pw_warning;
187 }
188 
189 const gchar *
191 {
192  return user->timezone;
193 }
194 
195 const gchar *
197 {
198  return user->address;
199 }
200 
201 const gchar *
203 {
204  return user->severity;
205 }
206 
207 const gchar *
209 {
210  return user->role;
211 }
212 
213 const gchar *
215 {
216  return user->password;
217 }
218 
219 const time_t
221 {
222  return user->time + (get_session_timeout () * 60);
223 }
224 
232 void
233 user_set_timezone (user_t *user, const gchar *timezone)
234 {
235  g_free (user->timezone);
236 
237  user->timezone = g_strdup (timezone);
238 }
239 
247 void
248 user_set_password (user_t *user, const gchar *password)
249 {
250  g_free (user->password);
251  g_free (user->pw_warning);
252 
253  user->password = g_strdup (password);
254  user->pw_warning = NULL;
255 }
256 
264 void
265 user_set_severity (user_t *user, const gchar *severity)
266 {
267  g_free (user->severity);
268 
269  user->severity = g_strdup (severity);
270 }
271 
279 void
280 user_set_language (user_t *user, const gchar *language)
281 {
282  g_free (user->language);
283 
284  if (language == NULL || str_equal (language, BROWSER_LANGUAGE))
285  {
286  user->language = NULL;
287  }
288  else
289  {
290  user->language = g_strdup (language);
291  }
292 }
293 
301 void
302 user_set_username (user_t *user, const gchar *username)
303 {
304  g_free (user->username);
305  user->username = g_strdup (username);
306 }
307 
315 int
317 {
319 
320  if (fuser)
321  {
322  session_remove_user (fuser->token);
323  user_free (fuser);
324  return 0;
325  }
326 
327  return -1;
328 }
329 
349 user_t *
350 user_add (const gchar *username, const gchar *password, const gchar *timezone,
351  const gchar *severity, const gchar *role, const gchar *capabilities,
352  const gchar *language, const gchar *pw_warning, const char *address)
353 {
355 
356  if (user && user_session_expired (user))
357  {
359  user_free (user);
360  }
361 
362  user = user_new_with_data (username, password, timezone, severity, role,
363  capabilities, language, pw_warning, address);
364 
366 
367  return user;
368 }
369 
388 int
389 user_find (const gchar *cookie, const gchar *token, const char *address,
390  user_t **user_return)
391 {
392  user_t *user = NULL;
393  if (token == NULL)
394  return USER_BAD_MISSING_TOKEN;
395 
396  user = session_get_user_by_id (token);
397 
398  if (user)
399  {
401  {
403  user_free (user);
404  return USER_EXPIRED_TOKEN;
405  }
406 
407  else if ((cookie == NULL) || !str_equal (user->cookie, cookie))
408  {
409  user_free (user);
411  }
412 
413  /* Verify that the user address matches the client's address. */
414  else if (address == NULL || !str_equal (address, user->address))
415  {
416  user_free (user);
418  }
419  else
420  {
422 
423  *user_return = user;
424  return USER_OK;
425  }
426  }
427 
428  /* should it be really USER_EXPIRED_TOKEN?
429  * No user has been found therefore the token couldn't even expire */
430  return USER_EXPIRED_TOKEN;
431 }
gsad_settings.h
Global settings for GSA.
user::language
gchar * language
User Interface Language.
Definition: gsad_user.c:52
user::time
time_t time
Login time.
Definition: gsad_user.c:55
user_get_username
const gchar * user_get_username(user_t *user)
Definition: gsad_user.c:154
get_session_timeout
int get_session_timeout()
Definition: gsad_settings.c:153
session_remove_user
void session_remove_user(const gchar *id)
Remove a user from the session "database".
Definition: gsad_session.c:164
user_get_cookie
const gchar * user_get_cookie(user_t *user)
Definition: gsad_user.c:166
user_get_client_address
const gchar * user_get_client_address(user_t *user)
Definition: gsad_user.c:196
user
User information structure, for sessions.
Definition: gsad_user.c:42
USER_IP_ADDRESS_MISSMATCH
#define USER_IP_ADDRESS_MISSMATCH
Definition: gsad_user.h:38
user_logout
int user_logout(user_t *user)
Logout a user.
Definition: gsad_user.c:316
user_set_language
void user_set_language(user_t *user, const gchar *language)
Set language of user.
Definition: gsad_user.c:280
user::severity
gchar * severity
Severity class.
Definition: gsad_user.c:50
gsad_gmp_auth.h
Authentication GMP.
session_get_user_by_id
user_t * session_get_user_by_id(const gchar *id)
Definition: gsad_session.c:97
user_new
user_t * user_new()
Definition: gsad_user.c:65
USER_OK
#define USER_OK
Definition: gsad_user.h:32
gsad_session.h
GSAD user session handling.
USER_BAD_MISSING_TOKEN
#define USER_BAD_MISSING_TOKEN
Definition: gsad_user.h:36
user::token
gchar * token
Request session token.
Definition: gsad_user.c:45
user::capabilities
gchar * capabilities
Capabilities.
Definition: gsad_user.c:51
user_renew_session
void user_renew_session(user_t *user)
Definition: gsad_user.c:59
user_new_with_data
user_t * user_new_with_data(const gchar *username, const gchar *password, const gchar *timezone, const gchar *severity, const gchar *role, const gchar *capabilities, const gchar *language, const gchar *pw_warning, const gchar *address)
Definition: gsad_user.c:72
user::timezone
gchar * timezone
Timezone.
Definition: gsad_user.c:49
session_get_user_by_username
user_t * session_get_user_by_username(const gchar *username)
Definition: gsad_session.c:116
user_session_expired
gboolean user_session_expired(user_t *user)
Definition: gsad_user.c:148
utils.h
Headers/structs for utility functions in GSAD.
user_get_session_timeout
const time_t user_get_session_timeout(user_t *user)
Definition: gsad_user.c:220
USER_EXPIRED_TOKEN
#define USER_EXPIRED_TOKEN
Definition: gsad_user.h:34
user_find
int user_find(const gchar *cookie, const gchar *token, const char *address, user_t **user_return)
Find a user, given a token and cookie.
Definition: gsad_user.c:389
gsad_user.h
GSAD user handling.
user_set_username
void user_set_username(user_t *user, const gchar *username)
Set username of user.
Definition: gsad_user.c:302
user::cookie
gchar * cookie
Cookie token.
Definition: gsad_user.c:44
session_add_user
void session_add_user(const gchar *id, user_t *user)
Add user to the session "database".
Definition: gsad_session.c:147
user_get_capabilities
const gchar * user_get_capabilities(user_t *user)
Definition: gsad_user.c:178
user_set_severity
void user_set_severity(user_t *user, const gchar *severity)
Set severity class of user.
Definition: gsad_user.c:265
user::username
gchar * username
Login name.
Definition: gsad_user.c:46
user_get_password
const gchar * user_get_password(user_t *user)
Definition: gsad_user.c:214
user_free
void user_free(user_t *user)
Definition: gsad_user.c:100
str_equal
gboolean str_equal(const gchar *str1, const gchar *str2)
Check if two strings are equal.
Definition: utils.c:35
user_get_language
const gchar * user_get_language(user_t *user)
Definition: gsad_user.c:160
user_set_password
void user_set_password(user_t *user, const gchar *password)
Set password of user.
Definition: gsad_user.c:248
user_get_timezone
const gchar * user_get_timezone(user_t *user)
Definition: gsad_user.c:190
user_copy
user_t * user_copy(user_t *user)
Definition: gsad_user.c:122
USER_BAD_MISSING_COOKIE
#define USER_BAD_MISSING_COOKIE
Definition: gsad_user.h:35
BROWSER_LANGUAGE
#define BROWSER_LANGUAGE
Definition: gsad_user.c:37
user_add
user_t * user_add(const gchar *username, const gchar *password, const gchar *timezone, const gchar *severity, const gchar *role, const gchar *capabilities, const gchar *language, const gchar *pw_warning, const char *address)
Add a user.
Definition: gsad_user.c:350
user_get_password_warning
const gchar * user_get_password_warning(user_t *user)
Definition: gsad_user.c:184
user_get_severity
const gchar * user_get_severity(user_t *user)
Definition: gsad_user.c:202
user_get_role
const gchar * user_get_role(user_t *user)
Definition: gsad_user.c:208
user_set_timezone
void user_set_timezone(user_t *user, const gchar *timezone)
Set timezone of user.
Definition: gsad_user.c:233
user::role
gchar * role
Role.
Definition: gsad_user.c:48
gsad_base.h
Headers/structs used generally in GSA.
user::pw_warning
gchar * pw_warning
Password policy warning.
Definition: gsad_user.c:53
user::password
gchar * password
Password.
Definition: gsad_user.c:47
user_get_token
const gchar * user_get_token(user_t *user)
Definition: gsad_user.c:172
user::address
gchar * address
Client's IP address.
Definition: gsad_user.c:54