Esempio n. 1
0
/** Delete a branch of the tree.
 * Deletes a subtree pointed to by path.  If more than one matching entry
 * exists, the first one will be deleted. If del_empty==1, and by removing
 * the parent tree becomes empty, the parents will be removed recursively.
 * If del_empty==0, an empty subtree may be left behind.
 * In any case, memory of the subtree and deleted parents will be freed.
 * @param [in] cn_root pointer to the root of the tree
 * @param [in] path the path of the tree to be deleted
 * @param [in] del_empty set to 1 if empty parents shall be removed.
 */
int cnf_del_branch(struct cnfnode *cn_root, const char *path, int del_empty)
{
	struct cnfresult *cr;

	cr = cnf_find_entry_f(cn_root, path, FIND_ENTRY_FLAG_NOPATH|FIND_ENTRY_FLAG_FIRST);
	if(cr){
		struct cnfnode *cn_top = cr->cnfnode;

		unlink_node(cn_top);

		if(del_empty){
			/* must be done after unlink_(), so it does not find this one in the search
			   below, and before destroy_(), because we still need to access the structure. */
			struct cnfnode *cn;

			for(cn = cn_top->parent; cn->parent && cn != cn_root; cn = cn->parent){
				if(cn->first_child == NULL){
					unlink_node(cn);
					destroy_cnftree(cn);
				}
			}
		}
		destroy_cnftree(cn_top);

		destroy_cnfresult_list(cr);
		return 0;
	}

	errno = ENOENT;
	return -ENOENT;
}
Esempio n. 2
0
/* Parse configuration file */
void check_ini(char *path_ini)
{
  struct cnfnode *cn_root;
  struct cnfmodule *mod_ini;
  struct cnfresult *cnf_res;

  register_ini(NULL);
  mod_ini = find_cnfmodule("ini");
  cn_root = cnfmodule_parse_file(mod_ini, path_ini);

  if (cn_root == NULL) {
    term_printf ("Could not find INI file: %s\n", path_ini);
    return;
  }

  /* Parse configuration flags */
  set_bool_from_ini(cn_root, "general/trace_only_after_first_taint",
    &conf_trace_only_after_first_taint);
  set_bool_from_ini(cn_root, "general/log_external_calls",
    &conf_log_external_calls);
  set_bool_from_ini(cn_root, "general/write_ops_at_insn_end",
    &conf_write_ops_at_insn_end);
  set_bool_from_ini(cn_root, "general/save_state_at_trace_stop",
    &conf_save_state_at_trace_stop);
  set_bool_from_ini(cn_root, "tracing/tracing_table_lookup",
    &tracing_table_lookup);
  set_bool_from_ini(cn_root, "tracing/tracing_tainted_only",
    &conf_tainted_only);
  set_bool_from_ini(cn_root, "tracing/tracing_kernel",
    &conf_tracing_kernel_all);
  set_bool_from_ini(cn_root, "tracing/tracing_kernel_tainted",
    &conf_tracing_kernel_tainted);
  set_bool_from_ini(cn_root, "tracing/tracing_kernel_partial",
    &conf_tracing_kernel_partial);

  /* Parse network configuration */
  set_bool_from_ini(cn_root, "network/ignore_dns",
    &conf_ignore_dns);
  check_filter_conf(cn_root);
  print_nic_filter();


  /* Find hook configuration file */
  cnf_res = cnf_find_entry(cn_root, "function hooks/plugin_ini");
  if (cnf_res)
  strncpy(hook_plugins_filename, cnf_res->cnfnode->value, 255);
  hook_plugins_filename[255] = '\0';
  term_printf("Loading plugin options from: %s\n", hook_plugins_filename);

  /* Find hooks directory */
  cnf_res = cnf_find_entry(cn_root, "function hooks/plugin_directory");
  if (cnf_res) {
    strncpy(hook_dirname, cnf_res->cnfnode->value, 255);
    hook_dirname[255] = '\0';
  }
  term_printf("Loading plugins from: %s\n", hook_dirname);

  destroy_cnftree(cn_root);
}
Esempio n. 3
0
/** Strip comments and empty lines from the tree.
 * In particular, all nodes with names starting with a dot are removed from the tree.
 * This will include any <tt>.comment</tt>, <tt>.empty</tt> or <tt>.unparsed</tt> nodes, for example.
 * @param [in] cn_root A pointer to the root of the tree which is to be stripped.
 */
void strip_cnftree(struct cnfnode *cn_root)
{
	struct cnfnode *cn, *cn_next;

	for(cn = cn_root->first_child; cn; cn = cn_next){
		cn_next = cn->next;

		strip_cnftree(cn);

		if(cn->name[0] == '.'){
			unlink_node(cn);
			destroy_cnftree(cn);
		}
	}
}
Esempio n. 4
0
int main(int argc, char *argv[])
{
    FILE *fout;
    struct cnfmodule *module;
    char *modname = "pslave";
    struct cnfnode *cn_root, *cn;
    struct cnfnode *cn_root_backup;
    struct cnfresult *cnf_res = NULL, *cr;
    int ret;
    char *path;
    const char *string;

    /* you have to register all modules you want to use
       somewhere in the beginning of your program: */
    register_pslave(NULL);

    /* find the module you want to use by name: */
    module = find_cnfmodule(modname);
    if(!module) {
        fprintf(stderr, "no module with name '%s' found.\n", modname);
        exit(1);
    }

#if 0
    /* open the file */
    fin = fopen(argv[1], "r");
    if(!fin) {
        fprintf(stderr, "could not open %s: %s\n", argv[1], strerror(errno));
        exit(1);
    }

    /* the parser reads from the file pointer */
    cn_root = module->parser(fin);

    /* the whole configuration is now parsed into a tree. Close the file */
    fclose(fin);

#else
    /* read and parse from file: */
    cn_root = cnfmodule_parse_file(module, argv[1]);
    if(!cn_root) {
        fprintf(stderr, "could not open %s: %s\n", argv[1], strerror(errno));
        exit(1);
    }
#endif

    /* we can now access the whole configuration through cn_root */

    /* make a backup, to be used later: */
    cn_root_backup = clone_cnftree(cn_root);

    ret = compare_cnftree(cn_root, cn_root_backup);
    printf("comparing trees returned %d\n", ret);

    /* search for some configuration entry: */
    cnf_res = cnf_find_entry(cn_root, "all/authtype");

    if(cnf_res) {
        /* cnf_res is a list with all matching entries: */
        for(cr = cnf_res; cr; cr = cr->next) {
            cn = cr->cnfnode;
            path = cr->path;
            printf("%s = %s\n", cr->path, cn->value);
        }

        /* path will contain the last full path to authtype. Save it,
           because destroy_cnfresult() will free the string: */
        path = strdup(path);

        /* Okay, let's change the last entry to 'local': */

        printf("setting '%s' to local.\n", path);

        cnf_res = cnf_find_entry(cn_root, path);
        if(cnf_res) {
            cn = cnf_res->cnfnode;
            cnfnode_setval(cn, "local");
        } else {
            fprintf(stderr, "'%s' not found\n", path);
        }
        /* idealy, we free the memory used by cnf_res: */
        destroy_cnfresult(cnf_res);
        cnf_res = NULL;
    } else {
        /* there was no entry for authtype. Let's add one.

         We can set the value at the same time.

         We do not want to merge with any existing branch,
         so we set the 'do_merge' parameter to 0: */
        cnf_add_branch(cn_root, "all/authtype=local", 0);
    }

    free(path);

    ret = compare_cnftree(cn_root, cn_root_backup);
    printf("comparing trees returned %d\n", ret);

    /* ideally, we free the memory used by cnf_res. If it's NULL, destroy_cnfresult() does nothing: */
    destroy_cnfresult(cnf_res);
    cnf_res = NULL;

    /* some other examples: */

    /* set an existing entry: */
    ret = cnf_set_entry(cn_root, "s0/authtype", "local", 0);
    if(ret < 0) {
        /* we did not want to create the entry if it does not exist,
           so we may fail: */
        printf("cnf_set_entry for '%s' failed: %s\n", "s0/authtype", strerror(errno));
    }

    /* set an entry also if it does not exist: */
    ret = cnf_set_entry(cn_root, "s1/authtype", "local", 1);
    if(ret < 0) {
        /* this should not fail: */
        printf("cnf_set_entry for '%s' failed: %s\n", "s1/authtype", strerror(errno));
    }

    /* just print an entry, if it exists: */
    string = cnf_get_entry(cn_root, "s2/authtype");
    if(string)
        printf("s2.authtype = %s\n", string);
    else
        printf("s2.authtype does not exist\n");

    /* now write the modified file: */
    fout = fopen(argv[2], "w");
    if(fout) {
        module->unparser(module, fout, cn_root);
    } else {
        fprintf(stderr, "could not open %s: %s\n", argv[2], strerror(errno));
        exit(1);
    }

    /* compare with backup: */

    ret = compare_cnftree(cn_root, cn_root_backup);
    printf("comparing trees returned %d\n", ret);

    /* clean up: */
    destroy_cnftree(cn_root);

    exit(0);
}