Greenbone Security Assistant  8.0.0
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 
153 int
154 handler_send_not_found (http_connection_t *connection, const gchar *url);
155 
160 {
169 };
170 
172 
173 int
175  int http_status_code,
176  authentication_reason_t reason);
177 
178 int
179 send_response (http_connection_t *connection, const char *content,
180  int status_code, const gchar *sid, content_type_t content_type,
181  const char *content_disposition, size_t content_length);
182 
183 int
184 send_redirect_to_uri (http_connection_t *connection, const char *uri,
185  const gchar *sid);
186 
187 void
189 
190 void
192 
193 void
195 
196 void
198 
199 /* helper functions required in gsad_http */
201 file_content_response (http_connection_t *connection, const char *url,
202  const char *path, cmd_response_data_t *response_data);
203 
204 gchar *
205 reconstruct_url (http_connection_t *connection, const char *url);
206 
207 int
208 get_client_address (http_connection_t *conn, char *client_address);
209 
210 int
211 serve_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
212  const char *filename, const char *content_type,
213  const char *transfer_encoding, const char *data, uint64_t off,
214  size_t size);
215 
216 int
217 remove_sid (http_response_t *response);
218 
219 int
220 attach_sid (http_response_t *response, const char *sid);
221 
222 int
223 attach_remove_sid (http_response_t *response, const gchar *sid);
224 
225 /* params_append_mhd, exec_gmp_... are still in gsad.c */
226 int
229 
230 int
232  const char *client_address);
233 
234 int
235 params_append_mhd (params_t *params, const char *name, const char *filename,
236  const char *chunk_data, int chunk_size, int chunk_offset);
237 
238 char *
239 gsad_message (credentials_t *, const char *, const char *, int, const char *,
241 
242 #endif /* _GSAD_HTTP_H */
content_type_t guess_content_type(const gchar *path)
Guess a content type from a file extension.
Definition: gsad_http.c:56
GSAD credentials handling.
Structure of credential related information.
Headers for Response Data struct.
params_t * params
Request parameters.
Definition: gsad_http.h:128
int attach_sid(http_response_t *response, const char *sid)
Attach SID cookie to a response, resetting "expire" arg.
Definition: gsad_http.c:558
int handler_send_not_found(http_connection_t *connection, const gchar *url)
Send a 404 response for a request.
Definition: gsad_http.c:438
int remove_sid(http_response_t *response)
Attach expired SID cookie to response.
Definition: gsad_http.c:512
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:850
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
int connectiontype
1=POST, 2=GET.
Definition: gsad_http.h:131
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
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:694
struct MHD_Connection http_connection_t
Definition: gsad_http.h:135
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
int exec_gmp_get(http_connection_t *connection, gsad_connection_info_t *con_info, credentials_t *credentials)
Response information for commands.
Definition: gsad_cmd.c:33
enum authentication_reason authentication_reason_t
Definition: gsad_http.h:171
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.
Definition: gsad_http.c:978
gchar * redirect
Redirect URL.
Definition: gsad_http.h:132
Connection information.
Definition: gsad_http.h:125
authentication_reason
Content types.
Definition: gsad_http.h:159
enum content_type content_type_t
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
gchar * reconstruct_url(http_connection_t *connection, const char *url)
Reconstruct the URL for a connection.
Definition: gsad_http.c:806
struct MHD_PostProcessor * postprocessor
POST processor.
Definition: gsad_http.h:127
int exec_gmp_post(http_connection_t *connection, gsad_connection_info_t *con_info, const char *client_address)
char * language
Language code e.g. en.
Definition: gsad_http.h:130
int get_client_address(http_connection_t *conn, char *client_address)
Get the client's address.
Definition: gsad_http.c:899
GSAD user handling.
void add_security_headers(http_response_t *response)
Add security headers to a MHD response.
Definition: gsad_http.c:828
struct gsad_connection_info gsad_connection_info_t
Connection information.
struct MHD_Response http_response_t
Definition: gsad_http.h:137
Headers for content type.
#define params_t
Definition: gsad_params.h:31
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:948
void add_forbid_caching_headers(http_response_t *response)
Add header to forbid caching to a HTTP response.
Definition: gsad_http.c:881
char * cookie
Value of SID cookie param.
Definition: gsad_http.h:129
void add_cors_headers(http_response_t *response)
Definition: gsad_http.c:861
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:459
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
content_type
Content types.
int attach_remove_sid(http_response_t *response, const gchar *sid)
Definition: gsad_http.c:636