Greenbone Security Assistant  8.0.0
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  gboolean guest;
57 };
58 
59 void
61 {
62  user->time = time (NULL);
63 }
64 
65 user_t *
67 {
68  user_t *user = g_malloc0 (sizeof (user_t));
69  return user;
70 }
71 
72 user_t *
73 user_new_with_data (const gchar *username, const gchar *password,
74  const gchar *timezone, const gchar *severity,
75  const gchar *role, const gchar *capabilities,
76  const gchar *language, const gchar *pw_warning,
77  const gchar *address, const gboolean guest)
78 {
79  user_t *user = user_new ();
80 
81  user->cookie = gvm_uuid_make ();
82  user->token = gvm_uuid_make ();
83 
84  user->username = g_strdup (username);
85  user->password = g_strdup (password);
86  user->timezone = g_strdup (timezone);
87  user->severity = g_strdup (severity);
88  user->role = g_strdup (role);
89  user->capabilities = g_strdup (capabilities);
90  user->language = g_strdup (language);
91  user->pw_warning = g_strdup (pw_warning);
92  user->address = g_strdup (address);
93  user->guest = guest;
94 
95  user_set_language (user, language);
97 
98  return user;
99 }
100 
101 void
103 {
104  if (!user)
105  {
106  return;
107  }
108 
109  g_free (user->cookie);
110  g_free (user->token);
111  g_free (user->username);
112  g_free (user->password);
113  g_free (user->role);
114  g_free (user->timezone);
115  g_free (user->severity);
116  g_free (user->capabilities);
117  g_free (user->language);
118  g_free (user->pw_warning);
119  g_free (user->address);
120  g_free (user);
121 }
122 
123 user_t *
125 {
126  if (!user)
127  {
128  return NULL;
129  }
130 
131  user_t *copy = user_new ();
132 
133  copy->cookie = g_strdup (user->cookie);
134  copy->token = g_strdup (user->token);
135  copy->username = g_strdup (user->username);
136  copy->password = g_strdup (user->password);
137  copy->role = g_strdup (user->role);
138  copy->timezone = g_strdup (user->timezone);
139  copy->severity = g_strdup (user->severity);
140  copy->capabilities = g_strdup (user->capabilities);
141  copy->language = g_strdup (user->language);
142  copy->pw_warning = g_strdup (user->pw_warning);
143  copy->address = g_strdup (user->address);
144  copy->time = user->time;
145  copy->guest = user->guest;
146 
147  return copy;
148 }
149 
150 gboolean
152 {
153  return (time (NULL) - user->time) > (get_session_timeout () * 60);
154 }
155 
156 const gchar *
158 {
159  return user->username;
160 }
161 
162 const gchar *
164 {
165  return user->language;
166 }
167 
168 const gchar *
170 {
171  return user->cookie;
172 }
173 
174 const gchar *
176 {
177  return user->token;
178 }
179 
180 const gchar *
182 {
183  return user->capabilities;
184 }
185 
186 const gchar *
188 {
189  return user->pw_warning;
190 }
191 
192 const gchar *
194 {
195  return user->timezone;
196 }
197 
198 const gchar *
200 {
201  return user->address;
202 }
203 
204 const gchar *
206 {
207  return user->severity;
208 }
209 
210 const gchar *
212 {
213  return user->role;
214 }
215 
216 const gchar *
218 {
219  return user->password;
220 }
221 
222 gboolean
224 {
225  return user->guest;
226 }
227 
228 const time_t
230 {
231  return user->time + (get_session_timeout () * 60);
232 }
233 
241 void
242 user_set_timezone (user_t *user, const gchar *timezone)
243 {
244  g_free (user->timezone);
245 
246  user->timezone = g_strdup (timezone);
247 }
248 
256 void
257 user_set_password (user_t *user, const gchar *password)
258 {
259  g_free (user->password);
260  g_free (user->pw_warning);
261 
262  user->password = g_strdup (password);
263  user->pw_warning = NULL;
264 }
265 
273 void
274 user_set_severity (user_t *user, const gchar *severity)
275 {
276  g_free (user->severity);
277 
278  user->severity = g_strdup (severity);
279 }
280 
288 void
289 user_set_language (user_t *user, const gchar *language)
290 {
291  g_free (user->language);
292 
293  if (language == NULL || str_equal (language, BROWSER_LANGUAGE))
294  {
295  user->language = NULL;
296  }
297  else
298  {
299  user->language = g_strdup (language);
300  }
301 }
302 
310 void
311 user_set_username (user_t *user, const gchar *username)
312 {
313  g_free (user->username);
314  user->username = g_strdup (username);
315 }
316 
324 int
326 {
328 
329  if (fuser)
330  {
331  session_remove_user (fuser->token);
332  user_free (fuser);
333  return 0;
334  }
335 
336  return -1;
337 }
338 
358 user_t *
359 user_add (const gchar *username, const gchar *password, const gchar *timezone,
360  const gchar *severity, const gchar *role, const gchar *capabilities,
361  const gchar *language, const gchar *pw_warning, const char *address)
362 {
363  const gchar *guest_username = get_guest_username ();
364 
366 
367  if (user && user_session_expired (user))
368  {
370  user_free (user);
371  }
372 
373  gboolean guest = 0;
374 
375  if (guest_username)
376  {
377  guest = str_equal (username, guest_username) ? 1 : 0;
378  }
379 
380  user =
381  user_new_with_data (username, password, timezone, severity, role,
382  capabilities, language, pw_warning, address, guest);
383 
385 
386  return user;
387 }
388 
410 int
411 user_find (const gchar *cookie, const gchar *token, const char *address,
412  user_t **user_return)
413 {
414  user_t *user = NULL;
415  if (token == NULL)
416  return USER_BAD_MISSING_TOKEN;
417 
418  user = session_get_user_by_id (token);
419 
420  if (user)
421  {
423  {
425  user_free (user);
426  return USER_EXPIRED_TOKEN;
427  }
428 
429  else if ((cookie == NULL) || !str_equal (user->cookie, cookie))
430  {
431  user_free (user);
433  }
434 
435  /* Verify that the user address matches the client's address. */
436  else if (address == NULL || !str_equal (address, user->address))
437  {
438  user_free (user);
440  }
441  else
442  {
444 
445  *user_return = user;
446  return USER_OK;
447  }
448  }
449 
450  /* should it be really USER_EXPIRED_TOKEN?
451  * No user has been found therefore the token couldn't even expire */
452  return USER_EXPIRED_TOKEN;
453 }
const gchar * user_get_severity(user_t *user)
Definition: gsad_user.c:205
gchar * pw_warning
Password policy warning.
Definition: gsad_user.c:53
void session_remove_user(const gchar *id)
Remove a user from the session "database".
Definition: gsad_session.c:164
void user_renew_session(user_t *user)
Definition: gsad_user.c:60
const gchar * user_get_password_warning(user_t *user)
Definition: gsad_user.c:187
void user_set_severity(user_t *user, const gchar *severity)
Set severity class of user.
Definition: gsad_user.c:274
time_t time
Login time.
Definition: gsad_user.c:55
gboolean str_equal(const gchar *str1, const gchar *str2)
Check if two strings are equal.
Definition: utils.c:35
gchar * language
User Interface Language.
Definition: gsad_user.c:52
void user_set_password(user_t *user, const gchar *password)
Set password of user.
Definition: gsad_user.c:257
void user_set_language(user_t *user, const gchar *language)
Set language of user.
Definition: gsad_user.c:289
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:359
#define BROWSER_LANGUAGE
Definition: gsad_user.c:37
GSAD user session handling.
void user_set_username(user_t *user, const gchar *username)
Set username of user.
Definition: gsad_user.c:311
user_t * session_get_user_by_username(const gchar *username)
Definition: gsad_session.c:116
int get_session_timeout()
const gchar * user_get_role(user_t *user)
Definition: gsad_user.c:211
void user_free(user_t *user)
Definition: gsad_user.c:102
gchar * capabilities
Capabilities.
Definition: gsad_user.c:51
const gchar * user_get_username(user_t *user)
Definition: gsad_user.c:157
Global settings for GSA.
#define USER_BAD_MISSING_COOKIE
Definition: gsad_user.h:35
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
gchar * severity
Severity class.
Definition: gsad_user.c:50
Headers/structs for utility functions in GSAD.
const gchar * user_get_token(user_t *user)
Definition: gsad_user.c:175
gchar * timezone
Timezone.
Definition: gsad_user.c:49
const time_t user_get_session_timeout(user_t *user)
Definition: gsad_user.c:229
Headers/structs used generally in GSA.
const gchar * user_get_password(user_t *user)
Definition: gsad_user.c:217
gboolean user_session_expired(user_t *user)
Definition: gsad_user.c:151
gchar * token
Request session token.
Definition: gsad_user.c:45
const gchar * get_guest_username()
void session_add_user(const gchar *id, user_t *user)
Add user to the session "database".
Definition: gsad_session.c:147
gchar * cookie
Cookie token.
Definition: gsad_user.c:44
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, const gboolean guest)
Definition: gsad_user.c:73
const gchar * user_get_capabilities(user_t *user)
Definition: gsad_user.c:181
gchar * username
Login name.
Definition: gsad_user.c:46
void user_set_timezone(user_t *user, const gchar *timezone)
Set timezone of user.
Definition: gsad_user.c:242
const gchar * user_get_timezone(user_t *user)
Definition: gsad_user.c:193
gboolean guest
Whether the user is a guest.
Definition: gsad_user.c:56
user_t * user_new()
Definition: gsad_user.c:66
#define USER_IP_ADDRESS_MISSMATCH
Definition: gsad_user.h:39
User information structure, for sessions.
Definition: gsad_user.c:42
GSAD user handling.
Authentication GMP.
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:411
gboolean user_get_guest(user_t *user)
Definition: gsad_user.c:223
#define USER_BAD_MISSING_TOKEN
Definition: gsad_user.h:36
const gchar * user_get_cookie(user_t *user)
Definition: gsad_user.c:169
gchar * password
Password.
Definition: gsad_user.c:47
const gchar * user_get_language(user_t *user)
Definition: gsad_user.c:163
gchar * role
Role.
Definition: gsad_user.c:48
#define USER_EXPIRED_TOKEN
Definition: gsad_user.h:34
gchar * guest_username
Guest username.
Definition: gsad_settings.c:37
const gchar * user_get_client_address(user_t *user)
Definition: gsad_user.c:199
gchar * address
Client's IP address.
Definition: gsad_user.c:54
#define USER_OK
Definition: gsad_user.h:32
int user_logout(user_t *user)
Logout a user.
Definition: gsad_user.c:325