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

📄 xsupconfwrite_connection.c

📁 linux 下通过802.1认证的安装包
💻 C
📖 第 1 页 / 共 2 页
字号:
				return NULL;
			}
		}
		
		if (assoc->pairwise_keys & CRYPT_FLAGS_WEP104)
		{
			if (xmlNewChild(assocNode, NULL, "Pairwise_Key_Type", "wep104") == NULL)
			{
#ifdef WRITE_CONNECTION_CONFIG
				printf("Couldn't create <Pairwise_Key_Type> node!\n");
#endif
				xmlFreeNode(assocNode);
				return NULL;
			}
		}
	}

	// Don't check write_all here.  The absence of this tag means that it should
	// be automagically figured out.
	if (assoc->group_keys != 0)
	{
		switch (assoc->group_keys)
		{
		case CRYPT_WEP40:
			temp = _strdup("wep40");
			break;

		case CRYPT_TKIP:
			temp = _strdup("tkip");
			break;

		case CRYPT_WRAP:
			temp = _strdup("wrap");
			break;

		case CRYPT_CCMP:
			temp = _strdup("ccmp");
			break;

		case CRYPT_WEP104:
			temp = _strdup("wep104");
			break;
		}

		if (xmlNewChild(assocNode, NULL, "Group_Key_Type", temp) == NULL)
		{
#ifdef WRITE_CONNECTION_CONFIG
			printf("Couldn't create <Group_Key_Type> node!\n");
#endif
			xmlFreeNode(assocNode);
			free(temp);
			return NULL;
		}

		free(temp);
		temp = NULL;
	}

	// Don't check write_all here.  If psk isn't set, then we don't
	// really care what the value is.
	if (assoc->psk != NULL)
	{
		if (pwcrypt_funcs_available() == TRUE)
		{
			// Write the encrypted version.
			if (pwcrypt_encrypt(assoc->psk, strlen(assoc->psk), &temp, &ressize) != 0)
			{
				// Couldn't encrypt the data.  So write the cleartext version.
				if (xmlNewChild(assocNode, NULL, "PSK", assoc->psk) == NULL)
				{
#ifdef WRITE_CONNECTION_CONFIG
					printf("Couldn't create <PSK> node.\n");
#endif
					xmlFreeNode(assocNode);
					return NULL;
				}
			}
			else
			{
				if (xmlNewChild(assocNode, NULL, "Encrypted_PSK", temp) == NULL)
				{
#ifdef WRITE_CONNECTION_CONFIG
					printf("Couldn't create <Encrypted_PSK> node.\n");
#endif
					xmlFreeNode(assocNode);
					free(temp);
					return NULL;
				}
				free(temp);
			}
		}
		else
		{
			if (xmlNewChild(assocNode, NULL, "PSK", assoc->psk) == NULL)
			{
#ifdef WRITE_CONNECTION_CONFIG
				printf("Couldn't create <PSK> node.\n");
#endif
				xmlFreeNode(assocNode);
				return NULL;
			}
		}
	}

	// Don't check write_all here.  If psk_hex isn't set, then we don't
	// really care what the value is.
	if (assoc->psk_hex != NULL)
	{
		if (xmlNewChild(assocNode, NULL, "PSK_Hex", assoc->psk_hex) == NULL)
		{
#ifdef WRITE_CONNECTION_CONFIG
			printf("Couldn't create <PSK_Hex> node!\n");
#endif
			xmlFreeNode(assocNode);
			return NULL;
		}
	}

	// Don't check write_all here.  If the TX_key isn't set, then we don't 
	// really care what the key values are.
	if (assoc->txkey != 0)
	{
		sprintf((char *)&static_temp, "%d", assoc->txkey);
		if (xmlNewChild(assocNode, NULL, "TX_Key", static_temp) == NULL)
		{
#ifdef WRITE_CONNECTION_CONFIG
			printf("Couldn't create <TX_Key> node!\n");
#endif
			xmlFreeNode(assocNode);
			return NULL;
		}

		for (i=1;i < 5; i++)
		{
			sprintf((char *)&static_temp, "Key_%d", i);
			if (xmlNewChild(assocNode, NULL, static_temp, assoc->keys[i]) == NULL)
			{
#ifdef WRITE_CONNECTION_CONFIG
				printf("Couldn't create <%s> node!\n", static_temp);
#endif
				xmlFreeNode(assocNode);
				return NULL;
			}
		}
	}

	return assocNode;
}

/**
 * \brief Create the <Connection> block for the configuration file in a format
 *        that libxml2 can understand.
 *
 * @param[in] con         A config_connection 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 <Connection> tree in a format that is used by 
 *         libxml2.
 **/
xmlNodePtr xsupconfwrite_connection_create_tree(struct config_connection *con, 
									 char write_all)
{
	xmlNodePtr connode = NULL;
	xmlNodePtr assocNode = NULL;
	xmlNodePtr ipNode = NULL;
	char static_temp[10];
	char *temp = NULL;
	
	if (con == NULL) return NULL;

	// Create the root node for the <Connection> block.
	connode = xmlNewNode(NULL, "Connection");
	if (connode == NULL)
	{
#ifdef WRITE_CONNECTION_CONFIG
		printf("Couldn't allocate memory to store <Connection> block!\n");
#endif
		return NULL;
	}

	if (con->ou != NULL)
	{
		xmlSetProp(connode, "OU", con->ou);
	}

	if ((write_all == TRUE) || (con->name != NULL))
	{
		xsupconfwrite_convert_amp(con->name, &temp);
		if (xmlNewChild(connode, NULL, "Name", temp) == NULL)
		{
#ifdef WRITE_CONNECTION_CONFIG
			printf("Couldn't allocate memory to store <Name> node!\n");
#endif
			xmlFreeNode(connode);
			free(temp);
			return NULL;
		}

		free(temp);
	}

	if ((write_all == TRUE) || (con->priority != DEFAULT_PRIORITY))
	{
		sprintf((char *)&static_temp, "%d", con->priority);
		if (xmlNewChild(connode, NULL, "Priority", static_temp) == NULL)
		{
#ifdef WRITE_CONNECTION_CONFIG
			printf("Couldn't allocate memory to store <Priority> node!\n");
#endif
			xmlFreeNode(connode);
			return NULL;
		}
	}

	//  Don't check write_all here!  Not having this tag means the connection
	//  is wired.
	if (con->ssid != NULL)
	{
		xsupconfwrite_convert_amp(con->ssid, &temp);
		if (xmlNewChild(connode, NULL, "SSID", temp) == NULL)
		{
#ifdef WRITE_CONNECTION_CONFIG
			printf("Couldn't allocate memory to store <SSID> node!\n");
#endif
			xmlFreeNode(connode);
			free(temp);
			return NULL;
		}

		free(temp);
	}

	// Don't check write_all here!  Not having a profile is allowed if the 
	// connection is WPA(2)-PSK, or Static WEP!
	if (con->profile != NULL)
	{
		xsupconfwrite_convert_amp(con->profile, &temp);
		if (xmlNewChild(connode, NULL, "Profile", temp) == NULL)
		{
#ifdef WRITE_CONNECTION_CONFIG
			printf("Couldn't allocate memory to store <Profile> node!\n");
#endif
			xmlFreeNode(connode);
			free(temp);
			return NULL;
		}

		free(temp);
	}

	if ((write_all == TRUE) || (con->device != NULL))
	{
		xsupconfwrite_convert_amp(con->device, &temp);
		if (xmlNewChild(connode, NULL, "Interface", con->device) == NULL)
		{
#ifdef WRITE_CONNECTION_CONFIG
			printf("Couldn't allocate memory to store <Interface> node!\n");
#endif
			xmlFreeNode(connode);
			free(temp);
			return NULL;
		}

		free(temp);
	}

	// Don't check write_all here, because if the MAC address is all 0s, we don't
	// want to write it.
	if (memcmp(con->dest_mac, "\x00\x00\x00\x00\x00\x00", 6) != 0)
	{
		temp = mac2str(con->dest_mac);
		if (xmlNewChild(connode, NULL, "Destination_MAC", temp) == NULL)
		{
#ifdef WRITE_CONNECTION_CONFIG
			printf("Couldn't allocate memory to store <Destination_MAC> node!\n");
#endif
			free(temp);
			xmlFreeNode(connode);
			return NULL;
		}

		free(temp);
		temp = NULL;
	}

	if ((write_all == TRUE) || (con->force_eapol_ver != 0))
	{
		sprintf((char *)&static_temp, "%d", con->force_eapol_ver);
		if (xmlNewChild(connode, NULL, "Force_EAPoL_Version", static_temp) == NULL)
		{
#ifdef WRITE_CONNECTION_CONFIG
			printf("Couldn't allocate memory to store <Force_EAPoL_Version> node!\n");
#endif
			xmlFreeNode(connode);
			return NULL;
		}
	}

	if ((write_all == TRUE) || (TEST_FLAG(con->flags, CONFIG_NET_IS_HIDDEN)))
	{
		if (TEST_FLAG(con->flags, CONFIG_NET_IS_HIDDEN))
		{
			if (xmlNewChild(connode, NULL, "Hidden_SSID", "yes") == NULL)
			{
#ifdef WRITE_CONNECTION_CONFIG
				printf("Couldn't allocate memory to store <Hidden_SSID> node!\n");
#endif
				xmlFreeNode(connode);
				return NULL;
			}
		}
		else
		{
			if (xmlNewChild(connode, NULL, "Hidden_SSID", "no") == NULL)
			{
#ifdef WRITE_CONNECTION_CONFIG
				printf("Couldn't allocate memory to store <Hidden_SSID> node!\n");
#endif
				xmlFreeNode(connode);
				return NULL;
			}
		}
	}

	assocNode = xsupconfwrite_connection_association(&con->association, write_all);
	if (assocNode == NULL)
	{
#ifdef WRITE_CONNECTION_CONFIG
		printf("Couldn't allocate memory to create <Association> block!\n");
#endif
		xmlFreeNode(connode);
		return NULL;
	}

	if (xmlAddChild(connode, assocNode) == NULL)
	{
#ifdef WRITE_CONNECTION_CONFIG
		printf("Couldn't add <Association> block as a child block of <Connection>!\n");
#endif
		xmlFreeNode(connode);
		xmlFreeNode(assocNode);
		return NULL;
	}

	ipNode = xsupconfwrite_connection_ipdata(&con->ip, write_all);
	if (ipNode == NULL)
	{
#ifdef WRITE_CONNECTION_CONFIG
		printf("Couldn't allocate memory to create <IPv4_Configuration> block!\n");
#endif
		xmlFreeNode(connode);
		return NULL;
	}

	if (xmlAddChild(connode, ipNode) == NULL)
	{
#ifdef WRITE_CONNECTION_CONFIG
		printf("Couldn't add <IPv4_Configuration> block as a child block of <Connection>!\n");
#endif
		xmlFreeNode(connode);
		xmlFreeNode(ipNode);
		return NULL;
	}

	return connode;
}

⌨️ 快捷键说明

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