1 /* Copyright (C) 2018-2020 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_GET_H
20 #define _GVMD_GMP_GET_H
21 
22 #include "manage_get.h"
23 
24 void
25 get_data_parse_attributes (get_data_t *, const gchar *, const gchar **,
26                            const gchar **);
27 
28 int
29 init_get (gchar *, get_data_t *, const gchar *, int *);
30 
31 /**
32  * @brief Call init_get for a GET end handler.
33  *
34  * @param[in]  type     Resource type.
35  * @param[in]  capital  Resource type, capitalised.
36  */
37 #define INIT_GET(type, capital)                                            \
38   count = 0;                                                               \
39   ret = init_get ("get_" G_STRINGIFY (type) "s", &get_##type##s_data->get, \
40                   G_STRINGIFY (capital) "s", &first);                      \
41   if (ret)                                                                 \
42     {                                                                      \
43       switch (ret)                                                         \
44         {                                                                  \
45         case 99:                                                           \
46           SEND_TO_CLIENT_OR_FAIL (XML_ERROR_SYNTAX (                       \
47             "get_" G_STRINGIFY (type) "s", "Permission denied"));          \
48           break;                                                           \
49         default:                                                           \
50           internal_error_send_to_client (error);                           \
51           return;                                                          \
52         }                                                                  \
53       get_##type##s_data_reset (get_##type##s_data);                       \
54       set_client_state (CLIENT_AUTHENTIC);                                 \
55       return;                                                              \
56     }
57 
58 int
59 get_next (iterator_t *, get_data_t *, int *, int *,
60           int (*) (iterator_t *, const get_data_t *));
61 
62 int
63 send_get_start (const char *, int (*) (const char *, void *), void *);
64 
65 /**
66  * @brief Send start of GET response to client, returning on fail.
67  *
68  * @param[in]  type  Type of resource.
69  * @param[in]  get   GET data.
70  */
71 #define SEND_GET_START(type)                               \
72   do                                                       \
73     {                                                      \
74       if (send_get_start (type, gmp_parser->client_writer, \
75                           gmp_parser->client_writer_data)) \
76         {                                                  \
77           error_send_to_client (error);                    \
78           return;                                          \
79         }                                                  \
80     }                                                      \
81   while (0)
82 
83 int
84 send_get_common (const char *, get_data_t *, iterator_t *,
85                  int (*) (const char *, void *), void *, int, int);
86 
87 /**
88  * @brief Send common part of GET response to client, returning on fail.
89  *
90  * @param[in]  type      Type of resource.
91  * @param[in]  get       GET data.
92  * @param[in]  iterator  Iterator.
93  */
94 #define SEND_GET_COMMON(type, get, iterator)                               \
95   do                                                                       \
96     {                                                                      \
97       if (send_get_common (                                                \
98             G_STRINGIFY (type), get, iterator, gmp_parser->client_writer,  \
99             gmp_parser->client_writer_data,                                \
100             (get)->trash                                                   \
101               ? trash_##type##_writable (get_iterator_resource (iterator)) \
102               : type##_writable (get_iterator_resource (iterator)),        \
103             (get)->trash                                                   \
104               ? trash_##type##_in_use (get_iterator_resource (iterator))   \
105               : type##_in_use (get_iterator_resource (iterator))))         \
106         {                                                                  \
107           error_send_to_client (error);                                    \
108           return;                                                          \
109         }                                                                  \
110     }                                                                      \
111   while (0)
112 
113 /**
114  * @brief Send common part of GET response to client, returning on fail.
115  *
116  * This will work for types not using the trashcan.
117  *
118  * @param[in]  type      Type of resource.
119  * @param[in]  get       GET data.
120  * @param[in]  iterator  Iterator.
121  */
122 #define SEND_GET_COMMON_NO_TRASH(type, get, iterator)                          \
123   do                                                                           \
124     {                                                                          \
125       if (send_get_common (G_STRINGIFY (type), get, iterator,                  \
126                            gmp_parser->client_writer,                          \
127                            gmp_parser->client_writer_data,                     \
128                            type##_writable (get_iterator_resource (iterator)), \
129                            type##_in_use (get_iterator_resource (iterator))))  \
130         {                                                                      \
131           error_send_to_client (error);                                        \
132           return;                                                              \
133         }                                                                      \
134     }                                                                          \
135   while (0)
136 
137 int
138 buffer_get_filter_xml (GString *, const char *, const get_data_t *,
139                        const char *, const char *);
140 
141 int
142 send_get_end (const char *, get_data_t *, int, int, int,
143               int (*) (const char *, void *), void *);
144 
145 int
146 send_get_end_no_counts (const char *, get_data_t *,
147                         int (*) (const char *, void *), void *);
148 
149 /**
150  * @brief Send end of GET response to client, returning on fail.
151  *
152  * @param[in]  type  Type of resource.
153  * @param[in]  get   GET data.
154  */
155 #define SEND_GET_END(type, get, count, filtered)                               \
156   do                                                                           \
157     {                                                                          \
158       if (send_get_end (type, get, count, filtered,                            \
159                         resource_count (type, get), gmp_parser->client_writer, \
160                         gmp_parser->client_writer_data))                       \
161         {                                                                      \
162           error_send_to_client (error);                                        \
163           return;                                                              \
164         }                                                                      \
165     }                                                                          \
166   while (0)
167 
168 #endif /* not _GVMD_GMP_GET_H */
169