suricata
conf.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2023 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Endace Technology Limited - Jason Ish <jason.ish@endace.com>
22  *
23  * This file provides a basic configuration system for the IDPS
24  * engine.
25  *
26  * NOTE: Setting values should only be done from one thread during
27  * engine initialization. Multiple threads should be able access read
28  * configuration data. Allowing run time changes to the configuration
29  * will require some locks.
30  *
31  * \todo Consider having the in-memory configuration database a direct
32  * reflection of the configuration file and moving command line
33  * parameters to a primary lookup table?
34  *
35  * \todo Get rid of allow override and go with a simpler first set,
36  * stays approach?
37  */
38 
39 #include "suricata-common.h"
40 #include "conf.h"
41 #include "util-unittest.h"
42 #include "util-debug.h"
43 #include "util-path.h"
44 #include "util-conf.h"
45 
46 /** Maximum size of a complete domain name. */
47 #define NODE_NAME_MAX 1024
48 
49 static ConfNode *root = NULL;
50 static ConfNode *root_backup = NULL;
51 
52 /**
53  * \brief Helper function to get a node, creating it if it does not
54  * exist.
55  *
56  * This function exits on memory failure as creating configuration
57  * nodes is usually part of application initialization.
58  *
59  * \param parent The node to use as the parent
60  * \param name The name of the configuration node to get.
61  * \param final Flag to set created nodes as final or not.
62  *
63  * \retval The existing configuration node if it exists, or a newly
64  * created node for the provided name. On error, NULL will be returned.
65  */
66 ConfNode *ConfNodeGetNodeOrCreate(ConfNode *parent, const char *name, int final)
67 {
68  ConfNode *node = NULL;
69  char node_name[NODE_NAME_MAX];
70  char *key;
71  char *next;
72 
73  if (strlcpy(node_name, name, sizeof(node_name)) >= sizeof(node_name)) {
74  SCLogError("Configuration name too long: %s", name);
75  return NULL;
76  }
77 
78  key = node_name;
79 
80  do {
81  if ((next = strchr(key, '.')) != NULL)
82  *next++ = '\0';
83  if ((node = ConfNodeLookupChild(parent, key)) == NULL) {
84  node = ConfNodeNew();
85  if (unlikely(node == NULL)) {
86  SCLogWarning("Failed to allocate memory for configuration.");
87  goto end;
88  }
89  node->name = SCStrdup(key);
90  if (unlikely(node->name == NULL)) {
91  ConfNodeFree(node);
92  node = NULL;
93  SCLogWarning("Failed to allocate memory for configuration.");
94  goto end;
95  }
96  node->parent = parent;
97  node->final = final;
98  TAILQ_INSERT_TAIL(&parent->head, node, next);
99  }
100  key = next;
101  parent = node;
102  } while (next != NULL);
103 
104 end:
105  return node;
106 }
107 
108 /**
109  * \brief Wrapper function for ConfNodeGetNodeOrCreate that operates
110  * on the current root node.
111  */
112 static ConfNode *ConfGetNodeOrCreate(const char *name, int final)
113 {
114  return ConfNodeGetNodeOrCreate(root, name, final);
115 }
116 
117 /**
118  * \brief Initialize the configuration system.
119  */
120 void ConfInit(void)
121 {
122  if (root != NULL) {
123  SCLogDebug("already initialized");
124  return;
125  }
126  root = ConfNodeNew();
127  if (root == NULL) {
128  FatalError("ERROR: Failed to allocate memory for root configuration node, "
129  "aborting.");
130  }
131  SCLogDebug("configuration module initialized");
132 }
133 
134 /**
135  * \brief Allocate a new configuration node.
136  *
137  * \retval An allocated configuration node on success, NULL on failure.
138  */
140 {
141  ConfNode *new;
142 
143  new = SCCalloc(1, sizeof(*new));
144  if (unlikely(new == NULL)) {
145  return NULL;
146  }
147  TAILQ_INIT(&new->head);
148 
149  return new;
150 }
151 
152 /**
153  * \brief Free a ConfNode and all of its children.
154  *
155  * \param node The configuration node to SCFree.
156  */
158 {
159  ConfNode *tmp;
160 
161  while ((tmp = TAILQ_FIRST(&node->head))) {
162  TAILQ_REMOVE(&node->head, tmp, next);
163  ConfNodeFree(tmp);
164  }
165 
166  if (node->name != NULL)
167  SCFree(node->name);
168  if (node->val != NULL)
169  SCFree(node->val);
170  SCFree(node);
171 }
172 
173 /**
174  * \brief Get a ConfNode by name.
175  *
176  * \param name The full name of the configuration node to lookup.
177  *
178  * \retval A pointer to ConfNode is found or NULL if the configuration
179  * node does not exist.
180  */
181 ConfNode *ConfGetNode(const char *name)
182 {
183  ConfNode *node = root;
184  char node_name[NODE_NAME_MAX];
185  char *key;
186  char *next;
187 
188  if (strlcpy(node_name, name, sizeof(node_name)) >= sizeof(node_name)) {
189  SCLogError("Configuration name too long: %s", name);
190  return NULL;
191  }
192 
193  key = node_name;
194  do {
195  if ((next = strchr(key, '.')) != NULL)
196  *next++ = '\0';
197  node = ConfNodeLookupChild(node, key);
198  key = next;
199  } while (next != NULL && node != NULL);
200 
201  return node;
202 }
203 
204 /**
205  * \brief Get the root configuration node.
206  */
208 {
209  return root;
210 }
211 
212 /**
213  * \brief Set a configuration value.
214  *
215  * Configuration values set with this function may be overridden by
216  * subsequent calls, or if the value appears multiple times in a
217  * configuration file.
218  *
219  * \param name The name of the configuration parameter to set.
220  * \param val The value of the configuration parameter.
221  *
222  * \retval 1 if the value was set otherwise 0.
223  */
224 int ConfSet(const char *name, const char *val)
225 {
226  ConfNode *node = ConfGetNodeOrCreate(name, 0);
227  if (node == NULL || node->final) {
228  return 0;
229  }
230  if (node->val != NULL)
231  SCFree(node->val);
232  node->val = SCStrdup(val);
233  if (unlikely(node->val == NULL)) {
234  return 0;
235  }
236  return 1;
237 }
238 
239 /**
240  * \brief Set a configuration parameter from a string.
241  *
242  * Where the input string is something like:
243  * stream.midstream=true
244  *
245  * \param input the input string to be parsed.
246  *
247  * \retval 1 if the value of set, otherwise 0.
248  */
249 int ConfSetFromString(const char *input, int final)
250 {
251  int retval = 0;
252  char *name = SCStrdup(input), *val = NULL;
253  if (unlikely(name == NULL)) {
254  goto done;
255  }
256  val = strchr(name, '=');
257  if (val == NULL) {
258  goto done;
259  }
260  *val++ = '\0';
261 
262  while (isspace((int)name[strlen(name) - 1])) {
263  name[strlen(name) - 1] = '\0';
264  }
265 
266  while (isspace((int)*val)) {
267  val++;
268  }
269 
270  if (final) {
271  if (!ConfSetFinal(name, val)) {
272  goto done;
273  }
274  }
275  else {
276  if (!ConfSet(name, val)) {
277  goto done;
278  }
279  }
280 
281  retval = 1;
282 done:
283  if (name != NULL) {
284  SCFree(name);
285  }
286  return retval;
287 }
288 
289 /**
290  * \brief Set a final configuration value.
291  *
292  * A final configuration value is a value that cannot be overridden by
293  * the configuration file. Its mainly useful for setting values that
294  * are supplied on the command line prior to the configuration file
295  * being loaded. However, a subsequent call to this function can
296  * override a previously set value.
297  *
298  * \param name The name of the configuration parameter to set.
299  * \param val The value of the configuration parameter.
300  *
301  * \retval 1 if the value was set otherwise 0.
302  */
303 int ConfSetFinal(const char *name, const char *val)
304 {
305  ConfNode *node = ConfGetNodeOrCreate(name, 1);
306  if (node == NULL) {
307  return 0;
308  }
309  if (node->val != NULL)
310  SCFree(node->val);
311  node->val = SCStrdup(val);
312  if (unlikely(node->val == NULL)) {
313  return 0;
314  }
315  node->final = 1;
316  return 1;
317 }
318 
319 /**
320  * \brief Retrieve the value of a configuration node.
321  *
322  * This function will return the value for a configuration node based
323  * on the full name of the node. It is possible that the value
324  * returned could be NULL, this could happen if the requested node
325  * does exist but is not a node that contains a value, but contains
326  * children ConfNodes instead.
327  *
328  * \param name Name of configuration parameter to get.
329  * \param vptr Pointer that will be set to the configuration value parameter.
330  * Note that this is just a reference to the actual value, not a copy.
331  *
332  * \retval 1 will be returned if the name is found, otherwise 0 will
333  * be returned.
334  */
335 int ConfGet(const char *name, const char **vptr)
336 {
337  ConfNode *node = ConfGetNode(name);
338  if (node == NULL) {
339  SCLogDebug("failed to lookup configuration parameter '%s'", name);
340  return 0;
341  }
342  else {
343  *vptr = node->val;
344  return 1;
345  }
346 }
347 
348 int ConfGetChildValue(const ConfNode *base, const char *name, const char **vptr)
349 {
350  ConfNode *node = ConfNodeLookupChild(base, name);
351 
352  if (node == NULL) {
353  SCLogDebug("failed to lookup configuration parameter '%s'", name);
354  return 0;
355  }
356  else {
357  *vptr = node->val;
358  return 1;
359  }
360 }
361 
363  const char *name)
364 {
365  ConfNode *node = ConfNodeLookupChild(base, name);
366  if (node != NULL)
367  return node;
368 
369  /* Get 'default' value */
370  if (dflt) {
371  return ConfNodeLookupChild(dflt, name);
372  }
373  return NULL;
374 }
375 
376 int ConfGetChildValueWithDefault(const ConfNode *base, const ConfNode *dflt,
377  const char *name, const char **vptr)
378 {
379  int ret = ConfGetChildValue(base, name, vptr);
380  /* Get 'default' value */
381  if (ret == 0 && dflt) {
382  return ConfGetChildValue(dflt, name, vptr);
383  }
384  return ret;
385 }
386 
387 /**
388  * \brief Retrieve a configuration value as an integer.
389  *
390  * \param name Name of configuration parameter to get.
391  * \param val Pointer to an intmax_t that will be set the
392  * configuration value.
393  *
394  * \retval 1 will be returned if the name is found and was properly
395  * converted to an interger, otherwise 0 will be returned.
396  */
397 int ConfGetInt(const char *name, intmax_t *val)
398 {
399  const char *strval = NULL;
400  intmax_t tmpint;
401  char *endptr;
402 
403  if (ConfGet(name, &strval) == 0)
404  return 0;
405 
406  if (strval == NULL) {
407  SCLogError("malformed integer value "
408  "for %s: NULL",
409  name);
410  return 0;
411  }
412 
413  errno = 0;
414  tmpint = strtoimax(strval, &endptr, 0);
415  if (strval[0] == '\0' || *endptr != '\0') {
416  SCLogError("malformed integer value "
417  "for %s: '%s'",
418  name, strval);
419  return 0;
420  }
421  if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
422  SCLogError("integer value for %s out "
423  "of range: '%s'",
424  name, strval);
425  return 0;
426  }
427 
428  *val = tmpint;
429  return 1;
430 }
431 
432 int ConfGetChildValueInt(const ConfNode *base, const char *name, intmax_t *val)
433 {
434  const char *strval = NULL;
435  intmax_t tmpint;
436  char *endptr;
437 
438  if (ConfGetChildValue(base, name, &strval) == 0)
439  return 0;
440  errno = 0;
441  tmpint = strtoimax(strval, &endptr, 0);
442  if (strval[0] == '\0' || *endptr != '\0') {
443  SCLogError("malformed integer value "
444  "for %s with base %s: '%s'",
445  name, base->name, strval);
446  return 0;
447  }
448  if (errno == ERANGE && (tmpint == INTMAX_MAX || tmpint == INTMAX_MIN)) {
449  SCLogError("integer value for %s with "
450  " base %s out of range: '%s'",
451  name, base->name, strval);
452  return 0;
453  }
454 
455  *val = tmpint;
456  return 1;
457 
458 }
459 
460 int ConfGetChildValueIntWithDefault(const ConfNode *base, const ConfNode *dflt,
461  const char *name, intmax_t *val)
462 {
463  int ret = ConfGetChildValueInt(base, name, val);
464  /* Get 'default' value */
465  if (ret == 0 && dflt) {
466  return ConfGetChildValueInt(dflt, name, val);
467  }
468  return ret;
469 }
470 
471 
472 /**
473  * \brief Retrieve a configuration value as an boolen.
474  *
475  * \param name Name of configuration parameter to get.
476  * \param val Pointer to an int that will be set to 1 for true, or 0
477  * for false.
478  *
479  * \retval 1 will be returned if the name is found and was properly
480  * converted to a boolean, otherwise 0 will be returned.
481  */
482 int ConfGetBool(const char *name, int *val)
483 {
484  const char *strval = NULL;
485 
486  *val = 0;
487  if (ConfGet(name, &strval) != 1)
488  return 0;
489 
490  *val = ConfValIsTrue(strval);
491 
492  return 1;
493 }
494 
495 /**
496  * Get a boolean value from the provided ConfNode.
497  *
498  * \retval 1 If the value exists, 0 if not.
499  */
500 int ConfGetChildValueBool(const ConfNode *base, const char *name, int *val)
501 {
502  const char *strval = NULL;
503 
504  *val = 0;
505  if (ConfGetChildValue(base, name, &strval) == 0)
506  return 0;
507 
508  *val = ConfValIsTrue(strval);
509 
510  return 1;
511 }
512 
514  const char *name, int *val)
515 {
516  int ret = ConfGetChildValueBool(base, name, val);
517  /* Get 'default' value */
518  if (ret == 0 && dflt) {
519  return ConfGetChildValueBool(dflt, name, val);
520  }
521  return ret;
522 }
523 
524 
525 /**
526  * \brief Check if a value is true.
527  *
528  * The value is considered true if it is a string with the value of 1,
529  * yes, true or on. The test is not case sensitive, any other value
530  * is false.
531  *
532  * \param val The string to test for a true value.
533  *
534  * \retval 1 If the value is true, 0 if not.
535  */
536 int ConfValIsTrue(const char *val)
537 {
538  const char *trues[] = {"1", "yes", "true", "on"};
539  size_t u;
540 
541  for (u = 0; u < sizeof(trues) / sizeof(trues[0]); u++) {
542  if (strcasecmp(val, trues[u]) == 0) {
543  return 1;
544  }
545  }
546 
547  return 0;
548 }
549 
550 /**
551  * \brief Check if a value is false.
552  *
553  * The value is considered false if it is a string with the value of 0,
554  * no, false or off. The test is not case sensitive, any other value
555  * is not false.
556  *
557  * \param val The string to test for a false value.
558  *
559  * \retval 1 If the value is false, 0 if not.
560  */
561 int ConfValIsFalse(const char *val)
562 {
563  const char *falses[] = {"0", "no", "false", "off"};
564  size_t u;
565 
566  for (u = 0; u < sizeof(falses) / sizeof(falses[0]); u++) {
567  if (strcasecmp(val, falses[u]) == 0) {
568  return 1;
569  }
570  }
571 
572  return 0;
573 }
574 
575 /**
576  * \brief Retrieve a configuration value as a double
577  *
578  * \param name Name of configuration parameter to get.
579  * \param val Pointer to an double that will be set the
580  * configuration value.
581  *
582  * \retval 1 will be returned if the name is found and was properly
583  * converted to a double, otherwise 0 will be returned.
584  */
585 int ConfGetDouble(const char *name, double *val)
586 {
587  const char *strval = NULL;
588  double tmpdo;
589  char *endptr;
590 
591  if (ConfGet(name, &strval) == 0)
592  return 0;
593 
594  errno = 0;
595  tmpdo = strtod(strval, &endptr);
596  if (strval[0] == '\0' || *endptr != '\0')
597  return 0;
598  if (errno == ERANGE)
599  return 0;
600 
601  *val = tmpdo;
602  return 1;
603 }
604 
605 /**
606  * \brief Retrieve a configuration value as a float
607  *
608  * \param name Name of configuration parameter to get.
609  * \param val Pointer to an float that will be set the
610  * configuration value.
611  *
612  * \retval 1 will be returned if the name is found and was properly
613  * converted to a double, otherwise 0 will be returned.
614  */
615 int ConfGetFloat(const char *name, float *val)
616 {
617  const char *strval = NULL;
618  double tmpfl;
619  char *endptr;
620 
621  if (ConfGet(name, &strval) == 0)
622  return 0;
623 
624  errno = 0;
625  tmpfl = strtof(strval, &endptr);
626  if (strval[0] == '\0' || *endptr != '\0')
627  return 0;
628  if (errno == ERANGE)
629  return 0;
630 
631  *val = tmpfl;
632  return 1;
633 }
634 
635 /**
636  * \brief Remove (and SCFree) the provided configuration node.
637  */
639 {
640  if (node->parent != NULL)
641  TAILQ_REMOVE(&node->parent->head, node, next);
642  ConfNodeFree(node);
643 }
644 
645 /**
646  * \brief Remove a configuration parameter from the configuration db.
647  *
648  * \param name The name of the configuration parameter to remove.
649  *
650  * \retval Returns 1 if the parameter was removed, otherwise 0 is returned
651  * most likely indicating the parameter was not set.
652  */
653 int ConfRemove(const char *name)
654 {
655  ConfNode *node;
656 
657  node = ConfGetNode(name);
658  if (node == NULL)
659  return 0;
660  else {
661  ConfNodeRemove(node);
662  return 1;
663  }
664 }
665 
666 /**
667  * \brief Creates a backup of the conf_hash hash_table used by the conf API.
668  */
670 {
671  root_backup = root;
672  root = NULL;
673 
674  return;
675 }
676 
677 /**
678  * \brief Restores the backup of the hash_table present in backup_conf_hash
679  * back to conf_hash.
680  */
682 {
683  root = root_backup;
684  root_backup = NULL;
685 
686  return;
687 }
688 
689 /**
690  * \brief De-initializes the configuration system.
691  */
692 void ConfDeInit(void)
693 {
694  if (root != NULL) {
695  ConfNodeFree(root);
696  root = NULL;
697  }
698 
699  SCLogDebug("configuration module de-initialized");
700 }
701 
702 static char *ConfPrintNameArray(char **name_arr, int level)
703 {
704  static char name[128*128];
705  int i;
706 
707  name[0] = '\0';
708  for (i = 0; i <= level; i++) {
709  strlcat(name, name_arr[i], sizeof(name));
710  if (i < level)
711  strlcat(name, ".", sizeof(name));
712  }
713 
714  return name;
715 }
716 
717 /**
718  * \brief Dump a configuration node and all its children.
719  */
720 void ConfNodeDump(const ConfNode *node, const char *prefix)
721 {
722  ConfNode *child;
723 
724  static char *name[128];
725  static int level = -1;
726 
727  level++;
728  TAILQ_FOREACH(child, &node->head, next) {
729  name[level] = SCStrdup(child->name);
730  if (unlikely(name[level] == NULL)) {
731  continue;
732  }
733  if (prefix == NULL) {
734  printf("%s = %s\n", ConfPrintNameArray(name, level),
735  child->val);
736  }
737  else {
738  printf("%s.%s = %s\n", prefix,
739  ConfPrintNameArray(name, level), child->val);
740  }
741  ConfNodeDump(child, prefix);
742  SCFree(name[level]);
743  }
744  level--;
745 }
746 
747 /**
748  * \brief Dump configuration to stdout.
749  */
750 void ConfDump(void)
751 {
752  ConfNodeDump(root, NULL);
753 }
754 
755 /**
756  * \brief Check if a node has any children.
757  *
758  * Checks if the provided node has any children. Any node that is a
759  * YAML map or array will have children.
760  *
761  * \param node The node to check.
762  *
763  * \retval true if node has children
764  * \retval false if node does not have children
765  */
766 bool ConfNodeHasChildren(const ConfNode *node)
767 {
768  if (TAILQ_EMPTY(&node->head)) {
769  return false;
770  }
771  return true;
772 }
773 
774 /**
775  * \brief Lookup a child configuration node by name.
776  *
777  * Given a ConfNode this function will lookup an immediate child
778  * ConfNode by name and return the child ConfNode.
779  *
780  * \param node The parent configuration node.
781  * \param name The name of the child node to lookup.
782  *
783  * \retval A pointer the child ConfNode if found otherwise NULL.
784  */
785 ConfNode *ConfNodeLookupChild(const ConfNode *node, const char *name)
786 {
787  ConfNode *child;
788 
789  if (node == NULL || name == NULL) {
790  return NULL;
791  }
792 
793  TAILQ_FOREACH(child, &node->head, next) {
794  if (child->name != NULL && strcmp(child->name, name) == 0)
795  return child;
796  }
797 
798  return NULL;
799 }
800 
801 /**
802  * \brief Lookup the value of a child configuration node by name.
803  *
804  * Given a parent ConfNode this function will return the value of a
805  * child configuration node by name returning a reference to that
806  * value.
807  *
808  * \param node The parent configuration node.
809  * \param name The name of the child node to lookup.
810  *
811  * \retval A pointer the child ConfNodes value if found otherwise NULL.
812  */
813 const char *ConfNodeLookupChildValue(const ConfNode *node, const char *name)
814 {
815  ConfNode *child;
816 
817  child = ConfNodeLookupChild(node, name);
818  if (child != NULL)
819  return child->val;
820 
821  return NULL;
822 }
823 
824 /**
825  * \brief Lookup for a key value under a specific node
826  *
827  * \return the ConfNode matching or NULL
828  */
829 
830 ConfNode *ConfNodeLookupKeyValue(const ConfNode *base, const char *key,
831  const char *value)
832 {
833  ConfNode *child;
834 
835  TAILQ_FOREACH(child, &base->head, next) {
836  if (!strncmp(child->val, key, strlen(child->val))) {
837  ConfNode *subchild;
838  TAILQ_FOREACH(subchild, &child->head, next) {
839  if ((!strcmp(subchild->name, key)) && (!strcmp(subchild->val, value))) {
840  return child;
841  }
842  }
843  }
844  }
845 
846  return NULL;
847 }
848 
849 /**
850  * \brief Test if a configuration node has a true value.
851  *
852  * \param node The parent configuration node.
853  * \param name The name of the child node to test.
854  *
855  * \retval 1 if the child node has a true value, otherwise 0 is
856  * returned, even if the child node does not exist.
857  */
858 int ConfNodeChildValueIsTrue(const ConfNode *node, const char *key)
859 {
860  const char *val;
861 
862  val = ConfNodeLookupChildValue(node, key);
863 
864  return val != NULL ? ConfValIsTrue(val) : 0;
865 }
866 
867 /**
868  * \brief Create the path for an include entry
869  * \param file The name of the file
870  * \retval str Pointer to the string path + sig_file
871  */
872 char *ConfLoadCompleteIncludePath(const char *file)
873 {
874  const char *defaultpath = NULL;
875  char *path = NULL;
876 
877  /* Path not specified */
878  if (PathIsRelative(file)) {
879  if (ConfGet("include-path", &defaultpath) == 1) {
880  SCLogDebug("Default path: %s", defaultpath);
881  size_t path_len = sizeof(char) * (strlen(defaultpath) +
882  strlen(file) + 2);
883  path = SCMalloc(path_len);
884  if (unlikely(path == NULL))
885  return NULL;
886  strlcpy(path, defaultpath, path_len);
887  if (path[strlen(path) - 1] != '/')
888  strlcat(path, "/", path_len);
889  strlcat(path, file, path_len);
890  } else {
891  path = SCStrdup(file);
892  if (unlikely(path == NULL))
893  return NULL;
894  }
895  } else {
896  path = SCStrdup(file);
897  if (unlikely(path == NULL))
898  return NULL;
899  }
900  return path;
901 }
902 
903 /**
904  * \brief Prune a configuration node.
905  *
906  * Pruning a configuration is similar to freeing, but only fields that
907  * may be overridden are, leaving final type parameters. Additional
908  * the value of the provided node is also free'd, but the node itself
909  * is left.
910  *
911  * \param node The configuration node to prune.
912  */
914 {
915  ConfNode *item, *it;
916 
917  for (item = TAILQ_FIRST(&node->head); item != NULL; item = it) {
918  it = TAILQ_NEXT(item, next);
919  if (!item->final) {
920  ConfNodePrune(item);
921  if (TAILQ_EMPTY(&item->head)) {
922  TAILQ_REMOVE(&node->head, item, next);
923  if (item->name != NULL)
924  SCFree(item->name);
925  if (item->val != NULL)
926  SCFree(item->val);
927  SCFree(item);
928  }
929  }
930  }
931 
932  if (node->val != NULL) {
933  SCFree(node->val);
934  node->val = NULL;
935  }
936 }
937 
938 /**
939  * \brief Check if a node is a sequence or node.
940  *
941  * \param node the node to check.
942  *
943  * \return 1 if node is a seuence, otherwise 0.
944  */
945 int ConfNodeIsSequence(const ConfNode *node)
946 {
947  return node->is_seq == 0 ? 0 : 1;
948 }
949 
950 /**
951  * @brief Finds an interface from the list of interfaces.
952  * @param ifaces_node_name - name of the node which holds a list of intefaces
953  * @param iface - interfaces name
954  * @return NULL on failure otherwise a valid pointer
955  */
956 ConfNode *ConfSetIfaceNode(const char *ifaces_node_name, const char *iface)
957 {
958  ConfNode *if_node;
959  ConfNode *ifaces_list_node;
960  /* Find initial node which holds all interfaces */
961  ifaces_list_node = ConfGetNode(ifaces_node_name);
962  if (ifaces_list_node == NULL) {
963  SCLogError("unable to find %s config", ifaces_node_name);
964  return NULL;
965  }
966 
967  if_node = ConfFindDeviceConfig(ifaces_list_node, iface);
968  if (if_node == NULL)
969  SCLogNotice("unable to find interface %s in DPDK config", iface);
970 
971  return if_node;
972 }
973 
974 /**
975  * @brief Finds and sets root and default node of the interface.
976  * @param ifaces_node_name Node which holds list of interfaces
977  * @param iface Name of the interface e.g. eth3
978  * @param if_root Node which will hold the interface configuration
979  * @param if_default Node which is the default configuration in the given list of interfaces
980  * @return 0 on success, -ENODEV when neither the root interface nor the default interface was found
981  */
983  const char *ifaces_node_name, const char *iface, ConfNode **if_root, ConfNode **if_default)
984 {
985  const char *default_iface = "default";
986  *if_root = ConfSetIfaceNode(ifaces_node_name, iface);
987  *if_default = ConfSetIfaceNode(ifaces_node_name, default_iface);
988 
989  if (*if_root == NULL && *if_default == NULL) {
990  SCLogError("unable to find configuration for the interface \"%s\" or the default "
991  "configuration (\"%s\")",
992  iface, default_iface);
993  return (-ENODEV);
994  }
995 
996  /* If there is no setting for current interface use default one as main iface */
997  if (*if_root == NULL) {
998  *if_root = *if_default;
999  *if_default = NULL;
1000  }
1001  return 0;
1002 }
1003 
1004 #ifdef UNITTESTS
1005 
1006 /**
1007  * Lookup a non-existant value.
1008  */
1009 static int ConfTestGetNonExistant(void)
1010 {
1011  char name[] = "non-existant-value";
1012  const char *value;
1013 
1014  FAIL_IF(ConfGet(name, &value));
1015  PASS;
1016 }
1017 
1018 /**
1019  * Set then lookup a value.
1020  */
1021 static int ConfTestSetAndGet(void)
1022 {
1023  char name[] = "some-name";
1024  char value[] = "some-value";
1025  const char *value0 = NULL;
1026 
1027  FAIL_IF(ConfSet(name, value) != 1);
1028  FAIL_IF(ConfGet(name, &value0) != 1);
1029  FAIL_IF(value0 == NULL);
1030  FAIL_IF(strcmp(value, value0) != 0);
1031 
1032  /* Cleanup. */
1033  ConfRemove(name);
1034 
1035  PASS;
1036 }
1037 
1038 /**
1039  * Test that overriding a value is allowed provided allow_override is
1040  * true and that the config parameter gets the new value.
1041  */
1042 static int ConfTestOverrideValue1(void)
1043 {
1044  char name[] = "some-name";
1045  char value0[] = "some-value";
1046  char value1[] = "new-value";
1047  const char *val = NULL;
1048 
1049  FAIL_IF(ConfSet(name, value0) != 1);
1050  FAIL_IF(ConfSet(name, value1) != 1);
1051  FAIL_IF(ConfGet(name, &val) != 1);
1052  FAIL_IF(val == NULL);
1053  FAIL_IF(strcmp(val, value1) != 0);
1054 
1055  /* Cleanup. */
1056  ConfRemove(name);
1057 
1058  PASS;
1059 }
1060 
1061 /**
1062  * Test that a final value will not be overrided by a ConfSet.
1063  */
1064 static int ConfTestOverrideValue2(void)
1065 {
1066  char name[] = "some-name";
1067  char value0[] = "some-value";
1068  char value1[] = "new-value";
1069  const char *val = NULL;
1070 
1071  FAIL_IF(ConfSetFinal(name, value0) != 1);
1072  FAIL_IF(ConfSet(name, value1) != 0);
1073  FAIL_IF(ConfGet(name, &val) != 1);
1074  FAIL_IF(val == NULL);
1075  FAIL_IF(strcmp(val, value0) != 0);
1076 
1077  /* Cleanup. */
1078  ConfRemove(name);
1079 
1080  PASS;
1081 }
1082 
1083 /**
1084  * Test retrieving an integer value from the configuration db.
1085  */
1086 static int ConfTestGetInt(void)
1087 {
1088  char name[] = "some-int.x";
1089  intmax_t val;
1090 
1091  FAIL_IF(ConfSet(name, "0") != 1);
1092  FAIL_IF(ConfGetInt(name, &val) != 1);
1093  FAIL_IF(val != 0);
1094 
1095  FAIL_IF(ConfSet(name, "-1") != 1);
1096  FAIL_IF(ConfGetInt(name, &val) != 1);
1097  FAIL_IF(val != -1);
1098 
1099  FAIL_IF(ConfSet(name, "0xffff") != 1);
1100  FAIL_IF(ConfGetInt(name, &val) != 1);
1101  FAIL_IF(val != 0xffff);
1102 
1103  FAIL_IF(ConfSet(name, "not-an-int") != 1);
1104  FAIL_IF(ConfGetInt(name, &val) != 0);
1105 
1106  PASS;
1107 }
1108 
1109 /**
1110  * Test retrieving a boolean value from the configuration db.
1111  */
1112 static int ConfTestGetBool(void)
1113 {
1114  char name[] = "some-bool";
1115  const char *trues[] = {
1116  "1",
1117  "on", "ON",
1118  "yes", "YeS",
1119  "true", "TRUE",
1120  };
1121  const char *falses[] = {
1122  "0",
1123  "something",
1124  "off", "OFF",
1125  "false", "FalSE",
1126  "no", "NO",
1127  };
1128  int val;
1129  size_t u;
1130 
1131  for (u = 0; u < sizeof(trues) / sizeof(trues[0]); u++) {
1132  FAIL_IF(ConfSet(name, trues[u]) != 1);
1133  FAIL_IF(ConfGetBool(name, &val) != 1);
1134  FAIL_IF(val != 1);
1135  }
1136 
1137  for (u = 0; u < sizeof(falses) / sizeof(falses[0]); u++) {
1138  FAIL_IF(ConfSet(name, falses[u]) != 1);
1139  FAIL_IF(ConfGetBool(name, &val) != 1);
1140  FAIL_IF(val != 0);
1141  }
1142 
1143  PASS;
1144 }
1145 
1146 static int ConfNodeLookupChildTest(void)
1147 {
1148  const char *test_vals[] = { "one", "two", "three" };
1149  size_t u;
1150 
1151  ConfNode *parent = ConfNodeNew();
1152  ConfNode *child;
1153 
1154  for (u = 0; u < sizeof(test_vals)/sizeof(test_vals[0]); u++) {
1155  child = ConfNodeNew();
1156  child->name = SCStrdup(test_vals[u]);
1157  child->val = SCStrdup(test_vals[u]);
1158  TAILQ_INSERT_TAIL(&parent->head, child, next);
1159  }
1160 
1161  child = ConfNodeLookupChild(parent, "one");
1162  FAIL_IF(child == NULL);
1163  FAIL_IF(strcmp(child->name, "one") != 0);
1164  FAIL_IF(strcmp(child->val, "one") != 0);
1165 
1166  child = ConfNodeLookupChild(parent, "two");
1167  FAIL_IF(child == NULL);
1168  FAIL_IF(strcmp(child->name, "two") != 0);
1169  FAIL_IF(strcmp(child->val, "two") != 0);
1170 
1171  child = ConfNodeLookupChild(parent, "three");
1172  FAIL_IF(child == NULL);
1173  FAIL_IF(strcmp(child->name, "three") != 0);
1174  FAIL_IF(strcmp(child->val, "three") != 0);
1175 
1176  child = ConfNodeLookupChild(parent, "four");
1177  FAIL_IF(child != NULL);
1178 
1179  FAIL_IF(ConfNodeLookupChild(NULL, NULL) != NULL);
1180 
1181  if (parent != NULL) {
1182  ConfNodeFree(parent);
1183  }
1184 
1185  PASS;
1186 }
1187 
1188 static int ConfNodeLookupChildValueTest(void)
1189 {
1190  const char *test_vals[] = { "one", "two", "three" };
1191  size_t u;
1192 
1193  ConfNode *parent = ConfNodeNew();
1194  ConfNode *child;
1195  const char *value;
1196 
1197  for (u = 0; u < sizeof(test_vals)/sizeof(test_vals[0]); u++) {
1198  child = ConfNodeNew();
1199  child->name = SCStrdup(test_vals[u]);
1200  child->val = SCStrdup(test_vals[u]);
1201  TAILQ_INSERT_TAIL(&parent->head, child, next);
1202  }
1203 
1204  value = (char *)ConfNodeLookupChildValue(parent, "one");
1205  FAIL_IF(value == NULL);
1206  FAIL_IF(strcmp(value, "one") != 0);
1207 
1208  value = (char *)ConfNodeLookupChildValue(parent, "two");
1209  FAIL_IF(value == NULL);
1210  FAIL_IF(strcmp(value, "two") != 0);
1211 
1212  value = (char *)ConfNodeLookupChildValue(parent, "three");
1213  FAIL_IF(value == NULL);
1214  FAIL_IF(strcmp(value, "three") != 0);
1215 
1216  value = (char *)ConfNodeLookupChildValue(parent, "four");
1217  FAIL_IF(value != NULL);
1218 
1219  ConfNodeFree(parent);
1220 
1221  PASS;
1222 }
1223 
1224 static int ConfGetChildValueWithDefaultTest(void)
1225 {
1226  const char *val = "";
1228  ConfInit();
1229  ConfSet("af-packet.0.interface", "eth0");
1230  ConfSet("af-packet.1.interface", "default");
1231  ConfSet("af-packet.1.cluster-type", "cluster_cpu");
1232 
1233  ConfNode *myroot = ConfGetNode("af-packet.0");
1234  ConfNode *dflt = ConfGetNode("af-packet.1");
1235  ConfGetChildValueWithDefault(myroot, dflt, "cluster-type", &val);
1236  FAIL_IF(strcmp(val, "cluster_cpu"));
1237 
1238  ConfSet("af-packet.0.cluster-type", "cluster_flow");
1239  ConfGetChildValueWithDefault(myroot, dflt, "cluster-type", &val);
1240 
1241  FAIL_IF(strcmp(val, "cluster_flow"));
1242 
1243  ConfDeInit();
1245  PASS;
1246 }
1247 
1248 static int ConfGetChildValueIntWithDefaultTest(void)
1249 {
1250  intmax_t val = 0;
1252  ConfInit();
1253  ConfSet("af-packet.0.interface", "eth0");
1254  ConfSet("af-packet.1.interface", "default");
1255  ConfSet("af-packet.1.threads", "2");
1256 
1257  ConfNode *myroot = ConfGetNode("af-packet.0");
1258  ConfNode *dflt = ConfGetNode("af-packet.1");
1259  ConfGetChildValueIntWithDefault(myroot, dflt, "threads", &val);
1260  FAIL_IF(val != 2);
1261 
1262  ConfSet("af-packet.0.threads", "1");
1263  ConfGetChildValueIntWithDefault(myroot, dflt, "threads", &val);
1264  FAIL_IF(val != 1);
1265 
1266  ConfDeInit();
1268 
1269  PASS;
1270 }
1271 
1272 static int ConfGetChildValueBoolWithDefaultTest(void)
1273 {
1274  int val;
1276  ConfInit();
1277  ConfSet("af-packet.0.interface", "eth0");
1278  ConfSet("af-packet.1.interface", "default");
1279  ConfSet("af-packet.1.use-mmap", "yes");
1280 
1281  ConfNode *myroot = ConfGetNode("af-packet.0");
1282  ConfNode *dflt = ConfGetNode("af-packet.1");
1283  ConfGetChildValueBoolWithDefault(myroot, dflt, "use-mmap", &val);
1284  FAIL_IF(val == 0);
1285 
1286  ConfSet("af-packet.0.use-mmap", "no");
1287  ConfGetChildValueBoolWithDefault(myroot, dflt, "use-mmap", &val);
1288  FAIL_IF(val);
1289 
1290  ConfDeInit();
1292 
1293  PASS;
1294 }
1295 
1296 /**
1297  * Test the removal of a configuration node.
1298  */
1299 static int ConfNodeRemoveTest(void)
1300 {
1302  ConfInit();
1303 
1304  FAIL_IF(ConfSet("some.nested.parameter", "blah") != 1);
1305 
1306  ConfNode *node = ConfGetNode("some.nested.parameter");
1307  FAIL_IF(node == NULL);
1308  ConfNodeRemove(node);
1309 
1310  node = ConfGetNode("some.nested.parameter");
1311  FAIL_IF(node != NULL);
1312 
1313  ConfDeInit();
1315 
1316  PASS;
1317 }
1318 
1319 static int ConfSetTest(void)
1320 {
1322  ConfInit();
1323 
1324  /* Set some value with 2 levels. */
1325  FAIL_IF(ConfSet("one.two", "three") != 1);
1326  ConfNode *n = ConfGetNode("one.two");
1327  FAIL_IF(n == NULL);
1328 
1329  /* Set another 2 level parameter with the same first level, this
1330  * used to trigger a bug that caused the second level of the name
1331  * to become a first level node. */
1332  FAIL_IF(ConfSet("one.three", "four") != 1);
1333 
1334  n = ConfGetNode("one.three");
1335  FAIL_IF(n == NULL);
1336 
1337  /* A top level node of "three" should not exist. */
1338  n = ConfGetNode("three");
1339  FAIL_IF(n != NULL);
1340 
1341  ConfDeInit();
1343 
1344  PASS;
1345 }
1346 
1347 static int ConfGetNodeOrCreateTest(void)
1348 {
1349  ConfNode *node;
1350 
1352  ConfInit();
1353 
1354  /* Get a node that should not exist, give it a value, re-get it
1355  * and make sure the second time it returns the existing node. */
1356  node = ConfGetNodeOrCreate("node0", 0);
1357  FAIL_IF(node == NULL);
1358  FAIL_IF(node->parent == NULL || node->parent != root);
1359  FAIL_IF(node->val != NULL);
1360  node->val = SCStrdup("node0");
1361  node = ConfGetNodeOrCreate("node0", 0);
1362  FAIL_IF(node == NULL);
1363  FAIL_IF(node->val == NULL);
1364  FAIL_IF(strcmp(node->val, "node0") != 0);
1365 
1366  /* Do the same, but for something deeply nested. */
1367  node = ConfGetNodeOrCreate("parent.child.grandchild", 0);
1368  FAIL_IF(node == NULL);
1369  FAIL_IF(node->parent == NULL || node->parent == root);
1370  FAIL_IF(node->val != NULL);
1371  node->val = SCStrdup("parent.child.grandchild");
1372  node = ConfGetNodeOrCreate("parent.child.grandchild", 0);
1373  FAIL_IF(node == NULL);
1374  FAIL_IF(node->val == NULL);
1375  FAIL_IF(strcmp(node->val, "parent.child.grandchild") != 0);
1376 
1377  /* Test that 2 child nodes have the same root. */
1378  ConfNode *child1 = ConfGetNodeOrCreate("parent.kids.child1", 0);
1379  ConfNode *child2 = ConfGetNodeOrCreate("parent.kids.child2", 0);
1380  FAIL_IF(child1 == NULL || child2 == NULL);
1381  FAIL_IF(child1->parent != child2->parent);
1382  FAIL_IF(strcmp(child1->parent->name, "kids") != 0);
1383 
1384  ConfDeInit();
1386 
1387  PASS;
1388 }
1389 
1390 static int ConfNodePruneTest(void)
1391 {
1392  ConfNode *node;
1393 
1395  ConfInit();
1396 
1397  /* Test that final nodes exist after a prune. */
1398  FAIL_IF(ConfSet("node.notfinal", "notfinal") != 1);
1399  FAIL_IF(ConfSetFinal("node.final", "final") != 1);
1400  FAIL_IF(ConfGetNode("node.notfinal") == NULL);
1401  FAIL_IF(ConfGetNode("node.final") == NULL);
1402  FAIL_IF((node = ConfGetNode("node")) == NULL);
1403  ConfNodePrune(node);
1404  FAIL_IF(ConfGetNode("node.notfinal") != NULL);
1405  FAIL_IF(ConfGetNode("node.final") == NULL);
1406 
1407  /* Test that everything under a final node exists after a prune. */
1408  FAIL_IF(ConfSet("node.final.one", "one") != 1);
1409  FAIL_IF(ConfSet("node.final.two", "two") != 1);
1410  ConfNodePrune(node);
1411  FAIL_IF(ConfNodeLookupChild(node, "final") == NULL);
1412  FAIL_IF(ConfGetNode("node.final.one") == NULL);
1413  FAIL_IF(ConfGetNode("node.final.two") == NULL);
1414 
1415  ConfDeInit();
1417 
1418  PASS;
1419 }
1420 
1421 static int ConfNodeIsSequenceTest(void)
1422 {
1423  ConfNode *node = ConfNodeNew();
1424  FAIL_IF(node == NULL);
1425  FAIL_IF(ConfNodeIsSequence(node));
1426  node->is_seq = 1;
1427  FAIL_IF(!ConfNodeIsSequence(node));
1428 
1429  if (node != NULL) {
1430  ConfNodeFree(node);
1431  }
1432  PASS;
1433 }
1434 
1435 static int ConfSetFromStringTest(void)
1436 {
1437  ConfNode *n;
1438 
1440  ConfInit();
1441 
1442  FAIL_IF_NOT(ConfSetFromString("stream.midstream=true", 0));
1443  n = ConfGetNode("stream.midstream");
1444  FAIL_IF_NULL(n);
1445  FAIL_IF_NULL(n->val);
1446  FAIL_IF(strcmp("true", n->val));
1447 
1448  FAIL_IF_NOT(ConfSetFromString("stream.midstream =false", 0));
1449  n = ConfGetNode("stream.midstream");
1450  FAIL_IF_NULL(n);
1451  FAIL_IF(n->val == NULL || strcmp("false", n->val));
1452 
1453  FAIL_IF_NOT(ConfSetFromString("stream.midstream= true", 0));
1454  n = ConfGetNode("stream.midstream");
1455  FAIL_IF_NULL(n);
1456  FAIL_IF(n->val == NULL || strcmp("true", n->val));
1457 
1458  FAIL_IF_NOT(ConfSetFromString("stream.midstream = false", 0));
1459  n = ConfGetNode("stream.midstream");
1460  FAIL_IF_NULL(n);
1461  FAIL_IF(n->val == NULL || strcmp("false", n->val));
1462 
1463  ConfDeInit();
1465  PASS;
1466 }
1467 
1468 static int ConfNodeHasChildrenTest(void)
1469 {
1471  ConfInit();
1472 
1473  /* Set a plain key with value. */
1474  ConfSet("no-children", "value");
1475  ConfNode *n = ConfGetNode("no-children");
1476  FAIL_IF_NULL(n);
1478 
1479  /* Set a key with a sub key to a value. This makes the first key a
1480  * map. */
1481  ConfSet("parent.child", "value");
1482  n = ConfGetNode("parent");
1483  FAIL_IF_NULL(n);
1485 
1486  ConfDeInit();
1488  PASS;
1489 }
1490 
1492 {
1493  UtRegisterTest("ConfTestGetNonExistant", ConfTestGetNonExistant);
1494  UtRegisterTest("ConfSetTest", ConfSetTest);
1495  UtRegisterTest("ConfTestSetAndGet", ConfTestSetAndGet);
1496  UtRegisterTest("ConfTestOverrideValue1", ConfTestOverrideValue1);
1497  UtRegisterTest("ConfTestOverrideValue2", ConfTestOverrideValue2);
1498  UtRegisterTest("ConfTestGetInt", ConfTestGetInt);
1499  UtRegisterTest("ConfTestGetBool", ConfTestGetBool);
1500  UtRegisterTest("ConfNodeLookupChildTest", ConfNodeLookupChildTest);
1501  UtRegisterTest("ConfNodeLookupChildValueTest",
1502  ConfNodeLookupChildValueTest);
1503  UtRegisterTest("ConfNodeRemoveTest", ConfNodeRemoveTest);
1504  UtRegisterTest("ConfGetChildValueWithDefaultTest",
1505  ConfGetChildValueWithDefaultTest);
1506  UtRegisterTest("ConfGetChildValueIntWithDefaultTest",
1507  ConfGetChildValueIntWithDefaultTest);
1508  UtRegisterTest("ConfGetChildValueBoolWithDefaultTest",
1509  ConfGetChildValueBoolWithDefaultTest);
1510  UtRegisterTest("ConfGetNodeOrCreateTest", ConfGetNodeOrCreateTest);
1511  UtRegisterTest("ConfNodePruneTest", ConfNodePruneTest);
1512  UtRegisterTest("ConfNodeIsSequenceTest", ConfNodeIsSequenceTest);
1513  UtRegisterTest("ConfSetFromStringTest", ConfSetFromStringTest);
1514  UtRegisterTest("ConfNodeHasChildrenTest", ConfNodeHasChildrenTest);
1515 }
1516 
1517 #endif /* UNITTESTS */
ConfGetChildValueInt
int ConfGetChildValueInt(const ConfNode *base, const char *name, intmax_t *val)
Definition: conf.c:432
ConfGetInt
int ConfGetInt(const char *name, intmax_t *val)
Retrieve a configuration value as an integer.
Definition: conf.c:397
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
ConfNodeChildValueIsTrue
int ConfNodeChildValueIsTrue(const ConfNode *node, const char *key)
Test if a configuration node has a true value.
Definition: conf.c:858
TAILQ_INIT
#define TAILQ_INIT(head)
Definition: queue.h:262
ConfNode_::val
char * val
Definition: conf.h:34
ConfGetBool
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as an boolen.
Definition: conf.c:482
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
ConfGetDouble
int ConfGetDouble(const char *name, double *val)
Retrieve a configuration value as a double.
Definition: conf.c:585
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
ConfGetChildValueBoolWithDefault
int ConfGetChildValueBoolWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, int *val)
Definition: conf.c:513
ConfNodeDump
void ConfNodeDump(const ConfNode *node, const char *prefix)
Dump a configuration node and all its children.
Definition: conf.c:720
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
ConfNodeNew
ConfNode * ConfNodeNew(void)
Allocate a new configuration node.
Definition: conf.c:139
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:248
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
ConfDump
void ConfDump(void)
Dump configuration to stdout.
Definition: conf.c:750
ConfNodeLookupKeyValue
ConfNode * ConfNodeLookupKeyValue(const ConfNode *base, const char *key, const char *value)
Lookup for a key value under a specific node.
Definition: conf.c:830
ConfSetFinal
int ConfSetFinal(const char *name, const char *val)
Set a final configuration value.
Definition: conf.c:303
TAILQ_INSERT_TAIL
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:294
ConfGetChildValueIntWithDefault
int ConfGetChildValueIntWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, intmax_t *val)
Definition: conf.c:460
NODE_NAME_MAX
#define NODE_NAME_MAX
Definition: conf.c:47
ConfGetChildWithDefault
ConfNode * ConfGetChildWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name)
Definition: conf.c:362
util-unittest.h
ConfNode_::is_seq
int is_seq
Definition: conf.h:36
ConfValIsTrue
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:536
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
ConfRemove
int ConfRemove(const char *name)
Remove a configuration parameter from the configuration db.
Definition: conf.c:653
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
ConfNodeFree
void ConfNodeFree(ConfNode *node)
Free a ConfNode and all of its children.
Definition: conf.c:157
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:335
ConfGetRootNode
ConfNode * ConfGetRootNode(void)
Get the root configuration node.
Definition: conf.c:207
ConfNodeRemove
void ConfNodeRemove(ConfNode *node)
Remove (and SCFree) the provided configuration node.
Definition: conf.c:638
TAILQ_REMOVE
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:312
util-debug.h
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:250
ConfNodePrune
void ConfNodePrune(ConfNode *node)
Prune a configuration node.
Definition: conf.c:913
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
strlcat
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
ConfNode_::parent
struct ConfNode_ * parent
Definition: conf.h:41
ConfFindDeviceConfig
ConfNode * ConfFindDeviceConfig(ConfNode *node, const char *iface)
Find the configuration node for a specific device.
Definition: util-conf.c:130
ConfNode_::final
int final
Definition: conf.h:39
ConfGetChildValueWithDefault
int ConfGetChildValueWithDefault(const ConfNode *base, const ConfNode *dflt, const char *name, const char **vptr)
Definition: conf.c:376
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
conf.h
ConfCreateContextBackup
void ConfCreateContextBackup(void)
Creates a backup of the conf_hash hash_table used by the conf API.
Definition: conf.c:669
ConfNodeHasChildren
bool ConfNodeHasChildren(const ConfNode *node)
Check if a node has any children.
Definition: conf.c:766
ConfGetChildValue
int ConfGetChildValue(const ConfNode *base, const char *name, const char **vptr)
Definition: conf.c:348
ConfNodeLookupChild
ConfNode * ConfNodeLookupChild(const ConfNode *node, const char *name)
Lookup a child configuration node by name.
Definition: conf.c:785
ConfGetFloat
int ConfGetFloat(const char *name, float *val)
Retrieve a configuration value as a float.
Definition: conf.c:615
util-conf.h
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
suricata-common.h
ConfRegisterTests
void ConfRegisterTests(void)
Definition: conf.c:1491
util-path.h
ConfNode_::name
char * name
Definition: conf.h:33
TAILQ_NEXT
#define TAILQ_NEXT(elm, field)
Definition: queue.h:307
ConfNodeIsSequence
int ConfNodeIsSequence(const ConfNode *node)
Check if a node is a sequence or node.
Definition: conf.c:945
ConfRestoreContextBackup
void ConfRestoreContextBackup(void)
Restores the backup of the hash_table present in backup_conf_hash back to conf_hash.
Definition: conf.c:681
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(...)
Definition: util-debug.h:502
ConfGetChildValueBool
int ConfGetChildValueBool(const ConfNode *base, const char *name, int *val)
Definition: conf.c:500
ConfSetFromString
int ConfSetFromString(const char *input, int final)
Set a configuration parameter from a string.
Definition: conf.c:249
SCMalloc
#define SCMalloc(sz)
Definition: util-mem.h:47
ConfInit
void ConfInit(void)
Initialize the configuration system.
Definition: conf.c:120
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
PathIsRelative
int PathIsRelative(const char *path)
Check if a path is relative.
Definition: util-path.c:69
ConfNodeGetNodeOrCreate
ConfNode * ConfNodeGetNodeOrCreate(ConfNode *parent, const char *name, int final)
Helper function to get a node, creating it if it does not exist.
Definition: conf.c:66
ConfValIsFalse
int ConfValIsFalse(const char *val)
Check if a value is false.
Definition: conf.c:561
ConfSetRootAndDefaultNodes
int ConfSetRootAndDefaultNodes(const char *ifaces_node_name, const char *iface, ConfNode **if_root, ConfNode **if_default)
Finds and sets root and default node of the interface.
Definition: conf.c:982
ConfDeInit
void ConfDeInit(void)
De-initializes the configuration system.
Definition: conf.c:692
ConfSet
int ConfSet(const char *name, const char *val)
Set a configuration value.
Definition: conf.c:224
ConfSetIfaceNode
ConfNode * ConfSetIfaceNode(const char *ifaces_node_name, const char *iface)
Finds an interface from the list of interfaces.
Definition: conf.c:956
ConfLoadCompleteIncludePath
char * ConfLoadCompleteIncludePath(const char *file)
Create the path for an include entry.
Definition: conf.c:872
SCLogNotice
#define SCLogNotice(...)
Macro used to log NOTICE messages.
Definition: util-debug.h:237
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
ConfNodeLookupChildValue
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
Definition: conf.c:813