Skip to content

Commit 6a9e3ed

Browse files
mattmundellbjoernricks
authored andcommitted
Change: move manage_test_alert to manage_alert.c
1 parent 69b4740 commit 6a9e3ed

File tree

5 files changed

+139
-134
lines changed

5 files changed

+139
-134
lines changed

src/manage.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -536,9 +536,6 @@ modify_alert (const char*, const char*, const char*, const char*,
536536
const char*, event_t, GPtrArray*, alert_condition_t, GPtrArray*,
537537
alert_method_t, GPtrArray*);
538538

539-
int
540-
manage_test_alert (const char *, gchar **);
541-
542539
int
543540
alert_in_use (alert_t);
544541

src/manage_alerts.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525

2626
#include "manage_alerts.h"
2727
#include "manage_sql.h"
28+
#include "manage_acl.h"
29+
30+
#include <gvm/util/uuidutils.h>
2831

2932
#undef G_LOG_DOMAIN
3033
/**
@@ -239,3 +242,133 @@ alert_method_from_name (const char* name)
239242
return ALERT_METHOD_VFIRE;
240243
return ALERT_METHOD_ERROR;
241244
}
245+
246+
/**
247+
* @brief Test an alert.
248+
*
249+
* @param[in] alert_id Alert UUID.
250+
* @param[out] script_message Custom message from the alert script.
251+
*
252+
* @return 0 success, 1 failed to find alert, 2 failed to find task,
253+
* 99 permission denied, -1 error, -2 failed to find report format
254+
* for alert, -3 failed to find filter for alert, -4 failed to find
255+
* credential for alert, -5 alert script failed.
256+
*/
257+
int
258+
manage_test_alert (const char *alert_id, gchar **script_message)
259+
{
260+
int ret;
261+
alert_t alert;
262+
task_t task;
263+
report_t report;
264+
result_t result;
265+
char *task_id, *report_id;
266+
time_t now;
267+
char now_string[26];
268+
gchar *clean;
269+
270+
if (acl_user_may ("test_alert") == 0)
271+
return 99;
272+
273+
if (find_alert_with_permission (alert_id, &alert, "test_alert"))
274+
return -1;
275+
if (alert == 0)
276+
return 1;
277+
278+
if (alert_event (alert) == EVENT_NEW_SECINFO
279+
|| alert_event (alert) == EVENT_UPDATED_SECINFO)
280+
{
281+
char *alert_event_data;
282+
gchar *type;
283+
284+
alert_event_data = alert_data (alert, "event", "secinfo_type");
285+
type = g_strdup_printf ("%s_example", alert_event_data ?: "NVT");
286+
free (alert_event_data);
287+
288+
if (alert_event (alert) == EVENT_NEW_SECINFO)
289+
ret = manage_alert (alert_id, "0", EVENT_NEW_SECINFO, (void*) type,
290+
script_message);
291+
else
292+
ret = manage_alert (alert_id, "0", EVENT_UPDATED_SECINFO, (void*) type,
293+
script_message);
294+
295+
g_free (type);
296+
297+
return ret;
298+
}
299+
300+
task = make_task (g_strdup ("Temporary Task for Alert"),
301+
g_strdup (""),
302+
0, /* Exclude from assets. */
303+
0); /* Skip event and log. */
304+
305+
report_id = gvm_uuid_make ();
306+
if (report_id == NULL)
307+
return -1;
308+
task_uuid (task, &task_id);
309+
report = make_report (task, report_id, TASK_STATUS_DONE);
310+
311+
result = make_result (task, "127.0.0.1", "localhost", "23/tcp",
312+
"1.3.6.1.4.1.25623.1.0.10330", "Alarm",
313+
"A telnet server seems to be running on this port.",
314+
NULL);
315+
if (result)
316+
report_add_result (report, result);
317+
318+
319+
result = make_result (
320+
task, "127.0.0.1", "localhost", "general/tcp",
321+
"1.3.6.1.4.1.25623.1.0.103823", "Alarm",
322+
"IP,Host,Port,SSL/TLS-Version,Ciphers,Application-CPE\n"
323+
"127.0.0.1,localhost,443,TLSv1.1;TLSv1.2",
324+
NULL);
325+
if (result)
326+
report_add_result (report, result);
327+
328+
now = time (NULL);
329+
if (strlen (ctime_r (&now, now_string)) == 0)
330+
{
331+
ret = -1;
332+
goto exit;
333+
}
334+
clean = g_strdup (now_string);
335+
if (clean[strlen (clean) - 1] == '\n')
336+
clean[strlen (clean) - 1] = '\0';
337+
set_task_start_time_ctime (task, g_strdup (clean));
338+
set_scan_start_time_ctime (report, g_strdup (clean));
339+
set_scan_host_start_time_ctime (report, "127.0.0.1", clean);
340+
341+
insert_report_host_detail (report,
342+
"127.0.0.1",
343+
"nvt",
344+
"1.3.6.1.4.1.25623.1.0.108577",
345+
"",
346+
"App",
347+
"cpe:/a:openbsd:openssh:8.9p1",
348+
"0123456789ABCDEF0123456789ABCDEF");
349+
350+
insert_report_host_detail (report,
351+
"127.0.0.1",
352+
"nvt",
353+
"1.3.6.1.4.1.25623.1.0.10330",
354+
"Host Details",
355+
"best_os_cpe",
356+
"cpe:/o:canonical:ubuntu_linux:22.04",
357+
"123456789ABCDEF0123456789ABCDEF0");
358+
359+
set_scan_host_end_time_ctime (report, "127.0.0.1", clean);
360+
set_scan_end_time_ctime (report, clean);
361+
g_free (clean);
362+
ret = manage_alert (alert_id,
363+
task_id,
364+
EVENT_TASK_RUN_STATUS_CHANGED,
365+
(void*) TASK_STATUS_DONE,
366+
script_message);
367+
exit:
368+
/* No one should be running this task, so we don't worry about the lock. We
369+
* could guarantee that no one runs the task, but this is a very rare case. */
370+
delete_task (task, 1);
371+
free (task_id);
372+
free (report_id);
373+
return ret;
374+
}

src/manage_alerts.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ alert_condition (alert_t);
109109
alert_method_t
110110
alert_method (alert_t alert);
111111

112+
int
113+
manage_test_alert (const char *, gchar **);
114+
112115
int
113116
init_alert_iterator (iterator_t*, get_data_t*);
114117

src/manage_sql.c

Lines changed: 1 addition & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -8114,7 +8114,7 @@ alert_filter_id (alert_t alert)
81148114
*
81158115
* @return Event.
81168116
*/
8117-
static event_t
8117+
event_t
81188118
alert_event (alert_t alert)
81198119
{
81208120
return sql_int ("SELECT event FROM alerts WHERE id = %llu;",
@@ -13522,136 +13522,6 @@ escalate_2 (alert_t alert, task_t task, report_t report, event_t event,
1352213522
"Name Title\n" \
1352313523
"------------------------------------------------------------------------------------------\n"
1352413524

13525-
/**
13526-
* @brief Test an alert.
13527-
*
13528-
* @param[in] alert_id Alert UUID.
13529-
* @param[out] script_message Custom message from the alert script.
13530-
*
13531-
* @return 0 success, 1 failed to find alert, 2 failed to find task,
13532-
* 99 permission denied, -1 error, -2 failed to find report format
13533-
* for alert, -3 failed to find filter for alert, -4 failed to find
13534-
* credential for alert, -5 alert script failed.
13535-
*/
13536-
int
13537-
manage_test_alert (const char *alert_id, gchar **script_message)
13538-
{
13539-
int ret;
13540-
alert_t alert;
13541-
task_t task;
13542-
report_t report;
13543-
result_t result;
13544-
char *task_id, *report_id;
13545-
time_t now;
13546-
char now_string[26];
13547-
gchar *clean;
13548-
13549-
if (acl_user_may ("test_alert") == 0)
13550-
return 99;
13551-
13552-
if (find_alert_with_permission (alert_id, &alert, "test_alert"))
13553-
return -1;
13554-
if (alert == 0)
13555-
return 1;
13556-
13557-
if (alert_event (alert) == EVENT_NEW_SECINFO
13558-
|| alert_event (alert) == EVENT_UPDATED_SECINFO)
13559-
{
13560-
char *alert_event_data;
13561-
gchar *type;
13562-
13563-
alert_event_data = alert_data (alert, "event", "secinfo_type");
13564-
type = g_strdup_printf ("%s_example", alert_event_data ?: "NVT");
13565-
free (alert_event_data);
13566-
13567-
if (alert_event (alert) == EVENT_NEW_SECINFO)
13568-
ret = manage_alert (alert_id, "0", EVENT_NEW_SECINFO, (void*) type,
13569-
script_message);
13570-
else
13571-
ret = manage_alert (alert_id, "0", EVENT_UPDATED_SECINFO, (void*) type,
13572-
script_message);
13573-
13574-
g_free (type);
13575-
13576-
return ret;
13577-
}
13578-
13579-
task = make_task (g_strdup ("Temporary Task for Alert"),
13580-
g_strdup (""),
13581-
0, /* Exclude from assets. */
13582-
0); /* Skip event and log. */
13583-
13584-
report_id = gvm_uuid_make ();
13585-
if (report_id == NULL)
13586-
return -1;
13587-
task_uuid (task, &task_id);
13588-
report = make_report (task, report_id, TASK_STATUS_DONE);
13589-
13590-
result = make_result (task, "127.0.0.1", "localhost", "23/tcp",
13591-
"1.3.6.1.4.1.25623.1.0.10330", "Alarm",
13592-
"A telnet server seems to be running on this port.",
13593-
NULL);
13594-
if (result)
13595-
report_add_result (report, result);
13596-
13597-
13598-
result = make_result (
13599-
task, "127.0.0.1", "localhost", "general/tcp",
13600-
"1.3.6.1.4.1.25623.1.0.103823", "Alarm",
13601-
"IP,Host,Port,SSL/TLS-Version,Ciphers,Application-CPE\n"
13602-
"127.0.0.1,localhost,443,TLSv1.1;TLSv1.2",
13603-
NULL);
13604-
if (result)
13605-
report_add_result (report, result);
13606-
13607-
now = time (NULL);
13608-
if (strlen (ctime_r (&now, now_string)) == 0)
13609-
{
13610-
ret = -1;
13611-
goto exit;
13612-
}
13613-
clean = g_strdup (now_string);
13614-
if (clean[strlen (clean) - 1] == '\n')
13615-
clean[strlen (clean) - 1] = '\0';
13616-
set_task_start_time_ctime (task, g_strdup (clean));
13617-
set_scan_start_time_ctime (report, g_strdup (clean));
13618-
set_scan_host_start_time_ctime (report, "127.0.0.1", clean);
13619-
13620-
insert_report_host_detail (report,
13621-
"127.0.0.1",
13622-
"nvt",
13623-
"1.3.6.1.4.1.25623.1.0.108577",
13624-
"",
13625-
"App",
13626-
"cpe:/a:openbsd:openssh:8.9p1",
13627-
"0123456789ABCDEF0123456789ABCDEF");
13628-
13629-
insert_report_host_detail (report,
13630-
"127.0.0.1",
13631-
"nvt",
13632-
"1.3.6.1.4.1.25623.1.0.10330",
13633-
"Host Details",
13634-
"best_os_cpe",
13635-
"cpe:/o:canonical:ubuntu_linux:22.04",
13636-
"123456789ABCDEF0123456789ABCDEF0");
13637-
13638-
set_scan_host_end_time_ctime (report, "127.0.0.1", clean);
13639-
set_scan_end_time_ctime (report, clean);
13640-
g_free (clean);
13641-
ret = manage_alert (alert_id,
13642-
task_id,
13643-
EVENT_TASK_RUN_STATUS_CHANGED,
13644-
(void*) TASK_STATUS_DONE,
13645-
script_message);
13646-
exit:
13647-
/* No one should be running this task, so we don't worry about the lock. We
13648-
* could guarantee that no one runs the task, but this is a very rare case. */
13649-
delete_task (task, 1);
13650-
free (task_id);
13651-
free (report_id);
13652-
return ret;
13653-
}
13654-
1365513525
/**
1365613526
* @brief Return the SecInfo count.
1365713527
*

src/manage_sql.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ int create_current_report (task_t, char **, task_status_t);
410410

411411
char *alert_data (alert_t, const char *, const char *);
412412

413+
event_t alert_event (alert_t);
414+
413415
int init_task_schedule_iterator (iterator_t *);
414416

415417
void cleanup_task_schedule_iterator (iterator_t *);

0 commit comments

Comments
 (0)