diff -ur freeradius.orig/man/man5/rlm_sql.5 freeradius.new/man/man5/rlm_sql.5
--- freeradius.orig/man/man5/rlm_sql.5	2007-08-15 20:45:12.000000000 -0400
+++ freeradius.new/man/man5/rlm_sql.5	2007-12-23 15:03:44.000000000 -0500
@@ -106,6 +106,8 @@
 .IP accounting_stop_query_alt
 The query to be run when receiving an Accounting Stop packet.  If the
 primary query fails, the alt query is run.
+.IP Accounting_failed_query
+The query to be run to when receiving a Failed packet.
 .IP simul_count_query
 The query to be run to return the number simultaneous sessions for the
 purposes of limiting Simultaneous Use.
Only in freeradius.new/man/man5: rlm_sql.5.orig
diff -ur freeradius.orig/raddb/sql/mssql/dialup.conf freeradius.new/raddb/sql/mssql/dialup.conf
--- freeradius.orig/raddb/sql/mssql/dialup.conf	2007-11-13 04:54:43.000000000 -0500
+++ freeradius.new/raddb/sql/mssql/dialup.conf	2007-12-23 15:03:45.000000000 -0500
@@ -70,6 +70,7 @@
 	# accounting_stop_query_alt	- query for Accounting start packets
 	#                               (alternate in case first query doesn't
 	#                                affect any existing rows in the table)
+	# accounting_failed_query	- query for Accounting failed packets
 	#######################################################################
 	accounting_onoff_query = "UPDATE ${acct_table1} SET AcctStopTime='%S', AcctSessionTime=unix_timestamp('%S') - unix_timestamp(AcctStartTime), AcctTerminateCause='%{Acct-Terminate-Cause}', AcctStopDelay = %{Acct-Delay-Time:-0} WHERE AcctSessionTime=0 AND AcctStopTime=0 AND NASIPAddress= '%{NAS-IP-Address}' AND AcctStartTime <= '%S'"
 
@@ -92,3 +93,7 @@
 	# removed. These fields are processing by a database
 	accounting_stop_query_alt = "INSERT into ${acct_table2} (AcctSessionId, AcctUniqueId, UserName, Realm, NASIPAddress, NASPort, NASPortType, AcctStopTime, AcctSessionTime, AcctAuthentic, ConnectInfo_start, ConnectInfo_stop, AcctInputOctets, AcctOutputOctets, CalledStationId, CallingStationId, AcctTerminateCause, ServiceType, FramedProtocol, FramedIPAddress, AcctStartDelay, AcctStopDelay) values('%{Acct-Session-Id}', '%{Acct-Unique-Session-Id}', '%{SQL-User-Name}', '%{Realm}', '%{NAS-IP-Address}', '%{NAS-Port-Id}', '%{NAS-Port-Type}', '%S', '%{Acct-Session-Time}', '%{Acct-Authentic}', '', '%{Connect-Info}', '%{Acct-Input-Octets}', '%{Acct-Output-Octets}', '%{Called-Station-Id}', '%{Calling-Station-Id}', '%{Acct-Terminate-Cause}', '%{Service-Type}', '%{Framed-Protocol}', '%{Framed-IP-Address}', '0', '%{Acct-Delay-Time:-0}')"
 
+	# accounting_failed_query: Inserting of RadAcctId and AcctStopTime was
+	# removed. These fields are processing by a database
+	accounting_failed_query = "INSERT into ${acct_table1} (AcctSessionId, AcctUniqueId, UserName, Realm, NASIPAddress, NASPort, NASPortType, AcctStartTime, AcctSessionTime, AcctAuthentic, ConnectInfo_start, ConnectInfo_stop, AcctInputOctets, AcctOutputOctets, CalledStationId, CallingStationId, AcctTerminateCause, ServiceType, ENUMtld, FramedIPAddress, AcctStartDelay, AcctStopDelay) values('%{Acct-Session-Id}', '%{Acct-Unique-Session-Id}', '%{SQL-User-Name}', '%{Realm}', '%{NAS-IP-Address}', '%{NAS-Port-Id}', '%{NAS-Port-Type}', '%S', '0', '%{Acct-Authentic}', '%{Connect-Info}', '', '0', '0', '%{Called-Station-Id}', '%{Calling-Station-Id}', '', '%{Service-Type}', '%{ENUMtld}', '%{Framed-IP-Address}', '%{Acct-Delay-Time}', '0')"
+
Only in freeradius.new/raddb/sql/mssql: dialup.conf.orig
diff -ur freeradius.orig/raddb/sql/mysql/dialup.conf freeradius.new/raddb/sql/mysql/dialup.conf
--- freeradius.orig/raddb/sql/mysql/dialup.conf	2007-11-05 03:59:00.000000000 -0500
+++ freeradius.new/raddb/sql/mysql/dialup.conf	2007-12-23 15:03:45.000000000 -0500
@@ -133,6 +133,7 @@
 	# accounting_stop_query_alt	- query for Accounting start packets
 	#                               (alternate in case first query doesn't
 	#                                affect any existing rows in the table)
+	# accounting_failed_query	- query for Accounting failed packets
 	#######################################################################
 	accounting_onoff_query = "\
           UPDATE ${acct_table1} \
@@ -262,6 +263,56 @@
              '%{Service-Type}', '%{Framed-Protocol}', '%{Framed-IP-Address}', \
              '0', '%{%{Acct-Delay-Time}:-0}')"
 
+	accounting_failed_query = " \
+	  INSERT INTO ${acct_table1} \
+	    (AcctSessionId, \
+             AcctUniqueId, \
+             UserName, \
+             Realm, \
+             NASIPAddress, \
+             NASPortId, \
+             NASPortType, \
+             AcctStartTime, \
+             AcctStopTime, \
+             AcctSessionTime, \
+             AcctAuthentic, \
+             ConnectInfo_start, \
+             ConnectInfo_stop, \
+             AcctInputOctets, \
+             AcctOutputOctets, \
+             CalledStationId, \
+             CallingStationId, \
+             AcctTerminateCause, \
+             ServiceType, \
+             ENUMtld, \
+             FramedIPAddress, \
+             AcctStartDelay, \
+             AcctStopDelay) \
+          VALUES \
+            ('%{Acct-Session-Id}', \
+             '%{Acct-Unique-Session-Id}', \
+             '%{SQL-User-Name}', \
+             '%{Realm}', \
+             '%{NAS-IP-Address}', \
+             '%{NAS-Port}', \
+             '%{NAS-Port-Type}', \
+             '%S', \
+             '0', \
+             '0', \
+             '%{Acct-Authentic}', \
+             '%{Connect-Info}', \
+             '', \
+             '0', \
+             '0', \
+             '%{Called-Station-Id}', \
+             '%{Calling-Station-Id}', \
+             '', \
+             '%{Service-Type}', \
+             '%{ENUMtld}', \
+             '%{Framed-IP-Address}', \
+             '%{Acct-Delay-Time}', \
+             '0')"
+
 	#######################################################################
 	# Simultaneous Use Checking Queries
 	#######################################################################
Only in freeradius.new/raddb/sql/mysql: dialup.conf.orig
diff -ur freeradius.orig/raddb/sql/oracle/dialup.conf freeradius.new/raddb/sql/oracle/dialup.conf
--- freeradius.orig/raddb/sql/oracle/dialup.conf	2007-09-18 02:08:36.000000000 -0400
+++ freeradius.new/raddb/sql/oracle/dialup.conf	2007-12-23 15:03:46.000000000 -0500
@@ -100,6 +100,7 @@
 	# accounting_stop_query_alt	- query for Accounting start packets 
 	#                               (alternate in case first query doesn't
 	#                                affect any existing rows in the table)
+	# accounting_failed_query	- query for Accounting failed packets
 	#######################################################################
 	accounting_onoff_query = "UPDATE ${acct_table1} SET AcctStopTime=TO_DATE('%S','yyyy-mm-dd hh24:mi:ss'), AcctSessionTime=((TO_DATE('%S','yyyy-mm-dd hh24:mi:ss') - AcctStartTime)*86400), AcctTerminateCause='%{Acct-Terminate-Cause}', AcctStopDelay = %{Acct-Delay-Time:-0} WHERE AcctSessionTime=0 AND AcctStopTime IS NULL AND NASIPAddress = '%{NAS-IP-Address}' AND AcctStartTime <= TO_DATE('%S','yyyy-mm-dd hh24:mi:ss')"
 
@@ -147,6 +148,8 @@
     '%{Called-Station-Id}', '%{Calling-Station-Id}', '%{Acct-Terminate-Cause}', '%{Service-Type}', \
     '%{Framed-Protocol}', '%{Framed-IP-Address}', '0', '%{Acct-Delay-Time:-0}')"
 
+accounting_failed_query = "INSERT into ${acct_table1} (RadAcctId, AcctSessionId, AcctUniqueId, UserName, Realm, NASIPAddress, NASPortId, NASPortType, AcctStartTime, AcctStopTime, AcctSessionTime, AcctAuthentic, ConnectInfo_start, ConnectInfo_stop, AcctInputOctets, AcctOutputOctets, CalledStationId, CallingStationId, AcctTerminateCause, ServiceType, ENUMtld, FramedIPAddress, AcctStartDelay, AcctStopDelay) values('', '%{Acct-Session-Id}', '%{Acct-Unique-Session-Id}', '%{SQL-User-Name}', '%{Realm}', '%{NAS-IP-Address}', '%{NAS-Port-Id}', '%{NAS-Port-Type}', TO_DATE('%S','yyyy-mm-dd hh24:mi:ss'), NULL, '0', '%{Acct-Authentic}', '%{Connect-Info}', '', '0', '0', '%{Called-Station-Id}', '%{Calling-Station-Id}', '', '%{Service-Type}', '%{ENUMtld}', '%{Framed-IP-Address}', '%{Acct-Delay-Time}', '0')"
+
 	#######################################################################
 	# Simultaneous Use Checking Queries
 	#######################################################################
Only in freeradius.new/raddb/sql/oracle: dialup.conf.orig
diff -ur freeradius.orig/raddb/sql/postgresql/dialup.conf freeradius.new/raddb/sql/postgresql/dialup.conf
--- freeradius.orig/raddb/sql/postgresql/dialup.conf	2007-12-23 11:22:11.000000000 -0500
+++ freeradius.new/raddb/sql/postgresql/dialup.conf	2007-12-23 15:03:47.000000000 -0500
@@ -158,6 +158,7 @@
 # accounting_stop_query_alt	- query for Accounting start packets
 #                               (alternate in case first query doesn't
 #                                affect any existing rows in the table)
+# accounting_failed_query	- query for Accounting failed packets
 #######################################################################
 
 accounting_onoff_query = "UPDATE ${acct_table1} \
@@ -268,6 +269,14 @@
   '%{Framed-Protocol}', \
   NULLIF('%{Framed-IP-Address}', '')::inet, 0)"
 
+accounting_failed_query = "INSERT into ${acct_table1} \
+  (AcctSessionId, AcctUniqueId, UserName, Realm, NASIPAddress, NASPortId, NASPortType, AcctStartTime, AcctAuthentic, \
+  ConnectInfo_start, CalledStationId, CallingStationId, ServiceType, ENUMtld, FramedIPAddress, AcctStartDelay) \
+  values('%{Acct-Session-Id}', '%{Acct-Unique-Session-Id}', '%{SQL-User-Name}', '%{Realm}', '%{NAS-IP-Address}', \
+  '%{NAS-Port}', '%{NAS-Port-Type}', (now() - '%{Acct-Delay-Time:-0}'::interval), '%{Acct-Authentic}', '%{Connect-Info}', \
+  '%{Called-Station-Id}', '%{Calling-Station-Id}', '%{Service-Type}', '%{ENUMtld}', \
+  NULLIF('%{Framed-IP-Address}', '')::inet, '%{Acct-Delay-Time:-0}')"
+
 #######################################################################
 # Group Membership Queries
 #######################################################################
Only in freeradius.new/raddb/sql/postgresql: dialup.conf.orig
diff -ur freeradius.orig/raddb/sql/postgresql/voip-postpaid.conf freeradius.new/raddb/sql/postgresql/voip-postpaid.conf
--- freeradius.orig/raddb/sql/postgresql/voip-postpaid.conf	2007-11-13 04:54:43.000000000 -0500
+++ freeradius.new/raddb/sql/postgresql/voip-postpaid.conf	2007-12-23 15:03:48.000000000 -0500
@@ -78,6 +78,7 @@
 	# accounting_stop_query_alt	- query for Accounting start packets 
 	#                               (alternate in case first query doesn't
 	#                                affect any existing rows in the table)
+	# accounting_failed_query	- query for Accounting failed packets
 	#######################################################################
 
 	# Note: The VoIP accouting does not need Alternate queries in the shipped
@@ -105,5 +106,12 @@
 		'%{h323-call-origin}', pick_id('%{h323-conf-id}', '%{call-id}'), strip_dot('%{h323-connect-time}'), '%{h323-disconnect-cause}', \
 		strip_dot('%{h323-disconnect-time}'), '%{h323-gw-id}', strip_dot('%{h323-setup-time}'))"
  
+	 accounting_failed_query = "INSERT into ${acct_table1}%{h323-call-type} \
+		(RadiusServerName, UserName, NASIPAddress, AcctTime, CalledStationId, \
+		CallingStationId, AcctDelayTime, h323gwid, h323callorigin, h323setuptime, H323ConnectTime, callid) \
+		values('${radius_server_name}', '%{SQL-User-Name}', \
+		'%{NAS-IP-Address}', now(), '%{Called-Station-Id}', \
+		'%{Calling-Station-Id}', '%{Acct-Delay-Time:-0}', '%{h323-gw-id}', \
+		'%{h323-call-origin}', strip_dot('%{h323-setup-time}'), strip_dot('%{h323-connect-time}'), pick_id('%{h323-conf-id}', '%{call-id}'))"
 
 }
Only in freeradius.new/raddb/sql/postgresql: voip-postpaid.conf.orig
diff -ur freeradius.orig/src/include/radius.h freeradius.new/src/include/radius.h
--- freeradius.orig/src/include/radius.h	2007-11-28 07:32:10.000000000 -0500
+++ freeradius.new/src/include/radius.h	2007-12-23 15:03:49.000000000 -0500
@@ -283,6 +283,7 @@
 #define PW_STATUS_ALIVE			3
 #define PW_STATUS_ACCOUNTING_ON		7
 #define PW_STATUS_ACCOUNTING_OFF	8
+#define PW_STATUS_FAILED		15
 
 /*
  *  Vendor Private Enterprise Codes
Only in freeradius.new/src/include: radius.h.orig
diff -ur freeradius.orig/src/modules/rlm_sql/conf.h freeradius.new/src/modules/rlm_sql/conf.h
--- freeradius.orig/src/modules/rlm_sql/conf.h	2006-11-14 16:22:29.000000000 -0500
+++ freeradius.new/src/modules/rlm_sql/conf.h	2007-12-23 15:03:50.000000000 -0500
@@ -32,6 +32,7 @@
 	char   *accounting_start_query_alt;
 	char   *accounting_stop_query;
 	char   *accounting_stop_query_alt;
+	char   *accounting_failed_query;
 	char   *simul_count_query;
 	char   *simul_verify_query;
 	char   *groupmemb_query;
@@ -74,7 +75,7 @@
 
 #define ASCEND_PORT_HACK
 #define ASCEND_CHANNELS_PER_LINE        23
-#define CISCO_ACCOUNTING_HACK
+/* #define CISCO_ACCOUNTING_HACK */
 
 /* SQL defines */
 #define MAX_QUERY_LEN			4096
Only in freeradius.new/src/modules/rlm_sql: conf.h.orig
diff -ur freeradius.orig/src/modules/rlm_sql/rlm_sql.c freeradius.new/src/modules/rlm_sql/rlm_sql.c
--- freeradius.orig/src/modules/rlm_sql/rlm_sql.c	2007-12-17 02:45:46.000000000 -0500
+++ freeradius.new/src/modules/rlm_sql/rlm_sql.c	2007-12-23 15:03:51.000000000 -0500
@@ -89,6 +89,8 @@
 	 offsetof(SQL_CONFIG,accounting_stop_query), NULL, ""},
 	{"accounting_stop_query_alt", PW_TYPE_STRING_PTR,
 	 offsetof(SQL_CONFIG,accounting_stop_query_alt), NULL, ""},
+	{"accounting_failed_query", PW_TYPE_STRING_PTR,
+	 offsetof(SQL_CONFIG,accounting_failed_query), NULL, ""},
 	{"group_membership_query", PW_TYPE_STRING_PTR,
 	 offsetof(SQL_CONFIG,groupmemb_query), NULL, NULL},
 	{"connect_failure_retry_delay", PW_TYPE_INTEGER,
@@ -1272,6 +1274,33 @@
 			break;
 
 			/*
+			 * Got accounting failed packet
+			 */
+		case PW_STATUS_FAILED:
+
+			/*
+			 * Set, escape, and check the user attr here
+			 */
+			sql_set_user(inst, request, sqlusername, NULL);
+
+			radius_xlat(querystr, sizeof(querystr), inst->config->accounting_failed_query, request, sql_escape_func);
+			query_log(request, inst, querystr);
+
+			sqlsocket = sql_get_socket(inst);
+			if (sqlsocket == NULL)
+				return(RLM_MODULE_FAIL);
+			if (*querystr) { /* non-empty query */
+				if (rlm_sql_query(sqlsocket, inst, querystr)) {
+					radlog(L_ERR, "rlm_sql (%s): Couldn't insert SQL accounting FAILED record - %s",
+						inst->config->xlat_name,
+						(char *)(inst->module->sql_error)(sqlsocket, inst->config));
+					ret = RLM_MODULE_FAIL;
+				}
+				(inst->module->sql_finish_query)(sqlsocket, inst->config);
+			}
+			break;
+
+			/*
 			 *	Anything else is ignored.
 			 */
 		default:
Only in freeradius.new/src/modules/rlm_sql: rlm_sql.c.orig
