Greenbone Security Assistant  9.0.1
gsad_http.h
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 #ifndef _GSAD_HTTP_H
26 #define _GSAD_HTTP_H
27 
28 #include "gsad_cmd.h" /* for cmd_response_data_t */
29 #include "gsad_content_type.h" /* for content_type_t */
30 #include "gsad_credentials.h" /* for credentials_t */
31 #include "gsad_user.h" /* for user_t */
32 
33 #include <glib.h>
34 #include <microhttpd.h>
35 
39 #define DATE_2822_LEN 100
40 
44 #define EXPIRES_LENGTH 100
45 
46 /*
47  * UTF-8 Error page HTML.
48  */
49 #define UTF8_ERROR_PAGE(location) \
50  "<html>" \
51  "<head><title>Invalid request</title></head>" \
52  "<body>The request contained invalid UTF-8 in " location ".</body>" \
53  "</html>"
54 
58 #define SID_COOKIE_NAME "GSAD_SID"
59 
60 #define REMOVE_SID "0"
61 
65 #define NOT_FOUND_TITLE "Invalid request"
66 
70 #define NOT_FOUND_MESSAGE "The requested page or file does not exist."
71 
75 #define ERROR_PAGE "<html><body>HTTP Method not supported</body></html>"
76 
80 #define BAD_REQUEST_PAGE "<html><body>Bad request.</body></html>"
81 
85 #define SERVER_ERROR \
86  "<html><body>An internal server error has occurred.</body></html>"
87 
88 #undef MAX_HOST_LEN
89 
93 #define MAX_HOST_LEN 1000
94 
95 #define LOGIN_URL "/login"
96 #define LOGOUT_URL "/logout"
97 
101 #define POST_BUFFER_SIZE 500000
102 
107 #ifndef MHD_HTTP_NOT_ACCEPTABLE
108 #define MHD_HTTP_NOT_ACCEPTABLE MHD_HTTP_METHOD_NOT_ACCEPTABLE
109 #endif
110 
114 #define MAX_FILE_NAME_SIZE 128
115 
125 typedef struct gsad_connection_info
126 {
127  struct MHD_PostProcessor *postprocessor;
129  char *cookie;
130  char *language;
132  gchar *redirect;
134 
135 typedef struct MHD_Connection http_connection_t;
136 
137 typedef struct MHD_Response http_response_t;
138 
140 guess_content_type (const gchar *path);
141 
142 void
144 
145 int
146 handler_create_response (http_connection_t *connection, gchar *data,
147  cmd_response_data_t *response_data, const gchar *sid);
148 
149 int
151  cmd_response_data_t *response_data, const gchar *sid);
152 
157 {
167 };
168 
170 
171 int
173  int http_status_code,
174  authentication_reason_t reason);
175 
176 int
177 send_response (http_connection_t *connection, const char *content,
178  int status_code, const gchar *sid, content_type_t content_type,
179  const char *content_disposition, size_t content_length);
180 
181 int
182 send_redirect_to_uri (http_connection_t *connection, const char *uri,
183  const gchar *sid);
184 
185 void
187 
188 void
190 
191 void
193 
194 void
196 
197 /* helper functions required in gsad_http */
199 file_content_response (http_connection_t *connection, const char *url,
200  const char *path, cmd_response_data_t *response_data);
201 
202 gchar *
203 reconstruct_url (http_connection_t *connection, const char *url);
204 
205 int
206 get_client_address (http_connection_t *conn, char *client_address);
207 
208 int
209 serve_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
210  const char *filename, const char *content_type,
211  const char *transfer_encoding, const char *data, uint64_t off,
212  size_t size);
213 
214 int
215 remove_sid (http_response_t *response);
216 
217 int
218 attach_sid (http_response_t *response, const char *sid);
219 
220 int
221 attach_remove_sid (http_response_t *response, const gchar *sid);
222 
223 /* params_append_mhd, exec_gmp_... are still in gsad.c */
224 int
227 
228 int
230  const char *client_address);
231 
232 int
233 params_append_mhd (params_t *params, const char *name, const char *filename,
234  const char *chunk_data, int chunk_size, int chunk_offset);
235 
236 char *
237 gsad_message (credentials_t *, const char *, const char *, int, const char *,
239 
240 #endif /* _GSAD_HTTP_H */
gsad_connection_info::cookie
char * cookie
Value of SID cookie param.
Definition: gsad_http.h:129
gsad_connection_info::params
params_t * params
Request parameters.
Definition: gsad_http.h:128
gsad_connection_info_t
struct gsad_connection_info gsad_connection_info_t
Connection information.
gsad_cmd.h
Headers for Response Data struct.
cmd_response_data
Response information for commands.
Definition: gsad_cmd.c:33
params_t
#define params_t
Definition: gsad_params.h:31
gsad_message
char * gsad_message(credentials_t *, const char *, const char *, int, const char *, cmd_response_data_t *)
Handles fatal errors.
Definition: gsad_http.c:957
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.
Definition: gsad_http.c:927
content_type
content_type
Content types.
Definition: gsad_content_type.h:31
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.
Definition: gsad_http.c:183
UNKOWN_ERROR
@ UNKOWN_ERROR
Definition: gsad_http.h:166
guess_content_type
content_type_t guess_content_type(const gchar *path)
Guess a content type from a file extension.
Definition: gsad_http.c:56
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.
Definition: gsad_http.c:673
credentials
Structure of credential related information.
Definition: gsad_credentials.c:34
gsad_credentials.h
GSAD credentials handling.
GMP_SERVICE_DOWN
@ GMP_SERVICE_DOWN
Definition: gsad_http.h:162
gsad_connection_info::connectiontype
int connectiontype
1=POST, 2=GET.
Definition: gsad_http.h:131
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.
Definition: gsad_http.c:89
exec_gmp_post
int exec_gmp_post(http_connection_t *connection, gsad_connection_info_t *con_info, const char *client_address)
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.
Definition: gsad_http.c:829
content_type_t
enum content_type content_type_t
Definition: gsad_content_type.h:54
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.
Definition: gsad_http.c:244
exec_gmp_get
int exec_gmp_get(http_connection_t *connection, gsad_connection_info_t *con_info, credentials_t *credentials)
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.
Definition: gsad_http.c:368
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)
gsad_content_type.h
Headers for content type.
http_connection_t
struct MHD_Connection http_connection_t
Definition: gsad_http.h:135
gsad_user.h
GSAD user handling.
gsad_connection_info::language
char * language
Language code e.g. en.
Definition: gsad_http.h:130
gsad_connection_info::postprocessor
struct MHD_PostProcessor * postprocessor
POST processor.
Definition: gsad_http.h:127
gsad_connection_info::redirect
gchar * redirect
Redirect URL.
Definition: gsad_http.h:132
BAD_MISSING_COOKIE
@ BAD_MISSING_COOKIE
Definition: gsad_http.h:164
authentication_reason_t
enum authentication_reason authentication_reason_t
Definition: gsad_http.h:169
remove_sid
int remove_sid(http_response_t *response)
Attach expired SID cookie to response.
Definition: gsad_http.c:491
reconstruct_url
gchar * reconstruct_url(http_connection_t *connection, const char *url)
Reconstruct the URL for a connection.
Definition: gsad_http.c:785
BAD_MISSING_TOKEN
@ BAD_MISSING_TOKEN
Definition: gsad_http.h:165
LOGOUT_ALREADY
@ LOGOUT_ALREADY
Definition: gsad_http.h:161
SESSION_EXPIRED
@ SESSION_EXPIRED
Definition: gsad_http.h:163
attach_remove_sid
int attach_remove_sid(http_response_t *response, const gchar *sid)
Definition: gsad_http.c:615
http_response_t
struct MHD_Response http_response_t
Definition: gsad_http.h:137
add_forbid_caching_headers
void add_forbid_caching_headers(http_response_t *response)
Add header to forbid caching to a HTTP response.
Definition: gsad_http.c:860
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.
Definition: gsad_http.c:295
LOGOUT
@ LOGOUT
Definition: gsad_http.h:160
LOGIN_ERROR
@ LOGIN_ERROR
Definition: gsad_http.h:159
authentication_reason
authentication_reason
Content types.
Definition: gsad_http.h:156
attach_sid
int attach_sid(http_response_t *response, const char *sid)
Attach SID cookie to a response, resetting "expire" arg.
Definition: gsad_http.c:537
get_client_address
int get_client_address(http_connection_t *conn, char *client_address)
Get the client's address.
Definition: gsad_http.c:878
add_security_headers
void add_security_headers(http_response_t *response)
Add security headers to a MHD response.
Definition: gsad_http.c:807
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.
Definition: gsad_http.c:435
gsad_connection_info
Connection information.
Definition: gsad_http.h:125
LOGIN_FAILED
@ LOGIN_FAILED
Definition: gsad_http.h:158
add_cors_headers
void add_cors_headers(http_response_t *response)
Definition: gsad_http.c:840