📄 xsupconfwrite_globals.c
字号:
/**
* Implementation for converting variables that make up the <Globals> section
* to the libxml2 format that can be written to disk, or manipulated in other ways.
*
* Licensed under a dual GPL/BSD license. (See LICENSE file for more info.)
*
* \file xsupconfwrite_globals.c
*
* \author chris@open1x.org
*
* $Id: xsupconfwrite_globals.c,v 1.1.2.21 2008/01/29 18:31:51 chessing Exp $
* $Date: 2008/01/29 18:31:51 $
**/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#ifndef WINDOWS
#include <stdint.h>
#endif
#include "../libxsupconfig/xsupconfig_structs.h"
#include "../../src/xsup_common.h"
#include "../libxsupconfig/xsupconfig.h"
#include "../libxsupconfig/xsupconfig_vars.h"
#include "../../src/xsup_debug.h"
#include "xsupconfwrite.h"
typedef struct multichoice_struct {
int value;
char *text;
} multichoice;
multichoice debug_choices_write[] = {
{DEBUG_NORMAL, "NORMAL"},
{DEBUG_INT, "INTERFACE"},
{DEBUG_PHYSICAL_STATE, "PHYSICAL"},
{DEBUG_DOT1X_STATE, "DOT1X_STATE"},
{DEBUG_1X_BE_STATE, "DOT1X_BACKEND_STATE"},
{DEBUG_EAP_STATE, "EAP_STATE"},
{DEBUG_KEY_STATE, "KEY_STATE"},
{DEBUG_KEY, "KEY"},
{DEBUG_AUTHTYPES, "AUTHTYPES"},
{DEBUG_CONFIG_PARSE, "CONFIG_PARSE"},
{DEBUG_CONFIG_WRITE, "CONFIG_WRITE"},
{DEBUG_SMARTCARD, "SMARTCARD"},
{DEBUG_SNMP, "SNMP"},
{DEBUG_IPC, "IPC"},
{DEBUG_INIT, "INIT"},
{DEBUG_DEINIT, "DEINIT"},
{DEBUG_CONTEXT, "CONTEXT"},
{DEBUG_EVENT_CORE, "EVENT_CORE"},
{DEBUG_TLS_CORE, "TLS_CORE"},
{DEBUG_TIMERS, "TIMERS"},
{DEBUG_CERTS, "CERTIFICATES"},
{DEBUG_TNC, "TNC"},
{DEBUG_TNC_IMC, "TNC_IMC"},
{DEBUG_VERBOSE, "VERBOSE"},
{-1, NULL}};
// Uncomment the #define below to enable textual debug output.
// #define WRITE_GLOBALS_DEBUG 1
/**
* \brief Given a set of debug flags, create the libxml2 format nodes to represent
* it in the configuration file.
*
* @param[in] parent The parent node that should contain the newly created debug
* level nodes.
* @param[in] dbglevel The bitmap that represents the debug level.
*
* \retval 0 on success
* \retval -1 on failure
**/
int xsupconfwrite_globals_build_debug(xmlNodePtr parent, char write_all, uint32_t dbglevel)
{
int i = 0;
while (debug_choices_write[i].value != -1)
{
if (dbglevel == DEBUG_ALL)
{
if (xmlNewChild(parent, NULL, "Log_Level", "ALL") == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Couldn't create loglevel node!\n");
#endif
return -1;
}
return 0;
}
if (TEST_FLAG(dbglevel, debug_choices_write[i].value))
{
if ((write_all == TRUE) || (debug_choices_write[i].value != DEBUG_NORMAL))
{
if (xmlNewChild(parent, NULL, "Log_Level", debug_choices_write[i].text) == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Couldn't create loglevel node!\n");
#endif
return -1;
}
}
}
i++;
}
return 0;
}
/**
* \brief Take the variables that are part of the config_globals structure, and
* convert them to be a tree of XML nodes that libxml2 can work with.
*
* @param[in] conf_globals A config_globals structure that contains all of the
* variables that we want to convert to XML.
* @param[in] write_all If set to TRUE, we will write all of the configuration
* options to the XML node tree, no matter if their values
* are set to the default or not.
*
* \retval NULL on error
* \retval xmlNodePtr containing the <Globals> tree in a format that is used by
* libxml2.
**/
xmlNodePtr xsupconfwrite_globals_create_tree(struct config_globals *conf_globals,
char write_all)
{
xmlNodePtr globalnode = NULL;
char *temp = NULL;
char static_temp[100];
if (conf_globals == NULL) return NULL;
// Create the root node for the <Globals> block.
globalnode = xmlNewNode(NULL, "Globals");
if (globalnode == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Couldn't allocate memory to store <Globals> block!\n");
#endif
return NULL;
}
// The "if" statements below should all check two things:
//
// 1. If write_all == TRUE, then we should go ahead and build the node no
// matter what the value is.
//
// 2. If write_all == FALSE, then we should check the value for the global
// variable against the default. If the value isn't the same as the global
// default, we should create the node.
if ((write_all == TRUE) || (conf_globals->logpath != NULL))
{
if (xmlNewChild(globalnode, NULL, "Log_Path", conf_globals->logpath) == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <Logfile> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
if ((write_all == TRUE) || (conf_globals->log_facility != NULL))
{
if (xmlNewChild(globalnode, NULL, "Log_Facility", conf_globals->log_facility) == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <Log_Facility> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
if ((write_all == TRUE) || (conf_globals->ipc_group_name != NULL))
{
if (xmlNewChild(globalnode, NULL, "IPC_Group", conf_globals->ipc_group_name) == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <IPC_Group> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
if ((write_all == TRUE) || (!TEST_FLAG(conf_globals->flags, CONFIG_GLOBALS_DETECT_ON_STARTUP)))
{
if (TEST_FLAG(conf_globals->flags, CONFIG_GLOBALS_DETECT_ON_STARTUP))
{
if (xmlNewChild(globalnode, NULL, "Detect_on_startup", "yes") == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <Detect_on_startup> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
else
{
if (xmlNewChild(globalnode, NULL, "Detect_on_startup", "no") == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <Detect_on_startup> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
}
if ((write_all == TRUE) || (TEST_FLAG(conf_globals->flags, CONFIG_GLOBALS_NO_FRIENDLY_WARNINGS)))
{
if (TEST_FLAG(conf_globals->flags, CONFIG_GLOBALS_NO_FRIENDLY_WARNINGS))
{
if (xmlNewChild(globalnode, NULL, "Friendly_Warnings", "no") == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <Friendly_Warnings> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
else
{
if (xmlNewChild(globalnode, NULL, "Friendly_Warnings", "yes") == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <Friendly_Warnings> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
}
if ((write_all == TRUE) || (TEST_FLAG(conf_globals->flags, CONFIG_GLOBALS_USE_SYSLOG)))
{
if (TEST_FLAG(conf_globals->flags, CONFIG_GLOBALS_USE_SYSLOG))
{
if (xmlNewChild(globalnode, NULL, "Use_Syslog", "yes") == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <Use_Syslog> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
else
{
if (xmlNewChild(globalnode, NULL, "Use_Syslog", "no") == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <Use_Syslog> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
}
if (xsupconfwrite_globals_build_debug(globalnode, write_all, conf_globals->loglevel) != 0)
{
xmlFreeNode(globalnode);
return NULL;
}
if ((write_all == TRUE) || (!TEST_FLAG(conf_globals->flags, CONFIG_GLOBALS_ALLMULTI)))
{
if (!TEST_FLAG(conf_globals->flags, CONFIG_GLOBALS_ALLMULTI))
{
if (xmlNewChild(globalnode, NULL, "Allmulti", "no") == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <Allmulti> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
else
{
if (xmlNewChild(globalnode, NULL, "Allmulti", "yes") == NULL)
{
#ifdef WRITE_GLOBALS_CONFIG
printf("Failed to create <Allmulti> node!\n");
#endif
xmlFreeNode(globalnode);
return NULL;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -