File: nvti.c

package info (click to toggle)
openvas-libraries 2.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,720 kB
  • ctags: 1,334
  • sloc: ansic: 12,441; sh: 8,238; makefile: 325
file content (1179 lines) | stat: -rw-r--r-- 29,194 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
/* openvas-libraries/libopenvascommon
 * $Id$
 * Description: Implementation of API to handle NVT Info datasets
 *
 * Authors:
 * Jan-Oliver Wagner <jan-oliver.wagner@greenbone.net>
 * Matthew Mundell <matt@mundell.ukfsn.org>
 *
 * Copyright:
 * Copyright (C) 2009 Greenbone Networks GmbH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2,
 * or, at your option, any later version as published by the Free
 * Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/**
 * @file nvti.c
 * @brief Implementation of API to handle NVT Info datasets
 *
 * This file contains all methods to handle NVT Information datasets
 * (nvti_t).
 *
 * The module consequently uses glib datatypes and api for memory
 * management etc.
 */

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <sys/stat.h>

#include "nvti.h"

/**
 * @brief Create a new nvtpref structure filled with the given values.
 *
 * @param name The name to be set. A copy will created of this.
 *
 * @param type The type to be set. A copy will created of this.
 *
 * @param dflt The default to be set. A copy will created of this.
 *
 * @return NULL in case the memory could not be allocated.
 *         Else a nvtpref structure which needs to be
 *         released using @ref nvtpref_free .
 */
nvtpref_t *
nvtpref_new (gchar * name, gchar * type, gchar * dflt)
{
  nvtpref_t * np = g_malloc0 (sizeof (nvtpref_t));

  if (! np) return NULL;

  if (name) np->name = g_strdup (name);
  if (type) np->type = g_strdup (type);
  if (dflt) np->dflt = g_strdup (dflt);

  return (np);
}

/**
 * @brief Free memory of a nvtpref structure.
 *
 * @param n The structure to be freed.
 */
void
nvtpref_free (nvtpref_t * np)
{
  if (np->name) g_free (np->name);
  if (np->type) g_free (np->type);
  if (np->dflt) g_free (np->dflt);
  g_free (np);
}

/**
 * @brief Get the Name of a NVT Preference.
 *
 * @param np The NVT Pref structure of which the Name should
 *           be returned.
 *
 * @return The name string. Don't free this.
 */
gchar *
nvtpref_name (const nvtpref_t * np)
{
  return (np->name);
}

/**
 * @brief Get the Type of a NVT Preference.
 *
 * @param np The NVT Pref structure of which the Type should
 *           be returned.
 *
 * @return The type string. Don't free this.
 */
gchar *
nvtpref_type (const nvtpref_t * np)
{
  return (np->type);
}

/**
 * @brief Get the Default of a NVT Preference.
 *
 * @param np The NVT Pref structure of which the Default should
 *           be returned.
 *
 * @return The default string. Don't free this.
 */
gchar *
nvtpref_default (const nvtpref_t * np)
{
  return (np->dflt);
}

/**
 * @brief Create a new (empty) nvti structure.
 *
 * @return NULL in case the memory could not be allocated.
 *         Else an empty nvti structure which needs to be
 *         released using @ref nvti_free .
 *         The whole struct is initalized with 0's.
 */
nvti_t *
nvti_new (void)
{
  return ((nvti_t *) g_malloc0 (sizeof (nvti_t)));
}

/**
 * @brief Free memory of a nvti structure.
 *
 * @param n The structure to be freed.
 */
void
nvti_free (nvti_t * n)
{
  if (n->oid) g_free (n->oid);
  if (n->version) g_free (n->version);
  if (n->name) g_free (n->name);
  if (n->summary) g_free (n->summary);
  if (n->description) g_free (n->description);
  if (n->copyright) g_free (n->copyright);
  if (n->cve) g_free (n->cve);
  if (n->bid) g_free (n->bid);
  if (n->xref) g_free (n->xref);
  if (n->tag) g_free (n->tag);
  if (n->dependencies) g_free (n->dependencies);
  if (n->required_keys) g_free (n->required_keys);
  if (n->excluded_keys) g_free (n->excluded_keys);
  if (n->required_ports) g_free (n->required_ports);
  if (n->required_udp_ports) g_free (n->required_udp_ports);
  if (n->sign_key_ids) g_free (n->sign_key_ids);
  if (n->family) g_free (n->family);
  if (n->src) g_free (n->src);
  if (n->prefs) {
    guint len = g_slist_length(n->prefs);
    int i;
    for (i = 0;i < len;i ++)
      nvtpref_free(g_slist_nth_data(n->prefs, i));
    g_slist_free(n->prefs);
  }
  g_free (n);
}

/**
 * @brief Get the OID string.
 *
 * @param n The NVT Info structure of which the OID should
 *          be returned.
 *
 * @return The OID string. Don't free this.
 */
gchar *
nvti_oid (const nvti_t * n)
{
  return (n->oid);
}

/**
 * @brief Get the version.
 *
 * @param n The NVT Info structure of which the OID should
 *          be returned.
 *
 * @return The version string. Don't free this.
 */
gchar *
nvti_version (const nvti_t * n)
{
  return (n->version);
}

/**
 * @brief Get the name.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The name string. Don't free this.
 */
gchar *
nvti_name (const nvti_t * n)
{
  return (n->name);
}

/**
 * @brief Get the summary.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The summary string. Don't free this.
 */
gchar *
nvti_summary (const nvti_t * n)
{
  return (n->summary);
}

/**
 * @brief Get the description.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The description string. Don't free this.
 */
gchar *
nvti_description (const nvti_t * n)
{
  return (n->description);
}

/**
 * @brief Get the copyright notice.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The copyright string. Don't free this.
 */
gchar *
nvti_copyright (const nvti_t * n)
{
  return (n->copyright);
}

/**
 * @brief Get the CVE references.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The CVE list as string. Don't free this.
 */
gchar *
nvti_cve (const nvti_t * n)
{
  return (n->cve);
}

/**
 * @brief Get the bid references.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The bid list as string. Don't free this.
 */
gchar *
nvti_bid (const nvti_t * n)
{
  return (n->bid);
}

/**
 * @brief Get the xref's.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The xref string. Don't free this.
 */
gchar *
nvti_xref (const nvti_t * n)
{
  return (n->xref);
}

/**
 * @brief Get the tag.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The tags string. Don't free this.
 */
gchar *
nvti_tag (const nvti_t * n)
{
  return (n->tag);
}

/**
 * @brief Get the dependencies list.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The dependencies string. Don't free this.
 */
gchar *
nvti_dependencies (const nvti_t * n)
{
  return (n->dependencies);
}

/**
 * @brief Get the required keys list.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The required keys string. Don't free this.
 */
gchar *
nvti_required_keys (const nvti_t * n)
{
  return (n->required_keys);
}

/**
 * @brief Get the excluded keys list.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The excluded keys string. Don't free this.
 */
gchar *
nvti_excluded_keys (const nvti_t * n)
{
  return (n->excluded_keys);
}

/**
 * @brief Get the required ports list.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The required ports string. Don't free this.
 */
gchar *
nvti_required_ports (const nvti_t * n)
{
  return (n->required_ports);
}

/**
 * @brief Get the required udp ports list.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The required udp ports string. Don't free this.
 */
gchar *
nvti_required_udp_ports (const nvti_t * n)
{
  return (n->required_udp_ports);
}

/**
 * @brief Get the sign key ids (fingerptints) list.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The sign key ids string. Don't free this.
 */
gchar *
nvti_sign_key_ids (const nvti_t * n)
{
  return (n->sign_key_ids);
}

/**
 * @brief Get the family name.
 *
 * @param n The NVT Info structure of which the name should
 *          be returned.
 *
 * @return The family name string. Don't free this.
 */
gchar *
nvti_family (const nvti_t * n)
{
  return (n->family);
}

/**
 * @brief Get the number of preferences of the NVT.
 *
 * @param n The NVT Info structure.
 *
 * @return The number of preferences.
 */
guint
nvti_pref_len (const nvti_t * n)
{
  return(g_slist_length(n->prefs));
}

/**
 * @brief Get the n'th preferences of the NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param p The position of the preference to return.
 *
 * @return The number of preferences. NULL if 
 */
nvtpref_t *
nvti_pref (const nvti_t * n, guint p)
{
  return(g_slist_nth_data(n->prefs, p));
}

/**
 * @brief Get the source URI of a NVT Info.
 *
 * @param n The NVT Info structure of which the source URI should
 *          be returned.
 *
 * @return The source URI string. This can be NULL if the NVT
 *         Info was created in memory. It can also be a file path.
 *         Don't free this.
 */
gchar *
nvti_src (const nvti_t * n)
{
  return (n->src);
}

/**
 * @brief Get the timeout for this NVT.
 *
 * @param n The NVT Info structure of which the timeout should
 *          be returned.
 *
 * @return The timeout integer number. A value <= 0 indicates it is not set.
 */
gint
nvti_timeout (const nvti_t * n)
{
  return (n->timeout);
}

/**
 * @brief Get the category for this NVT.
 *
 * @param n The NVT Info structure of which the timeout should
 *          be returned.
 *
 * @return The category integer code. A value <= 0 indicates it is not set.
 */
gint
nvti_category (const nvti_t * n)
{
  return (n->category);
}

/**
 * @brief Set the OID of a NVT Info.
 *
 * @param n The NVT Info structure.
 *
 * @param oid The OID to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_oid (nvti_t * n, const gchar * oid)
{
  if (n->oid)
    g_free (n->oid);
  n->oid = g_strdup (oid);
  return (0);
}

/**
 * @brief Set the version of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param version The version to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_version (nvti_t * n, const gchar * version)
{
  if (n->version)
    g_free (n->version);
  n->version = g_strdup (version);
  return (0);
}

/**
 * @brief Set the name of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param name The name to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_name (nvti_t * n, const gchar * name)
{
  if (n->name)
    g_free (n->name);
  n->name = g_strdup (name);
  return (0);
}

/**
 * @brief Set the summary of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param summary The summary to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_summary (nvti_t * n, const gchar * summary)
{
  if (n->summary)
    g_free (n->summary);
  n->summary = g_strdup (summary);
  return (0);
}

/**
 * @brief Set the description of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param description The description to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_description (nvti_t * n, const gchar * description)
{
  if (n->description)
    g_free (n->description);
  n->description = g_strdup (description);
  return (0);
}

/**
 * @brief Set the copyright of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param copyright The copyright to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_copyright (nvti_t * n, const gchar * copyright)
{
  if (n->copyright)
    g_free (n->copyright);
  n->copyright = g_strdup (copyright);
  return (0);
}

/**
 * @brief Set the CVE references of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param cve The cve list to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_cve (nvti_t * n, const gchar * cve)
{
  if (n->cve)
    g_free (n->cve);
  n->cve = g_strdup (cve);
  return (0);
}

/**
 * @brief Set the bid references of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param bid The bid to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_bid (nvti_t * n, const gchar * bid)
{
  if (n->bid)
    g_free (n->bid);
  n->bid = g_strdup (bid);
  return (0);
}

/**
 * @brief Set the xrefs of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param xref The xrefs to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_xref (nvti_t * n, const gchar * xref)
{
  if (n->xref)
    g_free (n->xref);
  if (xref && xref[0])
    n->xref = g_strdup (xref);
  else
    n->xref = NULL;
  return (0);
}

/**
 * @brief Set the tags of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param tag The tags to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_tag (nvti_t * n, const gchar * tag)
{
  if (n->tag)
    g_free (n->tag);
  if (tag && tag[0])
    n->tag = g_strdup (tag);
  else
    n->tag = NULL;
  return (0);
}

/**
 * @brief Set the dependencies of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param dependencies The dependencies to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_dependencies (nvti_t * n, const gchar * dependencies)
{
  if (n->dependencies)
    g_free (n->dependencies);
  if (dependencies && dependencies[0])
    n->dependencies = g_strdup (dependencies);
  else
    n->dependencies = NULL;
  return (0);
}

/**
 * @brief Set the required keys of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param required_keys The required keys to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_required_keys (nvti_t * n, const gchar * required_keys)
{
  if (n->required_keys)
    g_free (n->required_keys);
  if (required_keys && required_keys[0])
    n->required_keys = g_strdup (required_keys);
  else
    n->required_keys = NULL;
  return (0);
}

/**
 * @brief Set the excluded keys of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param excluded_keys The excluded keys to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_excluded_keys (nvti_t * n, const gchar * excluded_keys)
{
  if (n->excluded_keys)
    g_free (n->excluded_keys);
  if (excluded_keys && excluded_keys[0])
    n->excluded_keys = g_strdup (excluded_keys);
  else
    n->excluded_keys = NULL;
  return (0);
}

/**
 * @brief Set the required ports of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param required_ports The required ports to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_required_ports (nvti_t * n, const gchar * required_ports)
{
  if (n->required_ports)
    g_free (n->required_ports);
  if (required_ports && required_ports[0])
    n->required_ports = g_strdup (required_ports);
  else
    n->required_ports = NULL;
  return (0);
}

/**
 * @brief Set the required udp ports of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param required_udp_ports The required udp ports to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_required_udp_ports (nvti_t * n, const gchar * required_udp_ports)
{
  if (n->required_udp_ports)
    g_free (n->required_udp_ports);
  if (required_udp_ports && required_udp_ports[0])
    n->required_udp_ports = g_strdup (required_udp_ports);
  else
    n->required_udp_ports = NULL;
  return (0);
}

/**
 * @brief Set the sign key ids of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param sign_key_ids The sign key ids to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_sign_key_ids (nvti_t * n, const gchar * sign_key_ids)
{
  if (n->sign_key_ids)
    g_free (n->sign_key_ids);
  if (sign_key_ids && sign_key_ids[0])
    n->sign_key_ids = g_strdup (sign_key_ids);
  else
    n->sign_key_ids = NULL;
  return (0);
}

/**
 * @brief Set the family of a NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param family The family to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_family (nvti_t * n, const gchar * family)
{
  if (n->family)
    g_free (n->family);
  n->family = g_strdup (family);
  return (0);
}

/**
 * @brief Set the source identifier for the acutal NVT.
 *
 * @param n The NVT Info structure.
 *
 * @param src The URI to set. A copy will be created from this.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_src (nvti_t * n, const gchar * src)
{
  if (n->src)
    g_free (n->src);
  n->src = g_strdup (src);
  return (0);
}

/**
 * @brief Set the timout of a NVT Info.
 *
 * @param n The NVT Info structure.
 *
 * @param timout The timeout to set. Values <= 0 will indicate it is not set.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_timeout (nvti_t * n, const gint timeout)
{
  n->timeout = timeout;
  return (0);
}

/**
 * @brief Set the category type of a NVT Info.
 *
 * @param n The NVT Info structure.
 *
 * @param category The category to set. Values <= 0 will indicate it is not set.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_set_category (nvti_t * n, const gint category)
{
  n->category = category;
  return (0);
}

/**
 * @brief Add a preference to the NVT Info.
 *
 * @param n The NVT Info structure.
 *
 * @param np The NVT preference to add.
 *
 * @return 0 for success. Anything else indicates an error.
 */
int
nvti_add_pref (nvti_t * n, nvtpref_t * np)
{
  n->prefs = g_slist_append(n->prefs, np);
  return (0);
}

/**
 * @brief Create a human readable text representation of a NVT Info.
 *        This is mainly for debug purposes.
 *
 * @param n The NVT Info structure.
 *
 * @return A newly allocated string with multi-line text.
 *         The string needs to be freed with g_free().
 */
gchar *
nvti_as_text (const nvti_t * n)
{
  return (g_strconcat
          ("NVT Info for OID ", (n->oid ? n->oid : "(unset)"), ":\n\n",
           "\nVersion: ", (n->version ? n->version : "(unset, probably in-memory)"),
           "\nName: ", (n->name ? n->name : "(unset, probably in-memory)"),
           "\nSummary: ", (n->summary ? n->summary : "(unset, probably in-memory)"),
           "\nDescription: ", (n->description ? n->description : "(unset, probably in-memory)"),
           "\nCopyright: ", (n->copyright ? n->copyright : "(unset, probably in-memory)"),
           "\nCVE: ", (n->cve ? n->cve : "(unset, probably in-memory)"),
           "\nBID: ", (n->bid ? n->bid : "(unset, probably in-memory)"),
           "\nXref: ", (n->xref ? n->xref : "(unset, probably in-memory)"),
           "\nTag: ", (n->tag ? n->tag : "(unset, probably in-memory)"),
           "\nDependencies: ", (n->dependencies ? n->dependencies : "(unset, probably in-memory)"),
           "\nRequired Keys: ", (n->required_keys ? n->required_keys : "(unset, probably in-memory)"),
           "\nExcluded Keys: ", (n->excluded_keys ? n->excluded_keys : "(unset, probably in-memory)"),
           "\nRequired Ports: ", (n->required_ports ? n->required_ports : "(unset, probably in-memory)"),
           "\nRequired UDP ports: ", (n->required_udp_ports ? n->required_udp_ports : "(unset, probably in-memory)"),
           "\nSignKey IDs: ", (n->sign_key_ids ? n->sign_key_ids : "(unset, probably in-memory)"),
           "\nFamily: ", (n->family ? n->family : "(unset, probably in-memory)"),
           "\nSource: ", (n->src ? n->src : "(unset, probably in-memory)"),
//         "\nTimeout: ", (n->timeout <= 0 ? n->timeout : "(unset, probably in-memory)"),
//         "\nCategory: ", (n->category <= 0 ? n->category : "(unset, probably in-memory)"),
           "\n", NULL));
}

/**
 * @brief Create a single line representation of a NVT Info as
 *        used in "openvas_nvt_cache" files of OpenVAS-Client.
 *
 * @param n The NVT Info structure.
 *
 * @return A newly allocated string.
 *         The string needs to be freed with g_free().
 */
gchar *
nvti_as_openvas_nvt_cache_entry (const nvti_t * n)
{
  return (NULL);        // not implemented yet
}

/**
 * @brief Read NVT Info from a keyfile.
 *
 * @param fn The filename to read from.
 *
 * @return A newly allocated nvti_t object.
 *         The nvti_t needs to be freed with nvti_free().
 */
nvti_t *
nvti_from_keyfile (const gchar * fn)
{
  GKeyFile *keyfile = g_key_file_new ();
  nvti_t *n;
  GError *error = NULL;
  gchar **keys;
  int i;

  if (!g_key_file_load_from_file (keyfile, fn, G_KEY_FILE_NONE, &error))
    {
      g_error (error->message);
      return NULL;
    }

  n = nvti_new ();
  nvti_set_oid (n, g_key_file_get_string (keyfile, "NVT Info", "OID", NULL));
  nvti_set_version (n, g_key_file_get_string (keyfile, "NVT Info", "Version", NULL));
  nvti_set_name (n, g_key_file_get_string (keyfile, "NVT Info", "Name", NULL));
  nvti_set_summary (n, g_key_file_get_string (keyfile, "NVT Info", "Summary", NULL));
  nvti_set_description (n, g_key_file_get_string (keyfile, "NVT Info", "Description", NULL));
  nvti_set_copyright (n, g_key_file_get_string (keyfile, "NVT Info", "Copyright", NULL));
  nvti_set_cve (n, g_key_file_get_string (keyfile, "NVT Info", "CVEs", NULL));
  nvti_set_bid (n, g_key_file_get_string (keyfile, "NVT Info", "BIDs", NULL));
  nvti_set_xref (n, g_key_file_get_string (keyfile, "NVT Info", "XREFs", NULL));
  nvti_set_tag (n, g_key_file_get_string (keyfile, "NVT Info", "Tags", NULL));
  nvti_set_dependencies (n, g_key_file_get_string (keyfile, "NVT Info", "Dependencies", NULL));
  nvti_set_required_keys (n, g_key_file_get_string (keyfile, "NVT Info", "RequiredKeys", NULL));
  nvti_set_excluded_keys (n, g_key_file_get_string (keyfile, "NVT Info", "ExcludedKeys", NULL));
  nvti_set_required_ports (n, g_key_file_get_string (keyfile, "NVT Info", "RequiredPorts", NULL));
  nvti_set_required_udp_ports (n, g_key_file_get_string (keyfile, "NVT Info", "RequiredUDPPorts", NULL));
  nvti_set_sign_key_ids (n, g_key_file_get_string (keyfile, "NVT Info", "SignKeyIDs", NULL));
  nvti_set_family (n, g_key_file_get_string (keyfile, "NVT Info", "Family", NULL));
  nvti_set_src (n, g_key_file_get_string (keyfile, "NVT Info", "src", NULL));
  nvti_set_timeout (n, g_key_file_get_integer (keyfile, "NVT Info", "Timeout", NULL));
  nvti_set_category (n, g_key_file_get_integer (keyfile, "NVT Info", "Category", NULL));

  if (g_key_file_has_group(keyfile, "NVT Prefs")) {
    keys = g_key_file_get_keys(keyfile, "NVT Prefs", NULL, NULL);
    for (i = 0;keys[i];i ++) {
      gsize len;
      gchar ** items = g_key_file_get_string_list(keyfile, "NVT Prefs", keys[i], &len, NULL);
      if (len != 3) continue; // format error for this pref.
      nvtpref_t *np = nvtpref_new(items[0], items[1], items[2]);
      nvti_add_pref(n, np);
      g_strfreev(items);
    }
    g_strfreev(keys);
  }

  g_key_file_free (keyfile);

  return (n);
}

/**
 * @brief Store NVT Info into a keyfile.
 *
 * @param n The NVT Info object to store.
 *
 * @param fn The filename to write to.
 *
 * @return 0 on success. @TODO Anything else indicates an error.
 */
int
nvti_to_keyfile (const nvti_t * n, const gchar * fn)
{
  GKeyFile *keyfile = g_key_file_new ();
  gchar *text;
  GError *error = NULL;

  if (n->oid)
    g_key_file_set_string (keyfile, "NVT Info", "OID", n->oid);
  if (n->version)
    g_key_file_set_string (keyfile, "NVT Info", "Version", n->version);
  if (n->name)
    g_key_file_set_string (keyfile, "NVT Info", "Name", n->name);
  if (n->summary)
    g_key_file_set_string (keyfile, "NVT Info", "Summary", n->summary);
  if (n->description)
    g_key_file_set_string (keyfile, "NVT Info", "Description", n->description);
  if (n->copyright)
    g_key_file_set_string (keyfile, "NVT Info", "Copyright", n->copyright);
  if (n->cve)
    g_key_file_set_string (keyfile, "NVT Info", "CVEs", n->cve);
  if (n->bid)
    g_key_file_set_string (keyfile, "NVT Info", "BIDs", n->bid);
  if (n->xref)
    g_key_file_set_string (keyfile, "NVT Info", "XREFs", n->xref);
  if (n->tag)
    g_key_file_set_string (keyfile, "NVT Info", "Tags", n->tag);
  if (n->dependencies)
    g_key_file_set_string (keyfile, "NVT Info", "Dependencies", n->dependencies);
  if (n->required_keys)
    g_key_file_set_string (keyfile, "NVT Info", "RequiredKeys", n->required_keys);
  if (n->excluded_keys)
    g_key_file_set_string (keyfile, "NVT Info", "ExcludedKeys", n->excluded_keys);
  if (n->required_ports)
    g_key_file_set_string (keyfile, "NVT Info", "RequiredPorts", n->required_ports);
  if (n->required_udp_ports)
    g_key_file_set_string (keyfile, "NVT Info", "RequiredUDPPorts", n->required_udp_ports);
  if (n->sign_key_ids)
    g_key_file_set_string (keyfile, "NVT Info", "SignKeyIDs", n->sign_key_ids);
  if (n->family)
    g_key_file_set_string (keyfile, "NVT Info", "Family", n->family);
  if (n->src)
    g_key_file_set_string (keyfile, "NVT Info", "src", n->src);
  if (n->timeout > 0)
    g_key_file_set_integer (keyfile, "NVT Info", "Timeout", n->timeout);
  if (n->category > 0)
    g_key_file_set_integer (keyfile, "NVT Info", "Category", n->category);

  int i;
  for (i=0;i < nvti_pref_len(n);i ++) {
    nvtpref_t * np = nvti_pref(n, i);
    gchar * lst[3];
    gchar buf[10];
    lst[0] = ((nvtpref_t *)np)->name;
    lst[1] = ((nvtpref_t *)np)->type;
    lst[2] = ((nvtpref_t *)np)->dflt;

    g_snprintf(buf, 10, "P%d", i);
    g_key_file_set_string_list((GKeyFile *)keyfile, "NVT Prefs", buf, (const gchar **)lst, 3);
//    g_key_file_set_string_list((GKeyFile *)keyfile, "NVT Prefs", (gchar *)lst[0], (const gchar **)lst, 3);
  }

  text = g_key_file_to_data (keyfile, NULL, &error);
  if (error != NULL)
    {
      fprintf (stderr, "Error occured while preparing %s: %s\n",
               fn, error->message);
      g_error_free (error);
    }
  else
    {
      FILE *fp = fopen (fn, "w");
      if (! fp) { // second try: maybe the directory was missing.
        gchar * cache_dir = g_path_get_dirname(fn);
        if ((mkdir(cache_dir, 0755) < 0) && (errno != EEXIST)) {
          fprintf(stderr, "mkdir(%s) : %s\n", cache_dir, strerror(errno));
          g_free(text);
          g_key_file_free (keyfile);
	  return (1);
        }
        fp = fopen (fn, "w");
      }

      if (! fp) { // again failed
          fprintf(stderr, "fopen(%s) : %s\n", fn, strerror(errno));
          g_free(text);
          g_key_file_free (keyfile);
	  return (2);
      }

      fputs (text, fp);
      fclose (fp);
      g_free(text);
    }

  g_key_file_free (keyfile);

  return (0);
}


/* Collections of nvtis. */

/**
 * @brief Free an NVT Info, for g_hash_table_destroy.
 *
 * @param nvti The NVT Info.
 */
static void
free_nvti_for_hash_table (gpointer nvti)
{
  nvti_free ((nvti_t*) nvti);
}

/**
 * @brief Make a collection of NVT Infos.
 */
nvtis_t*
nvtis_new ()
{
  return g_hash_table_new_full (g_str_hash,
                                g_str_equal,
                                NULL,
                                free_nvti_for_hash_table);
}

/**
 * @brief Free a collection of NVT Infos.
 *
 * @param nvtis The collection of NVT Infos.
 */
void
nvtis_free (nvtis_t* nvtis)
{
  if (nvtis) g_hash_table_destroy (nvtis);
}

/**
 * @brief Get the size of a collection of NVT Infos.
 *
 * @return The number of entries in the collection.
 */
guint
nvtis_size (nvtis_t* nvtis)
{
  return g_hash_table_size (nvtis);
}

/**
 * @brief Add an NVT Info to a collection of NVT Infos.
 *
 * @param nvtis The collection of NVT Infos.
 */
void
nvtis_add (nvtis_t* nvtis, nvti_t* nvti)
{
  if (nvti)
    g_hash_table_insert (nvtis, (gpointer) nvti_oid (nvti), (gpointer) nvti);
}

/**
 * @brief Add an NVT Info to a collection of NVT Infos.
 *
 * @param nvtis The collection of NVT Infos.
 * @param nvtis The OID of the NVT.
 *
 * @return The NVT Info, if found, else NULL.
 */
nvti_t*
nvtis_lookup (nvtis_t* nvtis, const char* oid)
{
  return g_hash_table_lookup (nvtis, oid);
}