Greenbone Security Assistant  9.0.1
gsad_user.c File Reference

GSAD user handling. More...

#include "gsad_user.h"
#include "gsad_base.h"
#include "gsad_gmp_auth.h"
#include "gsad_session.h"
#include "gsad_settings.h"
#include "utils.h"
#include <assert.h>
#include <gvm/util/uuidutils.h>
#include <string.h>
Include dependency graph for gsad_user.c:

Go to the source code of this file.

Data Structures

struct  user
 User information structure, for sessions. More...
 

Macros

#define BROWSER_LANGUAGE   "Browser Language"
 

Functions

void user_renew_session (user_t *user)
 
user_t * user_new ()
 
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)
 
void user_free (user_t *user)
 
user_t * user_copy (user_t *user)
 
gboolean user_session_expired (user_t *user)
 
const gchar * user_get_username (user_t *user)
 
const gchar * user_get_language (user_t *user)
 
const gchar * user_get_cookie (user_t *user)
 
const gchar * user_get_token (user_t *user)
 
const gchar * user_get_capabilities (user_t *user)
 
const gchar * user_get_password_warning (user_t *user)
 
const gchar * user_get_timezone (user_t *user)
 
const gchar * user_get_client_address (user_t *user)
 
const gchar * user_get_severity (user_t *user)
 
const gchar * user_get_role (user_t *user)
 
const gchar * user_get_password (user_t *user)
 
const time_t user_get_session_timeout (user_t *user)
 
void user_set_timezone (user_t *user, const gchar *timezone)
 Set timezone of user. More...
 
void user_set_password (user_t *user, const gchar *password)
 Set password of user. More...
 
void user_set_severity (user_t *user, const gchar *severity)
 Set severity class of user. More...
 
void user_set_language (user_t *user, const gchar *language)
 Set language of user. More...
 
void user_set_username (user_t *user, const gchar *username)
 Set username of user. More...
 
int user_logout (user_t *user)
 Logout a user. More...
 
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. More...
 
int user_find (const gchar *cookie, const gchar *token, const char *address, user_t **user_return)
 Find a user, given a token and cookie. More...
 

Detailed Description

GSAD user handling.

Definition in file gsad_user.c.

Macro Definition Documentation

◆ BROWSER_LANGUAGE

#define BROWSER_LANGUAGE   "Browser Language"

Definition at line 37 of file gsad_user.c.

Function Documentation

◆ 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.

Creates and initializes a user object with given parameters

It's up to the caller to free the returned user.

Parameters
[in]usernameName of user.
[in]passwordPassword for user.
[in]timezoneTimezone of user.
[in]severitySeverity class setting of user.
[in]roleRole of user.
[in]capabilitiesCapabilities of manager.
[in]languageUser Interface Language (language name or code)
[in]pw_warningPassword policy warning.
[in]addressClient's IP address.
Returns
Added user.

Definition at line 350 of file gsad_user.c.

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 }

References session_add_user(), session_get_user_by_username(), session_remove_user(), user::token, user_free(), user_new_with_data(), and user_session_expired().

Referenced by login().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ user_copy()

user_t* user_copy ( user_t *  user)

Definition at line 122 of file gsad_user.c.

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 }

References user::address, user::capabilities, user::cookie, user::language, user::password, user::pw_warning, user::role, user::severity, user::time, user::timezone, user::token, user_new(), and user::username.

Referenced by credentials_new(), session_add_user_internal(), session_get_user_by_id(), and session_get_user_by_username().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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.

If a user is returned, the session of the user is renewed and it's up to the caller to free the user.

Parameters
[in]cookieToken in cookie.
[in]tokenToken request parameter.
[in]addressClient's IP address.
[out]user_returnCopy of the User or NULL in error cases.
Returns
0 ok (user in user_return), 1 bad token, 2 expired token, 3 bad/missing cookie, 4 bad/missing token, 7 IP address mismatch,

Definition at line 389 of file gsad_user.c.

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 }

References user::address, user::cookie, session_add_user(), session_get_user_by_id(), session_remove_user(), str_equal(), user::token, USER_BAD_MISSING_COOKIE, USER_BAD_MISSING_TOKEN, USER_EXPIRED_TOKEN, user_free(), USER_IP_ADDRESS_MISSMATCH, USER_OK, and user_session_expired().

Referenced by get_user_from_connection().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ user_free()

void user_free ( user_t *  user)

Definition at line 100 of file gsad_user.c.

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 }

References user::address, user::capabilities, user::cookie, user::language, user::password, user::pw_warning, user::role, user::severity, user::timezone, user::token, and user::username.

Referenced by credentials_free(), handle_logout(), handle_setup_credentials(), login(), session_remove_other_sessions(), session_remove_user_internal(), user_add(), user_find(), and user_logout().

Here is the caller graph for this function:

◆ user_get_capabilities()

const gchar* user_get_capabilities ( user_t *  user)

Definition at line 178 of file gsad_user.c.

179 {
180  return user->capabilities;
181 }

References user::capabilities.

Referenced by command_enabled(), and gsad_message().

Here is the caller graph for this function:

◆ user_get_client_address()

const gchar* user_get_client_address ( user_t *  user)

Definition at line 196 of file gsad_user.c.

197 {
198  return user->address;
199 }

References user::address.

Referenced by envelope_gmp(), and gsad_message().

Here is the caller graph for this function:

◆ user_get_cookie()

const gchar* user_get_cookie ( user_t *  user)

Definition at line 166 of file gsad_user.c.

167 {
168  return user->cookie;
169 }

References user::cookie.

Referenced by login().

Here is the caller graph for this function:

◆ user_get_language()

const gchar* user_get_language ( user_t *  user)

Definition at line 160 of file gsad_user.c.

161 {
162  return user->language;
163 }

References user::language.

Referenced by handle_setup_credentials().

Here is the caller graph for this function:

◆ user_get_password()

const gchar* user_get_password ( user_t *  user)

Definition at line 214 of file gsad_user.c.

215 {
216  return user->password;
217 }

References user::password.

Referenced by manager_connect().

Here is the caller graph for this function:

◆ user_get_password_warning()

const gchar* user_get_password_warning ( user_t *  user)

Definition at line 184 of file gsad_user.c.

185 {
186  return user->pw_warning;
187 }

References user::pw_warning.

Referenced by envelope_gmp().

Here is the caller graph for this function:

◆ user_get_role()

const gchar* user_get_role ( user_t *  user)

Definition at line 208 of file gsad_user.c.

209 {
210  return user->role;
211 }

References user::role.

Referenced by envelope_gmp(), and gsad_message().

Here is the caller graph for this function:

◆ user_get_session_timeout()

const time_t user_get_session_timeout ( user_t *  user)

Definition at line 220 of file gsad_user.c.

221 {
222  return user->time + (get_session_timeout () * 60);
223 }

References get_session_timeout(), and user::time.

Referenced by envelope_gmp(), and renew_session_gmp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ user_get_severity()

const gchar* user_get_severity ( user_t *  user)

Definition at line 202 of file gsad_user.c.

203 {
204  return user->severity;
205 }

References user::severity.

Referenced by envelope_gmp().

Here is the caller graph for this function:

◆ user_get_timezone()

const gchar* user_get_timezone ( user_t *  user)

Definition at line 190 of file gsad_user.c.

191 {
192  return user->timezone;
193 }

References user::timezone.

Referenced by envelope_gmp().

Here is the caller graph for this function:

◆ user_get_token()

const gchar* user_get_token ( user_t *  user)

Definition at line 172 of file gsad_user.c.

173 {
174  return user->token;
175 }

References user::token.

Referenced by envelope_gmp(), gsad_message(), renew_session_gmp(), save_my_settings_gmp(), save_user_gmp(), session_get_user_by_id_internal(), and session_remove_other_sessions().

Here is the caller graph for this function:

◆ user_get_username()

const gchar* user_get_username ( user_t *  user)

Definition at line 154 of file gsad_user.c.

155 {
156  return user->username;
157 }

References user::username.

Referenced by envelope_gmp(), format_file_name(), gsad_message(), handle_logout(), handle_setup_user(), manager_connect(), save_my_settings_gmp(), save_user_gmp(), session_get_user_by_username(), and session_remove_other_sessions().

Here is the caller graph for this function:

◆ user_logout()

int user_logout ( user_t *  user)

Logout a user.

Parameters
[in]userUser.
Returns
0 success, -1 error.

Definition at line 316 of file gsad_user.c.

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 }

References session_get_user_by_id(), session_remove_user(), user::token, and user_free().

Referenced by handle_logout().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ user_new()

user_t* user_new ( )

Definition at line 65 of file gsad_user.c.

66 {
67  user_t *user = g_malloc0 (sizeof (user_t));
68  return user;
69 }

Referenced by user_copy(), and user_new_with_data().

Here is the caller graph for this function:

◆ 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 at line 72 of file gsad_user.c.

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 }

References user::address, user::capabilities, user::cookie, user::language, user::password, user::pw_warning, user::role, user::severity, user::timezone, user::token, user_new(), user_renew_session(), user_set_language(), and user::username.

Referenced by user_add().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ user_renew_session()

void user_renew_session ( user_t *  user)

Definition at line 59 of file gsad_user.c.

60 {
61  user->time = time (NULL);
62 }

References user::time.

Referenced by renew_session_gmp(), and user_new_with_data().

Here is the caller graph for this function:

◆ user_session_expired()

gboolean user_session_expired ( user_t *  user)

Definition at line 148 of file gsad_user.c.

149 {
150  return (time (NULL) - user->time) > (get_session_timeout () * 60);
151 }

References get_session_timeout(), and user::time.

Referenced by user_add(), and user_find().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ user_set_language()

void user_set_language ( user_t *  user,
const gchar *  language 
)

Set language of user.

Parameters
[in]userUser.
[in]languageLanguage.

Definition at line 280 of file gsad_user.c.

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 }

References BROWSER_LANGUAGE, user::language, and str_equal().

Referenced by save_my_settings_gmp(), and user_new_with_data().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ user_set_password()

void user_set_password ( user_t *  user,
const gchar *  password 
)

Set password of user.

Parameters
[in]userUser.
[in]passwordPassword.

Definition at line 248 of file gsad_user.c.

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 }

References user::password, and user::pw_warning.

Referenced by save_my_settings_gmp(), and save_user_gmp().

Here is the caller graph for this function:

◆ user_set_severity()

void user_set_severity ( user_t *  user,
const gchar *  severity 
)

Set severity class of user.

Parameters
[in]userUser.
[in]severitySeverity class.

Definition at line 265 of file gsad_user.c.

266 {
267  g_free (user->severity);
268 
269  user->severity = g_strdup (severity);
270 }

References user::severity.

Referenced by save_my_settings_gmp().

Here is the caller graph for this function:

◆ user_set_timezone()

void user_set_timezone ( user_t *  user,
const gchar *  timezone 
)

Set timezone of user.

Parameters
[in]userUser.
[in]timezoneTimezone.

Definition at line 233 of file gsad_user.c.

234 {
235  g_free (user->timezone);
236 
237  user->timezone = g_strdup (timezone);
238 }

References user::timezone.

Referenced by save_my_settings_gmp().

Here is the caller graph for this function:

◆ user_set_username()

void user_set_username ( user_t *  user,
const gchar *  username 
)

Set username of user.

Parameters
[in]userUser.
[in]usernameUsername.

Definition at line 302 of file gsad_user.c.

303 {
304  g_free (user->username);
305  user->username = g_strdup (username);
306 }

References user::username.

Referenced by save_user_gmp().

Here is the caller graph for this function:
user::language
gchar * language
User Interface Language.
Definition: gsad_user.c:52
user::time
time_t time
Login time.
Definition: gsad_user.c:55
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
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_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
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
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
USER_EXPIRED_TOKEN
#define USER_EXPIRED_TOKEN
Definition: gsad_user.h:34
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::username
gchar * username
Login name.
Definition: gsad_user.c:46
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_BAD_MISSING_COOKIE
#define USER_BAD_MISSING_COOKIE
Definition: gsad_user.h:35
BROWSER_LANGUAGE
#define BROWSER_LANGUAGE
Definition: gsad_user.c:37
user::role
gchar * role
Role.
Definition: gsad_user.c:48
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::address
gchar * address
Client's IP address.
Definition: gsad_user.c:54