⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xsupconfwrite_eap_ttls.c

📁 linux 下通过802.1认证的安装包
💻 C
字号:
/**
 * Implementation for converting variables that make up the TTLS configuration 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_eap_ttls.c
 *
 * \author chris@open1x.org
 *
 * $Id: xsupconfwrite_eap_ttls.c,v 1.1.2.15 2007/09/08 01:03:41 chessing Exp $
 * $Date: 2007/09/08 01:03:41 $
 **/

#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 "xsupconfwrite_common.h"
#include "xsupconfwrite_eap.h"
#include "xsupconfwrite.h"

// Uncomment the #define below to enable textual debug output.
// #define WRITE_EAP_TTLS_DEBUG 1


/**
 * \brief Create a EAP-TTLS block for the configuration file in a format
 *        that libxml2 can understand.
 *
 * @param[in] tlsdata  A config_eap_ttls 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 TTLS configuration tree in a format that is used by 
 *         libxml2.
 **/
xmlNodePtr xsupconfwrite_eap_ttls_create_tree(struct config_eap_ttls *ttlsdata, 
										      char write_all)
{
	xmlNodePtr ttlsnode = NULL;
	xmlNodePtr p2node = NULL;
	xmlNodePtr eapnode = NULL;
	char *temp = NULL;
	char tempstatic[10];
	int ressize;
		
	if (ttlsdata == NULL) return NULL;

	ttlsnode = xsupconfwrite_common_newSibling(NULL, "Type", "TTLS");
	if (ttlsnode == NULL)
	{
#ifdef WRITE_EAP_TTLS_DEBUG
		printf("Couldn't create <Type> node for TTLS!\n");
#endif
		return NULL;
	}

	if ((write_all == TRUE) || (ttlsdata->user_cert != NULL))
	{
		if (xsupconfwrite_common_newSibling(ttlsnode, "User_Certificate", ttlsdata->user_cert) == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't create <User_Certificate> node for TTLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			return NULL;
		}
	}

	if ((write_all == TRUE) || (ttlsdata->trusted_server != NULL))
	{
		if (xsupconfwrite_common_newSibling(ttlsnode, "Trusted_Server", ttlsdata->trusted_server) == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't create <Trusted_Server> node for TTLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			free(temp);
			return NULL;
		}

		free(temp);
	}

	if ((write_all == TRUE) || (ttlsdata->crl_dir != NULL))
	{
		if (xsupconfwrite_common_newSibling(ttlsnode, "CRL_Directory", ttlsdata->crl_dir) == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't create <CRL_Directory> node for TTLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			return NULL;
		}
	}

	if ((write_all == TRUE) || (ttlsdata->user_key != NULL))
	{
		if (xsupconfwrite_common_newSibling(ttlsnode, "User_Key_File", ttlsdata->user_key) == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't create <User_Key_File> node for TTLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			return NULL;
		}
	}

	if ((write_all == TRUE) || (ttlsdata->user_key_pass != NULL))
	{
		if ((ttlsdata->user_key_pass != NULL) && (pwcrypt_funcs_available() == TRUE))
		{
			// Write the encrypted version.
			if (pwcrypt_encrypt(ttlsdata->user_key_pass, strlen(ttlsdata->user_key_pass), &temp, &ressize) != 0)
			{
				// Couldn't encrypt the data.  So write the cleartext version.
				xsupconfwrite_convert_amp(ttlsdata->user_key_pass, &temp);
				if (xsupconfwrite_common_newSibling(ttlsnode, "User_Key_Password", temp) == NULL)
				{
#ifdef WRITE_EAP_TTLS_DEBUG
					printf("Couldn't create <User_Key_Password> node for TTLS!\n");
#endif
					xmlFreeNode(ttlsnode);
					free(temp);
					return NULL;
				}

				free(temp);
			}
			else
			{
				if (xsupconfwrite_common_newSibling(ttlsnode, "Encrypted_User_Key_Password", temp) == NULL)
				{
#ifdef WRITE_EAP_TTLS_DEBUG
					printf("Couldn't create <Encrypted_User_Key_Password> node.\n");
#endif
					xmlFreeNode(ttlsnode);
					free(temp);
					return NULL;
				}

				free(temp);
			}
		}
		else
		{
			xsupconfwrite_convert_amp(ttlsdata->user_key_pass, &temp);
			if (xsupconfwrite_common_newSibling(ttlsnode, "User_Key_Password", temp) == NULL)
			{
#ifdef WRITE_EAP_TTLS_DEBUG
				printf("Couldn't create <User_Key_Password> node for TTLS!\n");
#endif
				xmlFreeNode(ttlsnode);
				free(temp);
				return NULL;
			}

			free(temp);
		}
	}

	if ((write_all == TRUE) || (ttlsdata->session_resume != RES_UNSET))
	{
		switch (ttlsdata->session_resume)
		{
		default:
		case RES_NO:
		case RES_UNSET:
			temp = _strdup("no");  // Default setting.
			break;

		case RES_YES:
			temp = _strdup("yes");
			break;
		}

		if (xsupconfwrite_common_newSibling(ttlsnode, "Session_Resume", temp) == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't create <Session_Resume> node for TTLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			free(temp);
			return NULL;
		}

		free(temp);
	}

	if ((write_all == TRUE) || (ttlsdata->validate_cert != TRUE))
	{
		switch (ttlsdata->validate_cert)
		{
		default:
		case TRUE:
			temp = _strdup("yes");  // Default setting.
			break;

		case FALSE:
			temp = _strdup("no");
			break;
		}

		if (xsupconfwrite_common_newSibling(ttlsnode, "Validate_Certificate", temp) == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't create <Validate_Certificate> node for TTLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			free(temp);
			return NULL;
		}

		free(temp);
	}

	if ((write_all == TRUE) || (ttlsdata->chunk_size != 0))
	{
		sprintf((char *)&tempstatic, "%d", ttlsdata->chunk_size);

		if (xsupconfwrite_common_newSibling(ttlsnode, "Chunk_Size", tempstatic) == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't create <Chunk_Size> node for TLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			return NULL;
		}
	}

	if ((write_all == TRUE) || (ttlsdata->random_file != NULL))
	{
		if (xsupconfwrite_common_newSibling(ttlsnode, "Random_File", ttlsdata->random_file) == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't create <Random_File> node for TTLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			return NULL;
		}
	}

	if ((write_all == TRUE) || (ttlsdata->inner_id != NULL))
	{
		xsupconfwrite_convert_amp(ttlsdata->inner_id, &temp);
		if (xsupconfwrite_common_newSibling(ttlsnode, "Inner_ID", temp) == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't create <Inner_ID> node for TTLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			free(temp);
			return NULL;
		}

		free(temp);
	}

	switch (ttlsdata->phase2_type)
	{
	case TTLS_PHASE2_PAP:
		temp = _strdup("PAP");
		break;

	case TTLS_PHASE2_CHAP:
		temp = _strdup("CHAP");
		break;

	case TTLS_PHASE2_MSCHAP:
		temp = _strdup("MSCHAP");
		break;

	case TTLS_PHASE2_MSCHAPV2:
		temp = _strdup("MSCHAPv2");
		break;

	case TTLS_PHASE2_EAP:
		temp = _strdup("EAP");
		break;
	}

	if (xsupconfwrite_common_newSibling(ttlsnode, "Inner_Method", temp) == NULL)
	{
#ifdef WRITE_EAP_TTLS_DEBUG
		printf("Couldn't create <Inner_Method> node for TTLS!\n");
#endif
		xmlFreeNode(ttlsnode);
		return NULL;
	}

	free(temp);
	temp = NULL;

	p2node = xmlNewNode(NULL, "Phase2");
	if (p2node == NULL)
	{
#ifdef WRITE_EAP_TTLS_DEBUG
		printf("Couldn't create <Phase2> node for TTLS!\n");
#endif
		xmlFreeNode(ttlsnode);
		return NULL;
	}

	p2node = xmlAddSibling(ttlsnode, p2node);
	if (p2node == NULL)
	{
#ifdef WRITE_EAP_TTLS_DEBUG
		printf("Couldn't add <Phase2> node to TTLS!\n");
#endif
		xmlFreeNode(ttlsnode);
		return NULL;
	}
	
	switch (ttlsdata->phase2_type)
	{
	case TTLS_PHASE2_PAP:
	case TTLS_PHASE2_CHAP:
	case TTLS_PHASE2_MSCHAP:
	case TTLS_PHASE2_MSCHAPV2:
		if (((struct config_pwd_only *)(ttlsdata->phase2_data)) != NULL)
		{
			if (pwcrypt_funcs_available() == TRUE)
			{
				if ((ttlsdata->phase2_data != NULL) && (((struct config_pwd_only *)(ttlsdata->phase2_data))->password != NULL))
				{
					// Write the encrypted version.
					if (pwcrypt_encrypt(((struct config_pwd_only *)(ttlsdata->phase2_data))->password, strlen(((struct config_pwd_only *)(ttlsdata->phase2_data))->password), &temp, &ressize) != 0)
					{
						// Couldn't encrypt the data.  So write the cleartext version.
						xsupconfwrite_convert_amp(((struct config_pwd_only *)(ttlsdata->phase2_data))->password, &temp);
						if (xmlNewChild(p2node, NULL, "Password", temp) == NULL)
						{
#ifdef WRITE_EAP_TTLS_DEBUG
							printf("Couldn't create <Password> node for phase 2 TTLS!\n");
#endif
							xmlFreeNode(ttlsnode);
							free(temp);
							return NULL;
						}

						free(temp);
					}
					else
					{
						if (xmlNewChild(p2node, NULL, "Encrypted_Password", temp) == NULL)
						{
#ifdef WRITE_EAP_TTLS_DEBUG
							printf("Couldn't create <Password> node for phase 2 TTLS!\n");
#endif
							free(temp);
							xmlFreeNode(ttlsnode);
							return NULL;
						}
						free(temp);
					}
				}
			}
			else
			{
				xsupconfwrite_convert_amp(((struct config_pwd_only *)(ttlsdata->phase2_data))->password, &temp);
				if (xmlNewChild(p2node, NULL, "Password", temp) == NULL)
				{
#ifdef WRITE_EAP_TTLS_DEBUG
					printf("Couldn't create <Password> node for phase 2 TTLS!\n");
#endif
					xmlFreeNode(ttlsnode);
					free(temp);
					return NULL;
				}

				free(temp);
			}
		}
		break;

	case TTLS_PHASE2_EAP:
		eapnode = xsupconfwrite_eap_create_tree(ttlsdata->phase2_data, write_all);
		if (eapnode == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't create phase 2 <EAP> data for TTLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			return NULL;
		}

		if (xmlAddChild(p2node, eapnode) == NULL)
		{
#ifdef WRITE_EAP_TTLS_DEBUG
			printf("Couldn't add phase 2 EAP data to TTLS!\n");
#endif
			xmlFreeNode(ttlsnode);
			return NULL;
		}
		break;
	}

	return ttlsnode;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -