Greenbone Security Assistant
gsad_http.h
Go to the documentation of this file.
1 /* Copyright (C) 2016-2021 Greenbone Networks GmbH
2  *
3  * SPDX-License-Identifier: AGPL-3.0-or-later
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as
7  * published by the Free Software Foundation, either version 3 of the
8  * 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 Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
24 #ifndef _GSAD_HTTP_H
25 #define _GSAD_HTTP_H
26 
27 #include "gsad_cmd.h" /* for cmd_response_data_t */
28 #include "gsad_content_type.h" /* for content_type_t */
29 #include "gsad_credentials.h" /* for credentials_t */
30 #include "gsad_user.h" /* for user_t */
31 
32 #include <glib.h>
33 #include <microhttpd.h>
34 
38 #define DATE_2822_LEN 100
39 
43 #define EXPIRES_LENGTH 100
44 
45 /*
46  * UTF-8 Error page HTML.
47  */
48 #define UTF8_ERROR_PAGE(location) \
49  "<html>" \
50  "<head><title>Invalid request</title></head>" \
51  "<body>The request contained invalid UTF-8 in " location ".</body>" \
52  "</html>"
53 
57 #define SID_COOKIE_NAME "GSAD_SID"
58 
59 #define REMOVE_SID "0"
60 
64 #define NOT_FOUND_TITLE "Invalid request"
65 
69 #define NOT_FOUND_MESSAGE "The requested page or file does not exist."
70 
74 #define ERROR_PAGE "<html><body>HTTP Method not supported</body></html>"
75 
79 #define BAD_REQUEST_PAGE "<html><body>Bad request.</body></html>"
80 
84 #define SERVER_ERROR \
85  "<html><body>An internal server error has occurred.</body></html>"
86 
87 #undef MAX_HOST_LEN
88 
92 #define MAX_HOST_LEN 1000
93 
94 #define LOGIN_URL "/login"
95 #define LOGOUT_URL "/logout"
96 
100 #define POST_BUFFER_SIZE 500000
101 
106 #ifndef MHD_HTTP_NOT_ACCEPTABLE
107 #define MHD_HTTP_NOT_ACCEPTABLE MHD_HTTP_METHOD_NOT_ACCEPTABLE
108 #endif
109 
113 #define MAX_FILE_NAME_SIZE 128
114 
124 typedef struct gsad_connection_info
125 {
126  struct MHD_PostProcessor *postprocessor;
128  char *cookie;
129  char *language;
131  gchar *redirect;
133 
134 typedef struct MHD_Connection http_connection_t;
135 
136 typedef struct MHD_Response http_response_t;
137 
139 guess_content_type (const gchar *path);
140 
141 void
143 
144 int
145 handler_create_response (http_connection_t *connection, gchar *data,
146  cmd_response_data_t *response_data, const gchar *sid);
147 
148 int
150  cmd_response_data_t *response_data, const gchar *sid);
151 
156 {
166 };
167 
169 
170 int
172  int http_status_code,
173  authentication_reason_t reason);
174 
175 int
176 send_response (http_connection_t *connection, const char *content,
177  int status_code, const gchar *sid, content_type_t content_type,
178  const char *content_disposition, size_t content_length);
179 
180 int
181 send_redirect_to_uri (http_connection_t *connection, const char *uri,
182  const gchar *sid);
183 
184 void
186 
187 void
189 
190 void
192 
193 void
195 
196 /* helper functions required in gsad_http */
198 file_content_response (http_connection_t *connection, const char *url,
199  const char *path, cmd_response_data_t *response_data);
200 
201 gchar *
202 reconstruct_url (http_connection_t *connection, const char *url);
203 
204 int
205 get_client_address (http_connection_t *conn, char *client_address);
206 
207 #if MHD_VERSION < 0x00097002
208 int
209 #else
210 enum MHD_Result
211 #endif
212 serve_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
213  const char *filename, const char *content_type,
214  const char *transfer_encoding, const char *data, uint64_t off,
215  size_t size);
216 
217 int
218 remove_sid (http_response_t *response);
219 
220 int
221 attach_sid (http_response_t *response, const char *sid);
222 
223 int
224 attach_remove_sid (http_response_t *response, const gchar *sid);
225 
226 /* params_append_mhd, exec_gmp_... are still in gsad.c */
227 int
230 
231 int
233  const char *client_address);
234 
235 int
236 params_append_mhd (params_t *params, const char *name, const char *filename,
237  const char *chunk_data, int chunk_size, int chunk_offset);
238 
239 char *
240 gsad_message (credentials_t *, const char *, const char *, int, const char *,
242 
243 #endif /* _GSAD_HTTP_H */
Headers for Response Data struct.
Headers for content type.
content_type
Content types.
Definition: gsad_content_type.h:31
enum content_type content_type_t
Definition: gsad_content_type.h:53
GSAD credentials handling.
int attach_sid(http_response_t *response, const char *sid)
Attach SID cookie to a response, resetting "expire" arg.
Definition: gsad_http.c:535
struct gsad_connection_info gsad_connection_info_t
Connection information.
struct MHD_Response http_response_t
Definition: gsad_http.h:136
struct MHD_Connection http_connection_t
Definition: gsad_http.h:134
void add_forbid_caching_headers(http_response_t *response)
Add header to forbid caching to a HTTP response.
Definition: gsad_http.c:863
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:433
content_type_t guess_content_type(const gchar *path)
Guess a content type from a file extension.
Definition: gsad_http.c:55
int exec_gmp_post(http_connection_t *connection, gsad_connection_info_t *con_info, const char *client_address)
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:934
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:182
gchar * reconstruct_url(http_connection_t *connection, const char *url)
Reconstruct the URL for a connection.
Definition: gsad_http.c:788
int exec_gmp_get(http_connection_t *connection, gsad_connection_info_t *con_info, credentials_t *credentials)
enum authentication_reason authentication_reason_t
Definition: gsad_http.h:168
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:293
int attach_remove_sid(http_response_t *response, const gchar *sid)
Definition: gsad_http.c:614
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:366
int remove_sid(http_response_t *response)
Attach expired SID cookie to response.
Definition: gsad_http.c:489
authentication_reason
Content types.
Definition: gsad_http.h:156
@ GMP_SERVICE_DOWN
Definition: gsad_http.h:161
@ LOGOUT_ALREADY
Definition: gsad_http.h:160
@ BAD_MISSING_TOKEN
Definition: gsad_http.h:164
@ LOGOUT
Definition: gsad_http.h:159
@ BAD_MISSING_COOKIE
Definition: gsad_http.h:163
@ UNKOWN_ERROR
Definition: gsad_http.h:165
@ LOGIN_ERROR
Definition: gsad_http.h:158
@ LOGIN_FAILED
Definition: gsad_http.h:157
@ SESSION_EXPIRED
Definition: gsad_http.h:162
int params_append_mhd(params_t *params, const char *name, const char *filename, const char *chunk_data, int chunk_size, int chunk_offset)
void add_cors_headers(http_response_t *response)
Definition: gsad_http.c:843
int get_client_address(http_connection_t *conn, char *client_address)
Get the client's address.
Definition: gsad_http.c:881
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:832
char * gsad_message(credentials_t *, const char *, const char *, int, const char *, cmd_response_data_t *)
Handles fatal errors.
Definition: gsad_http.c:964
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:242
void add_security_headers(http_response_t *response)
Add security headers to a MHD response.
Definition: gsad_http.c:810
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:672
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:88
#define params_t
Definition: gsad_params.h:30
GSAD user handling.
Response information for commands.
Definition: gsad_cmd.c:33
Structure of credential related information.
Definition: gsad_credentials.c:34
Connection information.
Definition: gsad_http.h:125
int connectiontype
1=POST, 2=GET.
Definition: gsad_http.h:130
char * cookie
Value of SID cookie param.
Definition: gsad_http.h:128
params_t * params
Request parameters.
Definition: gsad_http.h:127
struct MHD_PostProcessor * postprocessor
POST processor.
Definition: gsad_http.h:126
char * language
Language code e.g. en.
Definition: gsad_http.h:129
gchar * redirect
Redirect URL.
Definition: gsad_http.h:131