Greenbone Vulnerability Manager  22.4.0~dev1
gmp_base.h
1 /* Copyright (C) 2018-2022 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 
19 #ifndef _GVMD_GMP_BASE_H
20 #define _GVMD_GMP_BASE_H
21 
22 #include <glib.h>
23 
27 typedef struct
28 {
29  int (*client_writer) (const char *, void *);
31  int importing;
32  int read_over;
35 } gmp_parser_t;
36 
37 int
38 find_attribute (const gchar **, const gchar **, const char *, const gchar **);
39 
40 int
41 append_attribute (const gchar **, const gchar **, const char *, gchar **);
42 
43 void
44 buffer_xml_append_printf (GString *, const char *, ...);
45 
46 gboolean
47 send_to_client (const char *, int (*) (const char *, void *), void *);
48 
49 gboolean
50 send_find_error_to_client (const char *, const char *, const char *,
51  gmp_parser_t *);
52 
53 void
54 error_send_to_client (GError **);
55 
56 void
58 
69 #define SENDF_TO_CLIENT_OR_FAIL_WITH_RETURN(err_ret, format, args...) \
70  do \
71  { \
72  gchar *msg = g_markup_printf_escaped (format, ##args); \
73  if (send_to_client (msg, gmp_parser->client_writer, \
74  gmp_parser->client_writer_data)) \
75  { \
76  g_free (msg); \
77  error_send_to_client (error); \
78  return err_ret; \
79  } \
80  g_free (msg); \
81  } \
82  while (0)
83 
93 #define SENDF_TO_CLIENT_OR_FAIL(format, args...) \
94  do \
95  { \
96  gchar *msg = g_markup_printf_escaped (format, ##args); \
97  if (send_to_client (msg, gmp_parser->client_writer, \
98  gmp_parser->client_writer_data)) \
99  { \
100  g_free (msg); \
101  error_send_to_client (error); \
102  return; \
103  } \
104  g_free (msg); \
105  } \
106  while (0)
107 
116 #define SEND_TO_CLIENT_OR_FAIL(msg) \
117  do \
118  { \
119  if (send_to_client (msg, gmp_parser->client_writer, \
120  gmp_parser->client_writer_data)) \
121  { \
122  error_send_to_client (error); \
123  return; \
124  } \
125  } \
126  while (0)
127 
128 void
129 log_event (const char *, const char *, const char *, const char *)
130  __attribute__ ((weak));
131 
132 void
133 log_event_fail (const char *, const char *, const char *, const char *);
134 
135 /* Status codes. */
136 
137 /* HTTP status codes used:
138  *
139  * 200 OK
140  * 201 Created
141  * 202 Accepted
142  * 400 Bad request
143  * 401 Must auth
144  * 404 Missing
145  */
146 
150 #define STATUS_ERROR_SYNTAX "400"
151 
155 #define STATUS_ERROR_MUST_AUTH "401"
156 
160 #define STATUS_ERROR_MUST_AUTH_TEXT "Authenticate first"
161 
165 #define STATUS_ERROR_ACCESS "403"
166 
170 #define STATUS_ERROR_ACCESS_TEXT "Access to resource forbidden"
171 
175 #define STATUS_ERROR_MISSING "404"
176 
180 #define STATUS_ERROR_MISSING_TEXT "Resource missing"
181 
185 #define STATUS_ERROR_BUSY "409"
186 
190 #define STATUS_ERROR_BUSY_TEXT "Resource busy"
191 
195 #define STATUS_ERROR_AUTH_FAILED "400"
196 
200 #define STATUS_ERROR_AUTH_FAILED_TEXT "Authentication failed"
201 
205 #define STATUS_OK "200"
206 
210 #define STATUS_OK_TEXT "OK"
211 
215 #define STATUS_OK_CREATED "201"
216 
220 #define STATUS_OK_CREATED_TEXT "OK, resource created"
221 
225 #define STATUS_OK_REQUESTED "202"
226 
230 #define STATUS_OK_REQUESTED_TEXT "OK, request submitted"
231 
235 #define STATUS_INTERNAL_ERROR "500"
236 
240 #define STATUS_INTERNAL_ERROR_TEXT "Internal error"
241 
245 #define STATUS_SERVICE_UNAVAILABLE "503"
246 
250 #define STATUS_SERVICE_DOWN "503"
251 
255 #define STATUS_SERVICE_DOWN_TEXT "Service temporarily down"
256 
263 #define XML_ERROR_SYNTAX(tag, text) \
264  "<" tag "_response" \
265  " status=\"" STATUS_ERROR_SYNTAX "\"" \
266  " status_text=\"" text "\"/>"
267 
273 #define XML_ERROR_ACCESS(tag) \
274  "<" tag "_response" \
275  " status=\"" STATUS_ERROR_ACCESS "\"" \
276  " status_text=\"" STATUS_ERROR_ACCESS_TEXT "\"/>"
277 
284 #define XML_ERROR_UNAVAILABLE(tag, text) \
285  "<" tag "_response" \
286  " status=\"" STATUS_SERVICE_UNAVAILABLE "\"" \
287  " status_text=\"" text "\"/>"
288 
294 #define XML_ERROR_MISSING(tag) \
295  "<" tag "_response" \
296  " status=\"" STATUS_ERROR_MISSING "\"" \
297  " status_text=\"" STATUS_ERROR_MISSING_TEXT "\"/>"
298 
304 #define XML_ERROR_AUTH_FAILED(tag) \
305  "<" tag "_response" \
306  " status=\"" STATUS_ERROR_AUTH_FAILED "\"" \
307  " status_text=\"" STATUS_ERROR_AUTH_FAILED_TEXT "\"/>"
308 
314 #define XML_ERROR_BUSY(tag) \
315  "<" tag "_response" \
316  " status=\"" STATUS_ERROR_BUSY "\"" \
317  " status_text=\"" STATUS_ERROR_BUSY_TEXT "\"/>"
318 
324 #define XML_OK(tag) \
325  "<" tag "_response" \
326  " status=\"" STATUS_OK "\"" \
327  " status_text=\"" STATUS_OK_TEXT "\"/>"
328 
334 #define XML_OK_CREATED(tag) \
335  "<" tag "_response" \
336  " status=\"" STATUS_OK_CREATED "\"" \
337  " status_text=\"" STATUS_OK_CREATED_TEXT "\"/>"
338 
344 #define XML_OK_CREATED_ID(tag) \
345  "<" tag "_response" \
346  " status=\"" STATUS_OK_CREATED "\"" \
347  " status_text=\"" STATUS_OK_CREATED_TEXT "\"" \
348  " id=\"%s\"/>"
349 
355 #define XML_OK_REQUESTED(tag) \
356  "<" tag "_response" \
357  " status=\"" STATUS_OK_REQUESTED "\"" \
358  " status_text=\"" STATUS_OK_REQUESTED_TEXT "\"/>"
359 
365 #define XML_INTERNAL_ERROR(tag) \
366  "<" tag "_response" \
367  " status=\"" STATUS_INTERNAL_ERROR "\"" \
368  " status_text=\"" STATUS_INTERNAL_ERROR_TEXT "\"/>"
369 
375 #define SEND_XML_SERVICE_DOWN(tag) \
376  do \
377  { \
378  char *str; \
379  str = \
380  g_strdup_printf ("<%s_response status='%s' status_text='%s'/>", tag, \
381  STATUS_SERVICE_DOWN, STATUS_SERVICE_DOWN_TEXT); \
382  SEND_TO_CLIENT_OR_FAIL (str); \
383  g_free (str); \
384  } \
385  while (0);
386 
387 #endif /* not _GVMD_GMP_BASE_H */
void error_send_to_client(GError **error)
Set an out of space parse error on a GError.
Definition: gmp_base.c:170
int find_attribute(const gchar **attribute_names, const gchar **attribute_values, const char *attribute_name, const gchar **attribute_value)
Find an attribute in a parser callback list of attributes.
Definition: gmp_base.c:52
int append_attribute(const gchar **attribute_names, const gchar **attribute_values, const char *attribute_name, gchar **string)
Find an attribute in a parser callback list of attributes and append.
Definition: gmp_base.c:81
gboolean send_find_error_to_client(const char *command, const char *type, const char *id, gmp_parser_t *gmp_parser)
Send an XML find error response message to the client.
Definition: gmp_base.c:148
void log_event_fail(const char *type, const char *type_name, const char *id, const char *action)
Creates a log event failure entry for a resource action.
Definition: gmp_base.c:268
gboolean send_to_client(const char *msg, int(*user_send_to_client)(const char *, void *), void *user_send_to_client_data)
Send a response message to the client.
Definition: gmp_base.c:128
void internal_error_send_to_client(GError **error)
Set an internal error on a GError.
Definition: gmp_base.c:183
void buffer_xml_append_printf(GString *buffer, const char *format,...)
Format XML into a buffer.
Definition: gmp_base.c:104
void log_event(const char *type, const char *type_name, const char *id, const char *action)
Creates a log event entry for a resource action.
Definition: gmp_base.c:253
__attribute__((deprecated))
Return if argument 1 matches regular expression in argument 2.
Definition: manage_pg_server.c:52
A handle on a GMP parser.
Definition: gmp_base.h:28
int parent_state
Parent state when reading over.
Definition: gmp_base.h:33
void * client_writer_data
Argument to client_writer.
Definition: gmp_base.h:30
int importing
Whether the current op is importing.
Definition: gmp_base.h:31
gchar ** disabled_commands
Disabled commands.
Definition: gmp_base.h:34
int read_over
Read over any child elements.
Definition: gmp_base.h:32