1 /* Copyright (C) 2019-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 /**
20 * @file
21 * @brief GVM manage layer: GET utils.
22 */
23
24 #include "manage_get.h"
25 #include "manage_sql.h"
26 #include "sql.h"
27
28 #include <stdlib.h>
29 #include <string.h>
30
31 /**
32 * @brief Reset command data.
33 *
34 * @param[in] data Command data.
35 */
36 void
get_data_reset(get_data_t * data)37 get_data_reset (get_data_t *data)
38 {
39 free (data->id);
40 free (data->filt_id);
41 free (data->filter);
42 free (data->filter_replace);
43 free (data->filter_replacement);
44 free (data->subtype);
45 free (data->type);
46 if (data->extra_params)
47 g_hash_table_destroy (data->extra_params);
48
49 memset (data, 0, sizeof (get_data_t));
50 }
51
52 /**
53 * @brief Retrieves a type-specific extra parameter from a get_data_t.
54 *
55 * @param[in] data The get data to add the parameter to.
56 * @param[in] name Name of the parameter to add.
57 *
58 * @return Value of the parameter or NULL if not set.
59 */
60 const char *
get_data_get_extra(const get_data_t * data,const char * name)61 get_data_get_extra (const get_data_t *data, const char *name)
62 {
63 if (data->extra_params == NULL)
64 return NULL;
65 else
66 return g_hash_table_lookup (data->extra_params, name);
67 }
68
69 /**
70 * @brief Sets a type-specific extra parameter in a get_data_t.
71 *
72 * The names and values will be duplicated.
73 *
74 * @param[in] data The get data to add the parameter to.
75 * @param[in] name Name of the parameter to add.
76 * @param[in] value Value of the parameter to add.
77 */
78 void
get_data_set_extra(get_data_t * data,const char * name,const char * value)79 get_data_set_extra (get_data_t *data, const char *name, const char *value)
80 {
81 if (name == NULL)
82 return;
83
84 if (data->extra_params == NULL)
85 {
86 if (value == NULL)
87 return;
88
89 data->extra_params
90 = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
91 }
92
93 if (value)
94 g_hash_table_insert (data->extra_params,
95 g_strdup (name),
96 g_strdup (value));
97 else
98 g_hash_table_remove (data->extra_params, name);
99 }
100
101
102 /* GET iterators. */
103
104 /**
105 * @brief Get the resource from a GET iterator.
106 *
107 * @param[in] iterator Iterator.
108 *
109 * @return Resource.
110 */
111 resource_t
get_iterator_resource(iterator_t * iterator)112 get_iterator_resource (iterator_t* iterator)
113 {
114 if (iterator->done) return 0;
115 return iterator_int64 (iterator, 0);
116 }
117
118 /**
119 * @brief Get the UUID of the resource from a GET iterator.
120 *
121 * @param[in] iterator Iterator.
122 *
123 * @return UUID of the resource or NULL if iteration is complete.
124 */
125 DEF_ACCESS (get_iterator_uuid, 1);
126
127 /**
128 * @brief Get the name of the resource from a GET iterator.
129 *
130 * @param[in] iterator Iterator.
131 *
132 * @return Name of the resource or NULL if iteration is complete.
133 */
134 DEF_ACCESS (get_iterator_name, 2);
135
136 /**
137 * @brief Get the comment from a GET iterator.
138 *
139 * @param[in] iterator Iterator.
140 *
141 * @return Comment.
142 */
143 const char*
get_iterator_comment(iterator_t * iterator)144 get_iterator_comment (iterator_t* iterator)
145 {
146 const char *ret;
147 if (iterator->done) return "";
148 ret = iterator_string (iterator, 3);
149 return ret ? ret : "";
150 }
151
152 /**
153 * @brief Get the creation time of the resource from a GET iterator.
154 *
155 * @param[in] iterator Iterator.
156 *
157 * @return Creation time of the resource or NULL if iteration is complete.
158 */
159 DEF_ACCESS (get_iterator_creation_time, 4);
160
161 /**
162 * @brief Get the modification time of the resource from a GET iterator.
163 *
164 * @param[in] iterator Iterator.
165 *
166 * @return Modification time of the resource or NULL if iteration is complete.
167 */
168 DEF_ACCESS (get_iterator_modification_time, 5);
169
170 /**
171 * @brief Get the owner name of the resource from a GET iterator.
172 *
173 * @param[in] iterator Iterator.
174 *
175 * @return Owner name of the resource or NULL if iteration is complete.
176 */
177 DEF_ACCESS (get_iterator_owner_name, 8);
178
179 /**
180 * @brief Get the owner from a GET iterator.
181 *
182 * @param[in] iterator Iterator.
183 *
184 * @return Owner.
185 */
186 user_t
get_iterator_owner(iterator_t * iterator)187 get_iterator_owner (iterator_t* iterator)
188 {
189 if (iterator->done) return 0;
190 return iterator_int64 (iterator, 9);
191 }
192