OpenVAS Scanner 23.23.1
table_driven_lsc.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
10
11#include "table_driven_lsc.h"
12
13#include "base/networking.h"
14#include "kb_cache.h"
15#include "plugutils.h"
16
17#include <ctype.h> // for tolower()
18#include <curl/curl.h>
19#include <gnutls/gnutls.h>
20#include <gvm/base/prefs.h>
21#include <gvm/util/mqtt.h> // for mqtt_reset
22#include <gvm/util/uuidutils.h> // for gvm_uuid_make
23#include <json-glib/json-glib.h>
24#include <stddef.h>
25
26#undef G_LOG_DOMAIN
30#define G_LOG_DOMAIN "lib misc"
31
35static int lsc_flag = 0;
36
39void
41{
42 lsc_flag = 1;
43}
44
47int
49{
50 return lsc_flag;
51}
52
64static JsonBuilder *
65add_packages_str_to_list (JsonBuilder *builder, const gchar *packages)
66{
67 gchar **package_list = NULL;
68
69 json_builder_set_member_name (builder, "package_list");
70 json_builder_begin_array (builder);
71
72 package_list = g_strsplit (packages, "\n", 0);
73 if (package_list && package_list[0])
74 {
75 int i;
76 for (i = 0; package_list[i]; i++)
77 json_builder_add_string_value (builder, package_list[i]);
78 }
79
80 json_builder_end_array (builder);
81 g_strfreev (package_list);
82
83 return builder;
84}
85
101static gchar *
102make_table_driven_lsc_info_json_str (const char *scan_id, const char *ip_str,
103 const char *hostname,
104 const char *os_release,
105 const char *package_list)
106{
107 JsonBuilder *builder;
108 JsonGenerator *gen;
109 JsonNode *root;
110 gchar *json_str;
111
112 /* Build the message in json format to be published. */
113 builder = json_builder_new ();
114
115 json_builder_begin_object (builder);
116
117 json_builder_set_member_name (builder, "message_id");
118 builder = json_builder_add_string_value (builder, gvm_uuid_make ());
119
120 json_builder_set_member_name (builder, "group_id");
121 builder = json_builder_add_string_value (builder, gvm_uuid_make ());
122
123 json_builder_set_member_name (builder, "message_type");
124 builder = json_builder_add_string_value (builder, "scan.start");
125
126 json_builder_set_member_name (builder, "created");
127 builder = json_builder_add_int_value (builder, time (NULL));
128
129 json_builder_set_member_name (builder, "scan_id");
130 builder = json_builder_add_string_value (builder, scan_id);
131
132 json_builder_set_member_name (builder, "host_ip");
133 json_builder_add_string_value (builder, ip_str);
134
135 json_builder_set_member_name (builder, "host_name");
136 json_builder_add_string_value (builder, hostname);
137
138 json_builder_set_member_name (builder, "os_release");
139 json_builder_add_string_value (builder, os_release);
140
141 add_packages_str_to_list (builder, package_list);
142
143 json_builder_end_object (builder);
144
145 gen = json_generator_new ();
146 root = json_builder_get_root (builder);
147 json_generator_set_root (gen, root);
148 json_str = json_generator_to_data (gen, NULL);
149
150 json_node_free (root);
151 g_object_unref (gen);
152 g_object_unref (builder);
153
154 if (json_str == NULL)
155 g_warning ("%s: Error while creating JSON.", __func__);
156
157 return json_str;
158}
159
174static gchar *
176 const char *host_ip, const char *json,
177 int len)
178{
179 JsonParser *parser;
180 JsonReader *reader = NULL;
181
182 GError *err = NULL;
183 gchar *ret = NULL;
184
185 parser = json_parser_new ();
186 if (!json_parser_load_from_data (parser, json, len, &err))
187 {
188 goto cleanup;
189 }
190
191 reader = json_reader_new (json_parser_get_root (parser));
192
193 // Check for Scan ID
194 if (!json_reader_read_member (reader, "scan_id"))
195 {
196 goto cleanup;
197 }
198 if (g_strcmp0 (json_reader_get_string_value (reader), scan_id))
199 {
200 goto cleanup;
201 }
202 json_reader_end_member (reader);
203
204 // Check Host IP
205 if (!json_reader_read_member (reader, "host_ip"))
206 {
207 goto cleanup;
208 }
209 if (g_strcmp0 (json_reader_get_string_value (reader), host_ip))
210 {
211 goto cleanup;
212 }
213 json_reader_end_member (reader);
214
215 // Check Status
216 if (!json_reader_read_member (reader, "status"))
217 {
218 goto cleanup;
219 }
220 ret = g_strdup (json_reader_get_string_value (reader));
221
222 json_reader_end_member (reader);
223
224cleanup:
225 if (reader)
226 g_object_unref (reader);
227 g_object_unref (parser);
228 if (err != NULL)
229 {
230 g_warning ("%s: Unable to parse json. Reason: %s", __func__,
231 err->message);
232 }
233 return ret;
234}
235
236#define RSNOTUS
237#ifdef RSNOTUS
242{
243 char *server; // original openvasd server URL
244 char *schema; // schema is http or https
245 char *host; // server hostname
246 char *alpn; // Application layer protocol negotiation: http/1.0, http/1.1, h2
247 char *http_version; // same version as in application layer
248 int port; // server port
249 int tls; // 0: TLS encapsulation diable. Otherwise enable
250};
251
252typedef struct notus_info *notus_info_t;
253
260static notus_info_t
262{
263 notus_info_t notusdata;
264 notusdata = g_malloc0 (sizeof (struct notus_info));
265 if (!notusdata)
266 return NULL;
267 notusdata->server = g_strdup (server);
268 return notusdata;
269}
270
275static void
277{
278 if (notusdata)
279 {
280 g_free (notusdata->server);
281 g_free (notusdata->schema);
282 g_free (notusdata->host);
283 g_free (notusdata->alpn);
284 g_free (notusdata->http_version);
285 }
286}
287
294static char *
296{
297 for (char *p = s; *p; p++)
298 *p = tolower (*p);
299 return s;
300}
301
311static gchar *
312make_package_list_as_json_str (const char *packages)
313{
314 JsonBuilder *builder;
315 JsonGenerator *gen;
316 JsonNode *root;
317 gchar *json_str = NULL;
318 gchar **package_list = NULL;
319 builder = json_builder_new ();
320
321 json_builder_begin_array (builder);
322
323 package_list = g_strsplit (packages, "\n", 0);
324 if (package_list && package_list[0])
325 {
326 int i;
327 for (i = 0; package_list[i]; i++)
328 json_builder_add_string_value (builder, package_list[i]);
329 }
330
331 json_builder_end_array (builder);
332 g_strfreev (package_list);
333
334 gen = json_generator_new ();
335 root = json_builder_get_root (builder);
336 json_generator_set_root (gen, root);
337 json_str = json_generator_to_data (gen, NULL);
338
339 json_node_free (root);
340 g_object_unref (gen);
341 g_object_unref (builder);
342
343 if (json_str == NULL)
344 g_warning ("%s: Error while creating JSON.", __func__);
345
346 return json_str;
347}
348
358static int
360{
361 CURLU *h = curl_url ();
362 char *schema = NULL;
363 char *host = NULL;
364 char *port = NULL;
365
366 if (!notusdata)
367 return -1;
368
369 if (curl_url_set (h, CURLUPART_URL, (*notusdata)->server, 0) > 0)
370 {
371 g_warning ("%s: Error parsing URL %s", __func__, (*notusdata)->server);
372 return -1;
373 }
374
375 curl_url_get (h, CURLUPART_SCHEME, &schema, 0);
376 curl_url_get (h, CURLUPART_HOST, &host, 0);
377 curl_url_get (h, CURLUPART_PORT, &port, 0);
378
379 if (!schema || !host)
380 {
381 g_warning ("%s: Invalid URL %s. It must be in format: "
382 "schema://host:port. E.g. http://localhost:8080",
383 __func__, (*notusdata)->server);
384 curl_url_cleanup (h);
385 curl_free (schema);
386 curl_free (host);
387 curl_free (port);
388 return -1;
389 }
390
391 (*notusdata)->host = g_strdup (host);
392 if (port)
393 (*notusdata)->port = atoi (port);
394 else if (g_strcmp0 (schema, "https"))
395 (*notusdata)->port = 443;
396 else
397 (*notusdata)->port = 80;
398
399 (*notusdata)->schema = g_strdup (help_tolower (schema));
400 if (g_strrstr ((*notusdata)->schema, "https"))
401 {
402 (*notusdata)->tls = 1;
403 (*notusdata)->http_version = g_strdup ("2");
404 (*notusdata)->alpn = g_strdup ("h2");
405 }
406 else if (g_strrstr ((*notusdata)->schema, "http"))
407 {
408 (*notusdata)->tls = 0;
409 (*notusdata)->http_version = g_strdup ("1.1");
410 (*notusdata)->alpn = g_strdup ("http/1.1");
411 }
412 else
413 {
414 g_warning ("%s: Invalid openvasd server schema", (*notusdata)->server);
415 curl_url_cleanup (h);
416 curl_free (schema);
417 curl_free (host);
418 curl_free (port);
419 return -1;
420 }
421
422 curl_url_cleanup (h);
423 curl_free (schema);
424 curl_free (host);
425 curl_free (port);
426
427 return 0;
428}
429
433{
434 UNKNOWN, // Unknown
435 RANGE, // Range of version which fixed the package
436 SINGLE, // A single version with a specifier (gt or lt)
437};
438
442{
443 char *version; // a version
444 char *specifier; // a lt or gt specifier
445};
447
451{
452 char *start; // <= the version
453 char *stop; // >= the version
454};
456
460{
461 char *pkg_name; // package name
462 char *install_version; // installed version of the vulnerable package
463 enum fixed_type type; // fixed version type: range or single
464 union
465 {
466 version_range_t *range; // range of vulnerable versions
467 fixed_version_t *version; // version and specifier for the fixed versions
468 };
469};
470
472
476{
477 char *oid; // Advisory OID
478 vuln_pkg_t *pkgs[100]; // list of vulnerable packages, installed version and
479 // fixed versions
480 size_t count; // Count of vulnerable packages this adivsory has
481};
482
483typedef struct advisory advisory_t;
484
488{
490 size_t count;
491 size_t max_size;
492};
494
500static advisories_t *
502{
503 advisories_t *advisories_list = g_malloc0 (sizeof (advisories_t));
504 advisories_list->max_size = 100;
505 advisories_list->advisories =
506 g_malloc0_n (advisories_list->max_size, sizeof (advisory_t));
507
508 return advisories_list;
509}
510
518static void
520{
521 // Reallocate more memory if the list is full
522 if (advisories_list->count == advisories_list->max_size)
523 {
524 advisories_list->max_size *= 2;
525 advisories_list->advisories =
526 g_realloc_n (advisories_list->advisories, advisories_list->max_size,
527 sizeof (advisory_t));
528 memset (advisories_list->advisories + advisories_list->count, '\0',
529 (advisories_list->max_size - advisories_list->count)
530 * sizeof (advisory_t *));
531 }
532 advisories_list->advisories[advisories_list->count] = advisory;
533 advisories_list->count++;
534}
535
542static advisory_t *
544{
545 advisory_t *adv = NULL;
546 adv = g_malloc0 (sizeof (advisory_t));
547 adv->oid = g_strdup (oid);
548 adv->count = 0;
549 return adv;
550}
551
560static void
562{
563 if (adv->count == 100)
564 {
565 g_warning ("%s: Failed adding new vulnerable package to the advisory %s. "
566 "No more free slots",
567 __func__, adv->oid);
568 return;
569 }
570
571 adv->pkgs[adv->count] = vuln;
572 adv->count++;
573}
574
580static void
582{
583 if (advisory == NULL)
584 return;
585
586 g_free (advisory->oid);
587 for (size_t i = 0; i < advisory->count; i++)
588 {
589 if (advisory->pkgs[i] != NULL)
590 {
591 g_free (advisory->pkgs[i]->pkg_name);
592 g_free (advisory->pkgs[i]->install_version);
593 if (advisory->pkgs[i]->type == RANGE)
594 {
595 g_free (advisory->pkgs[i]->range->start);
596 g_free (advisory->pkgs[i]->range->stop);
597 }
598 else if (advisory->pkgs[i]->type == SINGLE)
599 {
600 g_free (advisory->pkgs[i]->version->version);
601 g_free (advisory->pkgs[i]->version->specifier);
602 }
603 }
604 }
605 advisory = NULL;
606}
607
613static void
615{
616 if (advisories == NULL)
617 return;
618
619 for (size_t i = 0; i < advisories->count; i++)
621 advisories = NULL;
622}
623
638static vuln_pkg_t *
639vulnerable_pkg_new (const char *pkg_name, const char *install_version,
640 enum fixed_type type, char *item1, char *item2)
641{
642 vuln_pkg_t *vuln = NULL;
643 version_range_t *range = NULL;
644 fixed_version_t *fixed_ver = NULL;
645
646 vuln = g_malloc0 (sizeof (vuln_pkg_t));
647 vuln->pkg_name = g_strdup (pkg_name);
648 vuln->install_version = g_strdup (install_version);
649 vuln->type = type;
650 if (type == RANGE)
651 {
652 range = g_malloc0 (sizeof (range_t));
653 range->start = g_strdup (item1);
654 range->stop = g_strdup (item2);
655 vuln->range = range;
656 }
657 else
658 {
659 fixed_ver = g_malloc0 (sizeof (fixed_version_t));
660 fixed_ver->version = g_strdup (item1);
661 fixed_ver->specifier = g_strdup (item2);
662 vuln->version = fixed_ver;
663 }
664
665 return vuln;
666}
667
680static advisories_t *
681process_notus_response (const gchar *resp, const size_t len)
682{
683 JsonParser *parser = NULL;
684 JsonReader *reader = NULL;
685 GError *err = NULL;
686
688
689 parser = json_parser_new ();
690 if (!json_parser_load_from_data (parser, resp, len, &err))
691 {
692 g_message ("Error parsing");
693 }
694
695 reader = json_reader_new (json_parser_get_root (parser));
696
697 if (!json_reader_is_object (reader))
698 {
699 g_debug ("It is not an object");
700 goto cleanup_advisories;
701 }
702
703 char **members = json_reader_list_members (reader);
704
705 for (int i = 0; members[i]; i++)
706 {
708
709 if (!json_reader_read_member (reader, members[i]))
710 {
711 g_debug ("No member oid");
712 goto cleanup_advisories;
713 }
714 if (!json_reader_is_array (reader))
715 {
716 g_debug ("Is not an array");
717 goto cleanup_advisories;
718 }
719
720 advisory = advisory_new (g_strdup (members[i]));
721
722 int count_pkgs = json_reader_count_elements (reader);
723 g_debug ("There are %d packages for advisory %s", count_pkgs, members[i]);
724 for (int j = 0; j < count_pkgs; j++)
725 {
726 vuln_pkg_t *pkg = NULL;
727 char *name = NULL;
728 char *installed_version = NULL;
729 char *start = NULL;
730 char *stop = NULL;
731 char *version = NULL;
732 char *specifier = NULL;
733 enum fixed_type type = UNKNOWN;
734
735 json_reader_read_element (reader, j);
736 if (!json_reader_is_object (reader))
737 {
738 g_warning ("%s: Package %d of advisory %s is not an object",
739 __func__, j, members[i]);
741 goto cleanup_advisories;
742 }
743
744 json_reader_read_member (reader, "name");
745 name = g_strdup (json_reader_get_string_value (reader));
746 json_reader_end_member (reader);
747 g_debug ("name: %s", name);
748
749 json_reader_read_member (reader, "installed_version");
750 installed_version = g_strdup (json_reader_get_string_value (reader));
751 json_reader_end_member (reader);
752 g_debug ("installed_version: %s", installed_version);
753
754 json_reader_read_member (reader, "fixed_version");
755 g_debug ("Fixed_version has %d members",
756 json_reader_count_members (reader));
757
758 // Version Range
759 json_reader_read_member (reader, "start");
760 start = g_strdup (json_reader_get_string_value (reader));
761 json_reader_end_member (reader);
762 json_reader_read_member (reader, "end");
763 stop = g_strdup (json_reader_get_string_value (reader));
764 json_reader_end_member (reader);
765 g_debug ("start %s, end: %s", start, stop);
766
767 // version and specifier
768 json_reader_read_member (reader, "version");
769 version = g_strdup (json_reader_get_string_value (reader));
770 json_reader_end_member (reader);
771 json_reader_read_member (reader, "specifier");
772 specifier = g_strdup (json_reader_get_string_value (reader));
773 json_reader_end_member (reader);
774 g_debug ("version %s, specifier: %s", version, specifier);
775
776 // end read fixes version member
777 json_reader_end_member (reader);
778
779 // end package element
780 json_reader_end_element (reader);
781
782 char *item1 = NULL, *item2 = NULL;
783 if (start && stop)
784 {
785 type = RANGE;
786 item1 = start;
787 item2 = stop;
788 }
789 else if (version && specifier)
790 {
791 type = SINGLE;
792 item1 = version;
793 item2 = specifier;
794 }
795 else
796 {
797 g_warning ("%s: Error parsing json element", __func__);
798 g_free (name);
799 g_free (installed_version);
800 g_free (item1);
801 g_free (item2);
803 goto cleanup_advisories;
804 }
805
806 pkg =
807 vulnerable_pkg_new (name, installed_version, type, item1, item2);
808 g_free (name);
809 g_free (installed_version);
810 g_free (item1);
811 g_free (item2);
812
814 }
815 // end advisory
816 json_reader_end_member (reader);
818 }
819
820cleanup_advisories:
821 if (reader)
822 g_object_unref (reader);
823 g_object_unref (parser);
824
825 return advisories;
826}
827
830struct string
831{
832 char *ptr;
833 size_t len;
834};
835
840static void
841init_string (struct string *s)
842{
843 s->len = 0;
844 s->ptr = g_malloc0 (s->len + 1);
845 if (s->ptr == NULL)
846 {
847 g_warning ("%s: Error allocating memory for response", __func__);
848 return;
849 }
850 s->ptr[0] = '\0';
851}
852
858static size_t
859response_callback_fn (void *ptr, size_t size, size_t nmemb, void *struct_string)
860{
861 struct string *s = struct_string;
862 size_t new_len = s->len + size * nmemb;
863 char *ptr_aux = g_realloc (s->ptr, new_len + 1);
864 s->ptr = ptr_aux;
865 if (s->ptr == NULL)
866 {
867 g_warning ("%s: Error allocating memory for response", __func__);
868 return 0; // no memory left
869 }
870 memcpy (s->ptr + s->len, ptr, size * nmemb);
871 s->ptr[new_len] = '\0';
872 s->len = new_len;
873
874 return size * nmemb;
875}
876
887static long
888send_request (notus_info_t notusdata, const char *os, const char *pkg_list,
889 char **response)
890{
891 CURL *curl;
892 GString *url = NULL;
893 long http_code = -1;
894 struct string resp;
895 struct curl_slist *customheader = NULL;
896 char *os_aux;
897 GString *xapikey = NULL;
898
899 if ((curl = curl_easy_init ()) == NULL)
900 {
901 g_warning ("Not possible to initialize curl library");
902 return http_code;
903 }
904
905 url = g_string_new (notusdata->server);
906 g_string_append (url, "/notus/");
907
908 //
909 os_aux = help_tolower (g_strdup (os));
910 for (size_t i = 0; i < strlen (os_aux); i++)
911 {
912 if (os_aux[i] == ' ')
913 os_aux[i] = '_';
914 }
915
916 g_string_append (url, os_aux);
917 g_free (os_aux);
918
919 g_debug ("%s: URL: %s", __func__, url->str);
920 // Set URL
921 if (curl_easy_setopt (curl, CURLOPT_URL, g_strdup (url->str)) != CURLE_OK)
922 {
923 g_warning ("Not possible to set the URL");
924 curl_easy_cleanup (curl);
925 return http_code;
926 }
927 g_string_free (url, TRUE);
928
929 // Accept an insecure connection. Don't verify the server certificate
930 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
931 curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
932
933 // Set API KEY
934 if (prefs_get ("x-apikey"))
935 {
936 xapikey = g_string_new ("X-APIKEY: ");
937 g_string_append (xapikey, prefs_get ("x-apikey"));
938 customheader = curl_slist_append (customheader, g_strdup (xapikey->str));
939 g_string_free (xapikey, TRUE);
940 }
941 // SET Content type
942 customheader =
943 curl_slist_append (customheader, "Content-Type: application/json");
944 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, customheader);
945 // Set body
946 curl_easy_setopt (curl, CURLOPT_POSTFIELDS, pkg_list);
947 curl_easy_setopt (curl, CURLOPT_POSTFIELDSIZE, strlen (pkg_list));
948
949 // Init the struct where the response is stored and set the callback function
950 init_string (&resp);
951 curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, response_callback_fn);
952 curl_easy_setopt (curl, CURLOPT_WRITEDATA, &resp);
953
954 int ret = CURLE_OK;
955 if ((ret = curl_easy_perform (curl)) != CURLE_OK)
956 {
957 g_warning ("%s: Error sending request: %d", __func__, ret);
958 curl_easy_cleanup (curl);
959 g_free (resp.ptr);
960 return http_code;
961 }
962
963 curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
964
965 curl_easy_cleanup (curl);
966 g_debug ("Server response %s", resp.ptr);
967 *response = g_strdup (resp.ptr);
968 g_free (resp.ptr);
969 // already free()'ed with curl_easy_cleanup().
970
971 return http_code;
972}
973
982static char *
983notus_get_response (const char *pkg_list, const char *os)
984{
985 const char *server = NULL;
986 char *json_pkglist;
987 char *response = NULL;
988 notus_info_t notusdata;
989 long ret;
990
991 // Parse the server and get the port, host, schema
992 // and necessary information to build the message
993 server = prefs_get ("openvasd_server");
994 notusdata = init_notus_info (server);
995
996 if (parse_server (&notusdata) < 0)
997 {
998 free_notus_info (notusdata);
999 return NULL;
1000 }
1001
1002 // Convert the packge list string into a string containing json
1003 // array of packages
1004 if ((json_pkglist = make_package_list_as_json_str (pkg_list)) == NULL)
1005 {
1006 free_notus_info (notusdata);
1007 return NULL;
1008 }
1009
1010 ret = send_request (notusdata, os, json_pkglist, &response);
1011 if (ret != 200)
1012 g_warning ("%ld: Error sending request to openvasd: %s", ret, response);
1013
1014 free_notus_info (notusdata);
1015 g_free (json_pkglist);
1016
1017 return response;
1018}
1019
1030static int
1031call_rs_notus (const char *ip_str, const char *hostname, const char *pkg_list,
1032 const char *os)
1033{
1034 char *body = NULL;
1035 advisories_t *advisories = NULL;
1036 int res_count = 0;
1037 if ((body = notus_get_response (pkg_list, os)) == NULL)
1038 return -1;
1039
1040 advisories = process_notus_response (body, strlen (body));
1041
1042 // Process the advisories, generate results and store them in the kb
1043 for (size_t i = 0; i < advisories->count; i++)
1044 {
1046 gchar *buffer;
1047 GString *result = g_string_new (NULL);
1048 for (size_t j = 0; j < advisory->count; j++)
1049 {
1050 vuln_pkg_t *pkg = advisory->pkgs[j];
1051 GString *res = g_string_new (NULL);
1052
1053 if (pkg->type == RANGE)
1054 {
1055 g_string_printf (res,
1056 "\nVulnerable package: %s\n"
1057 "Installed version: %s-%s\n"
1058 "Fixed version: <=%s-%s\n"
1059 "Fixed version: >=%s-%s\n",
1060 pkg->pkg_name, pkg->pkg_name,
1061 pkg->install_version, pkg->pkg_name,
1062 pkg->range->start, pkg->pkg_name,
1063 pkg->range->stop);
1064 }
1065 else if (pkg->type == SINGLE)
1066 {
1067 int spec_len = 8 - (int) strlen (pkg->version->specifier);
1068 g_string_printf (res,
1069 "\nVulnerable package:%*s%s\n"
1070 "Installed version:%*s%s-%s\n"
1071 "Fixed version:%*s%s%s-%s\n",
1072 3, "", pkg->pkg_name, 4, "", pkg->pkg_name,
1073 pkg->install_version, spec_len, "",
1074 pkg->version->specifier, pkg->pkg_name,
1075 pkg->version->version);
1076 }
1077 else
1078 {
1079 g_warning ("%s: Unknown fixed version type for advisory %s",
1080 __func__, advisory->oid);
1081 g_string_free (result, TRUE);
1083 return -1;
1084 }
1085 g_string_append (result, res->str);
1086 g_string_free (res, TRUE);
1087 }
1088
1089 // type|||IP|||HOSTNAME|||package|||OID|||the result message|||URI
1090 buffer = g_strdup_printf ("%s|||%s|||%s|||%s|||%s|||%s|||%s", "ALARM",
1091 ip_str, hostname ? hostname : " ", "package",
1092 advisory->oid, result->str, "");
1093 g_string_free (result, TRUE);
1094 kb_item_push_str_with_main_kb_check (get_main_kb (), "internal/results",
1095 buffer);
1096 res_count++;
1097 g_free (buffer);
1098 }
1099
1101 return res_count;
1102}
1103
1104#endif // End RSNOTUS
1105
1121int
1122run_table_driven_lsc (const char *scan_id, const char *ip_str,
1123 const char *hostname, const char *package_list,
1124 const char *os_release)
1125{
1126 int err = 0;
1127 if (!os_release || !package_list)
1128 return 0;
1129
1130 if (prefs_get ("openvasd_server"))
1131 {
1132 g_message ("Running Notus for %s via openvasd", ip_str);
1133 err = call_rs_notus (ip_str, hostname, package_list, os_release);
1134
1135 return err;
1136 }
1137 else
1138 {
1139 gchar *json_str;
1140 gchar *topic;
1141 gchar *payload;
1142 gchar *status = NULL;
1143 int topic_len;
1144 int payload_len;
1145
1146 // Subscribe to status topic
1147 err = mqtt_subscribe ("scanner/status");
1148 if (err)
1149 {
1150 g_warning ("%s: Error starting lsc. Unable to subscribe", __func__);
1151 return -1;
1152 }
1153 /* Get the OS release. TODO: have a list with supported OS. */
1154
1156 os_release, package_list);
1157
1158 // Run table driven lsc
1159 if (json_str == NULL)
1160 return -1;
1161
1162 g_message ("Running Notus for %s", ip_str);
1163 err = mqtt_publish ("scanner/package/cmd/notus", json_str);
1164 if (err)
1165 {
1166 g_warning ("%s: Error publishing message for Notus.", __func__);
1167 g_free (json_str);
1168 return -1;
1169 }
1170
1171 g_free (json_str);
1172
1173 // Wait for Notus scanner to start or interrupt
1174 while (!status)
1175 {
1176 err = mqtt_retrieve_message (&topic, &topic_len, &payload,
1177 &payload_len, 60000);
1178 if (err == -1 || err == 1)
1179 {
1180 g_warning ("%s: Unable to retrieve status message from notus. %s",
1181 __func__, err == 1 ? "Timeout after 60 s." : "");
1182 return -1;
1183 }
1184
1185 // Get status if it belongs to corresponding scan and host
1186 // Else wait for next status message
1188 scan_id, ip_str, payload, payload_len);
1189
1190 g_free (topic);
1191 g_free (payload);
1192 }
1193 // If started wait for it to finish or interrupt
1194 if (!g_strcmp0 (status, "running"))
1195 {
1196 g_debug ("%s: table driven LSC with scan id %s successfully started "
1197 "for host %s",
1198 __func__, scan_id, ip_str);
1199 g_free (status);
1200 status = NULL;
1201 while (!status)
1202 {
1203 err = mqtt_retrieve_message (&topic, &topic_len, &payload,
1204 &payload_len, 60000);
1205 if (err == -1)
1206 {
1207 g_warning (
1208 "%s: Unable to retrieve status message from notus.",
1209 __func__);
1210 return -1;
1211 }
1212 if (err == 1)
1213 {
1214 g_warning (
1215 "%s: Unablet to retrieve message. Timeout after 60s.",
1216 __func__);
1217 return -1;
1218 }
1219
1221 scan_id, ip_str, payload, payload_len);
1222 g_free (topic);
1223 g_free (payload);
1224 }
1225 }
1226 else
1227 {
1228 g_warning ("%s: Unable to start lsc. Got status: %s", __func__,
1229 status);
1230 g_free (status);
1231 return -1;
1232 }
1233
1234 if (g_strcmp0 (status, "finished"))
1235 {
1236 g_warning (
1237 "%s: table driven lsc with scan id %s did not finish successfully "
1238 "for host %s. Last status was %s",
1239 __func__, scan_id, ip_str, status);
1240 err = -1;
1241 }
1242 else
1243 g_debug ("%s: table driven lsc with scan id %s successfully finished "
1244 "for host %s",
1245 __func__, scan_id, ip_str);
1246 g_free (status);
1247 }
1248 return err;
1249}
kb_t get_main_kb(void)
gets the main_kb. @description returns the previously set main_kb; when asserts are enabled it will a...
Definition kb_cache.c:41
Header file to cache main_kb.
const char * oid
Definition nasl_builtin_find_service.c:51
u_char * payload
Definition nasl_frame_forgery.c:1
const char * name
Definition nasl_init.c:436
uint8_t len
Definition nasl_packet_forgery.c:1
const char * hostname
Definition pluginlaunch.c:68
int kb_item_push_str_with_main_kb_check(kb_t kb, const char *name, const char *value)
Check if the current kb corresponds to the original scanid, if it matches it kb_item_push_str....
Definition plugutils.c:532
Header file for module plugutils.
const char * scan_id
Definition scan_id.c:10
Definition table_driven_lsc.c:488
size_t max_size
Definition table_driven_lsc.c:491
advisory_t ** advisories
Definition table_driven_lsc.c:489
size_t count
Definition table_driven_lsc.c:490
Definition table_driven_lsc.c:476
size_t count
Definition table_driven_lsc.c:480
char * oid
Definition table_driven_lsc.c:477
vuln_pkg_t * pkgs[100]
Definition table_driven_lsc.c:478
Fixed version.
Definition table_driven_lsc.c:442
char * specifier
Definition table_driven_lsc.c:444
char * version
Definition table_driven_lsc.c:443
Host information, implemented as doubly linked list.
Definition hosts.c:37
Struct to hold necessary information to call and run notus.
Definition table_driven_lsc.c:242
int tls
Definition table_driven_lsc.c:249
int port
Definition table_driven_lsc.c:248
char * host
Definition table_driven_lsc.c:245
char * alpn
Definition table_driven_lsc.c:246
char * schema
Definition table_driven_lsc.c:244
char * server
Definition table_driven_lsc.c:243
char * http_version
Definition table_driven_lsc.c:247
Define a string struct for storing the response.
Definition table_driven_lsc.c:831
size_t len
Definition table_driven_lsc.c:833
char * ptr
Definition table_driven_lsc.c:832
Specify a version range.
Definition table_driven_lsc.c:451
char * stop
Definition table_driven_lsc.c:453
char * start
Definition table_driven_lsc.c:452
Define a vulnerable package.
Definition table_driven_lsc.c:460
fixed_version_t * version
Definition table_driven_lsc.c:467
enum fixed_type type
Definition table_driven_lsc.c:463
version_range_t * range
Definition table_driven_lsc.c:466
char * pkg_name
Definition table_driven_lsc.c:461
char * install_version
Definition table_driven_lsc.c:462
static gchar * make_table_driven_lsc_info_json_str(const char *scan_id, const char *ip_str, const char *hostname, const char *os_release, const char *package_list)
Build a json object with data necessary to start a table drive LSC.
Definition table_driven_lsc.c:102
static JsonBuilder * add_packages_str_to_list(JsonBuilder *builder, const gchar *packages)
Split the package list string and creates a json array.
Definition table_driven_lsc.c:65
static advisory_t * advisory_new(char *oid)
Initialize a new adivisory.
Definition table_driven_lsc.c:543
struct advisories advisories_t
Definition table_driven_lsc.c:493
static notus_info_t init_notus_info(const char *server)
Initialize a notus info struct and stores the server URL.
Definition table_driven_lsc.c:261
static void free_notus_info(notus_info_t notusdata)
Free notus info structure.
Definition table_driven_lsc.c:276
int run_table_driven_lsc(const char *scan_id, const char *ip_str, const char *hostname, const char *package_list, const char *os_release)
Publish the necessary data to start a Table driven LSC scan.
Definition table_driven_lsc.c:1122
fixed_type
Fixed version format.
Definition table_driven_lsc.c:433
@ SINGLE
Definition table_driven_lsc.c:436
@ RANGE
Definition table_driven_lsc.c:435
@ UNKNOWN
Definition table_driven_lsc.c:434
static long send_request(notus_info_t notusdata, const char *os, const char *pkg_list, char **response)
Send a request to the server.
Definition table_driven_lsc.c:888
static advisories_t * process_notus_response(const gchar *resp, const size_t len)
Process a json object which contains advisories and vulnerable packages.
Definition table_driven_lsc.c:681
static int parse_server(notus_info_t *notusdata)
Parse the server URL.
Definition table_driven_lsc.c:359
static void advisories_add(advisories_t *advisories_list, advisory_t *advisory)
Initialize a new adivisories struct with 100 slots.
Definition table_driven_lsc.c:519
struct advisory advisory_t
Definition table_driven_lsc.c:483
static advisories_t * advisories_new()
Initialize a new adivisories struct with 100 slots.
Definition table_driven_lsc.c:501
static gchar * make_package_list_as_json_str(const char *packages)
Build a json array from the package list to start a table drive LSC.
Definition table_driven_lsc.c:312
int lsc_has_run(void)
Get lsc_flag value.
Definition table_driven_lsc.c:48
static gchar * get_status_of_table_driven_lsc_from_json(const char *scan_id, const char *host_ip, const char *json, int len)
Get the status of table driven lsc from json object.
Definition table_driven_lsc.c:175
struct fixed_version fixed_version_t
Definition table_driven_lsc.c:446
static char * notus_get_response(const char *pkg_list, const char *os)
Sent the installed package list and OS to notus.
Definition table_driven_lsc.c:983
static size_t response_callback_fn(void *ptr, size_t size, size_t nmemb, void *struct_string)
Call back function to stored the response.
Definition table_driven_lsc.c:859
static vuln_pkg_t * vulnerable_pkg_new(const char *pkg_name, const char *install_version, enum fixed_type type, char *item1, char *item2)
Creates a new Vulnerable packages which belongs to an advisory.
Definition table_driven_lsc.c:639
static void advisory_free(advisory_t *advisory)
Free()'s an advisory.
Definition table_driven_lsc.c:581
struct vulnerable_pkg vuln_pkg_t
Definition table_driven_lsc.c:471
static int call_rs_notus(const char *ip_str, const char *hostname, const char *pkg_list, const char *os)
Call notus and stores the results.
Definition table_driven_lsc.c:1031
struct version_range version_range_t
Definition table_driven_lsc.c:455
static void advisories_free(advisories_t *advisories)
Free()'s an advisories.
Definition table_driven_lsc.c:614
static char * help_tolower(char *s)
helper function to lower case
Definition table_driven_lsc.c:295
struct notus_info * notus_info_t
Definition table_driven_lsc.c:252
void set_lsc_flag(void)
Set lsc_flag to 1.
Definition table_driven_lsc.c:40
static int lsc_flag
LSC ran or didn't 0 didn't run. 1 ran.
Definition table_driven_lsc.c:35
static void init_string(struct string *s)
Initialize the string struct to hold the response.
Definition table_driven_lsc.c:841
static void advisory_add_vuln_pkg(advisory_t *adv, vuln_pkg_t *vuln)
Add a new vulnerability to the advisory.
Definition table_driven_lsc.c:561