CREATE TABLE `loginout` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `username` varchar(50) collate latin1_german2_ci NOT NULL default '',
  `host` varchar(50) collate latin1_german2_ci NOT NULL default '',
  `sessionid` varchar(100) collate latin1_german2_ci NOT NULL default '',
  `intime` datetime NOT NULL default '0000-00-00 00:00:00',
  `outtime` datetime NOT NULL default '0000-00-00 00:00:00',
  PRIMARY KEY  (`id`),
  KEY `host` (`host`),
  KEY `intime` (`intime`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_german2_ci COMMENT='Tabelle für Login-out Rückfolgung';

diff -Nur samba-3.0.11-orig/source3/Makefile samba-3.0.11/source3/Makefile
--- samba-3.0.11-orig/source3/Makefile.in	2005-02-05 21:26:27.000000000 +0100
+++ samba-3.0.11/source3/Makefile.in	2005-02-05 21:42:49.000000000 +0100
@@ -37,7 +37,7 @@
 # (GCC) warnings. This is done automtically for --enable-developer,
 # --enable-picky-developer and --enable-krb5developer.
 DEVELOPER_CFLAGS=@DEVELOPER_CFLAGS@
-CFLAGS=@CFLAGS@
+CFLAGS=@CFLAGS@ `mysql_config --include` -Dmysqllogging
 CPPFLAGS=-DHAVE_CONFIG_H @CPPFLAGS@
 
 EXEEXT=@EXEEXT@
@@ -1325,7 +1325,7 @@
 		$(KRB5LIBS) $(DYNEXP) $(PRINT_LIBS) $(AUTH_LIBS) \
 		$(ACL_LIBS) $(PASSDB_LIBS) $(LIBS) $(DNSSD_LIBS) $(AVAHI_LIBS) \
 		$(POPT_LIBS) @SMBD_LIBS@ $(LIBTALLOC_LIBS) $(LIBTDB_LIBS) \
-		$(LIBWBCLIENT_LIBS) $(ZLIB_LIBS)
+		$(LIBWBCLIENT_LIBS) $(ZLIB_LIBS) `mysql_config --libs`
 
 bin/nmbd@EXEEXT@: $(BINARY_PREREQS) $(NMBD_OBJ) @BUILD_POPT@ @LIBTALLOC_TARGET@ @LIBTDB_TARGET@
 	@echo Linking $@
@@ -1512,7 +1512,7 @@
 		$(TERMLIBS) $(DYNEXP) $(PRINT_LIBS) $(AUTH_LIBS) $(DNSSD_LIBS) \
 		$(ACL_LIBS) $(LIBS) $(POPT_LIBS) $(KRB5LIBS) $(LDAP_LIBS) \
 		@SMBD_LIBS@ $(NSCD_LIBS) $(LIBTALLOC_LIBS) $(LIBTDB_LIBS) \
-		$(LIBWBCLIENT_LIBS) $(ZLIB_LIBS)
+		$(LIBWBCLIENT_LIBS) $(ZLIB_LIBS) `mysql_config --libs`
 
 bin/smbiconv@EXEEXT@: $(BINARY_PREREQS) $(SMBICONV_OBJ) @BUILD_POPT@ @LIBTALLOC_TARGET@ @LIBTDB_TARGET@
 	@echo Linking $@
diff -Nur samba-3.0.11-orig/source3/param/loadparm.c samba-3.0.11/source3/param/loadparm.c
--- samba-3.0.11-orig/source3/param/loadparm.c	2005-02-04 04:07:46.000000000 +0100
+++ samba-3.0.11/source3/param/loadparm.c	2005-02-06 12:51:32.000000000 +0100
@@ -178,6 +178,15 @@
 	char *szUtmpDir;
 	char *szWtmpDir;
 	bool bUtmp;
+#ifdef mysqllogging
+	bool bMySQLSessionLogging;
+	char *szMySQLSessionLoggingserver;
+	int iMySQLSessionLoggingport;
+	char *szMySQLSessionLoggingusername;
+	char *szMySQLSessionLoggingpw;
+	char *szMySQLSessionLoggingdb;
+	char *szMySQLSessionLoggingtable;
+#endif
 	char *szIdmapUID;
 	char *szIdmapGID;
 	bool bPassdbExpandExplicit;
@@ -3733,6 +3733,71 @@
 		.flags		= FLAG_ADVANCED,
 	},
 #endif
+#ifdef mysqllogging
+	{
+		.label		= "mysqlsessionlogging",
+		.type		= P_BOOL,
+		.p_class	= P_GLOBAL,
+		.ptr		= &Globals.bMySQLSessionLogging,
+		.special	= NULL,
+		.enum_list	= NULL,
+		.flags		= FLAG_ADVANCED
+	},
+	{
+		.label		= "mysqlsessionloggingserver",
+		.type		= P_STRING,
+		.p_class	= P_GLOBAL,
+		.ptr		= &Globals.szMySQLSessionLoggingserver,
+		.special	= NULL,
+		.enum_list	= NULL,
+		.flags		= FLAG_ADVANCED
+	},
+	{
+		.label		= "mysqlsessionloggingusername",
+		.type		= P_STRING,
+		.p_class	= P_GLOBAL,
+		.ptr		= &Globals.szMySQLSessionLoggingusername,
+		.special	= NULL,
+		.enum_list	= NULL,
+		.flags		= FLAG_ADVANCED
+	},
+	{
+		.label		= "mysqlsessionloggingpassword",
+		.type		= P_STRING,
+		.p_class	= P_GLOBAL,
+		.ptr		= &Globals.szMySQLSessionLoggingpw,
+		.special	= NULL,
+		.enum_list	= NULL,
+		.flags		= FLAG_ADVANCED
+	},
+	{
+		.label		= "mysqlsessionloggingport",
+		.type		= P_INTEGER,
+		.p_class	= P_GLOBAL,
+		.ptr		= &Globals.iMySQLSessionLoggingport,
+		.special	= NULL,
+		.enum_list	= NULL,
+		.flags		= FLAG_ADVANCED
+	},
+	{
+		.label		= "mysqlsessionloggingdb",
+		.type		= P_STRING,
+		.p_class	= P_GLOBAL,
+		.ptr		= &Globals.szMySQLSessionLoggingdb,
+		.special	= NULL,
+		.enum_list	= NULL,
+		.flags		= FLAG_ADVANCED
+	},
+	{
+		.label		= "mysqlsessionloggingtable",
+		.type		= P_STRING,
+		.p_class	= P_GLOBAL,
+		.ptr		= &Globals.szMySQLSessionLoggingtable,
+		.special	= NULL,
+		.enum_list	= NULL,
+		.flags		= FLAG_ADVANCED
+	},
+#endif
 	{
 		.label		= "default service",
 		.type		= P_STRING,
@@ -4965,6 +5030,15 @@
 FN_GLOBAL_STRING(lp_utmpdir, &Globals.szUtmpDir)
 FN_GLOBAL_STRING(lp_wtmpdir, &Globals.szWtmpDir)
 FN_GLOBAL_BOOL(lp_utmp, &Globals.bUtmp)
+#ifdef mysqllogging
+FN_GLOBAL_BOOL(lp_mysqllogging, &Globals.bMySQLSessionLogging)
+FN_GLOBAL_STRING(lp_mysqlloggingserver, &Globals.szMySQLSessionLoggingserver)
+FN_GLOBAL_INTEGER(lp_mysqlloggingport, &Globals.iMySQLSessionLoggingport)
+FN_GLOBAL_STRING(lp_mysqlloggingdb, &Globals.szMySQLSessionLoggingdb)
+FN_GLOBAL_STRING(lp_mysqlloggingtable, &Globals.szMySQLSessionLoggingtable)
+FN_GLOBAL_STRING(lp_mysqlloggingusername, &Globals.szMySQLSessionLoggingusername)
+FN_GLOBAL_STRING(lp_mysqlloggingpw, &Globals.szMySQLSessionLoggingpw)
+#endif
 FN_GLOBAL_STRING(lp_rootdir, &Globals.szRootdir)
 FN_GLOBAL_STRING(lp_perfcount_module, &Globals.szSMBPerfcountModule)
 FN_GLOBAL_STRING(lp_defaultservice, &Globals.szDefaultService)
diff -Nur samba-3.0.11-orig/source3/smbd/session.c samba-3.0.11/source3/smbd/session.c
--- samba-3.0.11-orig/source3/smbd/session.c	2004-12-15 15:33:17.000000000 +0100
+++ samba-3.0.11/source3/smbd/session.c	2005-02-06 12:53:12.000000000 +0100
@@ -27,6 +27,31 @@
 
 #include "includes.h"
 #include "smbd/globals.h"
+#ifdef mysqllogging
+#include "mysql.h"
+
+char *sql_escape_string(TALLOC_CTX *mem_ctx, const char *unesc)
+{
+	char *esc = talloc_array(mem_ctx, char, strlen(unesc) * 2 + 3);
+	size_t pos_unesc = 0, pos_esc = 0;
+
+	for(pos_unesc = 0; unesc[pos_unesc]; pos_unesc++) {
+		switch(unesc[pos_unesc]) {
+		case '\\':
+		case '\'':
+		case '"':
+			esc[pos_esc] = '\\'; pos_esc++;
+		default:
+			esc[pos_esc] = unesc[pos_unesc]; pos_esc++;
+			break;
+		}
+	}
+
+	esc[pos_esc] = '\0';
+	
+	return esc;
+}
+#endif
 
 /********************************************************************
 ********************************************************************/
@@ -151,6 +152,33 @@
 			       sessionid.id_str, sessionid.id_num);
 	}
 
+#ifdef mysqllogging
+	if (lp_mysqllogging()) {
+		MYSQL *handle;
+		handle = mysql_init(NULL);
+		if (handle) {
+			if (mysql_real_connect(handle,
+				lp_mysqlloggingserver(),
+				lp_mysqlloggingusername(),
+				lp_mysqlloggingpw(),
+				lp_mysqlloggingdb(),
+				lp_mysqlloggingport(), 
+				NULL, 0)) {
+					char *query;
+					int ret;
+					asprintf(&query,"INSERT into %s set username='%s',host='%s',sessionid='%s',intime=NOW();",lp_mysqlloggingtable(),sql_escape_string(NULL,sessionid.username),sql_escape_string(NULL,sessionid.hostname),sql_escape_string(NULL,sessionid.id_str));
+					ret = mysql_query(handle, query);
+					SAFE_FREE(query);
+					if (ret) { DEBUG(0,("Error executing MySQL query %s\n", mysql_error(handle))); }
+					mysql_close(handle);
+			} else {
+				DEBUG(0,("Failed to connect to mysql database: error: %s\n", mysql_error(handle)));
+				mysql_close(handle);
+			}
+		} else { DEBUG(0, ("Failed to connect to audit-MySQL-server\n")); }
+	}
+#endif
+
 	vuser->session_keystr = talloc_strdup(vuser, keystr);
 	if (!vuser->session_keystr) {
 		DEBUG(0, ("session_claim:  talloc_strdup() failed for session_keystr\n"));
@@ -195,6 +219,33 @@
			       sessionid.id_str, sessionid.id_num);
 	}
 
+#ifdef mysqllogging
+	if (lp_mysqllogging()) {
+		MYSQL *handle;
+		handle = mysql_init(NULL);
+		if (handle) {
+			if (mysql_real_connect(handle,
+				lp_mysqlloggingserver(),
+				lp_mysqlloggingusername(),
+				lp_mysqlloggingpw(),
+				lp_mysqlloggingdb(),
+				lp_mysqlloggingport(), 
+				NULL, 0)) {
+					char *query;
+					int ret;
+					asprintf(&query,"UPDATE %s set outtime=NOW() WHERE username='%s' and host='%s' and sessionid='%s' and outtime = 0 LIMIT 1;",lp_mysqlloggingtable(),sql_escape_string(NULL,sessionid.username),sql_escape_string(NULL,sessionid.hostname),sql_escape_string(NULL,sessionid.id_str));
+					ret = mysql_query(handle, query);
+					SAFE_FREE(query);
+					if (ret) { DEBUG(0,("Error executing MySQL query %s\n", mysql_error(handle))); }
+					mysql_close(handle);
+			} else {
+				DEBUG(0,("Failed to connect to mysql database: error: %s\n", mysql_error(handle)));
+				mysql_close(handle);
+			}
+		} else { DEBUG(0, ("Failed to connect to audit-MySQL-server\n")); }
+	}
+#endif
+
 	smb_pam_close_session(sessionid.username, sessionid.id_str,
 			      sessionid.hostname);
 
