Greenbone Security Assistant  7.0.3~git
gsad.c File Reference

Main module of Greenbone Security Assistant daemon. More...

#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <gcrypt.h>
#include <glib.h>
#include <gnutls/gnutls.h>
#include <langinfo.h>
#include <locale.h>
#include <netinet/in.h>
#include <openvas/misc/openvas_logging.h>
#include <openvas/base/openvas_file.h>
#include <openvas/base/openvas_networking.h>
#include <openvas/base/pidfile.h>
#include <openvas/omp/xml.h>
#include <openvas/misc/openvas_uuid.h>
#include <pthread.h>
#include <pwd.h>
#include <grp.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <microhttpd.h>
#include "gsad_base.h"
#include "gsad_omp.h"
#include "validator.h"
#include "xslt_i18n.h"
Include dependency graph for gsad.c:

Go to the source code of this file.

Data Structures

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

Macros

#define _GNU_SOURCE   /* for strcasecmp */
 The Glib fatal mask, redefined to leave out G_LOG_FLAG_RECURSION. More...
 
#define G_LOG_DOMAIN   "gsad main"
 GLib log domain. More...
 
#define G_LOG_FATAL_MASK   G_LOG_LEVEL_ERROR
 
#define MHD_HTTP_NOT_ACCEPTABLE   MHD_HTTP_METHOD_NOT_ACCEPTABLE
 The symbol is deprecated, but older versions (0.9.37 - Debian jessie) don't define it yet. More...
 
#define SID_COOKIE_NAME   "GSAD_SID"
 Name of the cookie used to store the SID. More...
 
#define DEFAULT_GSAD_HTTPS_PORT   443
 Fallback GSAD port for HTTPS. More...
 
#define DEFAULT_GSAD_HTTP_PORT   80
 Fallback GSAD port for HTTP. More...
 
#define DEFAULT_GSAD_PORT   9392
 Fallback unprivileged GSAD port. More...
 
#define DEFAULT_GSAD_REDIRECT_PORT   80
 Fallback GSAD port. More...
 
#define DEFAULT_OPENVAS_MANAGER_PORT   9390
 Fallback Manager port. More...
 
#define POST_BUFFER_SIZE   500000
 Buffer size for POST processor. More...
 
#define MAX_FILE_NAME_SIZE   128
 Maximum length of "file name" for /help/ URLs. More...
 
#define SESSION_TIMEOUT   15
 Max number of minutes between activity in a session. More...
 
#define DEFAULT_CLIENT_WATCH_INTERVAL   1
 Default value for client_watch_interval. More...
 
#define DEFAULT_GSAD_FACE   "classic"
 Default face name. More...
 
#define DEFAULT_GSAD_X_FRAME_OPTIONS   "SAMEORIGIN"
 Default value for HTTP header "X-Frame-Options". More...
 
#define DEFAULT_GSAD_CONTENT_SECURITY_POLICY
 Default value for HTTP header "Content-Security-Policy". More...
 
#define DEFAULT_GSAD_GUEST_CHART_X_FRAME_OPTIONS   "SAMEORIGIN"
 Default value for HTTP header "X-Frame-Options" for guest charts. More...
 
#define DEFAULT_GSAD_GUEST_CHART_CONTENT_SECURITY_POLICY
 Default guest charts value for HTTP header "Content-Security-Policy". More...
 
#define DEFAULT_GSAD_HSTS_MAX_AGE   31536000
 Default "max-age" for HTTP header "Strict-Transport-Security". More...
 
#define UTF8_ERROR_PAGE(location)
 
#define HOST_HEADER_ERROR_PAGE
 
#define USER_OK   0
 
#define USER_BAD_TOKEN   1
 
#define USER_EXPIRED_TOKEN   2
 
#define USER_BAD_MISSING_COOKIE   3
 
#define USER_BAD_MISSING_TOKEN   4
 
#define USER_GUEST_LOGIN_FAILED   5
 
#define USER_OMP_DOWN   6
 
#define USER_IP_ADDRESS_MISSMATCH   7
 
#define USER_GUEST_LOGIN_ERROR   -1
 
#define ELSE(name)
 
#define ELSE(name)
 
#define EXPIRES_LENGTH   100
 
#define MAX_HOST_LEN   1000
 
#define DATE_2822_LEN   100
 

Typedefs

typedef struct user user_t
 User information type, for sessions. More...
 

Functions

void add_security_headers (struct MHD_Response *response)
 Add security headers to a MHD response. More...
 
void add_guest_chart_content_security_headers (struct MHD_Response *response)
 Add guest chart content security headers to a MHD response. More...
 
user_tuser_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, GTree *chart_prefs, const gchar *autorefresh, 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...
 
int user_set_timezone (const gchar *token, const gchar *timezone)
 Set timezone of user. More...
 
int user_set_password (const gchar *token, const gchar *password)
 Set password of user. More...
 
int user_set_severity (const gchar *token, const gchar *severity)
 Set severity class of user. More...
 
int user_set_language (const gchar *token, const gchar *language)
 Set language of user. More...
 
int user_set_charts (const gchar *token, const int charts)
 Set charts setting of user. More...
 
int user_set_chart_pref (const gchar *token, gchar *pref_id, gchar *pref_value)
 Set a chart preference of a user. More...
 
int user_set_autorefresh (const gchar *token, const gchar *autorefresh)
 Set default autorefresh interval of user. More...
 
int user_logout_all_sessions (const gchar *username, credentials_t *credentials)
 Logs out all sessions of a given user, except the current one. More...
 
void user_release (user_t *user)
 Release a user_t returned by user_add or user_find. More...
 
void user_remove (user_t *user)
 Remove a user from the session "database", releasing the user_t too. More...
 
int token_user (const gchar *token, user_t **user_return)
 Find a user, given a token. More...
 
int token_user_remove (const char *token)
 Remove a user from the session "database", releasing the user_t too. More...
 
void init_validator ()
 Initialise the parameter validator. More...
 

Variables

volatile int termination_signal = 0
 Flag for signal handler. More...
 
 GCRY_THREAD_OPTION_PTHREAD_IMPL
 Libgcrypt thread callback definition for libgcrypt < 1.6.0. More...
 
const char * NOT_FOUND_TITLE = "Invalid request"
 Title for "Page not found" messages. More...
 
const char * NOT_FOUND_MESSAGE = "The requested page or file does not exist."
 Main message for "Page not found" messages. More...
 
const char * ERROR_PAGE = "<html><body>HTTP Method not supported</body></html>"
 Error page HTML. More...
 
char * BAD_REQUEST_PAGE
 Bad request error HTML. More...
 
char * SERVER_ERROR
 Server error HTML. More...
 
struct MHD_Daemon * gsad_daemon
 The handle on the embedded HTTP daemon. More...
 
GSList * address_list = NULL
 The IP addresses of this program, "the GSAD". More...
 
GHashTable * gsad_header_hosts = NULL
 Host names and IP accepted in the "Host" HTTP header. More...
 
gchar * redirect_location = NULL
 Location for redirection server. More...
 
pid_t redirect_pid = 0
 PID of redirect child in parent, 0 in child. More...
 
pid_t unix_pid = 0
 PID of unix socket child in parent, 0 in child. More...
 
int unix_socket = 0
 Unix socket to listen on. More...
 
GSList * log_config = NULL
 Logging parameters, as passed to setup_log_handlers. More...
 
int use_secure_cookie = 1
 Whether to use a secure cookie. More...
 
int session_timeout
 Maximum number of minutes of user idle time. More...
 
gchar * guest_username = NULL
 Guest username. More...
 
gchar * guest_password = NULL
 Guest password. More...
 
GPtrArray * users = NULL
 User session data. More...
 
gchar * http_x_frame_options
 Current value for HTTP header "X-Frame-Options". More...
 
gchar * http_content_security_policy
 Current value for HTTP header "Content-Security-Policy". More...
 
gchar * http_guest_chart_x_frame_options
 Current guest chart specific value for HTTP header "X-Frame-Options". More...
 
gchar * http_guest_chart_content_security_policy
 Current guest chart value for HTTP header "Content-Security-Policy". More...
 
gchar * http_strict_transport_security
 Current value of for HTTP header "Strict-Transport-Security". More...
 
gboolean ignore_http_x_real_ip
 Current preference for using X_Real_IP from HTTP header. More...
 
int chroot_state = 0
 Whether chroot is used. More...
 
int client_watch_interval = DEFAULT_CLIENT_WATCH_INTERVAL
 Interval in seconds to check whether client connection was closed. More...
 
validator_t validator
 Parameter validator. More...
 

Detailed Description

Main module of Greenbone Security Assistant daemon.

This file contains the core of the GSA server process that handles HTTPS requests and communicates with OpenVAS-Manager via the OMP protocol.

Definition in file gsad.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE   /* for strcasecmp */

The Glib fatal mask, redefined to leave out G_LOG_FLAG_RECURSION.

Definition at line 63 of file gsad.c.

◆ DATE_2822_LEN

#define DATE_2822_LEN   100

◆ DEFAULT_CLIENT_WATCH_INTERVAL

#define DEFAULT_CLIENT_WATCH_INTERVAL   1

Default value for client_watch_interval.

Definition at line 173 of file gsad.c.

◆ DEFAULT_GSAD_CONTENT_SECURITY_POLICY

#define DEFAULT_GSAD_CONTENT_SECURITY_POLICY
Value:
"default-src 'self' 'unsafe-inline';" \
" img-src 'self' blob:;" \
" frame-ancestors 'self'"

Default value for HTTP header "Content-Security-Policy".

Definition at line 188 of file gsad.c.

◆ DEFAULT_GSAD_FACE

#define DEFAULT_GSAD_FACE   "classic"

Default face name.

Definition at line 178 of file gsad.c.

◆ DEFAULT_GSAD_GUEST_CHART_CONTENT_SECURITY_POLICY

#define DEFAULT_GSAD_GUEST_CHART_CONTENT_SECURITY_POLICY
Value:
"default-src 'self' 'unsafe-inline';" \
" img-src 'self' blob:;" \
" frame-ancestors *"

Default guest charts value for HTTP header "Content-Security-Policy".

Definition at line 201 of file gsad.c.

◆ DEFAULT_GSAD_GUEST_CHART_X_FRAME_OPTIONS

#define DEFAULT_GSAD_GUEST_CHART_X_FRAME_OPTIONS   "SAMEORIGIN"

Default value for HTTP header "X-Frame-Options" for guest charts.

Definition at line 196 of file gsad.c.

◆ DEFAULT_GSAD_HSTS_MAX_AGE

#define DEFAULT_GSAD_HSTS_MAX_AGE   31536000

Default "max-age" for HTTP header "Strict-Transport-Security".

Definition at line 209 of file gsad.c.

◆ DEFAULT_GSAD_HTTP_PORT

#define DEFAULT_GSAD_HTTP_PORT   80

Fallback GSAD port for HTTP.

Definition at line 138 of file gsad.c.

◆ DEFAULT_GSAD_HTTPS_PORT

#define DEFAULT_GSAD_HTTPS_PORT   443

Fallback GSAD port for HTTPS.

Definition at line 133 of file gsad.c.

◆ DEFAULT_GSAD_PORT

#define DEFAULT_GSAD_PORT   9392

Fallback unprivileged GSAD port.

Definition at line 143 of file gsad.c.

◆ DEFAULT_GSAD_REDIRECT_PORT

#define DEFAULT_GSAD_REDIRECT_PORT   80

Fallback GSAD port.

Definition at line 148 of file gsad.c.

◆ DEFAULT_GSAD_X_FRAME_OPTIONS

#define DEFAULT_GSAD_X_FRAME_OPTIONS   "SAMEORIGIN"

Default value for HTTP header "X-Frame-Options".

Definition at line 183 of file gsad.c.

◆ DEFAULT_OPENVAS_MANAGER_PORT

#define DEFAULT_OPENVAS_MANAGER_PORT   9390

Fallback Manager port.

Definition at line 153 of file gsad.c.

◆ ELSE [1/2]

#define ELSE (   name)
Value:
else if (!strcmp (cmd, G_STRINGIFY (name))) \
con_info->response = name ## _omp (&connection, credentials, \
con_info->params, &response_data);

◆ ELSE [2/2]

#define ELSE (   name)
Value:
else if (!strcmp (cmd, G_STRINGIFY (name))) \
ret = name ## _omp (&connection, credentials, params, response_data);

◆ EXPIRES_LENGTH

#define EXPIRES_LENGTH   100

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "gsad main"

GLib log domain.

Definition at line 112 of file gsad.c.

◆ G_LOG_FATAL_MASK

#define G_LOG_FATAL_MASK   G_LOG_LEVEL_ERROR

Definition at line 115 of file gsad.c.

◆ HOST_HEADER_ERROR_PAGE

#define HOST_HEADER_ERROR_PAGE
Value:
"<html>" \
"<head><title>Invalid request</title></head>" \
"<body>The request contained an unknown or invalid Host header." \
" If you are trying to access GSA via its hostname or a proxy," \
" make sure GSA is set up to allow it." \
"</body>" \
"</html>"

Definition at line 264 of file gsad.c.

◆ MAX_FILE_NAME_SIZE

#define MAX_FILE_NAME_SIZE   128

Maximum length of "file name" for /help/ URLs.

Definition at line 163 of file gsad.c.

◆ MAX_HOST_LEN

#define MAX_HOST_LEN   1000

◆ MHD_HTTP_NOT_ACCEPTABLE

#define MHD_HTTP_NOT_ACCEPTABLE   MHD_HTTP_METHOD_NOT_ACCEPTABLE

The symbol is deprecated, but older versions (0.9.37 - Debian jessie) don't define it yet.

Definition at line 122 of file gsad.c.

◆ POST_BUFFER_SIZE

#define POST_BUFFER_SIZE   500000

Buffer size for POST processor.

Definition at line 158 of file gsad.c.

◆ SESSION_TIMEOUT

#define SESSION_TIMEOUT   15

Max number of minutes between activity in a session.

Definition at line 168 of file gsad.c.

◆ SID_COOKIE_NAME

#define SID_COOKIE_NAME   "GSAD_SID"

Name of the cookie used to store the SID.

Definition at line 128 of file gsad.c.

◆ USER_BAD_MISSING_COOKIE

#define USER_BAD_MISSING_COOKIE   3

Definition at line 516 of file gsad.c.

◆ USER_BAD_MISSING_TOKEN

#define USER_BAD_MISSING_TOKEN   4

Definition at line 517 of file gsad.c.

◆ USER_BAD_TOKEN

#define USER_BAD_TOKEN   1

Definition at line 514 of file gsad.c.

◆ USER_EXPIRED_TOKEN

#define USER_EXPIRED_TOKEN   2

Definition at line 515 of file gsad.c.

◆ USER_GUEST_LOGIN_ERROR

#define USER_GUEST_LOGIN_ERROR   -1

Definition at line 521 of file gsad.c.

◆ USER_GUEST_LOGIN_FAILED

#define USER_GUEST_LOGIN_FAILED   5

Definition at line 518 of file gsad.c.

◆ USER_IP_ADDRESS_MISSMATCH

#define USER_IP_ADDRESS_MISSMATCH   7

Definition at line 520 of file gsad.c.

◆ USER_OK

#define USER_OK   0

Definition at line 513 of file gsad.c.

◆ USER_OMP_DOWN

#define USER_OMP_DOWN   6

Definition at line 519 of file gsad.c.

◆ UTF8_ERROR_PAGE

#define UTF8_ERROR_PAGE (   location)
Value:
"<html>" \
"<head><title>Invalid request</title></head>" \
"<body>The request contained invalid UTF-8 in " location ".</body>" \
"</html>"

Definition at line 255 of file gsad.c.

Typedef Documentation

◆ user_t

typedef struct user user_t

User information type, for sessions.

Definition at line 440 of file gsad.c.

Function Documentation

◆ add_guest_chart_content_security_headers()

void add_guest_chart_content_security_headers ( struct MHD_Response *  response)

Add guest chart content security headers to a MHD response.

Definition at line 403 of file gsad.c.

404 {
405  if (strcmp (http_x_frame_options, ""))
406  MHD_add_response_header (response, "X-Frame-Options",
408  if (strcmp (http_content_security_policy, ""))
409  MHD_add_response_header (response, "Content-Security-Policy",
411 }
gchar * http_guest_chart_x_frame_options
Current guest chart specific value for HTTP header "X-Frame-Options".
Definition: gsad.c:355
gchar * http_x_frame_options
Current value for HTTP header "X-Frame-Options".
Definition: gsad.c:345
gchar * http_content_security_policy
Current value for HTTP header "Content-Security-Policy".
Definition: gsad.c:350
gchar * http_guest_chart_content_security_policy
Current guest chart value for HTTP header "Content-Security-Policy".
Definition: gsad.c:360

References http_content_security_policy, http_guest_chart_content_security_policy, http_guest_chart_x_frame_options, and http_x_frame_options.

◆ add_security_headers()

void add_security_headers ( struct MHD_Response *  response)

Add security headers to a MHD response.

Definition at line 386 of file gsad.c.

387 {
388  if (strcmp (http_x_frame_options, ""))
389  MHD_add_response_header (response, "X-Frame-Options",
391  if (strcmp (http_content_security_policy, ""))
392  MHD_add_response_header (response, "Content-Security-Policy",
395  MHD_add_response_header (response, "Strict-Transport-Security",
397 }
gchar * http_x_frame_options
Current value for HTTP header "X-Frame-Options".
Definition: gsad.c:345
gchar * http_content_security_policy
Current value for HTTP header "Content-Security-Policy".
Definition: gsad.c:350
gchar * http_strict_transport_security
Current value of for HTTP header "Strict-Transport-Security".
Definition: gsad.c:365

References http_content_security_policy, http_strict_transport_security, and http_x_frame_options.

◆ init_validator()

void init_validator ( )

Initialise the parameter validator.

Definition at line 999 of file gsad.c.

References openvas_validator_add(), openvas_validator_alias(), openvas_validator_new(), and validator.

Here is the call graph for this function:

◆ token_user()

int token_user ( const gchar *  token,
user_t **  user_return 
)

Find a user, given a token.

If a user is returned, it's up to the caller to release the user.

Parameters
[in]tokenToken request parameter.
[out]user_returnUser.
Returns
0 ok (user in user_return), 1 bad token, 2 expired token.

Definition at line 938 of file gsad.c.

939 {
940  int ret;
941  user_t *user = NULL;
942  int index;
943  g_mutex_lock (mutex);
944  for (index = 0; index < users->len; index++)
945  {
946  user_t *item;
947  item = (user_t*) g_ptr_array_index (users, index);
948  if (strcmp (item->token, token) == 0)
949  {
950  user = item;
951  break;
952  }
953  }
954  if (user)
955  {
956  if (time (NULL) - user->time > (session_timeout * 60))
957  ret = 2;
958  else
959  {
960  *user_return = user;
961  ret = 0;
962  user->time = time (NULL);
963  return ret;
964  }
965  }
966  else
967  ret = 1;
968  g_mutex_unlock (mutex);
969  return ret;
970 }
time_t time
Login time.
Definition: gsad.c:429
int session_timeout
Maximum number of minutes of user idle time.
Definition: gsad.c:325
char * token
Request session token.
Definition: gsad.c:419
GPtrArray * users
User session data.
Definition: gsad.c:340
User information structure, for sessions.
Definition: gsad.c:416

Referenced by token_user_remove().

Here is the caller graph for this function:

◆ token_user_remove()

int token_user_remove ( const char *  token)

Remove a user from the session "database", releasing the user_t too.

Parameters
[in]tokenUser's token.
Returns
0 success, -1 error.

Definition at line 980 of file gsad.c.

981 {
982  user_t *user;
983  if (token_user (token, &user))
984  return -1;
985  g_ptr_array_remove (users, (gpointer) user);
986  g_mutex_unlock (mutex);
987  return 0;
988 }
GPtrArray * users
User session data.
Definition: gsad.c:340
User information structure, for sessions.
Definition: gsad.c:416
int token_user(const gchar *token, user_t **user_return)
Find a user, given a token.
Definition: gsad.c:938

References user::token, token_user(), and users.

Referenced by logout().

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

◆ 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,
GTree *  chart_prefs,
const gchar *  autorefresh,
const char *  address 
)

Add a user.

Creates and initializes a user object with given parameters

It's up to the caller to release 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]chart_prefsThe chart preferences.
[in]autorefreshThe autorefresh preference.
[in]addressClient's IP address.
Returns
Added user.

Definition at line 469 of file gsad.c.

473 {
474  user_t *user = NULL;
475  int index;
476  g_mutex_lock (mutex);
477  for (index = 0; index < users->len; index++)
478  {
479  user_t *item;
480  item = (user_t*) g_ptr_array_index (users, index);
481  if (strcmp (item->username, username) == 0)
482  {
483  if (time (NULL) - item->time > (session_timeout * 60))
484  g_ptr_array_remove (users, (gpointer) item);
485  }
486  }
487  user = g_malloc (sizeof (user_t));
488  user->cookie = openvas_uuid_make ();
489  user->token = openvas_uuid_make ();
490  user->username = g_strdup (username);
491  user->password = g_strdup (password);
492  user->role = g_strdup (role);
493  user->timezone = g_strdup (timezone);
494  user->severity = g_strdup (severity);
495  user->capabilities = g_strdup (capabilities);
496  user->pw_warning = pw_warning ? g_strdup (pw_warning) : NULL;
497  user->chart_prefs = chart_prefs;
498  user->autorefresh = g_strdup (autorefresh);
499  user->last_filt_ids = g_tree_new_full ((GCompareDataFunc) g_strcmp0,
500  NULL, g_free, g_free);
501  g_ptr_array_add (users, (gpointer) user);
502  set_language_code (&user->language, language);
503  user->time = time (NULL);
504  user->charts = 0;
505  if (guest_username)
506  user->guest = strcmp (username, guest_username) ? 0 : 1;
507  else
508  user->guest = 0;
509  user->address = g_strdup (address);
510  return user;
511 }
char * address
Client's IP address.
Definition: gsad.c:428
gchar * pw_warning
Password policy warning.
Definition: gsad.c:427
time_t time
Login time.
Definition: gsad.c:429
gchar * language
User Interface Language, in short form like "en".
Definition: gsad.c:426
gchar * guest_username
Guest username.
Definition: gsad.c:330
int charts
Whether to show charts for this user.
Definition: gsad.c:430
int session_timeout
Maximum number of minutes of user idle time.
Definition: gsad.c:325
void set_language_code(gchar **lang, const gchar *language)
Set language code of user.
Definition: gsad_base.c:218
gchar * capabilities
Capabilities.
Definition: gsad.c:425
GTree * chart_prefs
Chart preferences.
Definition: gsad.c:431
gchar * severity
Severity class.
Definition: gsad.c:424
char * token
Request session token.
Definition: gsad.c:419
gchar * timezone
Timezone.
Definition: gsad.c:423
int guest
Whether the user is a guest.
Definition: gsad.c:434
GPtrArray * users
User session data.
Definition: gsad.c:340
gchar * username
Login name.
Definition: gsad.c:420
GTree * last_filt_ids
Last used filter ids.
Definition: gsad.c:433
User information structure, for sessions.
Definition: gsad.c:416
gchar * password
Password.
Definition: gsad.c:421
gchar * role
Role.
Definition: gsad.c:422
char * cookie
Cookie token.
Definition: gsad.c:418
gchar * autorefresh
Auto-Refresh interval.
Definition: gsad.c:432

◆ 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, it's up to the caller to release the user.

Parameters
[in]cookieToken in cookie.
[in]tokenToken request parameter.
[in]addressClient's IP address.
[out]user_returnUser.
Returns
0 ok (user in user_return), 1 bad token, 2 expired token, 3 bad/missing cookie, 4 bad/missing token, 5 guest login failed, 6 OMP down for guest login, 7 IP address mismatch, -1 error during guest login.

Definition at line 539 of file gsad.c.

541 {
542  int ret;
543  user_t *user = NULL;
544  int index;
545  if (token == NULL)
546  return USER_BAD_MISSING_TOKEN;
547 
548  if (guest_username && token && (strcmp (token, "guest") == 0))
549  {
550  int ret;
551  gchar *timezone, *role, *capabilities, *severity, *language;
552  gchar *pw_warning, *autorefresh;
553  GTree *chart_prefs;
554 
555  if (cookie)
556  {
557  /* Look for an existing guest user from the same browser (that is,
558  * with the same cookie). */
559 
560  g_mutex_lock (mutex);
561  for (index = 0; index < users->len; index++)
562  {
563  user_t *item;
564  item = (user_t*) g_ptr_array_index (users, index);
565  if (item->guest && (strcmp (item->cookie, cookie) == 0))
566  {
567  user = item;
568  break;
569  }
570  }
571  if (user)
572  {
573  *user_return = user;
574  user->time = time (NULL);
575  return USER_OK;
576  }
577  g_mutex_unlock (mutex);
578  }
579 
580  /* Log in as guest. */
581 
584  &role,
585  &timezone,
586  &severity,
587  &capabilities,
588  &language,
589  &pw_warning,
590  &chart_prefs,
591  &autorefresh);
592  if (ret == 1)
594  else if (ret == 2)
595  return USER_OMP_DOWN;
596  else if (ret == -1)
597  return USER_GUEST_LOGIN_ERROR;
598  else
599  {
600  user_t *user;
601  user = user_add (guest_username, guest_password, timezone, severity,
602  role, capabilities, language, pw_warning,
603  chart_prefs, autorefresh, address);
604  *user_return = user;
605  g_free (timezone);
606  g_free (severity);
607  g_free (capabilities);
608  g_free (language);
609  g_free (role);
610  g_free (pw_warning);
611  g_free (autorefresh);
612  return USER_OK;
613  }
614  }
615 
616  g_mutex_lock (mutex);
617  ret = USER_OK;
618  for (index = 0; index < users->len; index++)
619  {
620  user_t *item;
621  item = (user_t*) g_ptr_array_index (users, index);
622  if (strcmp (item->token, token) == 0)
623  {
624  if ((cookie == NULL) || strcmp (item->cookie, cookie))
625  {
626  /* Check if the session has expired. */
627  if (time (NULL) - item->time > (session_timeout * 60))
628  /* Probably the browser removed the cookie. */
629  ret = USER_EXPIRED_TOKEN;
630  else
632  break;
633  }
634  user = item;
635  break;
636  }
637  }
638  if (user)
639  {
640  /* Verify that the user address matches the client's address. */
641  if (strcmp (address, user->address))
643  else if (time (NULL) - user->time > (session_timeout * 60))
644  ret = USER_EXPIRED_TOKEN;
645  else
646  {
647  *user_return = user;
648  user->time = time (NULL);
649  /* FIXME mutex is not unlocked */
650  return USER_OK;
651  }
652  }
653  else if (ret == 0)
654  /* should it be really USER_EXPIRED_TOKEN?
655  * No user has been found therefore the token couldn't even expire */
656  ret = USER_EXPIRED_TOKEN;
657  g_mutex_unlock (mutex);
658  return ret;
659 }
char * address
Client's IP address.
Definition: gsad.c:428
int authenticate_omp(const gchar *username, const gchar *password, gchar **role, gchar **timezone, gchar **severity, gchar **capabilities, gchar **language, gchar **pw_warning, GTree **chart_prefs, gchar **autorefresh)
Check authentication credentials.
Definition: gsad_omp.c:27622
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, GTree *chart_prefs, const gchar *autorefresh, const char *address)
Add a user.
Definition: gsad.c:469
time_t time
Login time.
Definition: gsad.c:429
#define USER_GUEST_LOGIN_ERROR
Definition: gsad.c:521
#define USER_BAD_MISSING_COOKIE
Definition: gsad.c:516
gchar * guest_username
Guest username.
Definition: gsad.c:330
int session_timeout
Maximum number of minutes of user idle time.
Definition: gsad.c:325
#define USER_GUEST_LOGIN_FAILED
Definition: gsad.c:518
#define USER_IP_ADDRESS_MISSMATCH
Definition: gsad.c:520
gchar * guest_password
Guest password.
Definition: gsad.c:335
char * token
Request session token.
Definition: gsad.c:419
int guest
Whether the user is a guest.
Definition: gsad.c:434
#define USER_BAD_MISSING_TOKEN
Definition: gsad.c:517
GPtrArray * users
User session data.
Definition: gsad.c:340
#define USER_EXPIRED_TOKEN
Definition: gsad.c:515
User information structure, for sessions.
Definition: gsad.c:416
#define USER_OMP_DOWN
Definition: gsad.c:519
char * cookie
Cookie token.
Definition: gsad.c:418
#define USER_OK
Definition: gsad.c:513

References user::autorefresh, user::capabilities, user::chart_prefs, user::cookie, guest_username, user::language, user::pw_warning, user::role, user::severity, user::timezone, user::token, and USER_BAD_MISSING_TOKEN.

◆ user_logout_all_sessions()

int user_logout_all_sessions ( const gchar *  username,
credentials_t credentials 
)

Logs out all sessions of a given user, except the current one.

Parameters
[in]usernameUser name.
[in]credentialsCurrent user's credentials.
Returns
0 ok, -1 error.

Definition at line 882 of file gsad.c.

883 {
884  int index;
885  g_mutex_lock (mutex);
886  for (index = 0; index < users->len; index++)
887  {
888  user_t *item;
889  item = (user_t*) g_ptr_array_index (users, index);
890  if (strcmp (item->username, username) == 0
891  && strcmp (item->token, credentials->token))
892  {
893  g_debug ("%s: logging out user '%s', token '%s'",
894  __FUNCTION__, item->username, item->token);
895  g_ptr_array_remove (users, (gpointer) item);
896  index --;
897  }
898  }
899  g_mutex_unlock (mutex);
900 
901  return 0;
902 }
char * token
Request session token.
Definition: gsad.c:419
GPtrArray * users
User session data.
Definition: gsad.c:340
gchar * username
Login name.
Definition: gsad.c:420
char * token
Session token.
Definition: gsad_base.h:73
User information structure, for sessions.
Definition: gsad.c:416

◆ user_release()

void user_release ( user_t user)

Release a user_t returned by user_add or user_find.

Parameters
[in]userUser.

Definition at line 910 of file gsad.c.

911 {
912  g_mutex_unlock (mutex);
913 }

◆ user_remove()

void user_remove ( user_t user)

Remove a user from the session "database", releasing the user_t too.

Parameters
[in]userUser.

Definition at line 921 of file gsad.c.

922 {
923  g_ptr_array_remove (users, (gpointer) user);
924  g_mutex_unlock (mutex);
925 }
GPtrArray * users
User session data.
Definition: gsad.c:340
User information structure, for sessions.
Definition: gsad.c:416

References users.

◆ user_set_autorefresh()

int user_set_autorefresh ( const gchar *  token,
const gchar *  autorefresh 
)

Set default autorefresh interval of user.

Parameters
[in]tokenUser token.
[in]autorefreshAutorefresh interval.
Returns
0 ok, 1 failed to find user.

Definition at line 852 of file gsad.c.

853 {
854  int index, ret;
855  ret = 1;
856  g_mutex_lock (mutex);
857  for (index = 0; index < users->len; index++)
858  {
859  user_t *item;
860  item = (user_t*) g_ptr_array_index (users, index);
861  if (strcmp (item->token, token) == 0)
862  {
863  g_free (item->autorefresh);
864  item->autorefresh = g_strdup (autorefresh);
865  ret = 0;
866  break;
867  }
868  }
869  g_mutex_unlock (mutex);
870  return ret;
871 }
char * token
Request session token.
Definition: gsad.c:419
GPtrArray * users
User session data.
Definition: gsad.c:340
User information structure, for sessions.
Definition: gsad.c:416
gchar * autorefresh
Auto-Refresh interval.
Definition: gsad.c:432

◆ user_set_chart_pref()

int user_set_chart_pref ( const gchar *  token,
gchar *  pref_id,
gchar *  pref_value 
)

Set a chart preference of a user.

Parameters
[in]tokenUser token.
[in]pref_idID of the chart preference.
[in]pref_valuePreference value to set.
Returns
0 ok, 1 failed to find user.

Definition at line 822 of file gsad.c.

823 {
824  int index, ret;
825  ret = 1;
826  g_mutex_lock (mutex);
827  for (index = 0; index < users->len; index++)
828  {
829  user_t *item;
830  item = (user_t*) g_ptr_array_index (users, index);
831  if (strcmp (item->token, token) == 0)
832  {
833  g_tree_replace (item->chart_prefs,
834  pref_id, pref_value);
835  ret = 0;
836  break;
837  }
838  }
839  g_mutex_unlock (mutex);
840  return ret;
841 }
GTree * chart_prefs
Chart preferences.
Definition: gsad.c:431
char * token
Request session token.
Definition: gsad.c:419
GPtrArray * users
User session data.
Definition: gsad.c:340
User information structure, for sessions.
Definition: gsad.c:416

◆ user_set_charts()

int user_set_charts ( const gchar *  token,
const int  charts 
)

Set charts setting of user.

Parameters
[in]tokenUser token.
[in]chartsWhether to show charts.
Returns
0 ok, 1 failed to find user.

Definition at line 792 of file gsad.c.

793 {
794  int index, ret;
795  ret = 1;
796  g_mutex_lock (mutex);
797  for (index = 0; index < users->len; index++)
798  {
799  user_t *item;
800  item = (user_t*) g_ptr_array_index (users, index);
801  if (strcmp (item->token, token) == 0)
802  {
803  item->charts = charts;
804  ret = 0;
805  break;
806  }
807  }
808  g_mutex_unlock (mutex);
809  return ret;
810 }
int charts
Whether to show charts for this user.
Definition: gsad.c:430
char * token
Request session token.
Definition: gsad.c:419
GPtrArray * users
User session data.
Definition: gsad.c:340
User information structure, for sessions.
Definition: gsad.c:416

◆ user_set_language()

int user_set_language ( const gchar *  token,
const gchar *  language 
)

Set language of user.

Parameters
[in]tokenUser token.
[in]languageLanguage.
Returns
0 ok, 1 failed to find user.

Definition at line 762 of file gsad.c.

763 {
764  int index, ret;
765  ret = 1;
766  g_mutex_lock (mutex);
767  for (index = 0; index < users->len; index++)
768  {
769  user_t *item;
770  item = (user_t*) g_ptr_array_index (users, index);
771  if (strcmp (item->token, token) == 0)
772  {
773  g_free (item->language);
774  set_language_code (&item->language, language);
775  ret = 0;
776  break;
777  }
778  }
779  g_mutex_unlock (mutex);
780  return ret;
781 }
gchar * language
User Interface Language, in short form like "en".
Definition: gsad.c:426
void set_language_code(gchar **lang, const gchar *language)
Set language code of user.
Definition: gsad_base.c:218
char * token
Request session token.
Definition: gsad.c:419
GPtrArray * users
User session data.
Definition: gsad.c:340
User information structure, for sessions.
Definition: gsad.c:416

◆ user_set_password()

int user_set_password ( const gchar *  token,
const gchar *  password 
)

Set password of user.

Parameters
[in]tokenUser token.
[in]passwordPassword.
Returns
0 ok, 1 failed to find user.

Definition at line 700 of file gsad.c.

701 {
702  int index, ret;
703  ret = 1;
704  g_mutex_lock (mutex);
705  for (index = 0; index < users->len; index++)
706  {
707  user_t *item;
708  item = (user_t*) g_ptr_array_index (users, index);
709  if (strcmp (item->token, token) == 0)
710  {
711  g_free (item->password);
712  g_free (item->pw_warning);
713  item->password = g_strdup (password);
714  item->pw_warning = NULL;
715  ret = 0;
716  break;
717  }
718  }
719  g_mutex_unlock (mutex);
720  return ret;
721 }
gchar * pw_warning
Password policy warning.
Definition: gsad.c:427
char * token
Request session token.
Definition: gsad.c:419
GPtrArray * users
User session data.
Definition: gsad.c:340
User information structure, for sessions.
Definition: gsad.c:416
gchar * password
Password.
Definition: gsad.c:421

◆ user_set_severity()

int user_set_severity ( const gchar *  token,
const gchar *  severity 
)

Set severity class of user.

Parameters
[in]tokenUser token.
[in]severitySeverity class.
Returns
0 ok, 1 failed to find user.

Definition at line 732 of file gsad.c.

733 {
734  int index, ret;
735  ret = 1;
736  g_mutex_lock (mutex);
737  for (index = 0; index < users->len; index++)
738  {
739  user_t *item;
740  item = (user_t*) g_ptr_array_index (users, index);
741  if (strcmp (item->token, token) == 0)
742  {
743  g_free (item->severity);
744  item->severity = g_strdup (severity);
745  ret = 0;
746  break;
747  }
748  }
749  g_mutex_unlock (mutex);
750  return ret;
751 }
gchar * severity
Severity class.
Definition: gsad.c:424
char * token
Request session token.
Definition: gsad.c:419
GPtrArray * users
User session data.
Definition: gsad.c:340
User information structure, for sessions.
Definition: gsad.c:416

◆ user_set_timezone()

int user_set_timezone ( const gchar *  token,
const gchar *  timezone 
)

Set timezone of user.

Parameters
[in]tokenUser token.
[in]timezoneTimezone.
Returns
0 ok, 1 failed to find user.

Definition at line 670 of file gsad.c.

671 {
672  int index, ret;
673  ret = 1;
674  g_mutex_lock (mutex);
675  for (index = 0; index < users->len; index++)
676  {
677  user_t *item;
678  item = (user_t*) g_ptr_array_index (users, index);
679  if (strcmp (item->token, token) == 0)
680  {
681  g_free (item->timezone);
682  item->timezone = g_strdup (timezone);
683  ret = 0;
684  break;
685  }
686  }
687  g_mutex_unlock (mutex);
688  return ret;
689 }
char * token
Request session token.
Definition: gsad.c:419
gchar * timezone
Timezone.
Definition: gsad.c:423
GPtrArray * users
User session data.
Definition: gsad.c:340
User information structure, for sessions.
Definition: gsad.c:416

Variable Documentation

◆ address_list

GSList* address_list = NULL

The IP addresses of this program, "the GSAD".

Definition at line 281 of file gsad.c.

◆ BAD_REQUEST_PAGE

char* BAD_REQUEST_PAGE
Initial value:
=
"<html><body>Bad request.</body></html>"

Bad request error HTML.

Definition at line 243 of file gsad.c.

◆ chroot_state

int chroot_state = 0

Whether chroot is used.

Definition at line 375 of file gsad.c.

Referenced by get_chroot_state(), and set_chroot_state().

◆ client_watch_interval

int client_watch_interval = DEFAULT_CLIENT_WATCH_INTERVAL

Interval in seconds to check whether client connection was closed.

Definition at line 380 of file gsad.c.

◆ ERROR_PAGE

const char* ERROR_PAGE = "<html><body>HTTP Method not supported</body></html>"

Error page HTML.

Definition at line 238 of file gsad.c.

◆ GCRY_THREAD_OPTION_PTHREAD_IMPL

GCRY_THREAD_OPTION_PTHREAD_IMPL

Libgcrypt thread callback definition for libgcrypt < 1.6.0.

Definition at line 220 of file gsad.c.

◆ gsad_daemon

struct MHD_Daemon* gsad_daemon

The handle on the embedded HTTP daemon.

Definition at line 276 of file gsad.c.

◆ gsad_header_hosts

GHashTable* gsad_header_hosts = NULL

Host names and IP accepted in the "Host" HTTP header.

Definition at line 286 of file gsad.c.

◆ guest_password

gchar* guest_password = NULL

Guest password.

Definition at line 335 of file gsad.c.

◆ guest_username

gchar* guest_username = NULL

Guest username.

Definition at line 330 of file gsad.c.

Referenced by user_find().

◆ http_content_security_policy

gchar* http_content_security_policy

Current value for HTTP header "Content-Security-Policy".

Definition at line 350 of file gsad.c.

Referenced by add_guest_chart_content_security_headers(), and add_security_headers().

◆ http_guest_chart_content_security_policy

gchar* http_guest_chart_content_security_policy

Current guest chart value for HTTP header "Content-Security-Policy".

Definition at line 360 of file gsad.c.

Referenced by add_guest_chart_content_security_headers().

◆ http_guest_chart_x_frame_options

gchar* http_guest_chart_x_frame_options

Current guest chart specific value for HTTP header "X-Frame-Options".

Definition at line 355 of file gsad.c.

Referenced by add_guest_chart_content_security_headers().

◆ http_strict_transport_security

gchar* http_strict_transport_security

Current value of for HTTP header "Strict-Transport-Security".

Definition at line 365 of file gsad.c.

Referenced by add_security_headers().

◆ http_x_frame_options

gchar* http_x_frame_options

Current value for HTTP header "X-Frame-Options".

Definition at line 345 of file gsad.c.

Referenced by add_guest_chart_content_security_headers(), and add_security_headers().

◆ ignore_http_x_real_ip

gboolean ignore_http_x_real_ip

Current preference for using X_Real_IP from HTTP header.

Definition at line 370 of file gsad.c.

◆ log_config

GSList* log_config = NULL

Logging parameters, as passed to setup_log_handlers.

Todo:
Ensure the accesses to these are thread safe.

Definition at line 313 of file gsad.c.

◆ NOT_FOUND_MESSAGE

const char* NOT_FOUND_MESSAGE = "The requested page or file does not exist."

Main message for "Page not found" messages.

Definition at line 233 of file gsad.c.

◆ NOT_FOUND_TITLE

const char* NOT_FOUND_TITLE = "Invalid request"

Title for "Page not found" messages.

Definition at line 227 of file gsad.c.

◆ redirect_location

gchar* redirect_location = NULL

Location for redirection server.

Definition at line 291 of file gsad.c.

◆ redirect_pid

pid_t redirect_pid = 0

PID of redirect child in parent, 0 in child.

Definition at line 296 of file gsad.c.

◆ SERVER_ERROR

char* SERVER_ERROR
Initial value:
=
"<html><body>An internal server error has occurred.</body></html>"

Server error HTML.

Definition at line 249 of file gsad.c.

◆ session_timeout

int session_timeout

Maximum number of minutes of user idle time.

Definition at line 325 of file gsad.c.

◆ termination_signal

volatile int termination_signal = 0

Flag for signal handler.

Definition at line 214 of file gsad.c.

◆ unix_pid

pid_t unix_pid = 0

PID of unix socket child in parent, 0 in child.

Definition at line 301 of file gsad.c.

◆ unix_socket

int unix_socket = 0

Unix socket to listen on.

Definition at line 306 of file gsad.c.

◆ use_secure_cookie

int use_secure_cookie = 1

Whether to use a secure cookie.

This is always true when using HTTPS.

Definition at line 320 of file gsad.c.

◆ users

GPtrArray* users = NULL

User session data.

Definition at line 340 of file gsad.c.

Referenced by create_group_omp(), create_role_omp(), save_group_omp(), save_role_omp(), token_user_remove(), and user_remove().

◆ validator