Greenbone Security Assistant
Data Structures | Macros | Typedefs | Enumerations | Functions
gsad_http.h File Reference

HTTP handling of GSA. More...

#include "gsad_cmd.h"
#include "gsad_content_type.h"
#include "gsad_credentials.h"
#include "gsad_user.h"
#include <glib.h>
#include <microhttpd.h>

Go to the source code of this file.

Data Structures

struct  gsad_connection_info
 Connection information. More...
 

Macros

#define DATE_2822_LEN   100
 At least maximum length of rfc2822 format date. More...
 
#define EXPIRES_LENGTH   100
 Max length of cookie expires param. More...
 
#define UTF8_ERROR_PAGE(location)
 
#define SID_COOKIE_NAME   "GSAD_SID"
 Name of the cookie used to store the SID. More...
 
#define REMOVE_SID   "0"
 
#define NOT_FOUND_TITLE   "Invalid request"
 Title for "Page not found" messages. More...
 
#define NOT_FOUND_MESSAGE   "The requested page or file does not exist."
 Main message for "Page not found" messages. More...
 
#define ERROR_PAGE   "<html><body>HTTP Method not supported</body></html>"
 Error page HTML. More...
 
#define BAD_REQUEST_PAGE   "<html><body>Bad request.</body></html>"
 Bad request error HTML. More...
 
#define SERVER_ERROR    "<html><body>An internal server error has occurred.</body></html>"
 Server error HTML. More...
 
#define MAX_HOST_LEN   1000
 Maximum length of the host portion of the redirect address. More...
 
#define LOGIN_URL   "/login"
 
#define LOGOUT_URL   "/logout"
 
#define POST_BUFFER_SIZE   500000
 Buffer size for POST processor. More...
 
#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 MAX_FILE_NAME_SIZE   128
 Maximum length of "file name" for /help/ URLs. More...
 

Typedefs

typedef struct gsad_connection_info gsad_connection_info_t
 Connection information. More...
 
typedef struct MHD_Connection http_connection_t
 
typedef struct MHD_Response http_response_t
 
typedef enum authentication_reason authentication_reason_t
 

Enumerations

enum  authentication_reason {
  LOGIN_FAILED , LOGIN_ERROR , LOGOUT , LOGOUT_ALREADY ,
  GMP_SERVICE_DOWN , SESSION_EXPIRED , BAD_MISSING_COOKIE , BAD_MISSING_TOKEN ,
  UNKOWN_ERROR
}
 Content types. More...
 

Functions

content_type_t guess_content_type (const gchar *path)
 Guess a content type from a file extension. More...
 
void gsad_add_content_type_header (http_response_t *response, content_type_t *ct)
 Adds content-type header fields to a response. More...
 
int handler_create_response (http_connection_t *connection, gchar *data, cmd_response_data_t *response_data, const gchar *sid)
 Create and send a response. More...
 
int handler_send_response (http_connection_t *connection, http_response_t *response, cmd_response_data_t *response_data, const gchar *sid)
 Send response for handle_request. More...
 
int handler_send_reauthentication (http_connection_t *connection, int http_status_code, authentication_reason_t reason)
 Allow for reauthentication of a user. More...
 
int send_response (http_connection_t *connection, const char *content, int status_code, const gchar *sid, content_type_t content_type, const char *content_disposition, size_t content_length)
 Sends a HTTP response. More...
 
int send_redirect_to_uri (http_connection_t *connection, const char *uri, const gchar *sid)
 Sends a HTTP redirection to an uri. More...
 
void add_security_headers (http_response_t *response)
 Add security headers to a MHD response. More...
 
void add_guest_chart_content_security_headers (http_response_t *response)
 Add guest chart content security headers to a MHD response. More...
 
void add_cors_headers (http_response_t *response)
 
void add_forbid_caching_headers (http_response_t *response)
 Add header to forbid caching to a HTTP response. More...
 
http_response_tfile_content_response (http_connection_t *connection, const char *url, const char *path, cmd_response_data_t *response_data)
 Create a response to serve a file from a path. More...
 
gchar * reconstruct_url (http_connection_t *connection, const char *url)
 Reconstruct the URL for a connection. More...
 
int get_client_address (http_connection_t *conn, char *client_address)
 Get the client's address. More...
 
int serve_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, const char *filename, const char *content_type, const char *transfer_encoding, const char *data, uint64_t off, size_t size)
 Serves part of a POST request. More...
 
int remove_sid (http_response_t *response)
 Attach expired SID cookie to response. More...
 
int attach_sid (http_response_t *response, const char *sid)
 Attach SID cookie to a response, resetting "expire" arg. More...
 
int attach_remove_sid (http_response_t *response, const gchar *sid)
 
int exec_gmp_get (http_connection_t *connection, gsad_connection_info_t *con_info, credentials_t *credentials)
 
int exec_gmp_post (http_connection_t *connection, gsad_connection_info_t *con_info, const char *client_address)
 
int params_append_mhd (params_t *params, const char *name, const char *filename, const char *chunk_data, int chunk_size, int chunk_offset)
 
char * gsad_message (credentials_t *, const char *, const char *, int, const char *, cmd_response_data_t *)
 Handles fatal errors. More...
 

Detailed Description

HTTP handling of GSA.

Macro Definition Documentation

◆ BAD_REQUEST_PAGE

#define BAD_REQUEST_PAGE   "<html><body>Bad request.</body></html>"

Bad request error HTML.

◆ DATE_2822_LEN

#define DATE_2822_LEN   100

At least maximum length of rfc2822 format date.

◆ ERROR_PAGE

#define ERROR_PAGE   "<html><body>HTTP Method not supported</body></html>"

Error page HTML.

◆ EXPIRES_LENGTH

#define EXPIRES_LENGTH   100

Max length of cookie expires param.

◆ LOGIN_URL

#define LOGIN_URL   "/login"

◆ LOGOUT_URL

#define LOGOUT_URL   "/logout"

◆ MAX_FILE_NAME_SIZE

#define MAX_FILE_NAME_SIZE   128

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

◆ MAX_HOST_LEN

#define MAX_HOST_LEN   1000

Maximum length of the host portion of the redirect address.

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

◆ NOT_FOUND_MESSAGE

#define NOT_FOUND_MESSAGE   "The requested page or file does not exist."

Main message for "Page not found" messages.

◆ NOT_FOUND_TITLE

#define NOT_FOUND_TITLE   "Invalid request"

Title for "Page not found" messages.

◆ POST_BUFFER_SIZE

#define POST_BUFFER_SIZE   500000

Buffer size for POST processor.

◆ REMOVE_SID

#define REMOVE_SID   "0"

◆ SERVER_ERROR

#define SERVER_ERROR    "<html><body>An internal server error has occurred.</body></html>"

Server error HTML.

◆ SID_COOKIE_NAME

#define SID_COOKIE_NAME   "GSAD_SID"

Name of the cookie used to store the SID.

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

Typedef Documentation

◆ authentication_reason_t

◆ gsad_connection_info_t

Connection information.

These objects are used to hold connection information during the multiple calls of the request handler that refer to the same request.

Once a request is finished, the object will be free'd.

◆ http_connection_t

typedef struct MHD_Connection http_connection_t

◆ http_response_t

typedef struct MHD_Response http_response_t

Enumeration Type Documentation

◆ authentication_reason

Content types.

Enumerator
LOGIN_FAILED 
LOGIN_ERROR 
LOGOUT 
LOGOUT_ALREADY 
GMP_SERVICE_DOWN 
SESSION_EXPIRED 
BAD_MISSING_COOKIE 
BAD_MISSING_TOKEN 
UNKOWN_ERROR 

Function Documentation

◆ add_cors_headers()

void add_cors_headers ( http_response_t response)

◆ add_forbid_caching_headers()

void add_forbid_caching_headers ( http_response_t response)

Add header to forbid caching to a HTTP response.

Parameters
[in]responseThe HTTP response to add the headers to.
[in]allow_caching1 to allow caching, 0 to forbid.

◆ add_guest_chart_content_security_headers()

void add_guest_chart_content_security_headers ( http_response_t response)

Add guest chart content security headers to a MHD response.

◆ add_security_headers()

void add_security_headers ( http_response_t response)

Add security headers to a MHD response.

◆ attach_remove_sid()

int attach_remove_sid ( http_response_t response,
const gchar *  sid 
)

Attach or remove session id

If sid is "0" the session id will be removed. Otherwise if the sid is not NULL the sid will be attached to the response.

Parameters
[in]responseHTTP response
[in]sidSession ID
Returns
MHD_YES on success, MHD_NO on failure

◆ attach_sid()

int attach_sid ( http_response_t response,
const char *  sid 
)

Attach SID cookie to a response, resetting "expire" arg.

Parameters
[in]responseResponse.
[in]sidSession ID.
Returns
MHD_NO in case of problems. MHD_YES if all is OK.

◆ exec_gmp_get()

int exec_gmp_get ( http_connection_t connection,
gsad_connection_info_t con_info,
credentials_t credentials 
)

◆ exec_gmp_post()

int exec_gmp_post ( http_connection_t connection,
gsad_connection_info_t con_info,
const char *  client_address 
)

◆ file_content_response()

http_response_t* file_content_response ( http_connection_t connection,
const char *  url,
const char *  path,
cmd_response_data_t response_data 
)

Create a response to serve a file from a path.

Parameters
[in]connectionConnection.
[in]urlRequested URL.
[in]pathPath to file.
[out]response_dataReturn response data
Returns
Response to send in combination with the response code. NULL only if file information could not be retrieved.

◆ get_client_address()

int get_client_address ( http_connection_t conn,
char *  client_address 
)

Get the client's address.

Parameters
[in]connConnection.
[out]client_addressBuffer to store client address. Must have at least INET6_ADDRSTRLEN bytes.
Returns
0 success, 1 invalid UTF-8 in X-Real-IP header

◆ gsad_add_content_type_header()

void gsad_add_content_type_header ( http_response_t response,
content_type_t ct 
)

Adds content-type header fields to a response.

This function should be called only once per response and is the only function where values of enum content_types are translated into strings.

Parameters
[in,out]responseResponse to add header to.
[in]ctContent Type to set.

◆ gsad_message()

char* gsad_message ( credentials_t credentials,
const char *  title,
const char *  function,
int  line,
const char *  msg,
cmd_response_data_t response_data 
)

Handles fatal errors.

Todo:
Make it accept formatted strings.
Parameters
[in]credentialsUser authentication information.
[in]titleThe title for the message.
[in]functionThe function in which the error occurred.
[in]lineThe line number at which the error occurred.
[in]msgThe response message.
[out]response_dataExtra data return for the HTTP response.
Returns
An XML document as a newly allocated string.

◆ guess_content_type()

content_type_t guess_content_type ( const gchar *  path)

Guess a content type from a file extension.

Parameters
[in]pathfilename with extension
Returns
a content_type_t for the file

◆ handler_create_response()

int handler_create_response ( http_connection_t connection,
gchar *  data,
cmd_response_data_t response_data,
const gchar *  sid 
)

Create and send a response.

The passed response data will be freed and can't be used afterwards

Parameters
[in]connectionConnection handle, e.g. used to send response.
[in]dataData to send in response
[in]response_dataResponse data struct. Response data will be freed.
[in]sidSession ID, or NULL. "0" to remove session.
Returns
MHD_YES on success, else MHD_NO.

◆ handler_send_reauthentication()

int handler_send_reauthentication ( http_connection_t connection,
int  http_status_code,
authentication_reason_t  reason 
)

Allow for reauthentication of a user.

Parameters
[in]connectionConnection handle, e.g. used to send response.
[in]http_status_codeHTTP status code for the response.
[in]reasonReason for re-authentication
Returns
MHD_YES on success. MHD_NO on errors.

◆ handler_send_response()

int handler_send_response ( http_connection_t connection,
http_response_t response,
cmd_response_data_t response_data,
const gchar *  sid 
)

Send response for handle_request.

The passed response data will be freed and can't be used afterwards

Parameters
[in]connectionConnection handle, e.g. used to send response.
[in]responseResponse.
[in]response_dataResponse data struct. Response data will be freed.
[in]sidSession ID, or NULL. "0" to remove session.
Returns
MHD_YES on success, else MHD_NO.

◆ params_append_mhd()

int params_append_mhd ( params_t params,
const char *  name,
const char *  filename,
const char *  chunk_data,
int  chunk_size,
int  chunk_offset 
)

◆ reconstruct_url()

gchar* reconstruct_url ( http_connection_t connection,
const char *  url 
)

Reconstruct the URL for a connection.

Parameters
[in]connectionConnection.
[in]urlBase part of URL.
Returns
URL.

◆ remove_sid()

int remove_sid ( http_response_t response)

Attach expired SID cookie to response.

Parameters
[in]responseResponse.
Returns
MHD_NO in case of problems. MHD_YES if all is OK.

◆ send_redirect_to_uri()

int send_redirect_to_uri ( http_connection_t connection,
const char *  uri,
const gchar *  sid 
)

Sends a HTTP redirection to an uri.

Parameters
[in]connectionThe connection handle.
[in]uriThe full URI to redirect to.
[in]sidSession ID to add, or NULL.
Returns
MHD_NO in case of a problem. Else MHD_YES.

◆ send_response()

int send_response ( http_connection_t connection,
const char *  content,
int  status_code,
const gchar *  sid,
content_type_t  content_type,
const char *  content_disposition,
size_t  content_length 
)

Sends a HTTP response.

Parameters
[in]connectionThe connection handle.
[in]contentThe content.
[in]status_codeThe HTTP status code.
[in]sidSession ID, or NULL.
[in]content_typeThe content type.
[in]content_dispositionThe content disposition or NULL.
[in]content_lengthContent length, 0 for strlen (content).
Returns
MHD_YES on success, MHD_NO on error.

◆ serve_post()

int serve_post ( void *  coninfo_cls,
enum MHD_ValueKind  kind,
const char *  key,
const char *  filename,
const char *  content_type,
const char *  transfer_encoding,
const char *  data,
uint64_t  off,
size_t  size 
)

Serves part of a POST request.

Implements an MHD_PostDataIterator.

Called one or more times to collect the multiple parts (key/value pairs) of a POST request. Fills the params of a gsad_connection_info.

After serve_post, the connection info is free'd.

Parameters
[in,out]coninfo_clsConnection info (a gsad_connection_info).
[in]kindType of request data (header, cookie, etc.).
[in]keyName of data (name of request variable).
[in]filenameName of uploaded file if any, else NULL.
[in]content_typeMIME type of data if known, else NULL.
[in]transfer_encodingTransfer encoding if known, else NULL.
[in]dataData.
[in]offOffset into entire data.
[in]sizeSize of data, in bytes.
Returns
MHD_YES to continue iterating over post data, MHD_NO to stop.