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

📄 xsupconfwrite.c

📁 Linux上的802.1x 的supplicant的实现。很多supplicant程序都是基于它开发的
💻 C
📖 第 1 页 / 共 5 页
字号:
/******************************************************************* * Implementation for writing a config from memory to the disk. * * Licensed under a dual GPL/BSD license.  (See LICENSE file for more info.) * * File: xsupconfwrite.c * * Authors: Chris.Hessing@utah.edu * * $Id: xsupconfwrite.c,v 1.14 2006/06/05 19:56:42 chessing Exp $ * $Date: 2006/06/05 19:56:42 $ * $Log: xsupconfwrite.c,v $ * Revision 1.14  2006/06/05 19:56:42  chessing * Fixed a 16k memory leak in the config parse code.  Cleaned up some of the TLS code. * * Revision 1.13  2006/05/30 23:40:23  chessing * Small patches/additions and addition to the readme file that contains a link to the location of libtnc. * * Revision 1.12  2006/05/30 16:56:31  chessing * More patches from Carsten Grohmann and a #if that was missing from the 1.2.5 release. * * Revision 1.11  2006/05/30 04:33:19  chessing * Some small updates for the GUI code. * * Revision 1.10  2006/05/26 22:04:58  chessing * Fixed some memory access errors, and cleaned up some wext stuff that was causing issues with the madwifi driver in wext mode. * * Revision 1.9  2006/05/24 04:36:30  chessing * More work on the GUI interface.  (We can now save.)  Fixed xsupconfwrite so that it doesn't write the firmware roaming option twice. * * Revision 1.8  2006/05/22 22:29:17  chessing * Compiler warnings patches from Eric Evans.  (And one from me. ;) * * Revision 1.7  2006/05/13 05:56:44  chessing * Removed last pieces of code that relied on SIGALRM.  Active scan timeout is now configurable so that people that wish to hammer on their cards now have the option to do that. ;) * * Revision 1.6  2006/04/17 03:56:24  chessing * Added some support to enable/disable TNC support both via the configuration file, and via IPC. * * Revision 1.5  2006/03/08 00:16:04  chessing * Fixed EAP hints code to work correctly when the request ID packet is padded out with null bytes.  (Observed in Aruba APs.)  Some changes/fixes for the EAP-AKA module. * * Revision 1.4  2006/02/23 22:26:53  chessing * Fix for bug id #1415020.  'Building Xsupplicant 1.2.3 Fails on FC4'. * * Revision 1.3  2006/01/03 04:02:35  chessing * Added the ability to store the PEAP password in a hashed format.  (Basically, an MS-CHAPv1 hash.)  Also added an 'ntpwdhash' program to the tools directory that will convert a cleartext password in to a hash that can be copied to the configuration file. * * Revision 1.2  2005/11/16 02:18:38  chessing * Updates to the config writing library.  The config-parser tool has been modified to be able to parse a config, and rewrite it. * * Revision 1.1  2005/11/14 18:06:55  chessing * Added library to take the parsed configuration structure out of memory, and write it to a file.  (This will be useful for GUI interfaces.) * * *******************************************************************/#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <string.h>#include <stdlib.h>#include "xsupconfig.h"#include "xsupconfwrite.h"#ifdef USE_EFENCE#include <efence.h>#endif// Enable this for textual debug output.//#define DEBUG  1/******************************************************************* * * Error result strings for various errors that can occur. * *******************************************************************/char *xsupconfwrite_errunknown = "Unknown Error";char *xsupconfwrite_errnone = "There was no error.";char *xsupconfwrite_errcheckerrno = "Please check errno for the proper error result.";char *xsupconfwrite_errgenioerr = "An I/O error occurred.  This may be because the requested file cannot be written, or no longer exists.";char *xsupconfwrite_errinvalid_dest = "An invalid value was set for the 'destination' in the config file.";char *xsupconfwrite_errbadnetlist = "You must have a value set for 'network_list'.  (If you are unsure, set this to 'all'.)";char *xsupconfwrite_errnofacility = "Your log file is set to syslog, but you have not defined a logging facility.";char *xsupconfwrite_errnofilehandle = "There is no available file handle to write your new config!";char *xsupconfwrite_errnoglobaldata = "There is no available global configuration data to write!";char *xsupconfwrite_errnonetworkdata = "There is no available network configuration data to write!";char *xsupconfwrite_errnonetworkname = "There is no network name available in the network configuration data.";char *xsupconfwrite_errinvalidcrypto = "Attempted to write an invalid crypto type value.";char *xsupconfwrite_errnoeapdata = "There was no EAP data available to write.";char *xsupconfwrite_errbadttlsphase2 = "Invalid TTLS phase 2 configuration.";char *xsupconfwrite_errnophase2eapdata = "Invalid PEAP phase 2 data.";char *xsupconfwrite_errinvalidinnereap = "Invalid EAP method requested for PEAP phase 2.";/******************************************************************* * * Create a config file, and return an error if we get one. * *******************************************************************/int xsupconfwrite_create_config_file(char *filename, FILE **desc){#ifdef DEBUG  printf("Opening file %s\n", filename);#endif  (*desc) = fopen(filename, "w");  if (!(*desc)) return XSUPCONFWRITE_CHECKERRNO;  return XSUPCONFWRITE_ERRNONE;}/******************************************************************** * * Write a silly little header that indicates that this is an Xsupplicant * config file. * ********************************************************************/int xsupconfwrite_write_header(FILE *fileid){  if (!fileid)    {#ifdef DEBUG      printf("Invalid file handle! In %s():%d!\n", __FUNCTION__, __LINE__);#endif      return XSUPCONFWRITE_NOFILEHANDLE;    }  if (fprintf(fileid, "# Xsupplicant configuration file for Xsupplicant %s (or"      " later)\n\n\n", VERSION) < 0)     {#ifdef DEBUG      printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif      return XSUPCONFWRITE_GENIOERR;    }  return XSUPCONFWRITE_ERRNONE;}/******************************************************************** * * Write a string list as a set of comma seperated strings. * ********************************************************************/int xsupconfwrite_write_stringlist(FILE *fileid, 				   struct config_string_list *list){  int comma = 0;  struct config_string_list *cur;  cur = list;  while (cur != NULL)    {      if (comma) 	{	  if (fprintf(fileid, ",") < 0) 	    {#ifdef DEBUG	      printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	      return XSUPCONFWRITE_GENIOERR;	    }	}      if (fprintf(fileid, "%s", cur->name) < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}      comma = 1;      cur = cur->next;    }  if (fprintf(fileid, "\n\n") < 0)     {#ifdef DEBUG      printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif      return XSUPCONFWRITE_GENIOERR;    }  return XSUPCONFWRITE_ERRNONE;}/******************************************************************** * * This function will write appropriate values in the global config section * of the file. * ********************************************************************/int xsupconfwrite_write_globals(FILE *fileid, struct config_globals *globals){  int err = 0;  if (!fileid)     {#ifdef DEBUG      printf("No valid file handle found at %s():%d!\n", __FUNCTION__,	     __LINE__);#endif      return XSUPCONFWRITE_NOFILEHANDLE;    }  if (!globals)    {#ifdef DEBUG      printf("No valid data to write to config file at %s():%d!\n", 	     __FUNCTION__, __LINE__);#endif      return XSUPCONFWRITE_NOGLOBALDATA;    }  if (fprintf(fileid, "# --- Global Variables ---\n\n") < 0)     {#ifdef DEBUG      printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif      return XSUPCONFWRITE_GENIOERR;    }  if (!globals->allowed_nets)     {      if (fprintf(fileid, "network_list = all\n") < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    } else {      if (fprintf(fileid, "network_list = ") < 0) 	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}            err = xsupconfwrite_write_stringlist(fileid, globals->allowed_nets);      if (err < 0) return err;    }  if (fprintf(fileid, "default_netname = %s\n\n", globals->default_net) < 0)    {#ifdef DEBUG      printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif      return XSUPCONFWRITE_GENIOERR;    }  if (globals->destination != DEST_AUTO)    {      if (fprintf(fileid, "destination = ") < 0) 	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}      // We will never see DEST_AUTO, so ignore that case.      switch (globals->destination)	{	case DEST_BSSID:	  if (fprintf(fileid, "BSSID\n\n") < 0) 	    {#ifdef DEBUG	      printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	      return XSUPCONFWRITE_GENIOERR;	    }	  break;	case DEST_MULTICAST:	  if (fprintf(fileid, "multicast\n\n") < 0) 	    {#ifdef DEBUG	      printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	      return XSUPCONFWRITE_GENIOERR;	    }	  break;	case DEST_SOURCE:	  if (fprintf(fileid, "source\n\n") < 0)	    {#ifdef DEBUG	      printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	      return XSUPCONFWRITE_GENIOERR;	    }	  break;	default:	  return XSUPCONFWRITE_INVALID_DEST;	}    }  if (globals->logfile)    {      if (fprintf(fileid, "logfile = %s\n\n", globals->logfile) < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}            if (strncmp(globals->logfile, "syslog", 6) == 0)	{	  // We are logging to syslog, so we need to write a facility	  // value.	  	  if (!globals->log_facility) return XSUPCONFWRITE_NOFACILITY;	  	  if (fprintf(fileid, "log_facility = %s\n\n", globals->log_facility) < 0)	    {#ifdef DEBUG	      printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	      return XSUPCONFWRITE_GENIOERR;	    }	}    }  if (globals->ipc_group_name)    {      if (fprintf(fileid, "ipc_group = %s\n\n", globals->ipc_group_name) < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (TEST_FLAG(globals->flags, CONFIG_GLOBALS_AUTH_PER))    {      if (fprintf(fileid, "auth_period = %d\n\n", globals->auth_period) < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (TEST_FLAG(globals->flags, CONFIG_GLOBALS_HELD_PER))    {      if (fprintf(fileid, "held_period = %d\n\n", globals->held_period) < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (TEST_FLAG(globals->flags, CONFIG_GLOBALS_MAX_STARTS))    {      if (fprintf(fileid, "max_starts = %d\n\n", globals->max_starts) < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (TEST_FLAG(globals->flags, CONFIG_GLOBALS_NO_FRIENDLY_WARNINGS))    {      if (fprintf(fileid, "friendly_warnings = no\n\n") < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (!TEST_FLAG(globals->flags, CONFIG_GLOBALS_PASSIVE_SCAN))    {      if (fprintf(fileid, "passive_scanning = no\n\n") < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (globals->passive_timeout != 0)    {      if (fprintf(fileid, "passive_timer = %d\n\n", globals->passive_timeout) < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }    if (globals->active_timeout != RESCAN_TIMEOUT)    {      if (fprintf(fileid, "scan_timeout = %d\n\n", globals->active_timeout) < 0)	{#ifdef DEBUG	  printf("Error writing to the file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (TEST_FLAG(globals->flags, CONFIG_GLOBALS_FIRMWARE_ROAM))    {      if (fprintf(fileid, "roaming = firmware\n\n") < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (!TEST_FLAG(globals->flags, CONFIG_GLOBALS_ALLMULTI))    {      if (fprintf(fileid, "allmulti = no\n\n") < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (!TEST_FLAG(globals->flags, CONFIG_GLOBALS_ASSOC_AUTO))    {      if (fprintf(fileid, "association = manual\n\n") < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (TEST_FLAG(globals->flags, CONFIG_GLOBALS_NO_EAP_HINTS))    {      if (fprintf(fileid, "use_eap_hints = no\n\n") < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  if (globals->assoc_timeout != ASSOCIATION_TIMEOUT)    {      if (fprintf(fileid, "association_timeout = %d\n\n", 		  globals->assoc_timeout) < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}    }  return XSUPCONFWRITE_ERRNONE;}/************************************************************************** * * If 'comma' is >=1 then write a comma, and a space.  Since this call should * only come deep inside of another function, we don't check to be sure that * fileid is valid.  (Why would you write a comma to a file that you hadn't * written something to already? ;) * *************************************************************************/int xsupconfwrite_write_comma(FILE *fileid, int comma){  if (comma >= 1)    {      return fprintf(fileid, ", ");    }  return 0;}/**************************************************************************** * * Write out a list of EAP types that we are going to allow. * ****************************************************************************/int xsupconfwrite_write_allowed_eaps(FILE *fileid, struct config_network *cur){  int comma = 0, retval;  if (!fileid)    {#ifdef DEBUG      printf("Invalid file handle at %s():%d!\n", __FUNCTION__, __LINE__);#endif      return XSUPCONFWRITE_NOFILEHANDLE;    }  if (!cur)    {#ifdef DEBUG      printf("Invalid network configuration structure at %s():%d!\n",	     __FUNCTION__, __LINE__);#endif      return XSUPCONFWRITE_NONETWORKDATA;    }  if ((cur->flags & CONFIG_NET_ALLOW_ALL) == CONFIG_NET_ALLOW_ALL)    {      if (fprintf(fileid, "\tallow_types = all\n") < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	} else {	  // We can return here, because if we are supporting ALL, then there	  // is nothing more to write.	  return XSUPCONFWRITE_ERRNONE;	}    }  if ((cur->flags & CONFIG_NET_ALLOW_ALL) > 0)    {      // Otherwise, we need to write every specific type we support.      if (fprintf(fileid, "\tallow_types = ") < 0)	{#ifdef DEBUG	  printf("Error writing to file @ %s():%d\n", __FUNCTION__, __LINE__);#endif	  return XSUPCONFWRITE_GENIOERR;	}            if (TEST_FLAG(cur->flags, CONFIG_NET_ALLOW_TLS))	{	  if (fprintf(fileid, "eap-tls") < 0) 

⌨️ 快捷键说明

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