Greenbone Vulnerability Manager  8.0.0~git
gmp_base.h
1 /* GVM
2  * $Id$
3  * Description: GVM GMP layer: Headers used internally.
4  *
5  * Authors:
6  * Matthew Mundell <matthew.mundell@greenbone.net>
7  *
8  * Copyright:
9  * Copyright (C) 2018 Greenbone Networks GmbH
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
26 #ifndef _GVMD_GMP_BASE_H
27 #define _GVMD_GMP_BASE_H
28 
29 #include <glib.h>
30 
34 typedef struct
35 {
36  int (*client_writer) (const char *, void *);
38  int importing;
39  int read_over;
42 } gmp_parser_t;
43 
44 int
45 find_attribute (const gchar **, const gchar **, const char *, const gchar **);
46 
47 int
48 append_attribute (const gchar **, const gchar **, const char *, gchar **);
49 
50 void
51 buffer_xml_append_printf (GString *, const char *, ...);
52 
53 gboolean
54 send_to_client (const char *, int (*) (const char *, void *), void *);
55 
56 gboolean
57 send_element_error_to_client (const char *,
58  const char *,
59  int (*) (const char *, void *),
60  void *);
61 
62 gboolean
63 send_find_error_to_client (const char *,
64  const char *,
65  const char *,
66  gmp_parser_t *);
67 
68 void
69 error_send_to_client (GError **);
70 
71 void
73 
83 #define SENDF_TO_CLIENT_OR_FAIL(format, args...) \
84  do \
85  { \
86  gchar *msg = g_markup_printf_escaped (format, ##args); \
87  if (send_to_client ( \
88  msg, gmp_parser->client_writer, gmp_parser->client_writer_data)) \
89  { \
90  g_free (msg); \
91  error_send_to_client (error); \
92  return; \
93  } \
94  g_free (msg); \
95  } \
96  while (0)
97 
106 #define SEND_TO_CLIENT_OR_FAIL(msg) \
107  do \
108  { \
109  if (send_to_client ( \
110  msg, gmp_parser->client_writer, gmp_parser->client_writer_data)) \
111  { \
112  error_send_to_client (error); \
113  return; \
114  } \
115  } \
116  while (0)
117 
118 void
119 log_event (const char *, const char *, const char *, const char *);
120 
121 void
122 log_event_fail (const char *, const char *, const char *, const char *);
123 
124 /* Status codes. */
125 
126 /* HTTP status codes used:
127  *
128  * 200 OK
129  * 201 Created
130  * 202 Accepted
131  * 400 Bad request
132  * 401 Must auth
133  * 404 Missing
134  */
135 
139 #define STATUS_ERROR_SYNTAX "400"
140 
144 #define STATUS_ERROR_MUST_AUTH "401"
145 
149 #define STATUS_ERROR_MUST_AUTH_TEXT "Authenticate first"
150 
154 #define STATUS_ERROR_ACCESS "403"
155 
159 #define STATUS_ERROR_ACCESS_TEXT "Access to resource forbidden"
160 
164 #define STATUS_ERROR_MISSING "404"
165 
169 #define STATUS_ERROR_MISSING_TEXT "Resource missing"
170 
174 #define STATUS_ERROR_BUSY "409"
175 
179 #define STATUS_ERROR_BUSY_TEXT "Resource busy"
180 
184 #define STATUS_ERROR_AUTH_FAILED "400"
185 
189 #define STATUS_ERROR_AUTH_FAILED_TEXT "Authentication failed"
190 
194 #define STATUS_OK "200"
195 
199 #define STATUS_OK_TEXT "OK"
200 
204 #define STATUS_OK_CREATED "201"
205 
209 #define STATUS_OK_CREATED_TEXT "OK, resource created"
210 
214 #define STATUS_OK_REQUESTED "202"
215 
219 #define STATUS_OK_REQUESTED_TEXT "OK, request submitted"
220 
224 #define STATUS_INTERNAL_ERROR "500"
225 
229 #define STATUS_INTERNAL_ERROR_TEXT "Internal error"
230 
234 #define STATUS_SERVICE_UNAVAILABLE "503"
235 
239 #define STATUS_SERVICE_DOWN "503"
240 
244 #define STATUS_SERVICE_DOWN_TEXT "Service temporarily down"
245 
252 #define XML_ERROR_SYNTAX(tag, text) \
253  "<" tag "_response" \
254  " status=\"" STATUS_ERROR_SYNTAX "\"" \
255  " status_text=\"" text "\"/>"
256 
262 #define XML_ERROR_ACCESS(tag) \
263  "<" tag "_response" \
264  " status=\"" STATUS_ERROR_ACCESS "\"" \
265  " status_text=\"" STATUS_ERROR_ACCESS_TEXT "\"/>"
266 
273 #define XML_ERROR_UNAVAILABLE(tag, text) \
274  "<" tag "_response" \
275  " status=\"" STATUS_SERVICE_UNAVAILABLE "\"" \
276  " status_text=\"" text "\"/>"
277 
283 #define XML_ERROR_MISSING(tag) \
284  "<" tag "_response" \
285  " status=\"" STATUS_ERROR_MISSING "\"" \
286  " status_text=\"" STATUS_ERROR_MISSING_TEXT "\"/>"
287 
293 #define XML_ERROR_AUTH_FAILED(tag) \
294  "<" tag "_response" \
295  " status=\"" STATUS_ERROR_AUTH_FAILED "\"" \
296  " status_text=\"" STATUS_ERROR_AUTH_FAILED_TEXT "\"/>"
297 
303 #define XML_ERROR_BUSY(tag) \
304  "<" tag "_response" \
305  " status=\"" STATUS_ERROR_BUSY "\"" \
306  " status_text=\"" STATUS_ERROR_BUSY_TEXT "\"/>"
307 
313 #define XML_OK(tag) \
314  "<" tag "_response" \
315  " status=\"" STATUS_OK "\"" \
316  " status_text=\"" STATUS_OK_TEXT "\"/>"
317 
323 #define XML_OK_CREATED(tag) \
324  "<" tag "_response" \
325  " status=\"" STATUS_OK_CREATED "\"" \
326  " status_text=\"" STATUS_OK_CREATED_TEXT "\"/>"
327 
333 #define XML_OK_CREATED_ID(tag) \
334  "<" tag "_response" \
335  " status=\"" STATUS_OK_CREATED "\"" \
336  " status_text=\"" STATUS_OK_CREATED_TEXT "\"" \
337  " id=\"%s\"/>"
338 
344 #define XML_OK_REQUESTED(tag) \
345  "<" tag "_response" \
346  " status=\"" STATUS_OK_REQUESTED "\"" \
347  " status_text=\"" STATUS_OK_REQUESTED_TEXT "\"/>"
348 
354 #define XML_INTERNAL_ERROR(tag) \
355  "<" tag "_response" \
356  " status=\"" STATUS_INTERNAL_ERROR "\"" \
357  " status_text=\"" STATUS_INTERNAL_ERROR_TEXT "\"/>"
358 
364 #define SEND_XML_SERVICE_DOWN(tag) \
365  do \
366  { \
367  char *str; \
368  if (scanner_current_loading && scanner_total_loading) \
369  str = g_strdup_printf ("<%s_response status='%s' " \
370  "status_text='Scanner loading nvts (%d/%d)'/>", \
371  tag, \
372  STATUS_SERVICE_DOWN, \
373  scanner_current_loading, \
374  scanner_total_loading); \
375  else \
376  str = g_strdup_printf ("<%s_response status='%s' status_text='%s'/>", \
377  tag, \
378  STATUS_SERVICE_DOWN, \
379  STATUS_SERVICE_DOWN_TEXT); \
380  SEND_TO_CLIENT_OR_FAIL (str); \
381  g_free (str); \
382  } \
383  while (0);
384 
385 #endif /* not _GVMD_GMP_BASE_H */
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:300
gboolean send_element_error_to_client(const char *command, const char *element, int(*write_to_client)(const char *, void *), void *write_to_client_data)
Send an XML element error response message to the client.
Definition: gmp_base.c:154
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:134
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:59
int parent_state
Parent state when reading over.
Definition: gmp_base.h:40
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:317
A handle on a GMP parser.
Definition: gmp_base.h:34
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:88
int read_over
Read over any child elements.
Definition: gmp_base.h:39
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:183
void error_send_to_client(GError **error)
Set an out of space parse error on a GError.
Definition: gmp_base.c:208
void internal_error_send_to_client(GError **error)
Set an internal error on a GError.
Definition: gmp_base.c:223
void buffer_xml_append_printf(GString *buffer, const char *format,...)
Format XML into a buffer.
Definition: gmp_base.c:111
void * client_writer_data
Argument to client_writer.
Definition: gmp_base.h:37
int importing
Whether the current op is importing.
Definition: gmp_base.h:38
gchar ** disabled_commands
Disabled commands.
Definition: gmp_base.h:41