-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathopenvasd.c
2334 lines (2047 loc) · 63.6 KB
/
openvasd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* SPDX-FileCopyrightText: 2024 Greenbone AG
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/**
* @file
* @brief API for Openvas Daemon communication.
*/
#include "openvasd.h"
#include "../base/array.h"
#include "../base/networking.h"
#include <cjson/cJSON.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <curl/multi.h>
#include <netinet/in.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#undef G_LOG_DOMAIN
/**
* @brief GLib log domain.
*/
#define G_LOG_DOMAIN "libgvm ovd"
#define RESP_CODE_ERR -1
#define RESP_CODE_OK 0
/**
* @brief Wrapps a CURLM * handler and the custom header.
*/
typedef struct openvasd_curlm
{
CURLM *h;
struct curl_slist *customheader;
} openvasd_curlm_t;
/** @brief Define a string struct for storing the response
* and the curl handler
*/
struct openvasd_string
{
gchar *ptr;
size_t len;
openvasd_curlm_t *curl_hnd;
};
typedef struct openvasd_string *openvasd_vt_stream_t;
/**
* @brief Struct holding the data for connecting with Openvasd.
*/
struct openvasd_connector
{
gchar *ca_cert; /**< Path to the directory holding the CA certificate. */
gchar *cert; /**< Client certificate. */
gchar *key; /**< Client key. */
gchar *apikey; /**< API key for authentication. */
gchar *server; /**< original openvasd server URL. */
gchar *host; /**< server hostname. */
gchar *scan_id; /**< Scan ID. */
int port; /**< server port. */
openvasd_vt_stream_t stream_resp; /** For response */
};
/**
* @brief Struct holding options for Openvasd parameters.
*/
struct openvasd_param
{
gchar *id; /**< Parameter id. */
gchar *name; /**< Parameter name. */
gchar *defval; /**< Default value. */
gchar *description; /**< Description. */
gchar *type; /**< Parameter type. */
int mandatory; /**< If mandatory. */
};
/**
* @brief Struct credential information for Openvasd.
*/
struct openvasd_credential
{
gchar *type; /**< Credential type */
gchar *service; /**< Service the credential is for */
gchar *port; /**< Port the credential is for */
GHashTable *auth_data; /**< Authentication data (username, password, etc.)*/
};
/**
* @brief Struct holding target information.
*/
struct openvasd_target
{
gchar *scan_id; /** Scan ID */
GSList *credentials; /** Credentials to use in the scan */
gchar *exclude_hosts; /** String defining one or many hosts to exclude */
gchar *hosts; /** String defining one or many hosts to scan */
gchar *ports; /** String defining the ports to scan */
gchar *finished_hosts; /** String defining hosts to exclude as finished */
gboolean icmp; /** Alive test method icmp */
gboolean tcp_syn; /** Alive test method tcp_syn */
gboolean tcp_ack; /** Alive test method tcp_ack */
gboolean arp; /** Alive test method arp */
gboolean consider_alive; /** Alive test method consider alive */
int reverse_lookup_unify; /** Value defining reverse_lookup_unify opt */
int reverse_lookup_only; /** Value defining reverse_lookup_only opt */
};
/**
* @brief Struct holding vt information
*/
struct openvasd_vt_single
{
gchar *vt_id;
GHashTable *vt_values;
};
/**
* @brief Request methods
*/
enum openvas_request_method
{
POST,
GET,
HEAD,
DELETE,
};
typedef enum openvas_request_method openvasd_req_method_t;
/**
* @brief Allocate openvasd curl handler
*
* @return Openvasd curl handler.
*/
static openvasd_curlm_t *
openvasd_curlm_handler_new (void)
{
return (openvasd_curlm_t *) g_malloc0 (sizeof (struct openvasd_curlm));
}
/**
* @brief Cleanup an openvasd curl handler
*
* @param h Openvasd curl handler to clean
*/
static void
openvasd_curlm_handler_close (openvasd_curlm_t *h)
{
int queued = 0;
/* when an easy handle has completed, remove it */
CURLMsg *msg = curl_multi_info_read (h->h, &queued);
if (msg)
{
if (msg->msg == CURLMSG_DONE)
{
curl_multi_remove_handle (h->h, msg->easy_handle);
curl_easy_cleanup (msg->easy_handle);
curl_slist_free_all (h->customheader);
curl_multi_cleanup (h->h);
return;
}
g_warning ("%s: Not possible to clean up the curl handler", __func__);
}
}
/** @brief Allocate the vt stream struct to hold the response
* and the curlm handler
*
* @return The vt stream struct. Must be free with openvasd_vt_stream_new().
*/
static openvasd_vt_stream_t
openvasd_vt_stream_new (void)
{
openvasd_vt_stream_t s;
s = g_malloc0 (sizeof (struct openvasd_string));
s->len = 0;
s->ptr = g_malloc0 (s->len + 1);
s->curl_hnd = openvasd_curlm_handler_new ();
return s;
}
/** @brief Cleanup the string struct to hold the response and the
* curl multiperform handler
*
* @param s The string struct to be freed
*/
static void
openvasd_vt_stream_free (openvasd_vt_stream_t s)
{
if (s == NULL)
return;
g_free (s->ptr);
if (s->curl_hnd)
openvasd_curlm_handler_close (s->curl_hnd);
g_free (s);
}
/** @brief Reinitialize the string struct to hold the response
*
* @param s The string struct to be reset
*/
static void
openvasd_vt_stream_reset (openvasd_vt_stream_t s)
{
if (s)
{
g_free (s->ptr);
s->len = 0;
s->ptr = g_malloc0 (s->len + 1);
}
}
/** @brief Initialize an openvasd connector.
*
* @return An an openvasd connector struct. It must be freed
* with openvasd_connector_free()
*/
openvasd_connector_t
openvasd_connector_new (void)
{
openvasd_connector_t connector;
openvasd_vt_stream_t stream;
connector = g_malloc0 (sizeof (struct openvasd_connector));
stream = openvasd_vt_stream_new ();
connector->stream_resp = stream;
return connector;
}
/** @brief Build a openvasd connector
*
* Receive option name and value to build the openvasd connector
*
* @param conn struct holding the openvasd connector information
* @param opt option to set
* @param val value to set
*
* @return Return OK on success, otherwise error;
*/
openvasd_error_t
openvasd_connector_builder (openvasd_connector_t conn, openvasd_conn_opt_t opt,
const void *val)
{
if (conn == NULL)
conn = openvasd_connector_new ();
if (opt < OPENVASD_CA_CERT || opt > OPENVASD_PORT)
return OPENVASD_INVALID_OPT;
if (val == NULL)
return OPENVASD_INVALID_VALUE;
switch (opt)
{
case OPENVASD_CA_CERT:
conn->ca_cert = g_strdup ((char *) val);
break;
case OPENVASD_CERT:
conn->cert = g_strdup ((char *) val);
break;
case OPENVASD_KEY:
conn->key = g_strdup ((char *) val);
break;
case OPENVASD_API_KEY:
conn->apikey = g_strdup ((char *) val);
break;
case OPENVASD_SERVER:
conn->server = g_strdup ((char *) val);
break;
case OPENVASD_HOST:
conn->host = g_strdup ((char *) val);
break;
case OPENVASD_SCAN_ID:
conn->scan_id = g_strdup ((const gchar *) val);
break;
case OPENVASD_PORT:
default:
conn->port = *((int *) val);
break;
};
return OPENVASD_OK;
}
/** @brief Build a openvasd connector
*
* Receive option name and value to build the openvasd connector
*
* @param conn struct holding the openvasd connector information
*
* @return Return OPENVASD_OK
*/
openvasd_error_t
openvasd_connector_free (openvasd_connector_t conn)
{
if (conn == NULL)
return OPENVASD_OK;
g_free (conn->ca_cert);
g_free (conn->cert);
g_free (conn->key);
g_free (conn->apikey);
g_free (conn->server);
g_free (conn->host);
g_free (conn->scan_id);
openvasd_vt_stream_free (conn->stream_resp);
g_free (conn);
conn = NULL;
return OPENVASD_OK;
}
/**
* @brief Free an openvasd response struct
*
* @param resp Response to be freed
*/
void
openvasd_response_cleanup (openvasd_resp_t resp)
{
if (resp == NULL)
return;
g_free (resp->body);
g_free (resp->header);
g_free (resp);
resp = NULL;
}
/** @brief Call back function to stored the response.
*
* The function signature is the necessary to work with
* libcurl. It stores the response in s. It reallocate memory if necessary.
*/
static size_t
response_callback_fn (void *ptr, size_t size, size_t nmemb, void *struct_string)
{
openvasd_vt_stream_t s = struct_string;
size_t new_len = s->len + size * nmemb;
gchar *ptr_aux = g_realloc (s->ptr, new_len + 1);
s->ptr = ptr_aux;
memcpy (s->ptr + s->len, ptr, size * nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size * nmemb;
}
static struct curl_slist *
init_customheader (const gchar *apikey, gboolean contenttype)
{
struct curl_slist *customheader = NULL;
struct curl_slist *temp = NULL;
// Set API KEY
if (apikey)
{
GString *xapikey;
xapikey = g_string_new ("X-API-KEY: ");
g_string_append (xapikey, apikey);
temp = curl_slist_append (customheader, xapikey->str);
if (!temp)
g_warning ("%s: Not possible to set API-KEY", __func__);
else
customheader = temp;
g_string_free (xapikey, TRUE);
}
// SET Content type
if (contenttype)
{
temp = curl_slist_append (customheader, "Content-Type: application/json");
if (!temp)
g_warning ("%s: Not possible to set Content-Type", __func__);
else
customheader = temp;
}
return customheader;
}
/** @brief Create a CURL handler
*
* @param conn struct holding the openvasd connector information
* @param method request method (e.g. GET)
* @param path Path to the resource (e.g. /vts)
* @param data String containing the request body in json format (scan
* action, scan config)
* @param customheader A CURL slist with custom headers. It is set in the
* handler and must be free after use with curl_slist_free_all().
* @param resp Structure holding the body response, filled by the
* callback function
* @param err On error, this variable is filled with an error message
* in json format.
*
* @return a CURL handler, or NULL on error.
*/
static CURL *
handler (openvasd_connector_t conn, openvasd_req_method_t method, gchar *path,
gchar *data, struct curl_slist *customheader, gchar **err)
{
CURL *curl;
GString *url = NULL;
if (!conn)
{
*err = g_strdup ("{\"error\": \"Missing openvasd connector\"}");
g_warning ("%s: Missing openvasd connector", __func__);
return NULL;
}
if ((curl = curl_easy_init ()) == NULL)
{
*err =
g_strdup ("{\"error\": \"Not possible to initialize curl library\"}");
g_warning ("%s: Not possible to initialize curl library", __func__);
return NULL;
}
url = g_string_new (g_strdup (conn->server));
if (conn->port > 0 && conn->port < 65535)
{
char buf[6];
g_snprintf (buf, sizeof (buf), ":%d", conn->port);
g_string_append (url, buf);
}
if (path != NULL && path[0] != '\0')
g_string_append (url, path);
// Set URL
g_debug ("%s: URL: %s", __func__, url->str);
if (curl_easy_setopt (curl, CURLOPT_URL, url->str) != CURLE_OK)
{
g_string_free (url, TRUE);
g_warning ("%s: Not possible to set the URL", __func__);
curl_easy_cleanup (curl);
*err = g_strdup ("{\"error\": \"Not possible to set URL\"}");
return NULL;
}
g_string_free (url, TRUE);
// Server verification
if (conn->ca_cert != NULL)
{
struct curl_blob blob;
blob.data = conn->ca_cert;
blob.len = strlen (conn->ca_cert);
blob.flags = CURL_BLOB_COPY;
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 1L);
if (curl_easy_setopt (curl, CURLOPT_CAINFO_BLOB, &blob) != CURLE_OK)
{
g_warning ("%s: Not possible to set the CA certificate", __func__);
curl_easy_cleanup (curl);
*err =
g_strdup ("{\"error\": \"Not possible to set CA certificate\"}");
return NULL;
}
}
else
{
// Accept an insecure connection. Don't verify the server certificate
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt (curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2);
g_debug ("%s: Server certificate verification disabled.", __func__);
}
// Client certificate
if (conn->cert != NULL && conn->key != NULL)
{
struct curl_blob blob;
blob.data = conn->cert;
blob.len = strlen (conn->cert);
blob.flags = CURL_BLOB_COPY;
if (curl_easy_setopt (curl, CURLOPT_SSLCERT_BLOB, &blob) != CURLE_OK)
{
g_warning ("%s: Not possible to set the Client certificate",
__func__);
curl_easy_cleanup (curl);
*err = g_strdup (
"{\"error\": \"Not possible to set Client certificate\"}");
return NULL;
}
blob.data = conn->key;
blob.len = strlen (conn->key);
blob.flags = CURL_BLOB_COPY;
if (curl_easy_setopt (curl, CURLOPT_SSLKEY_BLOB, &blob) != CURLE_OK)
{
g_warning ("%s: Not possible to set the Client private key",
__func__);
curl_easy_cleanup (curl);
*err = g_strdup (
"{\"error\": \"Not possible to set Client private key\"}");
return NULL;
}
}
switch (method)
{
case POST:
if (data != NULL && data[0] != '\0')
{
// Set body
curl_easy_setopt (curl, CURLOPT_POSTFIELDS, data);
curl_easy_setopt (curl, CURLOPT_POSTFIELDSIZE, strlen (data));
}
break;
case GET:
curl_easy_setopt (curl, CURLOPT_HTTPGET, 1L);
break;
case DELETE:
curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, "DELETE");
break;
default:
curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, "HEAD");
break;
};
if (customheader != NULL)
curl_easy_setopt (curl, CURLOPT_HTTPHEADER, customheader);
// Init the struct where the response is stored and set the callback function
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, response_callback_fn);
curl_easy_setopt (curl, CURLOPT_WRITEDATA, conn->stream_resp);
return curl;
}
/** @brief Send request
*
* @param curl The CURL handler to perform an request.
* @param header_name If this field is set, is looked in the header and
* its value is returned inside the response.
* @param response The response struct to be filled with the response
* code and the header value.
*
* @return Return struct containing the http response code and the response
* body. In case of error the struct is filled with code RESP_CODE_ERR (-1) and
* a message. NULL on memory related error. Response must be free()'ed by the
* caller with openvasd_response_free()
*/
static openvasd_resp_t
openvasd_send_request (CURL *curl, const gchar *header_name,
openvasd_resp_t response)
{
long http_code = RESP_CODE_ERR;
int ret = CURLE_OK;
if ((ret = curl_easy_perform (curl)) != CURLE_OK)
{
g_warning ("%s: Error sending request: %d", __func__, ret);
curl_easy_cleanup (curl);
response->code = http_code;
response->body = g_strdup ("{\"error\": \"Error sending request\"}");
return response;
}
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
if (header_name)
{
struct curl_header *hname;
curl_easy_header (curl, header_name, 0, CURLH_HEADER, -1, &hname);
response->header = g_strdup (hname->value);
}
curl_easy_cleanup (curl);
response->code = http_code;
return response;
}
/**
* @brief Request HEAD
*
* @param conn Connector struct with the data necessary for the connection
*
* @return Response containing the header information
*/
openvasd_resp_t
openvasd_get_version (openvasd_connector_t conn)
{
gchar *err = NULL;
CURL *hnd = NULL;
openvasd_resp_t response = NULL;
struct curl_slist *customheader = NULL;
response = g_malloc0 (sizeof (struct openvasd_response));
customheader = init_customheader (conn->apikey, FALSE);
hnd = handler (conn, HEAD, "/", NULL, customheader, &err);
if (hnd == NULL)
{
curl_slist_free_all (customheader);
response->code = RESP_CODE_ERR;
response->body = err;
openvasd_reset_vt_stream (conn);
return response;
}
openvasd_send_request (hnd, NULL, response);
curl_slist_free_all (customheader);
if (response->code != RESP_CODE_ERR)
response->body = g_strdup (openvasd_vt_stream_str (conn));
openvasd_reset_vt_stream (conn);
return response;
}
/**
* @brief Initialized an curl multiperform handler which allows fetch feed
* metadata chunk by chunk.
*
* @param conn Connector struct with the data necessary for the connection
* @param mhnd The curl multiperform handler. It the caller doesn't provide
* it initialized, it will be initialized. The caller has to free it with
* openvasd_curlm_handler_close().
* @param resp The stringstream struct for the write callback function.
*
* @return The response.
*/
openvasd_resp_t
openvasd_get_vt_stream_init (openvasd_connector_t conn)
{
GString *path;
openvasd_resp_t response = NULL;
gchar *err = NULL;
CURL *hnd = NULL;
CURLM *h = NULL;
struct curl_slist *customheader = NULL;
response = g_malloc0 (sizeof (struct openvasd_response));
path = g_string_new ("/vts?information=1");
customheader = init_customheader (conn->apikey, FALSE);
hnd = handler (conn, GET, path->str, NULL, customheader, &err);
if (hnd == NULL)
{
curl_slist_free_all (customheader);
g_string_free (path, TRUE);
response->code = RESP_CODE_ERR;
response->body = err;
return response;
}
g_string_free (path, TRUE);
h = curl_multi_init ();
curl_multi_add_handle (h, hnd);
conn->stream_resp->curl_hnd->h = h;
conn->stream_resp->curl_hnd->customheader = customheader;
response->code = RESP_CODE_OK;
return response;
}
void
openvasd_reset_vt_stream (openvasd_connector_t conn)
{
openvasd_vt_stream_reset (conn->stream_resp);
}
gchar *
openvasd_vt_stream_str (openvasd_connector_t conn)
{
return conn->stream_resp->ptr;
}
size_t
openvasd_vt_stream_len (openvasd_connector_t conn)
{
return conn->stream_resp->len;
}
/**
* @brief Get a new feed metadata chunk.
*
* This function must be call until the
* return value is 0, meaning there is no more data to fetch.
*
* @param mhnd Curl multiperfom for requesting the feed metadata
*
* @return greather than 0 if the handler is still getting data. 0 if the
* transmision finished. -1 on error
*/
int
openvasd_get_vt_stream (openvasd_connector_t conn)
{
static int running = 0;
CURLM *h = conn->stream_resp->curl_hnd->h;
if (!(h))
{
return -1;
}
CURLMcode mc = curl_multi_perform (h, &running);
if (!mc && running)
/* wait for activity, timeout or "nothing" */
mc = curl_multi_poll (h, NULL, 0, 5000, NULL);
if (mc != CURLM_OK)
{
g_warning ("%s: error on curl_multi_poll(): %d\n", __func__, mc);
return -1;
}
return running;
}
/**
* @brief Get VT's metadata
*
* @param conn Connector struct with the data necessary for the connection
*
* @return Response Struct containing the feed metadata in json format in the
* body.
*/
openvasd_resp_t
openvasd_get_vts (openvasd_connector_t conn)
{
GString *path;
openvasd_resp_t response = NULL;
gchar *err = NULL;
CURL *hnd = NULL;
struct curl_slist *customheader = NULL;
response = g_malloc0 (sizeof (struct openvasd_response));
path = g_string_new ("/vts?information=1");
customheader = init_customheader (conn->apikey, FALSE);
hnd = handler (conn, GET, path->str, NULL, customheader, &err);
if (hnd == NULL)
{
curl_slist_free_all (customheader);
g_string_free (path, TRUE);
response->code = RESP_CODE_ERR;
response->body = err;
return response;
}
g_string_free (path, TRUE);
openvasd_send_request (hnd, NULL, response);
curl_slist_free_all (customheader);
if (response->code != RESP_CODE_ERR)
response->body = g_strdup (openvasd_vt_stream_str (conn));
openvasd_reset_vt_stream (conn);
return response;
}
/**
* @Brief Get VT's metadata
*
* @param conn Connector struct with the data necessary for the connection
* @param data String containing the scan config in JSON format.
*
* @return Response Struct containing the resonse.
*/
openvasd_resp_t
openvasd_start_scan (openvasd_connector_t conn, gchar *data)
{
openvasd_resp_t response = NULL;
cJSON *parser = NULL;
GString *path;
gchar *err = NULL;
CURL *hnd = NULL;
struct curl_slist *customheader = NULL;
response = g_malloc0 (sizeof (struct openvasd_response));
customheader = init_customheader (conn->apikey, TRUE);
hnd = handler (conn, POST, "/scans", data, customheader, &err);
if (hnd == NULL)
{
curl_slist_free_all (customheader);
response->code = RESP_CODE_ERR;
response->body = err;
return response;
}
openvasd_send_request (hnd, NULL, response);
curl_slist_free_all (customheader);
if (response->code == RESP_CODE_ERR)
{
response->code = RESP_CODE_ERR;
if (response->body == NULL)
response->body =
g_strdup ("{\"error\": \"Storing scan configuration\"}");
g_warning ("%s: Error storing scan configuration ", __func__);
openvasd_reset_vt_stream (conn);
return response;
}
// Get the Scan ID
parser = cJSON_Parse (openvasd_vt_stream_str (conn));
if (!parser)
{
const gchar *error_ptr = cJSON_GetErrorPtr ();
if (error_ptr != NULL)
{
response->body = g_strdup_printf ("{\"error\": \"%s\"}", error_ptr);
g_warning ("%s: %s", __func__, error_ptr);
}
else
{
response->body = g_strdup (
"{\"error\": \"Parsing json string to get the scan ID\"}");
g_warning ("%s: Parsing json string to get the scan ID", __func__);
}
response->code = RESP_CODE_ERR;
cJSON_Delete (parser);
openvasd_reset_vt_stream (conn);
return response;
}
conn->scan_id = g_strdup (cJSON_GetStringValue (parser));
// Start the scan
path = g_string_new ("/scans");
if (conn->scan_id != NULL && conn->scan_id[0] != '\0')
{
g_string_append (path, "/");
g_string_append (path, conn->scan_id);
}
else
{
response->code = RESP_CODE_ERR;
response->body = g_strdup ("{\"error\": \"Missing scan ID\"}");
g_string_free (path, TRUE);
g_warning ("%s: Missing scan ID", __func__);
cJSON_Delete (parser);
return response;
}
openvasd_reset_vt_stream (conn);
customheader = init_customheader (conn->apikey, TRUE);
hnd = handler (conn, POST, path->str, "{\"action\": \"start\"}", customheader,
&err);
if (hnd == NULL)
{
curl_slist_free_all (customheader);
g_string_free (path, TRUE);
response->code = RESP_CODE_ERR;
response->body = err;
return response;
}
g_string_free (path, TRUE);
openvasd_send_request (hnd, NULL, response);
curl_slist_free_all (customheader);
if (response->code == RESP_CODE_ERR)
{
response->code = RESP_CODE_ERR;
if (response->body == NULL)
response->body = g_strdup ("{\"error\": \"Starting the scan.\"}");
g_warning ("%s: Error starting the scan.", __func__);
return response;
}
cJSON_Delete (parser);
response->body = g_strdup (openvasd_vt_stream_str (conn));
openvasd_reset_vt_stream (conn);
return response;
}
openvasd_resp_t
openvasd_stop_scan (openvasd_connector_t conn)
{
openvasd_resp_t response = NULL;
GString *path;
gchar *err = NULL;
CURL *hnd = NULL;
struct curl_slist *customheader = NULL;
response = g_malloc0 (sizeof (struct openvasd_response));
// Stop the scan
path = g_string_new ("/scans");
if (conn->scan_id != NULL && conn->scan_id[0] != '\0')
{
g_string_append (path, "/");
g_string_append (path, conn->scan_id);
}
else
{
response->code = RESP_CODE_ERR;
response->body = g_strdup ("{\"error\": \"Missing scan ID\"}");
g_string_free (path, TRUE);
g_warning ("%s: Missing scan ID", __func__);
return response;
}
customheader = init_customheader (conn->apikey, TRUE);
hnd = handler (conn, POST, path->str, "{\"action\": \"stop\"}", customheader,
&err);
if (hnd == NULL)
{
curl_slist_free_all (customheader);
g_string_free (path, TRUE);
response->code = RESP_CODE_ERR;
response->body = err;
return response;
}
g_string_free (path, TRUE);
openvasd_send_request (hnd, NULL, response);
curl_slist_free_all (customheader);
if (response->code != RESP_CODE_ERR)
response->body = g_strdup (openvasd_vt_stream_str (conn));
openvasd_reset_vt_stream (conn);
return response;
}
openvasd_resp_t
openvasd_get_scan_results (openvasd_connector_t conn, long first, long last)
{
openvasd_resp_t response = NULL;
GString *path = NULL;
gchar *err = NULL;
CURL *hnd = NULL;
struct curl_slist *customheader = NULL;
response = g_malloc0 (sizeof (struct openvasd_response));
path = g_string_new ("/scans");
if (conn->scan_id != NULL && conn->scan_id[0] != '\0')
{
g_string_append (path, "/");
g_string_append (path, conn->scan_id);
if (last > first)
g_string_append_printf (path, "/results?range%ld-%ld", first, last);
else if (last < first)
g_string_append_printf (path, "/results?range=%ld", first);
else
g_string_append (path, "/results");
}
else
{
response->code = RESP_CODE_ERR;
response->body = g_strdup ("{\"error\": \"Missing scan ID\"}");
g_string_free (path, TRUE);
g_warning ("%s: Missing scan ID", __func__);
return response;
}
customheader = init_customheader (conn->apikey, FALSE);
hnd = handler (conn, GET, path->str, NULL, customheader, &err);
if (hnd == NULL)
{
curl_slist_free_all (customheader);
g_string_free (path, TRUE);
response->code = RESP_CODE_ERR;
response->body = err;
return response;
}
g_string_free (path, TRUE);
openvasd_send_request (hnd, NULL, response);
curl_slist_free_all (customheader);
if (response->code != RESP_CODE_ERR)
response->body = g_strdup (openvasd_vt_stream_str (conn));
else if (response->code == RESP_CODE_ERR)
{
g_warning ("%s: Not possible to get scan results", __func__);
response->body =
g_strdup ("{\"error\": \"Not possible to get scan results\"}");
}
openvasd_reset_vt_stream (conn);
return response;
}
openvasd_result_t
openvasd_result_new (unsigned long id, gchar *type, gchar *ip_address,
gchar *hostname, gchar *oid, int port, gchar *protocol,
gchar *message, gchar *detail_name, gchar *detail_value,
gchar *detail_source_type, gchar *detail_source_name,
gchar *detail_source_description)
{
openvasd_result_t result = g_malloc0 (sizeof (struct openvasd_result));
result->id = id;
result->type = g_strdup (type);
result->ip_address = g_strdup (ip_address);
result->hostname = g_strdup (hostname);