Greenbone Vulnerability Manager  9.0.1
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_find_error_to_client (const char *, const char *, const char *,
58  gmp_parser_t *);
59 
60 void
61 error_send_to_client (GError **);
62 
63 void
65 
75 #define SENDF_TO_CLIENT_OR_FAIL(format, args...) \
76  do \
77  { \
78  gchar *msg = g_markup_printf_escaped (format, ##args); \
79  if (send_to_client (msg, gmp_parser->client_writer, \
80  gmp_parser->client_writer_data)) \
81  { \
82  g_free (msg); \
83  error_send_to_client (error); \
84  return; \
85  } \
86  g_free (msg); \
87  } \
88  while (0)
89 
98 #define SEND_TO_CLIENT_OR_FAIL(msg) \
99  do \
100  { \
101  if (send_to_client (msg, gmp_parser->client_writer, \
102  gmp_parser->client_writer_data)) \
103  { \
104  error_send_to_client (error); \
105  return; \
106  } \
107  } \
108  while (0)
109 
110 void
111 log_event (const char *, const char *, const char *, const char *)
112  __attribute__ ((weak));
113 
114 void
115 log_event_fail (const char *, const char *, const char *, const char *);
116 
117 /* Status codes. */
118 
119 /* HTTP status codes used:
120  *
121  * 200 OK
122  * 201 Created
123  * 202 Accepted
124  * 400 Bad request
125  * 401 Must auth
126  * 404 Missing
127  */
128 
132 #define STATUS_ERROR_SYNTAX "400"
133 
137 #define STATUS_ERROR_MUST_AUTH "401"
138 
142 #define STATUS_ERROR_MUST_AUTH_TEXT "Authenticate first"
143 
147 #define STATUS_ERROR_ACCESS "403"
148 
152 #define STATUS_ERROR_ACCESS_TEXT "Access to resource forbidden"
153 
157 #define STATUS_ERROR_MISSING "404"
158 
162 #define STATUS_ERROR_MISSING_TEXT "Resource missing"
163 
167 #define STATUS_ERROR_BUSY "409"
168 
172 #define STATUS_ERROR_BUSY_TEXT "Resource busy"
173 
177 #define STATUS_ERROR_AUTH_FAILED "400"
178 
182 #define STATUS_ERROR_AUTH_FAILED_TEXT "Authentication failed"
183 
187 #define STATUS_OK "200"
188 
192 #define STATUS_OK_TEXT "OK"
193 
197 #define STATUS_OK_CREATED "201"
198 
202 #define STATUS_OK_CREATED_TEXT "OK, resource created"
203 
207 #define STATUS_OK_REQUESTED "202"
208 
212 #define STATUS_OK_REQUESTED_TEXT "OK, request submitted"
213 
217 #define STATUS_INTERNAL_ERROR "500"
218 
222 #define STATUS_INTERNAL_ERROR_TEXT "Internal error"
223 
227 #define STATUS_SERVICE_UNAVAILABLE "503"
228 
232 #define STATUS_SERVICE_DOWN "503"
233 
237 #define STATUS_SERVICE_DOWN_TEXT "Service temporarily down"
238 
245 #define XML_ERROR_SYNTAX(tag, text) \
246  "<" tag "_response" \
247  " status=\"" STATUS_ERROR_SYNTAX "\"" \
248  " status_text=\"" text "\"/>"
249 
255 #define XML_ERROR_ACCESS(tag) \
256  "<" tag "_response" \
257  " status=\"" STATUS_ERROR_ACCESS "\"" \
258  " status_text=\"" STATUS_ERROR_ACCESS_TEXT "\"/>"
259 
266 #define XML_ERROR_UNAVAILABLE(tag, text) \
267  "<" tag "_response" \
268  " status=\"" STATUS_SERVICE_UNAVAILABLE "\"" \
269  " status_text=\"" text "\"/>"
270 
276 #define XML_ERROR_MISSING(tag) \
277  "<" tag "_response" \
278  " status=\"" STATUS_ERROR_MISSING "\"" \
279  " status_text=\"" STATUS_ERROR_MISSING_TEXT "\"/>"
280 
286 #define XML_ERROR_AUTH_FAILED(tag) \
287  "<" tag "_response" \
288  " status=\"" STATUS_ERROR_AUTH_FAILED "\"" \
289  " status_text=\"" STATUS_ERROR_AUTH_FAILED_TEXT "\"/>"
290 
296 #define XML_ERROR_BUSY(tag) \
297  "<" tag "_response" \
298  " status=\"" STATUS_ERROR_BUSY "\"" \
299  " status_text=\"" STATUS_ERROR_BUSY_TEXT "\"/>"
300 
306 #define XML_OK(tag) \
307  "<" tag "_response" \
308  " status=\"" STATUS_OK "\"" \
309  " status_text=\"" STATUS_OK_TEXT "\"/>"
310 
316 #define XML_OK_CREATED(tag) \
317  "<" tag "_response" \
318  " status=\"" STATUS_OK_CREATED "\"" \
319  " status_text=\"" STATUS_OK_CREATED_TEXT "\"/>"
320 
326 #define XML_OK_CREATED_ID(tag) \
327  "<" tag "_response" \
328  " status=\"" STATUS_OK_CREATED "\"" \
329  " status_text=\"" STATUS_OK_CREATED_TEXT "\"" \
330  " id=\"%s\"/>"
331 
337 #define XML_OK_REQUESTED(tag) \
338  "<" tag "_response" \
339  " status=\"" STATUS_OK_REQUESTED "\"" \
340  " status_text=\"" STATUS_OK_REQUESTED_TEXT "\"/>"
341 
347 #define XML_INTERNAL_ERROR(tag) \
348  "<" tag "_response" \
349  " status=\"" STATUS_INTERNAL_ERROR "\"" \
350  " status_text=\"" STATUS_INTERNAL_ERROR_TEXT "\"/>"
351 
357 #define SEND_XML_SERVICE_DOWN(tag) \
358  do \
359  { \
360  char *str; \
361  str = \
362  g_strdup_printf ("<%s_response status='%s' status_text='%s'/>", tag, \
363  STATUS_SERVICE_DOWN, STATUS_SERVICE_DOWN_TEXT); \
364  SEND_TO_CLIENT_OR_FAIL (str); \
365  g_free (str); \
366  } \
367  while (0);
368 
369 #endif /* not _GVMD_GMP_BASE_H */
gmp_parser_t::read_over
int read_over
Read over any child elements.
Definition: gmp_base.h:39
send_find_error_to_client
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:155
find_attribute
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
send_to_client
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:135
gmp_parser_t::client_writer_data
void * client_writer_data
Argument to client_writer.
Definition: gmp_base.h:37
append_attribute
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
log_event_fail
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:275
gmp_parser_t
A handle on a GMP parser.
Definition: gmp_base.h:35
error_send_to_client
void error_send_to_client(GError **error)
Set an out of space parse error on a GError.
Definition: gmp_base.c:177
log_event
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:260
gmp_parser_t::importing
int importing
Whether the current op is importing.
Definition: gmp_base.h:38
gmp_parser_t::disabled_commands
gchar ** disabled_commands
Disabled commands.
Definition: gmp_base.h:41
internal_error_send_to_client
void internal_error_send_to_client(GError **error)
Set an internal error on a GError.
Definition: gmp_base.c:190
buffer_xml_append_printf
void buffer_xml_append_printf(GString *buffer, const char *format,...)
Format XML into a buffer.
Definition: gmp_base.c:111
gmp_parser_t::parent_state
int parent_state
Parent state when reading over.
Definition: gmp_base.h:40